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>
21 #include "resources.h"
25 static GValue *presetsPlist = NULL;
26 static GValue *internalPlist = NULL;
27 static GValue *prefsPlist = NULL;
30 plist_get_dict(GValue *presets, const gchar *name)
32 if (presets == NULL || name == NULL) return NULL;
33 return ghb_dict_lookup(presets, name);
37 ghb_set_preset_default(GValue *settings)
41 preset = ghb_settings_get_string (settings, "preset");
42 ghb_settings_set_string(settings, "default_preset", preset);
43 ghb_prefs_save(settings);
47 // Used for sorting dictionaries.
49 key_cmp(gconstpointer a, gconstpointer b)
51 gchar *stra = (gchar*)a;
52 gchar *strb = (gchar*)b;
54 return strcmp(stra, strb);
58 ghb_presets_get_description(const gchar *name)
61 pdict = plist_get_dict(presetsPlist, name);
62 if (pdict == NULL) return g_strdup("");
63 return ghb_value_string(ghb_dict_lookup(pdict, "preset_description"));
67 preset_dict_get_value(
71 const GValue *gval = NULL;
75 gval = ghb_dict_lookup(dict, key);
77 if (internalPlist == NULL) return NULL;
80 dict = plist_get_dict(internalPlist, "Presets");
81 if (dict == NULL) return NULL;
82 gval = ghb_dict_lookup(dict, key);
94 dict = plist_get_dict(presetsPlist, name);
95 return preset_dict_get_value(dict, key);
99 ghb_presets_get_names()
103 GList *standard = NULL;
104 GList *custom = NULL;
106 if (presetsPlist == NULL) return NULL;
107 dict = g_value_get_boxed(presetsPlist);
108 link = names = g_hash_table_get_keys(dict);
114 name = (gchar*)link->data;
115 ptype = ghb_value_int(preset_get_value(name, "preset_type"));
117 custom = g_list_append(custom, name);
119 standard = g_list_append(standard, name);
122 custom = g_list_sort(custom, key_cmp);
123 standard = g_list_sort(standard, key_cmp);
125 names = g_list_concat(standard, custom);
130 ghb_preset_flags(const gchar *name)
137 dict = plist_get_dict(presetsPlist, name);
138 gval = preset_dict_get_value(dict, "preset_type");
141 ptype = ghb_value_int(gval);
142 ret = (ptype != 0 ? PRESET_CUSTOM : 0);
147 static void init_settings_from_dict(
148 GValue *dest, GValue *internal, GValue *dict);
151 init_settings_from_array(
159 count = ghb_array_len(array);
160 // The first element of the internal version is always the
161 // template for the allowed values
162 gval = ghb_array_get_nth(internal, 0);
163 for (ii = 0; ii < count; ii++)
166 val = ghb_array_get_nth(array, ii);
169 if (G_VALUE_TYPE(gval) == ghb_dict_get_type())
172 new_dict = ghb_dict_value_new();
173 ghb_array_append(dest, new_dict);
174 if (G_VALUE_TYPE(val) == ghb_dict_get_type())
175 init_settings_from_dict(new_dict, gval, val);
177 init_settings_from_dict(new_dict, gval, gval);
179 else if (G_VALUE_TYPE(gval) == ghb_array_get_type())
182 new_array = ghb_array_value_new(8);
183 ghb_array_append(dest, new_array);
184 if (G_VALUE_TYPE(val) == ghb_array_get_type())
185 init_settings_from_array(new_array, gval, val);
187 init_settings_from_array(new_array, gval, gval);
191 ghb_array_append(dest, val);
197 init_settings_from_dict(
206 ghb_dict_iter_init(&iter, internal);
207 // middle (void*) cast prevents gcc warning "defreferencing type-punned
208 // pointer will break strict-aliasing rules"
209 while (g_hash_table_iter_next(
210 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
214 val = ghb_dict_lookup(dict, key);
217 if (G_VALUE_TYPE(gval) == ghb_dict_get_type())
220 new_dict = ghb_dict_value_new();
221 ghb_settings_take_value(dest, key, new_dict);
222 if (G_VALUE_TYPE(val) == ghb_dict_get_type())
223 init_settings_from_dict(new_dict, gval, val);
225 init_settings_from_dict(new_dict, gval, gval);
227 else if (G_VALUE_TYPE(gval) == ghb_array_get_type())
230 new_array = ghb_array_value_new(8);
231 ghb_settings_take_value(dest, key, new_array);
232 if (G_VALUE_TYPE(val) == ghb_array_get_type())
233 init_settings_from_array(new_array, gval, val);
235 init_settings_from_array(new_array, gval, gval);
240 ghb_settings_set_value(dest, key, val);
247 signal_user_data_t *ud,
255 ghb_dict_iter_init(&iter, internal);
256 // middle (void*) cast prevents gcc warning "defreferencing type-punned
257 // pointer will break strict-aliasing rules"
258 while (g_hash_table_iter_next(
259 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
263 val = ghb_dict_lookup(dict, key);
266 ghb_ui_update(ud, key, val);
271 preset_to_ui(signal_user_data_t *ud, GValue *dict)
273 g_debug("preset_to_ui()\n");
274 // Initialize the ui from presets file.
277 // Get key list from internal default presets. This way we do not
278 // load any unknown keys.
279 if (internalPlist == NULL) return;
280 internal = plist_get_dict(internalPlist, "Presets");
281 // Setting a ui widget will cause the corresponding setting
282 // to be set, but it also triggers a callback that can
283 // have the side effect of using other settings values
284 // that have not yet been set. So set *all* settings first
285 // then update the ui.
286 init_settings_from_dict(ud->settings, internal, dict);
287 init_ui_from_dict(ud, internal, dict);
289 if (ghb_settings_get_boolean(ud->settings, "allow_tweaks"))
292 gval = preset_dict_get_value(dict, "deinterlace");
295 ghb_ui_update(ud, "tweak_deinterlace", gval);
297 gval = preset_dict_get_value(dict, "denoise");
300 ghb_ui_update(ud, "tweak_denoise", gval);
306 ghb_set_preset(signal_user_data_t *ud, const gchar *name)
310 g_debug("ghb_set_preset() %s\n", name);
314 // Try to get the first preset
315 presets = ghb_presets_get_names();
318 name = (const gchar*)presets->data;
319 g_list_free(presets);
322 dict = plist_get_dict(presetsPlist, name);
323 if (dict == NULL || name == NULL)
325 preset_to_ui(ud, NULL);
329 preset_to_ui(ud, dict);
330 ghb_settings_set_string(ud->settings, "preset", name);
335 ghb_update_from_preset(
336 signal_user_data_t *ud,
342 g_debug("ghb_update_from_preset() %s %s", name, key);
343 if (name == NULL) return;
344 gval = preset_get_value(name, key);
347 ghb_ui_update(ud, key, gval);
352 store_plist(GValue *plist, const gchar *name)
358 dir = g_get_user_config_dir();
359 config = g_strdup_printf ("%s/ghb", dir);
360 if (!g_file_test(config, G_FILE_TEST_IS_DIR))
362 g_mkdir (config, 0755);
365 config = g_strdup_printf ("%s/ghb/%s", dir, name);
366 file = g_fopen(config, "w");
368 ghb_plist_write(file, plist);
373 load_plist(const gchar *name)
377 GValue *plist = NULL;
379 dir = g_get_user_config_dir();
380 config = g_strdup_printf ("%s/ghb/%s", dir, name);
381 if (g_file_test(config, G_FILE_TEST_IS_REGULAR))
383 plist = ghb_plist_parse_file(config);
390 remove_plist(const gchar *name)
395 dir = g_get_user_config_dir();
396 config = g_strdup_printf ("%s/ghb/%s", dir, name);
397 if (g_file_test(config, G_FILE_TEST_IS_REGULAR))
404 static gboolean prefs_initializing = FALSE;
407 ghb_prefs_to_ui(signal_user_data_t *ud)
412 GValue *internal, *dict;
416 prefs_initializing = TRUE;
418 // Setting a ui widget will cause the corresponding setting
419 // to be set, but it also triggers a callback that can
420 // have the side effect of using other settings values
421 // that have not yet been set. So set *all* settings first
422 // then update the ui.
423 internal = plist_get_dict(internalPlist, "Initialization");
424 ghb_dict_iter_init(&iter, internal);
425 // middle (void*) cast prevents gcc warning "defreferencing type-punned
426 // pointer will break strict-aliasing rules"
427 while (g_hash_table_iter_next(
428 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
430 ghb_ui_update(ud, key, gval);
433 dict = plist_get_dict(prefsPlist, "Preferences");
434 internal = plist_get_dict(internalPlist, "Preferences");
435 ghb_dict_iter_init(&iter, internal);
436 // middle (void*) cast prevents gcc warning "defreferencing type-punned
437 // pointer will break strict-aliasing rules"
438 while (g_hash_table_iter_next(
439 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
441 const GValue *value = NULL;
443 value = ghb_dict_lookup(dict, key);
446 ghb_settings_set_value(ud->settings, key, value);
448 internal = plist_get_dict(internalPlist, "Preferences");
449 ghb_dict_iter_init(&iter, internal);
450 // middle (void*) cast prevents gcc warning "defreferencing type-punned
451 // pointer will break strict-aliasing rules"
452 while (g_hash_table_iter_next(
453 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
455 const GValue *value = NULL;
457 value = ghb_dict_lookup(dict, key);
460 ghb_ui_update(ud, key, value);
463 val = ghb_settings_get_value(ud->settings, "show_presets");
464 ghb_ui_update(ud, "show_presets", val);
465 if (ghb_settings_get_boolean(ud->settings, "hbfd_feature"))
468 val = ghb_settings_get_value(ud->settings, "hbfd");
469 ghb_ui_update(ud, "hbfd", val);
470 action = GHB_ACTION (ud->builder, "hbfd");
471 gtk_action_set_visible(action, TRUE);
475 ghb_ui_update(ud, "hbfd", ghb_int64_value(0));
477 gval = ghb_settings_get_value(ud->settings, "default_source");
478 ghb_settings_set_value (ud->settings, "source", gval);
479 str = ghb_settings_get_string(ud->settings, "destination_dir");
481 gchar *path = g_strdup_printf ("%s/new_video.mp4", str);
482 ghb_ui_update(ud, "destination", ghb_string_value(path));
486 prefs_initializing = FALSE;
490 ghb_prefs_save(GValue *settings)
498 if (prefs_initializing) return;
499 dict = plist_get_dict(internalPlist, "Preferences");
500 if (dict == NULL) return;
501 pref_dict = plist_get_dict(prefsPlist, "Preferences");
502 if (pref_dict == NULL) return;
503 ghb_dict_iter_init(&iter, dict);
504 // middle (void*) cast prevents gcc warning "defreferencing type-punned
505 // pointer will break strict-aliasing rules"
506 while (g_hash_table_iter_next(
507 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
509 value = ghb_settings_get_value(settings, key);
512 ghb_dict_insert(pref_dict, g_strdup(key), ghb_value_dup(value));
515 store_plist(prefsPlist, "preferences");
519 ghb_pref_save(GValue *settings, const gchar *key)
523 if (prefs_initializing) return;
524 value = ghb_settings_get_value(settings, key);
528 dict = plist_get_dict(prefsPlist, "Preferences");
529 if (dict == NULL) return;
530 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
531 store_plist(prefsPlist, "preferences");
536 ghb_settings_init(signal_user_data_t *ud)
544 g_debug("ghb_settings_init");
545 prefs_initializing = TRUE;
547 internalPlist = ghb_resource_get("internal-defaults");
548 // Setting a ui widget will cause the corresponding setting
549 // to be set, but it also triggers a callback that can
550 // have the side effect of using other settings values
551 // that have not yet been set. So set *all* settings first
552 // then update the ui.
553 internal = plist_get_dict(internalPlist, "Initialization");
554 ghb_dict_iter_init(&iter, internal);
555 // middle (void*) cast prevents gcc warning "defreferencing type-punned
556 // pointer will break strict-aliasing rules"
557 while (g_hash_table_iter_next(
558 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
560 ghb_settings_set_value(ud->settings, key, gval);
563 internal = plist_get_dict(internalPlist, "Presets");
564 ghb_dict_iter_init(&iter, internal);
565 // middle (void*) cast prevents gcc warning "defreferencing type-punned
566 // pointer will break strict-aliasing rules"
567 while (g_hash_table_iter_next(
568 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
570 ghb_settings_set_value(ud->settings, key, gval);
573 internal = plist_get_dict(internalPlist, "Preferences");
574 ghb_dict_iter_init(&iter, internal);
575 // middle (void*) cast prevents gcc warning "defreferencing type-punned
576 // pointer will break strict-aliasing rules"
577 while (g_hash_table_iter_next(
578 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
580 ghb_settings_set_value(ud->settings, key, gval);
582 prefs_initializing = FALSE;
589 //ghb_value_free(internalPlist);
591 ghb_value_free(presetsPlist);
593 ghb_value_free(prefsPlist);
597 ghb_prefs_load(signal_user_data_t *ud)
599 GValue *dict, *internal;
604 g_debug("ghb_prefs_load");
605 prefsPlist = load_plist("preferences");
606 if (prefsPlist == NULL)
607 prefsPlist = ghb_dict_value_new();
608 dict = plist_get_dict(prefsPlist, "Preferences");
609 internal = plist_get_dict(internalPlist, "Preferences");
610 if (dict == NULL && internal)
612 dict = ghb_dict_value_new();
613 ghb_dict_insert(prefsPlist, g_strdup("Preferences"), dict);
615 // Get defaults from internal defaults
616 ghb_dict_iter_init(&iter, internal);
617 // middle (void*) cast prevents gcc warning "defreferencing type-punned
618 // pointer will break strict-aliasing rules"
619 while (g_hash_table_iter_next(
620 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&gval))
622 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
624 const gchar *dir = g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS);
629 ghb_dict_insert(dict,
630 g_strdup("destination_dir"), ghb_value_dup(ghb_string_value(dir)));
631 store_plist(prefsPlist, "preferences");
636 ghb_presets_reload(signal_user_data_t *ud)
638 GValue *std_dict, *dict;
639 GHashTableIter std_iter;
641 g_debug("ghb_presets_reload()\n");
642 std_dict = ghb_resource_get("standard-presets");
643 if (std_dict == NULL) return;
645 // Merge the keyfile contents into our presets
649 ghb_dict_iter_init(&std_iter, std_dict);
650 // middle (void*) cast prevents gcc warning "defreferencing type-punned
651 // pointer will break strict-aliasing rules"
652 while (g_hash_table_iter_next(
653 &std_iter, (gpointer*)(void*)&name, (gpointer*)(void*)&orig_dict))
659 dict = ghb_dict_value_new();
660 ghb_dict_insert(presetsPlist, g_strdup(name), dict);
661 ghb_dict_iter_init(&iter, orig_dict);
662 // middle (void*) cast prevents gcc warning "defreferencing type-punned
663 // pointer will break strict-aliasing rules"
664 while (g_hash_table_iter_next(
665 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
667 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(value));
670 store_plist(presetsPlist, "presets");
676 g_debug("presets_store ()\n");
677 store_plist(presetsPlist, "presets");
681 ghb_save_queue(GValue *queue)
683 store_plist(queue, "queue");
689 return load_plist("queue");
693 ghb_remove_queue_file()
695 remove_plist("queue");
701 presetsPlist = load_plist("presets");
702 if (presetsPlist == NULL)
704 presetsPlist = ghb_value_dup(ghb_resource_get("standard-presets"));
710 ghb_settings_save(signal_user_data_t *ud, const gchar *name)
712 GValue *dict, *internal;
718 if (internalPlist == NULL) return;
719 if (ghb_settings_get_boolean(ud->settings, "allow_tweaks"))
722 str = ghb_settings_get_string(ud->settings, "tweak_deinterlace");
725 ghb_settings_set_string(ud->settings, "deinterlace", str);
728 str = ghb_settings_get_string(ud->settings, "tweak_denoise");
731 ghb_settings_set_string(ud->settings, "denoise", str);
735 autoscale = ghb_settings_get_boolean(ud->settings, "autoscale");
736 ghb_settings_set_int64(ud->settings, "preset_type", 1);
738 dict = ghb_dict_value_new();
739 ghb_dict_insert(presetsPlist, g_strdup(name), dict);
740 internal = plist_get_dict(internalPlist, "Presets");
742 ghb_dict_iter_init(&iter, internal);
743 // middle (void*) cast prevents gcc warning "defreferencing type-punned
744 // pointer will break strict-aliasing rules"
745 while (g_hash_table_iter_next(
746 &iter, (gpointer*)(void*)&key, (gpointer*)(void*)&value))
750 if (strcmp(key, "scale_width"))
754 if (strcmp(key, "scale_height"))
760 gval = ghb_settings_get_value(ud->settings, key);
763 g_debug("Setting (%s) is not in defaults\n", (gchar*)key);
766 if (ghb_value_cmp(gval, value) != 0)
768 // Differs from default value. Store it.
769 ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
773 ud->dont_clear_presets = TRUE;
774 ghb_set_preset (ud, name);
775 ud->dont_clear_presets = FALSE;
779 ghb_presets_remove(const gchar *name)
781 if (ghb_dict_lookup(presetsPlist, name))
783 ghb_dict_remove(presetsPlist, name);