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