Merge git://staff.get-e.org/users/andrunko/lightmediascanner
[lms] / lightmediascanner / src / plugins / video-dummy / video-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  * Video Dummy plugin, just register matched extensions in videos 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[] = "video-dummy";
39 static const struct lms_string_size _exts[] = {
40     LMS_STATIC_STRING_SIZE(".avi"),
41     LMS_STATIC_STRING_SIZE(".mpg"),
42     LMS_STATIC_STRING_SIZE(".mpeg"),
43     LMS_STATIC_STRING_SIZE(".3gp"),
44     LMS_STATIC_STRING_SIZE(".ram"),
45     LMS_STATIC_STRING_SIZE(".ogm"),
46 };
47
48 struct plugin {
49     struct lms_plugin plugin;
50     lms_db_video_t *video_db;
51 };
52
53 static void *
54 _match(struct plugin *p, const char *path, int len, int base)
55 {
56     int i;
57
58     i = lms_which_extension(path, len, _exts, LMS_ARRAY_SIZE(_exts));
59     if (i < 0)
60         return NULL;
61     else
62         return (void*)(i + 1);
63 }
64
65 static int
66 _parse(struct plugin *plugin, struct lms_context *ctxt, const struct lms_file_info *finfo, void *match)
67 {
68     struct lms_video_info info = {0};
69     int r, ext_idx;
70
71     ext_idx = ((int)match) - 1;
72     info.title.len = finfo->path_len - finfo->base - _exts[ext_idx].len;
73     info.title.str = malloc((info.title.len + 1) * sizeof(char));
74     memcpy(info.title.str, finfo->path + finfo->base, info.title.len);
75     info.title.str[info.title.len] = '\0';
76     lms_charset_conv(ctxt->cs_conv, &info.title.str, &info.title.len);
77
78     info.id = finfo->id;
79     r = lms_db_video_add(plugin->video_db, &info);
80
81     free(info.title.str);
82
83     return r;
84 }
85
86 static int
87 _setup(struct plugin *plugin, struct lms_context *ctxt)
88 {
89     plugin->video_db = lms_db_video_new(ctxt->db);
90     if (!plugin->video_db)
91         return -1;
92
93     return 0;
94 }
95
96 static int
97 _start(struct plugin *plugin, struct lms_context *ctxt)
98 {
99     return lms_db_video_start(plugin->video_db);
100 }
101
102 static int
103 _finish(struct plugin *plugin, struct lms_context *ctxt)
104 {
105     if (plugin->video_db)
106         return lms_db_video_free(plugin->video_db);
107
108     return 0;
109 }
110
111
112 static int
113 _close(struct plugin *plugin)
114 {
115     free(plugin);
116     return 0;
117 }
118
119 API struct lms_plugin *
120 lms_plugin_open(void)
121 {
122     struct plugin *plugin;
123
124     plugin = malloc(sizeof(*plugin));
125     plugin->plugin.name = _name;
126     plugin->plugin.match = (lms_plugin_match_fn_t)_match;
127     plugin->plugin.parse = (lms_plugin_parse_fn_t)_parse;
128     plugin->plugin.close = (lms_plugin_close_fn_t)_close;
129     plugin->plugin.setup = (lms_plugin_setup_fn_t)_setup;
130     plugin->plugin.start = (lms_plugin_start_fn_t)_start;
131     plugin->plugin.finish = (lms_plugin_finish_fn_t)_finish;
132
133     return (struct lms_plugin *)plugin;
134 }