OSDN Git Service

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