OSDN Git Service

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