OSDN Git Service

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