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