OSDN Git Service

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