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. Since 1.8 you can also provide extra paths
46 * where to find presets through the GST_PRESET_PATH environment variable.
47 * Presets found in those paths will be concidered as "app presets".
51 * - we need to avoid two instances writing the preset file
52 * -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
54 * -> better save the new file to a tempfile and then rename?
55 * - we like to know when any other instance makes changes to the keyfile
56 * - then ui can be updated
57 * - and we make sure that we don't lose edits
58 * -> its the same problem actually, once for inside a process, once system-
60 * - can we use a lock inside a names shared memory segment?
62 * - should there be a 'preset-list' property to get the preset list
63 * (and to connect a notify:: to to listen for changes)
64 * we could use gnome_vfs_monitor_add() to monitor the user preset_file.
66 * - should there be a 'preset-name' property so that we can set a preset via
67 * gst-launch, or should we handle this with special syntax in gst-launch:
68 * gst-launch element preset:<preset-name> property=value ...
69 * - this would allow to have preset-bundles too (a preset on bins that
70 * specifies presets for children
73 #include "gst_private.h"
75 #include "gstpreset.h"
76 #include "gstchildproxy.h"
83 #include <glib/gstdio.h>
86 #define WIN32_LEAN_AND_MEAN
89 extern HMODULE _priv_gst_dll_handle;
92 #define GST_CAT_DEFAULT preset_debug
93 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
95 /* defines for keyfile usage, this group contains the element type name and
96 * version these presets belong to. */
97 #define PRESET_HEADER "_presets_"
99 /* keys of the preset header section */
100 #define PRESET_HEADER_ELEMENT_NAME "element-name"
101 #define PRESET_HEADER_VERSION "version"
103 static GQuark preset_user_path_quark = 0;
104 static GQuark preset_app_path_quark = 0;
105 static GQuark preset_system_path_quark = 0;
106 static GQuark preset_quark = 0;
108 /* the application can set a custom path that is checked in addition to standard
109 * system and user dirs. This helps to develop new presets first local to the
112 static gchar *preset_app_dir = NULL;
114 /* default iface implementation */
116 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
120 * @preset: a #GObject that implements #GstPreset
121 * @preset_user_path: (out) (allow-none): location for path or %NULL
122 * @preset_app_path: (out) (allow-none): location for path or %NULL
123 * @preset_system_path: (out) (allow-none): location for path or %NULL
125 * Fetch the preset_path for user local, application specific and system wide
126 * settings. Don't free after use.
128 * Returns: %FALSE if no paths could be found.
131 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
132 const gchar ** preset_app_path, const gchar ** preset_system_path)
134 GType type = G_TYPE_FROM_INSTANCE (preset);
136 const gchar *element_name;
138 /* we use the element name when we must construct the paths */
139 element_name = G_OBJECT_TYPE_NAME (preset);
140 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
142 if (preset_user_path) {
143 /* preset user path requested, see if we have it cached in the qdata */
144 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
147 /* user presets go in user's XDG data directory. */
148 preset_dir = g_build_filename (g_get_user_data_dir (),
149 "gstreamer-" GST_API_VERSION, "presets", NULL);
150 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
152 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
154 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
156 g_mkdir_with_parents (preset_dir, 0755);
159 /* cache the preset path to the type */
160 g_type_set_qdata (type, preset_user_path_quark, preset_path);
162 *preset_user_path = preset_path;
165 if (preset_app_path) {
166 if (preset_app_dir) {
167 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
168 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
169 preset_app_dir, element_name);
170 GST_INFO_OBJECT (preset, "application_preset_path: '%s'", preset_path);
172 /* cache the preset path to the type */
173 g_type_set_qdata (type, preset_app_path_quark, preset_path);
175 *preset_app_path = preset_path;
177 *preset_app_path = NULL;
181 if (preset_system_path) {
182 /* preset system path requested, see if we have it cached in the qdata */
183 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
186 /* system presets in '$GST_DATADIR/gstreamer-1.0/presets/GstAudioPanorama.prs' */
189 g_win32_get_package_installation_directory_of_module
190 (_priv_gst_dll_handle);
192 g_build_filename (basedir, "share", "gstreamer-" GST_API_VERSION,
196 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_API_VERSION,
199 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
200 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
201 preset_dir, element_name);
202 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
204 g_mkdir_with_parents (preset_dir, 0755);
207 /* cache the preset path to the type */
208 g_type_set_qdata (type, preset_system_path_quark, preset_path);
210 *preset_system_path = preset_path;
216 preset_skip_property (GParamSpec * property)
218 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
219 (property->flags & G_PARAM_CONSTRUCT_ONLY))
221 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
226 preset_parse_version (const gchar * str_version)
228 guint major, minor, micro, nano;
231 major = minor = micro = nano = 0;
233 /* parse version (e.g. 0.10.15.1) to guint64 */
234 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
235 /* make sure we have at least "major.minor" */
239 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
240 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
243 return G_GUINT64_CONSTANT (0);
247 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
248 guint64 * preset_version)
251 GError *error = NULL;
253 const gchar *element_name;
256 in = g_key_file_new ();
258 res = g_key_file_load_from_file (in, preset_path,
259 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
260 if (!res || error != NULL)
263 /* element type name and preset name must match or we are dealing with a wrong
265 element_name = G_OBJECT_TYPE_NAME (preset);
267 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
270 if (!name || strcmp (name, element_name))
275 /* get the version now so that the caller can check it */
276 if (preset_version) {
278 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
279 *preset_version = preset_parse_version (str);
288 GST_WARNING_OBJECT (preset, "Unable to read preset file %s: %s",
289 preset_path, error->message);
290 g_error_free (error);
291 g_key_file_free (in);
296 GST_WARNING_OBJECT (preset,
297 "Wrong element name in preset file %s. Expected %s, got %s",
298 preset_path, element_name, GST_STR_NULL (name));
300 g_key_file_free (in);
306 preset_merge (GKeyFile * system, GKeyFile * user)
309 gchar **groups, **keys;
310 gsize i, j, num_groups, num_keys;
312 /* copy file comment if there is any */
313 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
314 g_key_file_set_comment (system, NULL, NULL, str, NULL);
318 /* get groups in user and copy into system */
319 groups = g_key_file_get_groups (user, &num_groups);
320 for (i = 0; i < num_groups; i++) {
321 /* copy group comment if there is any */
322 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
323 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
327 /* ignore private groups */
328 if (groups[i][0] == '_')
331 /* if group already exists in system, remove and re-add keys from user */
332 if (g_key_file_has_group (system, groups[i])) {
333 g_key_file_remove_group (system, groups[i], NULL);
336 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
337 for (j = 0; j < num_keys; j++) {
338 /* copy key comment if there is any */
339 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
340 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
343 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
344 g_key_file_set_value (system, groups[i], keys[j], str);
352 /* reads the user and system presets files and merges them together. This
353 * function caches the GKeyFile on the element type. If there is no existing
354 * preset file, a new in-memory GKeyFile will be created. */
356 preset_get_keyfile (GstPreset * preset)
359 GType type = G_TYPE_FROM_INSTANCE (preset);
361 /* first see if the have a cached version for the type */
362 if (!(presets = g_type_get_qdata (type, preset_quark))) {
363 const gchar *preset_user_path, *preset_app_path, *preset_system_path;
364 guint64 version_system = G_GUINT64_CONSTANT (0);
365 guint64 version_app = G_GUINT64_CONSTANT (0);
366 guint64 version_user = G_GUINT64_CONSTANT (0);
367 guint64 version = G_GUINT64_CONSTANT (0);
368 gboolean merged = FALSE;
369 GKeyFile *in_user = NULL, *in_app = NULL, *in_system;
373 /* try to load the user, app and system presets, we do this to get the
374 * versions of all files. */
375 envvar = g_getenv ("GST_PRESET_PATH");
378 gchar **preset_dirs = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, -1);
380 for (i = 0; preset_dirs[i]; i++) {
381 gchar *preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
382 preset_dirs[i], G_OBJECT_TYPE_NAME (preset));
384 in_user = preset_open_and_parse_header (preset, preset_path,
386 g_free (preset_path);
390 g_strfreev (preset_dirs);
393 preset_get_paths (preset, &preset_user_path, &preset_app_path,
394 &preset_system_path);
396 in_user = preset_open_and_parse_header (preset, preset_user_path,
400 if (preset_app_path) {
401 in_app = preset_open_and_parse_header (preset, preset_app_path,
404 in_system = preset_open_and_parse_header (preset, preset_system_path,
407 /* compare version to check for merge */
410 version = version_system;
413 /* if system version is higher, merge */
414 if (version > version_app) {
415 preset_merge (presets, in_app);
416 g_key_file_free (in_app);
419 g_key_file_free (presets);
421 version = version_app;
425 /* if system or app version is higher, merge */
426 if (version > version_user) {
427 preset_merge (presets, in_user);
428 g_key_file_free (in_user);
432 g_key_file_free (presets);
438 /* we did not load a user, app or system presets file, create a new one */
439 presets = g_key_file_new ();
440 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
441 G_OBJECT_TYPE_NAME (preset));
444 /* attach the preset to the type */
445 g_type_set_qdata (type, preset_quark, (gpointer) presets);
448 gst_preset_default_save_presets_file (preset);
455 compare_strings (gchar ** a, gchar ** b, gpointer user_data)
457 return g_strcmp0 (*a, *b);
460 /* get a list of all supported preset names for an element */
462 gst_preset_default_get_preset_names (GstPreset * preset)
468 /* get the presets from the type */
469 if (!(presets = preset_get_keyfile (preset)))
472 /* get the groups, which are also the preset names */
473 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
476 /* remove all private group names starting with '_' from the array */
477 for (i = 0; i < num_groups; i++) {
478 if (groups[i][0] == '_') {
479 /* free private group */
481 /* move last element of list down */
483 /* move last element into removed element */
484 groups[i] = groups[num_groups];
485 groups[num_groups] = NULL;
489 GST_INFO_OBJECT (preset, "Empty preset file");
494 /* sort the array now */
495 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
496 (GCompareDataFunc) compare_strings, NULL);
503 GST_WARNING_OBJECT (preset, "Could not load presets");
508 GST_WARNING_OBJECT (preset, "Could not find preset groups");
513 /* get a list of all property names that are used for presets */
515 gst_preset_default_get_property_names (GstPreset * preset)
518 guint i, j = 0, n_props;
519 GObjectClass *gclass;
520 gboolean is_child_proxy;
521 gchar **result = NULL;
523 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
524 is_child_proxy = GST_IS_CHILD_PROXY (preset);
526 /* get a list of object properties */
527 props = g_object_class_list_properties (gclass, &n_props);
529 /* allocate array big enough to hold the worst case, including a terminating
531 result = g_new (gchar *, n_props + 1);
533 /* now filter out the properties that we can use for presets */
534 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
535 for (i = 0; i < n_props; i++) {
536 if (preset_skip_property (props[i]))
538 GST_DEBUG_OBJECT (preset, " using: %s", props[i]->name);
539 result[j++] = g_strdup (props[i]->name);
544 if (is_child_proxy) {
548 n_children = gst_child_proxy_get_children_count ((GstChildProxy *) preset);
549 for (c = 0; c < n_children; c++) {
550 child = gst_child_proxy_get_child_by_index ((GstChildProxy *) preset, c);
551 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (child));
553 props = g_object_class_list_properties (gclass, &n_props);
555 /* resize property name array */
556 result = g_renew (gchar *, result, j + n_props + 1);
558 /* now filter out the properties that we can use for presets */
559 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
560 for (i = 0; i < n_props; i++) {
561 if (preset_skip_property (props[i]))
563 GST_DEBUG_OBJECT (preset, " using: %s::%s",
564 GST_OBJECT_NAME (child), props[i]->name);
565 result[j++] = g_strdup_printf ("%s::%s", GST_OBJECT_NAME (child),
571 g_object_unref (child);
575 GST_INFO_OBJECT (preset, "object has no properties");
582 /* load the presets of @name for the instance @preset. Returns %FALSE if something
585 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
590 GObjectClass *gclass;
591 gboolean is_child_proxy;
593 /* get the presets from the type */
594 if (!(presets = preset_get_keyfile (preset)))
597 /* get the preset name */
598 if (!g_key_file_has_group (presets, name))
601 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
603 /* get the properties that we can configure in this element */
604 if (!(props = gst_preset_get_property_names (preset)))
607 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
608 is_child_proxy = GST_IS_CHILD_PROXY (preset);
610 /* for each of the property names, find the preset parameter and try to
611 * configure the property with its value */
612 for (i = 0; props[i]; i++) {
614 GValue gvalue = { 0, };
615 GParamSpec *property = NULL;
617 /* check if we have a settings for this element property */
618 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
619 /* the element has a property but the parameter is not in the keyfile */
620 GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
624 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
627 if (is_child_proxy) {
628 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
631 property = g_object_class_find_property (gclass, props[i]);
634 /* the parameter was in the keyfile, the element said it supported it but
635 * then the property was not found in the element. This should not happen. */
636 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
641 /* try to deserialize the property value from the keyfile and set it as
642 * the object property */
643 g_value_init (&gvalue, property->value_type);
644 if (gst_value_deserialize (&gvalue, str)) {
645 if (is_child_proxy) {
646 gst_child_proxy_set_property ((GstChildProxy *) preset, props[i],
649 g_object_set_property ((GObject *) preset, props[i], &gvalue);
652 GST_WARNING_OBJECT (preset,
653 "deserialization of value '%s' for property '%s' failed", str,
656 g_value_unset (&gvalue);
666 GST_WARNING_OBJECT (preset, "no presets");
671 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
676 GST_INFO_OBJECT (preset, "no properties");
681 /* save the presets file. A copy of the existing presets file is stored in a
684 gst_preset_default_save_presets_file (GstPreset * preset)
687 const gchar *preset_path;
688 GError *error = NULL;
689 gchar *bak_file_name;
690 gboolean backup = TRUE;
694 preset_get_paths (preset, &preset_path, NULL, NULL);
696 /* get the presets from the type */
697 if (!(presets = preset_get_keyfile (preset)))
700 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
702 /* create backup if possible */
703 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
704 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
705 if (g_unlink (bak_file_name)) {
707 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
712 if (g_rename (preset_path, bak_file_name)) {
713 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
717 g_free (bak_file_name);
719 /* update gstreamer version */
720 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
723 /* get new contents, wee need this to save it */
724 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
728 if (!g_file_set_contents (preset_path, data, data_size, &error))
738 GST_WARNING_OBJECT (preset,
739 "no presets, trying to unlink possibly existing preset file: '%s'",
741 g_unlink (preset_path);
746 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
748 g_error_free (error);
754 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
755 preset_path, error->message);
756 g_error_free (error);
762 /* save the preset with the given name */
764 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
769 GObjectClass *gclass;
770 gboolean is_child_proxy;
772 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
774 /* get the presets from the type */
775 if (!(presets = preset_get_keyfile (preset)))
778 /* take copies of current gobject properties from preset */
779 if (!(props = gst_preset_get_property_names (preset)))
782 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
783 is_child_proxy = GST_IS_CHILD_PROXY (preset);
785 /* loop over the object properties and store the property value in the
787 for (i = 0; props[i]; i++) {
788 GValue gvalue = { 0, };
790 GParamSpec *property = NULL;
792 if (is_child_proxy) {
793 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
796 property = g_object_class_find_property (gclass, props[i]);
799 /* the element said it supported the property but then it does not have
800 * that property. This should not happen. */
801 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
805 g_value_init (&gvalue, property->value_type);
806 if (is_child_proxy) {
807 gst_child_proxy_get_property ((GstChildProxy *) preset, props[i],
810 g_object_get_property ((GObject *) preset, props[i], &gvalue);
813 if ((str = gst_value_serialize (&gvalue))) {
814 g_key_file_set_string (presets, name, props[i], (gpointer) str);
817 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
820 g_value_unset (&gvalue);
822 GST_INFO_OBJECT (preset, " saved");
825 /* save updated version */
826 return gst_preset_default_save_presets_file (preset);
831 GST_WARNING_OBJECT (preset, "no presets");
836 GST_INFO_OBJECT (preset, "no properties");
841 /* copies all keys and comments from one group to another, deleting the old
844 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
845 const gchar * new_name)
852 /* get the presets from the type */
853 if (!(presets = preset_get_keyfile (preset)))
856 if (!g_key_file_has_group (presets, old_name))
859 /* copy group comment if there is any */
860 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
861 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
865 /* get all keys from the old group and copy them in the new group */
866 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
867 for (i = 0; i < num_keys; i++) {
868 /* copy key comment if there is any */
869 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
870 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
874 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
875 g_key_file_set_value (presets, new_name, keys[i], str);
880 /* remove old group */
881 g_key_file_remove_group (presets, old_name, NULL);
883 /* save updated version */
884 return gst_preset_default_save_presets_file (preset);
889 GST_WARNING_OBJECT (preset, "no presets");
894 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
899 /* delete a group from the keyfile */
901 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
905 /* get the presets from the type */
906 if (!(presets = preset_get_keyfile (preset)))
910 if (!g_key_file_has_group (presets, name))
913 /* remove the group */
914 g_key_file_remove_group (presets, name, NULL);
916 /* save updated version */
917 return gst_preset_default_save_presets_file (preset);
922 GST_WARNING_OBJECT (preset, "no presets");
927 GST_WARNING_OBJECT (preset, "no preset named %s", name);
933 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
934 const gchar * tag, const gchar * value)
939 /* get the presets from the type */
940 if (!(presets = preset_get_keyfile (preset)))
943 key = g_strdup_printf ("_meta/%s", tag);
944 if (value && *value) {
945 g_key_file_set_value (presets, name, key, value);
947 g_key_file_remove_key (presets, name, key, NULL);
951 /* save updated keyfile */
952 return gst_preset_default_save_presets_file (preset);
957 GST_WARNING_OBJECT (preset, "no presets");
962 /* the caller must free @value after usage */
964 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
965 const gchar * tag, gchar ** value)
970 /* get the presets from the type */
971 if (!(presets = preset_get_keyfile (preset)))
974 key = g_strdup_printf ("_meta/%s", tag);
975 *value = g_key_file_get_value (presets, name, key, NULL);
983 GST_WARNING_OBJECT (preset, "no presets");
992 * gst_preset_get_preset_names:
993 * @preset: a #GObject that implements #GstPreset
995 * Get a copy of preset names as a %NULL terminated string array.
997 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
998 * list with names, use g_strfreev() after usage.
1001 gst_preset_get_preset_names (GstPreset * preset)
1003 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
1005 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
1009 * gst_preset_get_property_names:
1010 * @preset: a #GObject that implements #GstPreset
1012 * Get a the names of the GObject properties that can be used for presets.
1014 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
1015 * array of property names which should be freed with g_strfreev() after use.
1018 gst_preset_get_property_names (GstPreset * preset)
1020 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
1022 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
1026 * gst_preset_load_preset:
1027 * @preset: a #GObject that implements #GstPreset
1028 * @name: preset name to load
1030 * Load the given preset.
1032 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1035 gst_preset_load_preset (GstPreset * preset, const gchar * name)
1037 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1038 g_return_val_if_fail (name, FALSE);
1040 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
1044 * gst_preset_save_preset:
1045 * @preset: a #GObject that implements #GstPreset
1046 * @name: preset name to save
1048 * Save the current object settings as a preset under the given name. If there
1049 * is already a preset by this @name it will be overwritten.
1051 * Returns: %TRUE for success, %FALSE
1054 gst_preset_save_preset (GstPreset * preset, const gchar * name)
1056 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1057 g_return_val_if_fail (name, FALSE);
1059 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
1063 * gst_preset_rename_preset:
1064 * @preset: a #GObject that implements #GstPreset
1065 * @old_name: current preset name
1066 * @new_name: new preset name
1068 * Renames a preset. If there is already a preset by the @new_name it will be
1071 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1074 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1075 const gchar * new_name)
1077 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1078 g_return_val_if_fail (old_name, FALSE);
1079 g_return_val_if_fail (new_name, FALSE);
1081 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1086 * gst_preset_delete_preset:
1087 * @preset: a #GObject that implements #GstPreset
1088 * @name: preset name to remove
1090 * Delete the given preset.
1092 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1095 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1097 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1098 g_return_val_if_fail (name, FALSE);
1100 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1104 * gst_preset_set_meta:
1105 * @preset: a #GObject that implements #GstPreset
1106 * @name: preset name
1107 * @tag: meta data item name
1108 * @value: (allow-none): new value
1110 * Sets a new @value for an existing meta data item or adds a new item. Meta
1111 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1112 * @value will unset an existing value.
1114 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1117 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1118 const gchar * value)
1120 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1121 g_return_val_if_fail (name, FALSE);
1122 g_return_val_if_fail (tag, FALSE);
1124 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1128 * gst_preset_get_meta:
1129 * @preset: a #GObject that implements #GstPreset
1130 * @name: preset name
1131 * @tag: meta data item name
1132 * @value: (out callee-allocates): value
1134 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1135 * something like e.g. "comment". Returned values need to be released when done.
1137 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1138 * or no value for the given @tag
1141 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1144 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1145 g_return_val_if_fail (name, FALSE);
1146 g_return_val_if_fail (tag, FALSE);
1147 g_return_val_if_fail (value, FALSE);
1149 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1153 * gst_preset_set_app_dir:
1154 * @app_dir: the application specific preset dir
1156 * Sets an extra directory as an absolute path that should be considered when
1157 * looking for presets. Any presets in the application dir will shadow the
1160 * Returns: %TRUE for success, %FALSE if the dir already has been set
1163 gst_preset_set_app_dir (const gchar * app_dir)
1165 g_return_val_if_fail (app_dir, FALSE);
1167 if (!preset_app_dir) {
1168 preset_app_dir = g_strdup (app_dir);
1175 * gst_preset_get_app_dir:
1177 * Gets the directory for application specific presets if set by the
1180 * Returns: (nullable): the directory or %NULL, don't free or modify
1184 gst_preset_get_app_dir (void)
1186 return preset_app_dir;
1190 * gst_preset_is_editable:
1191 * @preset: a #GObject that implements #GstPreset
1193 * Check if one can add new presets, change existing ones and remove presets.
1195 * Returns: %TRUE if presets are editable or %FALSE if they are static
1200 gst_preset_is_editable (GstPreset * preset)
1202 GstPresetInterface *iface = GST_PRESET_GET_INTERFACE (preset);
1204 return iface->save_preset && iface->delete_preset;
1207 /* class internals */
1210 gst_preset_class_init (GstPresetInterface * iface)
1212 iface->get_preset_names = gst_preset_default_get_preset_names;
1213 iface->get_property_names = gst_preset_default_get_property_names;
1215 iface->load_preset = gst_preset_default_load_preset;
1216 iface->save_preset = gst_preset_default_save_preset;
1217 iface->rename_preset = gst_preset_default_rename_preset;
1218 iface->delete_preset = gst_preset_default_delete_preset;
1220 iface->set_meta = gst_preset_default_set_meta;
1221 iface->get_meta = gst_preset_default_get_meta;
1225 gst_preset_base_init (gpointer g_class)
1227 static gboolean initialized = FALSE;
1230 /* init default implementation */
1231 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1232 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1234 /* create quarks for use with g_type_{g,s}et_qdata() */
1235 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1236 preset_user_path_quark =
1237 g_quark_from_static_string ("GstPreset::user_path");
1238 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1239 preset_system_path_quark =
1240 g_quark_from_static_string ("GstPreset::system_path");
1243 /* create interface properties, each element would need to override this
1244 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1245 * and in _set_property() do
1246 * case PROP_PRESET_NAME: {
1247 * gchar *name = g_value_get_string (value);
1249 * gst_preset_load_preset(preset, name);
1252 g_object_interface_install_property (g_class,
1253 g_param_spec_string ("preset-name",
1254 "preset-name property",
1255 "load given preset", NULL, G_PARAM_WRITABLE));
1263 gst_preset_get_type (void)
1265 static volatile gsize type = 0;
1267 if (g_once_init_enter (&type)) {
1269 const GTypeInfo info = {
1270 sizeof (GstPresetInterface),
1271 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1272 NULL, /* base_finalize */
1273 (GClassInitFunc) gst_preset_class_init, /* class_init */
1274 NULL, /* class_finalize */
1275 NULL, /* class_data */
1277 0, /* n_preallocs */
1278 NULL /* instance_init */
1280 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1281 g_once_init_leave (&type, _type);