2 * Copyright (C) 2007 by Levi Bard
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.
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.
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.
18 * @author Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com>
21 #include "ruby-lightmediascanner.h"
22 #include "lightmediascanner_scanner.h"
29 * @param db_path The path to the sqlite3 db to be used
31 * @return A new Scanner instance
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)));
42 * This will add or update media found in the given directory or its children.
44 * @param top_path The top directory to scan.
46 * @return 0 on success
49 static VALUE scanner_process(VALUE self, VALUE top_path) {
52 char *ctop_path = NULL;
54 Check_Type(top_path, T_STRING);
55 Data_Get_Struct(self, lms_t, lms);
56 ctop_path = StringValuePtr(top_path);
58 rv = lms_process(lms, ctop_path);
61 rb_raise(rb_eRuntimeError, "Failure processing %s", ctop_path);
64 return INT2FIX(lms_process(lms, StringValuePtr(top_path)));
71 * Check consistency of the given directory.
73 * @param top_path The top directory to scan.
75 * @return 0 on success
78 static VALUE scanner_check(VALUE self, VALUE top_path) {
81 char *ctop_path = NULL;
83 Check_Type(top_path, T_STRING);
84 Data_Get_Struct(self, lms_t, lms);
85 ctop_path = StringValuePtr(top_path);
87 rv = lms_process(lms, ctop_path);
90 rb_raise(rb_eRuntimeError, "Failure checking %s", ctop_path);
93 return INT2FIX(lms_check(lms, StringValuePtr(top_path)));
100 * Accessor for LMS DB path.
102 * @return The database path given at creation time
105 static VALUE scanner_db_path(VALUE self) {
107 Data_Get_Struct(self, lms_t, lms);
109 return rb_tainted_str_new2(lms_get_db_path(lms));
114 * Scanner::processing?
116 * Checks if Light Media Scanner is being used in a processing operation like lms_process() or lms_check().
118 * @return Boolean representing the instance's processing state
121 static VALUE scanner_processing(VALUE self) {
123 Data_Get_Struct(self, lms_t, lms);
125 return (lms_is_processing(lms)? Qtrue: Qfalse);
126 }// scanner_processing
132 * Get the maximum amount of milliseconds the slave can take to serve one file.
134 * If a slave takes more than this amount of milliseconds, it will be killed and the scanner will continue with the next file.
136 * @return The timeout in milliseconds, or -1 on error
139 static VALUE scanner_get_slave_timeout(VALUE self) {
141 Data_Get_Struct(self, lms_t, lms);
143 return INT2FIX(lms_get_slave_timeout(lms));
144 }// scanner_get_slave_timeout
150 * Set the maximum amount of milliseconds the slave can take to serve one file.
152 * If a slave takes more than this amount of milliseconds, it will be killed and the scanner will continue with the next file.
154 * @param timeout_ms The timeout in milliseconds
156 * @return The new timeout, in milliseconds
159 static VALUE scanner_set_slave_timeout(VALUE self, VALUE timeout_ms) {
162 Check_Type(timeout_ms, T_FIXNUM);
163 Data_Get_Struct(self, lms_t, lms);
165 lms_set_slave_timeout(lms, FIX2INT(timeout_ms));
166 return INT2FIX(lms_get_slave_timeout(lms));
167 }// scanner_set_slave_timeout
171 * Scanner::commit_interval
173 * Get the number of files served between database transactions.
175 * @return The number of files served between database transactions
178 static VALUE scanner_get_commit_interval(VALUE self) {
180 Data_Get_Struct(self, lms_t, lms);
182 return INT2FIX(lms_get_commit_interval(lms));
183 }// scanner_get_commit_interval
187 * Scanner::commit_interval=
189 * Set the number of files to be served between database transactions.
191 * @param transactions The number of files between commits
193 * @return The new number of files to be served between database transactions
196 static VALUE scanner_set_commit_interval(VALUE self, VALUE transactions) {
199 Check_Type(transactions, T_FIXNUM);
200 Data_Get_Struct(self, lms_t, lms);
202 lms_set_commit_interval(lms, FIX2INT(transactions));
203 return INT2FIX(lms_get_commit_interval(lms));
204 }// scanner_set_commit_interval
208 * Scanner::add_charset
210 * Register a new charset encoding to be used.
212 * Throws a RuntimeError if LMS is unable to add the charset.
214 * @param charset charset name as understood by iconv_open(3).
219 static VALUE scanner_add_charset(VALUE self, VALUE charset) {
221 char *ccharset = NULL;
223 Check_Type(charset, T_STRING);
224 Data_Get_Struct(self, lms_t, lms);
225 ccharset = StringValuePtr(charset);
227 if(lms_charset_add(lms, ccharset)) {
228 rb_raise(rb_eRuntimeError, "Unable to add charset %s", ccharset);
232 }// scanner_add_charset
236 * Scanner::remove_charset
238 * Forget about a registered charset encoding.
240 * Throws a RuntimeError if LMS is unable to remove the charset.
242 * @param charset charset name as understood by iconv_open(3).
247 static VALUE scanner_remove_charset(VALUE self, VALUE charset) {
249 char *ccharset = NULL;
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);
259 }// scanner_remove_charset
262 * LightMediaScanner::Scanner is a class to represent a
263 * LightMediaScanner instance.
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