API: add gst_tag_parse_extended_comment() (#351426).
[platform/upstream/gstreamer.git] / gst-libs / gst / tag / tags.c
1 /* GStreamer non-core tag registration and tag utility functions
2  * Copyright (C) 2005 Ross Burton <ross@burtonini.com>
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst-i18n-plugin.h>
26 #include <gst/gst.h>
27 #include "tag.h"
28
29 #include <string.h>
30
31 /**
32  * SECTION:gsttag
33  * @short_description: additional tag definitions for plugins and applications
34  * @see_also: #GstTagList
35  * 
36  * <refsect2>
37  * <para>
38  * Contains additional standardized GStreamer tag definitions for plugins
39  * and applications, and functions to register them with the GStreamer
40  * tag system.
41  * </para>
42  * </refsect2>
43  */
44
45
46 static gpointer
47 gst_tag_register_musicbrainz_tags_internal (gpointer unused)
48 {
49 #ifdef ENABLE_NLS
50   GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
51       LOCALEDIR);
52   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
53 #endif
54
55   gst_tag_register (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
56       G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
57   gst_tag_register (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
58       G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
59   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
60       G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
61   gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
62       G_TYPE_STRING,
63       _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
64   gst_tag_register (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
65       G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
66   gst_tag_register (GST_TAG_MUSICBRAINZ_SORTNAME, GST_TAG_FLAG_META,
67       G_TYPE_STRING,
68       _("artist sortname"), _("MusicBrainz artist sortname"), NULL);
69
70   return NULL;
71 }
72
73 /**
74  * gst_tag_register_musicbrainz_tags
75  *
76  * Registers additional musicbrainz-specific tags with the GStreamer tag
77  * system. Plugins and applications that use these tags should call this
78  * function before using them. Can be called multiple times.
79  */
80 void
81 gst_tag_register_musicbrainz_tags (void)
82 {
83   static GOnce mb_once = G_ONCE_INIT;
84
85   g_once (&mb_once, gst_tag_register_musicbrainz_tags_internal, NULL);
86 }
87
88 static void
89 register_tag_image_type_enum (GType * id)
90 {
91   static const GEnumValue image_types[] = {
92     {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
93     {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
94         "front-cover"},
95     {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
96         "back-cover"},
97     {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
98         "leaflet-page"},
99     {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
100     {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
101         "lead-artist"},
102     {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
103     {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
104     {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
105         "band-orchestra"},
106     {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
107     {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
108     {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
109           "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
110         "recording-location"},
111     {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
112         "during-recording"},
113     {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
114           "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
115         "during-performance"},
116     {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
117         "video-capture"},
118     {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
119     {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
120         "illustration"},
121     {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
122         "artist-logo"},
123     {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
124           "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
125         "publisher-studio-logo"},
126     {0, NULL, NULL}
127   };
128
129   *id = g_enum_register_static ("GstTagImageType", image_types);
130 }
131
132 GType
133 gst_tag_image_type_get_type (void)
134 {
135   static GType id;
136   static GOnce once = G_ONCE_INIT;
137
138   g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
139   return id;
140 }
141
142 /**
143  * gst_tag_parse_extended_comment:
144  * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
145  * @key: return location for the comment description key, or NULL
146  * @lang: return location for the comment ISO-639 language code, or NULL
147  * @value: return location for the actual comment string, or NULL
148  * @fail_if_no_key: whether to fail if strings are not in key=value form
149  *
150  * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
151  * separate it into its components.
152  *
153  * If successful, @key, @lang and/or @value will be set to newly allocated
154  * strings that you need to free with g_free() when done. @key and @lang
155  * may also be set to NULL by this function if there is no key or no language
156  * code in the extended comment string.
157  *
158  * Returns: TRUE if the string could be parsed, otherwise FALSE
159  *
160  * Since: 0.10.10
161  */
162 gboolean
163 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
164     gchar ** lang, gchar ** value, gboolean fail_if_no_key)
165 {
166   const gchar *div, *bop, *bcl;
167
168   g_return_val_if_fail (ext_comment != NULL, FALSE);
169   g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
170
171   if (key)
172     *key = NULL;
173   if (lang)
174     *lang = NULL;
175
176   div = strchr (ext_comment, '=');
177   bop = strchr (ext_comment, '[');
178   bcl = strchr (ext_comment, ']');
179
180   if (div == NULL) {
181     if (fail_if_no_key)
182       return FALSE;
183     if (value)
184       *value = g_strdup (ext_comment);
185     return TRUE;
186   }
187
188   if (bop != NULL && bop < div) {
189     if (bcl < bop || bcl > div)
190       return FALSE;
191     if (key)
192       *key = g_strndup (ext_comment, bop - ext_comment);
193     if (lang)
194       *lang = g_strndup (bop + 1, bcl - bop - 1);
195   } else {
196     if (key)
197       *key = g_strndup (ext_comment, div - ext_comment);
198   }
199
200   if (value)
201     *value = g_strdup (div + 1);
202
203   return TRUE;
204 }