tracker-0.16.2/src/tracker-writeback/tracker-writeback-taglib.c

No issues found

  1 /*
  2  * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) any later version.
  8  *
  9  * This library is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  *
 19  */
 20 
 21 #include "config.h"
 22 
 23 #include <stdlib.h>
 24 
 25 #include <taglib/tag_c.h>
 26 
 27 #include <glib-object.h>
 28 
 29 #include <libtracker-common/tracker-ontologies.h>
 30 
 31 #include "tracker-writeback-file.h"
 32 
 33 #define TRACKER_TYPE_WRITEBACK_TAGLIB (tracker_writeback_taglib_get_type ())
 34 
 35 typedef struct TrackerWritebackTaglib TrackerWritebackTaglib;
 36 typedef struct TrackerWritebackTaglibClass TrackerWritebackTaglibClass;
 37 
 38 struct TrackerWritebackTaglib {
 39 	TrackerWritebackFile parent_instance;
 40 };
 41 
 42 struct TrackerWritebackTaglibClass {
 43 	TrackerWritebackFileClass parent_class;
 44 };
 45 
 46 static GType                tracker_writeback_taglib_get_type         (void) G_GNUC_CONST;
 47 static gboolean             writeback_taglib_update_file_metadata     (TrackerWritebackFile     *wbf,
 48                                                                        GFile                    *file,
 49                                                                        GPtrArray                *values,
 50                                                                        TrackerSparqlConnection  *connection,
 51                                                                        GCancellable             *cancellable,
 52                                                                        GError                   **error);
 53 static const gchar * const *writeback_taglib_content_types            (TrackerWritebackFile     *wbf);
 54 static gchar*               writeback_taglib_get_artist_name          (TrackerSparqlConnection  *connection,
 55                                                                        const gchar              *urn);
 56 static gchar*               writeback_taglib_get_album_name           (TrackerSparqlConnection  *connection,
 57                                                                        const gchar              *urn);
 58 
 59 G_DEFINE_DYNAMIC_TYPE (TrackerWritebackTaglib, tracker_writeback_taglib, TRACKER_TYPE_WRITEBACK_FILE);
 60 
 61 static void
 62 tracker_writeback_taglib_class_init (TrackerWritebackTaglibClass *klass)
 63 {
 64 	TrackerWritebackFileClass *writeback_file_class = TRACKER_WRITEBACK_FILE_CLASS (klass);
 65 
 66 	writeback_file_class->update_file_metadata = writeback_taglib_update_file_metadata;
 67 	writeback_file_class->content_types = writeback_taglib_content_types;
 68 }
 69 
 70 static void
 71 tracker_writeback_taglib_class_finalize (TrackerWritebackTaglibClass *klass)
 72 {
 73 }
 74 
 75 static void
 76 tracker_writeback_taglib_init (TrackerWritebackTaglib *wbt)
 77 {
 78 }
 79 
 80 static const gchar * const *
 81 writeback_taglib_content_types (TrackerWritebackFile *wbf)
 82 {
 83 	static const gchar *content_types [] = {
 84 		"audio/x-mpc",
 85 		"audio/x-musepack",
 86 		"audio/mpc",
 87 		"audio/musepack",
 88 		"audio/mpeg",
 89 		"audio/x-mpeg",
 90 		"audio/mp3",
 91 		"audio/x-mp3",
 92 		"audio/mpeg3",
 93 		"audio/x-mpeg3",
 94 		"audio/mpg",
 95 		"audio/x-mpg",
 96 		"audio/x-mpegaudio",
 97 		"audio/flac",
 98 		"audio/mp4",
 99 		"audio/asf",
100 		"application/asx",
101 		"video/x-ms-asf-plugin",
102 		"application/x-mplayer2",
103 		"video/x-ms-asf",
104 		"application/vnd.ms-asf",
105 		"video/x-ms-asf-plugin",
106 		"video/x-ms-wm",
107 		"video/x-ms-wmx",
108 		"audio/aiff",
109 		"audio/x-aiff",
110 		"sound/aiff",
111 		"audio/rmf",
112 		"audio/x-rmf",
113 		"audio/x-pn-aiff",
114 		"audio/x-gsm",
115 		"audio/mid",
116 		"audio/x-midi",
117 		"audio/vnd.qcelp",
118 		"audio/wav",
119 		"audio/x-wav",
120 		"audio/wave",
121 		"audio/x-pn-wav",
122 		"audio/tta",
123 		"audio/x-tta",
124 		"audio/ogg",
125 		"application/ogg",
126 		"audio/x-ogg",
127 		"application/x-ogg",
128 		"audio/x-speex",
129 		NULL
130 	};
131 
132 	return content_types;
133 }
134 
135 static gboolean
136 writeback_taglib_update_file_metadata (TrackerWritebackFile     *writeback_file,
137                                        GFile                    *file,
138                                        GPtrArray                *values,
139                                        TrackerSparqlConnection  *connection,
140                                        GCancellable             *cancellable,
141                                        GError                  **error)
142 {
143 	gboolean ret;
144 	gchar *path;
145 	TagLib_File *taglib_file = NULL;
146 	TagLib_Tag *tag;
147 	guint n;
148 
149 	ret = FALSE;
150 	path = g_file_get_path (file);
151 	taglib_file = taglib_file_new (path);
152 
153 	if (!taglib_file || !taglib_file_is_valid (taglib_file)) {
154 		goto out;
155 	}
156 
157 	tag = taglib_file_tag (taglib_file);
158 
159 	for (n = 0; n < values->len; n++) {
160 		const GStrv row = g_ptr_array_index (values, n);
161 
162 		if (g_strcmp0 (row[2], TRACKER_NIE_PREFIX "title") == 0) {
163 			taglib_tag_set_title (tag, row[3]);
164 		}
165 
166 		if (g_strcmp0 (row[2], TRACKER_NMM_PREFIX "performer") == 0) {
167 			gchar *artist_name = writeback_taglib_get_artist_name (connection, row[3]);
168 
169 			if (artist_name) {
170 				taglib_tag_set_artist (tag, artist_name);
171 				g_free (artist_name);
172 			}
173 		}
174 
175 		if (g_strcmp0 (row[2], TRACKER_NMM_PREFIX "musicAlbum") == 0) {
176 			gchar *album_name = writeback_taglib_get_album_name (connection, row[3]);
177 
178 			if (album_name) {
179 				taglib_tag_set_album (tag, album_name);
180 				g_free (album_name);
181 			}
182 		}
183 
184 		if (g_strcmp0 (row[2], TRACKER_RDFS_PREFIX "comment") == 0) {
185 			taglib_tag_set_comment (tag, row[3]);
186 		}
187 
188 		if (g_strcmp0 (row[2], TRACKER_NMM_PREFIX "genre") == 0) {
189 			taglib_tag_set_genre (tag, row[3]);
190 		}
191 
192 		if (g_strcmp0 (row[2], TRACKER_NMM_PREFIX "trackNumber") == 0) {
193 			taglib_tag_set_track (tag, atoi (row[3]));
194 		}
195 	}
196 
197 	taglib_file_save (taglib_file);
198 
199 	ret = TRUE;
200 
201  out:
202 	g_free (path);
203 	if (taglib_file) {
204 		taglib_file_free (taglib_file);
205 	}
206 
207 	return ret;
208 }
209 
210 static gchar*
211 writeback_taglib_get_from_query (TrackerSparqlConnection *connection,
212                                  const gchar             *urn,
213                                  const gchar             *query,
214                                  const gchar             *errmsg)
215 {
216 	TrackerSparqlCursor *cursor;
217 	GError *error = NULL;
218 	gchar *value = NULL;
219 
220 	cursor = tracker_sparql_connection_query (connection,
221 	                                          query,
222 	                                          NULL,
223 	                                          &error);
224 
225 	if (error || !cursor || !tracker_sparql_cursor_next (cursor, NULL, NULL)) {
226 		g_warning ("Couldn't find %s for artist with urn '%s', %s",
227 		           errmsg,
228 		           urn,
229 		           error ? error->message : "no such was found");
230 
231 		if (error) {
232 			g_error_free (error);
233 		}
234 	} else {
235 		value = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
236 	}
237 
238 	g_object_unref (cursor);
239 
240 	return value;
241 }
242 
243 
244 static gchar*
245 writeback_taglib_get_artist_name (TrackerSparqlConnection *connection,
246                                   const gchar             *urn)
247 {
248 	gchar *val, *query;
249 
250 	query = g_strdup_printf ("SELECT ?artistName WHERE {<%s> nmm:artistName ?artistName}",
251 	                         urn);
252 	val = writeback_taglib_get_from_query (connection, urn, query, "artist name");
253 	g_free (query);
254 
255 	return val;
256 }
257 
258 static gchar*
259 writeback_taglib_get_album_name (TrackerSparqlConnection *connection,
260                                  const gchar             *urn)
261 {
262 	gchar *val, *query;
263 
264 	query = g_strdup_printf ("SELECT ?albumName WHERE {<%s> dc:title ?albumName}",
265 	                         urn);
266 	val = writeback_taglib_get_from_query (connection, urn, query, "album name");
267 	g_free (query);
268 
269 	return val;
270 }
271 
272 TrackerWriteback *
273 writeback_module_create (GTypeModule *module)
274 {
275 	tracker_writeback_taglib_register_type (module);
276 
277 	return g_object_new (TRACKER_TYPE_WRITEBACK_TAGLIB, NULL);
278 }
279 
280 const gchar * const *
281 writeback_module_get_rdf_types (void)
282 {
283 	static const gchar *rdftypes[] = {
284 		TRACKER_NFO_PREFIX "Audio",
285 		NULL
286 	};
287 
288 	return rdftypes;
289 }