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