OSDN Git Service

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