Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstpreset.c
1 /* GStreamer
2  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstpreset.c: helper interface for element presets
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:gstpreset
23  * @short_description: helper interface for element presets
24  *
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
28  * single presets.
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.
31  *
32  * The interface comes with a default implementation that serves most plugins.
33  * Wrapper plugins will override most methods to implement support for the
34  * native preset format of those wrapped plugins.
35  * One method that is useful to be overridden is gst_preset_get_property_names().
36  * With that one can control which properties are saved and in which order.
37  */
38 /* FIXME:
39  * - non racyness
40  *   - we need to avoid two instances writing the preset file
41  *     -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
42  *     -> open exclusive
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-
48  *      wide
49  *     - can we use a lock inside a names shared memory segment?
50  *
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
54  *
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.
58  *
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
64  *
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);
72  *       // v1 ----
73  *       g_object_class_list_properties(child,&num);
74  *       // foreach prop
75  *       //   g_param_spec_set_qdata(prop, quark, (gpointer)child);
76  *       //   add to result
77  *       // v2 ----
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
81  *     }
82  *     OBJECT_UNLOCK(obj);
83  *
84  */
85
86 #include "gst_private.h"
87
88 #include "gstpreset.h"
89 #include "gstinfo.h"
90 #include "gstvalue.h"
91
92 #ifdef HAVE_UNISTD_H
93 #include <unistd.h>
94 #endif
95 #include <glib/gstdio.h>
96
97 #define GST_CAT_DEFAULT preset_debug
98 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
99
100 /* defines for keyfile usage, this group contains the element type name and
101  * version these presets belong to. */
102 #define PRESET_HEADER "_presets_"
103
104 /* keys of the preset header section */
105 #define PRESET_HEADER_ELEMENT_NAME "element-name"
106 #define PRESET_HEADER_VERSION "version"
107
108 static GQuark preset_user_path_quark = 0;
109 static GQuark preset_system_path_quark = 0;
110 static GQuark preset_quark = 0;
111
112 /*static GQuark property_list_quark = 0;*/
113
114 /* default iface implementation */
115
116 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
117
118 /*
119  * preset_get_paths:
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
123  *
124  * Fetch the preset_path for user local and system wide settings. Don't free
125  * after use.
126  *
127  * Returns: %FALSE if no paths could be found.
128  */
129 static gboolean
130 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
131     const gchar ** preset_system_path)
132 {
133   GType type = G_TYPE_FROM_INSTANCE (preset);
134   gchar *preset_path;
135   const gchar *element_name;
136
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);
140
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))) {
144       gchar *preset_dir;
145
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);
150       preset_path =
151           g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
152           element_name);
153       GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
154       /* create dirs */
155       g_mkdir_with_parents (preset_dir, 0755);
156       g_free (preset_dir);
157
158       /* cache the preset path to the type */
159       g_type_set_qdata (type, preset_user_path_quark, preset_path);
160     }
161     *preset_user_path = preset_path;
162   }
163
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))) {
167       gchar *preset_dir;
168
169       /* system presets in '$GST_DATADIR/gstreamer-0.10/presets/GstAudioPanorama.prs' */
170       preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
171           "presets", NULL);
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);
176       /* create dirs */
177       g_mkdir_with_parents (preset_dir, 0755);
178       g_free (preset_dir);
179
180       /* cache the preset path to the type */
181       g_type_set_qdata (type, preset_system_path_quark, preset_path);
182     }
183     *preset_system_path = preset_path;
184   }
185   return TRUE;
186 }
187
188 static gboolean
189 preset_skip_property (GParamSpec * property)
190 {
191   if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
192       (property->flags & G_PARAM_CONSTRUCT_ONLY))
193     return TRUE;
194   /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
195   return FALSE;
196 }
197
198 /* caller must free @preset_version after use */
199 static GKeyFile *
200 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
201     gchar ** preset_version)
202 {
203   GKeyFile *in;
204   GError *error = NULL;
205   gboolean res;
206   const gchar *element_name;
207   gchar *name;
208
209   in = g_key_file_new ();
210
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)
214     goto load_error;
215
216   /* element type name and preset name must match or we are dealing with a wrong
217    * preset file */
218   element_name = G_OBJECT_TYPE_NAME (preset);
219   name =
220       g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
221       NULL);
222
223   if (!name || strcmp (name, element_name))
224     goto wrong_name;
225
226   g_free (name);
227
228   /* get the version now so that the caller can check it */
229   if (preset_version)
230     *preset_version =
231         g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
232
233   return in;
234
235   /* ERRORS */
236 load_error:
237   {
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);
242     return NULL;
243   }
244 wrong_name:
245   {
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));
249     g_free (name);
250     g_key_file_free (in);
251     return NULL;
252   }
253 }
254
255 static guint64
256 preset_parse_version (const gchar * str_version)
257 {
258   gint major, minor, micro, nano, num;
259
260   major = minor = micro = nano = 0;
261
262   /* parse version (e.g. 0.10.15.1) to guint64 */
263   num = sscanf (str_version, "%d.%d.%d.%d", &major, &minor, &micro, &nano);
264   /* make sure we have atleast "major.minor" */
265   if (num > 1) {
266     guint64 version;
267
268     version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
269     GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
270     return version;
271   }
272   return G_GUINT64_CONSTANT (0);
273 }
274
275 static void
276 preset_merge (GKeyFile * system, GKeyFile * user)
277 {
278   gchar *str;
279   gchar **groups, **keys;
280   gsize i, j, num_groups, num_keys;
281
282   /* copy file comment if there is any */
283   if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
284     g_key_file_set_comment (system, NULL, NULL, str, NULL);
285     g_free (str);
286   }
287
288   /* get groups in user and copy into system */
289   groups = g_key_file_get_groups (user, &num_groups);
290   for (i = 0; i < num_groups; i++) {
291     /* copy group comment if there is any */
292     if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
293       g_key_file_set_comment (system, groups[i], NULL, str, NULL);
294       g_free (str);
295     }
296
297     /* ignore private groups */
298     if (groups[i][0] == '_')
299       continue;
300
301     /* if group already exists in system, remove and re-add keys from user */
302     if (g_key_file_has_group (system, groups[i])) {
303       g_key_file_remove_group (system, groups[i], NULL);
304     }
305
306     keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
307     for (j = 0; j < num_keys; j++) {
308       /* copy key comment if there is any */
309       if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
310         g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
311         g_free (str);
312       }
313       str = g_key_file_get_value (user, groups[i], keys[j], NULL);
314       g_key_file_set_value (system, groups[i], keys[j], str);
315       g_free (str);
316     }
317     g_strfreev (keys);
318   }
319   g_strfreev (groups);
320 }
321
322 /* reads the user and system presets files and merges them together. This
323  * function caches the GKeyFile on the element type. If there is no existing
324  * preset file, a new in-memory GKeyFile will be created. */
325 static GKeyFile *
326 preset_get_keyfile (GstPreset * preset)
327 {
328   GKeyFile *presets;
329   GType type = G_TYPE_FROM_INSTANCE (preset);
330
331   /* first see if the have a cached version for the type */
332   if (!(presets = g_type_get_qdata (type, preset_quark))) {
333     const gchar *preset_user_path, *preset_system_path;
334     gchar *str_version_user = NULL, *str_version_system = NULL;
335     gboolean updated_from_system = FALSE;
336     GKeyFile *in_user, *in_system;
337
338     preset_get_paths (preset, &preset_user_path, &preset_system_path);
339
340     /* try to load the user and system presets, we do this to get the versions
341      * of both files. */
342     in_user = preset_open_and_parse_header (preset, preset_user_path,
343         &str_version_user);
344     in_system = preset_open_and_parse_header (preset, preset_system_path,
345         &str_version_system);
346
347     /* compare version to check for merge */
348     if (in_system) {
349       /* keep system presets if there is no user preset or when the system
350        * version is higher than the user version. */
351       if (!in_user) {
352         presets = in_system;
353       } else if (preset_parse_version (str_version_system) >
354           preset_parse_version (str_version_user)) {
355         presets = in_system;
356         updated_from_system = TRUE;
357       }
358     }
359     if (in_user) {
360       if (updated_from_system) {
361         /* merge user on top of system presets */
362         preset_merge (presets, in_user);
363         g_key_file_free (in_user);
364       } else {
365         /* keep user presets */
366         presets = in_user;
367       }
368     }
369     if (!in_user && !in_system) {
370       /* we did not load a user or system presets file, create a new one */
371       presets = g_key_file_new ();
372       g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
373           G_OBJECT_TYPE_NAME (preset));
374     }
375
376     g_free (str_version_user);
377     g_free (str_version_system);
378
379     /* attach the preset to the type */
380     g_type_set_qdata (type, preset_quark, (gpointer) presets);
381
382     if (updated_from_system) {
383       gst_preset_default_save_presets_file (preset);
384     }
385   }
386   return presets;
387 }
388
389 /* get a list of all supported preset names for an element */
390 static gchar **
391 gst_preset_default_get_preset_names (GstPreset * preset)
392 {
393   GKeyFile *presets;
394   gsize i, num_groups;
395   gchar **groups;
396
397   /* get the presets from the type */
398   if (!(presets = preset_get_keyfile (preset)))
399     goto no_presets;
400
401   /* get the groups, which are also the preset names */
402   if (!(groups = g_key_file_get_groups (presets, &num_groups)))
403     goto no_groups;
404
405   /* remove all private group names starting with '_' from the array */
406   for (i = 0; i < num_groups; i++) {
407     if (groups[i][0] == '_') {
408       /* free private group */
409       g_free (groups[i]);
410       /* move last element of list down */
411       num_groups--;
412       /* move last element into removed element */
413       groups[i] = groups[num_groups];
414       groups[num_groups] = NULL;
415     }
416   }
417   /* sort the array now */
418   g_qsort_with_data (groups, num_groups, sizeof (gchar *),
419       (GCompareDataFunc) strcmp, NULL);
420
421   return groups;
422
423   /* ERRORS */
424 no_presets:
425   {
426     GST_WARNING_OBJECT (preset, "Could not load presets");
427     return NULL;
428   }
429 no_groups:
430   {
431     GST_WARNING_OBJECT (preset, "Could not find preset groups");
432     return NULL;
433   }
434 }
435
436 /* get a list of all property names that are used for presets */
437 static gchar **
438 gst_preset_default_get_property_names (GstPreset * preset)
439 {
440   GParamSpec **props;
441   guint i, j, n_props;
442   GObjectClass *gclass;
443   gchar **result;
444
445   gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
446
447   /* get a list of normal properties. 
448    * FIXME, change this for childproxy support. */
449   props = g_object_class_list_properties (gclass, &n_props);
450   if (!props)
451     goto no_properties;
452
453   /* allocate array big enough to hold the worst case, including a terminating
454    * NULL pointer. */
455   result = g_new (gchar *, n_props + 1);
456
457   /* now filter out the properties that we can use for presets */
458   GST_DEBUG_OBJECT (preset, "  filtering properties: %u", n_props);
459   for (i = j = 0; i < n_props; i++) {
460     if (preset_skip_property (props[i]))
461       continue;
462
463     /* copy and increment out pointer */
464     result[j++] = g_strdup (props[i]->name);
465   }
466   result[j] = NULL;
467   g_free (props);
468
469   return result;
470
471   /* ERRORS */
472 no_properties:
473   {
474     GST_INFO_OBJECT (preset, "object has no properties");
475     return NULL;
476   }
477 }
478
479 /* load the presets of @name for the instance @preset. Returns %FALSE if something
480  * failed. */
481 static gboolean
482 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
483 {
484   GKeyFile *presets;
485   gchar **props;
486   guint i;
487   GObjectClass *gclass;
488
489   /* get the presets from the type */
490   if (!(presets = preset_get_keyfile (preset)))
491     goto no_presets;
492
493   /* get the preset name */
494   if (!g_key_file_has_group (presets, name))
495     goto no_group;
496
497   GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
498
499   /* get the properties that we can configure in this element */
500   if (!(props = gst_preset_get_property_names (preset)))
501     goto no_properties;
502
503   gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
504
505   /* for each of the property names, find the preset parameter and try to
506    * configure the property with its value */
507   for (i = 0; props[i]; i++) {
508     gchar *str;
509     GValue gvalue = { 0, };
510     GParamSpec *property;
511
512     /* check if we have a settings for this element property */
513     if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
514       /* the element has a property but the parameter is not in the keyfile */
515       GST_WARNING_OBJECT (preset, "parameter '%s' not in preset", props[i]);
516       continue;
517     }
518
519     GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
520         props[i]);
521
522     /* FIXME, change for childproxy to get the property and element.  */
523     if (!(property = g_object_class_find_property (gclass, props[i]))) {
524       /* the parameter was in the keyfile, the element said it supported it but
525        * then the property was not found in the element. This should not happen. */
526       GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
527       g_free (str);
528       continue;
529     }
530
531     /* try to deserialize the property value from the keyfile and set it as
532      * the object property */
533     g_value_init (&gvalue, property->value_type);
534     if (gst_value_deserialize (&gvalue, str)) {
535       /* FIXME, change for childproxy support */
536       g_object_set_property (G_OBJECT (preset), props[i], &gvalue);
537     } else {
538       GST_WARNING_OBJECT (preset,
539           "deserialization of value '%s' for property '%s' failed", str,
540           props[i]);
541     }
542     g_value_unset (&gvalue);
543     g_free (str);
544   }
545   g_strfreev (props);
546
547   return TRUE;
548
549   /* ERRORS */
550 no_presets:
551   {
552     GST_WARNING_OBJECT (preset, "no presets");
553     return FALSE;
554   }
555 no_group:
556   {
557     GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
558     return FALSE;
559   }
560 no_properties:
561   {
562     GST_INFO_OBJECT (preset, "no properties");
563     return FALSE;
564   }
565 }
566
567 /* save the presets file. A copy of the existing presets file is stored in a
568  * .bak file */
569 static gboolean
570 gst_preset_default_save_presets_file (GstPreset * preset)
571 {
572   GKeyFile *presets;
573   const gchar *preset_path;
574   GError *error = NULL;
575   gchar *bak_file_name;
576   gboolean backup = TRUE;
577   gchar *data;
578   gsize data_size;
579
580   preset_get_paths (preset, &preset_path, NULL);
581
582   /* get the presets from the type */
583   if (!(presets = preset_get_keyfile (preset)))
584     goto no_presets;
585
586   GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
587
588   /* create backup if possible */
589   bak_file_name = g_strdup_printf ("%s.bak", preset_path);
590   if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
591     if (g_unlink (bak_file_name)) {
592       backup = FALSE;
593       GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
594           bak_file_name);
595     }
596   }
597   if (backup) {
598     if (g_rename (preset_path, bak_file_name)) {
599       GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
600           bak_file_name);
601     }
602   }
603   g_free (bak_file_name);
604
605   /* update gstreamer version */
606   g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
607       PACKAGE_VERSION);
608
609   /* get new contents, wee need this to save it */
610   if (!(data = g_key_file_to_data (presets, &data_size, &error)))
611     goto convert_failed;
612
613   /* write presets */
614   if (!g_file_set_contents (preset_path, data, data_size, &error))
615     goto write_failed;
616
617   g_free (data);
618
619   return TRUE;
620
621   /* ERRORS */
622 no_presets:
623   {
624     GST_WARNING_OBJECT (preset,
625         "no presets, trying to unlink possibly existing preset file: '%s'",
626         preset_path);
627     g_unlink (preset_path);
628     return FALSE;
629   }
630 convert_failed:
631   {
632     GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
633         error->message);
634     g_error_free (error);
635     g_free (data);
636     return FALSE;
637   }
638 write_failed:
639   {
640     GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
641         preset_path, error->message);
642     g_error_free (error);
643     g_free (data);
644     return FALSE;
645   }
646 }
647
648 /* save the preset with the given name */
649 static gboolean
650 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
651 {
652   GKeyFile *presets;
653   gchar **props;
654   guint i;
655   GObjectClass *gclass;
656
657   GST_INFO_OBJECT (preset, "saving new preset: %s", name);
658
659   /* get the presets from the type */
660   if (!(presets = preset_get_keyfile (preset)))
661     goto no_presets;
662
663   /* take copies of current gobject properties from preset */
664   if (!(props = gst_preset_get_property_names (preset)))
665     goto no_properties;
666
667   gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
668
669   /* loop over the object properties and store the property value in the
670    * keyfile */
671   for (i = 0; props[i]; i++) {
672     GValue gvalue = { 0, };
673     gchar *str;
674     GParamSpec *property;
675
676     /* FIXME, change for childproxy to get the property and element.  */
677     if (!(property = g_object_class_find_property (gclass, props[i]))) {
678       /* the element said it supported the property but then it does not have
679        * that property. This should not happen. */
680       GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
681       continue;
682     }
683
684     g_value_init (&gvalue, property->value_type);
685     /* FIXME, change for childproxy */
686     g_object_get_property (G_OBJECT (preset), props[i], &gvalue);
687
688     if ((str = gst_value_serialize (&gvalue))) {
689       g_key_file_set_string (presets, name, props[i], (gpointer) str);
690       g_free (str);
691     } else {
692       GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
693           props[i]);
694     }
695     g_value_unset (&gvalue);
696   }
697   GST_INFO_OBJECT (preset, "  saved");
698   g_strfreev (props);
699
700   /* save updated version */
701   return gst_preset_default_save_presets_file (preset);
702
703   /* ERRORS */
704 no_presets:
705   {
706     GST_WARNING_OBJECT (preset, "no presets");
707     return FALSE;
708   }
709 no_properties:
710   {
711     GST_INFO_OBJECT (preset, "no properties");
712     return FALSE;
713   }
714 }
715
716 /* copies all keys and comments from one group to another, deleting the old
717  * group. */
718 static gboolean
719 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
720     const gchar * new_name)
721 {
722   GKeyFile *presets;
723   gchar *str;
724   gchar **keys;
725   gsize i, num_keys;
726
727   /* get the presets from the type */
728   if (!(presets = preset_get_keyfile (preset)))
729     goto no_presets;
730
731   if (!g_key_file_has_group (presets, old_name))
732     goto no_group;
733
734   /* copy group comment if there is any */
735   if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
736     g_key_file_set_comment (presets, new_name, NULL, str, NULL);
737     g_free (str);
738   }
739
740   /* get all keys from the old group and copy them in the new group */
741   keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
742   for (i = 0; i < num_keys; i++) {
743     /* copy key comment if there is any */
744     if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
745       g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
746       g_free (str);
747     }
748     /* copy key value */
749     str = g_key_file_get_value (presets, old_name, keys[i], NULL);
750     g_key_file_set_value (presets, new_name, keys[i], str);
751     g_free (str);
752   }
753   g_strfreev (keys);
754
755   /* remove old group */
756   g_key_file_remove_group (presets, old_name, NULL);
757
758   /* save updated version */
759   return gst_preset_default_save_presets_file (preset);
760
761   /* ERRORS */
762 no_presets:
763   {
764     GST_WARNING_OBJECT (preset, "no presets");
765     return FALSE;
766   }
767 no_group:
768   {
769     GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
770     return FALSE;
771   }
772 }
773
774 /* delete a group from the keyfile */
775 static gboolean
776 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
777 {
778   GKeyFile *presets;
779
780   /* get the presets from the type */
781   if (!(presets = preset_get_keyfile (preset)))
782     goto no_presets;
783
784   /* get the group */
785   if (!g_key_file_has_group (presets, name))
786     goto no_group;
787
788   /* remove the group */
789   g_key_file_remove_group (presets, name, NULL);
790
791   /* save updated version */
792   return gst_preset_default_save_presets_file (preset);
793
794   /* ERRORS */
795 no_presets:
796   {
797     GST_WARNING_OBJECT (preset, "no presets");
798     return FALSE;
799   }
800 no_group:
801   {
802     GST_WARNING_OBJECT (preset, "no preset named %s", name);
803     return FALSE;
804   }
805 }
806
807 static gboolean
808 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
809     const gchar * tag, const gchar * value)
810 {
811   GKeyFile *presets;
812   gchar *key;
813
814   /* get the presets from the type */
815   if (!(presets = preset_get_keyfile (preset)))
816     goto no_presets;
817
818   key = g_strdup_printf ("_meta/%s", tag);
819   if (value && *value) {
820     g_key_file_set_value (presets, name, key, value);
821   } else {
822     g_key_file_remove_key (presets, name, key, NULL);
823   }
824   g_free (key);
825
826   /* save updated keyfile */
827   return gst_preset_default_save_presets_file (preset);
828
829   /* ERRORS */
830 no_presets:
831   {
832     GST_WARNING_OBJECT (preset, "no presets");
833     return FALSE;
834   }
835 }
836
837 /* the caller must free @value after usage */
838 static gboolean
839 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
840     const gchar * tag, gchar ** value)
841 {
842   GKeyFile *presets;
843   gchar *key;
844
845   /* get the presets from the type */
846   if (!(presets = preset_get_keyfile (preset)))
847     goto no_presets;
848
849   key = g_strdup_printf ("_meta/%s", tag);
850   *value = g_key_file_get_value (presets, name, key, NULL);
851   g_free (key);
852
853   return TRUE;
854
855   /* ERRORS */
856 no_presets:
857   {
858     GST_WARNING_OBJECT (preset, "no presets");
859     *value = NULL;
860     return FALSE;
861   }
862 }
863
864 /* wrapper */
865
866 /**
867  * gst_preset_get_preset_names:
868  * @preset: a #GObject that implements #GstPreset
869  *
870  * Get a copy of preset names as a NULL terminated string array.
871  *
872  * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
873  *     list with names, ue g_strfreev() after usage.
874  *
875  * Since: 0.10.20
876  */
877 gchar **
878 gst_preset_get_preset_names (GstPreset * preset)
879 {
880   g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
881
882   return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
883 }
884
885 /**
886  * gst_preset_get_property_names:
887  * @preset: a #GObject that implements #GstPreset
888  *
889  * Get a the names of the GObject properties that can be used for presets.
890  *
891  * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): an
892  *   array of property names which should be freed with g_strfreev() after use.
893  *
894  * Since: 0.10.20
895  */
896 gchar **
897 gst_preset_get_property_names (GstPreset * preset)
898 {
899   g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
900
901   return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
902 }
903
904 /**
905  * gst_preset_load_preset:
906  * @preset: a #GObject that implements #GstPreset
907  * @name: preset name to load
908  *
909  * Load the given preset.
910  *
911  * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
912  *
913  * Since: 0.10.20
914  */
915 gboolean
916 gst_preset_load_preset (GstPreset * preset, const gchar * name)
917 {
918   g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
919   g_return_val_if_fail (name, FALSE);
920
921   return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
922 }
923
924 /**
925  * gst_preset_save_preset:
926  * @preset: a #GObject that implements #GstPreset
927  * @name: preset name to save
928  *
929  * Save the current object settings as a preset under the given name. If there
930  * is already a preset by this @name it will be overwritten.
931  *
932  * Returns: %TRUE for success, %FALSE
933  *
934  * Since: 0.10.20
935  */
936 gboolean
937 gst_preset_save_preset (GstPreset * preset, const gchar * name)
938 {
939   g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
940   g_return_val_if_fail (name, FALSE);
941
942   return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
943 }
944
945 /**
946  * gst_preset_rename_preset:
947  * @preset: a #GObject that implements #GstPreset
948  * @old_name: current preset name
949  * @new_name: new preset name
950  *
951  * Renames a preset. If there is already a preset by the @new_name it will be
952  * overwritten.
953  *
954  * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
955  *
956  * Since: 0.10.20
957  */
958 gboolean
959 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
960     const gchar * new_name)
961 {
962   g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
963   g_return_val_if_fail (old_name, FALSE);
964   g_return_val_if_fail (new_name, FALSE);
965
966   return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
967           new_name));
968 }
969
970 /**
971  * gst_preset_delete_preset:
972  * @preset: a #GObject that implements #GstPreset
973  * @name: preset name to remove
974  *
975  * Delete the given preset.
976  *
977  * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
978  *
979  * Since: 0.10.20
980  */
981 gboolean
982 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
983 {
984   g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
985   g_return_val_if_fail (name, FALSE);
986
987   return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
988 }
989
990 /**
991  * gst_preset_set_meta:
992  * @preset: a #GObject that implements #GstPreset
993  * @name: preset name
994  * @tag: meta data item name
995  * @value: new value
996  *
997  * Sets a new @value for an existing meta data item or adds a new item. Meta
998  * data @tag names can be something like e.g. "comment". Supplying %NULL for the
999  * @value will unset an existing value.
1000  *
1001  * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1002  *
1003  * Since: 0.10.20
1004  */
1005 gboolean
1006 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1007     const gchar * value)
1008 {
1009   g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1010   g_return_val_if_fail (name, FALSE);
1011   g_return_val_if_fail (tag, FALSE);
1012
1013   return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1014 }
1015
1016 /**
1017  * gst_preset_get_meta:
1018  * @preset: a #GObject that implements #GstPreset
1019  * @name: preset name
1020  * @tag: meta data item name
1021  * @value: (out callee-allocates): value
1022  *
1023  * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1024  * something like e.g. "comment". Returned values need to be released when done.
1025  *
1026  * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1027  * or no value for the given @tag
1028  *
1029  * Since: 0.10.20
1030  */
1031 gboolean
1032 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1033     gchar ** value)
1034 {
1035   g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1036   g_return_val_if_fail (name, FALSE);
1037   g_return_val_if_fail (tag, FALSE);
1038   g_return_val_if_fail (value, FALSE);
1039
1040   return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1041 }
1042
1043 /* class internals */
1044
1045 static void
1046 gst_preset_class_init (GstPresetInterface * iface)
1047 {
1048   iface->get_preset_names = gst_preset_default_get_preset_names;
1049   iface->get_property_names = gst_preset_default_get_property_names;
1050
1051   iface->load_preset = gst_preset_default_load_preset;
1052   iface->save_preset = gst_preset_default_save_preset;
1053   iface->rename_preset = gst_preset_default_rename_preset;
1054   iface->delete_preset = gst_preset_default_delete_preset;
1055
1056   iface->set_meta = gst_preset_default_set_meta;
1057   iface->get_meta = gst_preset_default_get_meta;
1058 }
1059
1060 static void
1061 gst_preset_base_init (gpointer g_class)
1062 {
1063   static gboolean initialized = FALSE;
1064
1065   if (!initialized) {
1066     /* init default implementation */
1067     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1068         GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1069
1070     /* create quarks for use with g_type_{g,s}et_qdata() */
1071     preset_quark = g_quark_from_static_string ("GstPreset::presets");
1072     preset_user_path_quark =
1073         g_quark_from_static_string ("GstPreset::user_path");
1074     preset_system_path_quark =
1075         g_quark_from_static_string ("GstPreset::system_path");
1076
1077 #if 0
1078     property_list_quark = g_quark_from_static_string ("GstPreset::properties");
1079
1080     /* create interface properties, each element would need to override this
1081      *   g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1082      * and in _set_property() do
1083      *   case PROP_PRESET_NAME: {
1084      *     gchar *name = g_value_get_string (value);
1085      *     if (name)
1086      *       gst_preset_load_preset(preset, name);
1087      *   } break;
1088      */
1089     g_object_interface_install_property (g_class,
1090         g_param_spec_string ("preset-name",
1091             "preset-name property",
1092             "load given preset", NULL, G_PARAM_WRITABLE));
1093 #endif
1094
1095     initialized = TRUE;
1096   }
1097 }
1098
1099 GType
1100 gst_preset_get_type (void)
1101 {
1102   static volatile gsize type = 0;
1103
1104   if (g_once_init_enter (&type)) {
1105     GType _type;
1106     const GTypeInfo info = {
1107       sizeof (GstPresetInterface),
1108       (GBaseInitFunc) gst_preset_base_init,     /* base_init */
1109       NULL,                     /* base_finalize */
1110       (GClassInitFunc) gst_preset_class_init,   /* class_init */
1111       NULL,                     /* class_finalize */
1112       NULL,                     /* class_data */
1113       0,
1114       0,                        /* n_preallocs */
1115       NULL                      /* instance_init */
1116     };
1117     _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1118     g_once_init_leave (&type, _type);
1119   }
1120   return type;
1121 }