OSDN Git Service

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