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 "lightmediascanner_scanner.h"
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
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)));
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
42 static VALUE scanner_process(VALUE self, VALUE top_path) {
45 char *ctop_path = NULL;
47 Check_Type(top_path, T_STRING);
48 Data_Get_Struct(self, lms_t, lms);
49 ctop_path = StringValuePtr(top_path);
51 rv = lms_process(lms, ctop_path);
54 rb_raise(rb_eRuntimeError, "Failure processing %s", ctop_path);
57 return INT2FIX(lms_process(lms, StringValuePtr(top_path)));
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
67 static VALUE scanner_check(VALUE self, VALUE top_path) {
70 char *ctop_path = NULL;
72 Check_Type(top_path, T_STRING);
73 Data_Get_Struct(self, lms_t, lms);
74 ctop_path = StringValuePtr(top_path);
76 rv = lms_process(lms, ctop_path);
79 rb_raise(rb_eRuntimeError, "Failure checking %s", ctop_path);
82 return INT2FIX(lms_check(lms, StringValuePtr(top_path)));
87 * Accessor for LMS DB path.
88 * @param self The current Scanner instance
89 * @return The database path given at creation time
91 static VALUE scanner_db_path(VALUE self) {
93 Data_Get_Struct(self, lms_t, lms);
95 return rb_tainted_str_new2(lms_get_db_path(lms));
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
104 static VALUE scanner_processing(VALUE self) {
106 Data_Get_Struct(self, lms_t, lms);
108 return (lms_is_processing(lms)? Qtrue: Qfalse);
109 }// scanner_processing
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
118 static VALUE scanner_get_slave_timeout(VALUE self) {
120 Data_Get_Struct(self, lms_t, lms);
122 return INT2FIX(lms_get_slave_timeout(lms));
123 }// scanner_get_slave_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
133 static VALUE scanner_set_slave_timeout(VALUE self, VALUE timeout_ms) {
136 Check_Type(timeout_ms, T_FIXNUM);
137 Data_Get_Struct(self, lms_t, lms);
139 lms_set_slave_timeout(lms, FIX2INT(timeout_ms));
140 return INT2FIX(lms_get_slave_timeout(lms));
141 }// scanner_set_slave_timeout
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
149 static VALUE scanner_get_commit_interval(VALUE self) {
151 Data_Get_Struct(self, lms_t, lms);
153 return INT2FIX(lms_get_commit_interval(lms));
154 }// scanner_get_commit_interval
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
163 static VALUE scanner_set_commit_interval(VALUE self, VALUE transactions) {
166 Check_Type(transactions, T_FIXNUM);
167 Data_Get_Struct(self, lms_t, lms);
169 lms_set_commit_interval(lms, FIX2INT(transactions));
170 return INT2FIX(lms_get_commit_interval(lms));
171 }// scanner_set_commit_interval
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).
181 static VALUE scanner_add_charset(VALUE self, VALUE charset) {
183 char *ccharset = NULL;
185 Check_Type(charset, T_STRING);
186 Data_Get_Struct(self, lms_t, lms);
187 ccharset = StringValuePtr(charset);
189 if(lms_charset_add(lms, ccharset)) {
190 rb_raise(rb_eRuntimeError, "Unable to add charset %s", ccharset);
194 }// scanner_add_charset
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).
204 static VALUE scanner_remove_charset(VALUE self, VALUE charset) {
206 char *ccharset = NULL;
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);
216 }// scanner_remove_charset
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