Updated documentation formatting
[lms] / ruby-lightmediascanner / lightmediascanner_scanner.c
1 /**
2  * Copyright (C) 2007 by Levi Bard
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 Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com>
19  */
20
21 #include "ruby-lightmediascanner.h"
22 #include "lightmediascanner_scanner.h"
23
24 /**
25  * Scanner#new
26  *
27  * constructor
28  *
29  * @param db_path The path to the sqlite3 db to be used
30  *
31  * @return A new Scanner instance
32  */
33 static VALUE scanner_new(VALUE obj, VALUE db_path) {
34         Check_Type(db_path, T_STRING);
35         return Data_Wrap_Struct(lightmediascanner_scanner, 0, lms_free, lms_new(StringValuePtr(db_path)));
36 }// scanner_new
37
38 /**
39  *
40  * Scanner::process
41  *
42  * This will add or update media found in the given directory or its children.
43  *
44  * @param top_path The top directory to scan.
45  *
46  * @return 0 on success
47  *
48  */
49 static VALUE scanner_process(VALUE self, VALUE top_path) {
50         lms_t *lms;
51         int rv = 0;
52         char *ctop_path = NULL;
53
54         Check_Type(top_path, T_STRING);
55         Data_Get_Struct(self, lms_t, lms);
56         ctop_path = StringValuePtr(top_path);
57
58         rv = lms_process(lms, ctop_path);
59
60         if(rv) {
61                 rb_raise(rb_eRuntimeError, "Failure processing %s", ctop_path);
62         }// FAIL
63
64         return INT2FIX(lms_process(lms, StringValuePtr(top_path)));
65 }// scanner_process
66
67 /**
68  *
69  * Scanner::check
70  *
71  * Check consistency of the given directory.
72  *
73  * @param top_path The top directory to scan.
74  *
75  * @return 0 on success
76  *
77  */
78 static VALUE scanner_check(VALUE self, VALUE top_path) {
79         lms_t *lms;
80         int rv = 0;
81         char *ctop_path = NULL;
82
83         Check_Type(top_path, T_STRING);
84         Data_Get_Struct(self, lms_t, lms);
85         ctop_path = StringValuePtr(top_path);
86
87         rv = lms_process(lms, ctop_path);
88
89         if(rv) {
90                 rb_raise(rb_eRuntimeError, "Failure checking %s", ctop_path);
91         }// FAIL
92
93         return INT2FIX(lms_check(lms, StringValuePtr(top_path)));
94 }// scanner_check
95
96 /**
97  *
98  * Scanner::db_path
99  *
100  * Accessor for LMS DB path.
101  *
102  * @return The database path given at creation time
103  *
104  */
105 static VALUE scanner_db_path(VALUE self) {
106         lms_t *lms;
107         Data_Get_Struct(self, lms_t, lms);
108
109         return rb_tainted_str_new2(lms_get_db_path(lms));
110 }// scanner_db_path
111
112 /**
113  *
114  * Scanner::processing?
115  *
116  * Checks if Light Media Scanner is being used in a processing operation like lms_process() or lms_check().
117  *
118  * @return Boolean representing the instance's processing state
119  *
120  */
121 static VALUE scanner_processing(VALUE self) {
122         lms_t *lms;
123         Data_Get_Struct(self, lms_t, lms);
124
125         return (lms_is_processing(lms)? Qtrue: Qfalse);
126 }// scanner_processing
127
128 /**
129  *
130  * Scanner::timeout
131  *
132  * Get the maximum amount of milliseconds the slave can take to serve one file. 
133  *
134  * If a slave takes more than this amount of milliseconds, it will be killed and the scanner will continue with the next file.
135  *
136  * @return The timeout in milliseconds, or -1 on error
137  *
138  */
139 static VALUE scanner_get_slave_timeout(VALUE self) {
140         lms_t *lms;
141         Data_Get_Struct(self, lms_t, lms);
142
143         return INT2FIX(lms_get_slave_timeout(lms));
144 }// scanner_get_slave_timeout
145
146 /**
147  *
148  * Scanner::timeout=
149  *
150  * Set the maximum amount of milliseconds the slave can take to serve one file.
151  *
152  * If a slave takes more than this amount of milliseconds, it will be killed and the scanner will continue with the next file.
153  *
154  * @param timeout_ms The timeout in milliseconds
155  *
156  * @return The new timeout, in milliseconds
157  *
158  */
159 static VALUE scanner_set_slave_timeout(VALUE self, VALUE timeout_ms) {
160         lms_t *lms;
161
162         Check_Type(timeout_ms, T_FIXNUM);
163         Data_Get_Struct(self, lms_t, lms);
164
165         lms_set_slave_timeout(lms, FIX2INT(timeout_ms));
166         return INT2FIX(lms_get_slave_timeout(lms));
167 }// scanner_set_slave_timeout
168
169 /**
170  *
171  * Scanner::commit_interval
172  *
173  * Get the number of files served between database transactions.
174  *
175  * @return The number of files served between database transactions
176  *
177  */
178 static VALUE scanner_get_commit_interval(VALUE self) {
179         lms_t *lms;
180         Data_Get_Struct(self, lms_t, lms);
181
182         return INT2FIX(lms_get_commit_interval(lms));
183 }// scanner_get_commit_interval
184
185 /**
186  *
187  * Scanner::commit_interval=
188  *
189  * Set the number of files to be served between database transactions.
190  *
191  * @param transactions The number of files between commits
192  *
193  * @return The new number of files to be served between database transactions
194  *
195  */
196 static VALUE scanner_set_commit_interval(VALUE self, VALUE transactions) {
197         lms_t *lms;
198
199         Check_Type(transactions, T_FIXNUM);
200         Data_Get_Struct(self, lms_t, lms);
201
202         lms_set_commit_interval(lms, FIX2INT(transactions));
203         return INT2FIX(lms_get_commit_interval(lms));
204 }// scanner_set_commit_interval
205
206 /**
207  *
208  * Scanner::add_charset
209  *
210  * Register a new charset encoding to be used.
211  *
212  * Throws a RuntimeError if LMS is unable to add the charset.
213  *
214  * @param charset charset name as understood by iconv_open(3).
215  *
216  * @return charset
217  *
218  */
219 static VALUE scanner_add_charset(VALUE self, VALUE charset) {
220         lms_t *lms;
221         char *ccharset = NULL;
222
223         Check_Type(charset, T_STRING);
224         Data_Get_Struct(self, lms_t, lms);
225         ccharset = StringValuePtr(charset);
226
227         if(lms_charset_add(lms, ccharset)) {
228                 rb_raise(rb_eRuntimeError, "Unable to add charset %s", ccharset);
229         }//FAIL
230
231         return charset;
232 }// scanner_add_charset
233
234 /**
235  *
236  * Scanner::remove_charset
237  *
238  * Forget about a registered charset encoding.
239  *
240  * Throws a RuntimeError if LMS is unable to remove the charset.
241  *
242  * @param charset charset name as understood by iconv_open(3).
243  *
244  * @return charset
245  *
246  */
247 static VALUE scanner_remove_charset(VALUE self, VALUE charset) {
248         lms_t *lms;
249         char *ccharset = NULL;
250
251         Check_Type(charset, T_STRING);
252         Data_Get_Struct(self, lms_t, lms);
253         ccharset = StringValuePtr(charset);
254         if(lms_charset_del(lms, ccharset)) {
255                 rb_raise(rb_eRuntimeError, "Unable to remove charset %s", ccharset);
256         }//FAIL
257
258         return charset;
259 }// scanner_remove_charset
260
261 /**
262  * LightMediaScanner::Scanner is a class to represent a 
263  * LightMediaScanner instance.
264  */
265 void Init_lightmediascanner_scanner() {
266         lightmediascanner_scanner = rb_define_class_under(lightmediascanner_module, "Scanner", rb_cObject);
267         rb_define_singleton_method(lightmediascanner_scanner, "new", scanner_new, 1);
268         rb_define_method(lightmediascanner_scanner, "process", scanner_process, 1);
269         rb_define_method(lightmediascanner_scanner, "check", scanner_check, 1);
270         rb_define_method(lightmediascanner_scanner, "db_path", scanner_db_path, 0);
271         rb_define_method(lightmediascanner_scanner, "processing?", scanner_processing, 0);
272         rb_define_method(lightmediascanner_scanner, "timeout", scanner_get_slave_timeout, 0);
273         rb_define_method(lightmediascanner_scanner, "timeout=", scanner_set_slave_timeout, 1);
274         rb_define_method(lightmediascanner_scanner, "commit_interval", scanner_get_commit_interval, 0);
275         rb_define_method(lightmediascanner_scanner, "commit_interval=", scanner_set_commit_interval, 1);
276         rb_define_method(lightmediascanner_scanner, "add_charset", scanner_add_charset, 1);
277         rb_define_method(lightmediascanner_scanner, "remove_charset", scanner_remove_charset, 1);
278 }// Init_lightmediascanner