OSDN Git Service

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