Get both lead artist and band, use in order: band, artist
[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 inline static int
96 _id3lib_get_string_if_need(const ID3_Frame *frame, struct lms_string_size *s)
97 {
98   if (!s->str)
99     _id3lib_get_string(frame, ID3FN_TEXT, s);
100
101   return !!s->str;
102 }
103
104 static int
105 _id3lib_get_data(const ID3_Tag &tag, struct lms_audio_info *info)
106 {
107   lms_string_size band = {NULL, 0}, lead_artist = {NULL, 0};
108   ID3_Tag::ConstIterator *itr;
109   const ID3_Frame *frame;
110   int todo;
111
112   todo = 8; /* info fields left to parse: title, lead artist, band,
113                album, genre, trackno, rating, playcnt */
114
115   itr = tag.CreateIterator();
116
117   while ((frame = itr->GetNext()) != NULL && todo > 0) {
118     ID3_FrameID fid;
119
120     fid = frame->GetID();
121
122     switch (fid) {
123     case ID3FID_TITLE:
124       if (_id3lib_get_string_if_need(frame, &info->title))
125           todo--;
126       break;
127
128     case ID3FID_LEADARTIST:
129       if (_id3lib_get_string_if_need(frame, &lead_artist))
130           todo--;
131       break;
132
133     case ID3FID_BAND:
134       if (_id3lib_get_string_if_need(frame, &band))
135           todo--;
136       break;
137
138     case ID3FID_ALBUM:
139       if (_id3lib_get_string_if_need(frame, &info->album))
140           todo--;
141       break;
142
143     case ID3FID_CONTENTTYPE:
144       if (_id3lib_get_string_if_need(frame, &info->genre))
145           todo--;
146       break;
147
148     case ID3FID_TRACKNUM:
149       if (!info->trackno) {
150         info->trackno = _id3lib_get_string_as_int(frame, ID3FN_TEXT);
151         if (info->trackno)
152           todo--;
153       }
154       break;
155
156     case ID3FID_POPULARIMETER:
157       if (!info->rating) {
158         info->rating = frame->GetField(ID3FN_RATING)->Get();
159         if (info->rating)
160           todo--;
161       }
162       if (!info->playcnt) {
163         info->playcnt = frame->GetField(ID3FN_COUNTER)->Get();
164         if (info->playcnt)
165           todo--;
166       }
167       break;
168
169     default:
170       break;
171     }
172   }
173
174   if (band.str && lead_artist.str) {
175     info->artist = band;
176     free(lead_artist.str);
177   } else if (band.str)
178     info->artist = band;
179   else if (lead_artist.str)
180     info->artist = lead_artist;
181
182   delete itr;
183   return 0;
184 }
185
186 static const char _name[] = "id3lib";
187 static const struct lms_string_size _exts[] = {
188     LMS_STATIC_STRING_SIZE(".mp3")
189 };
190
191 struct plugin {
192     struct lms_plugin plugin;
193     lms_db_audio_t *audio_db;
194 };
195
196 static void *
197 _match(struct plugin *p, const char *path, int len, int base)
198 {
199     int i;
200
201     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
202     if (i < 0)
203       return NULL;
204     else
205       return (void*)(i + 1);
206 }
207
208 static int
209 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
210 {
211     struct lms_audio_info info = {0};
212     ID3_Tag tag;
213     int r;
214
215     tag.Link(finfo->path);
216     r = _id3lib_get_data(tag, &info);
217     if (r != 0)
218       goto done;
219
220     if (!info.title.str) {
221       int ext_idx;
222
223       ext_idx = ((int)match) - 1;
224       info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
225       info.title.str = (char *)malloc((info.title.len + 1) * sizeof(char));
226       memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
227       info.title.str[info.title.len] = '\0';
228     }
229
230     if (info.title.str)
231       lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
232     if (info.artist.str)
233       lms_charset_conv(ctxt->cs_conv, &info.artist.str, &info.artist.len);
234     if (info.album.str)
235       lms_charset_conv(ctxt->cs_conv, &info.album.str, &info.album.len);
236     if (info.genre.str)
237       lms_charset_conv(ctxt->cs_conv, &info.genre.str, &info.genre.len);
238
239     info.id = finfo->id;
240     r = lms_db_audio_add(plugin->audio_db, &info);
241
242  done:
243     if (info.title.str)
244         free(info.title.str);
245     if (info.artist.str)
246         free(info.artist.str);
247     if (info.album.str)
248         free(info.album.str);
249     if (info.genre.str)
250         free(info.genre.str);
251
252     return r;
253 }
254
255 static int
256 _setup(struct plugin *plugin, struct lms_context *ctxt)
257 {
258     plugin->audio_db = lms_db_audio_new(ctxt->db);
259     if (!plugin->audio_db)
260         return -1;
261
262     return 0;
263 }
264
265 static int
266 _start(struct plugin *plugin, struct lms_context *ctxt)
267 {
268     return lms_db_audio_start(plugin->audio_db);
269 }
270
271 static int
272 _finish(struct plugin *plugin,  struct lms_context *ctxt)
273 {
274     if (plugin->audio_db)
275         return lms_db_audio_free(plugin->audio_db);
276
277     return 0;
278 }
279
280 static int
281 _close(struct plugin *plugin)
282 {
283     free(plugin);
284     return 0;
285 }
286
287 API struct lms_plugin *
288 lms_plugin_open(void)
289 {
290     struct plugin *plugin;
291
292     plugin = (struct plugin *)malloc(sizeof(*plugin));
293     plugin->plugin.name = _name;
294     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
295     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
296     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
297     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
298     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
299     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
300
301     return (struct lms_plugin *)plugin;
302 }
303
304 }