OSDN Git Service

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