2 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
4 * gstpreset.c: helper interface for element presets
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 * @short_description: helper interface for element presets
26 * This interface offers methods to query and manipulate parameter preset sets.
27 * A preset is a bunch of property settings, together with meta data and a name.
28 * The name of a preset serves as key for subsequent method calls to manipulate
30 * All instances of one type will share the list of presets. The list is created
31 * on demand, if presets are not used, the list is not created.
33 * The interface comes with a default implementation that serves most plugins.
34 * Wrapper plugins will override most methods to implement support for the
35 * native preset format of those wrapped plugins.
36 * One method that is useful to be overridden is gst_preset_get_property_names().
37 * With that one can control which properties are saved and in which order.
38 * When implementing support for read-only presets, one should set the vmethods
39 * for gst_preset_save_preset() and gst_preset_delete_preset() to %NULL.
40 * Applications can use gst_preset_is_editable() to check for that.
42 * The default implementation supports presets located in a system directory,
43 * application specific directory and in the users home directory. When getting
44 * a list of presets individual presets are read and overlaid in 1) system,
45 * 2) application and 3) user order. Whenever an earlier entry is newer, the
46 * later entries will be updated. Since 1.8 you can also provide extra paths
47 * where to find presets through the GST_PRESET_PATH environment variable.
48 * Presets found in those paths will be concidered as "app presets".
52 * - we need to avoid two instances writing the preset file
53 * -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
55 * -> better save the new file to a tempfile and then rename?
56 * - we like to know when any other instance makes changes to the keyfile
57 * - then ui can be updated
58 * - and we make sure that we don't lose edits
59 * -> its the same problem actually, once for inside a process, once system-
61 * - can we use a lock inside a names shared memory segment?
63 * - should there be a 'preset-list' property to get the preset list
64 * (and to connect a notify:: to to listen for changes)
65 * we could use gnome_vfs_monitor_add() to monitor the user preset_file.
67 * - should there be a 'preset-name' property so that we can set a preset via
68 * gst-launch, or should we handle this with special syntax in gst-launch:
69 * gst-launch element preset:<preset-name> property=value ...
70 * - this would allow to have preset-bundles too (a preset on bins that
71 * specifies presets for children
74 #include "gst_private.h"
76 #include "gstpreset.h"
77 #include "gstchildproxy.h"
84 #include <glib/gstdio.h>
87 #define WIN32_LEAN_AND_MEAN
90 extern HMODULE _priv_gst_dll_handle;
93 #define GST_CAT_DEFAULT preset_debug
94 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
96 /* defines for keyfile usage, this group contains the element type name and
97 * version these presets belong to. */
98 #define PRESET_HEADER "_presets_"
100 /* keys of the preset header section */
101 #define PRESET_HEADER_ELEMENT_NAME "element-name"
102 #define PRESET_HEADER_VERSION "version"
104 static GQuark preset_user_path_quark = 0;
105 static GQuark preset_app_path_quark = 0;
106 static GQuark preset_system_path_quark = 0;
107 static GQuark preset_quark = 0;
109 /* the application can set a custom path that is checked in addition to standard
110 * system and user dirs. This helps to develop new presets first local to the
113 static gchar *preset_app_dir = NULL;
115 /* default iface implementation */
117 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
121 * @preset: a #GObject that implements #GstPreset
122 * @preset_user_path: (out) (allow-none): location for path or %NULL
123 * @preset_app_path: (out) (allow-none): location for path or %NULL
124 * @preset_system_path: (out) (allow-none): location for path or %NULL
126 * Fetch the preset_path for user local, application specific and system wide
127 * settings. Don't free after use.
129 * Returns: %FALSE if no paths could be found.
132 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
133 const gchar ** preset_app_path, const gchar ** preset_system_path)
135 GType type = G_TYPE_FROM_INSTANCE (preset);
137 const gchar *element_name;
139 /* we use the element name when we must construct the paths */
140 element_name = G_OBJECT_TYPE_NAME (preset);
141 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
143 if (preset_user_path) {
144 /* preset user path requested, see if we have it cached in the qdata */
145 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
148 /* user presets go in user's XDG data directory. */
149 preset_dir = g_build_filename (g_get_user_data_dir (),
150 "gstreamer-" GST_API_VERSION, "presets", NULL);
151 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
153 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
155 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
157 g_mkdir_with_parents (preset_dir, 0755);
160 /* cache the preset path to the type */
161 g_type_set_qdata (type, preset_user_path_quark, preset_path);
163 *preset_user_path = preset_path;
166 if (preset_app_path) {
167 if (preset_app_dir) {
168 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
169 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
170 preset_app_dir, element_name);
171 GST_INFO_OBJECT (preset, "application_preset_path: '%s'", preset_path);
173 /* cache the preset path to the type */
174 g_type_set_qdata (type, preset_app_path_quark, preset_path);
176 *preset_app_path = preset_path;
178 *preset_app_path = NULL;
182 if (preset_system_path) {
183 /* preset system path requested, see if we have it cached in the qdata */
184 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
187 /* system presets in '$GST_DATADIR/gstreamer-1.0/presets/GstAudioPanorama.prs' */
190 g_win32_get_package_installation_directory_of_module
191 (_priv_gst_dll_handle);
193 g_build_filename (basedir, "share", "gstreamer-" GST_API_VERSION,
197 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_API_VERSION,
200 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
201 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
202 preset_dir, element_name);
203 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
205 g_mkdir_with_parents (preset_dir, 0755);
208 /* cache the preset path to the type */
209 g_type_set_qdata (type, preset_system_path_quark, preset_path);
211 *preset_system_path = preset_path;
217 preset_skip_property (GParamSpec * property)
219 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
220 (property->flags & G_PARAM_CONSTRUCT_ONLY))
222 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
227 preset_parse_version (const gchar * str_version)
229 guint major, minor, micro, nano;
232 major = minor = micro = nano = 0;
234 /* parse version (e.g. 0.10.15.1) to guint64 */
235 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
236 /* make sure we have at least "major.minor" */
240 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
241 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
244 return G_GUINT64_CONSTANT (0);
248 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
249 guint64 * preset_version)
252 GError *error = NULL;
254 const gchar *element_name;
257 in = g_key_file_new ();
259 res = g_key_file_load_from_file (in, preset_path,
260 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
261 if (!res || error != NULL)
264 /* element type name and preset name must match or we are dealing with a wrong
266 element_name = G_OBJECT_TYPE_NAME (preset);
268 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
271 if (!name || strcmp (name, element_name))
276 /* get the version now so that the caller can check it */
277 if (preset_version) {
279 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
280 *preset_version = preset_parse_version (str);
289 GST_INFO_OBJECT (preset, "Unable to read preset file %s: %s",
290 preset_path, error->message);
291 g_error_free (error);
292 g_key_file_free (in);
297 GST_WARNING_OBJECT (preset,
298 "Wrong element name in preset file %s. Expected %s, got %s",
299 preset_path, element_name, GST_STR_NULL (name));
301 g_key_file_free (in);
307 preset_merge (GKeyFile * system, GKeyFile * user)
310 gchar **groups, **keys;
311 gsize i, j, num_groups, num_keys;
313 /* copy file comment if there is any */
314 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
315 g_key_file_set_comment (system, NULL, NULL, str, NULL);
319 /* get groups in user and copy into system */
320 groups = g_key_file_get_groups (user, &num_groups);
321 for (i = 0; i < num_groups; i++) {
322 /* copy group comment if there is any */
323 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
324 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
328 /* ignore private groups */
329 if (groups[i][0] == '_')
332 /* if group already exists in system, remove and re-add keys from user */
333 if (g_key_file_has_group (system, groups[i])) {
334 g_key_file_remove_group (system, groups[i], NULL);
337 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
338 for (j = 0; j < num_keys; j++) {
339 /* copy key comment if there is any */
340 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
341 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
344 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
345 g_key_file_set_value (system, groups[i], keys[j], str);
360 compare_preset_and_version (gconstpointer a, gconstpointer b,
363 const PresetAndVersion *pa = a, *pb = b;
365 if (pa->version > pb->version)
367 if (pa->version < pb->version)
373 /* reads the user and system presets files and merges them together. This
374 * function caches the GKeyFile on the element type. If there is no existing
375 * preset file, a new in-memory GKeyFile will be created. */
377 preset_get_keyfile (GstPreset * preset)
380 GType type = G_TYPE_FROM_INSTANCE (preset);
382 /* first see if the have a cached version for the type */
383 if (!(presets = g_type_get_qdata (type, preset_quark))) {
384 const gchar *preset_user_path, *preset_app_path, *preset_system_path;
385 guint64 version_system = G_GUINT64_CONSTANT (0);
386 guint64 version_app = G_GUINT64_CONSTANT (0);
387 guint64 version_user = G_GUINT64_CONSTANT (0);
388 guint64 version = G_GUINT64_CONSTANT (0);
389 gboolean merged = FALSE;
390 GKeyFile *in_user, *in_app = NULL, *in_system;
391 GQueue in_env = G_QUEUE_INIT;
392 gboolean have_env = FALSE;
395 /* try to load the user, app and system presets, we do this to get the
396 * versions of all files. */
397 preset_get_paths (preset, &preset_user_path, &preset_app_path,
398 &preset_system_path);
399 in_user = preset_open_and_parse_header (preset, preset_user_path,
402 if (preset_app_path) {
403 in_app = preset_open_and_parse_header (preset, preset_app_path,
407 envvar = g_getenv ("GST_PRESET_PATH");
410 gchar **preset_dirs = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, -1);
412 for (i = 0; preset_dirs[i]; i++) {
413 gchar *preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
414 preset_dirs[i], G_OBJECT_TYPE_NAME (preset));
418 env_file = preset_open_and_parse_header (preset, preset_path,
420 g_free (preset_path);
422 PresetAndVersion *pv = g_new (PresetAndVersion, 1);
423 pv->preset = env_file;
424 pv->version = env_version;
425 g_queue_push_tail (&in_env, pv);
429 g_strfreev (preset_dirs);
432 in_system = preset_open_and_parse_header (preset, preset_system_path,
435 /* compare version to check for merge */
438 version = version_system;
444 /* merge the ones from the environment paths. If any of them has a
445 * higher version, take that as the "master" version. Lower versions are
446 * then just merged in. */
447 g_queue_sort (&in_env, compare_preset_and_version, NULL);
448 /* highest version to lowest */
449 for (l = in_env.head; l; l = l->next) {
450 PresetAndVersion *pv = l->data;
452 if (version > pv->version) {
453 preset_merge (presets, pv->preset);
454 g_key_file_free (pv->preset);
457 g_key_file_free (presets);
458 presets = pv->preset;
459 version = pv->version;
463 g_queue_clear (&in_env);
467 /* if system/env version is higher, merge */
468 if (version > version_app) {
469 preset_merge (presets, in_app);
470 g_key_file_free (in_app);
473 g_key_file_free (presets);
475 version = version_app;
479 /* if system/env or app version is higher, merge */
480 if (version > version_user) {
481 preset_merge (presets, in_user);
482 g_key_file_free (in_user);
486 g_key_file_free (presets);
492 /* we did not load a user, app or system presets file, create a new one */
493 presets = g_key_file_new ();
494 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
495 G_OBJECT_TYPE_NAME (preset));
498 /* attach the preset to the type */
499 g_type_set_qdata (type, preset_quark, (gpointer) presets);
502 gst_preset_default_save_presets_file (preset);
509 compare_strings (gchar ** a, gchar ** b, gpointer user_data)
511 return g_strcmp0 (*a, *b);
514 /* get a list of all supported preset names for an element */
516 gst_preset_default_get_preset_names (GstPreset * preset)
522 /* get the presets from the type */
523 if (!(presets = preset_get_keyfile (preset)))
526 /* get the groups, which are also the preset names */
527 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
530 /* remove all private group names starting with '_' from the array */
531 for (i = 0; i < num_groups; i++) {
532 if (groups[i][0] == '_') {
533 /* free private group */
535 /* move last element of list down */
537 /* move last element into removed element */
538 groups[i] = groups[num_groups];
539 groups[num_groups] = NULL;
543 GST_INFO_OBJECT (preset, "Empty preset file");
548 /* sort the array now */
549 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
550 (GCompareDataFunc) compare_strings, NULL);
557 GST_WARNING_OBJECT (preset, "Could not load presets");
562 GST_WARNING_OBJECT (preset, "Could not find preset groups");
567 /* get a list of all property names that are used for presets */
569 gst_preset_default_get_property_names (GstPreset * preset)
572 guint i, j = 0, n_props;
573 GObjectClass *gclass;
574 gboolean is_child_proxy;
575 gchar **result = NULL;
577 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
578 is_child_proxy = GST_IS_CHILD_PROXY (preset);
580 /* get a list of object properties */
581 props = g_object_class_list_properties (gclass, &n_props);
583 /* allocate array big enough to hold the worst case, including a terminating
585 result = g_new (gchar *, n_props + 1);
587 /* now filter out the properties that we can use for presets */
588 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
589 for (i = 0; i < n_props; i++) {
590 if (preset_skip_property (props[i]))
592 GST_DEBUG_OBJECT (preset, " using: %s", props[i]->name);
593 result[j++] = g_strdup (props[i]->name);
598 if (is_child_proxy) {
602 n_children = gst_child_proxy_get_children_count ((GstChildProxy *) preset);
603 for (c = 0; c < n_children; c++) {
604 child = gst_child_proxy_get_child_by_index ((GstChildProxy *) preset, c);
605 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (child));
607 props = g_object_class_list_properties (gclass, &n_props);
609 /* resize property name array */
610 result = g_renew (gchar *, result, j + n_props + 1);
612 /* now filter out the properties that we can use for presets */
613 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
614 for (i = 0; i < n_props; i++) {
615 if (preset_skip_property (props[i]))
617 GST_DEBUG_OBJECT (preset, " using: %s::%s",
618 GST_OBJECT_NAME (child), props[i]->name);
619 result[j++] = g_strdup_printf ("%s::%s", GST_OBJECT_NAME (child),
625 g_object_unref (child);
629 GST_INFO_OBJECT (preset, "object has no properties");
636 /* load the presets of @name for the instance @preset. Returns %FALSE if something
639 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
644 GObjectClass *gclass;
645 gboolean is_child_proxy;
647 /* get the presets from the type */
648 if (!(presets = preset_get_keyfile (preset)))
651 /* get the preset name */
652 if (!g_key_file_has_group (presets, name))
655 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
657 /* get the properties that we can configure in this element */
658 if (!(props = gst_preset_get_property_names (preset)))
661 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
662 is_child_proxy = GST_IS_CHILD_PROXY (preset);
664 /* for each of the property names, find the preset parameter and try to
665 * configure the property with its value */
666 for (i = 0; props[i]; i++) {
668 GValue gvalue = { 0, };
669 GParamSpec *property = NULL;
671 /* check if we have a settings for this element property */
672 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
673 /* the element has a property but the parameter is not in the keyfile */
674 GST_INFO_OBJECT (preset, "parameter '%s' not in preset", props[i]);
678 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
681 if (is_child_proxy) {
682 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
685 property = g_object_class_find_property (gclass, props[i]);
688 /* the parameter was in the keyfile, the element said it supported it but
689 * then the property was not found in the element. This should not happen. */
690 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
695 /* try to deserialize the property value from the keyfile and set it as
696 * the object property */
697 g_value_init (&gvalue, property->value_type);
698 if (gst_value_deserialize (&gvalue, str)) {
699 if (is_child_proxy) {
700 gst_child_proxy_set_property ((GstChildProxy *) preset, props[i],
703 g_object_set_property ((GObject *) preset, props[i], &gvalue);
706 GST_WARNING_OBJECT (preset,
707 "deserialization of value '%s' for property '%s' failed", str,
710 g_value_unset (&gvalue);
720 GST_WARNING_OBJECT (preset, "no presets");
725 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
730 GST_INFO_OBJECT (preset, "no properties");
735 /* save the presets file. A copy of the existing presets file is stored in a
738 gst_preset_default_save_presets_file (GstPreset * preset)
741 const gchar *preset_path;
742 GError *error = NULL;
743 gchar *bak_file_name;
744 gboolean backup = TRUE;
748 preset_get_paths (preset, &preset_path, NULL, NULL);
750 /* get the presets from the type */
751 if (!(presets = preset_get_keyfile (preset)))
754 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
756 /* create backup if possible */
757 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
758 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
759 if (g_unlink (bak_file_name)) {
761 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
766 if (g_rename (preset_path, bak_file_name)) {
767 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
771 g_free (bak_file_name);
773 /* update gstreamer version */
774 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
777 /* get new contents, wee need this to save it */
778 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
782 if (!g_file_set_contents (preset_path, data, data_size, &error))
792 GST_WARNING_OBJECT (preset,
793 "no presets, trying to unlink possibly existing preset file: '%s'",
795 g_unlink (preset_path);
800 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
802 g_error_free (error);
808 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
809 preset_path, error->message);
810 g_error_free (error);
816 /* save the preset with the given name */
818 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
823 GObjectClass *gclass;
824 gboolean is_child_proxy;
826 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
828 /* get the presets from the type */
829 if (!(presets = preset_get_keyfile (preset)))
832 /* take copies of current gobject properties from preset */
833 if (!(props = gst_preset_get_property_names (preset)))
836 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
837 is_child_proxy = GST_IS_CHILD_PROXY (preset);
839 /* loop over the object properties and store the property value in the
841 for (i = 0; props[i]; i++) {
842 GValue gvalue = { 0, };
844 GParamSpec *property = NULL;
846 if (is_child_proxy) {
847 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
850 property = g_object_class_find_property (gclass, props[i]);
853 /* the element said it supported the property but then it does not have
854 * that property. This should not happen. */
855 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
859 if (property->flags & G_PARAM_DEPRECATED) {
860 GST_INFO_OBJECT (preset, "Not saving property %s as it is deprecated",
865 if (property->flags & G_PARAM_DEPRECATED) {
866 GST_INFO_OBJECT (preset, "Not saving property %s as it is deprecated",
871 g_value_init (&gvalue, property->value_type);
872 if (is_child_proxy) {
873 gst_child_proxy_get_property ((GstChildProxy *) preset, props[i],
876 g_object_get_property ((GObject *) preset, props[i], &gvalue);
879 if ((str = gst_value_serialize (&gvalue))) {
880 g_key_file_set_string (presets, name, props[i], (gpointer) str);
883 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
886 g_value_unset (&gvalue);
888 GST_INFO_OBJECT (preset, " saved");
891 /* save updated version */
892 return gst_preset_default_save_presets_file (preset);
897 GST_WARNING_OBJECT (preset, "no presets");
902 GST_INFO_OBJECT (preset, "no properties");
907 /* copies all keys and comments from one group to another, deleting the old
910 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
911 const gchar * new_name)
918 /* get the presets from the type */
919 if (!(presets = preset_get_keyfile (preset)))
922 if (!g_key_file_has_group (presets, old_name))
925 /* copy group comment if there is any */
926 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
927 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
931 /* get all keys from the old group and copy them in the new group */
932 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
933 for (i = 0; i < num_keys; i++) {
934 /* copy key comment if there is any */
935 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
936 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
940 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
941 g_key_file_set_value (presets, new_name, keys[i], str);
946 /* remove old group */
947 g_key_file_remove_group (presets, old_name, NULL);
949 /* save updated version */
950 return gst_preset_default_save_presets_file (preset);
955 GST_WARNING_OBJECT (preset, "no presets");
960 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
965 /* delete a group from the keyfile */
967 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
971 /* get the presets from the type */
972 if (!(presets = preset_get_keyfile (preset)))
976 if (!g_key_file_has_group (presets, name))
979 /* remove the group */
980 g_key_file_remove_group (presets, name, NULL);
982 /* save updated version */
983 return gst_preset_default_save_presets_file (preset);
988 GST_WARNING_OBJECT (preset, "no presets");
993 GST_WARNING_OBJECT (preset, "no preset named %s", name);
999 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
1000 const gchar * tag, const gchar * value)
1005 /* get the presets from the type */
1006 if (!(presets = preset_get_keyfile (preset)))
1009 key = g_strdup_printf ("_meta/%s", tag);
1010 if (value && *value) {
1011 g_key_file_set_value (presets, name, key, value);
1013 g_key_file_remove_key (presets, name, key, NULL);
1017 /* save updated keyfile */
1018 return gst_preset_default_save_presets_file (preset);
1023 GST_WARNING_OBJECT (preset, "no presets");
1028 /* the caller must free @value after usage */
1030 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
1031 const gchar * tag, gchar ** value)
1036 /* get the presets from the type */
1037 if (!(presets = preset_get_keyfile (preset)))
1040 key = g_strdup_printf ("_meta/%s", tag);
1041 *value = g_key_file_get_value (presets, name, key, NULL);
1049 GST_WARNING_OBJECT (preset, "no presets");
1058 * gst_preset_get_preset_names:
1059 * @preset: a #GObject that implements #GstPreset
1061 * Get a copy of preset names as a %NULL terminated string array.
1063 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
1064 * list with names, use g_strfreev() after usage.
1067 gst_preset_get_preset_names (GstPreset * preset)
1069 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
1071 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
1075 * gst_preset_get_property_names:
1076 * @preset: a #GObject that implements #GstPreset
1078 * Get a the names of the GObject properties that can be used for presets.
1080 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
1081 * array of property names which should be freed with g_strfreev() after use.
1084 gst_preset_get_property_names (GstPreset * preset)
1086 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
1088 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
1092 * gst_preset_load_preset:
1093 * @preset: a #GObject that implements #GstPreset
1094 * @name: preset name to load
1096 * Load the given preset.
1098 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1101 gst_preset_load_preset (GstPreset * preset, const gchar * name)
1103 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1104 g_return_val_if_fail (name, FALSE);
1106 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
1110 * gst_preset_save_preset:
1111 * @preset: a #GObject that implements #GstPreset
1112 * @name: preset name to save
1114 * Save the current object settings as a preset under the given name. If there
1115 * is already a preset by this @name it will be overwritten.
1117 * Returns: %TRUE for success, %FALSE
1120 gst_preset_save_preset (GstPreset * preset, const gchar * name)
1122 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1123 g_return_val_if_fail (name, FALSE);
1125 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
1129 * gst_preset_rename_preset:
1130 * @preset: a #GObject that implements #GstPreset
1131 * @old_name: current preset name
1132 * @new_name: new preset name
1134 * Renames a preset. If there is already a preset by the @new_name it will be
1137 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1140 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1141 const gchar * new_name)
1143 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1144 g_return_val_if_fail (old_name, FALSE);
1145 g_return_val_if_fail (new_name, FALSE);
1147 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1152 * gst_preset_delete_preset:
1153 * @preset: a #GObject that implements #GstPreset
1154 * @name: preset name to remove
1156 * Delete the given preset.
1158 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1161 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1163 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1164 g_return_val_if_fail (name, FALSE);
1166 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1170 * gst_preset_set_meta:
1171 * @preset: a #GObject that implements #GstPreset
1172 * @name: preset name
1173 * @tag: meta data item name
1174 * @value: (allow-none): new value
1176 * Sets a new @value for an existing meta data item or adds a new item. Meta
1177 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1178 * @value will unset an existing value.
1180 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1183 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1184 const gchar * value)
1186 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1187 g_return_val_if_fail (name, FALSE);
1188 g_return_val_if_fail (tag, FALSE);
1190 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1194 * gst_preset_get_meta:
1195 * @preset: a #GObject that implements #GstPreset
1196 * @name: preset name
1197 * @tag: meta data item name
1198 * @value: (out callee-allocates): value
1200 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1201 * something like e.g. "comment". Returned values need to be released when done.
1203 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1204 * or no value for the given @tag
1207 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1210 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1211 g_return_val_if_fail (name, FALSE);
1212 g_return_val_if_fail (tag, FALSE);
1213 g_return_val_if_fail (value, FALSE);
1215 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1219 * gst_preset_set_app_dir:
1220 * @app_dir: the application specific preset dir
1222 * Sets an extra directory as an absolute path that should be considered when
1223 * looking for presets. Any presets in the application dir will shadow the
1226 * Returns: %TRUE for success, %FALSE if the dir already has been set
1229 gst_preset_set_app_dir (const gchar * app_dir)
1231 g_return_val_if_fail (app_dir, FALSE);
1233 if (!preset_app_dir) {
1234 preset_app_dir = g_strdup (app_dir);
1241 * gst_preset_get_app_dir:
1243 * Gets the directory for application specific presets if set by the
1246 * Returns: (nullable): the directory or %NULL, don't free or modify
1250 gst_preset_get_app_dir (void)
1252 return preset_app_dir;
1256 * gst_preset_is_editable:
1257 * @preset: a #GObject that implements #GstPreset
1259 * Check if one can add new presets, change existing ones and remove presets.
1261 * Returns: %TRUE if presets are editable or %FALSE if they are static
1266 gst_preset_is_editable (GstPreset * preset)
1268 GstPresetInterface *iface = GST_PRESET_GET_INTERFACE (preset);
1270 return iface->save_preset && iface->delete_preset;
1273 /* class internals */
1276 gst_preset_class_init (GstPresetInterface * iface)
1278 iface->get_preset_names = gst_preset_default_get_preset_names;
1279 iface->get_property_names = gst_preset_default_get_property_names;
1281 iface->load_preset = gst_preset_default_load_preset;
1282 iface->save_preset = gst_preset_default_save_preset;
1283 iface->rename_preset = gst_preset_default_rename_preset;
1284 iface->delete_preset = gst_preset_default_delete_preset;
1286 iface->set_meta = gst_preset_default_set_meta;
1287 iface->get_meta = gst_preset_default_get_meta;
1291 gst_preset_base_init (gpointer g_class)
1293 static gboolean initialized = FALSE;
1296 /* init default implementation */
1297 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1298 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1300 /* create quarks for use with g_type_{g,s}et_qdata() */
1301 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1302 preset_user_path_quark =
1303 g_quark_from_static_string ("GstPreset::user_path");
1304 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1305 preset_system_path_quark =
1306 g_quark_from_static_string ("GstPreset::system_path");
1309 /* create interface properties, each element would need to override this
1310 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1311 * and in _set_property() do
1312 * case PROP_PRESET_NAME: {
1313 * gchar *name = g_value_get_string (value);
1315 * gst_preset_load_preset(preset, name);
1318 g_object_interface_install_property (g_class,
1319 g_param_spec_string ("preset-name",
1320 "preset-name property",
1321 "load given preset", NULL, G_PARAM_WRITABLE));
1329 gst_preset_get_type (void)
1331 static volatile gsize type = 0;
1333 if (g_once_init_enter (&type)) {
1335 const GTypeInfo info = {
1336 sizeof (GstPresetInterface),
1337 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1338 NULL, /* base_finalize */
1339 (GClassInitFunc) gst_preset_class_init, /* class_init */
1340 NULL, /* class_finalize */
1341 NULL, /* class_data */
1343 0, /* n_preallocs */
1344 NULL /* instance_init */
1346 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1347 g_once_init_leave (&type, _type);