OSDN Git Service

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