1 |
iliev |
1200 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* Copyright (C) 2007 Grigor Iliev * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the Free Software * |
17 |
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
|
|
* MA 02110-1301 USA * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
|
21 |
|
|
#include "../common/global.h" |
22 |
|
|
|
23 |
|
|
#if HAVE_SQLITE3 |
24 |
|
|
|
25 |
|
|
#ifndef __LS_INSTRUMENTSDBUTILITIES_H__ |
26 |
|
|
#define __LS_INSTRUMENTSDBUTILITIES_H__ |
27 |
|
|
|
28 |
|
|
#include <vector> |
29 |
|
|
#include <gig.h> |
30 |
|
|
#include <sqlite3.h> |
31 |
|
|
#include <sys/stat.h> |
32 |
|
|
|
33 |
|
|
namespace LinuxSampler { |
34 |
|
|
|
35 |
|
|
class DbInstrument { |
36 |
|
|
public: |
37 |
|
|
String InstrFile; |
38 |
|
|
int InstrNr; |
39 |
|
|
String FormatFamily; |
40 |
|
|
String FormatVersion; |
41 |
|
|
long long int Size; |
42 |
|
|
String Created; |
43 |
|
|
String Modified; |
44 |
|
|
String Description; |
45 |
|
|
bool IsDrum; |
46 |
|
|
String Product; |
47 |
|
|
String Artists; |
48 |
|
|
String Keywords; |
49 |
|
|
|
50 |
|
|
DbInstrument() { } |
51 |
|
|
DbInstrument(const DbInstrument& Instr) { Copy(Instr); } |
52 |
|
|
void operator=(const DbInstrument& Instr) { Copy(Instr); } |
53 |
|
|
void Copy(const DbInstrument&); |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
class DbDirectory { |
57 |
|
|
public: |
58 |
|
|
String Created; |
59 |
|
|
String Modified; |
60 |
|
|
String Description; |
61 |
|
|
|
62 |
|
|
DbDirectory() { } |
63 |
|
|
DbDirectory(const DbDirectory& Dir) { Copy(Dir); } |
64 |
|
|
void operator=(const DbDirectory& Dir) { Copy(Dir); } |
65 |
|
|
void Copy(const DbDirectory&); |
66 |
|
|
}; |
67 |
|
|
|
68 |
|
|
class SearchQuery { |
69 |
|
|
public: |
70 |
|
|
enum InstrumentType { |
71 |
|
|
CHROMATIC = 0, |
72 |
|
|
DRUM = 1, |
73 |
|
|
BOTH = 2 |
74 |
|
|
}; |
75 |
|
|
|
76 |
|
|
String Name; |
77 |
|
|
std::vector<String> FormatFamilies; |
78 |
|
|
long long MinSize; |
79 |
|
|
long long MaxSize; |
80 |
|
|
String CreatedBefore; |
81 |
|
|
String CreatedAfter; |
82 |
|
|
String ModifiedBefore; |
83 |
|
|
String ModifiedAfter; |
84 |
|
|
String Description; |
85 |
|
|
String Product; |
86 |
|
|
String Artists; |
87 |
|
|
String Keywords; |
88 |
|
|
InstrumentType InstrType; |
89 |
|
|
|
90 |
|
|
SearchQuery(); |
91 |
|
|
void SetFormatFamilies(String s); |
92 |
|
|
void SetSize(String s); |
93 |
|
|
void SetCreated(String s); |
94 |
|
|
void SetModified(String s); |
95 |
|
|
|
96 |
|
|
private: |
97 |
|
|
String GetMin(String s); |
98 |
|
|
String GetMax(String s); |
99 |
|
|
}; |
100 |
|
|
|
101 |
|
|
enum ScanMode { |
102 |
|
|
RECURSIVE = 0, |
103 |
|
|
NON_RECURSIVE = 1, |
104 |
|
|
FLAT = 2 |
105 |
|
|
}; |
106 |
|
|
|
107 |
|
|
typedef std::auto_ptr<std::vector<int> > IntListPtr; |
108 |
|
|
typedef std::auto_ptr<std::vector<String> > StringListPtr; |
109 |
|
|
|
110 |
|
|
class ScanJob { |
111 |
|
|
public: |
112 |
|
|
int JobId; |
113 |
|
|
int FilesTotal; |
114 |
|
|
int FilesScanned; |
115 |
|
|
String Scanning; |
116 |
|
|
int Status; |
117 |
|
|
|
118 |
|
|
ScanJob() { } |
119 |
|
|
ScanJob(const ScanJob& Job) { Copy(Job); } |
120 |
|
|
void operator=(const ScanJob& Job) { Copy(Job); } |
121 |
|
|
void Copy(const ScanJob&); |
122 |
|
|
}; |
123 |
|
|
|
124 |
|
|
class JobList { |
125 |
|
|
public: |
126 |
|
|
JobList() { Counter = 0; } |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* Adds the specified job to the list and keeps the list |
130 |
|
|
* to have maximum 3 jobs (the oldest one is removed). |
131 |
|
|
* @returns The ID used to identify this job. |
132 |
|
|
*/ |
133 |
|
|
int AddJob(ScanJob Job); |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* Returns the job with ID JobId. |
137 |
|
|
* @throws Exception If job with ID JobId doesn't exist. |
138 |
|
|
*/ |
139 |
|
|
ScanJob& GetJobById(int JobId); |
140 |
|
|
|
141 |
|
|
private: |
142 |
|
|
std::vector<ScanJob> Jobs; |
143 |
|
|
int Counter; |
144 |
|
|
}; |
145 |
|
|
|
146 |
|
|
class DirectoryHandler { |
147 |
|
|
public: |
148 |
|
|
virtual void ProcessDirectory(String Path, int DirId) = 0; |
149 |
|
|
}; |
150 |
|
|
|
151 |
|
|
class AbstractFinder : public DirectoryHandler { |
152 |
|
|
public: |
153 |
|
|
virtual void ProcessDirectory(String Path, int DirId) = 0; |
154 |
|
|
|
155 |
|
|
bool IsRegex(String Pattern); |
156 |
|
|
void AddSql(String Col, String Pattern, std::stringstream& Sql); |
157 |
|
|
|
158 |
|
|
protected: |
159 |
|
|
std::vector<String> Params; |
160 |
|
|
}; |
161 |
|
|
|
162 |
|
|
class DirectoryFinder : public AbstractFinder { |
163 |
|
|
public: |
164 |
|
|
DirectoryFinder(SearchQuery* pQuery); |
165 |
|
|
~DirectoryFinder(); |
166 |
|
|
StringListPtr GetDirectories(); |
167 |
|
|
virtual void ProcessDirectory(String Path, int DirId); |
168 |
|
|
|
169 |
|
|
private: |
170 |
|
|
sqlite3_stmt* pStmt; |
171 |
|
|
String SqlQuery; |
172 |
|
|
SearchQuery* pQuery; |
173 |
|
|
StringListPtr pDirectories; |
174 |
|
|
}; |
175 |
|
|
|
176 |
|
|
class InstrumentFinder : public AbstractFinder { |
177 |
|
|
public: |
178 |
|
|
InstrumentFinder(SearchQuery* pQuery); |
179 |
|
|
~InstrumentFinder(); |
180 |
|
|
StringListPtr GetInstruments(); |
181 |
|
|
virtual void ProcessDirectory(String Path, int DirId); |
182 |
|
|
|
183 |
|
|
private: |
184 |
|
|
sqlite3_stmt* pStmt; |
185 |
|
|
String SqlQuery; |
186 |
|
|
SearchQuery* pQuery; |
187 |
|
|
StringListPtr pInstruments; |
188 |
|
|
}; |
189 |
|
|
|
190 |
|
|
class DirectoryCounter : public DirectoryHandler { |
191 |
|
|
public: |
192 |
|
|
DirectoryCounter() { count = 0; } |
193 |
|
|
virtual void ProcessDirectory(String Path, int DirId); |
194 |
|
|
int GetDirectoryCount() { return count; } |
195 |
|
|
|
196 |
|
|
private: |
197 |
|
|
int count; |
198 |
|
|
}; |
199 |
|
|
|
200 |
|
|
class InstrumentCounter : public DirectoryHandler { |
201 |
|
|
public: |
202 |
|
|
InstrumentCounter() { count = 0; } |
203 |
|
|
virtual void ProcessDirectory(String Path, int DirId); |
204 |
|
|
int GetInstrumentCount() { return count; } |
205 |
|
|
|
206 |
|
|
private: |
207 |
|
|
int count; |
208 |
|
|
}; |
209 |
|
|
|
210 |
|
|
class DirectoryCopier : public DirectoryHandler { |
211 |
|
|
public: |
212 |
|
|
DirectoryCopier(String SrcParentDir, String DestDir); |
213 |
|
|
virtual void ProcessDirectory(String Path, int DirId); |
214 |
|
|
|
215 |
|
|
private: |
216 |
|
|
String SrcParentDir; |
217 |
|
|
String DestDir; |
218 |
|
|
}; |
219 |
|
|
|
220 |
|
|
/** |
221 |
|
|
* Used to monitor the scan progress of instrument files |
222 |
|
|
*/ |
223 |
|
|
class ScanProgress { |
224 |
|
|
public: |
225 |
|
|
ScanProgress(); |
226 |
|
|
|
227 |
|
|
/** |
228 |
|
|
* Invoked when there is some progress on the monitored task. |
229 |
|
|
*/ |
230 |
|
|
void StatusChanged(); |
231 |
|
|
|
232 |
|
|
/** |
233 |
|
|
* Gets the total number of files scheduled for scanning. |
234 |
|
|
*/ |
235 |
|
|
int GetTotalFileCount(); |
236 |
|
|
|
237 |
|
|
/** |
238 |
|
|
* Sets the total number of files scheduled for scanning. |
239 |
|
|
*/ |
240 |
|
|
void SetTotalFileCount(int Count); |
241 |
|
|
|
242 |
|
|
/** |
243 |
|
|
* Gets the current nuber of scanned files. |
244 |
|
|
*/ |
245 |
|
|
int GetScannedFileCount(); |
246 |
|
|
|
247 |
|
|
/** |
248 |
|
|
* Sets the current nuber of scanned files. |
249 |
|
|
*/ |
250 |
|
|
void SetScannedFileCount(int Count); |
251 |
|
|
|
252 |
|
|
/** |
253 |
|
|
* Returns an integer value between 0 and 100 indicating the |
254 |
|
|
* scanning progress percentage of the file which is currently |
255 |
|
|
* being scanned. Negative value indicates an error. |
256 |
|
|
*/ |
257 |
|
|
int GetStatus(); |
258 |
|
|
|
259 |
|
|
/** |
260 |
|
|
* Sets the scanning progress percentage of the file which is |
261 |
|
|
* currently being scanned. If negative value is specified, the |
262 |
|
|
* status is set to 0. If value greater then 100 is specified, |
263 |
|
|
* the status is set to 100. |
264 |
|
|
*/ |
265 |
|
|
void SetStatus(int Status); |
266 |
|
|
|
267 |
|
|
/** |
268 |
|
|
* Sets and error code to indicate task failure. |
269 |
|
|
* @param Err Should be negative value. If positive value |
270 |
|
|
* is suplied, it is reverted to negative. |
271 |
|
|
*/ |
272 |
|
|
void SetErrorStatus(int Err); |
273 |
|
|
|
274 |
|
|
/** |
275 |
|
|
* The absolute path name of the file which is currently being scanned. |
276 |
|
|
*/ |
277 |
|
|
String CurrentFile; |
278 |
|
|
|
279 |
|
|
int JobId; |
280 |
|
|
struct ::gig::progress_t GigFileProgress; |
281 |
|
|
|
282 |
|
|
private: |
283 |
|
|
int TotalFileCount; |
284 |
|
|
int ScannedFileCount; |
285 |
|
|
int Status; |
286 |
|
|
|
287 |
|
|
static void GigFileProgressCallback(::gig::progress_t* pProgress); |
288 |
|
|
}; |
289 |
|
|
|
290 |
|
|
/** |
291 |
|
|
* A job which adds the instruments in the specified |
292 |
|
|
* file to the specified instruments database directory. |
293 |
|
|
*/ |
294 |
|
|
class AddInstrumentsFromFileJob : public Runnable { |
295 |
|
|
public: |
296 |
|
|
/** |
297 |
|
|
* param JobId the ID of this job. |
298 |
|
|
* @param DbDir The absolute path name of a directory in the |
299 |
|
|
* instruments database in which only the new instruments |
300 |
|
|
* (that are not already in the database) will be added. |
301 |
|
|
* @param FilePath The absolute path name of the instrument file. |
302 |
|
|
* @param Index The index of the instrument (in the given |
303 |
|
|
* instrument file) to add. If -1 is specified, all instruments in |
304 |
|
|
* the supplied instrument file will be added. |
305 |
|
|
*/ |
306 |
|
|
AddInstrumentsFromFileJob(int JobId, String DbDir, String FilePath, int Index = -1); |
307 |
|
|
|
308 |
|
|
/** |
309 |
|
|
* The entry point of the job. |
310 |
|
|
*/ |
311 |
|
|
virtual void Run(); |
312 |
|
|
|
313 |
|
|
private: |
314 |
|
|
int JobId; |
315 |
|
|
String DbDir; |
316 |
|
|
String FilePath; |
317 |
|
|
int Index; |
318 |
|
|
ScanProgress Progress; |
319 |
|
|
}; |
320 |
|
|
|
321 |
|
|
/** |
322 |
|
|
* A job which adds all supported instruments in the specified |
323 |
|
|
* directory to the specified instruments database directory. |
324 |
|
|
*/ |
325 |
|
|
class AddInstrumentsJob : public Runnable { |
326 |
|
|
public: |
327 |
|
|
/** |
328 |
|
|
* param JobId the ID of this job. |
329 |
|
|
* @param Mode Determines the scanning mode. If RECURSIVE is |
330 |
|
|
* specified, all supported instruments in the specified file system |
331 |
|
|
* direcotry will be added to the specified instruments database |
332 |
|
|
* directory, including the instruments in subdirectories |
333 |
|
|
* of the supplied directory. If NON_RECURSIVE is specified, |
334 |
|
|
* the instruments in the subdirectories will not be processed. |
335 |
|
|
* If FLAT is specified, all supported instruments in the specified |
336 |
|
|
* file system direcotry will be added, including the instruments in |
337 |
|
|
* subdirectories of the supplied directory, but the respective |
338 |
|
|
* subdirectory structure will not be recreated in the instruments |
339 |
|
|
* database and all instruments will be added directly in the |
340 |
|
|
* specified database directory. |
341 |
|
|
* @param DbDir The absolute path name of a directory in the |
342 |
|
|
* instruments database in which only the new instruments |
343 |
|
|
* (that are not already in the database) will be added. |
344 |
|
|
* @param FsDir The absolute path name of a directory in the |
345 |
|
|
* file system. |
346 |
|
|
*/ |
347 |
|
|
AddInstrumentsJob(int JobId, ScanMode Mode, String DbDir, String FsDir); |
348 |
|
|
|
349 |
|
|
/** |
350 |
|
|
* The entry point of the job. |
351 |
|
|
*/ |
352 |
|
|
virtual void Run(); |
353 |
|
|
|
354 |
|
|
private: |
355 |
|
|
int JobId; |
356 |
|
|
ScanMode Mode; |
357 |
|
|
String DbDir; |
358 |
|
|
String FsDir; |
359 |
|
|
ScanProgress Progress; |
360 |
|
|
|
361 |
|
|
int GetFileCount(); |
362 |
|
|
}; |
363 |
|
|
|
364 |
|
|
class DirectoryScanner { |
365 |
|
|
public: |
366 |
|
|
/** |
367 |
|
|
* Recursively scans all subdirectories of the specified file |
368 |
|
|
* system directory and adds the supported instruments to the database. |
369 |
|
|
* @param pProgress The progress used to monitor the scan process. |
370 |
|
|
* @throws Exception - if the specified directories are invalid. |
371 |
|
|
*/ |
372 |
|
|
static void Scan(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress = NULL); |
373 |
|
|
|
374 |
|
|
private: |
375 |
|
|
static String DbDir; |
376 |
|
|
static String FsDir; |
377 |
|
|
static bool Flat; |
378 |
|
|
static ScanProgress* pProgress; |
379 |
|
|
static int FtwCallback(const char* fpath, const struct stat* sb, int typeflag); |
380 |
|
|
}; |
381 |
|
|
|
382 |
|
|
class InstrumentFileCounter { |
383 |
|
|
public: |
384 |
|
|
/** |
385 |
|
|
* Recursively scans all subdirectories of the specified file |
386 |
|
|
* system directory and returns the number of supported instruments. |
387 |
|
|
* @param FsDir The file system directory to process. |
388 |
|
|
* @throws Exception - if the specified directory is invalid. |
389 |
|
|
*/ |
390 |
|
|
static int Count(String FsDir); |
391 |
|
|
|
392 |
|
|
static int FtwCallback(const char* fpath, const struct stat* sb, int typeflag); |
393 |
|
|
|
394 |
|
|
private: |
395 |
|
|
static int FileCount; |
396 |
|
|
}; |
397 |
|
|
|
398 |
|
|
} // namespace LinuxSampler |
399 |
|
|
|
400 |
|
|
#endif // __LS_INSTRUMENTSDBUTILITIES_H__ |
401 |
|
|
|
402 |
|
|
#endif // HAVE_SQLITE3 |