ed7682f77fe3531d88d1b88c17e1867a0b6842a2
[lms] / lightmediascanner / src / plugins / audio-dummy / audio-dummy.c
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  * Audio Dummy plugin, just register matched extensions in audios DB.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #define _XOPEN_SOURCE 600
32 #include <lightmediascanner_plugin.h>
33 #include <lightmediascanner_db.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 static const char _name[] = "audio-dummy";
39 static const struct lms_string_size _exts[] = {
40     LMS_STATIC_STRING_SIZE(".m4a"),
41     LMS_STATIC_STRING_SIZE(".aac"),
42     LMS_STATIC_STRING_SIZE(".ra"),
43     LMS_STATIC_STRING_SIZE(".wav")
44 };
45
46 struct plugin {
47     struct lms_plugin plugin;
48     lms_db_audio_t *audio_db;
49 };
50
51 static void *
52 _match(struct plugin *p, const char *path, int len, int base)
53 {
54     int i;
55
56     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
57     if (i < 0)
58         return NULL;
59     else
60         return (void*)(i + 1);
61 }
62
63 static int
64 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
65 {
66     struct lms_audio_info info = {0};
67     int r, ext_idx;
68
69     ext_idx = ((int)match) - 1;
70     info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
71     info.title.str = malloc((info.title.len + 1) * sizeof(char));
72     memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
73     info.title.str[info.title.len] = '\0';
74     lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
75
76     info.id = finfo->id;
77     r = lms_db_audio_add(plugin->audio_db, &info);
78
79     free(info.title.str);
80
81     return r;
82 }
83
84 static int
85 _setup(struct plugin *plugin, struct lms_context *ctxt)
86 {
87     plugin->audio_db = lms_db_audio_new(ctxt->db);
88     if (!plugin->audio_db)
89         return -1;
90
91     return 0;
92 }
93
94 static int
95 _start(struct plugin *plugin, struct lms_context *ctxt)
96 {
97     return lms_db_audio_start(plugin->audio_db);
98 }
99
100 static int
101 _finish(struct plugin *plugin, struct lms_context *ctxt)
102 {
103     if (plugin->audio_db)
104         return lms_db_audio_free(plugin->audio_db);
105
106     return 0;
107 }
108
109
110 static int
111 _close(struct plugin *plugin)
112 {
113     free(plugin);
114     return 0;
115 }
116
117 API struct lms_plugin *
118 lms_plugin_open(void)
119 {
120     struct plugin *plugin;
121
122     plugin = malloc(sizeof(*plugin));
123     plugin->plugin.name = _name;
124     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
125     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
126     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
127     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
128     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
129     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
130
131     return (struct lms_plugin *)plugin;
132 }