OSDN Git Service

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