OSDN Git Service

LinGui: rearrange the preset menu a bit.
[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", "none"},
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", "none"},
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", "none"},
1879         {"1", "custom"},
1880         {"2", "default"},
1881         {NULL, NULL}
1882 };
1883
1884 value_map_t decomb_xlat[] =
1885 {
1886         {"0", "none"},
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                         gval = ghb_string_value_new(ghb_language_table[ii].eng_name);
1909                         g_free(str);
1910                         return gval;
1911                 }
1912         }
1913         g_debug("Can't map language value: (%s)", str);
1914         g_free(str);
1915         return NULL;
1916 }
1917
1918 static GValue*
1919 export_subtitle_xlat2(GValue *lin_val)
1920 {
1921         gchar *str;
1922         GValue *gval;
1923
1924         if (lin_val == NULL) return NULL;
1925         str = ghb_value_string(lin_val);
1926         if (strcmp(str, "none") == 0)
1927         {
1928                 gval = ghb_string_value_new("None");
1929         }
1930         else if (strcmp(str, "auto") == 0)
1931         {
1932                 gval = ghb_string_value_new("Autoselect");
1933         }
1934         else
1935         {
1936                 gval = export_lang_xlat2(lin_val);
1937         }
1938         g_free(str);
1939         return gval;
1940 }
1941
1942 static GValue*
1943 import_lang_xlat2(GValue *mac_val)
1944 {
1945         GValue *gval;
1946
1947         if (mac_val == NULL) return NULL;
1948         gint ii;
1949         gchar *str;
1950
1951         str = ghb_value_string(mac_val);
1952         for (ii = 0; ghb_language_table[ii].eng_name; ii++)
1953         {
1954                 if (strcmp(str, ghb_language_table[ii].eng_name) == 0)
1955                 {
1956                         gval = ghb_string_value_new(ghb_language_table[ii].iso639_2);
1957                         g_free(str);
1958                         return gval;
1959                 }
1960         }
1961         g_debug("Can't map language value: (%s)", str);
1962         g_free(str);
1963         return NULL;
1964 }
1965
1966 static GValue*
1967 import_subtitle_xlat2(GValue *mac_val)
1968 {
1969         gchar *str;
1970         GValue *gval;
1971
1972         if (mac_val == NULL) return NULL;
1973         str = ghb_value_string(mac_val);
1974         if (strcmp(str, "None") == 0)
1975         {
1976                 gval = ghb_string_value_new("none");
1977         }
1978         else if (strcmp(str, "Autoselect") == 0)
1979         {
1980                 gval = ghb_string_value_new("auto");
1981         }
1982         else
1983         {
1984                 gval = import_lang_xlat2(mac_val);
1985         }
1986         g_free(str);
1987         return gval;
1988 }
1989
1990 static GValue*
1991 export_audio_track_xlat2(GValue *lin_val)
1992 {
1993         gchar *str;
1994         GValue *gval = NULL;
1995
1996         if (lin_val == NULL) return NULL;
1997         str = ghb_value_string(lin_val);
1998         if (strcmp(str, "none") == 0)
1999         {
2000                 gval = ghb_int_value_new(1);
2001         }
2002         else
2003         {
2004                 gint val = ghb_value_int(lin_val) + 1;
2005                 gval = ghb_int_value_new(val);
2006         }
2007         g_free(str);
2008         return gval;
2009 }
2010
2011 static GValue*
2012 import_audio_track_xlat2(GValue *mac_val)
2013 {
2014         gint val;
2015         gchar *str;
2016         GValue *gval;
2017
2018         if (mac_val == NULL) return NULL;
2019         val = ghb_value_int(mac_val);
2020         if (val <= 0)
2021         {
2022                 val = 0;
2023         }
2024         else
2025         {
2026                 val--;
2027         }
2028         str = g_strdup_printf("%d", val);
2029         gval = ghb_string_value_new(str);
2030         g_free(str);
2031         return gval;
2032 }
2033
2034 static GValue*
2035 export_value_xlat2(value_map_t *value_map, GValue *lin_val, GType mac_type)
2036 {
2037         GValue *gval;
2038
2039         if (lin_val == NULL) return NULL;
2040         gint ii;
2041         gchar *str;
2042         GValue *sval;
2043
2044         str = ghb_value_string(lin_val);
2045         for (ii = 0; value_map[ii].mac_val; ii++)
2046         {
2047                 if (strcmp(str, value_map[ii].lin_val) == 0)
2048                 {
2049                         sval = ghb_string_value_new(value_map[ii].mac_val);
2050                         g_free(str);
2051                         gval = ghb_value_new(mac_type);
2052                         if (!g_value_transform(sval, gval))
2053                         {
2054                                 g_warning("can't transform");
2055                                 ghb_value_free(gval);
2056                                 ghb_value_free(sval);
2057                                 return NULL;
2058                         }
2059                         ghb_value_free(sval);
2060                         return gval;
2061                 }
2062         }
2063         g_debug("Can't map value: (%s)", str);
2064         g_free(str);
2065         return NULL;
2066 }
2067
2068 static void
2069 export_value_xlat(GValue *dict)
2070 {
2071         GValue *lin_val, *gval;
2072         const gchar *key;
2073
2074         key = "VideoEncoder";
2075         lin_val = ghb_dict_lookup(dict, key);
2076         gval = export_value_xlat2(vcodec_xlat, lin_val, G_TYPE_STRING);
2077         if (gval)
2078                 ghb_dict_insert(dict, g_strdup(key), gval);
2079         key = "FileFormat";
2080         lin_val = ghb_dict_lookup(dict, key);
2081         gval = export_value_xlat2(container_xlat, lin_val, G_TYPE_STRING);
2082         if (gval)
2083                 ghb_dict_insert(dict, g_strdup(key), gval);
2084         key = "VideoFramerate";
2085         lin_val = ghb_dict_lookup(dict, key);
2086         gval = export_value_xlat2(framerate_xlat, lin_val, G_TYPE_STRING);
2087         if (gval)
2088                 ghb_dict_insert(dict, g_strdup(key), gval);
2089         key = "PictureDetelecine";
2090         lin_val = ghb_dict_lookup(dict, key);
2091         gval = export_value_xlat2(detel_xlat, lin_val, G_TYPE_INT);
2092         if (gval)
2093                 ghb_dict_insert(dict, g_strdup(key), gval);
2094         key = "PictureDecomb";
2095         lin_val = ghb_dict_lookup(dict, key);
2096         gval = export_value_xlat2(decomb_xlat, lin_val, G_TYPE_INT);
2097         if (gval)
2098                 ghb_dict_insert(dict, g_strdup(key), gval);
2099         key = "PictureDeinterlace";
2100         lin_val = ghb_dict_lookup(dict, key);
2101         gval = export_value_xlat2(deint_xlat, lin_val, G_TYPE_INT);
2102         if (gval)
2103                 ghb_dict_insert(dict, g_strdup(key), gval);
2104         key = "PictureDenoise";
2105         lin_val = ghb_dict_lookup(dict, key);
2106         gval = export_value_xlat2(denoise_xlat, lin_val, G_TYPE_INT);
2107         if (gval)
2108                 ghb_dict_insert(dict, g_strdup(key), gval);
2109
2110         GValue *slist;
2111         GValue *sdict;
2112         gint count, ii;
2113
2114         slist = ghb_dict_lookup(dict, "SubtitleList");
2115         count = ghb_array_len(slist);
2116         for (ii = 0; ii < count; ii++)
2117         {
2118                 sdict = ghb_array_get_nth(slist, ii);
2119                 key = "SubtitleLanguage";
2120                 lin_val = ghb_dict_lookup(sdict, key);
2121                 gval = export_subtitle_xlat2(lin_val);
2122                 if (gval)
2123                         ghb_dict_insert(sdict, g_strdup(key), gval);
2124         }
2125
2126         GValue *alist;
2127         GValue *adict;
2128
2129         alist = ghb_dict_lookup(dict, "AudioList");
2130         count = ghb_array_len(alist);
2131         for (ii = 0; ii < count; ii++)
2132         {
2133                 adict = ghb_array_get_nth(alist, ii);
2134                 key = "AudioTrack";
2135                 lin_val = ghb_dict_lookup(adict, key);
2136                 gval = export_audio_track_xlat2(lin_val);
2137                 if (gval)
2138                         ghb_dict_insert(adict, g_strdup(key), gval);
2139                 key = "AudioEncoder";
2140                 lin_val = ghb_dict_lookup(adict, key);
2141                 gval = export_value_xlat2(acodec_xlat, lin_val, G_TYPE_STRING);
2142                 if (gval)
2143                         ghb_dict_insert(adict, g_strdup(key), gval);
2144                 key = "AudioSamplerate";
2145                 lin_val = ghb_dict_lookup(adict, key);
2146                 gval = export_value_xlat2(samplerate_xlat, lin_val, G_TYPE_STRING);
2147                 if (gval)
2148                         ghb_dict_insert(adict, g_strdup(key), gval);
2149                 key = "AudioMixdown";
2150                 lin_val = ghb_dict_lookup(adict, key);
2151                 gval = export_value_xlat2(mix_xlat, lin_val, G_TYPE_STRING);
2152                 if (gval)
2153                         ghb_dict_insert(adict, g_strdup(key), gval);
2154         }
2155 }
2156
2157
2158 static GValue*
2159 import_value_xlat2(
2160         GValue *defaults, 
2161         value_map_t *value_map,
2162         const gchar *key, 
2163         GValue *mac_val)
2164 {
2165         GValue *gval, *def_val;
2166
2167         if (mac_val == NULL) return NULL;
2168         def_val = ghb_dict_lookup(defaults, key);
2169         if (def_val)
2170         {
2171                 gint ii;
2172                 gchar *str;
2173                 GValue *sval;
2174
2175                 str = ghb_value_string(mac_val);
2176                 for (ii = 0; value_map[ii].mac_val; ii++)
2177                 {
2178                         if (strcmp(str, value_map[ii].mac_val) == 0)
2179                         {
2180                                 sval = ghb_string_value_new(value_map[ii].lin_val);
2181                                 g_free(str);
2182                                 gval = ghb_value_new(G_VALUE_TYPE(def_val));
2183                                 if (!g_value_transform(sval, gval))
2184                                 {
2185                                         g_warning("can't transform");
2186                                         ghb_value_free(gval);
2187                                         ghb_value_free(sval);
2188                                         return NULL;
2189                                 }
2190                                 ghb_value_free(sval);
2191                                 return gval;
2192                         }
2193                 }
2194                 g_free(str);
2195         }
2196         else
2197         {
2198                 gint ii;
2199                 gchar *str;
2200                 GValue *sval;
2201
2202                 str = ghb_value_string(mac_val);
2203                 for (ii = 0; value_map[ii].mac_val; ii++)
2204                 {
2205                         if (strcmp(str, value_map[ii].mac_val) == 0)
2206                         {
2207                                 sval = ghb_string_value_new(value_map[ii].lin_val);
2208                                 g_free(str);
2209                                 gval = ghb_value_new(G_VALUE_TYPE(mac_val));
2210                                 if (!g_value_transform(sval, gval))
2211                                 {
2212                                         g_warning("can't transform");
2213                                         ghb_value_free(gval);
2214                                         ghb_value_free(sval);
2215                                         return NULL;
2216                                 }
2217                                 ghb_value_free(sval);
2218                                 return gval;
2219                         }
2220                 }
2221                 g_free(str);
2222         }
2223         return NULL;
2224 }
2225
2226 static void
2227 import_value_xlat(GValue *dict)
2228 {
2229         GValue *defaults, *mac_val, *gval;
2230         const gchar *key;
2231
2232         defaults = plist_get_dict(internalPlist, "Presets");
2233         key = "VideoEncoder";
2234         mac_val = ghb_dict_lookup(dict, key);
2235         gval = import_value_xlat2(defaults, vcodec_xlat, key, mac_val);
2236         if (gval)
2237                 ghb_dict_insert(dict, g_strdup(key), gval);
2238         key = "FileFormat";
2239         mac_val = ghb_dict_lookup(dict, key);
2240         gval = import_value_xlat2(defaults, container_xlat, key, mac_val);
2241         if (gval)
2242                 ghb_dict_insert(dict, g_strdup(key), gval);
2243         key = "VideoFramerate";
2244         mac_val = ghb_dict_lookup(dict, key);
2245         gval = import_value_xlat2(defaults, framerate_xlat, key, mac_val);
2246         if (gval)
2247                 ghb_dict_insert(dict, g_strdup(key), gval);
2248         key = "PictureDetelecine";
2249         mac_val = ghb_dict_lookup(dict, key);
2250         gval = import_value_xlat2(defaults, detel_xlat, key, mac_val);
2251         if (gval)
2252                 ghb_dict_insert(dict, g_strdup(key), gval);
2253         key = "PictureDecomb";
2254         mac_val = ghb_dict_lookup(dict, key);
2255         gval = import_value_xlat2(defaults, decomb_xlat, key, mac_val);
2256         if (gval)
2257                 ghb_dict_insert(dict, g_strdup(key), gval);
2258         key = "PictureDeinterlace";
2259         mac_val = ghb_dict_lookup(dict, key);
2260         gval = import_value_xlat2(defaults, deint_xlat, key, mac_val);
2261         if (gval)
2262                 ghb_dict_insert(dict, g_strdup(key), gval);
2263         key = "PictureDenoise";
2264         mac_val = ghb_dict_lookup(dict, key);
2265         gval = import_value_xlat2(defaults, denoise_xlat, key, mac_val);
2266         if (gval)
2267                 ghb_dict_insert(dict, g_strdup(key), gval);
2268
2269
2270         GValue *sdeflist;
2271         GValue *sdefaults;
2272         GValue *slist;
2273         GValue *sdict;
2274         gint count, ii;
2275
2276         sdeflist = ghb_dict_lookup(defaults, "SubtitleList");
2277         if (sdeflist)
2278         {
2279                 slist = ghb_dict_lookup(dict, "SubtitleList");
2280                 if (slist)
2281                 {
2282                         sdefaults = ghb_array_get_nth(sdeflist, 0);
2283                         count = ghb_array_len(slist);
2284                         for (ii = 0; ii < count; ii++)
2285                         {
2286                                 sdict = ghb_array_get_nth(slist, ii);
2287                                 key = "SubtitleLanguage";
2288                                 mac_val = ghb_dict_lookup(sdict, key);
2289                                 gval = import_subtitle_xlat2(mac_val);
2290                                 if (gval)
2291                                         ghb_dict_insert(sdict, g_strdup(key), gval);
2292                         }
2293                 
2294                 }
2295                 else
2296                 {
2297                         key = "Subtitles";
2298                         mac_val = ghb_dict_lookup(dict, key);
2299                         slist = ghb_array_value_new(8);
2300                         ghb_dict_insert(dict, g_strdup("SubtitleList"), slist);
2301                         if (mac_val)
2302                         {
2303                                 gchar *lang;
2304         
2305                                 gval = import_subtitle_xlat2(mac_val);
2306                                 lang = ghb_value_string(gval);
2307                                 if (lang && strcasecmp(lang, "none") != 0 && !slist)
2308                                 {
2309                                         sdict = ghb_dict_value_new();
2310                                         ghb_array_append(slist, sdict);
2311                                         ghb_dict_insert(sdict, g_strdup("SubtitleLanguage"), gval);
2312                                         gval = ghb_dict_lookup(dict, "SubtitlesForced");
2313                                         if (gval != NULL)
2314                                         {
2315                                                 ghb_dict_insert(sdict, g_strdup("SubtitleForced"), 
2316                                                                                 ghb_value_dup(gval));
2317                                         }
2318                                         else
2319                                         {
2320                                                 ghb_dict_insert(sdict, g_strdup("SubtitleForced"), 
2321                                                                                 ghb_boolean_value_new(FALSE));
2322                                         }
2323                                         ghb_dict_insert(sdict, g_strdup("SubtitleBurned"),
2324                                                                         ghb_boolean_value_new(TRUE));
2325                                         ghb_dict_insert(sdict, g_strdup("SubtitleDefaultTrack"),
2326                                                                         ghb_boolean_value_new(FALSE));
2327                                 }
2328                                 else
2329                                 {
2330                                         ghb_value_free(gval);
2331                                 }
2332                                 if (lang)
2333                                         g_free(lang);
2334                         }
2335                 }
2336         }
2337         ghb_dict_remove(dict, "Subtitles");
2338         ghb_dict_remove(dict, "SubtitlesForced");
2339
2340
2341         GValue *alist;
2342         GValue *adict;
2343         GValue *adefaults;
2344         GValue *adeflist;
2345
2346         adeflist = ghb_dict_lookup(defaults, "AudioList");
2347         if (adeflist)
2348         {
2349                 adefaults = ghb_array_get_nth(adeflist, 0);
2350                 alist = ghb_dict_lookup(dict, "AudioList");
2351                 count = ghb_array_len(alist);
2352                 for (ii = 0; ii < count; ii++)
2353                 {
2354                         adict = ghb_array_get_nth(alist, ii);
2355                         key = "AudioTrack";
2356                         mac_val = ghb_dict_lookup(adict, key);
2357                         gval = import_audio_track_xlat2(mac_val);
2358                         if (gval)
2359                                 ghb_dict_insert(adict, g_strdup(key), gval);
2360                         key = "AudioEncoder";
2361                         mac_val = ghb_dict_lookup(adict, key);
2362                         gval = import_value_xlat2(adefaults, acodec_xlat, key, mac_val);
2363                         if (gval)
2364                                 ghb_dict_insert(adict, g_strdup(key), gval);
2365                         key = "AudioSamplerate";
2366                         mac_val = ghb_dict_lookup(adict, key);
2367                         gval = import_value_xlat2(adefaults, samplerate_xlat, key, mac_val);
2368                         if (gval)
2369                                 ghb_dict_insert(adict, g_strdup(key), gval);
2370                         key = "AudioMixdown";
2371                         mac_val = ghb_dict_lookup(adict, key);
2372                         gval = import_value_xlat2(adefaults, mix_xlat, key, mac_val);
2373                         if (gval)
2374                                 ghb_dict_insert(adict, g_strdup(key), gval);
2375
2376                         mac_val = ghb_dict_lookup(adict, "AudioTrackDRCSlider");
2377                         if (mac_val != NULL)
2378                         {
2379                                 gdouble drc;
2380                                 drc = ghb_value_double(mac_val);
2381                                 if (drc < 1.0 && drc > 0.0)
2382                                 {
2383                                         ghb_dict_insert(adict, g_strdup("AudioTrackDRCSlider"), 
2384                                                                         ghb_double_value_new(0.0));
2385                                 }
2386                         }
2387                 }
2388         }
2389 }
2390
2391 static void
2392 import_xlat_preset(GValue *dict)
2393 {
2394         gboolean uses_max;
2395         gint uses_pic;
2396         gint par;
2397         gint vqtype;
2398
2399         g_debug("import_xlat_preset ()");
2400         uses_max = ghb_value_boolean(
2401                                                 preset_dict_get_value(dict, "UsesMaxPictureSettings"));
2402         uses_pic = ghb_value_int(
2403                                                 preset_dict_get_value(dict, "UsesPictureSettings"));
2404         par = ghb_value_int(preset_dict_get_value(dict, "PicturePAR"));
2405         vqtype = ghb_value_int(preset_dict_get_value(dict, "VideoQualityType"));
2406
2407         if (uses_max || uses_pic == 2)
2408         {
2409                 ghb_dict_insert(dict, g_strdup("autoscale"), 
2410                                                 ghb_boolean_value_new(TRUE));
2411         }
2412         switch (par)
2413         {
2414         case 0:
2415         {
2416                 if (ghb_dict_lookup(dict, "PictureModulus") == NULL)
2417                         ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2418                                                         ghb_int_value_new(16));
2419         } break;
2420         case 1:
2421         {
2422                 ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2423                                                 ghb_int_value_new(1));
2424         } break;
2425         case 2:
2426         {
2427                 if (ghb_dict_lookup(dict, "PictureModulus") == NULL)
2428                         ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2429                                                         ghb_int_value_new(16));
2430         } break;
2431         default:
2432         {
2433                 if (ghb_dict_lookup(dict, "PictureModulus") == NULL)
2434                         ghb_dict_insert(dict, g_strdup("PictureModulus"), 
2435                                                         ghb_int_value_new(16));
2436         } break;
2437         }
2438         // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
2439         switch (vqtype)
2440         {
2441         case 0:
2442         {
2443                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2444                                                 ghb_boolean_value_new(TRUE));
2445                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2446                                                 ghb_boolean_value_new(FALSE));
2447                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2448                                                 ghb_boolean_value_new(FALSE));
2449         } break;
2450         case 1:
2451         {
2452                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2453                                                 ghb_boolean_value_new(FALSE));
2454                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2455                                                 ghb_boolean_value_new(TRUE));
2456                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2457                                                 ghb_boolean_value_new(FALSE));
2458         } break;
2459         case 2:
2460         {
2461                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2462                                                 ghb_boolean_value_new(FALSE));
2463                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2464                                                 ghb_boolean_value_new(FALSE));
2465                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2466                                                 ghb_boolean_value_new(TRUE));
2467         } break;
2468         default:
2469         {
2470                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2471                                                 ghb_boolean_value_new(FALSE));
2472                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2473                                                 ghb_boolean_value_new(FALSE));
2474                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2475                                                 ghb_boolean_value_new(TRUE));
2476         } break;
2477         }
2478         import_value_xlat(dict);
2479
2480         gdouble vquality;
2481         const GValue *gval;
2482
2483         vquality = ghb_value_double(preset_dict_get_value(dict, "VideoQualitySlider"));
2484         if (vquality < 1.0)
2485         {
2486                 gint vcodec;
2487
2488                 gval = preset_dict_get_value(dict, "VideoEncoder");
2489                 vcodec = ghb_lookup_combo_int("VideoEncoder", gval);
2490                 switch (vcodec)
2491                 {
2492                         case HB_VCODEC_X264:
2493                         {
2494                                 vquality = 51. - vquality * 51.;
2495                         } break;
2496
2497                         case HB_VCODEC_FFMPEG:
2498                         {
2499                                 vquality = 31. - vquality * 30.;
2500                         } break;
2501
2502                         case HB_VCODEC_THEORA:
2503                         {
2504                                 vquality = vquality * 63.;
2505                         } break;
2506
2507                         default:
2508                         {
2509                                 vquality = 0.;
2510                         } break;
2511                 }
2512                 ghb_dict_insert(dict, g_strdup("VideoQualitySlider"), 
2513                                                 ghb_double_value_new(vquality));
2514         }
2515 }
2516
2517 static void
2518 import_xlat_presets(GValue *presets)
2519 {
2520         gint count, ii;
2521         GValue *dict;
2522         gboolean folder;
2523
2524         g_debug("import_xlat_presets ()");
2525         if (presets == NULL) return;
2526         count = ghb_array_len(presets);
2527         for (ii = 0; ii < count; ii++)
2528         {
2529                 dict = ghb_array_get_nth(presets, ii);
2530                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
2531                 if (folder)
2532                 {
2533                         GValue *nested;
2534
2535                         nested = ghb_dict_lookup(dict, "ChildrenArray");
2536                         import_xlat_presets(nested);
2537                 }
2538                 else
2539                 {
2540                         import_xlat_preset(dict);
2541                 }
2542         }
2543 }
2544
2545 static void
2546 export_xlat_preset(GValue *dict)
2547 {
2548         gboolean autoscale, target, br, constant;
2549
2550         g_debug("export_xlat_prest ()");
2551         autoscale = ghb_value_boolean(preset_dict_get_value(dict, "autoscale"));
2552         target = ghb_value_boolean(
2553                                 preset_dict_get_value(dict, "vquality_type_target"));
2554         br = ghb_value_boolean(
2555                                 preset_dict_get_value(dict, "vquality_type_bitrate"));
2556         constant = ghb_value_boolean(
2557                                 preset_dict_get_value(dict, "vquality_type_constant"));
2558
2559         if (autoscale)
2560                 ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
2561                                                 ghb_int_value_new(2));
2562         else
2563                 ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
2564                                                 ghb_int_value_new(1));
2565
2566         // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
2567         if (target)
2568         {
2569                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2570                                                 ghb_int_value_new(0));
2571         }
2572         else if (br)
2573         {
2574                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2575                                                 ghb_int_value_new(1));
2576         }
2577         else if (constant)
2578         {
2579                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2580                                                 ghb_int_value_new(2));
2581         }
2582
2583         GValue *alist, *adict;
2584         gint count, ii;
2585
2586         alist = ghb_dict_lookup(dict, "AudioList");
2587         count = ghb_array_len(alist);
2588         for (ii = 0; ii < count; ii++)
2589         {
2590                 gdouble drc;
2591
2592                 adict = ghb_array_get_nth(alist, ii);
2593                 drc = ghb_value_double(
2594                                 preset_dict_get_value(adict, "AudioTrackDRCSlider"));
2595                 if (drc < 1.0 && drc > 0.0)
2596                 {
2597                         ghb_dict_insert(adict, g_strdup("AudioTrackDRCSlider"), 
2598                                                         ghb_double_value_new(0.0));
2599                 }
2600         }
2601
2602         ghb_dict_remove(dict, "UsesMaxPictureSettings");
2603         ghb_dict_remove(dict, "autoscale");
2604         ghb_dict_remove(dict, "vquality_type_target");
2605         ghb_dict_remove(dict, "vquality_type_bitrate");
2606         ghb_dict_remove(dict, "vquality_type_constant");
2607         export_value_xlat(dict);
2608 }
2609
2610 static void
2611 export_xlat_presets(GValue *presets)
2612 {
2613         gint count, ii;
2614         GValue *dict;
2615         gboolean folder;
2616
2617         if (presets == NULL) return;
2618         count = ghb_array_len(presets);
2619         for (ii = 0; ii < count; ii++)
2620         {
2621                 dict = ghb_array_get_nth(presets, ii);
2622                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
2623                 if (folder)
2624                 {
2625                         GValue *nested;
2626
2627                         nested = ghb_dict_lookup(dict, "ChildrenArray");
2628                         export_xlat_presets(nested);
2629                 }
2630                 else
2631                 {
2632                         export_xlat_preset(dict);
2633                 }
2634         }
2635 }
2636
2637 static guint prefs_timeout_id = 0;
2638
2639 static gboolean
2640 delayed_store_prefs(gpointer data)
2641 {
2642         store_plist(prefsPlist, "preferences");
2643         prefs_timeout_id = 0;
2644         return FALSE;
2645 }
2646
2647 static void
2648 store_presets()
2649 {
2650         GValue *export;
2651
2652         export = ghb_value_dup(presetsPlist);
2653         export_xlat_presets(export);
2654         store_plist(export, "presets");
2655         ghb_value_free(export);
2656 }
2657
2658 static void
2659 store_prefs(void)
2660 {
2661         if (prefs_timeout_id != 0)
2662         {
2663                 GMainContext *mc;
2664                 GSource *source;
2665
2666                 mc = g_main_context_default();
2667                 source = g_main_context_find_source_by_id(mc, prefs_timeout_id);
2668                 if (source != NULL)
2669                         g_source_destroy(source);
2670         }
2671         prefs_timeout_id = g_timeout_add_seconds(1, (GSourceFunc)delayed_store_prefs, NULL);
2672 }
2673
2674 void
2675 ghb_presets_reload(signal_user_data_t *ud)
2676 {
2677         GValue *std_presets;
2678         gint count, ii;
2679         int *indices, len;
2680
2681         g_debug("ghb_presets_reload()\n");
2682         std_presets = ghb_resource_get("standard-presets");
2683         if (std_presets == NULL) return;
2684
2685         remove_std_presets(ud);
2686         indices = presets_find_default(presetsPlist, &len);
2687         if (indices)
2688         {
2689                 presets_clear_default(std_presets);
2690                 g_free(indices);
2691         }
2692         // Merge the keyfile contents into our presets
2693         count = ghb_array_len(std_presets);
2694         for (ii = count-1; ii >= 0; ii--)
2695         {
2696                 GValue *std_dict;
2697                 GValue *copy_dict;
2698                 gint indices = 0;
2699
2700                 std_dict = ghb_array_get_nth(std_presets, ii);
2701                 copy_dict = ghb_value_dup(std_dict);
2702                 ghb_dict_insert(copy_dict, g_strdup("PresetBuildNumber"), 
2703                                                 ghb_int64_value_new(hb_get_build(NULL)));
2704                 ghb_presets_insert(presetsPlist, copy_dict, &indices, 1);
2705                 presets_list_insert(ud, &indices, 1);
2706         }
2707         import_xlat_presets(presetsPlist);
2708         store_presets();
2709 }
2710
2711 static gboolean
2712 check_old_presets()
2713 {
2714         gint count, ii;
2715
2716         count = ghb_array_len(presetsPlist);
2717         for (ii = count-1; ii >= 0; ii--)
2718         {
2719                 GValue *dict;
2720                 GValue *type;
2721
2722                 dict = ghb_array_get_nth(presetsPlist, ii);
2723                 type = ghb_dict_lookup(dict, "Type");
2724                 if (type == NULL)
2725                         return TRUE;
2726         }
2727         return FALSE;
2728 }
2729
2730 static void
2731 replace_standard_presets()
2732 {
2733         GValue *std_presets;
2734         int *indices, len;
2735         gint count, ii;
2736
2737         count = ghb_array_len(presetsPlist);
2738         for (ii = count-1; ii >= 0; ii--)
2739         {
2740                 GValue *dict;
2741                 gint ptype;
2742
2743                 dict = ghb_array_get_nth(presetsPlist, ii);
2744                 ptype = ghb_value_int(preset_dict_get_value(dict, "Type"));
2745                 if (ptype == PRESETS_BUILTIN)
2746                 {
2747                         gint indices = 0;
2748                         ghb_presets_remove(presetsPlist, &indices, 1);
2749                 }
2750         }
2751
2752         std_presets = ghb_resource_get("standard-presets");
2753         if (std_presets == NULL) return;
2754
2755         indices = presets_find_default(presetsPlist, &len);
2756         if (indices)
2757         {
2758                 presets_clear_default(std_presets);
2759                 g_free(indices);
2760         }
2761         // Merge the keyfile contents into our presets
2762         count = ghb_array_len(std_presets);
2763         for (ii = count-1; ii >= 0; ii--)
2764         {
2765                 GValue *std_dict;
2766                 GValue *copy_dict;
2767                 gint indices = 0;
2768
2769                 std_dict = ghb_array_get_nth(std_presets, ii);
2770                 copy_dict = ghb_value_dup(std_dict);
2771                 ghb_dict_insert(copy_dict, g_strdup("PresetBuildNumber"), 
2772                                                 ghb_int64_value_new(hb_get_build(NULL)));
2773                 ghb_presets_insert(presetsPlist, copy_dict, &indices, 1);
2774         }
2775         import_xlat_presets(presetsPlist);
2776         store_presets();
2777 }
2778
2779 static void
2780 update_standard_presets(signal_user_data_t *ud)
2781 {
2782         gint count, ii;
2783
2784         count = ghb_array_len(presetsPlist);
2785         for (ii = count-1; ii >= 0; ii--)
2786         {
2787                 GValue *dict;
2788                 const GValue *gval;
2789                 gint64 build;
2790                 gint type;
2791
2792                 dict = ghb_array_get_nth(presetsPlist, ii);
2793                 gval = ghb_dict_lookup(dict, "Type");
2794                 if (gval == NULL)
2795                 {
2796                         // Old preset that doesn't have a Type
2797                         replace_standard_presets();
2798                         return;
2799                 }
2800                         
2801                 type = ghb_value_int(gval);
2802                 if (type == 0)
2803                 {
2804                         gval = ghb_dict_lookup(dict, "PresetBuildNumber");
2805                         if (gval == NULL)
2806                         {
2807                                 // Old preset that doesn't have a build number
2808                                 replace_standard_presets();
2809                                 return;
2810                         }
2811
2812                         build = ghb_value_int64(gval);
2813                         if (build != hb_get_build(NULL))
2814                         {
2815                                 // Build number does not match
2816                                 replace_standard_presets();
2817                                 return;
2818                         }
2819                 }
2820         }
2821         return;
2822 }
2823
2824 void
2825 ghb_presets_load(signal_user_data_t *ud)
2826 {
2827         presetsPlist = load_plist("presets");
2828         if (presetsPlist == NULL)
2829         {
2830                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2831                 import_xlat_presets(presetsPlist);
2832                 store_presets();
2833         }
2834         else if (G_VALUE_TYPE(presetsPlist) == ghb_dict_get_type())
2835         { // Presets is older dictionary format. Convert to array
2836                 ghb_value_free(presetsPlist);
2837                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2838                 import_xlat_presets(presetsPlist);
2839                 store_presets();
2840         }
2841         else if (check_old_presets())
2842         {
2843                 ghb_value_free(presetsPlist);
2844                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2845                 import_xlat_presets(presetsPlist);
2846                 store_presets();
2847         }
2848         update_standard_presets(ud);
2849         import_xlat_presets(presetsPlist);
2850 }
2851
2852 static void
2853 settings_save(signal_user_data_t *ud, const GValue *path)
2854 {
2855         GValue *dict, *internal;
2856         GHashTableIter iter;
2857         gchar *key;
2858         GValue *value;
2859         gboolean autoscale;
2860         gint *indices, len, count;
2861         gint *def_indices, def_len;
2862         const gchar *name;
2863         gboolean replace = FALSE;
2864
2865         g_debug("settings_save");
2866         if (internalPlist == NULL) return;
2867         count = ghb_array_len(path);
2868         name = g_value_get_string(ghb_array_get_nth(path, count-1));
2869         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
2870         if (indices)
2871         {
2872                 if (ghb_presets_get_folder(presetsPlist, indices, len))
2873                 {
2874                         gchar *message;
2875                         message = g_strdup_printf(
2876                                                 "%s: Folder already exists.\n"
2877                                                 "You can not replace it with a preset.",
2878                                                 name);
2879                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2880                         g_free(message);
2881                         return;
2882                 }
2883                 dict = ghb_dict_value_new();
2884                 ghb_presets_replace(presetsPlist, dict, indices, len);
2885                 replace = TRUE;
2886         }
2887         else
2888         {
2889                 indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
2890                 if (indices)
2891                 {
2892                         dict = ghb_dict_value_new();
2893                         ghb_presets_insert(presetsPlist, dict, indices, len);
2894                 }
2895                 else
2896                 {
2897                         g_warning("failed to find insert path");
2898                         return;
2899                 }
2900         }
2901         current_preset = dict;
2902         autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
2903         ghb_settings_set_int64(ud->settings, "Type", PRESETS_CUSTOM);
2904         ghb_settings_set_int64(ud->settings, "PresetBuildNumber", hb_get_build(NULL));
2905
2906         internal = plist_get_dict(internalPlist, "Presets");
2907         ghb_dict_iter_init(&iter, internal);
2908         // middle (void*) cast prevents gcc warning "defreferencing type-punned
2909         // pointer will break strict-aliasing rules"
2910         while (g_hash_table_iter_next(
2911                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
2912         {
2913                 const GValue *gval;
2914                 gchar *key2;
2915
2916                 key2 = key;
2917                 if (!autoscale)
2918                 {
2919                         if (strcmp(key, "PictureWidth") == 0)
2920                         {
2921                                 key2 = "scale_width";
2922                         }
2923                         else if (strcmp(key, "PictureHeight") == 0)
2924                         {
2925                                 key2 = "scale_height";
2926                         }
2927                 }
2928                 gval = ghb_settings_get_value(ud->settings, key2);
2929                 if (gval == NULL)
2930                 {
2931                         continue;
2932                 }
2933                 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
2934         }
2935         internal = plist_get_dict(internalPlist, "XlatPresets");
2936         ghb_dict_iter_init(&iter, internal);
2937         // middle (void*) cast prevents gcc warning "defreferencing type-punned
2938         // pointer will break strict-aliasing rules"
2939         while (g_hash_table_iter_next(
2940                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
2941         {
2942                 const GValue *gval;
2943
2944                 gval = ghb_settings_get_value(ud->settings, key);
2945                 if (gval == NULL)
2946                 {
2947                         continue;
2948                 }
2949                 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
2950         }
2951         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(name));
2952         if (replace)
2953         {
2954                 def_indices = presets_find_default(presetsPlist, &def_len);
2955                 if (def_indices != NULL && 
2956                         preset_path_cmp(indices, len, def_indices, def_len) != 0)
2957                 {
2958                         ghb_dict_insert(dict, g_strdup("Default"), 
2959                                                         ghb_boolean_value_new(FALSE));
2960                 }
2961                 presets_list_update_item(ud, indices, len, FALSE);
2962         }
2963         else
2964         {
2965                 ghb_dict_insert(dict, g_strdup("Default"), 
2966                                                 ghb_boolean_value_new(FALSE));
2967                 presets_list_insert(ud, indices, len);
2968         }
2969         store_presets();
2970         ud->dont_clear_presets = TRUE;
2971         // Make the new preset the selected item
2972         ghb_select_preset2(ud->builder, indices, len);
2973         g_free(indices);
2974         ud->dont_clear_presets = FALSE;
2975         return;
2976 }
2977
2978 static void
2979 folder_save(signal_user_data_t *ud, const GValue *path)
2980 {
2981         GValue *dict, *folder;
2982         gint *indices, len, count;
2983         const gchar *name;
2984
2985         count = ghb_array_len(path);
2986         name = g_value_get_string(ghb_array_get_nth(path, count-1));
2987         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
2988         if (indices)
2989         {
2990                 if (!ghb_presets_get_folder(presetsPlist, indices, len))
2991                 {
2992                         gchar *message;
2993                         message = g_strdup_printf(
2994                                                 "%s: Preset already exists.\n"
2995                                                 "You can not replace it with a folder.",
2996                                                 name);
2997                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2998                         g_free(message);
2999                         g_free(indices);
3000                         return;
3001                 }
3002                 // Already exists, update its description
3003                 dict = presets_get_dict(presetsPlist, indices, len);
3004                 ghb_dict_insert(dict, g_strdup("PresetDescription"), 
3005                         ghb_value_dup(preset_dict_get_value(
3006                                 ud->settings, "PresetDescription")));
3007                 presets_list_update_item(ud, indices, len, FALSE);
3008                 g_free(indices);
3009                 store_presets();
3010                 return;
3011         }
3012         else
3013         {
3014                 indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
3015                 if (indices)
3016                 {
3017                         dict = ghb_dict_value_new();
3018                         ghb_presets_insert(presetsPlist, dict, indices, len);
3019                 }
3020                 else
3021                 {
3022                         g_warning("failed to find insert path");
3023                         return;
3024                 }
3025         }
3026         ghb_dict_insert(dict, g_strdup("PresetDescription"), 
3027                 ghb_value_dup(preset_dict_get_value(
3028                         ud->settings, "PresetDescription")));
3029         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(name));
3030         folder = ghb_array_value_new(8);
3031         ghb_dict_insert(dict, g_strdup("ChildrenArray"), folder);
3032         ghb_dict_insert(dict, g_strdup("Type"),
3033                                                         ghb_int64_value_new(PRESETS_CUSTOM));
3034         ghb_dict_insert(dict, g_strdup("Folder"), ghb_boolean_value_new(TRUE));
3035
3036         presets_list_insert(ud, indices, len);
3037         g_free(indices);
3038         store_presets();
3039         return;
3040 }
3041
3042 void
3043 ghb_presets_list_default(signal_user_data_t *ud)
3044 {
3045         GtkTreeView *treeview;
3046         GtkTreePath *treepath;
3047         GtkTreeIter iter;
3048         GtkTreeStore *store;
3049         gint *indices, len;
3050         
3051         g_debug("ghb_presets_list_default ()");
3052         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3053         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
3054         indices = presets_find_default(presetsPlist, &len);
3055         if (indices == NULL) return;
3056         treepath = ghb_tree_path_new_from_indices(indices, len);
3057         if (treepath)
3058         {
3059                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
3060                 {
3061                         gtk_tree_store_set(store, &iter, 
3062                                                 1, 800, 
3063                                                 2, 2 ,
3064                                                 -1);
3065                 }
3066                 gtk_tree_path_free(treepath);
3067         }
3068         g_free(indices);
3069 }
3070
3071 void
3072 ghb_presets_list_clear_default(signal_user_data_t *ud)
3073 {
3074         GtkTreeView *treeview;
3075         GtkTreePath *treepath;
3076         GtkTreeIter iter;
3077         GtkTreeStore *store;
3078         gint *indices, len;
3079         
3080         g_debug("ghb_presets_list_clear_default ()");
3081         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3082         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
3083         indices = presets_find_default(presetsPlist, &len);
3084         if (indices == NULL) return;
3085         treepath = ghb_tree_path_new_from_indices(indices, len);
3086         if (treepath)
3087         {
3088                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
3089                 {
3090                         gtk_tree_store_set(store, &iter, 
3091                                                 1, 400, 
3092                                                 2, 0 ,
3093                                                 -1);
3094                 }
3095                 gtk_tree_path_free(treepath);
3096         }
3097         g_free(indices);
3098 }
3099
3100 static void
3101 update_audio_presets(signal_user_data_t *ud)
3102 {
3103         g_debug("update_audio_presets");
3104         const GValue *audio_list;
3105
3106         audio_list = ghb_settings_get_value(ud->settings, "audio_list");
3107         ghb_settings_set_value(ud->settings, "AudioList", audio_list);
3108 }
3109
3110 static void
3111 update_subtitle_presets(signal_user_data_t *ud)
3112 {
3113         g_debug("update_subtitle_presets");
3114         const GValue *subtitle_list, *subtitle;
3115         GValue *slist, *dict;
3116         gint count, ii, source;
3117
3118         subtitle_list = ghb_settings_get_value(ud->settings, "subtitle_list");
3119         slist = ghb_array_value_new(8);
3120         count = ghb_array_len(subtitle_list);
3121         for (ii = 0; ii < count; ii++)
3122         {
3123                 subtitle = ghb_array_get_nth(subtitle_list, ii);
3124                 source = ghb_settings_get_int(subtitle, "SubtitleSource");
3125                 if (source != SRTSUB)
3126                 {
3127                         dict = ghb_value_dup(subtitle);
3128                         ghb_array_append(slist, dict);
3129                 }
3130         }
3131         ghb_settings_take_value(ud->settings, "SubtitleList", slist);
3132 }
3133
3134 G_MODULE_EXPORT void
3135 presets_menu_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3136 {
3137         GtkMenu *menu;
3138
3139         menu = GTK_MENU(GHB_WIDGET(ud->builder, "presets_menu"));
3140         gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 1, 
3141                                         gtk_get_current_event_time());
3142 }
3143
3144 G_MODULE_EXPORT void
3145 preset_import_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3146 {
3147         GtkWidget *dialog;
3148         GtkResponseType response;
3149         gchar *exportDir;
3150         gchar *filename;
3151         GtkFileFilter *filter;
3152
3153         g_debug("preset_import_clicked_cb ()");
3154
3155         dialog = gtk_file_chooser_dialog_new("Import Preset", NULL,
3156                                 GTK_FILE_CHOOSER_ACTION_OPEN,
3157                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3158                                 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
3159                                 NULL);
3160
3161         filter = gtk_file_filter_new();
3162         gtk_file_filter_set_name(filter, "All (*)");
3163         gtk_file_filter_add_pattern(filter, "*");
3164         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
3165
3166         filter = gtk_file_filter_new();
3167         gtk_file_filter_set_name(filter, "Presets (*.plist)");
3168         gtk_file_filter_add_pattern(filter, "*.plist");
3169         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
3170         gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
3171
3172         exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3173         if (exportDir == NULL || exportDir[0] == '\0')
3174         {
3175                 exportDir = g_strdup(".");
3176         }
3177         gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
3178         g_free(exportDir);
3179
3180         response = gtk_dialog_run(GTK_DIALOG(dialog));
3181         gtk_widget_hide(dialog);
3182         if (response == GTK_RESPONSE_ACCEPT)
3183         {
3184                 GValue *dict, *array;
3185                 gchar  *dir;
3186                 gint count, ii;
3187
3188                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
3189
3190                 // import the preset
3191                 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
3192                 {
3193                         gtk_widget_destroy(dialog);
3194                         g_free(filename);
3195                         return;
3196                 }
3197                 array = ghb_plist_parse_file(filename);
3198                 g_free(filename);
3199
3200                 import_xlat_presets(array);
3201                 presets_clear_default(array);
3202                 presets_customize(array);
3203
3204                 count = ghb_array_len(array);
3205                 for (ii = 0; ii < count; ii++)
3206                 {
3207                         GValue *path, *name;
3208                         gint *indices, len;
3209                         gint index = 1;
3210
3211                         dict = ghb_array_get_nth(array, ii);
3212                         path = ghb_array_value_new(1);
3213                         name = ghb_value_dup(ghb_dict_lookup(dict, "PresetName"));
3214                         ghb_array_append(path, name);
3215                         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
3216                         // Modify the preset name till we make it unique
3217                         while (indices != NULL)
3218                         {
3219                                 gchar *str = ghb_value_string(name);
3220
3221                                 ghb_value_free(path);
3222                                 g_free(indices);
3223
3224                                 str = g_strdup_printf("%s %d", str, index);
3225                                 path = ghb_array_value_new(1);
3226                                 name = ghb_string_value_new(str);
3227                                 ghb_array_append(path, name);
3228                                 g_free(str);
3229
3230                                 index++;
3231                                 indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
3232                         }
3233                         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_value_dup(name));
3234                         indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
3235                         ghb_presets_insert(presetsPlist, ghb_value_dup(dict), indices, len);
3236                         presets_list_insert(ud, indices, len);
3237                         ghb_value_free(path);
3238                 }
3239                 ghb_value_free(array);
3240
3241                 exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3242                 dir = g_path_get_dirname(filename);
3243                 if (strcmp(dir, exportDir) != 0)
3244                 {
3245                         ghb_settings_set_string(ud->settings, "ExportDirectory", dir);
3246                         ghb_pref_save(ud->settings, "ExportDirectory");
3247                 }
3248                 g_free(exportDir);
3249                 g_free(dir);
3250         }
3251         gtk_widget_destroy(dialog);
3252 }
3253
3254 G_MODULE_EXPORT void
3255 preset_export_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3256 {
3257         GtkWidget *dialog;
3258         GtkResponseType response;
3259         GValue *preset;
3260         const gchar *name = "";
3261         gint count, *indices, len;
3262         gchar *exportDir;
3263         gchar *filename;
3264
3265         g_debug("preset_export_clicked_cb ()");
3266         preset = ghb_settings_get_value (ud->settings, "preset_selection");
3267         if (preset == NULL)
3268                 return;
3269
3270         count = ghb_array_len(preset);
3271         if (count <= 0)
3272                 return;
3273
3274         name = g_value_get_string(ghb_array_get_nth(preset, count-1));
3275
3276         dialog = gtk_file_chooser_dialog_new("Export Preset", NULL,
3277                                 GTK_FILE_CHOOSER_ACTION_SAVE,
3278                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3279                                 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
3280                                 NULL);
3281
3282         exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3283         if (exportDir == NULL || exportDir[0] == '\0')
3284         {
3285                 exportDir = g_strdup(".");
3286         }
3287         filename = g_strdup_printf("%s.plist", name);
3288         gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
3289         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
3290         g_free(filename);
3291         g_free(exportDir);
3292
3293         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3294         if (indices == NULL)
3295                 return;
3296
3297         response = gtk_dialog_run(GTK_DIALOG(dialog));
3298         gtk_widget_hide(dialog);
3299         if (response == GTK_RESPONSE_ACCEPT)
3300         {
3301                 GValue *export, *dict, *array;
3302                 FILE *file;
3303                 gchar  *dir;
3304
3305                 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
3306
3307                 // export the preset
3308                 dict = presets_get_dict(presetsPlist, indices, len);
3309
3310                 export = ghb_value_dup(dict);
3311                 array = ghb_array_value_new(1);
3312                 ghb_array_append(array, export);
3313                 presets_clear_default(array);
3314                 presets_customize(array);
3315                 export_xlat_presets(array);
3316
3317                 file = g_fopen(filename, "w");
3318                 if (file != NULL)
3319                 {
3320                         ghb_plist_write(file, array);
3321                         fclose(file);
3322                 }
3323                 ghb_value_free(array);
3324
3325                 exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
3326                 dir = g_path_get_dirname(filename);
3327                 if (strcmp(dir, exportDir) != 0)
3328                 {
3329                         ghb_settings_set_string(ud->settings, "ExportDirectory", dir);
3330                         ghb_pref_save(ud->settings, "ExportDirectory");
3331                 }
3332                 g_free(exportDir);
3333                 g_free(dir);
3334                 g_free(filename);
3335         }
3336         gtk_widget_destroy(dialog);
3337         g_free(indices);
3338 }
3339
3340 G_MODULE_EXPORT void
3341 presets_new_folder_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3342 {
3343         GtkWidget *dialog;
3344         GtkEntry *entry;
3345         GtkTextView *desc;
3346         GtkResponseType response;
3347         GValue *preset, *dict;
3348         const gchar *name = "";
3349         const gchar *description = "";
3350         gint count, *indices, len;
3351
3352         g_debug("presets_save_clicked_cb ()");
3353         preset = ghb_settings_get_value (ud->settings, "preset_selection");
3354
3355         count = ghb_array_len(preset);
3356         if (count > 0)
3357                 name = g_value_get_string(ghb_array_get_nth(preset, count-1));
3358         else
3359                 count = 1;
3360
3361         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3362         dict = presets_get_dict(presetsPlist, indices, len);
3363         if (dict != NULL)
3364         {
3365                 description = g_value_get_string(
3366                                                         ghb_dict_lookup(dict, "PresetDescription"));
3367                 ghb_ui_update(ud, "PresetDescription", ghb_string_value(description));
3368         }
3369
3370         desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
3371         dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
3372         entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
3373         gtk_entry_set_text(entry, name);
3374         response = gtk_dialog_run(GTK_DIALOG(dialog));
3375         gtk_widget_hide(dialog);
3376         if (response == GTK_RESPONSE_OK)
3377         {
3378                 // save the preset
3379                 const gchar *name = gtk_entry_get_text(entry);
3380                 GValue *dest;
3381
3382                 if (count > MAX_NESTED_PRESET-1)
3383                         count = MAX_NESTED_PRESET-1;
3384
3385                 dest = ghb_array_value_new(MAX_NESTED_PRESET);
3386                 if (indices != NULL)
3387                 {
3388                         gint ptype;
3389
3390                         ptype = ghb_presets_get_type(presetsPlist, indices, len);
3391                         if (ptype == PRESETS_CUSTOM)
3392                         {
3393                                 ghb_array_copy(dest, preset, count-1);
3394                         }
3395                 }
3396                 ghb_array_append(dest, ghb_string_value_new(name));
3397                 ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
3398                 folder_save(ud, dest);
3399                 ghb_value_free(dest);
3400         }
3401         if (indices != NULL)
3402                 g_free(indices);
3403 }
3404
3405 G_MODULE_EXPORT void
3406 presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3407 {
3408         GtkWidget *dialog;
3409         GtkEntry *entry;
3410         GtkTextView *desc;
3411         GtkResponseType response;
3412         GValue *preset;
3413         const gchar *name = "";
3414         gint count, *indices, len;
3415
3416         g_debug("presets_save_clicked_cb ()");
3417         preset = ghb_settings_get_value (ud->settings, "preset_selection");
3418
3419         count = ghb_array_len(preset);
3420         if (count > 0)
3421                 name = g_value_get_string(ghb_array_get_nth(preset, count-1));
3422         else
3423                 count = 1;
3424
3425         desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
3426         dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
3427         entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
3428         gtk_entry_set_text(entry, name);
3429         response = gtk_dialog_run(GTK_DIALOG(dialog));
3430         gtk_widget_hide(dialog);
3431         if (response == GTK_RESPONSE_OK)
3432         {
3433                 // save the preset
3434                 const gchar *name = gtk_entry_get_text(entry);
3435                 GValue *dest;
3436
3437                 dest = ghb_array_value_new(MAX_NESTED_PRESET);
3438                 indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3439                 if (indices)
3440                 {
3441                         gint ptype;
3442
3443                         ptype = ghb_presets_get_type(presetsPlist, indices, len);
3444                         if (ptype == PRESETS_CUSTOM)
3445                         {
3446                                 ghb_array_copy(dest, preset, count-1);
3447                         }
3448                         g_free(indices);
3449                 }
3450                 ghb_array_append(dest, ghb_string_value_new(name));
3451
3452                 ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
3453
3454                 // Construct the audio settings presets from the current audio list
3455                 update_audio_presets(ud);
3456                 update_subtitle_presets(ud);
3457                 settings_save(ud, dest);
3458                 ghb_value_free(dest);
3459         }
3460 }
3461
3462 G_MODULE_EXPORT void
3463 preset_type_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3464 {
3465         ghb_widget_to_setting(ud->settings, widget);
3466 }
3467
3468 G_MODULE_EXPORT void
3469 presets_restore_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3470 {
3471         GValue *preset;
3472
3473         g_debug("presets_restore_clicked_cb ()");
3474         // Reload only the standard presets
3475         ghb_presets_reload(ud);
3476         // Updating the presets list shuffles things around
3477         // need to make sure the proper preset is selected
3478         preset = ghb_settings_get_value (ud->settings, "preset");
3479         ghb_select_preset(ud->builder, preset);
3480 }
3481
3482 G_MODULE_EXPORT void
3483 presets_remove_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3484 {
3485         GtkTreeView *treeview;
3486         GtkTreeSelection *selection;
3487         GtkTreeModel *store;
3488         GtkTreeIter iter;
3489         gchar *preset;
3490         GtkResponseType response;
3491
3492         g_debug("presets_remove_clicked_cb ()");
3493         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3494         selection = gtk_tree_view_get_selection (treeview);
3495         if (gtk_tree_selection_get_selected(selection, &store, &iter))
3496         {
3497                 GtkWidget *dialog;
3498                 GtkTreePath *path;
3499                 gint *indices, len;
3500                 gboolean folder;
3501
3502                 gtk_tree_model_get(store, &iter, 0, &preset, -1);
3503                 path = gtk_tree_model_get_path(store, &iter);
3504                 indices = gtk_tree_path_get_indices(path);
3505                 len = gtk_tree_path_get_depth(path);
3506
3507                 folder = ghb_presets_get_folder(presetsPlist, indices, len);
3508                 dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
3509                                                         GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
3510                                                         "Confirm deletion of %s:\n\n%s", 
3511                                                         folder ? "folder" : "preset",
3512                                                         preset);
3513                 response = gtk_dialog_run(GTK_DIALOG(dialog));
3514                 gtk_widget_destroy (dialog);
3515                 if (response == GTK_RESPONSE_YES)
3516                 {
3517                         GtkTreeIter nextIter = iter;
3518                         gboolean valid = TRUE;
3519                         if (!gtk_tree_model_iter_next(store, &nextIter))
3520                         {
3521                                 if (!gtk_tree_model_iter_parent(store, &nextIter, &iter))
3522                                 {
3523                                         valid = FALSE;
3524                                 }
3525                         }
3526                         // Remove the selected item
3527                         // First unselect it so that selecting the new item works properly
3528                         gtk_tree_selection_unselect_iter (selection, &iter);
3529                         if (ghb_presets_remove(presetsPlist, indices, len))
3530                         {
3531                                 store_presets();
3532                                 presets_list_remove(ud, indices, len);
3533                         }
3534                         if (!valid)
3535                                 valid = gtk_tree_model_get_iter_first(store, &nextIter);
3536                         if (valid)
3537                         {
3538                                 gtk_tree_path_free(path);
3539                                 path = gtk_tree_model_get_path(store, &nextIter);
3540                                 indices = gtk_tree_path_get_indices(path);
3541                                 len = gtk_tree_path_get_depth(path);
3542                                 ghb_select_preset2(ud->builder, indices, len);
3543                         }
3544                 }
3545                 g_free(preset);
3546                 gtk_tree_path_free(path);
3547         }
3548 }
3549
3550 // controls where valid drop locations are
3551 G_MODULE_EXPORT gboolean
3552 presets_drag_motion_cb(
3553         GtkTreeView *tv,
3554         GdkDragContext *ctx,
3555         gint x,
3556         gint y,
3557         guint time,
3558         signal_user_data_t *ud)
3559 {
3560         GtkTreePath *path = NULL;
3561         GtkTreeViewDropPosition drop_pos;
3562         gint *indices, len;
3563         GtkTreeIter iter;
3564         GtkTreeView *srctv;
3565         GtkTreeModel *model;
3566         GtkTreeSelection *select;
3567         gint src_ptype, dst_ptype;
3568         gboolean src_folder, dst_folder;
3569         GValue *preset;
3570         gint tree_depth, ii;
3571         GtkWidget *widget;
3572
3573         widget = gtk_drag_get_source_widget(ctx);
3574         if (widget == NULL || widget != GTK_WIDGET(tv))
3575                 return TRUE;
3576
3577         // Get the type of the object being dragged
3578         srctv = GTK_TREE_VIEW(gtk_drag_get_source_widget(ctx));
3579         select = gtk_tree_view_get_selection (srctv);
3580         gtk_tree_selection_get_selected (select, &model, &iter);
3581         path = gtk_tree_model_get_path (model, &iter);
3582         indices = gtk_tree_path_get_indices(path);
3583         len = gtk_tree_path_get_depth(path);
3584
3585         preset = presets_get_dict(presetsPlist, indices, len);
3586         tree_depth = preset_tree_depth(preset);
3587
3588         src_ptype = ghb_presets_get_type(presetsPlist, indices, len);
3589         src_folder = ghb_presets_get_folder(presetsPlist, indices, len);
3590         gtk_tree_path_free(path);
3591
3592         if (src_folder && tree_depth == 1)
3593                 tree_depth = 2;
3594
3595         // The rest checks that the destination is a valid position
3596         // in the list.
3597         gtk_tree_view_get_dest_row_at_pos (tv, x, y, &path, &drop_pos);
3598         if (path == NULL)
3599         {
3600                 gdk_drag_status(ctx, 0, time);
3601                 return TRUE;
3602         }
3603         // Don't allow repositioning of builtin presets
3604         if (src_ptype != PRESETS_CUSTOM)
3605         {
3606                 gdk_drag_status(ctx, 0, time);
3607                 return TRUE;
3608         }
3609
3610         len = gtk_tree_path_get_depth(path);
3611         if (len+tree_depth-1 >= MAX_NESTED_PRESET)
3612         {
3613                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3614                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3615                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3616                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3617         }
3618         for (ii = len+tree_depth-1; ii > MAX_NESTED_PRESET; ii--)
3619                 gtk_tree_path_up(path);
3620         indices = gtk_tree_path_get_indices(path);
3621         len = gtk_tree_path_get_depth(path);
3622         dst_ptype = ghb_presets_get_type(presetsPlist, indices, len);
3623         dst_folder = ghb_presets_get_folder(presetsPlist, indices, len);
3624         // Don't allow mixing custom presets in the builtins
3625         if (dst_ptype != PRESETS_CUSTOM)
3626         {
3627                 gdk_drag_status(ctx, 0, time);
3628                 return TRUE;
3629         }
3630
3631         // Only allow *drop into* for folders
3632         if (!dst_folder)
3633         { 
3634                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3635                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3636                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3637                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3638         }
3639
3640         len = gtk_tree_path_get_depth(path);
3641         gtk_tree_view_set_drag_dest_row(tv, path, drop_pos);
3642         gtk_tree_path_free(path);
3643         gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
3644         return TRUE;
3645 }
3646
3647 G_MODULE_EXPORT void 
3648 presets_drag_cb(
3649         GtkTreeView *dstwidget, 
3650         GdkDragContext *dc, 
3651         gint x, gint y, 
3652         GtkSelectionData *selection_data, 
3653         guint info, guint t, 
3654         signal_user_data_t *ud)
3655 {
3656         GtkTreePath *path = NULL;
3657         GtkTreeViewDropPosition drop_pos;
3658         GtkTreeIter dstiter, srciter;
3659         gint *dst_indices, dst_len, *src_indices, src_len;
3660         gint src_ptype;
3661         gboolean src_folder, dst_folder;
3662         
3663         GtkTreeModel *dstmodel = gtk_tree_view_get_model(dstwidget);
3664                         
3665         g_debug("preset_drag_cb ()");
3666         // This doesn't work here for some reason...
3667         // gtk_tree_view_get_drag_dest_row(dstwidget, &path, &drop_pos);
3668         gtk_tree_view_get_dest_row_at_pos (dstwidget, x, y, &path, &drop_pos);
3669         // This little hack is needed because attempting to drop after
3670         // the last item gives us no path or drop_pos.
3671         if (path == NULL)
3672         {
3673                 gint n_children;
3674
3675                 n_children = gtk_tree_model_iter_n_children(dstmodel, NULL);
3676                 if (n_children)
3677                 {
3678                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3679                         path = gtk_tree_path_new_from_indices(n_children-1, -1);
3680                 }
3681                 else
3682                 {
3683                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3684                         path = gtk_tree_path_new_from_indices(0, -1);
3685                 }
3686         }
3687         if (path)
3688         {
3689                 GtkTreeView *srcwidget;
3690                 GtkTreeModel *srcmodel;
3691                 GtkTreeSelection *select;
3692                 GtkTreePath *srcpath = NULL;
3693                 GValue *preset;
3694                 gint tree_depth, ii;
3695
3696                 srcwidget = GTK_TREE_VIEW(gtk_drag_get_source_widget(dc));
3697                 select = gtk_tree_view_get_selection (srcwidget);
3698                 gtk_tree_selection_get_selected (select, &srcmodel, &srciter);
3699
3700                 srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
3701                 src_indices = gtk_tree_path_get_indices(srcpath);
3702                 src_len = gtk_tree_path_get_depth(srcpath);
3703                 src_ptype = ghb_presets_get_type(presetsPlist, src_indices, src_len);
3704                 src_folder = ghb_presets_get_folder(presetsPlist, src_indices, src_len);
3705                 preset = ghb_value_dup(
3706                                         presets_get_dict(presetsPlist, src_indices, src_len));
3707                 gtk_tree_path_free(srcpath);
3708
3709                 // Don't allow repositioning of builtin presets
3710                 if (src_ptype != PRESETS_CUSTOM)
3711                         return;
3712
3713                 tree_depth = preset_tree_depth(preset);
3714                 if (src_folder && tree_depth == 1)
3715                         tree_depth = 2;
3716
3717                 dst_len = gtk_tree_path_get_depth(path);
3718                 if (dst_len+tree_depth-1 >= MAX_NESTED_PRESET)
3719                 {
3720                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3721                                 drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3722                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3723                                 drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3724                 }
3725
3726                 for (ii = dst_len+tree_depth-1; ii > MAX_NESTED_PRESET; ii--)
3727                         gtk_tree_path_up(path);
3728                 dst_indices = gtk_tree_path_get_indices(path);
3729                 dst_len = gtk_tree_path_get_depth(path);
3730                 dst_folder = ghb_presets_get_folder(presetsPlist, dst_indices, dst_len);
3731                 // Only allow *drop into* for folders
3732                 if (!dst_folder)
3733                 { 
3734                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3735                                 drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3736                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3737                                 drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3738                 }
3739                 if (gtk_tree_model_get_iter (dstmodel, &dstiter, path))
3740                 {
3741                         GtkTreeIter iter;
3742                         GtkTreePath *dstpath = NULL;
3743
3744                         switch (drop_pos)
3745                         {
3746                                 case GTK_TREE_VIEW_DROP_BEFORE:
3747                                         gtk_tree_store_insert_before(GTK_TREE_STORE (dstmodel), 
3748                                                                                                 &iter, NULL, &dstiter);
3749                                         break;
3750
3751                                 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
3752                                         gtk_tree_store_insert(GTK_TREE_STORE (dstmodel), 
3753                                                                                                 &iter, &dstiter, 0);
3754                                         break;
3755
3756                                 case GTK_TREE_VIEW_DROP_AFTER:
3757                                         gtk_tree_store_insert_after(GTK_TREE_STORE (dstmodel), 
3758                                                                                                 &iter, NULL, &dstiter);
3759                                         break;
3760
3761                                 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
3762                                         gtk_tree_store_insert_after(GTK_TREE_STORE (dstmodel), 
3763                                                                                                 &iter, &dstiter, 0);
3764                                         break;
3765
3766                                 default:
3767                                         break;
3768                         }
3769
3770                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
3771                         dst_indices = gtk_tree_path_get_indices(dstpath);
3772                         dst_len = gtk_tree_path_get_depth(dstpath);
3773                         ghb_presets_insert(presetsPlist, preset, dst_indices, dst_len);
3774                         gtk_tree_path_free(dstpath);
3775
3776                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
3777                         src_indices = gtk_tree_path_get_indices(srcpath);
3778                         src_len = gtk_tree_path_get_depth(srcpath);
3779                         ghb_presets_remove(presetsPlist, src_indices, src_len);
3780                         gtk_tree_path_free(srcpath);
3781
3782                         gtk_tree_store_remove (GTK_TREE_STORE (srcmodel), &srciter);
3783
3784                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
3785                         dst_indices = gtk_tree_path_get_indices(dstpath);
3786                         dst_len = gtk_tree_path_get_depth(dstpath);
3787                         presets_list_update_item(ud, dst_indices, dst_len, TRUE);
3788                         gtk_tree_path_free(dstpath);
3789
3790                         store_presets();
3791                 }
3792                 gtk_tree_path_free(path);
3793         }
3794 }
3795
3796 void
3797 presets_row_expanded_cb(
3798         GtkTreeView *treeview, 
3799         GtkTreeIter *iter, 
3800         GtkTreePath *path, 
3801         signal_user_data_t *ud)
3802 {
3803         gint *indices, len;
3804         gboolean expanded, folder;
3805         GValue *dict;
3806
3807         expanded = gtk_tree_view_row_expanded(treeview, path);
3808         indices = gtk_tree_path_get_indices(path);
3809         len = gtk_tree_path_get_depth(path);
3810         dict = presets_get_dict(presetsPlist, indices, len);
3811         if (preset_folder_is_open(dict))
3812         {
3813                 if (expanded)
3814                         return;
3815         }
3816         else if (!expanded)
3817         {
3818                 return;
3819         }
3820         folder = ghb_presets_get_folder(presetsPlist, indices, len);
3821         if (folder)
3822         {
3823                 presets_set_folder_open(expanded, indices, len);
3824         }
3825
3826         // Collapsing parent folder collapses all children
3827         if (!expanded)
3828         {
3829                 GValue *presets = NULL;
3830                 gint *more_indices, count, ii;
3831
3832                 more_indices = g_malloc((len+1)*sizeof(gint));
3833                 memcpy(more_indices, indices, len*sizeof(gint));
3834
3835                 presets = presets_get_folder(presetsPlist, indices, len);
3836                 count = ghb_array_len(presets);
3837                 for (ii = 0; ii < count; ii++)
3838                 {
3839                         dict = ghb_array_get_nth(presets, ii);
3840                         folder = ghb_preset_folder(dict);
3841                         if (folder)
3842                         {
3843                                 more_indices[len] = ii;
3844                                 presets_set_folder_open(expanded, more_indices, len+1);
3845                         }
3846                 }
3847                 g_free(more_indices);
3848         }
3849         store_presets();
3850 }
3851
3852 static void
3853 preset_update_title_deps(signal_user_data_t *ud, ghb_title_info_t *tinfo)
3854 {
3855         GtkWidget *widget;
3856
3857         ghb_ui_update(ud, "scale_width", 
3858                         ghb_int64_value(tinfo->width - tinfo->crop[2] - tinfo->crop[3]));
3859         // If anamorphic or keep_aspect, the hight will be automatically calculated
3860         gboolean keep_aspect;
3861         gint pic_par;
3862         keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
3863         pic_par = ghb_settings_combo_int(ud->settings, "PicturePAR");
3864         if (!(keep_aspect || pic_par) || pic_par == 3)
3865         {
3866                 ghb_ui_update(ud, "scale_height", 
3867                         ghb_int64_value(tinfo->height - tinfo->crop[0] - tinfo->crop[1]));
3868         }
3869
3870         // Set the limits of cropping.  hb_set_anamorphic_size crashes if
3871         // you pass it a cropped width or height == 0.
3872         gint bound;
3873         bound = tinfo->height / 2 - 2;
3874         widget = GHB_WIDGET (ud->builder, "PictureTopCrop");
3875         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3876         widget = GHB_WIDGET (ud->builder, "PictureBottomCrop");
3877         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3878         bound = tinfo->width / 2 - 2;
3879         widget = GHB_WIDGET (ud->builder, "PictureLeftCrop");
3880         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3881         widget = GHB_WIDGET (ud->builder, "PictureRightCrop");
3882         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3883         if (ghb_settings_get_boolean(ud->settings, "PictureAutoCrop"))
3884         {
3885                 ghb_ui_update(ud, "PictureTopCrop", ghb_int64_value(tinfo->crop[0]));
3886                 ghb_ui_update(ud, "PictureBottomCrop", ghb_int64_value(tinfo->crop[1]));
3887                 ghb_ui_update(ud, "PictureLeftCrop", ghb_int64_value(tinfo->crop[2]));
3888                 ghb_ui_update(ud, "PictureRightCrop", ghb_int64_value(tinfo->crop[3]));
3889         }
3890 }
3891
3892 G_MODULE_EXPORT void
3893 presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_t *ud)
3894 {
3895         GtkTreeModel *store;
3896         GtkTreeIter iter;
3897         ghb_title_info_t tinfo;
3898         GtkWidget *widget;
3899         
3900         g_debug("presets_list_selection_changed_cb ()");
3901         widget = GHB_WIDGET (ud->builder, "presets_remove");
3902         if (gtk_tree_selection_get_selected(selection, &store, &iter))
3903         {
3904                 GtkTreePath *treepath;
3905                 gint *indices, len;
3906                 GValue *path;
3907                 gboolean folder;
3908
3909                 treepath = gtk_tree_model_get_path(store, &iter);
3910                 indices = gtk_tree_path_get_indices(treepath);
3911                 len = gtk_tree_path_get_depth(treepath);
3912
3913                 path = preset_path_from_indices(presetsPlist, indices, len);
3914                 ghb_settings_take_value(ud->settings, "preset_selection", path);
3915
3916                 folder = ghb_presets_get_folder(presetsPlist, indices, len);
3917                 if (!folder)
3918                 {
3919                         ud->dont_clear_presets = TRUE;
3920                         // Temporarily set the video_quality range to (0,100)
3921                         // This is needed so the video_quality value does not get
3922                         // truncated when set.  The range will be readjusted below
3923                         GtkWidget *qp = GHB_WIDGET(ud->builder, "VideoQualitySlider");
3924                         gtk_range_set_range (GTK_RANGE(qp), 0, 100);
3925                         gtk_scale_set_digits(GTK_SCALE(qp), 3);
3926                         // Clear the audio list prior to changing the preset.  Existing 
3927                         // audio can cause the container extension to be automatically 
3928                         // changed when it shouldn't be
3929                         ghb_clear_audio_list(ud);
3930                         ghb_set_preset_from_indices(ud, indices, len);
3931                         gtk_tree_path_free(treepath);
3932                         gint titleindex;
3933                         titleindex = ghb_settings_combo_int(ud->settings, "title");
3934                         ghb_set_pref_audio(titleindex, ud);
3935                         ghb_set_pref_subtitle(titleindex, ud);
3936                         ghb_settings_set_boolean(ud->settings, "preset_modified", FALSE);
3937                         if (ghb_get_title_info (&tinfo, titleindex))
3938                         {
3939                                 preset_update_title_deps(ud, &tinfo);
3940                         }
3941                         ghb_set_scale (ud, GHB_PIC_KEEP_PAR);
3942                         ud->dont_clear_presets = FALSE;
3943
3944                         gdouble vqmin, vqmax, step, page;
3945                         gint digits;
3946                         gboolean inverted;
3947
3948                         ghb_vquality_range(ud, &vqmin, &vqmax, &step, 
3949                                                                 &page, &digits, &inverted);
3950                         gtk_range_set_range (GTK_RANGE(qp), vqmin, vqmax);
3951                         gtk_range_set_increments (GTK_RANGE(qp), step, page);
3952                         gtk_scale_set_digits(GTK_SCALE(qp), digits);
3953                         gtk_range_set_inverted (GTK_RANGE(qp), inverted);
3954
3955                         gchar *text;
3956                         gint crop[4];
3957                         GtkWidget *crop_widget;
3958                         crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
3959                         crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
3960                         crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
3961                         crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
3962                         crop_widget = GHB_WIDGET (ud->builder, "crop_values");
3963                         text = g_strdup_printf("%d:%d:%d:%d", 
3964                                                                         crop[0], crop[1], crop[2], crop[3]);
3965                         gtk_label_set_text (GTK_LABEL(crop_widget), text);
3966                         g_free(text);
3967                 }
3968                 gtk_widget_set_sensitive(widget, TRUE);
3969         }
3970         else
3971         {
3972                 g_debug("No selection???  Perhaps unselected.");
3973                 gtk_widget_set_sensitive(widget, FALSE);
3974         }
3975 }
3976
3977 void
3978 ghb_clear_presets_selection(signal_user_data_t *ud)
3979 {
3980         GtkTreeView *treeview;
3981         GtkTreeSelection *selection;
3982         
3983         if (ud->dont_clear_presets) return;
3984         g_debug("ghb_clear_presets_selection()");
3985         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3986         selection = gtk_tree_view_get_selection (treeview);
3987         gtk_tree_selection_unselect_all (selection);
3988         ghb_settings_set_boolean(ud->settings, "preset_modified", TRUE);
3989 }
3990
3991 G_MODULE_EXPORT void
3992 presets_frame_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, signal_user_data_t *ud)
3993 {
3994         GtkTreeView *treeview;
3995         GtkTreeSelection *selection;
3996         GtkTreeModel *store;
3997         GtkTreeIter iter;
3998         
3999         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
4000         selection = gtk_tree_view_get_selection(treeview);
4001         if (gtk_tree_selection_get_selected(selection, &store, &iter))
4002         {
4003                 GtkTreePath *path;
4004                 path = gtk_tree_model_get_path (store, &iter);
4005                 // Make the parent visible in scroll window if it is not.
4006                 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0, 0);
4007                 gtk_tree_path_free(path);
4008         }
4009 }
4010
4011 G_MODULE_EXPORT void
4012 presets_default_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
4013 {
4014         GValue *preset;
4015         gint *indices, len;
4016
4017         g_debug("presets_default_clicked_cb ()");
4018         preset = ghb_settings_get_value(ud->settings, "preset_selection");
4019         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
4020         if (indices)
4021         {
4022                 if (!ghb_presets_get_folder(presetsPlist, indices, len))
4023                 {
4024                         ghb_presets_list_clear_default(ud);
4025                         presets_set_default(indices, len);
4026                         ghb_presets_list_default(ud);
4027                 }
4028                 g_free(indices);
4029         }
4030 }
4031
4032 G_MODULE_EXPORT void
4033 preset_edited_cb(
4034         GtkCellRendererText *cell, 
4035         gchar *path, 
4036         gchar *text, 
4037         signal_user_data_t *ud)
4038 {
4039         GtkTreePath *treepath;
4040         GtkTreeStore *store;
4041         GtkTreeView *treeview;
4042         GtkTreeIter iter;
4043         gint *indices, len, count;
4044         GValue *dict;
4045         GValue *preset, *dest;
4046         
4047         g_debug("preset_edited_cb ()");
4048         g_debug("path (%s)", path);
4049         g_debug("text (%s)", text);
4050
4051         preset = ghb_settings_get_value (ud->settings, "preset_selection");
4052         dest = ghb_array_value_new(MAX_NESTED_PRESET);
4053         count = ghb_array_len(preset);
4054         ghb_array_copy(dest, preset, count-1);
4055         ghb_array_append(dest, ghb_string_value_new(text));
4056         indices = ghb_preset_indices_from_path(presetsPlist, dest, &len);
4057         ghb_value_free(dest);
4058         if (indices != NULL)
4059         {
4060                 // Already exists
4061                 g_free(indices);
4062                 return;
4063         }
4064
4065         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
4066         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
4067         treepath = gtk_tree_path_new_from_string (path);
4068         indices = gtk_tree_path_get_indices(treepath);
4069         len = gtk_tree_path_get_depth(treepath);
4070         gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath);
4071         gtk_tree_store_set(store, &iter, 0, text, -1);
4072
4073         dict = presets_get_dict(presetsPlist, indices, len);
4074         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(text));
4075         store_presets();
4076         gtk_tree_path_free (treepath);
4077 }
4078