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