1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2007 - 2009 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 <memory> |
27 |
#include <vector> |
28 |
#include <gig.h> |
29 |
#include <sqlite3.h> |
30 |
|
31 |
#include "../common/File.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() : FilesTotal(0), FilesScanned(0), Status(0) { } |
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 |
::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 |
* @param insDir if true a separate directory will be created for |
306 |
* the gig file. |
307 |
*/ |
308 |
AddInstrumentsFromFileJob(int JobId, String DbDir, String FilePath, int Index = -1, bool insDir = false); |
309 |
|
310 |
/** |
311 |
* The entry point of the job. |
312 |
*/ |
313 |
virtual void Run(); |
314 |
|
315 |
private: |
316 |
int JobId; |
317 |
String DbDir; |
318 |
String FilePath; |
319 |
int Index; |
320 |
ScanProgress Progress; |
321 |
bool insDir; |
322 |
}; |
323 |
|
324 |
/** |
325 |
* A job which adds all supported instruments in the specified |
326 |
* directory to the specified instruments database directory. |
327 |
*/ |
328 |
class AddInstrumentsJob : public Runnable { |
329 |
public: |
330 |
/** |
331 |
* param JobId the ID of this job. |
332 |
* @param Mode Determines the scanning mode. If RECURSIVE is |
333 |
* specified, all supported instruments in the specified file system |
334 |
* direcotry will be added to the specified instruments database |
335 |
* directory, including the instruments in subdirectories |
336 |
* of the supplied directory. If NON_RECURSIVE is specified, |
337 |
* the instruments in the subdirectories will not be processed. |
338 |
* If FLAT is specified, all supported instruments in the specified |
339 |
* file system direcotry will be added, including the instruments in |
340 |
* subdirectories of the supplied directory, but the respective |
341 |
* subdirectory structure will not be recreated in the instruments |
342 |
* database and all instruments will be added directly in the |
343 |
* specified database directory. |
344 |
* @param DbDir The absolute path name of a directory in the |
345 |
* instruments database in which only the new instruments |
346 |
* (that are not already in the database) will be added. |
347 |
* @param FsDir The absolute path name of a directory in the |
348 |
* file system. |
349 |
* @param insDir If true a separate directory will be created for |
350 |
* each gig file |
351 |
*/ |
352 |
AddInstrumentsJob(int JobId, ScanMode Mode, String DbDir, String FsDir, bool insDir = false); |
353 |
|
354 |
/** |
355 |
* The entry point of the job. |
356 |
*/ |
357 |
virtual void Run(); |
358 |
|
359 |
private: |
360 |
int JobId; |
361 |
ScanMode Mode; |
362 |
String DbDir; |
363 |
String FsDir; |
364 |
ScanProgress Progress; |
365 |
bool insDir; |
366 |
|
367 |
int GetFileCount(); |
368 |
}; |
369 |
|
370 |
class DirectoryScanner: public File::DirectoryWalker { |
371 |
public: |
372 |
/** |
373 |
* Recursively scans all subdirectories of the specified file |
374 |
* system directory and adds the supported instruments to the database. |
375 |
* @param insDir is true this will create a directory for each gig file. |
376 |
* @param pProgress The progress used to monitor the scan process. |
377 |
* @throws Exception - if the specified directories are invalid. |
378 |
*/ |
379 |
void Scan(String DbDir, String FsDir, bool Flat, bool insDir = false, ScanProgress* pProgress = NULL); |
380 |
|
381 |
virtual void DirectoryEntry(std::string Path); |
382 |
virtual void FileEntry(std::string Path) { } |
383 |
|
384 |
private: |
385 |
String DbDir; |
386 |
String FsDir; |
387 |
bool Flat; |
388 |
ScanProgress* pProgress; |
389 |
bool HasInstrumentFiles(String Dir); |
390 |
bool insDir; |
391 |
}; |
392 |
|
393 |
class InstrumentFileCounter: public File::DirectoryWalker { |
394 |
public: |
395 |
/** |
396 |
* Recursively scans all subdirectories of the specified file |
397 |
* system directory and returns the number of supported instrument files. |
398 |
* @param FsDir The file system directory to process. |
399 |
* @throws Exception - if the specified directory is invalid. |
400 |
*/ |
401 |
int Count(String FsDir); |
402 |
|
403 |
virtual void DirectoryEntry(std::string Path) { } |
404 |
virtual void FileEntry(std::string Path); |
405 |
|
406 |
private: |
407 |
int FileCount; |
408 |
}; |
409 |
|
410 |
} // namespace LinuxSampler |
411 |
|
412 |
#endif // __LS_INSTRUMENTSDBUTILITIES_H__ |