1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
6 * presets.c is free software.
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)
15 #include <glib-object.h>
16 #include <glib/gstdio.h>
25 // Internal defaults stored in character arrays and parsed
26 // the same as external settings files.
27 const gchar defaultSettings[] =
28 #include "internal_defaults.h"
30 const gchar standardPresets[] =
31 #include "standard_presets.h"
34 static GValue *presetsPlist = NULL;
35 static GValue *internalPlist = NULL;
36 static GValue *prefsPlist = NULL;
39 plist_get_dict(GValue *presets, const gchar *name)
41 if (presets == NULL || name == NULL) return NULL;
42 return ghb_dict_lookup(presets, name);
46 ghb_set_preset_default(GValue *settings)
50 preset = ghb_settings_get_string (settings, "preset");
51 ghb_settings_set_string(settings, "default_preset", preset);
52 ghb_prefs_save(settings);
56 // Used for sorting dictionaries.
58 key_cmp(gconstpointer a, gconstpointer b)
60 gchar *stra = (gchar*)a;
61 gchar *strb = (gchar*)b;
63 return strcmp(stra, strb);
67 ghb_presets_get_description(const gchar *name)
70 pdict = plist_get_dict(presetsPlist, name);
71 if (pdict == NULL) return g_strdup("");
72 return ghb_value_string(ghb_dict_lookup(pdict, "preset_description"));
76 preset_dict_get_value(
80 const GValue *gval = NULL;
84 gval = ghb_dict_lookup(dict, key);
86 if (internalPlist == NULL) return NULL;
89 dict = plist_get_dict(internalPlist, "Presets");
90 if (dict == NULL) return NULL;
91 gval = ghb_dict_lookup(dict, key);
103 dict = plist_get_dict(presetsPlist, name);
104 return preset_dict_get_value(dict, key);
108 ghb_presets_get_names()
112 GList *standard = NULL;
113 GList *custom = NULL;
115 if (presetsPlist == NULL) return NULL;
116 dict = g_value_get_boxed(presetsPlist);
117 link = names = g_hash_table_get_keys(dict);
123 name = (gchar*)link->data;
124 ptype = ghb_value_int(preset_get_value(name, "preset_type"));
126 custom = g_list_append(custom, name);
128 standard = g_list_append(standard, name);
131 custom = g_list_sort(custom, key_cmp);
132 standard = g_list_sort(standard, key_cmp);
134 names = g_list_concat(standard, custom);
139 ghb_preset_flags(const gchar *name)
146 dict = plist_get_dict(presetsPlist, name);
147 gval = preset_dict_get_value(dict, "preset_type");
150 ptype = ghb_value_int(gval);
151 ret = (ptype != 0 ? PRESET_CUSTOM : 0);
156 static void init_settings_from_dict(
157 GValue *dest, GValue *internal, GValue *dict);
160 init_settings_from_array(
168 count = ghb_array_len(array);
169 // The first element of the internal version is always the
170 // template for the allowed values
171 gval = ghb_array_get_nth(internal, 0);
172 for (ii = 0; ii < count; ii++)
175 val = ghb_array_get_nth(array, ii);
178 if (G_VALUE_TYPE(gval) == ghb_dict_get_type())
181 new_dict = ghb_dict_value_new();
182 ghb_array_append(dest, new_dict);
183 if (G_VALUE_TYPE(val) == ghb_dict_get_type())
184 init_settings_from_dict(new_dict, gval, val);
186 init_settings_from_dict(new_dict, gval, gval);
188 else if (G_VALUE_TYPE(gval) == ghb_array_get_type())
191 new_array = ghb_array_value_new(8);
192 ghb_array_append(dest, new_array);
193 if (G_VALUE_TYPE(val) == ghb_array_get_type())
194 init_settings_from_array(new_array, gval, val);
196 init_settings_from_array(new_array, gval, gval);
200 ghb_array_append(dest, val);
206 init_settings_from_dict(
215 ghb_dict_iter_init(&iter, internal);
216 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
220 val = ghb_dict_lookup(dict, key);
223 if (G_VALUE_TYPE(gval) == ghb_dict_get_type())
226 new_dict = ghb_dict_value_new();
227 ghb_settings_take_value(dest, key, new_dict);
228 if (G_VALUE_TYPE(val) == ghb_dict_get_type())
229 init_settings_from_dict(new_dict, gval, val);
231 init_settings_from_dict(new_dict, gval, gval);
233 else if (G_VALUE_TYPE(gval) == ghb_array_get_type())
236 new_array = ghb_array_value_new(8);
237 ghb_settings_take_value(dest, key, new_array);
238 if (G_VALUE_TYPE(val) == ghb_array_get_type())
239 init_settings_from_array(new_array, gval, val);
241 init_settings_from_array(new_array, gval, gval);
246 ghb_settings_set_value(dest, key, val);
253 signal_user_data_t *ud,
261 ghb_dict_iter_init(&iter, internal);
262 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
266 val = ghb_dict_lookup(dict, key);
269 ghb_ui_update(ud, key, val);
274 preset_to_ui(signal_user_data_t *ud, GValue *dict)
276 g_debug("preset_to_ui()\n");
277 // Initialize the ui from presets file.
280 // Get key list from internal default presets. This way we do not
281 // load any unknown keys.
282 if (internalPlist == NULL) return;
283 internal = plist_get_dict(internalPlist, "Presets");
284 // Setting a ui widget will cause the corresponding setting
285 // to be set, but it also triggers a callback that can
286 // have the side effect of using other settings values
287 // that have not yet been set. So set *all* settings first
288 // then update the ui.
289 init_settings_from_dict(ud->settings, internal, dict);
290 init_ui_from_dict(ud, internal, dict);
292 if (ghb_settings_get_boolean(ud->settings, "allow_tweaks"))
295 gval = preset_dict_get_value(dict, "deinterlace");
298 ghb_ui_update(ud, "tweak_deinterlace", gval);
300 gval = preset_dict_get_value(dict, "denoise");
303 ghb_ui_update(ud, "tweak_denoise", gval);
309 ghb_set_preset(signal_user_data_t *ud, const gchar *name)
313 g_debug("ghb_set_preset() %s\n", name);
317 // Try to get the first preset
318 presets = ghb_presets_get_names();
321 name = (const gchar*)presets->data;
322 g_list_free(presets);
325 dict = plist_get_dict(presetsPlist, name);
326 if (dict == NULL || name == NULL)
328 preset_to_ui(ud, NULL);
332 preset_to_ui(ud, dict);
333 ghb_settings_set_string(ud->settings, "preset", name);
338 ghb_update_from_preset(
339 signal_user_data_t *ud,
345 g_debug("ghb_update_from_preset() %s %s", name, key);
346 if (name == NULL) return;
347 gval = preset_get_value(name, key);
350 ghb_ui_update(ud, key, gval);
355 store_plist(GValue *plist, const gchar *name)
361 dir = g_get_user_config_dir();
362 config = g_strdup_printf ("%s/ghb", dir);
363 if (!g_file_test(config, G_FILE_TEST_IS_DIR))
365 g_mkdir (config, 0755);
368 config = g_strdup_printf ("%s/ghb/%s", dir, name);
369 file = g_fopen(config, "w");
371 ghb_plist_write(file, plist);
375 static gboolean prefs_initializing = FALSE;
378 ghb_prefs_to_ui(signal_user_data_t *ud)
383 GValue *internal, *dict;
387 prefs_initializing = TRUE;
389 // Setting a ui widget will cause the corresponding setting
390 // to be set, but it also triggers a callback that can
391 // have the side effect of using other settings values
392 // that have not yet been set. So set *all* settings first
393 // then update the ui.
394 internal = plist_get_dict(internalPlist, "Initialization");
395 ghb_dict_iter_init(&iter, internal);
396 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
398 ghb_ui_update(ud, key, gval);
401 dict = plist_get_dict(prefsPlist, "Preferences");
402 internal = plist_get_dict(internalPlist, "Preferences");
403 ghb_dict_iter_init(&iter, internal);
404 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
406 const GValue *value = NULL;
408 value = ghb_dict_lookup(dict, key);
411 ghb_settings_set_value(ud->settings, key, value);
413 internal = plist_get_dict(internalPlist, "Preferences");
414 ghb_dict_iter_init(&iter, internal);
415 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
417 const GValue *value = NULL;
419 value = ghb_dict_lookup(dict, key);
422 ghb_ui_update(ud, key, value);
425 val = ghb_settings_get_value(ud->settings, "show_presets");
426 ghb_ui_update(ud, "show_presets", val);
427 if (ghb_settings_get_boolean(ud->settings, "hbfd_feature"))
430 val = ghb_settings_get_value(ud->settings, "hbfd");
431 ghb_ui_update(ud, "hbfd", val);
432 action = GHB_ACTION (ud->builder, "hbfd");
433 gtk_action_set_visible(action, TRUE);
437 ghb_ui_update(ud, "hbfd", ghb_int64_value(0));
439 gval = ghb_settings_get_value(ud->settings, "default_source");
440 ghb_settings_set_value (ud->settings, "source", gval);
441 str = ghb_settings_get_string(ud->settings, "destination_dir");
443 gchar *path = g_strdup_printf ("%s/new_video.mp4", str);
444 ghb_ui_update(ud, "destination", ghb_string_value(path));
448 prefs_initializing = FALSE;
452 ghb_prefs_save(GValue *settings)
460 if (prefs_initializing) return;
461 dict = plist_get_dict(internalPlist, "Preferences");
462 if (dict == NULL) return;
463 pref_dict = plist_get_dict(prefsPlist, "Preferences");
464 if (pref_dict == NULL) return;
465 ghb_dict_iter_init(&iter, dict);
466 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&value))
468 value = ghb_settings_get_value(settings, key);
471 ghb_dict_insert(pref_dict, g_strdup(key), ghb_value_dup(value));
474 store_plist(prefsPlist, "preferences");
478 ghb_pref_save(GValue *settings, const gchar *key)
482 if (prefs_initializing) return;
483 value = ghb_settings_get_value(settings, key);
487 dict = plist_get_dict(prefsPlist, "Preferences");
488 if (dict == NULL) return;
489 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
490 store_plist(prefsPlist, "preferences");
495 ghb_settings_init(signal_user_data_t *ud)
503 g_debug("ghb_settings_init");
504 prefs_initializing = TRUE;
506 internalPlist = ghb_plist_parse(defaultSettings, sizeof(defaultSettings)-1);
507 // Setting a ui widget will cause the corresponding setting
508 // to be set, but it also triggers a callback that can
509 // have the side effect of using other settings values
510 // that have not yet been set. So set *all* settings first
511 // then update the ui.
512 internal = plist_get_dict(internalPlist, "Initialization");
513 ghb_dict_iter_init(&iter, internal);
514 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
516 ghb_settings_set_value(ud->settings, key, gval);
519 internal = plist_get_dict(internalPlist, "Presets");
520 ghb_dict_iter_init(&iter, internal);
521 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
523 ghb_settings_set_value(ud->settings, key, gval);
526 internal = plist_get_dict(internalPlist, "Preferences");
527 ghb_dict_iter_init(&iter, internal);
528 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
530 ghb_settings_set_value(ud->settings, key, gval);
532 prefs_initializing = FALSE;
536 ghb_prefs_load(signal_user_data_t *ud)
540 GValue *dict, *internal;
545 g_debug("ghb_prefs_load");
546 dir = g_get_user_config_dir();
547 config = g_strdup_printf ("%s/ghb/preferences", dir);
548 if (g_file_test(config, G_FILE_TEST_IS_REGULAR))
550 prefsPlist = ghb_plist_parse_file(config);
551 if (prefsPlist == NULL)
552 prefsPlist = ghb_dict_value_new();
556 // Make an empty plist
557 prefsPlist = ghb_dict_value_new();
560 dict = plist_get_dict(prefsPlist, "Preferences");
561 internal = plist_get_dict(internalPlist, "Preferences");
562 if (dict == NULL && internal)
564 dict = ghb_dict_value_new();
565 ghb_dict_insert(prefsPlist, g_strdup("Preferences"), dict);
567 // Get defaults from internal defaults
568 ghb_dict_iter_init(&iter, internal);
569 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&gval))
571 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
573 const gchar *dir = g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS);
574 ghb_dict_insert(dict,
575 g_strdup("destination_dir"), ghb_value_dup(ghb_string_value(dir)));
576 store_plist(prefsPlist, "preferences");
582 ghb_presets_reload(signal_user_data_t *ud)
584 GValue *std_dict, *dict;
585 GHashTableIter std_iter;
587 g_debug("ghb_presets_reload()\n");
588 std_dict = ghb_plist_parse(standardPresets, sizeof(standardPresets)-1);
589 if (std_dict == NULL) return;
591 // Merge the keyfile contents into our presets
595 ghb_dict_iter_init(&std_iter, std_dict);
596 while (g_hash_table_iter_next(&std_iter, (gpointer*)&name, (gpointer*)&orig_dict))
602 dict = ghb_dict_value_new();
603 ghb_dict_insert(presetsPlist, g_strdup(name), dict);
604 ghb_dict_iter_init(&iter, orig_dict);
605 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&value))
607 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
610 ghb_value_free(std_dict);
611 store_plist(presetsPlist, "presets");
617 g_debug("presets_store ()\n");
618 store_plist(presetsPlist, "presets");
627 dir = g_get_user_config_dir();
628 config = g_strdup_printf ("%s/ghb/presets", dir);
629 if (g_file_test(config, G_FILE_TEST_IS_REGULAR))
631 presetsPlist = ghb_plist_parse_file(config);
632 if (presetsPlist == NULL)
634 presetsPlist = ghb_plist_parse(
635 standardPresets, sizeof(standardPresets)-1);
641 presetsPlist = ghb_plist_parse(
642 standardPresets, sizeof(standardPresets)-1);
648 ghb_settings_save(signal_user_data_t *ud, const gchar *name)
650 GValue *dict, *internal;
656 if (internalPlist == NULL) return;
657 if (ghb_settings_get_boolean(ud->settings, "allow_tweaks"))
660 str = ghb_settings_get_string(ud->settings, "tweak_deinterlace");
663 ghb_settings_set_string(ud->settings, "deinterlace", str);
666 str = ghb_settings_get_string(ud->settings, "tweak_denoise");
669 ghb_settings_set_string(ud->settings, "denoise", str);
673 autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
674 ghb_settings_set_int64(ud->settings, "preset_type", 1);
676 dict = ghb_dict_value_new();
677 ghb_dict_insert(presetsPlist, g_strdup(name), dict);
678 internal = plist_get_dict(internalPlist, "Presets");
680 ghb_dict_iter_init(&iter, internal);
681 while (g_hash_table_iter_next(&iter, (gpointer*)&key, (gpointer*)&value))
685 if (strcmp(key, "scale_width"))
689 if (strcmp(key, "scale_height"))
695 gval = ghb_settings_get_value(ud->settings, key);
698 g_debug("Setting (%s) is not in defaults\n", (gchar*)key);
701 if (ghb_value_cmp(gval, value) != 0)
703 // Differs from default value. Store it.
704 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
708 ud->dont_clear_presets = TRUE;
709 ghb_set_preset (ud, name);
710 ud->dont_clear_presets = FALSE;
714 ghb_presets_remove(const gchar *name)
716 if (ghb_dict_lookup(presetsPlist, name))
718 ghb_dict_remove(presetsPlist, name);