OSDN Git Service

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