Just get "lead artist" (TPE1), don't try others.
[lms] / lightmediascanner / src / plugins / id3lib / id3lib.cpp
1 /**
2  * Copyright (C) 2007 by INdT
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * @author Gustavo Sverzut Barbieri <gustavo.barbieri@openbossa.org>
19  */
20
21 /**
22  * @brief
23  *
24  * Reads ID3 tags from MP3 using id3lib.
25  *
26  * @todo: write a faster module to replace this one, using no external libs.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #define _XOPEN_SOURCE 600
34 #include <lightmediascanner_plugin.h>
35 #include <lightmediascanner_utils.h>
36 #include <lightmediascanner_db.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <id3/tag.h>
41
42 extern "C" {
43
44 static void
45 _id3lib_get_string(const ID3_Frame *frame, ID3_FieldID field_id, struct lms_string_size *s)
46 {
47   ID3_Field* field;
48   ID3_TextEnc enc;
49   size_t size;
50
51   field = frame->GetField(field_id);
52   if (!field)
53     return;
54
55   enc = field->GetEncoding();
56   field->SetEncoding(ID3TE_ISO8859_1);
57   size = field->Size();
58   if (size < 1)
59     goto done;
60
61   size++;
62   s->str = (char *)malloc(size * sizeof(char));
63   s->len = field->Get(s->str, size);
64   if (s->len > 0)
65     lms_strstrip(s->str, &s->len);
66
67   if (s->len < 1) {
68     free(s->str);
69     s->str = NULL;
70     s->len = 0;
71   }
72
73  done:
74   field->SetEncoding(enc);
75 }
76
77 static unsigned int
78 _id3lib_get_string_as_int(const ID3_Frame *frame, ID3_FieldID field_id)
79 {
80   char buf[32];
81   ID3_Field* field;
82   size_t size;
83
84   field = frame->GetField(field_id);
85   if (!field)
86     return 0;
87
88   size = field->Get(buf, sizeof(buf));
89   if (size > 0)
90     return atoi(buf);
91
92   return 0;
93 }
94
95 static int
96 _id3lib_get_data(const ID3_Tag &tag, struct lms_audio_info *info)
97 {
98   ID3_Tag::ConstIterator *itr;
99   const ID3_Frame *frame;
100   int todo;
101
102   todo = 7; /* info fields left to parse: title, artist, album, genre,
103                trackno, rating, playcnt */
104
105   itr = tag.CreateIterator();
106
107   while ((frame = itr->GetNext()) != NULL && todo > 0) {
108     ID3_FrameID fid;
109
110     fid = frame->GetID();
111
112     switch (fid) {
113     case ID3FID_TITLE:
114       if (!info->title.str) {
115         _id3lib_get_string(frame, ID3FN_TEXT, &info->title);
116         if (info->title.str)
117           todo--;
118       }
119       break;
120
121     case ID3FID_LEADARTIST:
122       if (!info->artist.str) {
123         _id3lib_get_string(frame, ID3FN_TEXT, &info->artist);
124         if (info->artist.str)
125           todo--;
126       }
127       break;
128
129     case ID3FID_ALBUM:
130       if (!info->album.str) {
131         _id3lib_get_string(frame, ID3FN_TEXT, &info->album);
132         if (info->album.str)
133           todo--;
134       }
135       break;
136
137     case ID3FID_CONTENTTYPE:
138       if (!info->genre.str) {
139         _id3lib_get_string(frame, ID3FN_TEXT, &info->genre);
140         if (info->genre.str)
141           todo--;
142       }
143       break;
144
145     case ID3FID_TRACKNUM:
146       if (!info->trackno) {
147         info->trackno = _id3lib_get_string_as_int(frame, ID3FN_TEXT);
148         if (info->trackno)
149           todo--;
150       }
151       break;
152
153     case ID3FID_POPULARIMETER:
154       if (!info->rating) {
155         info->rating = frame->GetField(ID3FN_RATING)->Get();
156         if (info->rating)
157           todo--;
158       }
159       if (!info->playcnt) {
160         info->playcnt = frame->GetField(ID3FN_COUNTER)->Get();
161         if (info->playcnt)
162           todo--;
163       }
164       break;
165
166     default:
167       break;
168     }
169   }
170
171   delete itr;
172   return 0;
173 }
174
175 static const char _name[] = "id3lib";
176 static const struct lms_string_size _exts[] = {
177     LMS_STATIC_STRING_SIZE(".mp3")
178 };
179
180 struct plugin {
181     struct lms_plugin plugin;
182     lms_db_audio_t *audio_db;
183 };
184
185 static void *
186 _match(struct plugin *p, const char *path, int len, int base)
187 {
188     int i;
189
190     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
191     if (i < 0)
192       return NULL;
193     else
194       return (void*)(i + 1);
195 }
196
197 static int
198 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
199 {
200     struct lms_audio_info info = {0};
201     ID3_Tag tag;
202     int r;
203
204     tag.Link(finfo->path);
205     r = _id3lib_get_data(tag, &info);
206     if (r != 0)
207       goto done;
208
209     if (!info.title.str) {
210       int ext_idx;
211
212       ext_idx = ((int)match) - 1;
213       info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
214       info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
215       memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
216       info.title.str[info.title.len] = '\0';
217     }
218
219     if (info.title.str)
220       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
221     if (info.artist.str)
222       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
223     if (info.album.str)
224       lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
225     if (info.genre.str)
226       lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
227
228     info.id = finfo->id;
229     r = lms_db_audio_add(plugin->audio_db, &info);
230
231  done:
232     if (info.title.str)
233         free(info.title.str);
234     if (info.artist.str)
235         free(info.artist.str);
236     if (info.album.str)
237         free(info.album.str);
238     if (info.genre.str)
239         free(info.genre.str);
240
241     return r;
242 }
243
244 static int
245 _setup(struct plugin *plugin, struct lms_context *ctxt)
246 {
247     plugin->audio_db = lms_db_audio_new(ctxt->db);
248     if (!plugin->audio_db)
249         return -1;
250
251     return 0;
252 }
253
254 static int
255 _start(struct plugin *plugin, struct lms_context *ctxt)
256 {
257     return lms_db_audio_start(plugin->audio_db);
258 }
259
260 static int
261 _finish(struct plugin *plugin,  struct lms_context *ctxt)
262 {
263     if (plugin->audio_db)
264         return lms_db_audio_free(plugin->audio_db);
265
266     return 0;
267 }
268
269 static int
270 _close(struct plugin *plugin)
271 {
272     free(plugin);
273     return 0;
274 }
275
276 API struct lms_plugin *
277 lms_plugin_open(void)
278 {
279     struct plugin *plugin;
280
281     plugin = (struct plugin *)malloc(sizeof(*plugin));
282     plugin->plugin.name = _name;
283     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
284     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
285     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
286     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
287     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
288     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
289
290     return (struct lms_plugin *)plugin;
291 }
292
293 }