OSDN Git Service

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