OSDN Git Service

LinGui: mingw doesn't have lockf, ifdef out queue file locking for mingw
[handbrake-jp/handbrake-jp-git.git] / gtk / src / presets.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * presets.c
4  * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
5  * 
6  * presets.c is free software.
7  * 
8  * You may redistribute it and/or modify it under the terms of the
9  * GNU General Public License, as published by the Free Software
10  * Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  * 
13  */
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <glib.h>
19 #include <glib-object.h>
20 #include <glib/gstdio.h>
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include "hb.h"
24 #include "settings.h"
25 #include "callbacks.h"
26 #include "audiohandler.h"
27 #include "subtitlehandler.h"
28 #include "hb-backend.h"
29 #include "plist.h"
30 #include "resources.h"
31 #include "presets.h"
32 #include "values.h"
33 #include "lang.h"
34
35 #define MAX_NESTED_PRESET 3
36
37 enum
38 {
39         PRESETS_BUILTIN = 0,
40         PRESETS_CUSTOM
41 };
42
43 static GValue *presetsPlist = NULL;
44 static GValue *internalPlist = NULL;
45 static GValue *prefsPlist = NULL;
46 static gboolean prefs_modified = FALSE;
47
48 static const GValue* preset_dict_get_value(GValue *dict, const gchar *key);
49 static void store_plist(GValue *plist, const gchar *name);
50 static void store_presets(void);
51 static void store_prefs(void);
52
53 gint
54 preset_path_cmp(gint *indices1, gint len1, gint *indices2, gint len2)
55 {
56         gint ii;
57         for (ii = 0; ii < len1 && ii < len2; ii++)
58         {
59                 if (indices1[ii] != indices2[ii])
60                         return indices1[ii] - indices2[ii];
61         }
62         return len1 - len2;
63 }
64
65 // This only handle limited depth
66 GtkTreePath*
67 ghb_tree_path_new_from_indices(gint *indices, gint len)
68 {
69         switch (len)
70         {
71                 case 1:
72                         return gtk_tree_path_new_from_indices(
73                                 indices[0], -1);
74                 case 2:
75                         return gtk_tree_path_new_from_indices(
76                                 indices[0], indices[1], -1);
77                 case 3:
78                         return gtk_tree_path_new_from_indices(
79                                 indices[0], indices[1], indices[2], -1);
80                 case 4:
81                         return gtk_tree_path_new_from_indices(
82                                 indices[0], indices[1], indices[2], indices[3], -1);
83                 case 5:
84                         return gtk_tree_path_new_from_indices(
85                                 indices[0], indices[1], indices[2], indices[3], indices[4], -1);
86                 default:
87                         return NULL;
88         }
89 }
90
91 GValue*
92 ghb_parse_preset_path(const gchar *path)
93 {
94         gchar **split;
95         GValue *preset;
96         gint ii;
97
98         preset = ghb_array_value_new(MAX_NESTED_PRESET);
99         split = g_strsplit(path, "#", MAX_NESTED_PRESET);
100         for (ii = 0; split[ii] != NULL; ii++)
101         {
102                 ghb_array_append(preset, ghb_string_value_new(split[ii]));
103         }
104         g_strfreev(split);
105         return preset;
106 }
107
108 static GValue*
109 preset_path_from_indices(GValue *presets, gint *indices, gint len)
110 {
111         gint ii;
112         GValue *path;
113
114         g_debug("preset_path_from_indices");
115         path = ghb_array_value_new(MAX_NESTED_PRESET);
116         for (ii = 0; ii < len; ii++)
117         {
118                 GValue *dict;
119                 gint count, folder;
120                 const GValue *name;
121
122                 count = ghb_array_len(presets);
123                 if (indices[ii] >= count) break;
124                 dict = ghb_array_get_nth(presets, indices[ii]);
125                 name = ghb_dict_lookup(dict, "PresetName");
126                 if (name)
127                         ghb_array_append(path, ghb_value_dup(name));
128                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
129                 if (!folder)
130                         break;
131                 presets = ghb_dict_lookup(dict, "ChildrenArray");
132         }
133         return path;
134 }
135
136 gchar*
137 ghb_preset_path_string(const GValue *path)
138 {
139         gint count, ii;
140         GString *gstr;
141         GValue *val;
142         gchar *str;
143
144         gstr = g_string_new("");
145         if (path != NULL)
146         {
147                 count = ghb_array_len(path);
148                 for (ii = 0; ii < count; ii++)
149                 {
150                         val = ghb_array_get_nth(path, ii);
151                         str = ghb_value_string(val);
152                         g_string_append(gstr, str);
153                         if (ii < count-1)
154                                 g_string_append(gstr, "->");
155                         g_free(str);
156                 }
157         }
158         str = g_string_free(gstr, FALSE);
159         return str;
160 }
161
162 void
163 dump_preset_path(const gchar *msg, const GValue *path)
164 {
165         gchar *str;
166
167         if (path)
168                 debug_show_type (G_VALUE_TYPE(path));
169         str = ghb_preset_path_string(path);
170         g_message("%s path: (%s)", msg, str);
171         g_free(str);
172 }
173
174 void
175 dump_preset_indices(const gchar *msg, gint *indices, gint len)
176 {
177         gint ii;
178
179         g_message("%s indices: len %d", msg, len);
180         for (ii = 0; ii < len; ii++)
181         {
182                 printf("%d ", indices[ii]);
183         }
184         printf("\n");
185 }
186
187 #if 0
188 static gint
189 preset_path_cmp(const GValue *path1, const GValue *path2)
190 {
191         gint count, ii;
192         GValue *val;
193         gchar *str1, *str2;
194         gint result;
195
196         count = ghb_array_len(path1);
197         ii = ghb_array_len(path2);
198         if (ii != count)
199                 return ii - count;
200         for (ii = 0; ii < count; ii++)
201         {
202                 val = ghb_array_get_nth(path1, ii);
203                 str1 = ghb_value_string(val);
204                 val = ghb_array_get_nth(path2, ii);
205                 str2 = ghb_value_string(val);
206                 result = strcmp(str1, str2);
207                 if (result != 0)
208                         return result;
209                 g_free(str1);
210                 g_free(str2);
211         }
212         return 0;
213 }
214 #endif
215
216 static GValue*
217 presets_get_dict(GValue *presets, gint *indices, gint len)
218 {
219         gint ii, count, folder;
220         GValue *dict = NULL;
221
222         g_debug("presets_get_dict ()");
223         for (ii = 0; ii < len; ii++)
224         {
225                 count = ghb_array_len(presets);
226                 if (indices[ii] >= count) return NULL;
227                 dict = ghb_array_get_nth(presets, indices[ii]);
228                 if (ii < len-1)
229                 {
230                         folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
231                         if (!folder)
232                                 return NULL;
233                         presets = ghb_dict_lookup(dict, "ChildrenArray");
234                 }
235         }
236         if (ii < len)
237                 return NULL;
238         return dict;
239 }
240
241 static GValue*
242 presets_get_folder(GValue *presets, gint *indices, gint len)
243 {
244         gint ii, count, folder;
245         GValue *dict;
246
247         g_debug("presets_get_folder ()");
248         for (ii = 0; ii < len; ii++)
249         {
250                 count = ghb_array_len(presets);
251                 if (indices[ii] >= count) return NULL;
252                 dict = ghb_array_get_nth(presets, indices[ii]);
253                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
254                 if (!folder)
255                         break;
256                 presets = ghb_dict_lookup(dict, "ChildrenArray");
257         }
258         if (ii < len)
259                 return NULL;
260         return presets;
261 }
262
263 static GValue*
264 plist_get_dict(GValue *presets, const gchar *name)
265 {
266         if (presets == NULL || name == NULL) return NULL;
267         return ghb_dict_lookup(presets, name);
268 }
269
270 static const gchar*
271 preset_get_name(GValue *dict)
272 {
273         return g_value_get_string(preset_dict_get_value(dict, "PresetName"));
274 }
275
276 static gboolean
277 preset_folder_is_open(GValue *dict)
278 {
279         const GValue *gval;
280
281         gval = preset_dict_get_value(dict, "FolderOpen");
282         if (gval != NULL)
283                 return g_value_get_boolean(gval);
284         return FALSE;
285 }
286
287 gboolean
288 ghb_preset_folder(GValue *dict)
289 {
290         return ghb_value_int(preset_dict_get_value(dict, "Folder"));
291 }
292
293 gint
294 ghb_preset_type(GValue *dict)
295 {
296         return ghb_value_int(preset_dict_get_value(dict, "Type"));
297 }
298
299 static void
300 presets_remove_nth(GValue *presets, gint pos)
301 {
302         GValue *dict;
303         gint count;
304         
305         if (presets == NULL || pos < 0) return;
306         count = ghb_array_len(presets);
307         if (pos >= count) return;
308         dict = ghb_array_get_nth(presets, pos);
309         ghb_array_remove(presets, pos);
310         ghb_value_free(dict);
311 }
312
313 gboolean
314 ghb_presets_remove(
315         GValue *presets, 
316         gint *indices,
317         gint len)
318 {
319         GValue *folder = NULL;
320
321         folder = presets_get_folder(presets, indices, len-1);
322         if (folder)
323                 presets_remove_nth(folder, indices[len-1]);
324         else
325         {
326                 g_warning("ghb_presets_remove (): internal preset lookup error");
327                 return FALSE;
328         }
329         return TRUE;
330 }
331
332 static void
333 ghb_presets_replace(
334         GValue *presets, 
335         GValue *dict,
336         gint *indices,
337         gint len)
338 {
339         GValue *folder = NULL;
340
341         folder = presets_get_folder(presets, indices, len-1);
342         if (folder)
343                 ghb_array_replace(folder, indices[len-1], dict);
344         else
345         {
346                 g_warning("ghb_presets_replace (): internal preset lookup error");
347         }
348 }
349
350 static void
351 ghb_presets_insert(
352         GValue *presets, 
353         GValue *dict,
354         gint *indices,
355         gint len)
356 {
357         GValue *folder = NULL;
358
359         folder = presets_get_folder(presets, indices, len-1);
360         if (folder)
361                 ghb_array_insert(folder, indices[len-1], dict);
362         else
363         {
364                 g_warning("ghb_presets_insert (): internal preset lookup error");
365         }
366 }
367
368 static gint
369 presets_find_element(GValue *presets, const gchar *name)
370 {
371         GValue *dict;
372         gint count, ii;
373         
374         g_debug("presets_find_element () (%s)", name);
375         if (presets == NULL || name == NULL) return -1;
376         count = ghb_array_len(presets);
377         for (ii = 0; ii < count; ii++)
378         {
379                 const gchar *str;
380                 dict = ghb_array_get_nth(presets, ii);
381                 str = preset_get_name(dict);
382                 if (strcmp(name, str) == 0)
383                 {
384                         return ii;
385                 }
386         }
387         return -1;
388 }
389
390 static gint
391 single_find_pos(GValue *presets, const gchar *name, gint type)
392 {
393         GValue *dict;
394         gint count, ii, ptype, last;
395         
396         if (presets == NULL || name == NULL) return -1;
397         last = count = ghb_array_len(presets);
398         for (ii = 0; ii < count; ii++)
399         {
400                 const gchar *str;
401                 dict = ghb_array_get_nth(presets, ii);
402                 str = preset_get_name(dict);
403                 ptype = ghb_value_int(preset_dict_get_value(dict, "Type"));
404                 if (strcasecmp(name, str) <= 0 && ptype == type)
405                 {
406                         return ii;
407                 }
408                 if (ptype == type)
409                         last = ii+1;
410         }
411         return last;
412 }
413
414 static gint*
415 presets_find_pos(const GValue *path, gint type, gint *len)
416 {
417         GValue *nested;
418         GValue *val;
419         gint count, ii;
420         gboolean folder;
421         gint *indices = NULL;
422         const gchar *name;
423         GValue *dict;
424
425         g_debug("presets_find_pos () ");
426         nested = presetsPlist;
427         count = ghb_array_len(path);
428         indices = g_malloc(MAX_NESTED_PRESET * sizeof(gint));
429         for (ii = 0; ii < count-1; ii++)
430         {
431                 val = ghb_array_get_nth(path, ii);
432                 name = g_value_get_string(val);
433                 indices[ii] = presets_find_element(nested, name);
434                 if (indices[ii] == -1) return NULL;
435                 dict = ghb_array_get_nth(nested, indices[ii]);
436                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
437                 nested = NULL;
438                 if (!folder)
439                         break;
440                 nested = ghb_dict_lookup(dict, "ChildrenArray");
441         }
442         if (nested)
443         {
444                 const gchar *name;
445
446                 name = g_value_get_string(ghb_array_get_nth(path, count-1));
447                 indices[ii] = single_find_pos(nested, name, type);
448                 ii++;
449         }
450         *len = ii;
451         return indices;
452 }
453
454 static gint
455 preset_tree_depth(GValue *dict)
456 {
457         gboolean folder;
458
459         folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
460         if (folder)
461         {
462                 gint depth = 0;
463                 gint count, ii;
464                 GValue *presets;
465
466                 presets = ghb_dict_lookup(dict, "ChildrenArray");
467                 count = ghb_array_len(presets);
468                 for (ii = 0; ii < count; ii++)
469                 {
470                         gint tmp;
471
472                         dict = ghb_array_get_nth(presets, ii);
473                         tmp = preset_tree_depth(dict);
474                         depth = MAX(depth, tmp);
475                 }
476                 return depth + 1;
477         }
478         else
479         {
480                 return 1;
481         }
482 }
483
484 static gboolean
485 preset_is_default(GValue *dict)
486 {
487         const GValue *val;
488
489         val = preset_dict_get_value(dict, "Default");
490         return ghb_value_boolean(val);
491 }
492
493 static void
494 presets_clear_default(GValue *presets)
495 {
496         gint count, ii;
497
498         count = ghb_array_len(presets);
499         for (ii = 0; ii < count; ii++)
500         {
501                 GValue *dict;
502                 gboolean folder;
503
504                 dict = ghb_array_get_nth(presets, ii);
505                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
506                 if (folder)
507                 {
508                         GValue *nested;
509
510                         nested = ghb_dict_lookup(dict, "ChildrenArray");
511                         presets_clear_default(nested);
512                 }
513                 else
514                 {
515                         if (preset_is_default(dict))
516                         {
517                                 ghb_dict_insert(dict, g_strdup("Default"), 
518                                                                 ghb_boolean_value_new(FALSE));
519                         }
520                 }
521         }
522 }
523
524 static void
525 presets_customize(GValue *presets)
526 {
527         gint count, ii;
528
529         count = ghb_array_len(presets);
530         for (ii = 0; ii < count; ii++)
531         {
532                 GValue *dict;
533                 gboolean folder;
534                 gint ptype;
535
536                 dict = ghb_array_get_nth(presets, ii);
537
538                 ptype = ghb_value_int(preset_dict_get_value(dict, "Type"));
539                 if (ptype != PRESETS_CUSTOM)
540                 {
541                         ghb_dict_insert(dict, g_strdup("Type"), 
542                                                 ghb_int64_value_new(PRESETS_CUSTOM));
543                 }
544                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
545                 if (folder)
546                 {
547                         GValue *nested;
548
549                         nested = ghb_dict_lookup(dict, "ChildrenArray");
550                         presets_customize(nested);
551                 }
552         }
553 }
554
555 static gint*
556 presets_find_default2(GValue *presets, gint *len)
557 {
558         gint count, ii;
559         gint *indices;
560
561         count = ghb_array_len(presets);
562         for (ii = 0; ii < count; ii++)
563         {
564                 GValue *dict;
565                 gboolean folder;
566
567                 dict = ghb_array_get_nth(presets, ii);
568                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
569                 if (folder)
570                 {
571                         GValue *nested;
572                         gint pos = *len;
573
574                         nested = ghb_dict_lookup(dict, "ChildrenArray");
575                         (*len)++;
576                         indices = presets_find_default2(nested, len);
577                         if (indices)
578                         {
579                                 indices[pos] = ii;
580                                 return indices;
581                         }
582                         else
583                                 *len = pos;
584                 }
585                 else
586                 {
587                         if (preset_is_default(dict))
588                         {
589                                 indices = g_malloc(MAX_NESTED_PRESET * sizeof(gint));
590                                 indices[*len] = ii;
591                                 (*len)++;
592                                 return indices;
593                         }
594                 }
595         }
596         return NULL;
597 }
598
599 static gint*
600 presets_find_default(GValue *presets, gint *len)
601 {
602         *len = 0;
603         return presets_find_default2(presets, len);
604 }
605
606 gint*
607 ghb_preset_indices_from_path(
608         GValue *presets, 
609         const GValue *path,
610         gint *len)
611 {
612         GValue *nested;
613         GValue *val;
614         gint count, ii;
615         gint *indices = NULL;
616         const gchar *name;
617         GValue *dict;
618         gboolean folder;
619
620         g_debug("ghb_preset_indices_from_path () ");
621         nested = presets;
622         count = ghb_array_len(path);
623         if (count)
624                 indices = g_malloc(MAX_NESTED_PRESET * sizeof(gint));
625         *len = 0;
626         for (ii = 0; ii < count; ii++)
627         {
628                 val = ghb_array_get_nth(path, ii);
629                 name = g_value_get_string(val);
630                 indices[ii] = presets_find_element(nested, name);
631                 if (indices[ii] == -1)
632                 {
633                         g_free(indices);
634                         return NULL;
635                 }
636                 if (ii < count-1)
637                 {
638                         dict = ghb_array_get_nth(nested, indices[ii]);
639                         folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
640                         if (!folder)
641                         {
642                                 g_free(indices);
643                                 return NULL;
644                         }
645                         nested = ghb_dict_lookup(dict, "ChildrenArray");
646                 }
647         }
648         *len = ii;
649         return indices;
650 }
651
652 static gint
653 ghb_presets_get_type(
654         GValue *presets, 
655         gint *indices,
656         gint len)
657 {
658         GValue *dict;
659         gint type = 0;
660
661         dict = presets_get_dict(presets, indices, len);
662         if (dict)
663         {
664                 type = ghb_preset_type(dict);
665         }
666         else
667         {
668                 g_warning("ghb_presets_get_type (): internal preset lookup error");
669         }
670         return type;
671 }
672
673 static gboolean
674 ghb_presets_get_folder(
675         GValue *presets, 
676         gint *indices,
677         gint len)
678 {
679         GValue *dict;
680         gboolean folder = FALSE;
681
682         dict = presets_get_dict(presets, indices, len);
683         if (dict)
684         {
685                 folder = ghb_preset_folder(dict);
686         }
687         else
688         {
689                 g_warning("ghb_presets_get_folder (): internal preset lookup error");
690         }
691         return folder;
692 }
693
694 void
695 presets_set_default(gint *indices, gint len)
696 {
697         GValue *dict;
698         
699         g_debug("presets_set_default ()");
700         presets_clear_default(presetsPlist);
701         dict = presets_get_dict(presetsPlist, indices, len);
702         if (dict)
703         {
704                 ghb_dict_insert(dict, g_strdup("Default"), ghb_boolean_value_new(TRUE));
705         }
706         store_presets();
707 }
708
709 static void
710 presets_set_folder_open(gboolean open, gint *indices, gint len)
711 {
712         GValue *dict;
713         
714         g_debug("presets_set_folder_open ()");
715         dict = presets_get_dict(presetsPlist, indices, len);
716         if (dict)
717         {
718                 ghb_dict_insert(dict, g_strdup("FolderOpen"), 
719                                                 ghb_boolean_value_new(open));
720         }
721 }
722
723 // Used for sorting dictionaries.
724 gint
725 key_cmp(gconstpointer a, gconstpointer b)
726 {
727         gchar *stra = (gchar*)a;
728         gchar *strb = (gchar*)b;
729
730         return strcmp(stra, strb);
731 }
732
733 static const GValue*
734 preset_dict_get_value(GValue *dict, const gchar *key)
735 {
736         const GValue *gval = NULL;
737
738         if (dict)
739         {
740                 gval = ghb_dict_lookup(dict, key);
741         }
742         if (internalPlist == NULL) return NULL;
743         if (gval == NULL)
744         {
745                 dict = plist_get_dict(internalPlist, "Presets");
746                 if (dict == NULL) return NULL;
747                 gval = ghb_dict_lookup(dict, key);
748         }
749         return gval;
750 }
751
752 const gchar*
753 ghb_presets_get_description(GValue *pdict)
754 {
755         const gchar *desc;
756
757         if (pdict == NULL) return NULL;
758         desc = g_value_get_string(
759                         preset_dict_get_value(pdict, "PresetDescription"));
760         if (desc[0] == 0) return NULL;
761         return desc;
762 }
763
764
765 static void init_settings_from_dict(
766         GValue *dest, GValue *internal, GValue *dict);
767
768 static void
769 init_settings_from_array(
770         GValue *dest, 
771         GValue *internal,
772         GValue *array)
773 {
774         GValue *gval, *val;
775         gint count, ii;
776         
777         count = ghb_array_len(array);
778         // The first element of the internal version is always the 
779         // template for the allowed values
780         gval = ghb_array_get_nth(internal, 0);
781         for (ii = 0; ii < count; ii++)
782         {
783                 val = NULL;
784                 val = ghb_array_get_nth(array, ii);
785                 if (val == NULL)
786                         val = gval;
787                 if (G_VALUE_TYPE(gval) == ghb_dict_get_type())
788                 {
789                         GValue *new_dict;
790                         new_dict = ghb_dict_value_new();
791                         ghb_array_append(dest, new_dict);
792                         if (G_VALUE_TYPE(val) == ghb_dict_get_type())
793                                 init_settings_from_dict(new_dict, gval, val);
794                         else
795                                 init_settings_from_dict(new_dict, gval, gval);
796                 }
797                 else if (G_VALUE_TYPE(gval) == ghb_array_get_type())
798                 {
799                         GValue *new_array;
800                         new_array = ghb_array_value_new(8);
801                         ghb_array_append(dest, new_array);
802                         if (G_VALUE_TYPE(val) == ghb_array_get_type())
803                                 init_settings_from_array(new_array, gval, val);
804                         else
805                                 init_settings_from_array(new_array, gval, gval);
806                 }
807                 else
808                 {
809                         ghb_array_append(dest, val);
810                 }
811         }
812 }
813
814 static void
815 init_settings_from_dict(
816         GValue *dest, 
817         GValue *internal,
818         GValue *dict)
819 {
820         GHashTableIter iter;
821         gchar *key;
822         GValue *gval, *val;
823         
824         ghb_dict_iter_init(&iter, internal);
825         // middle (void*) cast prevents gcc warning "defreferencing type-punned
826         // pointer will break strict-aliasing rules"
827         while (g_hash_table_iter_next(
828                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
829         {
830                 val = NULL;
831                 if (dict)
832                         val = ghb_dict_lookup(dict, key);
833                 if (val == NULL)
834                         val = gval;
835                 if (G_VALUE_TYPE(gval) == ghb_dict_get_type())
836                 {
837                         GValue *new_dict;
838                         new_dict = ghb_dict_value_new();
839                         ghb_settings_take_value(dest, key, new_dict);
840                         if (G_VALUE_TYPE(val) == ghb_dict_get_type())
841                                 init_settings_from_dict(new_dict, gval, val);
842                         else
843                                 init_settings_from_dict(new_dict, gval, gval);
844                 }
845                 else if (G_VALUE_TYPE(gval) == ghb_array_get_type())
846                 {
847                         GValue *new_array;
848                         new_array = ghb_array_value_new(8);
849                         ghb_settings_take_value(dest, key, new_array);
850                         if (G_VALUE_TYPE(val) == ghb_array_get_type())
851                                 init_settings_from_array(new_array, gval, val);
852                         else
853                                 init_settings_from_array(new_array, gval, gval);
854         
855                 }
856                 else
857                 {
858                         ghb_settings_set_value(dest, key, val);
859                 }
860         }
861 }
862
863 void
864 init_ui_from_dict(
865         signal_user_data_t *ud, 
866         GValue *internal,
867         GValue *dict)
868 {
869         GHashTableIter iter;
870         gchar *key;
871         GValue *gval, *val;
872         
873         ghb_dict_iter_init(&iter, internal);
874         // middle (void*) cast prevents gcc warning "defreferencing type-punned
875         // pointer will break strict-aliasing rules"
876         while (g_hash_table_iter_next(
877                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
878         {
879                 val = NULL;
880                 if (dict)
881                         val = ghb_dict_lookup(dict, key);
882                 if (val == NULL)
883                         val = gval;
884                 ghb_ui_update(ud, key, val);
885         }
886 }
887
888 static void
889 preset_to_ui(signal_user_data_t *ud, GValue *dict)
890 {
891         g_debug("preset_to_ui()\n");
892         // Initialize the ui from presets file.
893         GValue *internal, *hidden;
894
895         // Get key list from internal default presets.  This way we do not
896         // load any unknown keys.
897         if (internalPlist == NULL) return;
898         internal = plist_get_dict(internalPlist, "Presets");
899         hidden = plist_get_dict(internalPlist, "XlatPresets");
900         // Setting a ui widget will cause the corresponding setting
901         // to be set, but it also triggers a callback that can 
902         // have the side effect of using other settings values
903         // that have not yet been set.  So set *all* settings first
904         // then update the ui.
905         init_settings_from_dict(ud->settings, internal, dict);
906         init_settings_from_dict(ud->settings, hidden, dict);
907         init_ui_from_dict(ud, internal, dict);
908         init_ui_from_dict(ud, hidden, dict);
909 }
910
911 void
912 ghb_settings_to_ui(signal_user_data_t *ud, GValue *dict)
913 {
914         init_ui_from_dict(ud, dict, dict);
915 }
916
917 static GValue *current_preset = NULL;
918
919 gboolean
920 ghb_preset_is_custom()
921 {
922         const GValue *val;
923
924         if (current_preset == NULL) return FALSE;
925         val = preset_dict_get_value(current_preset, "Type");
926         return (ghb_value_int(val) == 1);
927 }
928
929 void
930 ghb_set_preset_from_indices(signal_user_data_t *ud, gint *indices, gint len)
931 {
932         GValue *dict = NULL;
933         gint fallback[2] = {0, -1};
934
935         if (indices)
936                 dict = presets_get_dict(presetsPlist, indices, len);
937         if (dict == NULL)
938         {
939                 indices = fallback;
940                 len = 1;
941                 dict = presets_get_dict(presetsPlist, indices, len);
942         }
943         if (dict == NULL)
944         {
945                 preset_to_ui(ud, NULL);
946                 current_preset = NULL;
947         }
948         else
949         {
950                 GValue *path;
951                 gboolean folder;
952
953                 current_preset = dict;
954                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
955                 if (folder)
956                         preset_to_ui(ud, NULL);
957                 else
958                         preset_to_ui(ud, dict);
959                 path = preset_path_from_indices(presetsPlist, indices, len);
960                 ghb_settings_set_value(ud->settings, "preset", path);
961                 ghb_value_free(path);
962         }
963 }
964
965 static const GValue*
966 curr_preset_get_value(const gchar *key)
967 {
968         if (current_preset == NULL) return NULL;
969         return preset_dict_get_value(current_preset, key);
970 }
971
972 void
973 ghb_update_from_preset(
974         signal_user_data_t *ud, 
975         const gchar *key)
976 {
977         const GValue *gval;
978         
979         g_debug("ghb_update_from_preset() %s", key);
980         gval = curr_preset_get_value(key);
981         if (gval != NULL)
982         {
983                 ghb_ui_update(ud, key, gval);
984         }
985 }
986
987 static void
988 ghb_select_preset2(
989         GtkBuilder *builder, 
990         gint *indices, 
991         gint len)
992 {
993         GtkTreeView *treeview;
994         GtkTreeSelection *selection;
995         GtkTreeModel *store;
996         GtkTreeIter iter;
997         GtkTreePath *path;
998         
999         g_debug("ghb_select_preset2()");
1000         treeview = GTK_TREE_VIEW(GHB_WIDGET(builder, "presets_list"));
1001         selection = gtk_tree_view_get_selection (treeview);
1002         store = gtk_tree_view_get_model (treeview);
1003         path = ghb_tree_path_new_from_indices(indices, len);
1004         if (path)
1005         {
1006                 if (gtk_tree_model_get_iter(store, &iter, path))
1007                 {
1008                         gtk_tree_selection_select_iter (selection, &iter);
1009                 }
1010                 else
1011                 {
1012                         if (gtk_tree_model_get_iter_first(store, &iter))
1013                                 gtk_tree_selection_select_iter (selection, &iter);
1014                 }
1015                 gtk_tree_path_free(path);
1016         }
1017 }
1018
1019 void
1020 ghb_select_preset(GtkBuilder *builder, const GValue *path)
1021 {
1022         gint *indices, len;
1023
1024         g_debug("ghb_select_preset()");
1025         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
1026         if (indices)
1027         {
1028                 ghb_select_preset2(builder, indices, len);
1029                 g_free(indices);
1030         }
1031 }
1032
1033 void
1034 ghb_select_default_preset(GtkBuilder *builder)
1035 {
1036         gint *indices, len;
1037
1038         g_debug("ghb_select_default_preset()");
1039         indices = presets_find_default(presetsPlist, &len);
1040         if (indices)
1041         {
1042                 ghb_select_preset2(builder, indices, len);
1043                 g_free(indices);
1044         }
1045 }
1046
1047 gchar*
1048 ghb_get_user_config_dir(gchar *subdir)
1049 {
1050         const gchar *dir;
1051         gchar *config;
1052
1053         dir = g_get_user_config_dir();
1054         if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
1055         {
1056                 dir = g_get_home_dir();
1057                 config = g_strdup_printf ("%s/.ghb", dir);
1058                 if (!g_file_test(config, G_FILE_TEST_IS_DIR))
1059                         g_mkdir (config, 0755);
1060         }
1061         else
1062         {
1063                 config = g_strdup_printf ("%s/ghb", dir);
1064                 if (!g_file_test(config, G_FILE_TEST_IS_DIR))
1065                         g_mkdir (config, 0755);
1066         }
1067         if (subdir)
1068         {
1069                 gchar **split;
1070                 gint ii;
1071
1072                 split = g_strsplit(subdir, G_DIR_SEPARATOR_S, -1);
1073                 for (ii = 0; split[ii] != NULL; ii++)
1074                 {
1075                         gchar *tmp;
1076
1077                         tmp = g_strdup_printf ("%s/%s", config, split[ii]);
1078                         g_free(config);
1079                         config = tmp;
1080                         if (!g_file_test(config, G_FILE_TEST_IS_DIR))
1081                                 g_mkdir (config, 0755);
1082                 }
1083         }
1084         return config;
1085 }
1086
1087 static void
1088 store_plist(GValue *plist, const gchar *name)
1089 {
1090         gchar *config, *path;
1091         FILE *file;
1092
1093         config = ghb_get_user_config_dir(NULL);
1094         path = g_strdup_printf ("%s/%s", config, name);
1095         file = g_fopen(path, "w");
1096         g_free(config);
1097         g_free(path);
1098         ghb_plist_write(file, plist);
1099         fclose(file);
1100 }
1101
1102 static GValue*
1103 load_plist(const gchar *name)
1104 {
1105         gchar *config, *path;
1106         GValue *plist = NULL;
1107
1108         config = ghb_get_user_config_dir(NULL);
1109         path = g_strdup_printf ("%s/%s", config, name);
1110         if (g_file_test(path, G_FILE_TEST_IS_REGULAR))
1111         {
1112                 plist = ghb_plist_parse_file(path);
1113         }
1114         g_free(config);
1115         g_free(path);
1116         return plist;
1117 }
1118
1119 gboolean
1120 ghb_lock_file(const gchar *name)
1121 {
1122 #if !defined(_WIN32)
1123         gchar *config, *path;
1124         int fd, lock = 0;
1125
1126         config = ghb_get_user_config_dir(NULL);
1127         path = g_strdup_printf ("%s/%s", config, name);
1128         fd = open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
1129         if (fd >= 0)
1130                 lock = lockf(fd, F_TLOCK, 0);
1131         if (lock)
1132                 close(fd);
1133         g_free(config);
1134         g_free(path);
1135         return !lock;
1136 #else
1137         return 1;
1138 #endif
1139 }
1140
1141 static void
1142 remove_plist(const gchar *name)
1143 {
1144         gchar *config, *path;
1145
1146         config = ghb_get_user_config_dir(NULL);
1147         path = g_strdup_printf ("%s/%s", config, name);
1148         if (g_file_test(path, G_FILE_TEST_IS_REGULAR))
1149         {
1150                 g_unlink(path);
1151         }
1152         g_free(path);
1153         g_free(config);
1154 }
1155
1156 static gboolean prefs_initializing = FALSE;
1157
1158 void
1159 ghb_prefs_to_ui(signal_user_data_t *ud)
1160 {
1161         const GValue *gval;
1162         gchar *key;
1163         gchar *str;
1164         GValue *internal, *dict;
1165         GHashTableIter iter;
1166         
1167
1168         g_debug("ghb_prefs_to_ui");
1169         prefs_initializing = TRUE;
1170
1171         // Setting a ui widget will cause the corresponding setting
1172         // to be set, but it also triggers a callback that can 
1173         // have the side effect of using other settings values
1174         // that have not yet been set.  So set *all* settings first
1175         // then update the ui.
1176         internal = plist_get_dict(internalPlist, "Initialization");
1177         ghb_dict_iter_init(&iter, internal);
1178         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1179         // pointer will break strict-aliasing rules"
1180         while (g_hash_table_iter_next(
1181                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1182         {
1183                 ghb_ui_update(ud, key, gval);
1184         }
1185
1186         dict = plist_get_dict(prefsPlist, "Preferences");
1187         internal = plist_get_dict(internalPlist, "Preferences");
1188         ghb_dict_iter_init(&iter, internal);
1189         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1190         // pointer will break strict-aliasing rules"
1191         while (g_hash_table_iter_next(
1192                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1193         {
1194                 const GValue *value = NULL;
1195                 if (dict)
1196                         value = ghb_dict_lookup(dict, key);
1197                 if (value == NULL)
1198                         value = gval;
1199                 ghb_settings_set_value(ud->settings, key, value);
1200         }
1201         internal = plist_get_dict(internalPlist, "Preferences");
1202         ghb_dict_iter_init(&iter, internal);
1203         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1204         // pointer will break strict-aliasing rules"
1205         while (g_hash_table_iter_next(
1206                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1207         {
1208                 const GValue *value = NULL;
1209                 if (dict)
1210                         value = ghb_dict_lookup(dict, key);
1211                 if (value == NULL)
1212                         value = gval;
1213                 ghb_ui_update(ud, key, value);
1214         }
1215         const GValue *val;
1216         val = ghb_settings_get_value(ud->settings, "show_presets");
1217         ghb_ui_update(ud, "show_presets", val);
1218         if (ghb_settings_get_boolean(ud->settings, "hbfd_feature"))
1219         {
1220                 GtkAction *action;
1221                 val = ghb_settings_get_value(ud->settings, "hbfd");
1222                 ghb_ui_update(ud, "hbfd", val);
1223                 action = GHB_ACTION (ud->builder, "hbfd");
1224                 gtk_action_set_visible(action, TRUE);
1225         }
1226         else
1227         {
1228                 ghb_ui_update(ud, "hbfd", ghb_int64_value(0));
1229         }
1230         gval = ghb_settings_get_value(ud->settings, "default_source");
1231         ghb_settings_set_value (ud->settings, "source", gval);
1232
1233         str = ghb_settings_get_string(ud->settings, "destination_dir");
1234         ghb_ui_update(ud, "dest_dir", ghb_string_value(str));
1235
1236         gchar *file = g_strdup_printf ("new_video.mp4");
1237         ghb_ui_update(ud, "dest_file", ghb_string_value(file));
1238         g_free(str);
1239         g_free(file);
1240
1241         prefs_initializing = FALSE;
1242 }
1243
1244 void
1245 ghb_prefs_save(GValue *settings)
1246 {
1247         GValue *dict;
1248         GValue *pref_dict;
1249         GHashTableIter iter;
1250         gchar *key;
1251         const GValue *value;
1252         
1253         if (prefs_initializing) return;
1254         dict = plist_get_dict(internalPlist, "Preferences");
1255         if (dict == NULL) return;
1256         pref_dict = plist_get_dict(prefsPlist, "Preferences");
1257         if (pref_dict == NULL) return;
1258         ghb_dict_iter_init(&iter, dict);
1259         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1260         // pointer will break strict-aliasing rules"
1261         while (g_hash_table_iter_next(
1262                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
1263         {
1264                 value = ghb_settings_get_value(settings, key);
1265                 if (value != NULL)
1266                 {
1267                         ghb_dict_insert(pref_dict, g_strdup(key), ghb_value_dup(value));
1268                 }
1269         }
1270         store_prefs();
1271         prefs_modified = FALSE;
1272 }
1273
1274 void
1275 ghb_pref_set(GValue *settings, const gchar *key)
1276 {
1277         const GValue *value, *value2;
1278         
1279         if (prefs_initializing) return;
1280         value = ghb_settings_get_value(settings, key);
1281         if (value != NULL)
1282         {
1283                 GValue *dict;
1284                 dict = plist_get_dict(prefsPlist, "Preferences");
1285                 if (dict == NULL) return;
1286                 value2 = ghb_dict_lookup(dict, key);
1287                 if (ghb_value_cmp(value, value2) != 0)
1288                 {
1289                         ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
1290                         store_prefs();
1291                         prefs_modified = TRUE;
1292                 }
1293         }
1294 }
1295
1296 void
1297 ghb_pref_save(GValue *settings, const gchar *key)
1298 {
1299         const GValue *value, *value2;
1300         
1301         if (prefs_initializing) return;
1302         value = ghb_settings_get_value(settings, key);
1303         if (value != NULL)
1304         {
1305                 GValue *dict;
1306                 dict = plist_get_dict(prefsPlist, "Preferences");
1307                 if (dict == NULL) return;
1308                 value2 = ghb_dict_lookup(dict, key);
1309                 if (ghb_value_cmp(value, value2) != 0)
1310                 {
1311                         ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
1312                         store_prefs();
1313                         prefs_modified = FALSE;
1314                 }
1315         }
1316 }
1317
1318 void
1319 ghb_prefs_store(void)
1320 {
1321         if (prefs_modified)
1322         {
1323                 store_prefs();
1324                 prefs_modified = FALSE;
1325         }
1326 }
1327
1328 void
1329 ghb_settings_init(signal_user_data_t *ud)
1330 {
1331         GValue *internal;
1332         GHashTableIter iter;
1333         gchar *key;
1334         GValue *gval;
1335
1336
1337         g_debug("ghb_settings_init");
1338         prefs_initializing = TRUE;
1339
1340         internalPlist = ghb_resource_get("internal-defaults");
1341         // Setting a ui widget will cause the corresponding setting
1342         // to be set, but it also triggers a callback that can 
1343         // have the side effect of using other settings values
1344         // that have not yet been set.  So set *all* settings first
1345         // then update the ui.
1346         internal = plist_get_dict(internalPlist, "Initialization");
1347         ghb_dict_iter_init(&iter, internal);
1348         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1349         // pointer will break strict-aliasing rules"
1350         while (g_hash_table_iter_next(
1351                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1352         {
1353                 ghb_settings_set_value(ud->settings, key, gval);
1354         }
1355
1356         internal = plist_get_dict(internalPlist, "Presets");
1357         ghb_dict_iter_init(&iter, internal);
1358         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1359         // pointer will break strict-aliasing rules"
1360         while (g_hash_table_iter_next(
1361                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1362         {
1363                 ghb_settings_set_value(ud->settings, key, gval);
1364         }
1365
1366         internal = plist_get_dict(internalPlist, "Preferences");
1367         ghb_dict_iter_init(&iter, internal);
1368         // middle (void*) cast prevents gcc warning "defreferencing type-punned
1369         // pointer will break strict-aliasing rules"
1370         while (g_hash_table_iter_next(
1371                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1372         {
1373                 ghb_settings_set_value(ud->settings, key, gval);
1374         }
1375         prefs_initializing = FALSE;
1376 }
1377
1378 void
1379 ghb_settings_close()
1380 {
1381         if (internalPlist)
1382                 ghb_value_free(internalPlist);
1383         if (presetsPlist)
1384                 ghb_value_free(presetsPlist);
1385         if (prefsPlist)
1386                 ghb_value_free(prefsPlist);
1387 }
1388
1389 #if defined(_WIN32)
1390 gchar*
1391 FindFirstCDROM(void)
1392 {
1393         gint ii, drives;
1394         gchar drive[5];
1395
1396         strcpy(drive, "A:" G_DIR_SEPARATOR_S);
1397         drives = GetLogicalDrives();
1398         for (ii = 0; ii < 26; ii++)
1399         {
1400                 if (drives & 0x01)
1401                 {
1402                         guint dtype;
1403
1404                         drive[0] = 'A' + ii;
1405                         dtype = GetDriveType(drive);
1406                         if (dtype == DRIVE_CDROM)
1407                         {
1408                                 return g_strdup(drive);
1409                         }
1410                 }
1411                 drives >>= 1;
1412         }
1413         return NULL;
1414 }
1415 #endif
1416
1417 void
1418 ghb_prefs_load(signal_user_data_t *ud)
1419 {
1420         GValue *dict, *internal;
1421         GHashTableIter iter;
1422         gchar *key;
1423         GValue *gval, *path;
1424         
1425         g_debug("ghb_prefs_load");
1426         prefsPlist = load_plist("preferences");
1427         if (prefsPlist == NULL)
1428                 prefsPlist = ghb_dict_value_new();
1429         dict = plist_get_dict(prefsPlist, "Preferences");
1430         internal = plist_get_dict(internalPlist, "Preferences");
1431         if (dict == NULL && internal)
1432         {
1433                 dict = ghb_dict_value_new();
1434                 ghb_dict_insert(prefsPlist, g_strdup("Preferences"), dict);
1435
1436                 // Get defaults from internal defaults 
1437                 ghb_dict_iter_init(&iter, internal);
1438                 // middle (void*) cast prevents gcc warning "defreferencing type-punned
1439                 // pointer will break strict-aliasing rules"
1440                 while (g_hash_table_iter_next(
1441                                 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
1442                 {
1443                         ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
1444                 }
1445
1446                 const gchar *dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
1447                 if (dir == NULL)
1448                 {
1449                         dir = ".";
1450                 }
1451                 ghb_dict_insert(dict, 
1452                         g_strdup("ExportDirectory"), ghb_value_dup(ghb_string_value(dir)));
1453
1454                 dir = g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS);
1455                 if (dir == NULL)
1456                 {
1457                         dir = ".";
1458                 }
1459                 ghb_dict_insert(dict, 
1460                         g_strdup("destination_dir"), ghb_value_dup(ghb_string_value(dir)));
1461
1462                 ghb_dict_insert(dict, 
1463                         g_strdup("SrtDir"), ghb_value_dup(ghb_string_value(dir)));
1464 #if defined(_WIN32)
1465                 gchar *source;
1466
1467                 source = FindFirstCDROM();
1468                 if (source == NULL)
1469                 {
1470                         source = g_strdup("C:" G_DIR_SEPARATOR_S);
1471                 }
1472                 ghb_dict_insert(dict, g_strdup("default_source"), 
1473                                                 ghb_value_dup(ghb_string_value(source)));
1474                 g_free(source);
1475 #endif
1476                 store_prefs();
1477         }
1478         // Read legacy default_preset preference and update accordingly
1479         path = ghb_dict_lookup(dict, "default_preset");
1480         if (path)
1481         {
1482                 gint *indices, len;
1483
1484                 if (G_VALUE_TYPE(path) == G_TYPE_STRING)
1485                 {
1486                         GValue *str = path;
1487
1488                         path = ghb_array_value_new(1);
1489                         ghb_array_append(path, ghb_value_dup(str));
1490                         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
1491                         ghb_value_free(path);
1492                 }
1493                 else
1494                         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
1495
1496                 if (indices)
1497                 {
1498                         presets_set_default(indices, len);
1499                         g_free(indices);
1500                 }
1501                 ghb_dict_remove(dict, "default_preset");
1502                 store_prefs();
1503         }
1504 }
1505
1506 static const gchar*
1507 get_preset_color(gint type, gboolean folder)
1508 {
1509         const gchar *color;
1510
1511         if (type == PRESETS_CUSTOM)
1512         {
1513                 color = "DimGray";
1514                 if (folder)
1515                 {
1516                         color = "black";
1517                 }
1518         }
1519         else
1520         {
1521                 color = "blue";
1522                 if (folder)
1523                 {
1524                         color = "Navy";
1525                 }
1526         }
1527         return color;
1528 }
1529
1530 void
1531 ghb_presets_list_init(
1532         signal_user_data_t *ud, 
1533         gint *indices,
1534         gint len)
1535 {
1536         GtkTreeView *treeview;
1537         GtkTreeIter iter, titer, *piter;
1538         
1539         GtkTreeStore *store;
1540         const gchar *preset;
1541         GtkTreePath *parent_path;
1542         const gchar *description;
1543         gboolean def;
1544         gint count, ii;
1545         GValue *dict;
1546         gint *more_indices;
1547         GValue *presets = NULL;
1548         
1549         g_debug("ghb_presets_list_init ()");
1550         more_indices = g_malloc((len+1)*sizeof(gint));
1551         memcpy(more_indices, indices, len*sizeof(gint));
1552         presets = presets_get_folder(presetsPlist, indices, len);
1553         if (presets == NULL)
1554         {
1555                 g_warning("Failed to find parent folder when adding child.");
1556                 return;
1557         }
1558         count = ghb_array_len(presets);
1559         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
1560         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
1561         parent_path = ghb_tree_path_new_from_indices(indices, len);
1562         if (parent_path)
1563         {
1564                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &titer, parent_path);
1565                 piter = &titer;
1566                 gtk_tree_path_free(parent_path);
1567         }
1568         else
1569         {
1570                 piter = NULL;
1571         }
1572         for (ii = 0; ii < count; ii++)
1573         {
1574                 const gchar *color;
1575                 gint type;
1576                 gboolean folder;
1577
1578                 // Additional settings, add row
1579                 dict = ghb_array_get_nth(presets, ii);
1580                 preset = preset_get_name(dict);
1581                 more_indices[len] = ii;
1582                 def = preset_is_default(dict);
1583
1584                 description = ghb_presets_get_description(dict);
1585                 gtk_tree_store_append(store, &iter, piter);
1586                 type = ghb_preset_type(dict);
1587                 folder = ghb_preset_folder(dict);
1588                 color = get_preset_color(type, folder);
1589                 gtk_tree_store_set(store, &iter, 0, preset, 
1590                                                         1, def ? 800 : 400, 
1591                                                         2, def ? 2 : 0,
1592                                                         3, color, 
1593                                                         4, description,
1594                                                         5, type == PRESETS_BUILTIN ? 0 : 1,
1595                                                         -1);
1596                 if (def && piter)
1597                 {
1598                         GtkTreePath *path;
1599                         GtkTreeIter ppiter;
1600
1601                         if (gtk_tree_model_iter_parent(
1602                                 GTK_TREE_MODEL(store), &ppiter, piter))
1603                         {
1604                                 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &ppiter);
1605                                 gtk_tree_view_expand_row(treeview, path, FALSE);
1606                                 gtk_tree_path_free(path);
1607                         }
1608                         path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), piter);
1609                         gtk_tree_view_expand_row(treeview, path, FALSE);
1610                         gtk_tree_path_free(path);
1611                 }
1612                 if (folder)
1613                 {
1614                         ghb_presets_list_init(ud, more_indices, len+1);
1615                         if (preset_folder_is_open(dict))
1616                         {
1617                                 GtkTreePath *path;
1618
1619                                 if (piter != NULL)
1620                                 {
1621                                         path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), piter);
1622                                         gtk_tree_view_expand_row(treeview, path, FALSE);
1623                                         gtk_tree_path_free(path);
1624                                 }
1625                                 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
1626                                 gtk_tree_view_expand_row(treeview, path, FALSE);
1627                                 gtk_tree_path_free(path);
1628                         }
1629                 }
1630         }
1631         g_free(more_indices);
1632 }
1633
1634 static void
1635 presets_list_update_item(
1636         signal_user_data_t *ud, 
1637         gint *indices,
1638         gint len,
1639         gboolean recurse)
1640 {
1641         GtkTreeView *treeview;
1642         GtkTreeStore *store;
1643         GtkTreeIter iter;
1644         GtkTreePath *treepath;
1645         const gchar *name;
1646         const gchar *description;
1647         gint type;
1648         gboolean def, folder;
1649         GValue *dict;
1650         const gchar *color;
1651         
1652         g_debug("presets_list_update_item ()");
1653         dict = presets_get_dict(presetsPlist, indices, len);
1654         if (dict == NULL)
1655                 return;
1656         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
1657         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
1658         treepath = ghb_tree_path_new_from_indices(indices, len);
1659         gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath);
1660         // Additional settings, add row
1661         name = preset_get_name(dict);
1662         def = preset_is_default(dict);
1663
1664         description = ghb_presets_get_description(dict);
1665         type = ghb_preset_type(dict);
1666         folder = ghb_preset_folder(dict);
1667         color = get_preset_color(type, folder);
1668         gtk_tree_store_set(store, &iter, 0, name, 
1669                                                 1, def ? 800 : 400, 
1670                                                 2, def ? 2 : 0,
1671                                                 3, color,
1672                                                 4, description,
1673                                                 5, type == PRESETS_BUILTIN ? 0 : 1,
1674                                                 -1);
1675         if (recurse && folder)
1676         {
1677                 ghb_presets_list_init(ud, indices, len);
1678         }
1679 }
1680
1681 static void
1682 presets_list_insert(
1683         signal_user_data_t *ud, 
1684         gint *indices,
1685         gint len)
1686 {
1687         GtkTreeView *treeview;
1688         GtkTreeIter iter, titer, *piter;
1689         GtkTreeStore *store;
1690         const gchar *preset;
1691         const gchar *description;
1692         gint type;
1693         gboolean def, folder;
1694         gint count;
1695         GValue *presets;
1696         GtkTreePath *parent_path;
1697         GValue *dict;
1698         const gchar *color;
1699         
1700         g_debug("presets_list_insert ()");
1701         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
1702         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
1703         presets = presets_get_folder(presetsPlist, indices, len-1);
1704         if (presets == NULL)
1705         {
1706                 g_warning("Failed to find parent folder while adding child.");
1707                 return;
1708         }
1709         parent_path = ghb_tree_path_new_from_indices(indices, len-1);
1710         if (parent_path)
1711         {
1712                 gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &titer, parent_path);
1713                 piter = &titer;
1714                 gtk_tree_path_free(parent_path);
1715         }
1716         else
1717         {
1718                 piter = NULL;
1719         }
1720         count = ghb_array_len(presets);
1721         if (indices[len-1] >= count)
1722                 return;
1723         // Additional settings, add row
1724         dict = ghb_array_get_nth(presets, indices[len-1]);
1725         preset = preset_get_name(dict);
1726         def = preset_is_default(dict);
1727
1728         description = ghb_presets_get_description(dict);
1729         gtk_tree_store_insert(store, &iter, piter, indices[len-1]);
1730         type = ghb_preset_type(dict);
1731         folder = ghb_preset_folder(dict);
1732         color = get_preset_color(type, folder);
1733         gtk_tree_store_set(store, &iter, 0, preset, 
1734                                                 1, def ? 800 : 400, 
1735                                                 2, def ? 2 : 0,
1736                                                 3, color,
1737                                                 4, description,
1738                                                 5, type == PRESETS_BUILTIN ? 0 : 1,
1739                                                 -1);
1740         if (folder)
1741         {
1742                 ghb_presets_list_init(ud, indices, len);
1743         }
1744 }
1745
1746 static void
1747 presets_list_remove(
1748         signal_user_data_t *ud, 
1749         gint *indices,
1750         gint len)
1751 {
1752         GtkTreeView *treeview;
1753         GtkTreePath *treepath;
1754         GtkTreeIter iter;
1755         GtkTreeStore *store;
1756         
1757         g_debug("presets_list_remove ()");
1758         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
1759         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
1760         treepath = ghb_tree_path_new_from_indices(indices, len);
1761         if (treepath)
1762         {
1763                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
1764                         gtk_tree_store_remove(store, &iter);
1765                 gtk_tree_path_free(treepath);
1766         }
1767 }
1768
1769 static void
1770 remove_std_presets(signal_user_data_t *ud)
1771 {
1772         gint count, ii;
1773         gint indices = 0;
1774
1775         count = ghb_array_len(presetsPlist);
1776         for (ii = count-1; ii >= 0; ii--)
1777         {
1778                 GValue *dict;
1779                 gint ptype;
1780
1781                 dict = ghb_array_get_nth(presetsPlist, ii);
1782                 ptype = ghb_value_int(preset_dict_get_value(dict, "Type"));
1783                 if (ptype == PRESETS_BUILTIN)
1784                 {
1785                         if (ghb_presets_remove(presetsPlist, &indices, 1))
1786                         {
1787                                 presets_list_remove(ud, &indices, 1);
1788                         }
1789                 }
1790         }
1791 }
1792
1793 void
1794 ghb_save_queue(GValue *queue)
1795 {
1796         store_plist(queue, "queue");
1797 }
1798
1799 GValue*
1800 ghb_load_queue()
1801 {
1802         return load_plist("queue");
1803 }
1804
1805 void
1806 ghb_remove_queue_file()
1807 {
1808         remove_plist("queue");
1809 }
1810
1811 typedef struct
1812 {
1813         gchar *mac_val;
1814         gchar *lin_val;
1815 } value_map_t;
1816
1817 static value_map_t vcodec_xlat[] =
1818 {
1819         {"MPEG-4 (FFmpeg)", "ffmpeg"},
1820         {"MPEG-4 (XviD)", "ffmpeg"},
1821         {"H.264 (x264)", "x264"},
1822         {"VP3 (Theora)", "theora"},
1823         {NULL,NULL}
1824 };
1825
1826 static value_map_t acodec_xlat[] =
1827 {
1828         {"AAC (faac)", "faac"},
1829         {"AAC (CoreAudio)", "faac"},
1830         {"AC3 Passthru", "ac3"},
1831         {"MP3 (lame)", "lame"},
1832         {"Vorbis (vorbis)", "vorbis"},
1833         {NULL,NULL}
1834 };
1835
1836 value_map_t container_xlat[] =
1837 {
1838         {"MP4 file", "mp4"},
1839         {"M4V file", "mp4"},
1840         {"MKV file", "mkv"},
1841         {"AVI file", "mkv"},
1842         {"OGM file", "mkv"},
1843         {NULL, NULL}
1844 };
1845
1846 value_map_t framerate_xlat[] =
1847 {
1848         {"Same as source", "source"},
1849         {"5", "5"},
1850         {"10", "10"},
1851         {"12", "12"},
1852         {"15", "15"},
1853         {"23.976", "23.976"},
1854         {"24", "24"},
1855         {"25", "25"},
1856         {"29.97", "29.97"},
1857         {NULL, NULL}
1858 };
1859
1860 value_map_t samplerate_xlat[] =
1861 {
1862         {"Auto", "source"},
1863         {"22.05", "22.05"},
1864         {"24", "24"},
1865         {"32", "32"},
1866         {"44.1", "44.1"},
1867         {"48", "48"},
1868         {NULL, NULL}
1869 };
1870
1871 value_map_t mix_xlat[] =
1872 {
1873         {"Mono", "mono"},
1874         {"Stereo", "stereo"},
1875         {"Dolby Surround", "dpl1"},
1876         {"Dolby Pro Logic II", "dpl2"},
1877         {"6-channel discrete", "6ch"},
1878         {"AC3 Passthru", "none"},
1879         {NULL, NULL}
1880 };
1881
1882 value_map_t deint_xlat[] =
1883 {
1884         {"0", "off"},
1885         {"1", "custom"},
1886         {"2", "fast"},
1887         {"3", "slow"},
1888         {"4", "slower"},
1889         {NULL, NULL}
1890 };
1891
1892 value_map_t denoise_xlat[] =
1893 {
1894         {"0", "off"},
1895         {"1", "custom"},
1896         {"2", "weak"},
1897         {"3", "medium"},
1898         {"4", "strong"},
1899         {NULL, NULL}
1900 };
1901
1902 value_map_t detel_xlat[] =
1903 {
1904         {"0", "off"},
1905         {"1", "custom"},
1906         {"2", "default"},
1907         {NULL, NULL}
1908 };
1909
1910 value_map_t decomb_xlat[] =
1911 {
1912         {"0", "off"},
1913         {"1", "custom"},
1914         {"2", "default"},
1915         {NULL, NULL}
1916 };
1917
1918 extern iso639_lang_t ghb_language_table[];
1919
1920 static GValue*
1921 export_lang_xlat2(GValue *lin_val)
1922 {
1923         GValue *gval;
1924
1925         if (lin_val == NULL) return NULL;
1926         gint ii;
1927         gchar *str;
1928
1929         str = ghb_value_string(lin_val);
1930         for (ii = 0; ghb_language_table[ii].eng_name; ii++)
1931         {
1932                 if (strcmp(str, ghb_language_table[ii].iso639_2) == 0)
1933                 {
1934                         const gchar *lang;
1935
1936                         if (ghb_language_table[ii].native_name[0] != 0)
1937                                 lang = ghb_language_table[ii].native_name;
1938                         else
1939                                 lang = ghb_language_table[ii].eng_name;
1940
1941                         gval = ghb_string_value_new(lang);
1942                         g_free(str);
1943                         return gval;
1944                 }
1945         }
1946         g_debug("Can't map language value: (%s)", str);
1947         g_free(str);
1948         return NULL;
1949 }
1950
1951 static GValue*
1952 export_subtitle_xlat2(GValue *lin_val)
1953 {
1954         gchar *str;
1955         GValue *gval;
1956
1957         if (lin_val == NULL) return NULL;
1958         str = ghb_value_string(lin_val);
1959         if (strcmp(str, "none") == 0)
1960         {
1961                 gval = ghb_string_value_new("None");
1962         }
1963         else if (strcmp(str, "auto") == 0)
1964         {
1965                 gval = ghb_string_value_new("Autoselect");
1966         }
1967         else
1968         {
1969                 gval = export_lang_xlat2(lin_val);
1970         }
1971         g_free(str);
1972         return gval;
1973 }
1974
1975 static GValue*
1976 import_lang_xlat2(GValue *mac_val)
1977 {
1978         GValue *gval;
1979
1980         if (mac_val == NULL) return NULL;
1981         gint ii;
1982         gchar *str;
1983
1984         str = ghb_value_string(mac_val);
1985         for (ii = 0; ghb_language_table[ii].eng_name; ii++)
1986         {
1987                 if ((strcmp(str, ghb_language_table[ii].eng_name) == 0) ||
1988                         (strcmp(str, ghb_language_table[ii].native_name) == 0))
1989                 {
1990                         gval = ghb_string_value_new(ghb_language_table[ii].iso639_2);
1991                         g_free(str);
1992                         return gval;
1993                 }
1994         }
1995         g_debug("Can't map language value: (%s)", str);
1996         g_free(str);
1997         return NULL;
1998 }
1999
2000 static GValue*
2001 import_subtitle_xlat2(GValue *mac_val)
2002 {
2003         gchar *str;
2004         GValue *gval;
2005
2006         if (mac_val == NULL) return NULL;
2007         str = ghb_value_string(mac_val);
2008         if (strcmp(str, "None") == 0)
2009         {
2010                 gval = ghb_string_value_new("none");
2011         }
2012         else if (strcmp(str, "Autoselect") == 0)
2013         {
2014                 gval = ghb_string_value_new("auto");
2015         }
2016         else
2017         {
2018                 gval = import_lang_xlat2(mac_val);
2019         }
2020         g_free(str);
2021         return gval;
2022 }
2023
2024 static GValue*
2025 export_audio_track_xlat2(GValue *lin_val)
2026 {
2027         gchar *str;
2028         GValue *gval = NULL;
2029
2030         if (lin_val == NULL) return NULL;
2031         str = ghb_value_string(lin_val);
2032         if (strcmp(str, "none") == 0)
2033         {
2034                 gval = ghb_int_value_new(1);
2035         }
2036         else
2037         {
2038                 gint val = ghb_value_int(lin_val) + 1;
2039                 gval = ghb_int_value_new(val);
2040         }
2041         g_free(str);
2042         return gval;
2043 }
2044
2045 static GValue*
2046 import_audio_track_xlat2(GValue *mac_val)
2047 {
2048         gint val;
2049         gchar *str;
2050         GValue *gval;
2051
2052         if (mac_val == NULL) return NULL;
2053         val = ghb_value_int(mac_val);
2054         if (val <= 0)
2055         {
2056                 val = 0;
2057         }
2058         else
2059         {
2060                 val--;
2061         }
2062         str = g_strdup_printf("%d", val);
2063         gval = ghb_string_value_new(str);
2064         g_free(str);
2065         return gval;
2066 }
2067
2068 static GValue*
2069 export_value_xlat2(value_map_t *value_map, GValue *lin_val, GType mac_type)
2070 {
2071         GValue *gval;
2072
2073         if (lin_val == NULL) return NULL;
2074         gint ii;
2075         gchar *str;
2076         GValue *sval;
2077
2078         str = ghb_value_string(lin_val);
2079         for (ii = 0; value_map[ii].mac_val; ii++)
2080         {
2081                 if (strcmp(str, value_map[ii].lin_val) == 0)
2082                 {
2083                         sval = ghb_string_value_new(value_map[ii].mac_val);
2084                         g_free(str);
2085                         gval = ghb_value_new(mac_type);
2086                         if (!g_value_transform(sval, gval))
2087                         {
2088                                 g_warning("can't transform");
2089                                 ghb_value_free(gval);
2090                                 ghb_value_free(sval);
2091                                 return NULL;
2092                         }
2093                         ghb_value_free(sval);
2094                         return gval;
2095                 }
2096         }
2097         g_debug("Can't map value: (%s)", str);
2098         g_free(str);
2099         return NULL;
2100 }
2101
2102 static void
2103 export_value_xlat(GValue *dict)
2104 {
2105         GValue *lin_val, *gval;
2106         const gchar *key;
2107
2108         key = "VideoEncoder";
2109         lin_val = ghb_dict_lookup(dict, key);
2110         gval = export_value_xlat2(vcodec_xlat, lin_val, G_TYPE_STRING);
2111         if (gval)
2112                 ghb_dict_insert(dict, g_strdup(key), gval);
2113         key = "FileFormat";
2114         lin_val = ghb_dict_lookup(dict, key);
2115         gval = export_value_xlat2(container_xlat, lin_val, G_TYPE_STRING);
2116         if (gval)
2117                 ghb_dict_insert(dict, g_strdup(key), gval);
2118         key = "VideoFramerate";
2119         lin_val = ghb_dict_lookup(dict, key);
2120         gval = export_value_xlat2(framerate_xlat, lin_val, G_TYPE_STRING);
2121         if (gval)
2122                 ghb_dict_insert(dict, g_strdup(key), gval);
2123         key = "PictureDetelecine";
2124         lin_val = ghb_dict_lookup(dict, key);
2125         gval = export_value_xlat2(detel_xlat, lin_val, G_TYPE_INT);
2126         if (gval)
2127                 ghb_dict_insert(dict, g_strdup(key), gval);
2128         key = "PictureDecomb";
2129         lin_val = ghb_dict_lookup(dict, key);
2130         gval = export_value_xlat2(decomb_xlat, lin_val, G_TYPE_INT);
2131         if (gval)
2132                 ghb_dict_insert(dict, g_strdup(key), gval);
2133         key = "PictureDeinterlace";
2134         lin_val = ghb_dict_lookup(dict, key);
2135         gval = export_value_xlat2(deint_xlat, lin_val, G_TYPE_INT);
2136         if (gval)
2137                 ghb_dict_insert(dict, g_strdup(key), gval);
2138         key = "PictureDenoise";
2139         lin_val = ghb_dict_lookup(dict, key);
2140         gval = export_value_xlat2(denoise_xlat, lin_val, G_TYPE_INT);
2141         if (gval)
2142                 ghb_dict_insert(dict, g_strdup(key), gval);
2143
2144         GValue *slist;
2145         GValue *sdict;
2146         gint count, ii;
2147
2148         slist = ghb_dict_lookup(dict, "SubtitleList");
2149         count = ghb_array_len(slist);
2150         for (ii = 0; ii < count; ii++)
2151         {
2152                 sdict = ghb_array_get_nth(slist, ii);
2153                 key = "SubtitleLanguage";
2154                 lin_val = ghb_dict_lookup(sdict, key);
2155                 gval = export_subtitle_xlat2(lin_val);
2156                 if (gval)
2157                         ghb_dict_insert(sdict, g_strdup(key), gval);
2158         }
2159
2160         GValue *alist;
2161         GValue *adict;
2162
2163         alist = ghb_dict_lookup(dict, "AudioList");
2164         count = ghb_array_len(alist);
2165         for (ii = 0; ii < count; ii++)
2166         {
2167                 adict = ghb_array_get_nth(alist, ii);
2168                 key = "AudioTrack";
2169                 lin_val = ghb_dict_lookup(adict, key);
2170                 gval = export_audio_track_xlat2(lin_val);
2171                 if (gval)
2172                         ghb_dict_insert(adict, g_strdup(key), gval);
2173                 key = "AudioEncoder";
2174                 lin_val = ghb_dict_lookup(adict, key);
2175                 gval = export_value_xlat2(acodec_xlat, lin_val, G_TYPE_STRING);
2176                 if (gval)
2177                         ghb_dict_insert(adict, g_strdup(key), gval);
2178                 key = "AudioSamplerate";
2179                 lin_val = ghb_dict_lookup(adict, key);
2180                 gval = export_value_xlat2(samplerate_xlat, lin_val, G_TYPE_STRING);
2181                 if (gval)
2182                         ghb_dict_insert(adict, g_strdup(key), gval);
2183                 key = "AudioMixdown";
2184                 lin_val = ghb_dict_lookup(adict, key);
2185                 gval = export_value_xlat2(mix_xlat, lin_val, G_TYPE_STRING);
2186                 if (gval)
2187                         ghb_dict_insert(adict, g_strdup(key), gval);
2188         }
2189 }
2190
2191
2192 static GValue*
2193 import_value_xlat2(
2194         GValue *defaults, 
2195         value_map_t *value_map,
2196         const gchar *key, 
2197         GValue *mac_val)
2198 {
2199         GValue *gval, *def_val;
2200
2201         if (mac_val == NULL) return NULL;
2202         def_val = ghb_dict_lookup(defaults, key);
2203         if (def_val)
2204         {
2205                 gint ii;
2206                 gchar *str;
2207                 GValue *sval;
2208
2209                 str = ghb_value_string(mac_val);
2210                 for (ii = 0; value_map[ii].mac_val; ii++)
2211                 {
2212                         if (strcmp(str, value_map[ii].mac_val) == 0)
2213                         {
2214                                 sval = ghb_string_value_new(value_map[ii].lin_val);
2215                                 g_free(str);
2216                                 gval = ghb_value_new(G_VALUE_TYPE(def_val));
2217                                 if (!g_value_transform(sval, gval))
2218                                 {
2219                                         g_warning("can't transform");
2220                                         ghb_value_free(gval);
2221                                         ghb_value_free(sval);
2222                                         return NULL;
2223                                 }
2224                                 ghb_value_free(sval);
2225                                 return gval;
2226                         }
2227                 }
2228                 g_free(str);
2229         }
2230         else
2231         {
2232                 gint ii;
2233                 gchar *str;
2234                 GValue *sval;
2235
2236                 str = ghb_value_string(mac_val);
2237                 for (ii = 0; value_map[ii].mac_val; ii++)
2238                 {
2239                         if (strcmp(str, value_map[ii].mac_val) == 0)
2240                         {
2241                                 sval = ghb_string_value_new(value_map[ii].lin_val);
2242                                 g_free(str);
2243                                 gval = ghb_value_new(G_VALUE_TYPE(mac_val));
2244                                 if (!g_value_transform(sval, gval))
2245                                 {
2246                                         g_warning("can't transform");
2247                                         ghb_value_free(gval);
2248                                         ghb_value_free(sval);
2249                                         return NULL;
2250                                 }
2251                                 ghb_value_free(sval);
2252                                 return gval;
2253                         }
2254                 }
2255                 g_free(str);
2256         }
2257         return NULL;
2258 }
2259
2260 static void
2261 import_value_xlat(GValue *dict)
2262 {
2263         GValue *defaults, *mac_val, *gval;
2264         const gchar *key;
2265
2266         defaults = plist_get_dict(internalPlist, "Presets");
2267         key = "VideoEncoder";
2268         mac_val = ghb_dict_lookup(dict, key);
2269         gval = import_value_xlat2(defaults, vcodec_xlat, key, mac_val);
2270         if (gval)
2271                 ghb_dict_insert(dict, g_strdup(key), gval);
2272         key = "FileFormat";
2273         mac_val = ghb_dict_lookup(dict, key);
2274         gval = import_value_xlat2(defaults, container_xlat, key, mac_val);
2275         if (gval)
2276                 ghb_dict_insert(dict, g_strdup(key), gval);
2277         key = "VideoFramerate";
2278         mac_val = ghb_dict_lookup(dict, key);
2279         gval = import_value_xlat2(defaults, framerate_xlat, key, mac_val);
2280         if (gval)
2281                 ghb_dict_insert(dict, g_strdup(key), gval);
2282         key = "PictureDetelecine";
2283         mac_val = ghb_dict_lookup(dict, key);
2284         gval = import_value_xlat2(defaults, detel_xlat, key, mac_val);
2285         if (gval)
2286                 ghb_dict_insert(dict, g_strdup(key), gval);
2287         key = "PictureDecomb";
2288         mac_val = ghb_dict_lookup(dict, key);
2289         gval = import_value_xlat2(defaults, decomb_xlat, key, mac_val);
2290         if (gval)
2291                 ghb_dict_insert(dict, g_strdup(key), gval);
2292         key = "PictureDeinterlace";
2293         mac_val = ghb_dict_lookup(dict, key);
2294         gval = import_value_xlat2(defaults, deint_xlat, key, mac_val);
2295         if (gval)
2296                 ghb_dict_insert(dict, g_strdup(key), gval);
2297         key = "PictureDenoise";
2298         mac_val = ghb_dict_lookup(dict, key);
2299         gval = import_value_xlat2(defaults, denoise_xlat, key, mac_val);
2300         if (gval)
2301                 ghb_dict_insert(dict, g_strdup(key), gval);
2302
2303
2304         GValue *sdeflist;
2305         GValue *sdefaults;
2306         GValue *slist;
2307         GValue *sdict;
2308         gint count, ii;
2309
2310         sdeflist = ghb_dict_lookup(defaults, "SubtitleList");
2311         if (sdeflist)
2312         {
2313                 slist = ghb_dict_lookup(dict, "SubtitleList");
2314                 if (slist)
2315                 {
2316                         sdefaults = ghb_array_get_nth(sdeflist, 0);
2317                         count = ghb_array_len(slist);
2318                         for (ii = 0; ii < count; ii++)
2319                         {
2320                                 sdict = ghb_array_get_nth(slist, ii);
2321                                 key = "SubtitleLanguage";
2322                                 mac_val = ghb_dict_lookup(sdict, key);
2323                                 gval = import_subtitle_xlat2(mac_val);
2324                                 if (gval)
2325                                         ghb_dict_insert(sdict, g_strdup(key), gval);
2326                         }
2327                 
2328                 }
2329                 else
2330                 {
2331                         key = "Subtitles";
2332                         mac_val = ghb_dict_lookup(dict, key);
2333                         slist = ghb_array_value_new(8);
2334                         ghb_dict_insert(dict, g_strdup("SubtitleList"), slist);
2335                         if (mac_val)
2336                         {
2337                                 gchar *lang;
2338         
2339                                 gval = import_subtitle_xlat2(mac_val);
2340                                 lang = ghb_value_string(gval);
2341                                 if (lang && strcasecmp(lang, "none") != 0 && !slist)
2342                                 {
2343                                         sdict = ghb_dict_value_new();
2344                                         ghb_array_append(slist, sdict);
2345                                         ghb_dict_insert(sdict, g_strdup("SubtitleLanguage"), gval);
2346                                         gval = ghb_dict_lookup(dict, "SubtitlesForced");
2347                                         if (gval != NULL)
2348                                         {
2349                                                 ghb_dict_insert(sdict, g_strdup("SubtitleForced"), 
2350                                                                                 ghb_value_dup(gval));
2351                                         }
2352                                         else
2353                                         {
2354                                                 ghb_dict_insert(sdict, g_strdup("SubtitleForced"), 
2355                                                                                 ghb_boolean_value_new(FALSE));
2356                                         }
2357                                         ghb_dict_insert(sdict, g_strdup("SubtitleBurned"),
2358                                                                         ghb_boolean_value_new(TRUE));
2359                                         ghb_dict_insert(sdict, g_strdup("SubtitleDefaultTrack"),
2360                                                                         ghb_boolean_value_new(FALSE));
2361                                 }
2362                                 else
2363                                 {
2364                                         ghb_value_free(gval);
2365                                 }
2366                                 if (lang)
2367                                         g_free(lang);
2368                         }
2369                 }
2370         }
2371         ghb_dict_remove(dict, "Subtitles");
2372         ghb_dict_remove(dict, "SubtitlesForced");
2373
2374
2375         GValue *alist;
2376         GValue *adict;
2377         GValue *adefaults;
2378         GValue *adeflist;
2379
2380         adeflist = ghb_dict_lookup(defaults, "AudioList");
2381         if (adeflist)
2382         {
2383                 adefaults = ghb_array_get_nth(adeflist, 0);
2384                 alist = ghb_dict_lookup(dict, "AudioList");
2385                 count = ghb_array_len(alist);
2386                 for (ii = 0; ii < count; ii++)
2387                 {
2388                         adict = ghb_array_get_nth(alist, ii);
2389                         key = "AudioTrack";
2390                         mac_val = ghb_dict_lookup(adict, key);
2391                         gval = import_audio_track_xlat2(mac_val);
2392                         if (gval)
2393                                 ghb_dict_insert(adict, g_strdup(key), gval);
2394                         key = "AudioEncoder";
2395                         mac_val = ghb_dict_lookup(adict, key);
2396                         gval = import_value_xlat2(adefaults, acodec_xlat, key, mac_val);
2397                         if (gval)
2398                                 ghb_dict_insert(adict, g_strdup(key), gval);
2399                         key = "AudioSamplerate";
2400                         mac_val = ghb_dict_lookup(adict, key);
2401                         gval = import_value_xlat2(adefaults, samplerate_xlat, key, mac_val);
2402                         if (gval)
2403                                 ghb_dict_insert(adict, g_strdup(key), gval);
2404                         key = "AudioMixdown";
2405                         mac_val = ghb_dict_lookup(adict, key);
2406                         gval = import_value_xlat2(adefaults, mix_xlat, key, mac_val);
2407                         if (gval)
2408                                 ghb_dict_insert(adict, g_strdup(key), gval);
2409
2410                         mac_val = ghb_dict_lookup(adict, "AudioTrackDRCSlider");
2411                         if (mac_val != NULL)
2412                         {
2413                                 gdouble drc;
2414                                 drc = ghb_value_double(mac_val);
2415                                 if (drc < 1.0 && drc > 0.0)
2416                                 {
2417                                         ghb_dict_insert(adict, g_strdup("AudioTrackDRCSlider"), 
2418                                                                         ghb_double_value_new(0.0));
2419                                 }
2420                         }
2421                 }
2422         }
2423 }
2424
2425 static void
2426 import_xlat_preset(GValue *dict)
2427 {
2428         gboolean uses_max;
2429         gint uses_pic;
2430         gint par;
2431         gint vqtype;
2432
2433         g_debug("import_xlat_preset ()");
2434         uses_max = ghb_value_boolean(
2435                                                 preset_dict_get_value(dict, "UsesMaxPictureSettings"));
2436         uses_pic = ghb_value_int(
2437                                                 preset_dict_get_value(dict, "UsesPictureSettings"));
2438         par = ghb_value_int(preset_dict_get_value(dict, "PicturePAR"));
2439         vqtype = ghb_value_int(preset_dict_get_value(dict, "VideoQualityType"));
2440
2441         if (uses_max || uses_pic == 2)
2442         {
2443                 ghb_dict_insert(dict, g_strdup("autoscale"), 
2444                                                 ghb_boolean_value_new(TRUE));
2445         }
2446         switch (par)
2447         {
2448         case 0:
2449         {
2450                 if (ghb_dict_lookup(dict, "PictureModulus") == NULL)
2451                         ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2452                                                         ghb_int_value_new(16));
2453         } break;
2454         case 1:
2455         {
2456                 ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2457                                                 ghb_int_value_new(1));
2458         } break;
2459         case 2:
2460         {
2461                 if (ghb_dict_lookup(dict, "PictureModulus") == NULL)
2462                         ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2463                                                         ghb_int_value_new(16));
2464         } break;
2465         default:
2466         {
2467                 if (ghb_dict_lookup(dict, "PictureModulus") == NULL)
2468                         ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2469                                                         ghb_int_value_new(16));
2470         } break;
2471         }
2472         // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
2473         switch (vqtype)
2474         {
2475         case 0:
2476         {
2477                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2478                                                 ghb_boolean_value_new(TRUE));
2479                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2480                                                 ghb_boolean_value_new(FALSE));
2481                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2482                                                 ghb_boolean_value_new(FALSE));
2483         } break;
2484         case 1:
2485         {
2486                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2487                                                 ghb_boolean_value_new(FALSE));
2488                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2489                                                 ghb_boolean_value_new(TRUE));
2490                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2491                                                 ghb_boolean_value_new(FALSE));
2492         } break;
2493         case 2:
2494         {
2495                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2496                                                 ghb_boolean_value_new(FALSE));
2497                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2498                                                 ghb_boolean_value_new(FALSE));
2499                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2500                                                 ghb_boolean_value_new(TRUE));
2501         } break;
2502         default:
2503         {
2504                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2505                                                 ghb_boolean_value_new(FALSE));
2506                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2507                                                 ghb_boolean_value_new(FALSE));
2508                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2509                                                 ghb_boolean_value_new(TRUE));
2510         } break;
2511         }
2512         import_value_xlat(dict);
2513
2514         gdouble vquality;
2515         const GValue *gval;
2516
2517         vquality = ghb_value_double(preset_dict_get_value(dict, "VideoQualitySlider"));
2518         if (vquality < 1.0)
2519         {
2520                 gint vcodec;
2521
2522                 gval = preset_dict_get_value(dict, "VideoEncoder");
2523                 vcodec = ghb_lookup_combo_int("VideoEncoder", gval);
2524                 switch (vcodec)
2525                 {
2526                         case HB_VCODEC_X264:
2527                         {
2528                                 vquality = 51. - vquality * 51.;
2529                         } break;
2530
2531                         case HB_VCODEC_FFMPEG:
2532                         {
2533                                 vquality = 31. - vquality * 30.;
2534                         } break;
2535
2536                         case HB_VCODEC_THEORA:
2537                         {
2538                                 vquality = vquality * 63.;
2539                         } break;
2540
2541                         default:
2542                         {
2543                                 vquality = 0.;
2544                         } break;
2545                 }
2546                 ghb_dict_insert(dict, g_strdup("VideoQualitySlider"), 
2547                                                 ghb_double_value_new(vquality));
2548         }
2549 }
2550
2551 static void
2552 import_xlat_presets(GValue *presets)
2553 {
2554         gint count, ii;
2555         GValue *dict;
2556         gboolean folder;
2557
2558         g_debug("import_xlat_presets ()");
2559         if (presets == NULL) return;
2560         count = ghb_array_len(presets);
2561         for (ii = 0; ii < count; ii++)
2562         {
2563                 dict = ghb_array_get_nth(presets, ii);
2564                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
2565                 if (folder)
2566                 {
2567                         GValue *nested;
2568
2569                         nested = ghb_dict_lookup(dict, "ChildrenArray");
2570                         import_xlat_presets(nested);
2571                 }
2572                 else
2573                 {
2574                         import_xlat_preset(dict);
2575                 }
2576         }
2577 }
2578
2579 static void
2580 export_xlat_preset(GValue *dict)
2581 {
2582         gboolean autoscale, target, br, constant;
2583
2584         g_debug("export_xlat_prest ()");
2585         autoscale = ghb_value_boolean(preset_dict_get_value(dict, "autoscale"));
2586         target = ghb_value_boolean(
2587                                 preset_dict_get_value(dict, "vquality_type_target"));
2588         br = ghb_value_boolean(
2589                                 preset_dict_get_value(dict, "vquality_type_bitrate"));
2590         constant = ghb_value_boolean(
2591                                 preset_dict_get_value(dict, "vquality_type_constant"));
2592
2593         if (autoscale)
2594                 ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
2595                                                 ghb_int_value_new(2));
2596         else
2597                 ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
2598                                                 ghb_int_value_new(1));
2599
2600         // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
2601         if (target)
2602         {
2603                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2604                                                 ghb_int_value_new(0));
2605         }
2606         else if (br)
2607         {
2608                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2609                                                 ghb_int_value_new(1));
2610         }
2611         else if (constant)
2612         {
2613                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2614                                                 ghb_int_value_new(2));
2615         }
2616
2617         GValue *alist, *adict;
2618         gint count, ii;
2619
2620         alist = ghb_dict_lookup(dict, "AudioList");
2621         count = ghb_array_len(alist);
2622         for (ii = 0; ii < count; ii++)
2623         {
2624                 gdouble drc;
2625
2626                 adict = ghb_array_get_nth(alist, ii);
2627                 drc = ghb_value_double(
2628                                 preset_dict_get_value(adict, "AudioTrackDRCSlider"));
2629                 if (drc < 1.0 && drc > 0.0)
2630                 {
2631                         ghb_dict_insert(adict, g_strdup("AudioTrackDRCSlider"), 
2632                                                         ghb_double_value_new(0.0));
2633                 }
2634         }
2635
2636         ghb_dict_remove(dict, "UsesMaxPictureSettings");
2637         ghb_dict_remove(dict, "autoscale");
2638         ghb_dict_remove(dict, "vquality_type_target");
2639         ghb_dict_remove(dict, "vquality_type_bitrate");
2640         ghb_dict_remove(dict, "vquality_type_constant");
2641         export_value_xlat(dict);
2642 }
2643
2644 static void
2645 export_xlat_presets(GValue *presets)
2646 {
2647         gint count, ii;
2648         GValue *dict;
2649         gboolean folder;
2650
2651         if (presets == NULL) return;
2652         count = ghb_array_len(presets);
2653         for (ii = 0; ii < count; ii++)
2654         {
2655                 dict = ghb_array_get_nth(presets, ii);
2656                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
2657                 if (folder)
2658                 {
2659                         GValue *nested;
2660
2661                         nested = ghb_dict_lookup(dict, "ChildrenArray");
2662                         export_xlat_presets(nested);
2663                 }
2664                 else
2665                 {
2666                         export_xlat_preset(dict);
2667                 }
2668         }
2669 }
2670
2671 static guint prefs_timeout_id = 0;
2672
2673 static gboolean
2674 delayed_store_prefs(gpointer data)
2675 {
2676         store_plist(prefsPlist, "preferences");
2677         prefs_timeout_id = 0;
2678         return FALSE;
2679 }
2680
2681 static void
2682 store_presets()
2683 {
2684         GValue *export;
2685
2686         export = ghb_value_dup(presetsPlist);
2687         export_xlat_presets(export);
2688         store_plist(export, "presets");
2689         ghb_value_free(export);
2690 }
2691
2692 static void
2693 store_prefs(void)
2694 {
2695         if (prefs_timeout_id != 0)
2696         {
2697                 GMainContext *mc;
2698                 GSource *source;
2699
2700                 mc = g_main_context_default();
2701                 source = g_main_context_find_source_by_id(mc, prefs_timeout_id);
2702                 if (source != NULL)
2703                         g_source_destroy(source);
2704         }
2705         prefs_timeout_id = g_timeout_add_seconds(1, (GSourceFunc)delayed_store_prefs, NULL);
2706 }
2707
2708 void
2709 ghb_presets_reload(signal_user_data_t *ud)
2710 {
2711         GValue *std_presets;
2712         gint count, ii;
2713         int *indices, len;
2714
2715         g_debug("ghb_presets_reload()\n");
2716         std_presets = ghb_resource_get("standard-presets");
2717         if (std_presets == NULL) return;
2718
2719         remove_std_presets(ud);
2720         indices = presets_find_default(presetsPlist, &len);
2721         if (indices)
2722         {
2723                 presets_clear_default(std_presets);
2724                 g_free(indices);
2725         }
2726         // Merge the keyfile contents into our presets
2727         count = ghb_array_len(std_presets);
2728         for (ii = count-1; ii >= 0; ii--)
2729         {
2730                 GValue *std_dict;
2731                 GValue *copy_dict;
2732                 gint indices = 0;
2733
2734                 std_dict = ghb_array_get_nth(std_presets, ii);
2735                 copy_dict = ghb_value_dup(std_dict);
2736                 ghb_dict_insert(copy_dict, g_strdup("PresetBuildNumber"), 
2737                                                 ghb_int64_value_new(hb_get_build(NULL)));
2738                 ghb_presets_insert(presetsPlist, copy_dict, &indices, 1);
2739                 presets_list_insert(ud, &indices, 1);
2740         }
2741         import_xlat_presets(presetsPlist);
2742         store_presets();
2743 }
2744
2745 static gboolean
2746 check_old_presets()
2747 {
2748         gint count, ii;
2749
2750         count = ghb_array_len(presetsPlist);
2751         for (ii = count-1; ii >= 0; ii--)
2752         {
2753                 GValue *dict;
2754                 GValue *type;
2755
2756                 dict = ghb_array_get_nth(presetsPlist, ii);
2757                 type = ghb_dict_lookup(dict, "Type");
2758                 if (type == NULL)
2759                         return TRUE;
2760         }
2761         return FALSE;
2762 }
2763
2764 static void
2765 replace_standard_presets()
2766 {
2767         GValue *std_presets;
2768         int *indices, len;
2769         gint count, ii;
2770
2771         count = ghb_array_len(presetsPlist);
2772         for (ii = count-1; ii >= 0; ii--)
2773         {
2774                 GValue *dict;
2775                 gint ptype;
2776
2777                 dict = ghb_array_get_nth(presetsPlist, ii);
2778                 ptype = ghb_value_int(preset_dict_get_value(dict, "Type"));
2779                 if (ptype == PRESETS_BUILTIN)
2780                 {
2781                         gint indices = 0;
2782                         ghb_presets_remove(presetsPlist, &indices, 1);
2783                 }
2784         }
2785
2786         std_presets = ghb_resource_get("standard-presets");
2787         if (std_presets == NULL) return;
2788
2789         indices = presets_find_default(presetsPlist, &len);
2790         if (indices)
2791         {
2792                 presets_clear_default(std_presets);
2793                 g_free(indices);
2794         }
2795         // Merge the keyfile contents into our presets
2796         count = ghb_array_len(std_presets);
2797         for (ii = count-1; ii >= 0; ii--)
2798         {
2799                 GValue *std_dict;
2800                 GValue *copy_dict;
2801                 gint indices = 0;
2802
2803                 std_dict = ghb_array_get_nth(std_presets, ii);
2804                 copy_dict = ghb_value_dup(std_dict);
2805                 ghb_dict_insert(copy_dict, g_strdup("PresetBuildNumber"), 
2806                                                 ghb_int64_value_new(hb_get_build(NULL)));
2807                 ghb_presets_insert(presetsPlist, copy_dict, &indices, 1);
2808         }
2809         import_xlat_presets(presetsPlist);
2810         store_presets();
2811 }
2812
2813 static void
2814 update_standard_presets(signal_user_data_t *ud)
2815 {
2816         gint count, ii;
2817
2818         count = ghb_array_len(presetsPlist);
2819         for (ii = count-1; ii >= 0; ii--)
2820         {
2821                 GValue *dict;
2822                 const GValue *gval;
2823                 gint64 build;
2824                 gint type;
2825
2826                 dict = ghb_array_get_nth(presetsPlist, ii);
2827                 gval = ghb_dict_lookup(dict, "Type");
2828                 if (gval == NULL)
2829                 {
2830                         // Old preset that doesn't have a Type
2831                         replace_standard_presets();
2832                         return;
2833                 }
2834                         
2835                 type = ghb_value_int(gval);
2836                 if (type == 0)
2837                 {
2838                         gval = ghb_dict_lookup(dict, "PresetBuildNumber");
2839                         if (gval == NULL)
2840                         {
2841                                 // Old preset that doesn't have a build number
2842                                 replace_standard_presets();
2843                                 return;
2844                         }
2845
2846                         build = ghb_value_int64(gval);
2847                         if (build != hb_get_build(NULL))
2848                         {
2849                                 // Build number does not match
2850                                 replace_standard_presets();
2851                                 return;
2852                         }
2853                 }
2854         }
2855         return;
2856 }
2857
2858 void
2859 ghb_presets_load(signal_user_data_t *ud)
2860 {
2861         presetsPlist = load_plist("presets");
2862         if (presetsPlist == NULL)
2863         {
2864                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2865                 import_xlat_presets(presetsPlist);
2866                 store_presets();
2867         }
2868         else if (G_VALUE_TYPE(presetsPlist) == ghb_dict_get_type())
2869         { // Presets is older dictionary format. Convert to array
2870                 ghb_value_free(presetsPlist);
2871                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2872                 import_xlat_presets(presetsPlist);
2873                 store_presets();
2874         }
2875         else if (check_old_presets())
2876         {
2877                 ghb_value_free(presetsPlist);
2878                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2879                 import_xlat_presets(presetsPlist);
2880                 store_presets();
2881         }
2882         update_standard_presets(ud);
2883         import_xlat_presets(presetsPlist);
2884 }
2885
2886 static void
2887 settings_save(signal_user_data_t *ud, const GValue *path)
2888 {
2889         GValue *dict, *internal;
2890         GHashTableIter iter;
2891         gchar *key;
2892         GValue *value;
2893         gboolean autoscale;
2894         gint *indices, len, count;
2895         gint *def_indices, def_len;
2896         const gchar *name;
2897         gboolean replace = FALSE;
2898
2899         g_debug("settings_save");
2900         if (internalPlist == NULL) return;
2901         count = ghb_array_len(path);
2902         name = g_value_get_string(ghb_array_get_nth(path, count-1));
2903         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
2904         if (indices)
2905         {
2906                 if (ghb_presets_get_folder(presetsPlist, indices, len))
2907                 {
2908                         gchar *message;
2909                         message = g_strdup_printf(
2910                                                 "%s: Folder already exists.\n"
2911                                                 "You can not replace it with a preset.",
2912                                                 name);
2913                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2914                         g_free(message);
2915                         return;
2916                 }
2917                 dict = ghb_dict_value_new();
2918                 ghb_presets_replace(presetsPlist, dict, indices, len);
2919                 replace = TRUE;
2920         }
2921         else
2922         {
2923                 indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
2924                 if (indices)
2925                 {
2926                         dict = ghb_dict_value_new();
2927                         ghb_presets_insert(presetsPlist, dict, indices, len);
2928                 }
2929                 else
2930                 {
2931                         g_warning("failed to find insert path");
2932                         return;
2933                 }
2934         }
2935         current_preset = dict;
2936         autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
2937         ghb_settings_set_int64(ud->settings, "Type", PRESETS_CUSTOM);
2938         ghb_settings_set_int64(ud->settings, "PresetBuildNumber", hb_get_build(NULL));
2939
2940         internal = plist_get_dict(internalPlist, "Presets");
2941         ghb_dict_iter_init(&iter, internal);
2942         // middle (void*) cast prevents gcc warning "defreferencing type-punned
2943         // pointer will break strict-aliasing rules"
2944         while (g_hash_table_iter_next(
2945                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
2946         {
2947                 const GValue *gval;
2948                 gchar *key2;
2949
2950                 key2 = key;
2951                 if (!autoscale)
2952                 {
2953                         if (strcmp(key, "PictureWidth") == 0)
2954                         {
2955                                 key2 = "scale_width";
2956                         }
2957                         else if (strcmp(key, "PictureHeight") == 0)
2958                         {
2959                                 key2 = "scale_height";
2960                         }
2961                 }
2962                 gval = ghb_settings_get_value(ud->settings, key2);
2963                 if (gval == NULL)
2964                 {
2965                         continue;
2966                 }
2967                 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
2968         }
2969         internal = plist_get_dict(internalPlist, "XlatPresets");
2970         ghb_dict_iter_init(&iter, internal);
2971         // middle (void*) cast prevents gcc warning "defreferencing type-punned
2972         // pointer will break strict-aliasing rules"
2973         while (g_hash_table_iter_next(
2974                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
2975         {
2976                 const GValue *gval;
2977
2978                 gval = ghb_settings_get_value(ud->settings, key);
2979                 if (gval == NULL)
2980                 {
2981                         continue;
2982                 }
2983                 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
2984         }
2985         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(name));
2986         if (replace)
2987         {
2988                 def_indices = presets_find_default(presetsPlist, &def_len);
2989                 if (def_indices != NULL && 
2990                         preset_path_cmp(indices, len, def_indices, def_len) != 0)
2991                 {
2992                         ghb_dict_insert(dict, g_strdup("Default"), 
2993                                                         ghb_boolean_value_new(FALSE));
2994                 }
2995                 presets_list_update_item(ud, indices, len, FALSE);
2996         }
2997         else
2998         {
2999                 ghb_dict_insert(dict, g_strdup("Default"), 
3000                                                 ghb_boolean_value_new(FALSE));
3001                 presets_list_insert(ud, indices, len);
3002         }
3003         store_presets();
3004         ud->dont_clear_presets = TRUE;
3005         // Make the new preset the selected item
3006         ghb_select_preset2(ud->builder, indices, len);
3007         g_free(indices);
3008         ud->dont_clear_presets = FALSE;
3009         return;
3010 }
3011
3012 static void
3013 folder_save(signal_user_data_t *ud, const GValue *path)
3014 {
3015         GValue *dict, *folder;
3016         gint *indices, len, count;
3017         const gchar *name;
3018
3019         count = ghb_array_len(path);
3020         name = g_value_get_string(ghb_array_get_nth(path, count-1));
3021         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
3022         if (indices)
3023         {
3024                 if (!ghb_presets_get_folder(presetsPlist, indices, len))
3025                 {
3026                         gchar *message;
3027                         message = g_strdup_printf(
3028                                                 "%s: Preset already exists.\n"
3029                                                 "You can not replace it with a folder.",
3030                                                 name);
3031                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
3032                         g_free(message);
3033                         g_free(indices);
3034                         return;
3035                 }
3036                 // Already exists, update its description
3037                 dict = presets_get_dict(presetsPlist, indices, len);
3038                 ghb_dict_insert(dict, g_strdup("PresetDescription"), 
3039                         ghb_value_dup(preset_dict_get_value(
3040                                 ud->settings, "PresetDescription")));
3041                 presets_list_update_item(ud, indices, len, FALSE);
3042                 g_free(indices);
3043                 store_presets();
3044                 return;
3045         }
3046         else
3047         {
3048                 indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
3049                 if (indices)
3050                 {
3051                         dict = ghb_dict_value_new();
3052                         ghb_presets_insert(presetsPlist, dict, indices, len);
3053                 }
3054                 else
3055                 {
3056                         g_warning("failed to find insert path");
3057                         return;
3058                 }
3059         }
3060         ghb_dict_insert(dict, g_strdup("PresetDescription"), 
3061                 ghb_value_dup(preset_dict_get_value(
3062                         ud->settings, "PresetDescription")));
3063         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(name));
3064         folder = ghb_array_value_new(8);
3065         ghb_dict_insert(dict, g_strdup("ChildrenArray"), folder);
3066         ghb_dict_insert(dict, g_strdup("Type"),
3067                                                         ghb_int64_value_new(PRESETS_CUSTOM));
3068         ghb_dict_insert(dict, g_strdup("Folder"), ghb_boolean_value_new(TRUE));
3069
3070         presets_list_insert(ud, indices, len);
3071         g_free(indices);
3072         store_presets();
3073         return;
3074 }
3075
3076 void
3077 ghb_presets_list_default(signal_user_data_t *ud)
3078 {
3079         GtkTreeView *treeview;
3080         GtkTreePath *treepath;
3081         GtkTreeIter iter;
3082         GtkTreeStore *store;
3083         gint *indices, len;
3084         
3085         g_debug("ghb_presets_list_default ()");
3086         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3087         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
3088         indices = presets_find_default(presetsPlist, &len);
3089         if (indices == NULL) return;
3090         treepath = ghb_tree_path_new_from_indices(indices, len);
3091         if (treepath)
3092         {
3093                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
3094                 {
3095                         gtk_tree_store_set(store, &iter, 
3096                                                 1, 800, 
3097                                                 2, 2 ,
3098                                                 -1);
3099                 }
3100                 gtk_tree_path_free(treepath);
3101         }
3102         g_free(indices);
3103 }
3104
3105 void
3106 ghb_presets_list_clear_default(signal_user_data_t *ud)
3107 {
3108         GtkTreeView *treeview;
3109         GtkTreePath *treepath;
3110         GtkTreeIter iter;
3111         GtkTreeStore *store;
3112         gint *indices, len;
3113         
3114         g_debug("ghb_presets_list_clear_default ()");
3115         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3116         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
3117         indices = presets_find_default(presetsPlist, &len);
3118         if (indices == NULL) return;
3119         treepath = ghb_tree_path_new_from_indices(indices, len);
3120         if (treepath)
3121         {
3122                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
3123                 {
3124                         gtk_tree_store_set(store, &iter, 
3125                                                 1, 400, 
3126                                                 2, 0 ,
3127                                                 -1);
3128                 }
3129                 gtk_tree_path_free(treepath);
3130         }
3131         g_free(indices);
3132 }
3133
3134 static void
3135 update_audio_presets(signal_user_data_t *ud)
3136 {
3137         g_debug("update_audio_presets");
3138         const GValue *audio_list;
3139
3140         audio_list = ghb_settings_get_value(ud->settings, "audio_list");
3141         ghb_settings_set_value(ud->settings, "AudioList", audio_list);
3142 }
3143
3144 static void
3145 update_subtitle_presets(signal_user_data_t *ud)
3146 {
3147         g_debug("update_subtitle_presets");
3148         const GValue *subtitle_list, *subtitle;
3149         GValue *slist, *dict;
3150         gint count, ii, source;
3151
3152         subtitle_list = ghb_settings_get_value(ud->settings, "subtitle_list");
3153         slist = ghb_array_value_new(8);
3154         count = ghb_array_len(subtitle_list);
3155         for (ii = 0; ii < count; ii++)
3156         {
3157                 subtitle = ghb_array_get_nth(subtitle_list, ii);
3158                 source = ghb_settings_get_int(subtitle, "SubtitleSource");
3159                 if (source != SRTSUB)
3160                 {
3161                         dict = ghb_value_dup(subtitle);
3162                         ghb_array_append(slist, dict);
3163                 }
3164         }
3165         ghb_settings_take_value(ud->settings, "SubtitleList", slist);
3166 }
3167
3168 G_MODULE_EXPORT void
3169 presets_menu_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3170 {
3171         GtkMenu *menu;
3172
3173         menu = GTK_MENU(GHB_WIDGET(ud->builder, "presets_menu"));
3174         gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 1, 
3175                                         gtk_get_current_event_time());
3176 }
3177
3178 G_MODULE_EXPORT void
3179 preset_import_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3180 {
3181         GtkWidget *dialog;
3182         GtkResponseType response;
3183         gchar *exportDir;
3184         gchar *filename;
3185         GtkFileFilter *filter;
3186
3187         g_debug("preset_import_clicked_cb ()");
3188
3189         dialog = gtk_file_chooser_dialog_new("Import Preset", NULL,
3190                                 GTK_FILE_CHOOSER_ACTION_OPEN,
3191                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3192                                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
3193                                 NULL);
3194
3195         filter = gtk_file_filter_new();
3196         gtk_file_filter_set_name(filter, "All (*)");
3197         gtk_file_filter_add_pattern(filter, "*");
3198         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
3199
3200         filter = gtk_file_filter_new();
3201         gtk_file_filter_set_name(filter, "Presets (*.plist)");
3202         gtk_file_filter_add_pattern(filter, "*.plist");
3203         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
3204         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
3205
3206         exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3207         if (exportDir == NULL || exportDir[0] == '\0')
3208         {
3209                 exportDir = g_strdup(".");
3210         }
3211         gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
3212         g_free(exportDir);
3213
3214         response = gtk_dialog_run(GTK_DIALOG(dialog));
3215         gtk_widget_hide(dialog);
3216         if (response == GTK_RESPONSE_ACCEPT)
3217         {
3218                 GValue *dict, *array;
3219                 gchar  *dir;
3220                 gint count, ii;
3221
3222                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
3223
3224                 // import the preset
3225                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
3226                 {
3227                         gtk_widget_destroy(dialog);
3228                         g_free(filename);
3229                         return;
3230                 }
3231                 array = ghb_plist_parse_file(filename);
3232                 g_free(filename);
3233
3234                 import_xlat_presets(array);
3235                 presets_clear_default(array);
3236                 presets_customize(array);
3237
3238                 count = ghb_array_len(array);
3239                 for (ii = 0; ii < count; ii++)
3240                 {
3241                         GValue *path, *name;
3242                         gint *indices, len;
3243                         gint index = 1;
3244
3245                         dict = ghb_array_get_nth(array, ii);
3246                         path = ghb_array_value_new(1);
3247                         name = ghb_value_dup(ghb_dict_lookup(dict, "PresetName"));
3248                         ghb_array_append(path, name);
3249                         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
3250                         // Modify the preset name till we make it unique
3251                         while (indices != NULL)
3252                         {
3253                                 gchar *str = ghb_value_string(name);
3254
3255                                 ghb_value_free(path);
3256                                 g_free(indices);
3257
3258                                 str = g_strdup_printf("%s %d", str, index);
3259                                 path = ghb_array_value_new(1);
3260                                 name = ghb_string_value_new(str);
3261                                 ghb_array_append(path, name);
3262                                 g_free(str);
3263
3264                                 index++;
3265                                 indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
3266                         }
3267                         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_value_dup(name));
3268                         indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
3269                         ghb_presets_insert(presetsPlist, ghb_value_dup(dict), indices, len);
3270                         presets_list_insert(ud, indices, len);
3271                         ghb_value_free(path);
3272                 }
3273                 ghb_value_free(array);
3274
3275                 exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3276                 dir = g_path_get_dirname(filename);
3277                 if (strcmp(dir, exportDir) != 0)
3278                 {
3279                         ghb_settings_set_string(ud->settings, "ExportDirectory", dir);
3280                         ghb_pref_save(ud->settings, "ExportDirectory");
3281                 }
3282                 g_free(exportDir);
3283                 g_free(dir);
3284         }
3285         gtk_widget_destroy(dialog);
3286 }
3287
3288 G_MODULE_EXPORT void
3289 preset_export_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3290 {
3291         GtkWidget *dialog;
3292         GtkResponseType response;
3293         GValue *preset;
3294         const gchar *name = "";
3295         gint count, *indices, len;
3296         gchar *exportDir;
3297         gchar *filename;
3298
3299         g_debug("preset_export_clicked_cb ()");
3300         preset = ghb_settings_get_value (ud->settings, "preset_selection");
3301         if (preset == NULL)
3302                 return;
3303
3304         count = ghb_array_len(preset);
3305         if (count <= 0)
3306                 return;
3307
3308         name = g_value_get_string(ghb_array_get_nth(preset, count-1));
3309
3310         dialog = gtk_file_chooser_dialog_new("Export Preset", NULL,
3311                                 GTK_FILE_CHOOSER_ACTION_SAVE,
3312                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3313                                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
3314                                 NULL);
3315
3316         exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3317         if (exportDir == NULL || exportDir[0] == '\0')
3318         {
3319                 exportDir = g_strdup(".");
3320         }
3321         filename = g_strdup_printf("%s.plist", name);
3322         gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
3323         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
3324         g_free(filename);
3325         g_free(exportDir);
3326
3327         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3328         if (indices == NULL)
3329                 return;
3330
3331         response = gtk_dialog_run(GTK_DIALOG(dialog));
3332         gtk_widget_hide(dialog);
3333         if (response == GTK_RESPONSE_ACCEPT)
3334         {
3335                 GValue *export, *dict, *array;
3336                 FILE *file;
3337                 gchar  *dir;
3338
3339                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
3340
3341                 // export the preset
3342                 dict = presets_get_dict(presetsPlist, indices, len);
3343
3344                 export = ghb_value_dup(dict);
3345                 array = ghb_array_value_new(1);
3346                 ghb_array_append(array, export);
3347                 presets_clear_default(array);
3348                 presets_customize(array);
3349                 export_xlat_presets(array);
3350
3351                 file = g_fopen(filename, "w");
3352                 if (file != NULL)
3353                 {
3354                         ghb_plist_write(file, array);
3355                         fclose(file);
3356                 }
3357                 ghb_value_free(array);
3358
3359                 exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3360                 dir = g_path_get_dirname(filename);
3361                 if (strcmp(dir, exportDir) != 0)
3362                 {
3363                         ghb_settings_set_string(ud->settings, "ExportDirectory", dir);
3364                         ghb_pref_save(ud->settings, "ExportDirectory");
3365                 }
3366                 g_free(exportDir);
3367                 g_free(dir);
3368                 g_free(filename);
3369         }
3370         gtk_widget_destroy(dialog);
3371         g_free(indices);
3372 }
3373
3374 G_MODULE_EXPORT void
3375 presets_new_folder_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3376 {
3377         GtkWidget *dialog;
3378         GtkEntry *entry;
3379         GtkTextView *desc;
3380         GtkResponseType response;
3381         GValue *preset, *dict;
3382         const gchar *name = "";
3383         const gchar *description = "";
3384         gint count, *indices, len;
3385
3386         g_debug("presets_save_clicked_cb ()");
3387         preset = ghb_settings_get_value (ud->settings, "preset_selection");
3388
3389         count = ghb_array_len(preset);
3390         if (count > 0)
3391                 name = g_value_get_string(ghb_array_get_nth(preset, count-1));
3392         else
3393                 count = 1;
3394
3395         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3396         dict = presets_get_dict(presetsPlist, indices, len);
3397         if (dict != NULL)
3398         {
3399                 description = g_value_get_string(
3400                                                         ghb_dict_lookup(dict, "PresetDescription"));
3401                 ghb_ui_update(ud, "PresetDescription", ghb_string_value(description));
3402         }
3403
3404         desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
3405         dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
3406         entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
3407         gtk_entry_set_text(entry, name);
3408         response = gtk_dialog_run(GTK_DIALOG(dialog));
3409         gtk_widget_hide(dialog);
3410         if (response == GTK_RESPONSE_OK)
3411         {
3412                 // save the preset
3413                 const gchar *name = gtk_entry_get_text(entry);
3414                 GValue *dest;
3415
3416                 if (count > MAX_NESTED_PRESET-1)
3417                         count = MAX_NESTED_PRESET-1;
3418
3419                 dest = ghb_array_value_new(MAX_NESTED_PRESET);
3420                 if (indices != NULL)
3421                 {
3422                         gint ptype;
3423
3424                         ptype = ghb_presets_get_type(presetsPlist, indices, len);
3425                         if (ptype == PRESETS_CUSTOM)
3426                         {
3427                                 ghb_array_copy(dest, preset, count-1);
3428                         }
3429                 }
3430                 ghb_array_append(dest, ghb_string_value_new(name));
3431                 ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
3432                 folder_save(ud, dest);
3433                 ghb_value_free(dest);
3434         }
3435         if (indices != NULL)
3436                 g_free(indices);
3437 }
3438
3439 G_MODULE_EXPORT void
3440 presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3441 {
3442         GtkWidget *dialog;
3443         GtkEntry *entry;
3444         GtkTextView *desc;
3445         GtkResponseType response;
3446         GValue *preset;
3447         const gchar *name = "";
3448         gint count, *indices, len;
3449
3450         g_debug("presets_save_clicked_cb ()");
3451         preset = ghb_settings_get_value (ud->settings, "preset_selection");
3452
3453         count = ghb_array_len(preset);
3454         if (count > 0)
3455                 name = g_value_get_string(ghb_array_get_nth(preset, count-1));
3456         else
3457                 count = 1;
3458
3459         desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
3460         dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
3461         entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
3462         gtk_entry_set_text(entry, name);
3463         response = gtk_dialog_run(GTK_DIALOG(dialog));
3464         gtk_widget_hide(dialog);
3465         if (response == GTK_RESPONSE_OK)
3466         {
3467                 // save the preset
3468                 const gchar *name = gtk_entry_get_text(entry);
3469                 GValue *dest;
3470
3471                 dest = ghb_array_value_new(MAX_NESTED_PRESET);
3472                 indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3473                 if (indices)
3474                 {
3475                         gint ptype;
3476
3477                         ptype = ghb_presets_get_type(presetsPlist, indices, len);
3478                         if (ptype == PRESETS_CUSTOM)
3479                         {
3480                                 ghb_array_copy(dest, preset, count-1);
3481                         }
3482                         g_free(indices);
3483                 }
3484                 ghb_array_append(dest, ghb_string_value_new(name));
3485
3486                 ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
3487
3488                 // Construct the audio settings presets from the current audio list
3489                 update_audio_presets(ud);
3490                 update_subtitle_presets(ud);
3491                 settings_save(ud, dest);
3492                 ghb_value_free(dest);
3493         }
3494 }
3495
3496 G_MODULE_EXPORT void
3497 preset_type_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3498 {
3499         ghb_widget_to_setting(ud->settings, widget);
3500 }
3501
3502 G_MODULE_EXPORT void
3503 presets_restore_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3504 {
3505         GValue *preset;
3506
3507         g_debug("presets_restore_clicked_cb ()");
3508         // Reload only the standard presets
3509         ghb_presets_reload(ud);
3510         // Updating the presets list shuffles things around
3511         // need to make sure the proper preset is selected
3512         preset = ghb_settings_get_value (ud->settings, "preset");
3513         ghb_select_preset(ud->builder, preset);
3514 }
3515
3516 G_MODULE_EXPORT void
3517 presets_remove_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3518 {
3519         GtkTreeView *treeview;
3520         GtkTreeSelection *selection;
3521         GtkTreeModel *store;
3522         GtkTreeIter iter;
3523         gchar *preset;
3524         GtkResponseType response;
3525
3526         g_debug("presets_remove_clicked_cb ()");
3527         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3528         selection = gtk_tree_view_get_selection (treeview);
3529         if (gtk_tree_selection_get_selected(selection, &store, &iter))
3530         {
3531                 GtkWidget *dialog;
3532                 GtkTreePath *path;
3533                 gint *indices, len;
3534                 gboolean folder;
3535
3536                 gtk_tree_model_get(store, &iter, 0, &preset, -1);
3537                 path = gtk_tree_model_get_path(store, &iter);
3538                 indices = gtk_tree_path_get_indices(path);
3539                 len = gtk_tree_path_get_depth(path);
3540
3541                 folder = ghb_presets_get_folder(presetsPlist, indices, len);
3542                 dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
3543                                                         GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
3544                                                         "Confirm deletion of %s:\n\n%s", 
3545                                                         folder ? "folder" : "preset",
3546                                                         preset);
3547                 response = gtk_dialog_run(GTK_DIALOG(dialog));
3548                 gtk_widget_destroy (dialog);
3549                 if (response == GTK_RESPONSE_YES)
3550                 {
3551                         GtkTreeIter nextIter = iter;
3552                         gboolean valid = TRUE;
3553                         if (!gtk_tree_model_iter_next(store, &nextIter))
3554                         {
3555                                 if (!gtk_tree_model_iter_parent(store, &nextIter, &iter))
3556                                 {
3557                                         valid = FALSE;
3558                                 }
3559                         }
3560                         // Remove the selected item
3561                         // First unselect it so that selecting the new item works properly
3562                         gtk_tree_selection_unselect_iter (selection, &iter);
3563                         if (ghb_presets_remove(presetsPlist, indices, len))
3564                         {
3565                                 store_presets();
3566                                 presets_list_remove(ud, indices, len);
3567                         }
3568                         if (!valid)
3569                                 valid = gtk_tree_model_get_iter_first(store, &nextIter);
3570                         if (valid)
3571                         {
3572                                 gtk_tree_path_free(path);
3573                                 path = gtk_tree_model_get_path(store, &nextIter);
3574                                 indices = gtk_tree_path_get_indices(path);
3575                                 len = gtk_tree_path_get_depth(path);
3576                                 ghb_select_preset2(ud->builder, indices, len);
3577                         }
3578                 }
3579                 g_free(preset);
3580                 gtk_tree_path_free(path);
3581         }
3582 }
3583
3584 // controls where valid drop locations are
3585 G_MODULE_EXPORT gboolean
3586 presets_drag_motion_cb(
3587         GtkTreeView *tv,
3588         GdkDragContext *ctx,
3589         gint x,
3590         gint y,
3591         guint time,
3592         signal_user_data_t *ud)
3593 {
3594         GtkTreePath *path = NULL;
3595         GtkTreeViewDropPosition drop_pos;
3596         gint *indices, len;
3597         GtkTreeIter iter;
3598         GtkTreeView *srctv;
3599         GtkTreeModel *model;
3600         GtkTreeSelection *select;
3601         gint src_ptype, dst_ptype;
3602         gboolean src_folder, dst_folder;
3603         GValue *preset;
3604         gint tree_depth, ii;
3605         GtkWidget *widget;
3606
3607         widget = gtk_drag_get_source_widget(ctx);
3608         if (widget == NULL || widget != GTK_WIDGET(tv))
3609                 return TRUE;
3610
3611         // Get the type of the object being dragged
3612         srctv = GTK_TREE_VIEW(gtk_drag_get_source_widget(ctx));
3613         select = gtk_tree_view_get_selection (srctv);
3614         gtk_tree_selection_get_selected (select, &model, &iter);
3615         path = gtk_tree_model_get_path (model, &iter);
3616         indices = gtk_tree_path_get_indices(path);
3617         len = gtk_tree_path_get_depth(path);
3618
3619         preset = presets_get_dict(presetsPlist, indices, len);
3620         tree_depth = preset_tree_depth(preset);
3621
3622         src_ptype = ghb_presets_get_type(presetsPlist, indices, len);
3623         src_folder = ghb_presets_get_folder(presetsPlist, indices, len);
3624         gtk_tree_path_free(path);
3625
3626         if (src_folder && tree_depth == 1)
3627                 tree_depth = 2;
3628
3629         // The rest checks that the destination is a valid position
3630         // in the list.
3631         gtk_tree_view_get_dest_row_at_pos (tv, x, y, &path, &drop_pos);
3632         if (path == NULL)
3633         {
3634                 gdk_drag_status(ctx, 0, time);
3635                 return TRUE;
3636         }
3637         // Don't allow repositioning of builtin presets
3638         if (src_ptype != PRESETS_CUSTOM)
3639         {
3640                 gdk_drag_status(ctx, 0, time);
3641                 return TRUE;
3642         }
3643
3644         len = gtk_tree_path_get_depth(path);
3645         if (len+tree_depth-1 >= MAX_NESTED_PRESET)
3646         {
3647                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3648                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3649                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3650                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3651         }
3652         for (ii = len+tree_depth-1; ii > MAX_NESTED_PRESET; ii--)
3653                 gtk_tree_path_up(path);
3654         indices = gtk_tree_path_get_indices(path);
3655         len = gtk_tree_path_get_depth(path);
3656         dst_ptype = ghb_presets_get_type(presetsPlist, indices, len);
3657         dst_folder = ghb_presets_get_folder(presetsPlist, indices, len);
3658         // Don't allow mixing custom presets in the builtins
3659         if (dst_ptype != PRESETS_CUSTOM)
3660         {
3661                 gdk_drag_status(ctx, 0, time);
3662                 return TRUE;
3663         }
3664
3665         // Only allow *drop into* for folders
3666         if (!dst_folder)
3667         { 
3668                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3669                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3670                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3671                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3672         }
3673
3674         len = gtk_tree_path_get_depth(path);
3675         gtk_tree_view_set_drag_dest_row(tv, path, drop_pos);
3676         gtk_tree_path_free(path);
3677         gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
3678         return TRUE;
3679 }
3680
3681 G_MODULE_EXPORT void 
3682 presets_drag_cb(
3683         GtkTreeView *dstwidget, 
3684         GdkDragContext *dc, 
3685         gint x, gint y, 
3686         GtkSelectionData *selection_data, 
3687         guint info, guint t, 
3688         signal_user_data_t *ud)
3689 {
3690         GtkTreePath *path = NULL;
3691         GtkTreeViewDropPosition drop_pos;
3692         GtkTreeIter dstiter, srciter;
3693         gint *dst_indices, dst_len, *src_indices, src_len;
3694         gint src_ptype;
3695         gboolean src_folder, dst_folder;
3696         
3697         GtkTreeModel *dstmodel = gtk_tree_view_get_model(dstwidget);
3698                         
3699         g_debug("preset_drag_cb ()");
3700         // This doesn't work here for some reason...
3701         // gtk_tree_view_get_drag_dest_row(dstwidget, &path, &drop_pos);
3702         gtk_tree_view_get_dest_row_at_pos (dstwidget, x, y, &path, &drop_pos);
3703         // This little hack is needed because attempting to drop after
3704         // the last item gives us no path or drop_pos.
3705         if (path == NULL)
3706         {
3707                 gint n_children;
3708
3709                 n_children = gtk_tree_model_iter_n_children(dstmodel, NULL);
3710                 if (n_children)
3711                 {
3712                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3713                         path = gtk_tree_path_new_from_indices(n_children-1, -1);
3714                 }
3715                 else
3716                 {
3717                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3718                         path = gtk_tree_path_new_from_indices(0, -1);
3719                 }
3720         }
3721         if (path)
3722         {
3723                 GtkTreeView *srcwidget;
3724                 GtkTreeModel *srcmodel;
3725                 GtkTreeSelection *select;
3726                 GtkTreePath *srcpath = NULL;
3727                 GValue *preset;
3728                 gint tree_depth, ii;
3729
3730                 srcwidget = GTK_TREE_VIEW(gtk_drag_get_source_widget(dc));
3731                 select = gtk_tree_view_get_selection (srcwidget);
3732                 gtk_tree_selection_get_selected (select, &srcmodel, &srciter);
3733
3734                 srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
3735                 src_indices = gtk_tree_path_get_indices(srcpath);
3736                 src_len = gtk_tree_path_get_depth(srcpath);
3737                 src_ptype = ghb_presets_get_type(presetsPlist, src_indices, src_len);
3738                 src_folder = ghb_presets_get_folder(presetsPlist, src_indices, src_len);
3739                 preset = ghb_value_dup(
3740                                         presets_get_dict(presetsPlist, src_indices, src_len));
3741                 gtk_tree_path_free(srcpath);
3742
3743                 // Don't allow repositioning of builtin presets
3744                 if (src_ptype != PRESETS_CUSTOM)
3745                         return;
3746
3747                 tree_depth = preset_tree_depth(preset);
3748                 if (src_folder && tree_depth == 1)
3749                         tree_depth = 2;
3750
3751                 dst_len = gtk_tree_path_get_depth(path);
3752                 if (dst_len+tree_depth-1 >= MAX_NESTED_PRESET)
3753                 {
3754                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3755                                 drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3756                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3757                                 drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3758                 }
3759
3760                 for (ii = dst_len+tree_depth-1; ii > MAX_NESTED_PRESET; ii--)
3761                         gtk_tree_path_up(path);
3762                 dst_indices = gtk_tree_path_get_indices(path);
3763                 dst_len = gtk_tree_path_get_depth(path);
3764                 dst_folder = ghb_presets_get_folder(presetsPlist, dst_indices, dst_len);
3765                 // Only allow *drop into* for folders
3766                 if (!dst_folder)
3767                 { 
3768                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3769                                 drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3770                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3771                                 drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3772                 }
3773                 if (gtk_tree_model_get_iter (dstmodel, &dstiter, path))
3774                 {
3775                         GtkTreeIter iter;
3776                         GtkTreePath *dstpath = NULL;
3777
3778                         switch (drop_pos)
3779                         {
3780                                 case GTK_TREE_VIEW_DROP_BEFORE:
3781                                         gtk_tree_store_insert_before(GTK_TREE_STORE (dstmodel), 
3782                                                                                                 &iter, NULL, &dstiter);
3783                                         break;
3784
3785                                 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
3786                                         gtk_tree_store_insert(GTK_TREE_STORE (dstmodel), 
3787                                                                                                 &iter, &dstiter, 0);
3788                                         break;
3789
3790                                 case GTK_TREE_VIEW_DROP_AFTER:
3791                                         gtk_tree_store_insert_after(GTK_TREE_STORE (dstmodel), 
3792                                                                                                 &iter, NULL, &dstiter);
3793                                         break;
3794
3795                                 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
3796                                         gtk_tree_store_insert_after(GTK_TREE_STORE (dstmodel), 
3797                                                                                                 &iter, &dstiter, 0);
3798                                         break;
3799
3800                                 default:
3801                                         break;
3802                         }
3803
3804                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
3805                         dst_indices = gtk_tree_path_get_indices(dstpath);
3806                         dst_len = gtk_tree_path_get_depth(dstpath);
3807                         ghb_presets_insert(presetsPlist, preset, dst_indices, dst_len);
3808                         gtk_tree_path_free(dstpath);
3809
3810                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
3811                         src_indices = gtk_tree_path_get_indices(srcpath);
3812                         src_len = gtk_tree_path_get_depth(srcpath);
3813                         ghb_presets_remove(presetsPlist, src_indices, src_len);
3814                         gtk_tree_path_free(srcpath);
3815
3816                         gtk_tree_store_remove (GTK_TREE_STORE (srcmodel), &srciter);
3817
3818                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
3819                         dst_indices = gtk_tree_path_get_indices(dstpath);
3820                         dst_len = gtk_tree_path_get_depth(dstpath);
3821                         presets_list_update_item(ud, dst_indices, dst_len, TRUE);
3822                         gtk_tree_path_free(dstpath);
3823
3824                         store_presets();
3825                 }
3826                 gtk_tree_path_free(path);
3827         }
3828 }
3829
3830 void
3831 presets_row_expanded_cb(
3832         GtkTreeView *treeview, 
3833         GtkTreeIter *iter, 
3834         GtkTreePath *path, 
3835         signal_user_data_t *ud)
3836 {
3837         gint *indices, len;
3838         gboolean expanded, folder;
3839         GValue *dict;
3840
3841         expanded = gtk_tree_view_row_expanded(treeview, path);
3842         indices = gtk_tree_path_get_indices(path);
3843         len = gtk_tree_path_get_depth(path);
3844         dict = presets_get_dict(presetsPlist, indices, len);
3845         if (preset_folder_is_open(dict))
3846         {
3847                 if (expanded)
3848                         return;
3849         }
3850         else if (!expanded)
3851         {
3852                 return;
3853         }
3854         folder = ghb_presets_get_folder(presetsPlist, indices, len);
3855         if (folder)
3856         {
3857                 presets_set_folder_open(expanded, indices, len);
3858         }
3859
3860         // Collapsing parent folder collapses all children
3861         if (!expanded)
3862         {
3863                 GValue *presets = NULL;
3864                 gint *more_indices, count, ii;
3865
3866                 more_indices = g_malloc((len+1)*sizeof(gint));
3867                 memcpy(more_indices, indices, len*sizeof(gint));
3868
3869                 presets = presets_get_folder(presetsPlist, indices, len);
3870                 count = ghb_array_len(presets);
3871                 for (ii = 0; ii < count; ii++)
3872                 {
3873                         dict = ghb_array_get_nth(presets, ii);
3874                         folder = ghb_preset_folder(dict);
3875                         if (folder)
3876                         {
3877                                 more_indices[len] = ii;
3878                                 presets_set_folder_open(expanded, more_indices, len+1);
3879                         }
3880                 }
3881                 g_free(more_indices);
3882         }
3883         store_presets();
3884 }
3885
3886 static void
3887 preset_update_title_deps(signal_user_data_t *ud, ghb_title_info_t *tinfo)
3888 {
3889         GtkWidget *widget;
3890
3891         ghb_ui_update(ud, "scale_width", 
3892                         ghb_int64_value(tinfo->width - tinfo->crop[2] - tinfo->crop[3]));
3893         // If anamorphic or keep_aspect, the hight will be automatically calculated
3894         gboolean keep_aspect;
3895         gint pic_par;
3896         keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
3897         pic_par = ghb_settings_combo_int(ud->settings, "PicturePAR");
3898         if (!(keep_aspect || pic_par) || pic_par == 3)
3899         {
3900                 ghb_ui_update(ud, "scale_height", 
3901                         ghb_int64_value(tinfo->height - tinfo->crop[0] - tinfo->crop[1]));
3902         }
3903
3904         // Set the limits of cropping.  hb_set_anamorphic_size crashes if
3905         // you pass it a cropped width or height == 0.
3906         gint bound;
3907         bound = tinfo->height / 2 - 2;
3908         widget = GHB_WIDGET (ud->builder, "PictureTopCrop");
3909         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3910         widget = GHB_WIDGET (ud->builder, "PictureBottomCrop");
3911         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3912         bound = tinfo->width / 2 - 2;
3913         widget = GHB_WIDGET (ud->builder, "PictureLeftCrop");
3914         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3915         widget = GHB_WIDGET (ud->builder, "PictureRightCrop");
3916         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3917         if (ghb_settings_get_boolean(ud->settings, "PictureAutoCrop"))
3918         {
3919                 ghb_ui_update(ud, "PictureTopCrop", ghb_int64_value(tinfo->crop[0]));
3920                 ghb_ui_update(ud, "PictureBottomCrop", ghb_int64_value(tinfo->crop[1]));
3921                 ghb_ui_update(ud, "PictureLeftCrop", ghb_int64_value(tinfo->crop[2]));
3922                 ghb_ui_update(ud, "PictureRightCrop", ghb_int64_value(tinfo->crop[3]));
3923         }
3924 }
3925
3926 G_MODULE_EXPORT void
3927 presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_t *ud)
3928 {
3929         GtkTreeModel *store;
3930         GtkTreeIter iter;
3931         ghb_title_info_t tinfo;
3932         GtkWidget *widget;
3933         
3934         g_debug("presets_list_selection_changed_cb ()");
3935         widget = GHB_WIDGET (ud->builder, "presets_remove");
3936         if (gtk_tree_selection_get_selected(selection, &store, &iter))
3937         {
3938                 GtkTreePath *treepath;
3939                 gint *indices, len;
3940                 GValue *path;
3941                 gboolean folder;
3942
3943                 treepath = gtk_tree_model_get_path(store, &iter);
3944                 indices = gtk_tree_path_get_indices(treepath);
3945                 len = gtk_tree_path_get_depth(treepath);
3946
3947                 path = preset_path_from_indices(presetsPlist, indices, len);
3948                 ghb_settings_take_value(ud->settings, "preset_selection", path);
3949
3950                 folder = ghb_presets_get_folder(presetsPlist, indices, len);
3951                 if (!folder)
3952                 {
3953                         ud->dont_clear_presets = TRUE;
3954                         // Temporarily set the video_quality range to (0,100)
3955                         // This is needed so the video_quality value does not get
3956                         // truncated when set.  The range will be readjusted below
3957                         GtkWidget *qp = GHB_WIDGET(ud->builder, "VideoQualitySlider");
3958                         gtk_range_set_range (GTK_RANGE(qp), 0, 100);
3959                         gtk_scale_set_digits(GTK_SCALE(qp), 3);
3960                         // Clear the audio list prior to changing the preset.  Existing 
3961                         // audio can cause the container extension to be automatically 
3962                         // changed when it shouldn't be
3963                         ghb_clear_audio_list(ud);
3964                         ghb_set_preset_from_indices(ud, indices, len);
3965                         gtk_tree_path_free(treepath);
3966                         gint titleindex;
3967                         titleindex = ghb_settings_combo_int(ud->settings, "title");
3968                         ghb_set_pref_audio(titleindex, ud);
3969                         ghb_set_pref_subtitle(titleindex, ud);
3970                         ghb_settings_set_boolean(ud->settings, "preset_modified", FALSE);
3971                         if (ghb_get_title_info (&tinfo, titleindex))
3972                         {
3973                                 preset_update_title_deps(ud, &tinfo);
3974                         }
3975                         ghb_set_scale (ud, GHB_PIC_KEEP_PAR);
3976                         ud->dont_clear_presets = FALSE;
3977
3978                         gdouble vqmin, vqmax, step, page;
3979                         gint digits;
3980                         gboolean inverted;
3981
3982                         ghb_vquality_range(ud, &vqmin, &vqmax, &step, 
3983                                                                 &page, &digits, &inverted);
3984                         gtk_range_set_range (GTK_RANGE(qp), vqmin, vqmax);
3985                         gtk_range_set_increments (GTK_RANGE(qp), step, page);
3986                         gtk_scale_set_digits(GTK_SCALE(qp), digits);
3987                         gtk_range_set_inverted (GTK_RANGE(qp), inverted);
3988
3989                         gchar *text;
3990                         gint crop[4];
3991                         GtkWidget *crop_widget;
3992                         crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
3993                         crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
3994                         crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
3995                         crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
3996                         crop_widget = GHB_WIDGET (ud->builder, "crop_values");
3997                         text = g_strdup_printf("%d:%d:%d:%d", 
3998                                                                         crop[0], crop[1], crop[2], crop[3]);
3999                         gtk_label_set_text (GTK_LABEL(crop_widget), text);
4000                         g_free(text);
4001                 }
4002                 gtk_widget_set_sensitive(widget, TRUE);
4003         }
4004         else
4005         {
4006                 g_debug("No selection???  Perhaps unselected.");
4007                 gtk_widget_set_sensitive(widget, FALSE);
4008         }
4009 }
4010
4011 void
4012 ghb_clear_presets_selection(signal_user_data_t *ud)
4013 {
4014         GtkTreeView *treeview;
4015         GtkTreeSelection *selection;
4016         
4017         if (ud->dont_clear_presets) return;
4018         g_debug("ghb_clear_presets_selection()");
4019         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
4020         selection = gtk_tree_view_get_selection (treeview);
4021         gtk_tree_selection_unselect_all (selection);
4022         ghb_settings_set_boolean(ud->settings, "preset_modified", TRUE);
4023 }
4024
4025 G_MODULE_EXPORT void
4026 presets_frame_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, signal_user_data_t *ud)
4027 {
4028         GtkTreeView *treeview;
4029         GtkTreeSelection *selection;
4030         GtkTreeModel *store;
4031         GtkTreeIter iter;
4032         
4033         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
4034         selection = gtk_tree_view_get_selection(treeview);
4035         if (gtk_tree_selection_get_selected(selection, &store, &iter))
4036         {
4037                 GtkTreePath *path;
4038                 path = gtk_tree_model_get_path (store, &iter);
4039                 // Make the parent visible in scroll window if it is not.
4040                 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0, 0);
4041                 gtk_tree_path_free(path);
4042         }
4043 }
4044
4045 G_MODULE_EXPORT void
4046 presets_default_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
4047 {
4048         GValue *preset;
4049         gint *indices, len;
4050
4051         g_debug("presets_default_clicked_cb ()");
4052         preset = ghb_settings_get_value(ud->settings, "preset_selection");
4053         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
4054         if (indices)
4055         {
4056                 if (!ghb_presets_get_folder(presetsPlist, indices, len))
4057                 {
4058                         ghb_presets_list_clear_default(ud);
4059                         presets_set_default(indices, len);
4060                         ghb_presets_list_default(ud);
4061                 }
4062                 g_free(indices);
4063         }
4064 }
4065
4066 G_MODULE_EXPORT void
4067 preset_edited_cb(
4068         GtkCellRendererText *cell, 
4069         gchar *path, 
4070         gchar *text, 
4071         signal_user_data_t *ud)
4072 {
4073         GtkTreePath *treepath;
4074         GtkTreeStore *store;
4075         GtkTreeView *treeview;
4076         GtkTreeIter iter;
4077         gint *indices, len, count;
4078         GValue *dict;
4079         GValue *preset, *dest;
4080         
4081         g_debug("preset_edited_cb ()");
4082         g_debug("path (%s)", path);
4083         g_debug("text (%s)", text);
4084
4085         preset = ghb_settings_get_value (ud->settings, "preset_selection");
4086         dest = ghb_array_value_new(MAX_NESTED_PRESET);
4087         count = ghb_array_len(preset);
4088         ghb_array_copy(dest, preset, count-1);
4089         ghb_array_append(dest, ghb_string_value_new(text));
4090         indices = ghb_preset_indices_from_path(presetsPlist, dest, &len);
4091         ghb_value_free(dest);
4092         if (indices != NULL)
4093         {
4094                 // Already exists
4095                 g_free(indices);
4096                 return;
4097         }
4098
4099         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
4100         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
4101         treepath = gtk_tree_path_new_from_string (path);
4102         indices = gtk_tree_path_get_indices(treepath);
4103         len = gtk_tree_path_get_depth(treepath);
4104         gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath);
4105         gtk_tree_store_set(store, &iter, 0, text, -1);
4106
4107         dict = presets_get_dict(presetsPlist, indices, len);
4108         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(text));
4109         store_presets();
4110         gtk_tree_path_free (treepath);
4111 }
4112