OSDN Git Service

LinGui: only update prefs file after a delay.
[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)", "xvid"},
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", "avi"},
1715         {"OGM file", "ogm"},
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                 ghb_dict_insert(dict, g_strdup("anamorphic"), 
2207                                                 ghb_boolean_value_new(FALSE));
2208                 if (ghb_dict_lookup(dict, "ModDimensions") == NULL)
2209                         ghb_dict_insert(dict, g_strdup("ModDimensions"), 
2210                                                         ghb_boolean_value_new(TRUE));
2211         } break;
2212         case 1:
2213         {
2214                 ghb_dict_insert(dict, g_strdup("anamorphic"), 
2215                                                 ghb_boolean_value_new(TRUE));
2216                 ghb_dict_insert(dict, g_strdup("ModDimensions"), 
2217                                                 ghb_boolean_value_new(FALSE));
2218         } break;
2219         case 2:
2220         {
2221                 ghb_dict_insert(dict, g_strdup("anamorphic"), 
2222                                                 ghb_boolean_value_new(TRUE));
2223                 ghb_dict_insert(dict, g_strdup("ModDimensions"), 
2224                                                 ghb_boolean_value_new(TRUE));
2225         } break;
2226         default:
2227         {
2228                 ghb_dict_insert(dict, g_strdup("anamorphic"), 
2229                                                 ghb_boolean_value_new(TRUE));
2230                 ghb_dict_insert(dict, g_strdup("ModDimensions"), 
2231                                                 ghb_boolean_value_new(TRUE));
2232         } break;
2233         }
2234         // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
2235         switch (vqtype)
2236         {
2237         case 0:
2238         {
2239                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2240                                                 ghb_boolean_value_new(TRUE));
2241                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2242                                                 ghb_boolean_value_new(FALSE));
2243                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2244                                                 ghb_boolean_value_new(FALSE));
2245         } break;
2246         case 1:
2247         {
2248                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2249                                                 ghb_boolean_value_new(FALSE));
2250                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2251                                                 ghb_boolean_value_new(TRUE));
2252                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2253                                                 ghb_boolean_value_new(FALSE));
2254         } break;
2255         case 2:
2256         {
2257                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2258                                                 ghb_boolean_value_new(FALSE));
2259                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2260                                                 ghb_boolean_value_new(FALSE));
2261                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2262                                                 ghb_boolean_value_new(TRUE));
2263         } break;
2264         default:
2265         {
2266                 ghb_dict_insert(dict, g_strdup("vquality_type_target"), 
2267                                                 ghb_boolean_value_new(FALSE));
2268                 ghb_dict_insert(dict, g_strdup("vquality_type_bitrate"), 
2269                                                 ghb_boolean_value_new(FALSE));
2270                 ghb_dict_insert(dict, g_strdup("vquality_type_constant"), 
2271                                                 ghb_boolean_value_new(TRUE));
2272         } break;
2273         }
2274         import_value_xlat(dict);
2275
2276         gdouble vquality;
2277         const GValue *gval;
2278
2279         vquality = ghb_value_double(preset_dict_get_value(dict, "VideoQualitySlider"));
2280         if (vquality < 1.0)
2281         {
2282                 gint vcodec;
2283
2284                 gval = preset_dict_get_value(dict, "VideoEncoder");
2285                 vcodec = ghb_lookup_combo_int("VideoEncoder", gval);
2286                 switch (vcodec)
2287                 {
2288                         case HB_VCODEC_X264:
2289                         {
2290                                 vquality = 51. - vquality * 51.;
2291                         } break;
2292
2293                         case HB_VCODEC_XVID:
2294                         case HB_VCODEC_FFMPEG:
2295                         {
2296                                 vquality = 31. - vquality * 30.;
2297                         } break;
2298
2299                         case HB_VCODEC_THEORA:
2300                         {
2301                                 vquality = vquality * 63.;
2302                         } break;
2303
2304                         default:
2305                         {
2306                                 vquality = 0.;
2307                         } break;
2308                 }
2309                 ghb_dict_insert(dict, g_strdup("VideoQualitySlider"), 
2310                                                 ghb_double_value_new(vquality));
2311         }
2312 }
2313
2314 static void
2315 import_xlat_presets(GValue *presets)
2316 {
2317         gint count, ii;
2318         GValue *dict;
2319         gboolean folder;
2320
2321         g_debug("import_xlat_presets ()");
2322         if (presets == NULL) return;
2323         count = ghb_array_len(presets);
2324         for (ii = 0; ii < count; ii++)
2325         {
2326                 dict = ghb_array_get_nth(presets, ii);
2327                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
2328                 if (folder)
2329                 {
2330                         GValue *nested;
2331
2332                         nested = ghb_dict_lookup(dict, "ChildrenArray");
2333                         import_xlat_presets(nested);
2334                 }
2335                 else
2336                 {
2337                         import_xlat_preset(dict);
2338                 }
2339         }
2340 }
2341
2342 static void
2343 export_xlat_preset(GValue *dict)
2344 {
2345         gboolean ana, round, autoscale, target, br, constant;
2346
2347         g_debug("export_xlat_prest ()");
2348         autoscale = ghb_value_boolean(preset_dict_get_value(dict, "autoscale"));
2349         ana = ghb_value_boolean(preset_dict_get_value(dict, "anamorphic"));
2350         round = ghb_value_boolean(preset_dict_get_value(dict, "ModDimensions"));
2351         target = ghb_value_boolean(
2352                                 preset_dict_get_value(dict, "vquality_type_target"));
2353         br = ghb_value_boolean(
2354                                 preset_dict_get_value(dict, "vquality_type_bitrate"));
2355         constant = ghb_value_boolean(
2356                                 preset_dict_get_value(dict, "vquality_type_constant"));
2357
2358         if (autoscale)
2359                 ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
2360                                                 ghb_int_value_new(2));
2361         else
2362                 ghb_dict_insert(dict, g_strdup("UsesPictureSettings"), 
2363                                                 ghb_int_value_new(1));
2364
2365         if (ana)
2366         {
2367                 if (round)
2368                         ghb_dict_insert(dict, g_strdup("PicturePAR"), 
2369                                                 ghb_int_value_new(2));
2370                 else
2371                         ghb_dict_insert(dict, g_strdup("PicturePAR"), 
2372                                                 ghb_int_value_new(1));
2373         }
2374         else
2375         {
2376                 ghb_dict_insert(dict, g_strdup("PicturePAR"), 
2377                                                 ghb_int_value_new(0));
2378         }
2379         // VideoQualityType/0/1/2 - vquality_type_/target/bitrate/constant
2380         if (target)
2381         {
2382                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2383                                                 ghb_int_value_new(0));
2384         }
2385         else if (br)
2386         {
2387                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2388                                                 ghb_int_value_new(1));
2389         }
2390         else if (constant)
2391         {
2392                 ghb_dict_insert(dict, g_strdup("VideoQualityType"), 
2393                                                 ghb_int_value_new(2));
2394         }
2395         ghb_dict_remove(dict, "UsesMaxPictureSettings");
2396         ghb_dict_remove(dict, "autoscale");
2397         ghb_dict_remove(dict, "anamorphic");
2398         ghb_dict_remove(dict, "vquality_type_target");
2399         ghb_dict_remove(dict, "vquality_type_bitrate");
2400         ghb_dict_remove(dict, "vquality_type_constant");
2401         export_value_xlat(dict);
2402 }
2403
2404 static void
2405 export_xlat_presets(GValue *presets)
2406 {
2407         gint count, ii;
2408         GValue *dict;
2409         gboolean folder;
2410
2411         if (presets == NULL) return;
2412         count = ghb_array_len(presets);
2413         for (ii = 0; ii < count; ii++)
2414         {
2415                 dict = ghb_array_get_nth(presets, ii);
2416                 folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
2417                 if (folder)
2418                 {
2419                         GValue *nested;
2420
2421                         nested = ghb_dict_lookup(dict, "ChildrenArray");
2422                         export_xlat_presets(nested);
2423                 }
2424                 else
2425                 {
2426                         export_xlat_preset(dict);
2427                 }
2428         }
2429 }
2430
2431 static guint prefs_timeout_id = 0;
2432
2433 static gboolean
2434 delayed_store_prefs(gpointer data)
2435 {
2436         store_plist(prefsPlist, "preferences");
2437         prefs_timeout_id = 0;
2438         return FALSE;
2439 }
2440
2441 static void
2442 store_presets()
2443 {
2444         GValue *export;
2445
2446         export = ghb_value_dup(presetsPlist);
2447         export_xlat_presets(export);
2448         store_plist(export, "presets");
2449         ghb_value_free(export);
2450 }
2451
2452 static void
2453 store_prefs(void)
2454 {
2455         if (prefs_timeout_id != 0)
2456         {
2457                 GMainContext *mc;
2458                 GSource *source;
2459
2460                 mc = g_main_context_default();
2461                 source = g_main_context_find_source_by_id(mc, prefs_timeout_id);
2462                 if (source != NULL)
2463                         g_source_destroy(source);
2464         }
2465         prefs_timeout_id = g_timeout_add_seconds(1, (GSourceFunc)delayed_store_prefs, NULL);
2466 }
2467
2468 void
2469 ghb_presets_reload(signal_user_data_t *ud)
2470 {
2471         GValue *std_presets;
2472         gint count, ii;
2473         int *indices, len;
2474
2475         g_debug("ghb_presets_reload()\n");
2476         std_presets = ghb_resource_get("standard-presets");
2477         if (std_presets == NULL) return;
2478
2479         remove_std_presets(ud);
2480     indices = presets_find_default(presetsPlist, &len);
2481         if (indices)
2482         {
2483                 presets_clear_default(std_presets);
2484                 g_free(indices);
2485         }
2486         // Merge the keyfile contents into our presets
2487         count = ghb_array_len(std_presets);
2488         for (ii = count-1; ii >= 0; ii--)
2489         {
2490                 GValue *std_dict;
2491                 GValue *copy_dict;
2492                 gint indices = 0;
2493
2494                 std_dict = ghb_array_get_nth(std_presets, ii);
2495                 copy_dict = ghb_value_dup(std_dict);
2496                 ghb_presets_insert(presetsPlist, copy_dict, &indices, 1);
2497                 presets_list_insert(ud, &indices, 1);
2498         }
2499         import_xlat_presets(presetsPlist);
2500         store_presets();
2501 }
2502
2503 static gboolean
2504 check_old_presets()
2505 {
2506         gint count, ii;
2507
2508         count = ghb_array_len(presetsPlist);
2509         for (ii = count-1; ii >= 0; ii--)
2510         {
2511                 GValue *dict;
2512                 GValue *type;
2513
2514                 dict = ghb_array_get_nth(presetsPlist, ii);
2515                 type = ghb_dict_lookup(dict, "Type");
2516                 if (type == NULL)
2517                         return TRUE;
2518         }
2519         return FALSE;
2520 }
2521
2522 void
2523 ghb_presets_load()
2524 {
2525         presetsPlist = load_plist("presets");
2526         if (presetsPlist == NULL)
2527         {
2528                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2529                 import_xlat_presets(presetsPlist);
2530                 store_presets();
2531         }
2532         else if (G_VALUE_TYPE(presetsPlist) == ghb_dict_get_type())
2533         { // Presets is older dictionary format. Convert to array
2534                 ghb_value_free(presetsPlist);
2535                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2536                 import_xlat_presets(presetsPlist);
2537                 store_presets();
2538         }
2539         else if (check_old_presets())
2540         {
2541                 ghb_value_free(presetsPlist);
2542                 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
2543                 import_xlat_presets(presetsPlist);
2544                 store_presets();
2545         }
2546         import_xlat_presets(presetsPlist);
2547 }
2548
2549 static void
2550 settings_save(signal_user_data_t *ud, const GValue *path)
2551 {
2552         GValue *dict, *internal;
2553         GHashTableIter iter;
2554         gchar *key;
2555         GValue *value;
2556         gboolean autoscale;
2557         gint *indices, len, count;
2558         const gchar *name;
2559         gboolean replace = FALSE;
2560
2561         g_debug("settings_save");
2562         if (internalPlist == NULL) return;
2563         count = ghb_array_len(path);
2564         name = g_value_get_string(ghb_array_get_nth(path, count-1));
2565         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
2566         if (indices)
2567         {
2568                 if (ghb_presets_get_folder(presetsPlist, indices, len))
2569                 {
2570                         gchar *message;
2571                         message = g_strdup_printf(
2572                                                 "%s: Folder already exists.\n"
2573                                                 "You can not replace it with a preset.",
2574                                                 name);
2575                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2576                         g_free(message);
2577                         return;
2578                 }
2579                 dict = ghb_dict_value_new();
2580                 ghb_presets_replace(presetsPlist, dict, indices, len);
2581                 replace = TRUE;
2582         }
2583         else
2584         {
2585                 indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
2586                 if (indices)
2587                 {
2588                         dict = ghb_dict_value_new();
2589                         ghb_presets_insert(presetsPlist, dict, indices, len);
2590                 }
2591                 else
2592                 {
2593                         g_warning("failed to find insert path");
2594                         return;
2595                 }
2596         }
2597         current_preset = dict;
2598         autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
2599         ghb_settings_set_int64(ud->settings, "Type", PRESETS_CUSTOM);
2600
2601         internal = plist_get_dict(internalPlist, "Presets");
2602         ghb_dict_iter_init(&iter, internal);
2603         // middle (void*) cast prevents gcc warning "defreferencing type-punned
2604         // pointer will break strict-aliasing rules"
2605         while (g_hash_table_iter_next(
2606                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
2607         {
2608                 const GValue *gval;
2609                 gchar *key2;
2610
2611                 key2 = key;
2612                 if (!autoscale)
2613                 {
2614                         if (strcmp(key, "PictureWidth") == 0)
2615                         {
2616                                 key2 = "scale_width";
2617                         }
2618                         else if (strcmp(key, "PictureHeight") == 0)
2619                         {
2620                                 key2 = "scale_height";
2621                         }
2622                 }
2623                 gval = ghb_settings_get_value(ud->settings, key2);
2624                 if (gval == NULL)
2625                 {
2626                         continue;
2627                 }
2628                 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
2629         }
2630         internal = plist_get_dict(internalPlist, "XlatPresets");
2631         ghb_dict_iter_init(&iter, internal);
2632         // middle (void*) cast prevents gcc warning "defreferencing type-punned
2633         // pointer will break strict-aliasing rules"
2634         while (g_hash_table_iter_next(
2635                         &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
2636         {
2637                 const GValue *gval;
2638
2639                 gval = ghb_settings_get_value(ud->settings, key);
2640                 if (gval == NULL)
2641                 {
2642                         continue;
2643                 }
2644                 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
2645         }
2646         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(name));
2647         if (replace)
2648                 presets_list_update_item(ud, indices, len);
2649         else
2650         {
2651                 ghb_dict_insert(dict, g_strdup("Default"), 
2652                                                 ghb_boolean_value_new(FALSE));
2653                 presets_list_insert(ud, indices, len);
2654         }
2655         store_presets();
2656         ud->dont_clear_presets = TRUE;
2657         // Make the new preset the selected item
2658         ghb_select_preset2(ud->builder, indices, len);
2659         g_free(indices);
2660         ud->dont_clear_presets = FALSE;
2661         return;
2662 }
2663
2664 static void
2665 folder_save(signal_user_data_t *ud, const GValue *path)
2666 {
2667         GValue *dict, *folder;
2668         gint *indices, len, count;
2669         const gchar *name;
2670
2671         count = ghb_array_len(path);
2672         name = g_value_get_string(ghb_array_get_nth(path, count-1));
2673         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
2674         if (indices)
2675         {
2676                 if (!ghb_presets_get_folder(presetsPlist, indices, len))
2677                 {
2678                         gchar *message;
2679                         message = g_strdup_printf(
2680                                                 "%s: Preset already exists.\n"
2681                                                 "You can not replace it with a folder.",
2682                                                 name);
2683                         ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
2684                         g_free(message);
2685                         g_free(indices);
2686                         return;
2687                 }
2688                 // Already exists, update its description
2689                 dict = presets_get_dict(presetsPlist, indices, len);
2690                 ghb_dict_insert(dict, g_strdup("PresetDescription"), 
2691                         ghb_value_dup(preset_dict_get_value(
2692                                 ud->settings, "PresetDescription")));
2693                 g_free(indices);
2694                 return;
2695         }
2696         else
2697         {
2698                 indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
2699                 if (indices)
2700                 {
2701                         dict = ghb_dict_value_new();
2702                         ghb_presets_insert(presetsPlist, dict, indices, len);
2703                 }
2704                 else
2705                 {
2706                         g_warning("failed to find insert path");
2707                         return;
2708                 }
2709         }
2710         ghb_dict_insert(dict, g_strdup("PresetDescription"), 
2711                 ghb_value_dup(preset_dict_get_value(
2712                         ud->settings, "PresetDescription")));
2713         ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(name));
2714         folder = ghb_array_value_new(8);
2715         ghb_dict_insert(dict, g_strdup("ChildrenArray"), folder);
2716         ghb_dict_insert(dict, g_strdup("Type"),
2717                                                         ghb_int64_value_new(PRESETS_CUSTOM));
2718         ghb_dict_insert(dict, g_strdup("Folder"), ghb_boolean_value_new(TRUE));
2719
2720         presets_list_insert(ud, indices, len);
2721         g_free(indices);
2722         store_presets();
2723         return;
2724 }
2725
2726 void
2727 ghb_presets_list_default(signal_user_data_t *ud)
2728 {
2729         GtkTreeView *treeview;
2730         GtkTreePath *treepath;
2731         GtkTreeIter iter;
2732         GtkTreeStore *store;
2733         gint *indices, len;
2734         
2735         g_debug("ghb_presets_list_default ()");
2736         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
2737         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
2738         indices = presets_find_default(presetsPlist, &len);
2739         if (indices == NULL) return;
2740         treepath = ghb_tree_path_new_from_indices(indices, len);
2741         if (treepath)
2742         {
2743                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
2744                 {
2745                         gtk_tree_store_set(store, &iter, 
2746                                                 1, 800, 
2747                                                 2, 2 ,
2748                                                 -1);
2749                 }
2750                 gtk_tree_path_free(treepath);
2751         }
2752         g_free(indices);
2753 }
2754
2755 void
2756 ghb_presets_list_clear_default(signal_user_data_t *ud)
2757 {
2758         GtkTreeView *treeview;
2759         GtkTreePath *treepath;
2760         GtkTreeIter iter;
2761         GtkTreeStore *store;
2762         gint *indices, len;
2763         
2764         g_debug("ghb_presets_list_clear_default ()");
2765         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
2766         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
2767         indices = presets_find_default(presetsPlist, &len);
2768         if (indices == NULL) return;
2769         treepath = ghb_tree_path_new_from_indices(indices, len);
2770         if (treepath)
2771         {
2772                 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath))
2773                 {
2774                         gtk_tree_store_set(store, &iter, 
2775                                                 1, 400, 
2776                                                 2, 0 ,
2777                                                 -1);
2778                 }
2779                 gtk_tree_path_free(treepath);
2780         }
2781         g_free(indices);
2782 }
2783
2784 static void
2785 update_audio_presets(signal_user_data_t *ud)
2786 {
2787         g_debug("update_audio_presets");
2788         const GValue *audio_list;
2789
2790         audio_list = ghb_settings_get_value(ud->settings, "audio_list");
2791         ghb_settings_set_value(ud->settings, "AudioList", audio_list);
2792 }
2793
2794 void
2795 enforce_preset_type(signal_user_data_t *ud, const GValue *path)
2796 {
2797         gint *indices, len;
2798         GtkWidget *normal, *folder;
2799         gboolean fold;
2800
2801         normal = GHB_WIDGET(ud->builder, "preset_type_normal");
2802         folder = GHB_WIDGET(ud->builder, "preset_type_folder");
2803         indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
2804         if (indices)
2805         {
2806                 fold = ghb_presets_get_folder(presetsPlist, indices, len);
2807                 if (fold)
2808                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(folder), 
2809                                                                         TRUE);
2810                 else
2811                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(normal), 
2812                                                                         TRUE);
2813                 gtk_widget_set_sensitive(folder,  fold);
2814                 gtk_widget_set_sensitive(normal,  !fold);
2815                 g_free(indices);
2816         }
2817         else
2818         {
2819                 gtk_widget_set_sensitive(folder, TRUE);
2820                 gtk_widget_set_sensitive(normal, TRUE);
2821         }
2822 }
2823
2824 void
2825 presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2826 {
2827         GtkWidget *dialog;
2828         GtkEntry *entry;
2829         GtkTextView *desc;
2830         GtkResponseType response;
2831         GValue *preset;
2832         const gchar *name = "";
2833         gint count, *indices, len;
2834
2835         g_debug("presets_save_clicked_cb ()");
2836         preset = ghb_settings_get_value (ud->settings, "preset_selection");
2837
2838         count = ghb_array_len(preset);
2839         if (count > 0)
2840                 name = g_value_get_string(ghb_array_get_nth(preset, count-1));
2841         else
2842                 count = 1;
2843         // Clear the description
2844         desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
2845         dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
2846         entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
2847         gtk_entry_set_text(entry, name);
2848         enforce_preset_type(ud, preset);
2849         response = gtk_dialog_run(GTK_DIALOG(dialog));
2850         gtk_widget_hide(dialog);
2851         if (response == GTK_RESPONSE_OK)
2852         {
2853                 // save the preset
2854                 const gchar *name = gtk_entry_get_text(entry);
2855                 GValue *dest;
2856
2857                 if (ghb_settings_get_boolean(ud->settings, "preset_type_folder"))
2858                 {
2859                         if (count > MAX_NESTED_PRESET-1)
2860                         {
2861                                 count = MAX_NESTED_PRESET-1;
2862                         }
2863                 }
2864                 dest = ghb_array_value_new(MAX_NESTED_PRESET);
2865                 indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
2866                 if (indices)
2867                 {
2868                         gint ptype;
2869
2870                         ptype = ghb_presets_get_type(presetsPlist, indices, len);
2871                         if (ptype == PRESETS_CUSTOM)
2872                         {
2873                                 ghb_array_copy(dest, preset, count-1);
2874                         }
2875                 }
2876                 ghb_array_append(dest, ghb_string_value_new(name));
2877
2878                 ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
2879                 if (ghb_settings_get_boolean(ud->settings, "preset_type_folder"))
2880                 {
2881                         folder_save(ud, dest);
2882                 }
2883                 else
2884                 {
2885                         // Construct the audio settings presets from the current audio list
2886                         update_audio_presets(ud);
2887                         settings_save(ud, dest);
2888                 }
2889                 ghb_value_free(dest);
2890         }
2891 }
2892
2893 void
2894 preset_type_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
2895 {
2896         ghb_widget_to_setting(ud->settings, widget);
2897 }
2898
2899 void
2900 preset_name_changed_cb(GtkWidget *entry, signal_user_data_t *ud)
2901 {
2902         gchar *name;
2903         GValue *preset, *dest;
2904         gint count;
2905
2906         preset = ghb_settings_get_value (ud->settings, "preset_selection");
2907         name = ghb_widget_string(entry);
2908         dest = ghb_value_dup(preset);
2909         count = ghb_array_len(dest);
2910         ghb_array_replace(dest, count-1, ghb_string_value_new(name));
2911         enforce_preset_type(ud, dest);
2912         ghb_value_free(dest);
2913 }
2914
2915 void
2916 presets_restore_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2917 {
2918         GValue *preset;
2919
2920         g_debug("presets_restore_clicked_cb ()");
2921         // Reload only the standard presets
2922         ghb_presets_reload(ud);
2923         // Updating the presets list shuffles things around
2924         // need to make sure the proper preset is selected
2925         preset = ghb_settings_get_value (ud->settings, "preset");
2926         ghb_select_preset(ud->builder, preset);
2927 }
2928
2929 void
2930 presets_remove_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2931 {
2932         GtkTreeView *treeview;
2933         GtkTreeSelection *selection;
2934         GtkTreeModel *store;
2935         GtkTreeIter iter;
2936         gchar *preset;
2937         GtkResponseType response;
2938
2939         g_debug("presets_remove_clicked_cb ()");
2940         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
2941         selection = gtk_tree_view_get_selection (treeview);
2942         if (gtk_tree_selection_get_selected(selection, &store, &iter))
2943         {
2944                 GtkWidget *dialog;
2945                 GtkTreePath *path;
2946                 gint *indices, len;
2947                 gboolean folder;
2948
2949                 gtk_tree_model_get(store, &iter, 0, &preset, -1);
2950                 path = gtk_tree_model_get_path(store, &iter);
2951                 indices = gtk_tree_path_get_indices(path);
2952                 len = gtk_tree_path_get_depth(path);
2953
2954                 folder = ghb_presets_get_folder(presetsPlist, indices, len);
2955                 dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
2956                                                         GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
2957                                                         "Confirm deletion of %s:\n\n%s", 
2958                                                         folder ? "folder" : "preset",
2959                                                         preset);
2960                 response = gtk_dialog_run(GTK_DIALOG(dialog));
2961                 gtk_widget_destroy (dialog);
2962                 if (response == GTK_RESPONSE_YES)
2963                 {
2964                         GtkTreeIter nextIter = iter;
2965                         gboolean valid = TRUE;
2966                         if (!gtk_tree_model_iter_next(store, &nextIter))
2967                         {
2968                                 if (!gtk_tree_model_iter_parent(store, &nextIter, &iter))
2969                                 {
2970                                         valid = FALSE;
2971                                 }
2972                         }
2973                         // Remove the selected item
2974                         // First unselect it so that selecting the new item works properly
2975                         gtk_tree_selection_unselect_iter (selection, &iter);
2976                         if (ghb_presets_remove(presetsPlist, indices, len))
2977                         {
2978                                 store_presets();
2979                                 presets_list_remove(ud, indices, len);
2980                         }
2981                         if (!valid)
2982                                 valid = gtk_tree_model_get_iter_first(store, &nextIter);
2983                         if (valid)
2984                         {
2985                                 gtk_tree_path_free(path);
2986                                 path = gtk_tree_model_get_path(store, &nextIter);
2987                                 indices = gtk_tree_path_get_indices(path);
2988                                 len = gtk_tree_path_get_depth(path);
2989                                 ghb_select_preset2(ud->builder, indices, len);
2990                         }
2991                 }
2992                 g_free(preset);
2993                 gtk_tree_path_free(path);
2994         }
2995 }
2996
2997 // controls where valid drop locations are
2998 gboolean
2999 presets_drag_motion_cb(
3000         GtkTreeView *tv,
3001         GdkDragContext *ctx,
3002         gint x,
3003         gint y,
3004         guint time,
3005         signal_user_data_t *ud)
3006 {
3007         GtkTreePath *path = NULL;
3008         GtkTreeViewDropPosition drop_pos;
3009         gint *indices, len;
3010         GtkTreeIter iter;
3011         GtkTreeView *srctv;
3012         GtkTreeModel *model;
3013         GtkTreeSelection *select;
3014         gint src_ptype, dst_ptype;
3015         gboolean src_folder, dst_folder;
3016         GValue *preset;
3017         gint tree_depth, ii;
3018
3019         // Get the type of the object being dragged
3020         srctv = GTK_TREE_VIEW(gtk_drag_get_source_widget(ctx));
3021         select = gtk_tree_view_get_selection (srctv);
3022         gtk_tree_selection_get_selected (select, &model, &iter);
3023         path = gtk_tree_model_get_path (model, &iter);
3024         indices = gtk_tree_path_get_indices(path);
3025         len = gtk_tree_path_get_depth(path);
3026
3027         preset = presets_get_dict(presetsPlist, indices, len);
3028         tree_depth = preset_tree_depth(preset);
3029
3030         src_ptype = ghb_presets_get_type(presetsPlist, indices, len);
3031         src_folder = ghb_presets_get_folder(presetsPlist, indices, len);
3032         gtk_tree_path_free(path);
3033
3034         if (src_folder && tree_depth == 1)
3035                 tree_depth = 2;
3036
3037         // The rest checks that the destination is a valid position
3038         // in the list.
3039         gtk_tree_view_get_dest_row_at_pos (tv, x, y, &path, &drop_pos);
3040         if (path == NULL)
3041         {
3042                 gdk_drag_status(ctx, 0, time);
3043                 return TRUE;
3044         }
3045         // Don't allow repositioning of builtin presets
3046         if (src_ptype != PRESETS_CUSTOM)
3047         {
3048                 gdk_drag_status(ctx, 0, time);
3049                 return TRUE;
3050         }
3051
3052         len = gtk_tree_path_get_depth(path);
3053         if (len+tree_depth-1 >= MAX_NESTED_PRESET)
3054         {
3055                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3056                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3057                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3058                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3059         }
3060         for (ii = len+tree_depth-1; ii > MAX_NESTED_PRESET; ii--)
3061                 gtk_tree_path_up(path);
3062         indices = gtk_tree_path_get_indices(path);
3063         len = gtk_tree_path_get_depth(path);
3064         dst_ptype = ghb_presets_get_type(presetsPlist, indices, len);
3065         dst_folder = ghb_presets_get_folder(presetsPlist, indices, len);
3066         // Don't allow mixing custom presets in the builtins
3067         if (dst_ptype != PRESETS_CUSTOM)
3068         {
3069                 gdk_drag_status(ctx, 0, time);
3070                 return TRUE;
3071         }
3072
3073         // Only allow *drop into* for folders
3074         if (!dst_folder)
3075         { 
3076                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3077                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3078                 if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3079                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3080         }
3081
3082         len = gtk_tree_path_get_depth(path);
3083         gtk_tree_view_set_drag_dest_row(tv, path, drop_pos);
3084         gtk_tree_path_free(path);
3085         gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
3086         return TRUE;
3087 }
3088
3089 void 
3090 presets_drag_cb(
3091         GtkTreeView *dstwidget, 
3092         GdkDragContext *dc, 
3093         gint x, gint y, 
3094         GtkSelectionData *selection_data, 
3095         guint info, guint t, 
3096         signal_user_data_t *ud)
3097 {
3098         GtkTreePath *path = NULL;
3099         GtkTreeViewDropPosition drop_pos;
3100         GtkTreeIter dstiter, srciter;
3101         gint *dst_indices, dst_len, *src_indices, src_len;
3102         gint src_ptype;
3103         gboolean src_folder, dst_folder;
3104         
3105         GtkTreeModel *dstmodel = gtk_tree_view_get_model(dstwidget);
3106                         
3107         g_debug("preset_drag_cb ()");
3108         // This doesn't work here for some reason...
3109         // gtk_tree_view_get_drag_dest_row(dstwidget, &path, &drop_pos);
3110         gtk_tree_view_get_dest_row_at_pos (dstwidget, x, y, &path, &drop_pos);
3111         // This little hack is needed because attempting to drop after
3112         // the last item gives us no path or drop_pos.
3113         if (path == NULL)
3114         {
3115                 gint n_children;
3116
3117                 n_children = gtk_tree_model_iter_n_children(dstmodel, NULL);
3118                 if (n_children)
3119                 {
3120                         drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3121                         path = gtk_tree_path_new_from_indices(n_children-1, -1);
3122                 }
3123                 else
3124                 {
3125                         drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3126                         path = gtk_tree_path_new_from_indices(0, -1);
3127                 }
3128         }
3129         if (path)
3130         {
3131                 GtkTreeView *srcwidget;
3132                 GtkTreeModel *srcmodel;
3133                 GtkTreeSelection *select;
3134                 GtkTreePath *srcpath = NULL;
3135                 GValue *preset;
3136                 gint tree_depth, ii;
3137
3138                 srcwidget = GTK_TREE_VIEW(gtk_drag_get_source_widget(dc));
3139                 select = gtk_tree_view_get_selection (srcwidget);
3140                 gtk_tree_selection_get_selected (select, &srcmodel, &srciter);
3141
3142                 srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
3143                 src_indices = gtk_tree_path_get_indices(srcpath);
3144                 src_len = gtk_tree_path_get_depth(srcpath);
3145                 src_ptype = ghb_presets_get_type(presetsPlist, src_indices, src_len);
3146                 src_folder = ghb_presets_get_folder(presetsPlist, src_indices, src_len);
3147                 preset = ghb_value_dup(
3148                                         presets_get_dict(presetsPlist, src_indices, src_len));
3149                 gtk_tree_path_free(srcpath);
3150
3151                 // Don't allow repositioning of builtin presets
3152                 if (src_ptype != PRESETS_CUSTOM)
3153                         return;
3154
3155                 tree_depth = preset_tree_depth(preset);
3156                 if (src_folder && tree_depth == 1)
3157                         tree_depth = 2;
3158
3159                 dst_len = gtk_tree_path_get_depth(path);
3160                 if (dst_len+tree_depth-1 >= MAX_NESTED_PRESET)
3161                 {
3162                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3163                                 drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3164                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3165                                 drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3166                 }
3167
3168                 for (ii = dst_len+tree_depth-1; ii > MAX_NESTED_PRESET; ii--)
3169                         gtk_tree_path_up(path);
3170                 dst_indices = gtk_tree_path_get_indices(path);
3171                 dst_len = gtk_tree_path_get_depth(path);
3172                 dst_folder = ghb_presets_get_folder(presetsPlist, dst_indices, dst_len);
3173                 // Only allow *drop into* for folders
3174                 if (!dst_folder)
3175                 { 
3176                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
3177                                 drop_pos = GTK_TREE_VIEW_DROP_BEFORE;
3178                         if (drop_pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
3179                                 drop_pos = GTK_TREE_VIEW_DROP_AFTER;
3180                 }
3181                 if (gtk_tree_model_get_iter (dstmodel, &dstiter, path))
3182                 {
3183                         GtkTreeIter iter;
3184                         GtkTreePath *dstpath = NULL;
3185
3186                         switch (drop_pos)
3187                         {
3188                                 case GTK_TREE_VIEW_DROP_BEFORE:
3189                                         gtk_tree_store_insert_before(GTK_TREE_STORE (dstmodel), 
3190                                                                                                 &iter, NULL, &dstiter);
3191                                         break;
3192
3193                                 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
3194                                         gtk_tree_store_insert(GTK_TREE_STORE (dstmodel), 
3195                                                                                                 &iter, &dstiter, 0);
3196                                         break;
3197
3198                                 case GTK_TREE_VIEW_DROP_AFTER:
3199                                         gtk_tree_store_insert_after(GTK_TREE_STORE (dstmodel), 
3200                                                                                                 &iter, NULL, &dstiter);
3201                                         break;
3202
3203                                 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
3204                                         gtk_tree_store_insert_after(GTK_TREE_STORE (dstmodel), 
3205                                                                                                 &iter, &dstiter, 0);
3206                                         break;
3207
3208                                 default:
3209                                         break;
3210                         }
3211
3212                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
3213                         dst_indices = gtk_tree_path_get_indices(dstpath);
3214                         dst_len = gtk_tree_path_get_depth(dstpath);
3215                         ghb_presets_insert(presetsPlist, preset, dst_indices, dst_len);
3216                         gtk_tree_path_free(dstpath);
3217
3218                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
3219                         src_indices = gtk_tree_path_get_indices(srcpath);
3220                         src_len = gtk_tree_path_get_depth(srcpath);
3221                         ghb_presets_remove(presetsPlist, src_indices, src_len);
3222                         gtk_tree_path_free(srcpath);
3223
3224                         gtk_tree_store_remove (GTK_TREE_STORE (srcmodel), &srciter);
3225
3226                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
3227                         dst_indices = gtk_tree_path_get_indices(dstpath);
3228                         dst_len = gtk_tree_path_get_depth(dstpath);
3229                         presets_list_update_item(ud, dst_indices, dst_len);
3230                         gtk_tree_path_free(dstpath);
3231
3232                         store_presets();
3233                 }
3234                 gtk_tree_path_free(path);
3235         }
3236 }
3237
3238 void
3239 presets_row_expanded_cb(
3240         GtkTreeView *treeview, 
3241         GtkTreeIter *iter, 
3242         GtkTreePath *path, 
3243         signal_user_data_t *ud)
3244 {
3245         gint *indices, len;
3246         gboolean expanded, folder;
3247
3248         expanded = gtk_tree_view_row_expanded(treeview, path);
3249         indices = gtk_tree_path_get_indices(path);
3250         len = gtk_tree_path_get_depth(path);
3251         folder = ghb_presets_get_folder(presetsPlist, indices, len);
3252         if (folder)
3253         {
3254                 presets_set_folder_open(expanded, indices, len);
3255         }
3256
3257         // Collapsing parent folder collapses all children
3258         if (!expanded)
3259         {
3260                 GValue *presets = NULL;
3261                 GValue *dict;
3262                 gint *more_indices, count, ii;
3263
3264                 more_indices = g_malloc((len+1)*sizeof(gint));
3265                 memcpy(more_indices, indices, len*sizeof(gint));
3266
3267                 presets = presets_get_folder(presetsPlist, indices, len);
3268                 count = ghb_array_len(presets);
3269                 for (ii = 0; ii < count; ii++)
3270                 {
3271                         dict = ghb_array_get_nth(presets, ii);
3272                         folder = ghb_preset_folder(dict);
3273                         if (folder)
3274                         {
3275                                 more_indices[len] = ii;
3276                                 presets_set_folder_open(expanded, more_indices, len+1);
3277                         }
3278                 }
3279                 g_free(more_indices);
3280         }
3281         store_presets();
3282 }
3283
3284 static void
3285 preset_update_title_deps(signal_user_data_t *ud, ghb_title_info_t *tinfo)
3286 {
3287         GtkWidget *widget;
3288
3289         ghb_ui_update(ud, "scale_width", 
3290                         ghb_int64_value(tinfo->width - tinfo->crop[2] - tinfo->crop[3]));
3291         // If anamorphic or keep_aspect, the hight will be automatically calculated
3292         gboolean keep_aspect, anamorphic;
3293         keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
3294         anamorphic = ghb_settings_get_boolean(ud->settings, "anamorphic");
3295         if (!(keep_aspect || anamorphic))
3296         {
3297                 ghb_ui_update(ud, "scale_height", 
3298                         ghb_int64_value(tinfo->height - tinfo->crop[0] - tinfo->crop[1]));
3299         }
3300
3301         // Set the limits of cropping.  hb_set_anamorphic_size crashes if
3302         // you pass it a cropped width or height == 0.
3303         gint bound;
3304         bound = tinfo->height / 2 - 2;
3305         widget = GHB_WIDGET (ud->builder, "PictureTopCrop");
3306         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3307         widget = GHB_WIDGET (ud->builder, "PictureBottomCrop");
3308         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3309         bound = tinfo->width / 2 - 2;
3310         widget = GHB_WIDGET (ud->builder, "PictureLeftCrop");
3311         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3312         widget = GHB_WIDGET (ud->builder, "PictureRightCrop");
3313         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
3314         if (ghb_settings_get_boolean(ud->settings, "PictureAutoCrop"))
3315         {
3316                 ghb_ui_update(ud, "PictureTopCrop", ghb_int64_value(tinfo->crop[0]));
3317                 ghb_ui_update(ud, "PictureBottomCrop", ghb_int64_value(tinfo->crop[1]));
3318                 ghb_ui_update(ud, "PictureLeftCrop", ghb_int64_value(tinfo->crop[2]));
3319                 ghb_ui_update(ud, "PictureRightCrop", ghb_int64_value(tinfo->crop[3]));
3320         }
3321 }
3322
3323 void
3324 presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_t *ud)
3325 {
3326         GtkTreeModel *store;
3327         GtkTreeIter iter;
3328         ghb_title_info_t tinfo;
3329         GtkWidget *widget;
3330         
3331         g_debug("presets_list_selection_changed_cb ()");
3332         widget = GHB_WIDGET (ud->builder, "presets_remove");
3333         if (gtk_tree_selection_get_selected(selection, &store, &iter))
3334         {
3335                 GtkTreePath *treepath;
3336                 gint *indices, len;
3337                 GValue *path;
3338                 gboolean folder;
3339
3340                 treepath = gtk_tree_model_get_path(store, &iter);
3341                 indices = gtk_tree_path_get_indices(treepath);
3342                 len = gtk_tree_path_get_depth(treepath);
3343
3344                 path = preset_path_from_indices(presetsPlist, indices, len);
3345                 ghb_settings_take_value(ud->settings, "preset_selection", path);
3346
3347                 folder = ghb_presets_get_folder(presetsPlist, indices, len);
3348                 if (!folder)
3349                 {
3350                         ud->dont_clear_presets = TRUE;
3351                         // Temporarily set the video_quality range to (0,100)
3352                         // This is needed so the video_quality value does not get
3353                         // truncated when set.  The range will be readjusted below
3354                         GtkWidget *qp = GHB_WIDGET(ud->builder, "VideoQualitySlider");
3355                         gtk_range_set_range (GTK_RANGE(qp), 0, 100);
3356                         gtk_scale_set_digits(GTK_SCALE(qp), 3);
3357                         // Clear the audio list prior to changing the preset.  Existing 
3358                         // audio can cause the container extension to be automatically 
3359                         // changed when it shouldn't be
3360                         ghb_clear_audio_list(ud);
3361                         ghb_set_preset_from_indices(ud, indices, len);
3362                         gtk_tree_path_free(treepath);
3363                         gint titleindex;
3364                         titleindex = ghb_settings_combo_int(ud->settings, "title");
3365                         ghb_set_pref_audio(titleindex, ud);
3366                         ghb_settings_set_boolean(ud->settings, "preset_modified", FALSE);
3367                         ud->dont_clear_presets = FALSE;
3368                         if (ghb_get_title_info (&tinfo, titleindex))
3369                         {
3370                                 preset_update_title_deps(ud, &tinfo);
3371                         }
3372                         ghb_set_scale (ud, GHB_SCALE_KEEP_NONE);
3373
3374                         gdouble vqmin, vqmax, step, page;
3375                         gint digits;
3376                         gboolean inverted;
3377
3378                         ghb_vquality_range(ud, &vqmin, &vqmax, &step, 
3379                                                                 &page, &digits, &inverted);
3380                         gtk_range_set_range (GTK_RANGE(qp), vqmin, vqmax);
3381                         gtk_range_set_increments (GTK_RANGE(qp), step, page);
3382                         gtk_scale_set_digits(GTK_SCALE(qp), digits);
3383                         gtk_range_set_inverted (GTK_RANGE(qp), inverted);
3384
3385                         gchar *text;
3386                         gint crop[4];
3387                         GtkWidget *crop_widget;
3388                         crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
3389                         crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
3390                         crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
3391                         crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
3392                         crop_widget = GHB_WIDGET (ud->builder, "crop_values");
3393                         text = g_strdup_printf("%d:%d:%d:%d", 
3394                                                                         crop[0], crop[1], crop[2], crop[3]);
3395                         gtk_label_set_text (GTK_LABEL(crop_widget), text);
3396                         g_free(text);
3397                 }
3398                 gtk_widget_set_sensitive(widget, TRUE);
3399         }
3400         else
3401         {
3402                 g_debug("No selection???  Perhaps unselected.");
3403                 gtk_widget_set_sensitive(widget, FALSE);
3404         }
3405 }
3406
3407 void
3408 ghb_clear_presets_selection(signal_user_data_t *ud)
3409 {
3410         GtkTreeView *treeview;
3411         GtkTreeSelection *selection;
3412         
3413         if (ud->dont_clear_presets) return;
3414         g_debug("ghb_clear_presets_selection()");
3415         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3416         selection = gtk_tree_view_get_selection (treeview);
3417         gtk_tree_selection_unselect_all (selection);
3418         ghb_settings_set_boolean(ud->settings, "preset_modified", TRUE);
3419 }
3420
3421 void
3422 presets_frame_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, signal_user_data_t *ud)
3423 {
3424         GtkTreeView *treeview;
3425         GtkTreeSelection *selection;
3426         GtkTreeModel *store;
3427         GtkTreeIter iter;
3428         
3429         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
3430         selection = gtk_tree_view_get_selection(treeview);
3431         if (gtk_tree_selection_get_selected(selection, &store, &iter))
3432         {
3433                 GtkTreePath *path;
3434                 path = gtk_tree_model_get_path (store, &iter);
3435                 // Make the parent visible in scroll window if it is not.
3436                 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0, 0);
3437                 gtk_tree_path_free(path);
3438         }
3439 }
3440
3441 void
3442 presets_default_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
3443 {
3444         GValue *preset;
3445         gint *indices, len;
3446
3447         g_debug("presets_default_clicked_cb ()");
3448         preset = ghb_settings_get_value(ud->settings, "preset_selection");
3449         indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
3450         if (indices)
3451         {
3452                 if (!ghb_presets_get_folder(presetsPlist, indices, len))
3453                 {
3454                         ghb_presets_list_clear_default(ud);
3455                         presets_set_default(indices, len);
3456                         ghb_presets_list_default(ud);
3457                 }
3458                 g_free(indices);
3459         }
3460 }
3461