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.
23 * @short_description: helper interface for element presets
25 * This interface offers methods to query and manipulate parameter preset sets.
26 * A preset is a bunch of property settings, together with meta data and a name.
27 * The name of a preset serves as key for subsequent method calls to manipulate
29 * All instances of one type will share the list of presets. The list is created
30 * on demand, if presets are not used, the list is not created.
32 * The interface comes with a default implementation that serves most plugins.
33 * Wrapper plugins will override most methods to implement support for the
34 * native preset format of those wrapped plugins.
35 * One method that is useful to be overridden is gst_preset_get_property_names().
36 * With that one can control which properties are saved and in which order.
37 * When implementing support for read-only presets, one should set the vmethods
38 * for gst_preset_save_preset() and gst_preset_delete_preset() to %NULL.
39 * Applications can use gst_preset_is_editable() to check for that.
41 * The default implementation supports presets located in a system directory,
42 * application specific directory and in the users home directory. When getting
43 * a list of presets individual presets are read and overlaid in 1) system,
44 * 2) application and 3) user order. Whenever an earlier entry is newer, the
45 * later entries will be updated.
50 * - we need to avoid two instances writing the preset file
51 * -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
53 * -> better save the new file to a tempfile and then rename?
54 * - we like to know when any other instance makes changes to the keyfile
55 * - then ui can be updated
56 * - and we make sure that we don't lose edits
57 * -> its the same problem actually, once for inside a process, once system-
59 * - can we use a lock inside a names shared memory segment?
61 * - should there be a 'preset-list' property to get the preset list
62 * (and to connect a notify:: to to listen for changes)
63 * we could use gnome_vfs_monitor_add() to monitor the user preset_file.
65 * - should there be a 'preset-name' property so that we can set a preset via
66 * gst-launch, or should we handle this with special syntax in gst-launch:
67 * gst-launch element preset:<preset-name> property=value ...
68 * - this would allow to have preset-bundles too (a preset on bins that
69 * specifies presets for children
72 #include "gst_private.h"
74 #include "gstpreset.h"
75 #include "gstchildproxy.h"
82 #include <glib/gstdio.h>
85 #define WIN32_LEAN_AND_MEAN
88 extern HMODULE _priv_gst_dll_handle;
91 #define GST_CAT_DEFAULT preset_debug
92 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
94 /* defines for keyfile usage, this group contains the element type name and
95 * version these presets belong to. */
96 #define PRESET_HEADER "_presets_"
98 /* keys of the preset header section */
99 #define PRESET_HEADER_ELEMENT_NAME "element-name"
100 #define PRESET_HEADER_VERSION "version"
102 static GQuark preset_user_path_quark = 0;
103 static GQuark preset_app_path_quark = 0;
104 static GQuark preset_system_path_quark = 0;
105 static GQuark preset_quark = 0;
107 /* the application can set a custom path that is checked in addition to standard
108 * system and user dirs. This helps to develop new presets first local to the
111 static gchar *preset_app_dir = NULL;
113 /* default iface implementation */
115 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
119 * @preset: a #GObject that implements #GstPreset
120 * @preset_user_path: (out) (allow-none): location for path or %NULL
121 * @preset_app_path: (out) (allow-none): location for path or %NULL
122 * @preset_system_path: (out) (allow-none): location for path or %NULL
124 * Fetch the preset_path for user local, application specific and system wide
125 * settings. Don't free after use.
127 * Returns: %FALSE if no paths could be found.
130 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
131 const gchar ** preset_app_path, const gchar ** preset_system_path)
133 GType type = G_TYPE_FROM_INSTANCE (preset);
135 const gchar *element_name;
137 /* we use the element name when we must construct the paths */
138 element_name = G_OBJECT_TYPE_NAME (preset);
139 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
141 if (preset_user_path) {
142 /* preset user path requested, see if we have it cached in the qdata */
143 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
146 /* user presets go in user's XDG data directory. */
147 preset_dir = g_build_filename (g_get_user_data_dir (),
148 "gstreamer-" GST_API_VERSION, "presets", NULL);
149 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
151 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
153 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
155 g_mkdir_with_parents (preset_dir, 0755);
158 /* cache the preset path to the type */
159 g_type_set_qdata (type, preset_user_path_quark, preset_path);
161 *preset_user_path = preset_path;
164 if (preset_app_path) {
165 if (preset_app_dir) {
166 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
167 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
168 preset_app_dir, element_name);
169 GST_INFO_OBJECT (preset, "application_preset_path: '%s'", preset_path);
171 /* cache the preset path to the type */
172 g_type_set_qdata (type, preset_app_path_quark, preset_path);
174 *preset_app_path = preset_path;
176 *preset_app_path = NULL;
180 if (preset_system_path) {
181 /* preset system path requested, see if we have it cached in the qdata */
182 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
185 /* system presets in '$GST_DATADIR/gstreamer-1.0/presets/GstAudioPanorama.prs' */
188 g_win32_get_package_installation_directory_of_module
189 (_priv_gst_dll_handle);
191 g_build_filename (basedir, "share", "gstreamer-" GST_API_VERSION,
195 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_API_VERSION,
198 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
199 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
200 preset_dir, element_name);
201 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
203 g_mkdir_with_parents (preset_dir, 0755);
206 /* cache the preset path to the type */
207 g_type_set_qdata (type, preset_system_path_quark, preset_path);
209 *preset_system_path = preset_path;
215 preset_skip_property (GParamSpec * property)
217 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
218 (property->flags & G_PARAM_CONSTRUCT_ONLY))
220 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
225 preset_parse_version (const gchar * str_version)
227 guint major, minor, micro, nano;
230 major = minor = micro = nano = 0;
232 /* parse version (e.g. 0.10.15.1) to guint64 */
233 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
234 /* make sure we have at least "major.minor" */
238 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
239 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
242 return G_GUINT64_CONSTANT (0);
246 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
247 guint64 * preset_version)
250 GError *error = NULL;
252 const gchar *element_name;
255 in = g_key_file_new ();
257 res = g_key_file_load_from_file (in, preset_path,
258 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
259 if (!res || error != NULL)
262 /* element type name and preset name must match or we are dealing with a wrong
264 element_name = G_OBJECT_TYPE_NAME (preset);
266 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
269 if (!name || strcmp (name, element_name))
274 /* get the version now so that the caller can check it */
275 if (preset_version) {
277 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
278 *preset_version = preset_parse_version (str);
287 GST_WARNING_OBJECT (preset, "Unable to read preset file %s: %s",
288 preset_path, error->message);
289 g_error_free (error);
290 g_key_file_free (in);
295 GST_WARNING_OBJECT (preset,
296 "Wrong element name in preset file %s. Expected %s, got %s",
297 preset_path, element_name, GST_STR_NULL (name));
299 g_key_file_free (in);
305 preset_merge (GKeyFile * system, GKeyFile * user)
308 gchar **groups, **keys;
309 gsize i, j, num_groups, num_keys;
311 /* copy file comment if there is any */
312 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
313 g_key_file_set_comment (system, NULL, NULL, str, NULL);
317 /* get groups in user and copy into system */
318 groups = g_key_file_get_groups (user, &num_groups);
319 for (i = 0; i < num_groups; i++) {
320 /* copy group comment if there is any */
321 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
322 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
326 /* ignore private groups */
327 if (groups[i][0] == '_')
330 /* if group already exists in system, remove and re-add keys from user */
331 if (g_key_file_has_group (system, groups[i])) {
332 g_key_file_remove_group (system, groups[i], NULL);
335 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
336 for (j = 0; j < num_keys; j++) {
337 /* copy key comment if there is any */
338 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
339 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
342 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
343 g_key_file_set_value (system, groups[i], keys[j], str);
351 /* reads the user and system presets files and merges them together. This
352 * function caches the GKeyFile on the element type. If there is no existing
353 * preset file, a new in-memory GKeyFile will be created. */
355 preset_get_keyfile (GstPreset * preset)
358 GType type = G_TYPE_FROM_INSTANCE (preset);
360 /* first see if the have a cached version for the type */
361 if (!(presets = g_type_get_qdata (type, preset_quark))) {
362 const gchar *preset_user_path, *preset_app_path, *preset_system_path;
363 guint64 version_system = G_GUINT64_CONSTANT (0);
364 guint64 version_app = G_GUINT64_CONSTANT (0);
365 guint64 version_user = G_GUINT64_CONSTANT (0);
366 guint64 version = G_GUINT64_CONSTANT (0);
367 gboolean merged = FALSE;
368 GKeyFile *in_user, *in_app = NULL, *in_system;
370 preset_get_paths (preset, &preset_user_path, &preset_app_path,
371 &preset_system_path);
373 /* try to load the user, app and system presets, we do this to get the
374 * versions of all files. */
375 in_user = preset_open_and_parse_header (preset, preset_user_path,
377 if (preset_app_path) {
378 in_app = preset_open_and_parse_header (preset, preset_app_path,
381 in_system = preset_open_and_parse_header (preset, preset_system_path,
384 /* compare version to check for merge */
387 version = version_system;
390 /* if system version is higher, merge */
391 if (version > version_app) {
392 preset_merge (presets, in_app);
393 g_key_file_free (in_app);
396 g_key_file_free (presets);
398 version = version_app;
402 /* if system or app version is higher, merge */
403 if (version > version_user) {
404 preset_merge (presets, in_user);
405 g_key_file_free (in_user);
409 g_key_file_free (presets);
415 /* we did not load a user, app or system presets file, create a new one */
416 presets = g_key_file_new ();
417 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
418 G_OBJECT_TYPE_NAME (preset));
421 /* attach the preset to the type */
422 g_type_set_qdata (type, preset_quark, (gpointer) presets);
425 gst_preset_default_save_presets_file (preset);
432 compare_strings (gchar ** a, gchar ** b, gpointer user_data)
434 return g_strcmp0 (*a, *b);
437 /* get a list of all supported preset names for an element */
439 gst_preset_default_get_preset_names (GstPreset * preset)
445 /* get the presets from the type */
446 if (!(presets = preset_get_keyfile (preset)))
449 /* get the groups, which are also the preset names */
450 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
453 /* remove all private group names starting with '_' from the array */
454 for (i = 0; i < num_groups; i++) {
455 if (groups[i][0] == '_') {
456 /* free private group */
458 /* move last element of list down */
460 /* move last element into removed element */
461 groups[i] = groups[num_groups];
462 groups[num_groups] = NULL;
466 GST_INFO_OBJECT (preset, "Empty preset file");
471 /* sort the array now */
472 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
473 (GCompareDataFunc) compare_strings, NULL);
480 GST_WARNING_OBJECT (preset, "Could not load presets");
485 GST_WARNING_OBJECT (preset, "Could not find preset groups");
490 /* get a list of all property names that are used for presets */
492 gst_preset_default_get_property_names (GstPreset * preset)
495 guint i, j = 0, n_props;
496 GObjectClass *gclass;
497 gboolean is_child_proxy;
498 gchar **result = NULL;
500 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
501 is_child_proxy = GST_IS_CHILD_PROXY (preset);
503 /* get a list of object properties */
504 props = g_object_class_list_properties (gclass, &n_props);
506 /* allocate array big enough to hold the worst case, including a terminating
508 result = g_new (gchar *, n_props + 1);
510 /* now filter out the properties that we can use for presets */
511 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
512 for (i = 0; i < n_props; i++) {
513 if (preset_skip_property (props[i]))
515 GST_DEBUG_OBJECT (preset, " using: %s", props[i]->name);
516 result[j++] = g_strdup (props[i]->name);
521 if (is_child_proxy) {
525 n_children = gst_child_proxy_get_children_count ((GstChildProxy *) preset);
526 for (c = 0; c < n_children; c++) {
527 child = gst_child_proxy_get_child_by_index ((GstChildProxy *) preset, c);
528 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (child));
530 props = g_object_class_list_properties (gclass, &n_props);
532 /* resize property name array */
533 result = g_renew (gchar *, result, j + n_props + 1);
535 /* now filter out the properties that we can use for presets */
536 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
537 for (i = 0; i < n_props; i++) {
538 if (preset_skip_property (props[i]))
540 GST_DEBUG_OBJECT (preset, " using: %s::%s",
541 GST_OBJECT_NAME (child), props[i]->name);
542 result[j++] = g_strdup_printf ("%s::%s", GST_OBJECT_NAME (child),
548 g_object_unref (child);
552 GST_INFO_OBJECT (preset, "object has no properties");
559 /* load the presets of @name for the instance @preset. Returns %FALSE if something
562 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
567 GObjectClass *gclass;
568 gboolean is_child_proxy;
570 /* get the presets from the type */
571 if (!(presets = preset_get_keyfile (preset)))
574 /* get the preset name */
575 if (!g_key_file_has_group (presets, name))
578 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
580 /* get the properties that we can configure in this element */
581 if (!(props = gst_preset_get_property_names (preset)))
584 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
585 is_child_proxy = GST_IS_CHILD_PROXY (preset);
587 /* for each of the property names, find the preset parameter and try to
588 * configure the property with its value */
589 for (i = 0; props[i]; i++) {
591 GValue gvalue = { 0, };
592 GParamSpec *property = NULL;
594 /* check if we have a settings for this element property */
595 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
596 /* the element has a property but the parameter is not in the keyfile */
597 GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
601 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
604 if (is_child_proxy) {
605 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
608 property = g_object_class_find_property (gclass, props[i]);
611 /* the parameter was in the keyfile, the element said it supported it but
612 * then the property was not found in the element. This should not happen. */
613 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
618 /* try to deserialize the property value from the keyfile and set it as
619 * the object property */
620 g_value_init (&gvalue, property->value_type);
621 if (gst_value_deserialize (&gvalue, str)) {
622 if (is_child_proxy) {
623 gst_child_proxy_set_property ((GstChildProxy *) preset, props[i],
626 g_object_set_property ((GObject *) preset, props[i], &gvalue);
629 GST_WARNING_OBJECT (preset,
630 "deserialization of value '%s' for property '%s' failed", str,
633 g_value_unset (&gvalue);
643 GST_WARNING_OBJECT (preset, "no presets");
648 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
653 GST_INFO_OBJECT (preset, "no properties");
658 /* save the presets file. A copy of the existing presets file is stored in a
661 gst_preset_default_save_presets_file (GstPreset * preset)
664 const gchar *preset_path;
665 GError *error = NULL;
666 gchar *bak_file_name;
667 gboolean backup = TRUE;
671 preset_get_paths (preset, &preset_path, NULL, NULL);
673 /* get the presets from the type */
674 if (!(presets = preset_get_keyfile (preset)))
677 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
679 /* create backup if possible */
680 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
681 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
682 if (g_unlink (bak_file_name)) {
684 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
689 if (g_rename (preset_path, bak_file_name)) {
690 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
694 g_free (bak_file_name);
696 /* update gstreamer version */
697 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
700 /* get new contents, wee need this to save it */
701 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
705 if (!g_file_set_contents (preset_path, data, data_size, &error))
715 GST_WARNING_OBJECT (preset,
716 "no presets, trying to unlink possibly existing preset file: '%s'",
718 g_unlink (preset_path);
723 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
725 g_error_free (error);
731 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
732 preset_path, error->message);
733 g_error_free (error);
739 /* save the preset with the given name */
741 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
746 GObjectClass *gclass;
747 gboolean is_child_proxy;
749 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
751 /* get the presets from the type */
752 if (!(presets = preset_get_keyfile (preset)))
755 /* take copies of current gobject properties from preset */
756 if (!(props = gst_preset_get_property_names (preset)))
759 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
760 is_child_proxy = GST_IS_CHILD_PROXY (preset);
762 /* loop over the object properties and store the property value in the
764 for (i = 0; props[i]; i++) {
765 GValue gvalue = { 0, };
767 GParamSpec *property = NULL;
769 if (is_child_proxy) {
770 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
773 property = g_object_class_find_property (gclass, props[i]);
776 /* the element said it supported the property but then it does not have
777 * that property. This should not happen. */
778 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
782 g_value_init (&gvalue, property->value_type);
783 if (is_child_proxy) {
784 gst_child_proxy_get_property ((GstChildProxy *) preset, props[i],
787 g_object_get_property ((GObject *) preset, props[i], &gvalue);
790 if ((str = gst_value_serialize (&gvalue))) {
791 g_key_file_set_string (presets, name, props[i], (gpointer) str);
794 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
797 g_value_unset (&gvalue);
799 GST_INFO_OBJECT (preset, " saved");
802 /* save updated version */
803 return gst_preset_default_save_presets_file (preset);
808 GST_WARNING_OBJECT (preset, "no presets");
813 GST_INFO_OBJECT (preset, "no properties");
818 /* copies all keys and comments from one group to another, deleting the old
821 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
822 const gchar * new_name)
829 /* get the presets from the type */
830 if (!(presets = preset_get_keyfile (preset)))
833 if (!g_key_file_has_group (presets, old_name))
836 /* copy group comment if there is any */
837 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
838 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
842 /* get all keys from the old group and copy them in the new group */
843 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
844 for (i = 0; i < num_keys; i++) {
845 /* copy key comment if there is any */
846 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
847 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
851 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
852 g_key_file_set_value (presets, new_name, keys[i], str);
857 /* remove old group */
858 g_key_file_remove_group (presets, old_name, NULL);
860 /* save updated version */
861 return gst_preset_default_save_presets_file (preset);
866 GST_WARNING_OBJECT (preset, "no presets");
871 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
876 /* delete a group from the keyfile */
878 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
882 /* get the presets from the type */
883 if (!(presets = preset_get_keyfile (preset)))
887 if (!g_key_file_has_group (presets, name))
890 /* remove the group */
891 g_key_file_remove_group (presets, name, NULL);
893 /* save updated version */
894 return gst_preset_default_save_presets_file (preset);
899 GST_WARNING_OBJECT (preset, "no presets");
904 GST_WARNING_OBJECT (preset, "no preset named %s", name);
910 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
911 const gchar * tag, const gchar * value)
916 /* get the presets from the type */
917 if (!(presets = preset_get_keyfile (preset)))
920 key = g_strdup_printf ("_meta/%s", tag);
921 if (value && *value) {
922 g_key_file_set_value (presets, name, key, value);
924 g_key_file_remove_key (presets, name, key, NULL);
928 /* save updated keyfile */
929 return gst_preset_default_save_presets_file (preset);
934 GST_WARNING_OBJECT (preset, "no presets");
939 /* the caller must free @value after usage */
941 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
942 const gchar * tag, gchar ** value)
947 /* get the presets from the type */
948 if (!(presets = preset_get_keyfile (preset)))
951 key = g_strdup_printf ("_meta/%s", tag);
952 *value = g_key_file_get_value (presets, name, key, NULL);
960 GST_WARNING_OBJECT (preset, "no presets");
969 * gst_preset_get_preset_names:
970 * @preset: a #GObject that implements #GstPreset
972 * Get a copy of preset names as a %NULL terminated string array.
974 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
975 * list with names, use g_strfreev() after usage.
978 gst_preset_get_preset_names (GstPreset * preset)
980 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
982 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
986 * gst_preset_get_property_names:
987 * @preset: a #GObject that implements #GstPreset
989 * Get a the names of the GObject properties that can be used for presets.
991 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
992 * array of property names which should be freed with g_strfreev() after use.
995 gst_preset_get_property_names (GstPreset * preset)
997 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
999 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
1003 * gst_preset_load_preset:
1004 * @preset: a #GObject that implements #GstPreset
1005 * @name: preset name to load
1007 * Load the given preset.
1009 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1012 gst_preset_load_preset (GstPreset * preset, const gchar * name)
1014 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1015 g_return_val_if_fail (name, FALSE);
1017 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
1021 * gst_preset_save_preset:
1022 * @preset: a #GObject that implements #GstPreset
1023 * @name: preset name to save
1025 * Save the current object settings as a preset under the given name. If there
1026 * is already a preset by this @name it will be overwritten.
1028 * Returns: %TRUE for success, %FALSE
1031 gst_preset_save_preset (GstPreset * preset, const gchar * name)
1033 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1034 g_return_val_if_fail (name, FALSE);
1036 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
1040 * gst_preset_rename_preset:
1041 * @preset: a #GObject that implements #GstPreset
1042 * @old_name: current preset name
1043 * @new_name: new preset name
1045 * Renames a preset. If there is already a preset by the @new_name it will be
1048 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1051 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1052 const gchar * new_name)
1054 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1055 g_return_val_if_fail (old_name, FALSE);
1056 g_return_val_if_fail (new_name, FALSE);
1058 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1063 * gst_preset_delete_preset:
1064 * @preset: a #GObject that implements #GstPreset
1065 * @name: preset name to remove
1067 * Delete the given preset.
1069 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1072 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1074 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1075 g_return_val_if_fail (name, FALSE);
1077 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1081 * gst_preset_set_meta:
1082 * @preset: a #GObject that implements #GstPreset
1083 * @name: preset name
1084 * @tag: meta data item name
1085 * @value: (allow-none): new value
1087 * Sets a new @value for an existing meta data item or adds a new item. Meta
1088 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1089 * @value will unset an existing value.
1091 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1094 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1095 const gchar * value)
1097 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1098 g_return_val_if_fail (name, FALSE);
1099 g_return_val_if_fail (tag, FALSE);
1101 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1105 * gst_preset_get_meta:
1106 * @preset: a #GObject that implements #GstPreset
1107 * @name: preset name
1108 * @tag: meta data item name
1109 * @value: (out callee-allocates): value
1111 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1112 * something like e.g. "comment". Returned values need to be released when done.
1114 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1115 * or no value for the given @tag
1118 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1121 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1122 g_return_val_if_fail (name, FALSE);
1123 g_return_val_if_fail (tag, FALSE);
1124 g_return_val_if_fail (value, FALSE);
1126 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1130 * gst_preset_set_app_dir:
1131 * @app_dir: the application specific preset dir
1133 * Sets an extra directory as an absolute path that should be considered when
1134 * looking for presets. Any presets in the application dir will shadow the
1137 * Returns: %TRUE for success, %FALSE if the dir already has been set
1140 gst_preset_set_app_dir (const gchar * app_dir)
1142 g_return_val_if_fail (app_dir, FALSE);
1144 if (!preset_app_dir) {
1145 preset_app_dir = g_strdup (app_dir);
1152 * gst_preset_get_app_dir:
1154 * Gets the directory for application specific presets if set by the
1157 * Returns: (nullable): the directory or %NULL, don't free or modify
1161 gst_preset_get_app_dir (void)
1163 return preset_app_dir;
1167 * gst_preset_is_editable:
1168 * @preset: a #GObject that implements #GstPreset
1170 * Check if one can add new presets, change existing ones and remove presets.
1172 * Returns: %TRUE if presets are editable or %FALSE if they are static
1177 gst_preset_is_editable (GstPreset * preset)
1179 GstPresetInterface *iface = GST_PRESET_GET_INTERFACE (preset);
1181 return iface->save_preset && iface->delete_preset;
1184 /* class internals */
1187 gst_preset_class_init (GstPresetInterface * iface)
1189 iface->get_preset_names = gst_preset_default_get_preset_names;
1190 iface->get_property_names = gst_preset_default_get_property_names;
1192 iface->load_preset = gst_preset_default_load_preset;
1193 iface->save_preset = gst_preset_default_save_preset;
1194 iface->rename_preset = gst_preset_default_rename_preset;
1195 iface->delete_preset = gst_preset_default_delete_preset;
1197 iface->set_meta = gst_preset_default_set_meta;
1198 iface->get_meta = gst_preset_default_get_meta;
1202 gst_preset_base_init (gpointer g_class)
1204 static gboolean initialized = FALSE;
1207 /* init default implementation */
1208 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1209 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1211 /* create quarks for use with g_type_{g,s}et_qdata() */
1212 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1213 preset_user_path_quark =
1214 g_quark_from_static_string ("GstPreset::user_path");
1215 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1216 preset_system_path_quark =
1217 g_quark_from_static_string ("GstPreset::system_path");
1220 /* create interface properties, each element would need to override this
1221 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1222 * and in _set_property() do
1223 * case PROP_PRESET_NAME: {
1224 * gchar *name = g_value_get_string (value);
1226 * gst_preset_load_preset(preset, name);
1229 g_object_interface_install_property (g_class,
1230 g_param_spec_string ("preset-name",
1231 "preset-name property",
1232 "load given preset", NULL, G_PARAM_WRITABLE));
1240 gst_preset_get_type (void)
1242 static volatile gsize type = 0;
1244 if (g_once_init_enter (&type)) {
1246 const GTypeInfo info = {
1247 sizeof (GstPresetInterface),
1248 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1249 NULL, /* base_finalize */
1250 (GClassInitFunc) gst_preset_class_init, /* class_init */
1251 NULL, /* class_finalize */
1252 NULL, /* class_data */
1254 0, /* n_preallocs */
1255 NULL /* instance_init */
1257 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1258 g_once_init_leave (&type, _type);