OSDN Git Service

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