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