OSDN Git Service

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