OSDN Git Service

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