OSDN Git Service

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