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 * - need to add support for GstChildProxy
59 * we can do this in a next iteration, the format is flexible enough
60 * http://www.buzztard.org/index.php/Preset_handling_interface
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 alloow to hanve preset-bundles too (a preset on bins that
70 * specifies presets for children
72 * - GstChildProxy suport
73 * - if we stick with GParamSpec **_list_properties()
74 * we need to use g_param_spec_set_qdata() to specify the instance on each GParamSpec
75 * OBJECT_LOCK(obj); // ChildProxy needs GstIterator support
76 * num=gst_child_proxy_get_children_count(obj);
77 * for(i=0;i<num;i++) {
78 * child=gst_child_proxy_get_child_by_index(obj,i);
80 * g_object_class_list_properties(child,&num);
82 * // g_param_spec_set_qdata(prop, quark, (gpointer)child);
85 * // children have to implement preset-iface too tag the returned GParamSpec* with the owner
86 * props=gst_preset_list_properties(child);
87 * // add props to result
93 #include "gst_private.h"
95 #include "gstpreset.h"
102 #include <glib/gstdio.h>
104 #define GST_CAT_DEFAULT preset_debug
105 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
107 /* defines for keyfile usage, this group contains the element type name and
108 * version these presets belong to. */
109 #define PRESET_HEADER "_presets_"
111 /* keys of the preset header section */
112 #define PRESET_HEADER_ELEMENT_NAME "element-name"
113 #define PRESET_HEADER_VERSION "version"
115 static GQuark preset_user_path_quark = 0;
116 static GQuark preset_app_path_quark = 0;
117 static GQuark preset_system_path_quark = 0;
118 static GQuark preset_quark = 0;
120 /*static GQuark property_list_quark = 0;*/
122 /* the application can set a custom path that is checked in addition to standart
123 * system and user dirs. This helps to develop new presets first local to the
126 static gchar *preset_app_dir = NULL;
128 /* default iface implementation */
130 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
134 * @preset: a #GObject that implements #GstPreset
135 * @preset_user_path: location for path or %NULL
136 * @preset_app_path: location for path or %NULL
137 * @preset_system_path: location for path or %NULL
139 * Fetch the preset_path for user local, application specific and system wide
140 * settings. Don't free after use.
142 * Returns: %FALSE if no paths could be found.
145 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
146 const gchar ** preset_app_path, const gchar ** preset_system_path)
148 GType type = G_TYPE_FROM_INSTANCE (preset);
150 const gchar *element_name;
152 /* we use the element name when we must contruct the paths */
153 element_name = G_OBJECT_TYPE_NAME (preset);
154 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
156 if (preset_user_path) {
157 /* preset user path requested, see if we have it cached in the qdata */
158 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
161 /* user presets go in user's XDG data directory. */
162 preset_dir = g_build_filename (g_get_user_data_dir (),
163 "gstreamer-" GST_MAJORMINOR, "presets", NULL);
164 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
166 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
168 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
170 g_mkdir_with_parents (preset_dir, 0755);
173 /* cache the preset path to the type */
174 g_type_set_qdata (type, preset_user_path_quark, preset_path);
176 *preset_user_path = preset_path;
179 if (preset_app_path) {
180 if (preset_app_dir) {
181 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
182 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
183 preset_app_dir, element_name);
184 GST_INFO_OBJECT (preset, "application_preset_path: '%s'", preset_path);
186 /* cache the preset path to the type */
187 g_type_set_qdata (type, preset_app_path_quark, preset_path);
189 *preset_app_path = preset_path;
191 *preset_app_path = NULL;
195 if (preset_system_path) {
196 /* preset system path requested, see if we have it cached in the qdata */
197 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
200 /* system presets in '$GST_DATADIR/gstreamer-0.10/presets/GstAudioPanorama.prs' */
201 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
203 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
204 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
205 preset_dir, element_name);
206 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
208 g_mkdir_with_parents (preset_dir, 0755);
211 /* cache the preset path to the type */
212 g_type_set_qdata (type, preset_system_path_quark, preset_path);
214 *preset_system_path = preset_path;
220 preset_skip_property (GParamSpec * property)
222 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
223 (property->flags & G_PARAM_CONSTRUCT_ONLY))
225 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
230 preset_parse_version (const gchar * str_version)
232 guint major, minor, micro, nano;
235 major = minor = micro = nano = 0;
237 /* parse version (e.g. 0.10.15.1) to guint64 */
238 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
239 /* make sure we have atleast "major.minor" */
243 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
244 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
247 return G_GUINT64_CONSTANT (0);
251 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
252 guint64 * preset_version)
255 GError *error = NULL;
257 const gchar *element_name;
260 in = g_key_file_new ();
262 res = g_key_file_load_from_file (in, preset_path,
263 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
264 if (!res || error != NULL)
267 /* element type name and preset name must match or we are dealing with a wrong
269 element_name = G_OBJECT_TYPE_NAME (preset);
271 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
274 if (!name || strcmp (name, element_name))
279 /* get the version now so that the caller can check it */
280 if (preset_version) {
282 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
283 *preset_version = preset_parse_version (str);
292 GST_WARNING_OBJECT (preset, "Unable to read preset file %s: %s",
293 preset_path, error->message);
294 g_error_free (error);
295 g_key_file_free (in);
300 GST_WARNING_OBJECT (preset,
301 "Wrong element name in preset file %s. Expected %s, got %s",
302 preset_path, element_name, GST_STR_NULL (name));
304 g_key_file_free (in);
310 preset_merge (GKeyFile * system, GKeyFile * user)
313 gchar **groups, **keys;
314 gsize i, j, num_groups, num_keys;
316 /* copy file comment if there is any */
317 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
318 g_key_file_set_comment (system, NULL, NULL, str, NULL);
322 /* get groups in user and copy into system */
323 groups = g_key_file_get_groups (user, &num_groups);
324 for (i = 0; i < num_groups; i++) {
325 /* copy group comment if there is any */
326 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
327 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
331 /* ignore private groups */
332 if (groups[i][0] == '_')
335 /* if group already exists in system, remove and re-add keys from user */
336 if (g_key_file_has_group (system, groups[i])) {
337 g_key_file_remove_group (system, groups[i], NULL);
340 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
341 for (j = 0; j < num_keys; j++) {
342 /* copy key comment if there is any */
343 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
344 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
347 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
348 g_key_file_set_value (system, groups[i], keys[j], str);
356 /* reads the user and system presets files and merges them together. This
357 * function caches the GKeyFile on the element type. If there is no existing
358 * preset file, a new in-memory GKeyFile will be created. */
360 preset_get_keyfile (GstPreset * preset)
363 GType type = G_TYPE_FROM_INSTANCE (preset);
365 /* first see if the have a cached version for the type */
366 if (!(presets = g_type_get_qdata (type, preset_quark))) {
367 const gchar *preset_user_path, *preset_app_path, *preset_system_path;
368 guint64 version_system = G_GUINT64_CONSTANT (0);
369 guint64 version_app = G_GUINT64_CONSTANT (0);
370 guint64 version_user = G_GUINT64_CONSTANT (0);
371 guint64 version = G_GUINT64_CONSTANT (0);
372 gboolean merged = FALSE;
373 GKeyFile *in_user, *in_app = NULL, *in_system;
375 preset_get_paths (preset, &preset_user_path, &preset_app_path,
376 &preset_system_path);
378 /* try to load the user, app and system presets, we do this to get the
379 * versions of all files. */
380 in_user = preset_open_and_parse_header (preset, preset_user_path,
382 if (preset_app_path) {
383 in_app = preset_open_and_parse_header (preset, preset_app_path,
386 in_system = preset_open_and_parse_header (preset, preset_system_path,
389 /* compare version to check for merge */
392 version = version_system;
395 /* if system version is higher, merge */
396 if (version > version_app) {
397 preset_merge (presets, in_app);
398 g_key_file_free (in_app);
401 g_key_file_free (presets);
403 version = version_system;
407 /* if system or app version is higher, merge */
408 if (version > version_user) {
409 preset_merge (presets, in_user);
410 g_key_file_free (in_user);
414 g_key_file_free (presets);
416 version = version_user;
421 /* we did not load a user, app or system presets file, create a new one */
422 presets = g_key_file_new ();
423 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
424 G_OBJECT_TYPE_NAME (preset));
427 /* attach the preset to the type */
428 g_type_set_qdata (type, preset_quark, (gpointer) presets);
431 gst_preset_default_save_presets_file (preset);
437 /* get a list of all supported preset names for an element */
439 gst_preset_default_get_preset_names (GstPreset * preset)
445 /* get the presets from the type */
446 if (!(presets = preset_get_keyfile (preset)))
449 /* get the groups, which are also the preset names */
450 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
453 /* remove all private group names starting with '_' from the array */
454 for (i = 0; i < num_groups; i++) {
455 if (groups[i][0] == '_') {
456 /* free private group */
458 /* move last element of list down */
460 /* move last element into removed element */
461 groups[i] = groups[num_groups];
462 groups[num_groups] = NULL;
465 /* sort the array now */
466 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
467 (GCompareDataFunc) strcmp, NULL);
474 GST_WARNING_OBJECT (preset, "Could not load presets");
479 GST_WARNING_OBJECT (preset, "Could not find preset groups");
484 /* get a list of all property names that are used for presets */
486 gst_preset_default_get_property_names (GstPreset * preset)
490 GObjectClass *gclass;
493 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
495 /* get a list of normal properties.
496 * FIXME, change this for childproxy support. */
497 props = g_object_class_list_properties (gclass, &n_props);
501 /* allocate array big enough to hold the worst case, including a terminating
503 result = g_new (gchar *, 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 = j = 0; i < n_props; i++) {
508 if (preset_skip_property (props[i]))
511 /* copy and increment out pointer */
512 result[j++] = g_strdup (props[i]->name);
522 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;
537 /* get the presets from the type */
538 if (!(presets = preset_get_keyfile (preset)))
541 /* get the preset name */
542 if (!g_key_file_has_group (presets, name))
545 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
547 /* get the properties that we can configure in this element */
548 if (!(props = gst_preset_get_property_names (preset)))
551 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
553 /* for each of the property names, find the preset parameter and try to
554 * configure the property with its value */
555 for (i = 0; props[i]; i++) {
557 GValue gvalue = { 0, };
558 GParamSpec *property;
560 /* check if we have a settings for this element property */
561 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
562 /* the element has a property but the parameter is not in the keyfile */
563 GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
567 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
570 /* FIXME, change for childproxy to get the property and element. */
571 if (!(property = g_object_class_find_property (gclass, props[i]))) {
572 /* the parameter was in the keyfile, the element said it supported it but
573 * then the property was not found in the element. This should not happen. */
574 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
579 /* try to deserialize the property value from the keyfile and set it as
580 * the object property */
581 g_value_init (&gvalue, property->value_type);
582 if (gst_value_deserialize (&gvalue, str)) {
583 /* FIXME, change for childproxy support */
584 g_object_set_property (G_OBJECT (preset), props[i], &gvalue);
586 GST_WARNING_OBJECT (preset,
587 "deserialization of value '%s' for property '%s' failed", str,
590 g_value_unset (&gvalue);
600 GST_WARNING_OBJECT (preset, "no presets");
605 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
610 GST_INFO_OBJECT (preset, "no properties");
615 /* save the presets file. A copy of the existing presets file is stored in a
618 gst_preset_default_save_presets_file (GstPreset * preset)
621 const gchar *preset_path;
622 GError *error = NULL;
623 gchar *bak_file_name;
624 gboolean backup = TRUE;
628 preset_get_paths (preset, &preset_path, NULL, NULL);
630 /* get the presets from the type */
631 if (!(presets = preset_get_keyfile (preset)))
634 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
636 /* create backup if possible */
637 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
638 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
639 if (g_unlink (bak_file_name)) {
641 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
646 if (g_rename (preset_path, bak_file_name)) {
647 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
651 g_free (bak_file_name);
653 /* update gstreamer version */
654 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
657 /* get new contents, wee need this to save it */
658 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
662 if (!g_file_set_contents (preset_path, data, data_size, &error))
672 GST_WARNING_OBJECT (preset,
673 "no presets, trying to unlink possibly existing preset file: '%s'",
675 g_unlink (preset_path);
680 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
682 g_error_free (error);
688 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
689 preset_path, error->message);
690 g_error_free (error);
696 /* save the preset with the given name */
698 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
703 GObjectClass *gclass;
705 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
707 /* get the presets from the type */
708 if (!(presets = preset_get_keyfile (preset)))
711 /* take copies of current gobject properties from preset */
712 if (!(props = gst_preset_get_property_names (preset)))
715 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
717 /* loop over the object properties and store the property value in the
719 for (i = 0; props[i]; i++) {
720 GValue gvalue = { 0, };
722 GParamSpec *property;
724 /* FIXME, change for childproxy to get the property and element. */
725 if (!(property = g_object_class_find_property (gclass, props[i]))) {
726 /* the element said it supported the property but then it does not have
727 * that property. This should not happen. */
728 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
732 g_value_init (&gvalue, property->value_type);
733 /* FIXME, change for childproxy */
734 g_object_get_property (G_OBJECT (preset), props[i], &gvalue);
736 if ((str = gst_value_serialize (&gvalue))) {
737 g_key_file_set_string (presets, name, props[i], (gpointer) str);
740 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
743 g_value_unset (&gvalue);
745 GST_INFO_OBJECT (preset, " saved");
748 /* save updated version */
749 return gst_preset_default_save_presets_file (preset);
754 GST_WARNING_OBJECT (preset, "no presets");
759 GST_INFO_OBJECT (preset, "no properties");
764 /* copies all keys and comments from one group to another, deleting the old
767 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
768 const gchar * new_name)
775 /* get the presets from the type */
776 if (!(presets = preset_get_keyfile (preset)))
779 if (!g_key_file_has_group (presets, old_name))
782 /* copy group comment if there is any */
783 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
784 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
788 /* get all keys from the old group and copy them in the new group */
789 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
790 for (i = 0; i < num_keys; i++) {
791 /* copy key comment if there is any */
792 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
793 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
797 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
798 g_key_file_set_value (presets, new_name, keys[i], str);
803 /* remove old group */
804 g_key_file_remove_group (presets, old_name, NULL);
806 /* save updated version */
807 return gst_preset_default_save_presets_file (preset);
812 GST_WARNING_OBJECT (preset, "no presets");
817 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
822 /* delete a group from the keyfile */
824 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
828 /* get the presets from the type */
829 if (!(presets = preset_get_keyfile (preset)))
833 if (!g_key_file_has_group (presets, name))
836 /* remove the group */
837 g_key_file_remove_group (presets, name, NULL);
839 /* save updated version */
840 return gst_preset_default_save_presets_file (preset);
845 GST_WARNING_OBJECT (preset, "no presets");
850 GST_WARNING_OBJECT (preset, "no preset named %s", name);
856 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
857 const gchar * tag, const gchar * value)
862 /* get the presets from the type */
863 if (!(presets = preset_get_keyfile (preset)))
866 key = g_strdup_printf ("_meta/%s", tag);
867 if (value && *value) {
868 g_key_file_set_value (presets, name, key, value);
870 g_key_file_remove_key (presets, name, key, NULL);
874 /* save updated keyfile */
875 return gst_preset_default_save_presets_file (preset);
880 GST_WARNING_OBJECT (preset, "no presets");
885 /* the caller must free @value after usage */
887 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
888 const gchar * tag, gchar ** value)
893 /* get the presets from the type */
894 if (!(presets = preset_get_keyfile (preset)))
897 key = g_strdup_printf ("_meta/%s", tag);
898 *value = g_key_file_get_value (presets, name, key, NULL);
906 GST_WARNING_OBJECT (preset, "no presets");
915 * gst_preset_get_preset_names:
916 * @preset: a #GObject that implements #GstPreset
918 * Get a copy of preset names as a NULL terminated string array.
920 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
921 * list with names, ue g_strfreev() after usage.
926 gst_preset_get_preset_names (GstPreset * preset)
928 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
930 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
934 * gst_preset_get_property_names:
935 * @preset: a #GObject that implements #GstPreset
937 * Get a the names of the GObject properties that can be used for presets.
939 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
940 * array of property names which should be freed with g_strfreev() after use.
945 gst_preset_get_property_names (GstPreset * preset)
947 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
949 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
953 * gst_preset_load_preset:
954 * @preset: a #GObject that implements #GstPreset
955 * @name: preset name to load
957 * Load the given preset.
959 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
964 gst_preset_load_preset (GstPreset * preset, const gchar * name)
966 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
967 g_return_val_if_fail (name, FALSE);
969 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
973 * gst_preset_save_preset:
974 * @preset: a #GObject that implements #GstPreset
975 * @name: preset name to save
977 * Save the current object settings as a preset under the given name. If there
978 * is already a preset by this @name it will be overwritten.
980 * Returns: %TRUE for success, %FALSE
985 gst_preset_save_preset (GstPreset * preset, const gchar * name)
987 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
988 g_return_val_if_fail (name, FALSE);
990 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
994 * gst_preset_rename_preset:
995 * @preset: a #GObject that implements #GstPreset
996 * @old_name: current preset name
997 * @new_name: new preset name
999 * Renames a preset. If there is already a preset by the @new_name it will be
1002 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1007 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1008 const gchar * new_name)
1010 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1011 g_return_val_if_fail (old_name, FALSE);
1012 g_return_val_if_fail (new_name, FALSE);
1014 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1019 * gst_preset_delete_preset:
1020 * @preset: a #GObject that implements #GstPreset
1021 * @name: preset name to remove
1023 * Delete the given preset.
1025 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1030 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1032 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1033 g_return_val_if_fail (name, FALSE);
1035 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1039 * gst_preset_set_meta:
1040 * @preset: a #GObject that implements #GstPreset
1041 * @name: preset name
1042 * @tag: meta data item name
1045 * Sets a new @value for an existing meta data item or adds a new item. Meta
1046 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1047 * @value will unset an existing value.
1049 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1054 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1055 const gchar * value)
1057 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1058 g_return_val_if_fail (name, FALSE);
1059 g_return_val_if_fail (tag, FALSE);
1061 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1065 * gst_preset_get_meta:
1066 * @preset: a #GObject that implements #GstPreset
1067 * @name: preset name
1068 * @tag: meta data item name
1069 * @value: (out callee-allocates): value
1071 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1072 * something like e.g. "comment". Returned values need to be released when done.
1074 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1075 * or no value for the given @tag
1080 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1083 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1084 g_return_val_if_fail (name, FALSE);
1085 g_return_val_if_fail (tag, FALSE);
1086 g_return_val_if_fail (value, FALSE);
1088 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1092 * gst_preset_set_app_dir:
1093 * @app_dir: the application specific preset dir
1095 * Sets an extra directory as an absolute path that should be considered when
1096 * looking for presets. Any presets in the application dir will shadow the
1099 * Returns: %TRUE for success, %FALSE if the dir already has been set
1104 gst_preset_set_app_dir (const gchar * app_dir)
1106 g_return_val_if_fail (app_dir, FALSE);
1108 if (!preset_app_dir) {
1109 preset_app_dir = g_strdup (app_dir);
1116 * gst_preset_get_app_dir:
1118 * Gets the directory for application specific presets if set by the
1121 * Returns: the directory or %NULL, don't free or modify the string
1126 gst_preset_get_app_dir (void)
1128 return preset_app_dir;
1131 /* class internals */
1134 gst_preset_class_init (GstPresetInterface * iface)
1136 iface->get_preset_names = gst_preset_default_get_preset_names;
1137 iface->get_property_names = gst_preset_default_get_property_names;
1139 iface->load_preset = gst_preset_default_load_preset;
1140 iface->save_preset = gst_preset_default_save_preset;
1141 iface->rename_preset = gst_preset_default_rename_preset;
1142 iface->delete_preset = gst_preset_default_delete_preset;
1144 iface->set_meta = gst_preset_default_set_meta;
1145 iface->get_meta = gst_preset_default_get_meta;
1149 gst_preset_base_init (gpointer g_class)
1151 static gboolean initialized = FALSE;
1154 /* init default implementation */
1155 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1156 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1158 /* create quarks for use with g_type_{g,s}et_qdata() */
1159 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1160 preset_user_path_quark =
1161 g_quark_from_static_string ("GstPreset::user_path");
1162 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1163 preset_system_path_quark =
1164 g_quark_from_static_string ("GstPreset::system_path");
1167 property_list_quark = g_quark_from_static_string ("GstPreset::properties");
1169 /* create interface properties, each element would need to override this
1170 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1171 * and in _set_property() do
1172 * case PROP_PRESET_NAME: {
1173 * gchar *name = g_value_get_string (value);
1175 * gst_preset_load_preset(preset, name);
1178 g_object_interface_install_property (g_class,
1179 g_param_spec_string ("preset-name",
1180 "preset-name property",
1181 "load given preset", NULL, G_PARAM_WRITABLE));
1189 gst_preset_get_type (void)
1191 static volatile gsize type = 0;
1193 if (g_once_init_enter (&type)) {
1195 const GTypeInfo info = {
1196 sizeof (GstPresetInterface),
1197 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1198 NULL, /* base_finalize */
1199 (GClassInitFunc) gst_preset_class_init, /* class_init */
1200 NULL, /* class_finalize */
1201 NULL, /* class_data */
1203 0, /* n_preallocs */
1204 NULL /* instance_init */
1206 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1207 g_once_init_leave (&type, _type);