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.
40 * - we need to avoid two instances writing the preset file
41 * -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
43 * -> better save the new file to a tempfile and then rename?
44 * - we like to know when any other instance makes changes to the keyfile
45 * - then ui can be updated
46 * - and we make sure that we don't lose edits
47 * -> its the same problem actually, once for inside a process, once system-
49 * - can we use a lock inside a names shared memory segment?
51 * - need to add support for GstChildProxy
52 * we can do this in a next iteration, the format is flexible enough
53 * http://www.buzztard.org/index.php/Preset_handling_interface
55 * - should there be a 'preset-list' property to get the preset list
56 * (and to connect a notify:: to to listen for changes)
57 * we could use gnome_vfs_monitor_add() to monitor the user preset_file.
59 * - should there be a 'preset-name' property so that we can set a preset via
60 * gst-launch, or should we handle this with special syntax in gst-launch:
61 * gst-launch element preset:<preset-name> property=value ...
62 * - this would alloow to hanve preset-bundles too (a preset on bins that
63 * specifies presets for children
65 * - GstChildProxy suport
66 * - if we stick with GParamSpec **_list_properties()
67 * we need to use g_param_spec_set_qdata() to specify the instance on each GParamSpec
68 * OBJECT_LOCK(obj); // ChildProxy needs GstIterator support
69 * num=gst_child_proxy_get_children_count(obj);
70 * for(i=0;i<num;i++) {
71 * child=gst_child_proxy_get_child_by_index(obj,i);
73 * g_object_class_list_properties(child,&num);
75 * // g_param_spec_set_qdata(prop, quark, (gpointer)child);
78 * // children have to implement preset-iface too tag the returned GParamSpec* with the owner
79 * props=gst_preset_list_properties(child);
80 * // add props to result
86 #include "gst_private.h"
88 #include "gstpreset.h"
95 #include <glib/gstdio.h>
97 #define GST_CAT_DEFAULT preset_debug
98 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
100 /* defines for keyfile usage, this group contains the element type name and
101 * version these presets belong to. */
102 #define PRESET_HEADER "_presets_"
104 /* keys of the preset header section */
105 #define PRESET_HEADER_ELEMENT_NAME "element-name"
106 #define PRESET_HEADER_VERSION "version"
108 static GQuark preset_user_path_quark = 0;
109 static GQuark preset_system_path_quark = 0;
110 static GQuark preset_quark = 0;
112 /*static GQuark property_list_quark = 0;*/
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: location for path or %NULL
122 * @preset_system_path: location for path or %NULL
124 * Fetch the preset_path for user local and system wide settings. Don't free
127 * Returns: %FALSE if no paths could be found.
130 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
131 const gchar ** preset_system_path)
133 GType type = G_TYPE_FROM_INSTANCE (preset);
135 const gchar *element_name;
137 /* we use the element name when we must contruct the paths */
138 element_name = G_OBJECT_TYPE_NAME (preset);
139 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
141 if (preset_user_path) {
142 /* preset user path requested, see if we have it cached in the qdata */
143 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
146 /* user presets go in user's XDG data directory. */
147 preset_dir = g_build_filename (g_get_user_data_dir (),
148 "gstreamer-" GST_MAJORMINOR, "presets", NULL);
149 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
151 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
153 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
155 g_mkdir_with_parents (preset_dir, 0755);
158 /* cache the preset path to the type */
159 g_type_set_qdata (type, preset_user_path_quark, preset_path);
161 *preset_user_path = preset_path;
164 if (preset_system_path) {
165 /* preset system path requested, see if we have it cached in the qdata */
166 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
169 /* system presets in '$GST_DATADIR/gstreamer-0.10/presets/GstAudioPanorama.prs' */
170 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
172 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
173 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
174 preset_dir, element_name);
175 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
177 g_mkdir_with_parents (preset_dir, 0755);
180 /* cache the preset path to the type */
181 g_type_set_qdata (type, preset_system_path_quark, preset_path);
183 *preset_system_path = preset_path;
189 preset_skip_property (GParamSpec * property)
191 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
192 (property->flags & G_PARAM_CONSTRUCT_ONLY))
194 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
198 /* caller must free @preset_version after use */
200 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
201 gchar ** preset_version)
204 GError *error = NULL;
206 const gchar *element_name;
209 in = g_key_file_new ();
211 res = g_key_file_load_from_file (in, preset_path,
212 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
213 if (!res || error != NULL)
216 /* element type name and preset name must match or we are dealing with a wrong
218 element_name = G_OBJECT_TYPE_NAME (preset);
220 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
223 if (!name || strcmp (name, element_name))
228 /* get the version now so that the caller can check it */
231 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
238 GST_WARNING_OBJECT (preset, "Unable to read preset file %s: %s",
239 preset_path, error->message);
240 g_error_free (error);
241 g_key_file_free (in);
246 GST_WARNING_OBJECT (preset,
247 "Wrong element name in preset file %s. Expected %s, got %s",
248 preset_path, element_name, GST_STR_NULL (name));
250 g_key_file_free (in);
256 preset_parse_version (const gchar * str_version)
258 guint major, minor, micro, nano;
261 major = minor = micro = nano = 0;
263 /* parse version (e.g. 0.10.15.1) to guint64 */
264 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
265 /* make sure we have atleast "major.minor" */
269 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
270 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
273 return G_GUINT64_CONSTANT (0);
277 preset_merge (GKeyFile * system, GKeyFile * user)
280 gchar **groups, **keys;
281 gsize i, j, num_groups, num_keys;
283 /* copy file comment if there is any */
284 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
285 g_key_file_set_comment (system, NULL, NULL, str, NULL);
289 /* get groups in user and copy into system */
290 groups = g_key_file_get_groups (user, &num_groups);
291 for (i = 0; i < num_groups; i++) {
292 /* copy group comment if there is any */
293 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
294 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
298 /* ignore private groups */
299 if (groups[i][0] == '_')
302 /* if group already exists in system, remove and re-add keys from user */
303 if (g_key_file_has_group (system, groups[i])) {
304 g_key_file_remove_group (system, groups[i], NULL);
307 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
308 for (j = 0; j < num_keys; j++) {
309 /* copy key comment if there is any */
310 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
311 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
314 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
315 g_key_file_set_value (system, groups[i], keys[j], str);
323 /* reads the user and system presets files and merges them together. This
324 * function caches the GKeyFile on the element type. If there is no existing
325 * preset file, a new in-memory GKeyFile will be created. */
327 preset_get_keyfile (GstPreset * preset)
330 GType type = G_TYPE_FROM_INSTANCE (preset);
332 /* first see if the have a cached version for the type */
333 if (!(presets = g_type_get_qdata (type, preset_quark))) {
334 const gchar *preset_user_path, *preset_system_path;
335 gchar *str_version_user = NULL, *str_version_system = NULL;
336 gboolean updated_from_system = FALSE;
337 GKeyFile *in_user, *in_system;
339 preset_get_paths (preset, &preset_user_path, &preset_system_path);
341 /* try to load the user and system presets, we do this to get the versions
343 in_user = preset_open_and_parse_header (preset, preset_user_path,
345 in_system = preset_open_and_parse_header (preset, preset_system_path,
346 &str_version_system);
348 /* compare version to check for merge */
350 /* keep system presets if there is no user preset or when the system
351 * version is higher than the user version. */
354 } else if (preset_parse_version (str_version_system) >
355 preset_parse_version (str_version_user)) {
357 updated_from_system = TRUE;
361 if (updated_from_system) {
362 /* merge user on top of system presets */
363 preset_merge (presets, in_user);
364 g_key_file_free (in_user);
366 /* keep user presets */
370 if (!in_user && !in_system) {
371 /* we did not load a user or system presets file, create a new one */
372 presets = g_key_file_new ();
373 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
374 G_OBJECT_TYPE_NAME (preset));
377 g_free (str_version_user);
378 g_free (str_version_system);
380 /* attach the preset to the type */
381 g_type_set_qdata (type, preset_quark, (gpointer) presets);
383 if (updated_from_system) {
384 gst_preset_default_save_presets_file (preset);
390 /* get a list of all supported preset names for an element */
392 gst_preset_default_get_preset_names (GstPreset * preset)
398 /* get the presets from the type */
399 if (!(presets = preset_get_keyfile (preset)))
402 /* get the groups, which are also the preset names */
403 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
406 /* remove all private group names starting with '_' from the array */
407 for (i = 0; i < num_groups; i++) {
408 if (groups[i][0] == '_') {
409 /* free private group */
411 /* move last element of list down */
413 /* move last element into removed element */
414 groups[i] = groups[num_groups];
415 groups[num_groups] = NULL;
418 /* sort the array now */
419 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
420 (GCompareDataFunc) strcmp, NULL);
427 GST_WARNING_OBJECT (preset, "Could not load presets");
432 GST_WARNING_OBJECT (preset, "Could not find preset groups");
437 /* get a list of all property names that are used for presets */
439 gst_preset_default_get_property_names (GstPreset * preset)
443 GObjectClass *gclass;
446 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
448 /* get a list of normal properties.
449 * FIXME, change this for childproxy support. */
450 props = g_object_class_list_properties (gclass, &n_props);
454 /* allocate array big enough to hold the worst case, including a terminating
456 result = g_new (gchar *, n_props + 1);
458 /* now filter out the properties that we can use for presets */
459 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
460 for (i = j = 0; i < n_props; i++) {
461 if (preset_skip_property (props[i]))
464 /* copy and increment out pointer */
465 result[j++] = g_strdup (props[i]->name);
475 GST_INFO_OBJECT (preset, "object has no properties");
480 /* load the presets of @name for the instance @preset. Returns %FALSE if something
483 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
488 GObjectClass *gclass;
490 /* get the presets from the type */
491 if (!(presets = preset_get_keyfile (preset)))
494 /* get the preset name */
495 if (!g_key_file_has_group (presets, name))
498 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
500 /* get the properties that we can configure in this element */
501 if (!(props = gst_preset_get_property_names (preset)))
504 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
506 /* for each of the property names, find the preset parameter and try to
507 * configure the property with its value */
508 for (i = 0; props[i]; i++) {
510 GValue gvalue = { 0, };
511 GParamSpec *property;
513 /* check if we have a settings for this element property */
514 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
515 /* the element has a property but the parameter is not in the keyfile */
516 GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
520 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
523 /* FIXME, change for childproxy to get the property and element. */
524 if (!(property = g_object_class_find_property (gclass, props[i]))) {
525 /* the parameter was in the keyfile, the element said it supported it but
526 * then the property was not found in the element. This should not happen. */
527 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
532 /* try to deserialize the property value from the keyfile and set it as
533 * the object property */
534 g_value_init (&gvalue, property->value_type);
535 if (gst_value_deserialize (&gvalue, str)) {
536 /* FIXME, change for childproxy support */
537 g_object_set_property (G_OBJECT (preset), props[i], &gvalue);
539 GST_WARNING_OBJECT (preset,
540 "deserialization of value '%s' for property '%s' failed", str,
543 g_value_unset (&gvalue);
553 GST_WARNING_OBJECT (preset, "no presets");
558 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
563 GST_INFO_OBJECT (preset, "no properties");
568 /* save the presets file. A copy of the existing presets file is stored in a
571 gst_preset_default_save_presets_file (GstPreset * preset)
574 const gchar *preset_path;
575 GError *error = NULL;
576 gchar *bak_file_name;
577 gboolean backup = TRUE;
581 preset_get_paths (preset, &preset_path, NULL);
583 /* get the presets from the type */
584 if (!(presets = preset_get_keyfile (preset)))
587 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
589 /* create backup if possible */
590 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
591 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
592 if (g_unlink (bak_file_name)) {
594 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
599 if (g_rename (preset_path, bak_file_name)) {
600 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
604 g_free (bak_file_name);
606 /* update gstreamer version */
607 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
610 /* get new contents, wee need this to save it */
611 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
615 if (!g_file_set_contents (preset_path, data, data_size, &error))
625 GST_WARNING_OBJECT (preset,
626 "no presets, trying to unlink possibly existing preset file: '%s'",
628 g_unlink (preset_path);
633 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
635 g_error_free (error);
641 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
642 preset_path, error->message);
643 g_error_free (error);
649 /* save the preset with the given name */
651 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
656 GObjectClass *gclass;
658 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
660 /* get the presets from the type */
661 if (!(presets = preset_get_keyfile (preset)))
664 /* take copies of current gobject properties from preset */
665 if (!(props = gst_preset_get_property_names (preset)))
668 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
670 /* loop over the object properties and store the property value in the
672 for (i = 0; props[i]; i++) {
673 GValue gvalue = { 0, };
675 GParamSpec *property;
677 /* FIXME, change for childproxy to get the property and element. */
678 if (!(property = g_object_class_find_property (gclass, props[i]))) {
679 /* the element said it supported the property but then it does not have
680 * that property. This should not happen. */
681 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
685 g_value_init (&gvalue, property->value_type);
686 /* FIXME, change for childproxy */
687 g_object_get_property (G_OBJECT (preset), props[i], &gvalue);
689 if ((str = gst_value_serialize (&gvalue))) {
690 g_key_file_set_string (presets, name, props[i], (gpointer) str);
693 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
696 g_value_unset (&gvalue);
698 GST_INFO_OBJECT (preset, " saved");
701 /* save updated version */
702 return gst_preset_default_save_presets_file (preset);
707 GST_WARNING_OBJECT (preset, "no presets");
712 GST_INFO_OBJECT (preset, "no properties");
717 /* copies all keys and comments from one group to another, deleting the old
720 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
721 const gchar * new_name)
728 /* get the presets from the type */
729 if (!(presets = preset_get_keyfile (preset)))
732 if (!g_key_file_has_group (presets, old_name))
735 /* copy group comment if there is any */
736 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
737 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
741 /* get all keys from the old group and copy them in the new group */
742 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
743 for (i = 0; i < num_keys; i++) {
744 /* copy key comment if there is any */
745 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
746 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
750 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
751 g_key_file_set_value (presets, new_name, keys[i], str);
756 /* remove old group */
757 g_key_file_remove_group (presets, old_name, NULL);
759 /* save updated version */
760 return gst_preset_default_save_presets_file (preset);
765 GST_WARNING_OBJECT (preset, "no presets");
770 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
775 /* delete a group from the keyfile */
777 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
781 /* get the presets from the type */
782 if (!(presets = preset_get_keyfile (preset)))
786 if (!g_key_file_has_group (presets, name))
789 /* remove the group */
790 g_key_file_remove_group (presets, name, NULL);
792 /* save updated version */
793 return gst_preset_default_save_presets_file (preset);
798 GST_WARNING_OBJECT (preset, "no presets");
803 GST_WARNING_OBJECT (preset, "no preset named %s", name);
809 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
810 const gchar * tag, const gchar * value)
815 /* get the presets from the type */
816 if (!(presets = preset_get_keyfile (preset)))
819 key = g_strdup_printf ("_meta/%s", tag);
820 if (value && *value) {
821 g_key_file_set_value (presets, name, key, value);
823 g_key_file_remove_key (presets, name, key, NULL);
827 /* save updated keyfile */
828 return gst_preset_default_save_presets_file (preset);
833 GST_WARNING_OBJECT (preset, "no presets");
838 /* the caller must free @value after usage */
840 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
841 const gchar * tag, gchar ** value)
846 /* get the presets from the type */
847 if (!(presets = preset_get_keyfile (preset)))
850 key = g_strdup_printf ("_meta/%s", tag);
851 *value = g_key_file_get_value (presets, name, key, NULL);
859 GST_WARNING_OBJECT (preset, "no presets");
868 * gst_preset_get_preset_names:
869 * @preset: a #GObject that implements #GstPreset
871 * Get a copy of preset names as a NULL terminated string array.
873 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
874 * list with names, ue g_strfreev() after usage.
879 gst_preset_get_preset_names (GstPreset * preset)
881 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
883 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
887 * gst_preset_get_property_names:
888 * @preset: a #GObject that implements #GstPreset
890 * Get a the names of the GObject properties that can be used for presets.
892 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
893 * array of property names which should be freed with g_strfreev() after use.
898 gst_preset_get_property_names (GstPreset * preset)
900 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
902 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
906 * gst_preset_load_preset:
907 * @preset: a #GObject that implements #GstPreset
908 * @name: preset name to load
910 * Load the given preset.
912 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
917 gst_preset_load_preset (GstPreset * preset, const gchar * name)
919 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
920 g_return_val_if_fail (name, FALSE);
922 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
926 * gst_preset_save_preset:
927 * @preset: a #GObject that implements #GstPreset
928 * @name: preset name to save
930 * Save the current object settings as a preset under the given name. If there
931 * is already a preset by this @name it will be overwritten.
933 * Returns: %TRUE for success, %FALSE
938 gst_preset_save_preset (GstPreset * preset, const gchar * name)
940 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
941 g_return_val_if_fail (name, FALSE);
943 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
947 * gst_preset_rename_preset:
948 * @preset: a #GObject that implements #GstPreset
949 * @old_name: current preset name
950 * @new_name: new preset name
952 * Renames a preset. If there is already a preset by the @new_name it will be
955 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
960 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
961 const gchar * new_name)
963 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
964 g_return_val_if_fail (old_name, FALSE);
965 g_return_val_if_fail (new_name, FALSE);
967 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
972 * gst_preset_delete_preset:
973 * @preset: a #GObject that implements #GstPreset
974 * @name: preset name to remove
976 * Delete the given preset.
978 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
983 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
985 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
986 g_return_val_if_fail (name, FALSE);
988 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
992 * gst_preset_set_meta:
993 * @preset: a #GObject that implements #GstPreset
995 * @tag: meta data item name
998 * Sets a new @value for an existing meta data item or adds a new item. Meta
999 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1000 * @value will unset an existing value.
1002 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1007 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1008 const gchar * value)
1010 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1011 g_return_val_if_fail (name, FALSE);
1012 g_return_val_if_fail (tag, FALSE);
1014 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1018 * gst_preset_get_meta:
1019 * @preset: a #GObject that implements #GstPreset
1020 * @name: preset name
1021 * @tag: meta data item name
1022 * @value: (out callee-allocates): value
1024 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1025 * something like e.g. "comment". Returned values need to be released when done.
1027 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1028 * or no value for the given @tag
1033 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1036 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1037 g_return_val_if_fail (name, FALSE);
1038 g_return_val_if_fail (tag, FALSE);
1039 g_return_val_if_fail (value, FALSE);
1041 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1044 /* class internals */
1047 gst_preset_class_init (GstPresetInterface * iface)
1049 iface->get_preset_names = gst_preset_default_get_preset_names;
1050 iface->get_property_names = gst_preset_default_get_property_names;
1052 iface->load_preset = gst_preset_default_load_preset;
1053 iface->save_preset = gst_preset_default_save_preset;
1054 iface->rename_preset = gst_preset_default_rename_preset;
1055 iface->delete_preset = gst_preset_default_delete_preset;
1057 iface->set_meta = gst_preset_default_set_meta;
1058 iface->get_meta = gst_preset_default_get_meta;
1062 gst_preset_base_init (gpointer g_class)
1064 static gboolean initialized = FALSE;
1067 /* init default implementation */
1068 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1069 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1071 /* create quarks for use with g_type_{g,s}et_qdata() */
1072 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1073 preset_user_path_quark =
1074 g_quark_from_static_string ("GstPreset::user_path");
1075 preset_system_path_quark =
1076 g_quark_from_static_string ("GstPreset::system_path");
1079 property_list_quark = g_quark_from_static_string ("GstPreset::properties");
1081 /* create interface properties, each element would need to override this
1082 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1083 * and in _set_property() do
1084 * case PROP_PRESET_NAME: {
1085 * gchar *name = g_value_get_string (value);
1087 * gst_preset_load_preset(preset, name);
1090 g_object_interface_install_property (g_class,
1091 g_param_spec_string ("preset-name",
1092 "preset-name property",
1093 "load given preset", NULL, G_PARAM_WRITABLE));
1101 gst_preset_get_type (void)
1103 static volatile gsize type = 0;
1105 if (g_once_init_enter (&type)) {
1107 const GTypeInfo info = {
1108 sizeof (GstPresetInterface),
1109 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1110 NULL, /* base_finalize */
1111 (GClassInitFunc) gst_preset_class_init, /* class_init */
1112 NULL, /* class_finalize */
1113 NULL, /* class_data */
1115 0, /* n_preallocs */
1116 NULL /* instance_init */
1118 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1119 g_once_init_leave (&type, _type);