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.
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);
397 /* we did not load a user, app or system presets file, create a new one */
398 presets = g_key_file_new ();
399 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
400 G_OBJECT_TYPE_NAME (preset));
403 /* attach the preset to the type */
404 g_type_set_qdata (type, preset_quark, (gpointer) presets);
407 gst_preset_default_save_presets_file (preset);
413 /* get a list of all supported preset names for an element */
415 gst_preset_default_get_preset_names (GstPreset * preset)
421 /* get the presets from the type */
422 if (!(presets = preset_get_keyfile (preset)))
425 /* get the groups, which are also the preset names */
426 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
429 /* remove all private group names starting with '_' from the array */
430 for (i = 0; i < num_groups; i++) {
431 if (groups[i][0] == '_') {
432 /* free private group */
434 /* move last element of list down */
436 /* move last element into removed element */
437 groups[i] = groups[num_groups];
438 groups[num_groups] = NULL;
441 /* sort the array now */
442 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
443 (GCompareDataFunc) strcmp, NULL);
450 GST_WARNING_OBJECT (preset, "Could not load presets");
455 GST_WARNING_OBJECT (preset, "Could not find preset groups");
460 /* get a list of all property names that are used for presets */
462 gst_preset_default_get_property_names (GstPreset * preset)
465 guint i, j = 0, n_props;
466 GObjectClass *gclass;
467 gboolean is_child_proxy;
468 gchar **result = NULL;
470 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
471 is_child_proxy = GST_IS_CHILD_PROXY (preset);
473 /* get a list of object properties */
474 props = g_object_class_list_properties (gclass, &n_props);
476 /* allocate array big enough to hold the worst case, including a terminating
478 result = g_new (gchar *, n_props + 1);
480 /* now filter out the properties that we can use for presets */
481 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
482 for (i = 0; i < n_props; i++) {
483 if (preset_skip_property (props[i]))
485 GST_DEBUG_OBJECT (preset, " using: %s", props[i]->name);
486 result[j++] = g_strdup (props[i]->name);
491 if (is_child_proxy) {
495 n_children = gst_child_proxy_get_children_count ((GstChildProxy *) preset);
496 for (c = 0; c < n_children; c++) {
497 child = gst_child_proxy_get_child_by_index ((GstChildProxy *) preset, c);
498 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (child));
500 props = g_object_class_list_properties (gclass, &n_props);
502 /* resize property name array */
503 result = g_renew (gchar *, result, j + n_props + 1);
505 /* now filter out the properties that we can use for presets */
506 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
507 for (i = 0; i < n_props; i++) {
508 if (preset_skip_property (props[i]))
510 GST_DEBUG_OBJECT (preset, " using: %s::%s",
511 GST_OBJECT_NAME (child), props[i]->name);
512 result[j++] = g_strdup_printf ("%s::%s", GST_OBJECT_NAME (child),
520 GST_INFO_OBJECT (preset, "object has no properties");
527 /* load the presets of @name for the instance @preset. Returns %FALSE if something
530 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
535 GObjectClass *gclass;
536 gboolean is_child_proxy;
538 /* get the presets from the type */
539 if (!(presets = preset_get_keyfile (preset)))
542 /* get the preset name */
543 if (!g_key_file_has_group (presets, name))
546 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
548 /* get the properties that we can configure in this element */
549 if (!(props = gst_preset_get_property_names (preset)))
552 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
553 is_child_proxy = GST_IS_CHILD_PROXY (preset);
555 /* for each of the property names, find the preset parameter and try to
556 * configure the property with its value */
557 for (i = 0; props[i]; i++) {
559 GValue gvalue = { 0, };
560 GParamSpec *property = NULL;
562 /* check if we have a settings for this element property */
563 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
564 /* the element has a property but the parameter is not in the keyfile */
565 GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
569 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
572 if (is_child_proxy) {
573 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
576 property = g_object_class_find_property (gclass, props[i]);
579 /* the parameter was in the keyfile, the element said it supported it but
580 * then the property was not found in the element. This should not happen. */
581 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
586 /* try to deserialize the property value from the keyfile and set it as
587 * the object property */
588 g_value_init (&gvalue, property->value_type);
589 if (gst_value_deserialize (&gvalue, str)) {
590 if (is_child_proxy) {
591 gst_child_proxy_set_property ((GstChildProxy *) preset, props[i],
594 g_object_set_property ((GObject *) preset, props[i], &gvalue);
597 GST_WARNING_OBJECT (preset,
598 "deserialization of value '%s' for property '%s' failed", str,
601 g_value_unset (&gvalue);
611 GST_WARNING_OBJECT (preset, "no presets");
616 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
621 GST_INFO_OBJECT (preset, "no properties");
626 /* save the presets file. A copy of the existing presets file is stored in a
629 gst_preset_default_save_presets_file (GstPreset * preset)
632 const gchar *preset_path;
633 GError *error = NULL;
634 gchar *bak_file_name;
635 gboolean backup = TRUE;
639 preset_get_paths (preset, &preset_path, NULL, NULL);
641 /* get the presets from the type */
642 if (!(presets = preset_get_keyfile (preset)))
645 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
647 /* create backup if possible */
648 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
649 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
650 if (g_unlink (bak_file_name)) {
652 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
657 if (g_rename (preset_path, bak_file_name)) {
658 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
662 g_free (bak_file_name);
664 /* update gstreamer version */
665 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
668 /* get new contents, wee need this to save it */
669 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
673 if (!g_file_set_contents (preset_path, data, data_size, &error))
683 GST_WARNING_OBJECT (preset,
684 "no presets, trying to unlink possibly existing preset file: '%s'",
686 g_unlink (preset_path);
691 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
693 g_error_free (error);
699 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
700 preset_path, error->message);
701 g_error_free (error);
707 /* save the preset with the given name */
709 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
714 GObjectClass *gclass;
715 gboolean is_child_proxy;
717 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
719 /* get the presets from the type */
720 if (!(presets = preset_get_keyfile (preset)))
723 /* take copies of current gobject properties from preset */
724 if (!(props = gst_preset_get_property_names (preset)))
727 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
728 is_child_proxy = GST_IS_CHILD_PROXY (preset);
730 /* loop over the object properties and store the property value in the
732 for (i = 0; props[i]; i++) {
733 GValue gvalue = { 0, };
735 GParamSpec *property = NULL;
737 if (is_child_proxy) {
738 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
741 property = g_object_class_find_property (gclass, props[i]);
744 /* the element said it supported the property but then it does not have
745 * that property. This should not happen. */
746 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
750 g_value_init (&gvalue, property->value_type);
751 if (is_child_proxy) {
752 gst_child_proxy_get_property ((GstChildProxy *) preset, props[i],
755 g_object_get_property ((GObject *) preset, props[i], &gvalue);
758 if ((str = gst_value_serialize (&gvalue))) {
759 g_key_file_set_string (presets, name, props[i], (gpointer) str);
762 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
765 g_value_unset (&gvalue);
767 GST_INFO_OBJECT (preset, " saved");
770 /* save updated version */
771 return gst_preset_default_save_presets_file (preset);
776 GST_WARNING_OBJECT (preset, "no presets");
781 GST_INFO_OBJECT (preset, "no properties");
786 /* copies all keys and comments from one group to another, deleting the old
789 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
790 const gchar * new_name)
797 /* get the presets from the type */
798 if (!(presets = preset_get_keyfile (preset)))
801 if (!g_key_file_has_group (presets, old_name))
804 /* copy group comment if there is any */
805 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
806 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
810 /* get all keys from the old group and copy them in the new group */
811 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
812 for (i = 0; i < num_keys; i++) {
813 /* copy key comment if there is any */
814 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
815 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
819 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
820 g_key_file_set_value (presets, new_name, keys[i], str);
825 /* remove old group */
826 g_key_file_remove_group (presets, old_name, NULL);
828 /* save updated version */
829 return gst_preset_default_save_presets_file (preset);
834 GST_WARNING_OBJECT (preset, "no presets");
839 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
844 /* delete a group from the keyfile */
846 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
850 /* get the presets from the type */
851 if (!(presets = preset_get_keyfile (preset)))
855 if (!g_key_file_has_group (presets, name))
858 /* remove the group */
859 g_key_file_remove_group (presets, name, NULL);
861 /* save updated version */
862 return gst_preset_default_save_presets_file (preset);
867 GST_WARNING_OBJECT (preset, "no presets");
872 GST_WARNING_OBJECT (preset, "no preset named %s", name);
878 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
879 const gchar * tag, const gchar * value)
884 /* get the presets from the type */
885 if (!(presets = preset_get_keyfile (preset)))
888 key = g_strdup_printf ("_meta/%s", tag);
889 if (value && *value) {
890 g_key_file_set_value (presets, name, key, value);
892 g_key_file_remove_key (presets, name, key, NULL);
896 /* save updated keyfile */
897 return gst_preset_default_save_presets_file (preset);
902 GST_WARNING_OBJECT (preset, "no presets");
907 /* the caller must free @value after usage */
909 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
910 const gchar * tag, gchar ** value)
915 /* get the presets from the type */
916 if (!(presets = preset_get_keyfile (preset)))
919 key = g_strdup_printf ("_meta/%s", tag);
920 *value = g_key_file_get_value (presets, name, key, NULL);
928 GST_WARNING_OBJECT (preset, "no presets");
937 * gst_preset_get_preset_names:
938 * @preset: a #GObject that implements #GstPreset
940 * Get a copy of preset names as a NULL terminated string array.
942 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
943 * list with names, ue g_strfreev() after usage.
946 gst_preset_get_preset_names (GstPreset * preset)
948 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
950 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
954 * gst_preset_get_property_names:
955 * @preset: a #GObject that implements #GstPreset
957 * Get a the names of the GObject properties that can be used for presets.
959 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
960 * array of property names which should be freed with g_strfreev() after use.
963 gst_preset_get_property_names (GstPreset * preset)
965 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
967 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
971 * gst_preset_load_preset:
972 * @preset: a #GObject that implements #GstPreset
973 * @name: preset name to load
975 * Load the given preset.
977 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
980 gst_preset_load_preset (GstPreset * preset, const gchar * name)
982 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
983 g_return_val_if_fail (name, FALSE);
985 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
989 * gst_preset_save_preset:
990 * @preset: a #GObject that implements #GstPreset
991 * @name: preset name to save
993 * Save the current object settings as a preset under the given name. If there
994 * is already a preset by this @name it will be overwritten.
996 * Returns: %TRUE for success, %FALSE
999 gst_preset_save_preset (GstPreset * preset, const gchar * name)
1001 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1002 g_return_val_if_fail (name, FALSE);
1004 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
1008 * gst_preset_rename_preset:
1009 * @preset: a #GObject that implements #GstPreset
1010 * @old_name: current preset name
1011 * @new_name: new preset name
1013 * Renames a preset. If there is already a preset by the @new_name it will be
1016 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1019 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1020 const gchar * new_name)
1022 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1023 g_return_val_if_fail (old_name, FALSE);
1024 g_return_val_if_fail (new_name, FALSE);
1026 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1031 * gst_preset_delete_preset:
1032 * @preset: a #GObject that implements #GstPreset
1033 * @name: preset name to remove
1035 * Delete the given preset.
1037 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1040 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1042 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1043 g_return_val_if_fail (name, FALSE);
1045 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1049 * gst_preset_set_meta:
1050 * @preset: a #GObject that implements #GstPreset
1051 * @name: preset name
1052 * @tag: meta data item name
1055 * Sets a new @value for an existing meta data item or adds a new item. Meta
1056 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1057 * @value will unset an existing value.
1059 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1062 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1063 const gchar * value)
1065 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1066 g_return_val_if_fail (name, FALSE);
1067 g_return_val_if_fail (tag, FALSE);
1069 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1073 * gst_preset_get_meta:
1074 * @preset: a #GObject that implements #GstPreset
1075 * @name: preset name
1076 * @tag: meta data item name
1077 * @value: (out callee-allocates): value
1079 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1080 * something like e.g. "comment". Returned values need to be released when done.
1082 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1083 * or no value for the given @tag
1086 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1089 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1090 g_return_val_if_fail (name, FALSE);
1091 g_return_val_if_fail (tag, FALSE);
1092 g_return_val_if_fail (value, FALSE);
1094 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1098 * gst_preset_set_app_dir:
1099 * @app_dir: the application specific preset dir
1101 * Sets an extra directory as an absolute path that should be considered when
1102 * looking for presets. Any presets in the application dir will shadow the
1105 * Returns: %TRUE for success, %FALSE if the dir already has been set
1108 gst_preset_set_app_dir (const gchar * app_dir)
1110 g_return_val_if_fail (app_dir, FALSE);
1112 if (!preset_app_dir) {
1113 preset_app_dir = g_strdup (app_dir);
1120 * gst_preset_get_app_dir:
1122 * Gets the directory for application specific presets if set by the
1125 * Returns: the directory or %NULL, don't free or modify the string
1128 gst_preset_get_app_dir (void)
1130 return preset_app_dir;
1133 /* class internals */
1136 gst_preset_class_init (GstPresetInterface * iface)
1138 iface->get_preset_names = gst_preset_default_get_preset_names;
1139 iface->get_property_names = gst_preset_default_get_property_names;
1141 iface->load_preset = gst_preset_default_load_preset;
1142 iface->save_preset = gst_preset_default_save_preset;
1143 iface->rename_preset = gst_preset_default_rename_preset;
1144 iface->delete_preset = gst_preset_default_delete_preset;
1146 iface->set_meta = gst_preset_default_set_meta;
1147 iface->get_meta = gst_preset_default_get_meta;
1151 gst_preset_base_init (gpointer g_class)
1153 static gboolean initialized = FALSE;
1156 /* init default implementation */
1157 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1158 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1160 /* create quarks for use with g_type_{g,s}et_qdata() */
1161 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1162 preset_user_path_quark =
1163 g_quark_from_static_string ("GstPreset::user_path");
1164 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1165 preset_system_path_quark =
1166 g_quark_from_static_string ("GstPreset::system_path");
1169 property_list_quark = g_quark_from_static_string ("GstPreset::properties");
1171 /* create interface properties, each element would need to override this
1172 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1173 * and in _set_property() do
1174 * case PROP_PRESET_NAME: {
1175 * gchar *name = g_value_get_string (value);
1177 * gst_preset_load_preset(preset, name);
1180 g_object_interface_install_property (g_class,
1181 g_param_spec_string ("preset-name",
1182 "preset-name property",
1183 "load given preset", NULL, G_PARAM_WRITABLE));
1191 gst_preset_get_type (void)
1193 static volatile gsize type = 0;
1195 if (g_once_init_enter (&type)) {
1197 const GTypeInfo info = {
1198 sizeof (GstPresetInterface),
1199 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1200 NULL, /* base_finalize */
1201 (GClassInitFunc) gst_preset_class_init, /* class_init */
1202 NULL, /* class_finalize */
1203 NULL, /* class_data */
1205 0, /* n_preallocs */
1206 NULL /* instance_init */
1208 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1209 g_once_init_leave (&type, _type);