OSDN Git Service

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