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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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.
38 * The default implementation supports presets located in a system directory,
39 * application specific directory and in the users home directory. When getting
40 * a list of presets individual presets are read and overlaid in 1) system,
41 * 2) application and 3) user order. Whenever an earlier entry is newer, the
42 * later entries will be updated.
47 * - we need to avoid two instances writing the preset file
48 * -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
50 * -> better save the new file to a tempfile and then rename?
51 * - we like to know when any other instance makes changes to the keyfile
52 * - then ui can be updated
53 * - and we make sure that we don't lose edits
54 * -> its the same problem actually, once for inside a process, once system-
56 * - can we use a lock inside a names shared memory segment?
58 * - should there be a 'preset-list' property to get the preset list
59 * (and to connect a notify:: to to listen for changes)
60 * we could use gnome_vfs_monitor_add() to monitor the user preset_file.
62 * - should there be a 'preset-name' property so that we can set a preset via
63 * gst-launch, or should we handle this with special syntax in gst-launch:
64 * gst-launch element preset:<preset-name> property=value ...
65 * - this would allow to have preset-bundles too (a preset on bins that
66 * specifies presets for children
69 #include "gst_private.h"
71 #include "gstpreset.h"
72 #include "gstchildproxy.h"
79 #include <glib/gstdio.h>
81 #define GST_CAT_DEFAULT preset_debug
82 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
84 /* defines for keyfile usage, this group contains the element type name and
85 * version these presets belong to. */
86 #define PRESET_HEADER "_presets_"
88 /* keys of the preset header section */
89 #define PRESET_HEADER_ELEMENT_NAME "element-name"
90 #define PRESET_HEADER_VERSION "version"
92 static GQuark preset_user_path_quark = 0;
93 static GQuark preset_app_path_quark = 0;
94 static GQuark preset_system_path_quark = 0;
95 static GQuark preset_quark = 0;
97 /*static GQuark property_list_quark = 0;*/
99 /* the application can set a custom path that is checked in addition to standard
100 * system and user dirs. This helps to develop new presets first local to the
103 static gchar *preset_app_dir = NULL;
105 /* default iface implementation */
107 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
111 * @preset: a #GObject that implements #GstPreset
112 * @preset_user_path: location for path or %NULL
113 * @preset_app_path: location for path or %NULL
114 * @preset_system_path: location for path or %NULL
116 * Fetch the preset_path for user local, application specific and system wide
117 * settings. Don't free after use.
119 * Returns: %FALSE if no paths could be found.
122 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
123 const gchar ** preset_app_path, const gchar ** preset_system_path)
125 GType type = G_TYPE_FROM_INSTANCE (preset);
127 const gchar *element_name;
129 /* we use the element name when we must construct the paths */
130 element_name = G_OBJECT_TYPE_NAME (preset);
131 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
133 if (preset_user_path) {
134 /* preset user path requested, see if we have it cached in the qdata */
135 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
138 /* user presets go in user's XDG data directory. */
139 preset_dir = g_build_filename (g_get_user_data_dir (),
140 "gstreamer-" GST_API_VERSION, "presets", NULL);
141 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
143 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
145 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
147 g_mkdir_with_parents (preset_dir, 0755);
150 /* cache the preset path to the type */
151 g_type_set_qdata (type, preset_user_path_quark, preset_path);
153 *preset_user_path = preset_path;
156 if (preset_app_path) {
157 if (preset_app_dir) {
158 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
159 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
160 preset_app_dir, element_name);
161 GST_INFO_OBJECT (preset, "application_preset_path: '%s'", preset_path);
163 /* cache the preset path to the type */
164 g_type_set_qdata (type, preset_app_path_quark, preset_path);
166 *preset_app_path = preset_path;
168 *preset_app_path = NULL;
172 if (preset_system_path) {
173 /* preset system path requested, see if we have it cached in the qdata */
174 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
177 /* system presets in '$GST_DATADIR/gstreamer-1.0/presets/GstAudioPanorama.prs' */
178 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_API_VERSION,
180 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
181 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
182 preset_dir, element_name);
183 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
185 g_mkdir_with_parents (preset_dir, 0755);
188 /* cache the preset path to the type */
189 g_type_set_qdata (type, preset_system_path_quark, preset_path);
191 *preset_system_path = preset_path;
197 preset_skip_property (GParamSpec * property)
199 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
200 (property->flags & G_PARAM_CONSTRUCT_ONLY))
202 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
207 preset_parse_version (const gchar * str_version)
209 guint major, minor, micro, nano;
212 major = minor = micro = nano = 0;
214 /* parse version (e.g. 0.10.15.1) to guint64 */
215 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
216 /* make sure we have at least "major.minor" */
220 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
221 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
224 return G_GUINT64_CONSTANT (0);
228 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
229 guint64 * preset_version)
232 GError *error = NULL;
234 const gchar *element_name;
237 in = g_key_file_new ();
239 res = g_key_file_load_from_file (in, preset_path,
240 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
241 if (!res || error != NULL)
244 /* element type name and preset name must match or we are dealing with a wrong
246 element_name = G_OBJECT_TYPE_NAME (preset);
248 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
251 if (!name || strcmp (name, element_name))
256 /* get the version now so that the caller can check it */
257 if (preset_version) {
259 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
260 *preset_version = preset_parse_version (str);
269 GST_WARNING_OBJECT (preset, "Unable to read preset file %s: %s",
270 preset_path, error->message);
271 g_error_free (error);
272 g_key_file_free (in);
277 GST_WARNING_OBJECT (preset,
278 "Wrong element name in preset file %s. Expected %s, got %s",
279 preset_path, element_name, GST_STR_NULL (name));
281 g_key_file_free (in);
287 preset_merge (GKeyFile * system, GKeyFile * user)
290 gchar **groups, **keys;
291 gsize i, j, num_groups, num_keys;
293 /* copy file comment if there is any */
294 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
295 g_key_file_set_comment (system, NULL, NULL, str, NULL);
299 /* get groups in user and copy into system */
300 groups = g_key_file_get_groups (user, &num_groups);
301 for (i = 0; i < num_groups; i++) {
302 /* copy group comment if there is any */
303 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
304 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
308 /* ignore private groups */
309 if (groups[i][0] == '_')
312 /* if group already exists in system, remove and re-add keys from user */
313 if (g_key_file_has_group (system, groups[i])) {
314 g_key_file_remove_group (system, groups[i], NULL);
317 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
318 for (j = 0; j < num_keys; j++) {
319 /* copy key comment if there is any */
320 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
321 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
324 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
325 g_key_file_set_value (system, groups[i], keys[j], str);
333 /* reads the user and system presets files and merges them together. This
334 * function caches the GKeyFile on the element type. If there is no existing
335 * preset file, a new in-memory GKeyFile will be created. */
337 preset_get_keyfile (GstPreset * preset)
340 GType type = G_TYPE_FROM_INSTANCE (preset);
342 /* first see if the have a cached version for the type */
343 if (!(presets = g_type_get_qdata (type, preset_quark))) {
344 const gchar *preset_user_path, *preset_app_path, *preset_system_path;
345 guint64 version_system = G_GUINT64_CONSTANT (0);
346 guint64 version_app = G_GUINT64_CONSTANT (0);
347 guint64 version_user = G_GUINT64_CONSTANT (0);
348 guint64 version = G_GUINT64_CONSTANT (0);
349 gboolean merged = FALSE;
350 GKeyFile *in_user, *in_app = NULL, *in_system;
352 preset_get_paths (preset, &preset_user_path, &preset_app_path,
353 &preset_system_path);
355 /* try to load the user, app and system presets, we do this to get the
356 * versions of all files. */
357 in_user = preset_open_and_parse_header (preset, preset_user_path,
359 if (preset_app_path) {
360 in_app = preset_open_and_parse_header (preset, preset_app_path,
363 in_system = preset_open_and_parse_header (preset, preset_system_path,
366 /* compare version to check for merge */
369 version = version_system;
372 /* if system version is higher, merge */
373 if (version > version_app) {
374 preset_merge (presets, in_app);
375 g_key_file_free (in_app);
378 g_key_file_free (presets);
380 version = version_system;
384 /* if system or app version is higher, merge */
385 if (version > version_user) {
386 preset_merge (presets, in_user);
387 g_key_file_free (in_user);
391 g_key_file_free (presets);
393 version = version_user;
398 /* we did not load a user, app or system presets file, create a new one */
399 presets = g_key_file_new ();
400 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
401 G_OBJECT_TYPE_NAME (preset));
404 /* attach the preset to the type */
405 g_type_set_qdata (type, preset_quark, (gpointer) presets);
408 gst_preset_default_save_presets_file (preset);
414 /* get a list of all supported preset names for an element */
416 gst_preset_default_get_preset_names (GstPreset * preset)
422 /* get the presets from the type */
423 if (!(presets = preset_get_keyfile (preset)))
426 /* get the groups, which are also the preset names */
427 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
430 /* remove all private group names starting with '_' from the array */
431 for (i = 0; i < num_groups; i++) {
432 if (groups[i][0] == '_') {
433 /* free private group */
435 /* move last element of list down */
437 /* move last element into removed element */
438 groups[i] = groups[num_groups];
439 groups[num_groups] = NULL;
442 /* sort the array now */
443 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
444 (GCompareDataFunc) strcmp, NULL);
451 GST_WARNING_OBJECT (preset, "Could not load presets");
456 GST_WARNING_OBJECT (preset, "Could not find preset groups");
461 /* get a list of all property names that are used for presets */
463 gst_preset_default_get_property_names (GstPreset * preset)
466 guint i, j = 0, n_props;
467 GObjectClass *gclass;
468 gboolean is_child_proxy;
469 gchar **result = NULL;
471 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
472 is_child_proxy = GST_IS_CHILD_PROXY (preset);
474 /* get a list of object properties */
475 props = g_object_class_list_properties (gclass, &n_props);
477 /* allocate array big enough to hold the worst case, including a terminating
479 result = g_new (gchar *, n_props + 1);
481 /* now filter out the properties that we can use for presets */
482 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
483 for (i = 0; i < n_props; i++) {
484 if (preset_skip_property (props[i]))
486 GST_DEBUG_OBJECT (preset, " using: %s", props[i]->name);
487 result[j++] = g_strdup (props[i]->name);
492 if (is_child_proxy) {
496 n_children = gst_child_proxy_get_children_count ((GstChildProxy *) preset);
497 for (c = 0; c < n_children; c++) {
498 child = gst_child_proxy_get_child_by_index ((GstChildProxy *) preset, c);
499 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (child));
501 props = g_object_class_list_properties (gclass, &n_props);
503 /* resize property name array */
504 result = g_renew (gchar *, result, j + n_props + 1);
506 /* now filter out the properties that we can use for presets */
507 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
508 for (i = 0; i < n_props; i++) {
509 if (preset_skip_property (props[i]))
511 GST_DEBUG_OBJECT (preset, " using: %s::%s",
512 GST_OBJECT_NAME (child), props[i]->name);
513 result[j++] = g_strdup_printf ("%s::%s", GST_OBJECT_NAME (child),
521 GST_INFO_OBJECT (preset, "object has no properties");
528 /* load the presets of @name for the instance @preset. Returns %FALSE if something
531 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
536 GObjectClass *gclass;
537 gboolean is_child_proxy;
539 /* get the presets from the type */
540 if (!(presets = preset_get_keyfile (preset)))
543 /* get the preset name */
544 if (!g_key_file_has_group (presets, name))
547 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
549 /* get the properties that we can configure in this element */
550 if (!(props = gst_preset_get_property_names (preset)))
553 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
554 is_child_proxy = GST_IS_CHILD_PROXY (preset);
556 /* for each of the property names, find the preset parameter and try to
557 * configure the property with its value */
558 for (i = 0; props[i]; i++) {
560 GValue gvalue = { 0, };
561 GParamSpec *property = NULL;
563 /* check if we have a settings for this element property */
564 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
565 /* the element has a property but the parameter is not in the keyfile */
566 GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
570 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
573 if (is_child_proxy) {
574 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
577 property = g_object_class_find_property (gclass, props[i]);
580 /* the parameter was in the keyfile, the element said it supported it but
581 * then the property was not found in the element. This should not happen. */
582 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
587 /* try to deserialize the property value from the keyfile and set it as
588 * the object property */
589 g_value_init (&gvalue, property->value_type);
590 if (gst_value_deserialize (&gvalue, str)) {
591 if (is_child_proxy) {
592 gst_child_proxy_set_property ((GstChildProxy *) preset, props[i],
595 g_object_set_property ((GObject *) preset, props[i], &gvalue);
598 GST_WARNING_OBJECT (preset,
599 "deserialization of value '%s' for property '%s' failed", str,
602 g_value_unset (&gvalue);
612 GST_WARNING_OBJECT (preset, "no presets");
617 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
622 GST_INFO_OBJECT (preset, "no properties");
627 /* save the presets file. A copy of the existing presets file is stored in a
630 gst_preset_default_save_presets_file (GstPreset * preset)
633 const gchar *preset_path;
634 GError *error = NULL;
635 gchar *bak_file_name;
636 gboolean backup = TRUE;
640 preset_get_paths (preset, &preset_path, NULL, NULL);
642 /* get the presets from the type */
643 if (!(presets = preset_get_keyfile (preset)))
646 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
648 /* create backup if possible */
649 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
650 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
651 if (g_unlink (bak_file_name)) {
653 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
658 if (g_rename (preset_path, bak_file_name)) {
659 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
663 g_free (bak_file_name);
665 /* update gstreamer version */
666 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
669 /* get new contents, wee need this to save it */
670 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
674 if (!g_file_set_contents (preset_path, data, data_size, &error))
684 GST_WARNING_OBJECT (preset,
685 "no presets, trying to unlink possibly existing preset file: '%s'",
687 g_unlink (preset_path);
692 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
694 g_error_free (error);
700 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
701 preset_path, error->message);
702 g_error_free (error);
708 /* save the preset with the given name */
710 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
715 GObjectClass *gclass;
716 gboolean is_child_proxy;
718 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
720 /* get the presets from the type */
721 if (!(presets = preset_get_keyfile (preset)))
724 /* take copies of current gobject properties from preset */
725 if (!(props = gst_preset_get_property_names (preset)))
728 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
729 is_child_proxy = GST_IS_CHILD_PROXY (preset);
731 /* loop over the object properties and store the property value in the
733 for (i = 0; props[i]; i++) {
734 GValue gvalue = { 0, };
736 GParamSpec *property = NULL;
738 if (is_child_proxy) {
739 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
742 property = g_object_class_find_property (gclass, props[i]);
745 /* the element said it supported the property but then it does not have
746 * that property. This should not happen. */
747 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
751 g_value_init (&gvalue, property->value_type);
752 if (is_child_proxy) {
753 gst_child_proxy_get_property ((GstChildProxy *) preset, props[i],
756 g_object_get_property ((GObject *) preset, props[i], &gvalue);
759 if ((str = gst_value_serialize (&gvalue))) {
760 g_key_file_set_string (presets, name, props[i], (gpointer) str);
763 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
766 g_value_unset (&gvalue);
768 GST_INFO_OBJECT (preset, " saved");
771 /* save updated version */
772 return gst_preset_default_save_presets_file (preset);
777 GST_WARNING_OBJECT (preset, "no presets");
782 GST_INFO_OBJECT (preset, "no properties");
787 /* copies all keys and comments from one group to another, deleting the old
790 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
791 const gchar * new_name)
798 /* get the presets from the type */
799 if (!(presets = preset_get_keyfile (preset)))
802 if (!g_key_file_has_group (presets, old_name))
805 /* copy group comment if there is any */
806 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
807 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
811 /* get all keys from the old group and copy them in the new group */
812 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
813 for (i = 0; i < num_keys; i++) {
814 /* copy key comment if there is any */
815 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
816 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
820 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
821 g_key_file_set_value (presets, new_name, keys[i], str);
826 /* remove old group */
827 g_key_file_remove_group (presets, old_name, NULL);
829 /* save updated version */
830 return gst_preset_default_save_presets_file (preset);
835 GST_WARNING_OBJECT (preset, "no presets");
840 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
845 /* delete a group from the keyfile */
847 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
851 /* get the presets from the type */
852 if (!(presets = preset_get_keyfile (preset)))
856 if (!g_key_file_has_group (presets, name))
859 /* remove the group */
860 g_key_file_remove_group (presets, name, NULL);
862 /* save updated version */
863 return gst_preset_default_save_presets_file (preset);
868 GST_WARNING_OBJECT (preset, "no presets");
873 GST_WARNING_OBJECT (preset, "no preset named %s", name);
879 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
880 const gchar * tag, const gchar * value)
885 /* get the presets from the type */
886 if (!(presets = preset_get_keyfile (preset)))
889 key = g_strdup_printf ("_meta/%s", tag);
890 if (value && *value) {
891 g_key_file_set_value (presets, name, key, value);
893 g_key_file_remove_key (presets, name, key, NULL);
897 /* save updated keyfile */
898 return gst_preset_default_save_presets_file (preset);
903 GST_WARNING_OBJECT (preset, "no presets");
908 /* the caller must free @value after usage */
910 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
911 const gchar * tag, 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 *value = g_key_file_get_value (presets, name, key, NULL);
929 GST_WARNING_OBJECT (preset, "no presets");
938 * gst_preset_get_preset_names:
939 * @preset: a #GObject that implements #GstPreset
941 * Get a copy of preset names as a NULL terminated string array.
943 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
944 * list with names, ue g_strfreev() after usage.
947 gst_preset_get_preset_names (GstPreset * preset)
949 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
951 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
955 * gst_preset_get_property_names:
956 * @preset: a #GObject that implements #GstPreset
958 * Get a the names of the GObject properties that can be used for presets.
960 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
961 * array of property names which should be freed with g_strfreev() after use.
964 gst_preset_get_property_names (GstPreset * preset)
966 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
968 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
972 * gst_preset_load_preset:
973 * @preset: a #GObject that implements #GstPreset
974 * @name: preset name to load
976 * Load the given preset.
978 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
981 gst_preset_load_preset (GstPreset * preset, const gchar * name)
983 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
984 g_return_val_if_fail (name, FALSE);
986 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
990 * gst_preset_save_preset:
991 * @preset: a #GObject that implements #GstPreset
992 * @name: preset name to save
994 * Save the current object settings as a preset under the given name. If there
995 * is already a preset by this @name it will be overwritten.
997 * Returns: %TRUE for success, %FALSE
1000 gst_preset_save_preset (GstPreset * preset, const gchar * name)
1002 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1003 g_return_val_if_fail (name, FALSE);
1005 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
1009 * gst_preset_rename_preset:
1010 * @preset: a #GObject that implements #GstPreset
1011 * @old_name: current preset name
1012 * @new_name: new preset name
1014 * Renames a preset. If there is already a preset by the @new_name it will be
1017 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1020 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1021 const gchar * new_name)
1023 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1024 g_return_val_if_fail (old_name, FALSE);
1025 g_return_val_if_fail (new_name, FALSE);
1027 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1032 * gst_preset_delete_preset:
1033 * @preset: a #GObject that implements #GstPreset
1034 * @name: preset name to remove
1036 * Delete the given preset.
1038 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1041 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1043 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1044 g_return_val_if_fail (name, FALSE);
1046 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1050 * gst_preset_set_meta:
1051 * @preset: a #GObject that implements #GstPreset
1052 * @name: preset name
1053 * @tag: meta data item name
1056 * Sets a new @value for an existing meta data item or adds a new item. Meta
1057 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1058 * @value will unset an existing value.
1060 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1063 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1064 const gchar * value)
1066 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1067 g_return_val_if_fail (name, FALSE);
1068 g_return_val_if_fail (tag, FALSE);
1070 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1074 * gst_preset_get_meta:
1075 * @preset: a #GObject that implements #GstPreset
1076 * @name: preset name
1077 * @tag: meta data item name
1078 * @value: (out callee-allocates): value
1080 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1081 * something like e.g. "comment". Returned values need to be released when done.
1083 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1084 * or no value for the given @tag
1087 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1090 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1091 g_return_val_if_fail (name, FALSE);
1092 g_return_val_if_fail (tag, FALSE);
1093 g_return_val_if_fail (value, FALSE);
1095 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1099 * gst_preset_set_app_dir:
1100 * @app_dir: the application specific preset dir
1102 * Sets an extra directory as an absolute path that should be considered when
1103 * looking for presets. Any presets in the application dir will shadow the
1106 * Returns: %TRUE for success, %FALSE if the dir already has been set
1109 gst_preset_set_app_dir (const gchar * app_dir)
1111 g_return_val_if_fail (app_dir, FALSE);
1113 if (!preset_app_dir) {
1114 preset_app_dir = g_strdup (app_dir);
1121 * gst_preset_get_app_dir:
1123 * Gets the directory for application specific presets if set by the
1126 * Returns: the directory or %NULL, don't free or modify the string
1129 gst_preset_get_app_dir (void)
1131 return preset_app_dir;
1134 /* class internals */
1137 gst_preset_class_init (GstPresetInterface * iface)
1139 iface->get_preset_names = gst_preset_default_get_preset_names;
1140 iface->get_property_names = gst_preset_default_get_property_names;
1142 iface->load_preset = gst_preset_default_load_preset;
1143 iface->save_preset = gst_preset_default_save_preset;
1144 iface->rename_preset = gst_preset_default_rename_preset;
1145 iface->delete_preset = gst_preset_default_delete_preset;
1147 iface->set_meta = gst_preset_default_set_meta;
1148 iface->get_meta = gst_preset_default_get_meta;
1152 gst_preset_base_init (gpointer g_class)
1154 static gboolean initialized = FALSE;
1157 /* init default implementation */
1158 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1159 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1161 /* create quarks for use with g_type_{g,s}et_qdata() */
1162 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1163 preset_user_path_quark =
1164 g_quark_from_static_string ("GstPreset::user_path");
1165 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1166 preset_system_path_quark =
1167 g_quark_from_static_string ("GstPreset::system_path");
1170 property_list_quark = g_quark_from_static_string ("GstPreset::properties");
1172 /* create interface properties, each element would need to override this
1173 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1174 * and in _set_property() do
1175 * case PROP_PRESET_NAME: {
1176 * gchar *name = g_value_get_string (value);
1178 * gst_preset_load_preset(preset, name);
1181 g_object_interface_install_property (g_class,
1182 g_param_spec_string ("preset-name",
1183 "preset-name property",
1184 "load given preset", NULL, G_PARAM_WRITABLE));
1192 gst_preset_get_type (void)
1194 static volatile gsize type = 0;
1196 if (g_once_init_enter (&type)) {
1198 const GTypeInfo info = {
1199 sizeof (GstPresetInterface),
1200 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1201 NULL, /* base_finalize */
1202 (GClassInitFunc) gst_preset_class_init, /* class_init */
1203 NULL, /* class_finalize */
1204 NULL, /* class_data */
1206 0, /* n_preallocs */
1207 NULL /* instance_init */
1209 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1210 g_once_init_leave (&type, _type);