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 |
#if __cplusplus >= 201103L |
108 |
typedef std::unique_ptr<std::vector<int>> IntListPtr; |
109 |
typedef std::unique_ptr<std::vector<String>> StringListPtr; |
110 |
#else |
111 |
typedef std::auto_ptr<std::vector<int> > IntListPtr; |
112 |
typedef std::auto_ptr<std::vector<String> > StringListPtr; |
113 |
#endif |
114 |
|
115 |
class ScanJob { |
116 |
public: |
117 |
int JobId; |
118 |
int FilesTotal; |
119 |
int FilesScanned; |
120 |
String Scanning; |
121 |
int Status; |
122 |
|
123 |
ScanJob() : FilesTotal(0), FilesScanned(0), Status(0) { } |
124 |
ScanJob(const ScanJob& Job) { Copy(Job); } |
125 |
void operator=(const ScanJob& Job) { Copy(Job); } |
126 |
void Copy(const ScanJob&); |
127 |
}; |
128 |
|
129 |
class JobList { |
130 |
public: |
131 |
JobList() { Counter = 0; } |
132 |
|
133 |
/** |
134 |
* Adds the specified job to the list and keeps the list |
135 |
* to have maximum 3 jobs (the oldest one is removed). |
136 |
* @returns The ID used to identify this job. |
137 |
*/ |
138 |
int AddJob(ScanJob Job); |
139 |
|
140 |
/** |
141 |
* Returns the job with ID JobId. |
142 |
* @throws Exception If job with ID JobId doesn't exist. |
143 |
*/ |
144 |
ScanJob& GetJobById(int JobId); |
145 |
|
146 |
private: |
147 |
std::vector<ScanJob> Jobs; |
148 |
int Counter; |
149 |
}; |
150 |
|
151 |
class DirectoryHandler { |
152 |
public: |
153 |
virtual void ProcessDirectory(String Path, int DirId) = 0; |
154 |
}; |
155 |
|
156 |
class AbstractFinder : public DirectoryHandler { |
157 |
public: |
158 |
virtual void ProcessDirectory(String Path, int DirId) = 0; |
159 |
|
160 |
bool IsRegex(String Pattern); |
161 |
void AddSql(String Col, String Pattern, std::stringstream& Sql); |
162 |
|
163 |
protected: |
164 |
std::vector<String> Params; |
165 |
}; |
166 |
|
167 |
class DirectoryFinder : public AbstractFinder { |
168 |
public: |
169 |
DirectoryFinder(SearchQuery* pQuery); |
170 |
~DirectoryFinder(); |
171 |
StringListPtr GetDirectories(); |
172 |
virtual void ProcessDirectory(String Path, int DirId); |
173 |
|
174 |
private: |
175 |
sqlite3_stmt* pStmt; |
176 |
String SqlQuery; |
177 |
SearchQuery* pQuery; |
178 |
StringListPtr pDirectories; |
179 |
}; |
180 |
|
181 |
class InstrumentFinder : public AbstractFinder { |
182 |
public: |
183 |
InstrumentFinder(SearchQuery* pQuery); |
184 |
~InstrumentFinder(); |
185 |
StringListPtr GetInstruments(); |
186 |
virtual void ProcessDirectory(String Path, int DirId); |
187 |
|
188 |
private: |
189 |
sqlite3_stmt* pStmt; |
190 |
String SqlQuery; |
191 |
SearchQuery* pQuery; |
192 |
StringListPtr pInstruments; |
193 |
}; |
194 |
|
195 |
class DirectoryCounter : public DirectoryHandler { |
196 |
public: |
197 |
DirectoryCounter() { count = 0; } |
198 |
virtual void ProcessDirectory(String Path, int DirId); |
199 |
int GetDirectoryCount() { return count; } |
200 |
|
201 |
private: |
202 |
int count; |
203 |
}; |
204 |
|
205 |
class InstrumentCounter : public DirectoryHandler { |
206 |
public: |
207 |
InstrumentCounter() { count = 0; } |
208 |
virtual void ProcessDirectory(String Path, int DirId); |
209 |
int GetInstrumentCount() { return count; } |
210 |
|
211 |
private: |
212 |
int count; |
213 |
}; |
214 |
|
215 |
class DirectoryCopier : public DirectoryHandler { |
216 |
public: |
217 |
DirectoryCopier(String SrcParentDir, String DestDir); |
218 |
virtual void ProcessDirectory(String Path, int DirId); |
219 |
|
220 |
private: |
221 |
String SrcParentDir; |
222 |
String DestDir; |
223 |
}; |
224 |
|
225 |
/** |
226 |
* Used to monitor the scan progress of instrument files |
227 |
*/ |
228 |
class ScanProgress { |
229 |
public: |
230 |
ScanProgress(); |
231 |
|
232 |
/** |
233 |
* Invoked when there is some progress on the monitored task. |
234 |
*/ |
235 |
void StatusChanged(); |
236 |
|
237 |
/** |
238 |
* Gets the total number of files scheduled for scanning. |
239 |
*/ |
240 |
int GetTotalFileCount(); |
241 |
|
242 |
/** |
243 |
* Sets the total number of files scheduled for scanning. |
244 |
*/ |
245 |
void SetTotalFileCount(int Count); |
246 |
|
247 |
/** |
248 |
* Gets the current nuber of scanned files. |
249 |
*/ |
250 |
int GetScannedFileCount(); |
251 |
|
252 |
/** |
253 |
* Sets the current nuber of scanned files. |
254 |
*/ |
255 |
void SetScannedFileCount(int Count); |
256 |
|
257 |
/** |
258 |
* Returns an integer value between 0 and 100 indicating the |
259 |
* scanning progress percentage of the file which is currently |
260 |
* being scanned. Negative value indicates an error. |
261 |
*/ |
262 |
int GetStatus(); |
263 |
|
264 |
/** |
265 |
* Sets the scanning progress percentage of the file which is |
266 |
* currently being scanned. If negative value is specified, the |
267 |
* status is set to 0. If value greater then 100 is specified, |
268 |
* the status is set to 100. |
269 |
*/ |
270 |
void SetStatus(int Status); |
271 |
|
272 |
/** |
273 |
* Sets and error code to indicate task failure. |
274 |
* @param Err Should be negative value. If positive value |
275 |
* is suplied, it is reverted to negative. |
276 |
*/ |
277 |
void SetErrorStatus(int Err); |
278 |
|
279 |
/** |
280 |
* The absolute path name of the file which is currently being scanned. |
281 |
*/ |
282 |
String CurrentFile; |
283 |
|
284 |
int JobId; |
285 |
::gig::progress_t GigFileProgress; |
286 |
|
287 |
private: |
288 |
int TotalFileCount; |
289 |
int ScannedFileCount; |
290 |
int Status; |
291 |
|
292 |
static void GigFileProgressCallback(::gig::progress_t* pProgress); |
293 |
}; |
294 |
|
295 |
/** |
296 |
* A job which adds the instruments in the specified |
297 |
* file to the specified instruments database directory. |
298 |
*/ |
299 |
class AddInstrumentsFromFileJob : public Runnable { |
300 |
public: |
301 |
/** |
302 |
* param JobId the ID of this job. |
303 |
* @param DbDir The absolute path name of a directory in the |
304 |
* instruments database in which only the new instruments |
305 |
* (that are not already in the database) will be added. |
306 |
* @param FilePath The absolute path name of the instrument file. |
307 |
* @param Index The index of the instrument (in the given |
308 |
* instrument file) to add. If -1 is specified, all instruments in |
309 |
* the supplied instrument file will be added. |
310 |
* @param insDir if true a separate directory will be created for |
311 |
* the gig file. |
312 |
*/ |
313 |
AddInstrumentsFromFileJob(int JobId, String DbDir, String FilePath, int Index = -1, bool insDir = false); |
314 |
|
315 |
/** |
316 |
* The entry point of the job. |
317 |
*/ |
318 |
virtual void Run(); |
319 |
|
320 |
private: |
321 |
int JobId; |
322 |
String DbDir; |
323 |
String FilePath; |
324 |
int Index; |
325 |
ScanProgress Progress; |
326 |
bool insDir; |
327 |
}; |
328 |
|
329 |
/** |
330 |
* A job which adds all supported instruments in the specified |
331 |
* directory to the specified instruments database directory. |
332 |
*/ |
333 |
class AddInstrumentsJob : public Runnable { |
334 |
public: |
335 |
/** |
336 |
* param JobId the ID of this job. |
337 |
* @param Mode Determines the scanning mode. If RECURSIVE is |
338 |
* specified, all supported instruments in the specified file system |
339 |
* direcotry will be added to the specified instruments database |
340 |
* directory, including the instruments in subdirectories |
341 |
* of the supplied directory. If NON_RECURSIVE is specified, |
342 |
* the instruments in the subdirectories will not be processed. |
343 |
* If FLAT is specified, all supported instruments in the specified |
344 |
* file system direcotry will be added, including the instruments in |
345 |
* subdirectories of the supplied directory, but the respective |
346 |
* subdirectory structure will not be recreated in the instruments |
347 |
* database and all instruments will be added directly in the |
348 |
* specified database directory. |
349 |
* @param DbDir The absolute path name of a directory in the |
350 |
* instruments database in which only the new instruments |
351 |
* (that are not already in the database) will be added. |
352 |
* @param FsDir The absolute path name of a directory in the |
353 |
* file system. |
354 |
* @param insDir If true a separate directory will be created for |
355 |
* each gig file |
356 |
*/ |
357 |
AddInstrumentsJob(int JobId, ScanMode Mode, String DbDir, String FsDir, bool insDir = false); |
358 |
|
359 |
/** |
360 |
* The entry point of the job. |
361 |
*/ |
362 |
virtual void Run(); |
363 |
|
364 |
private: |
365 |
int JobId; |
366 |
ScanMode Mode; |
367 |
String DbDir; |
368 |
String FsDir; |
369 |
ScanProgress Progress; |
370 |
bool insDir; |
371 |
|
372 |
int GetFileCount(); |
373 |
}; |
374 |
|
375 |
class DirectoryScanner: public File::DirectoryWalker { |
376 |
public: |
377 |
/** |
378 |
* Recursively scans all subdirectories of the specified file |
379 |
* system directory and adds the supported instruments to the database. |
380 |
* @param insDir is true this will create a directory for each gig file. |
381 |
* @param pProgress The progress used to monitor the scan process. |
382 |
* @throws Exception - if the specified directories are invalid. |
383 |
*/ |
384 |
void Scan(String DbDir, String FsDir, bool Flat, bool insDir = false, ScanProgress* pProgress = NULL); |
385 |
|
386 |
virtual void DirectoryEntry(std::string Path); |
387 |
virtual void FileEntry(std::string Path) { } |
388 |
|
389 |
private: |
390 |
String DbDir; |
391 |
String FsDir; |
392 |
bool Flat; |
393 |
ScanProgress* pProgress; |
394 |
bool HasInstrumentFiles(String Dir); |
395 |
bool insDir; |
396 |
}; |
397 |
|
398 |
class InstrumentFileCounter: public File::DirectoryWalker { |
399 |
public: |
400 |
/** |
401 |
* Recursively scans all subdirectories of the specified file |
402 |
* system directory and returns the number of supported instrument files. |
403 |
* @param FsDir The file system directory to process. |
404 |
* @throws Exception - if the specified directory is invalid. |
405 |
*/ |
406 |
int Count(String FsDir); |
407 |
|
408 |
virtual void DirectoryEntry(std::string Path) { } |
409 |
virtual void FileEntry(std::string Path); |
410 |
|
411 |
private: |
412 |
int FileCount; |
413 |
}; |
414 |
|
415 |
} // namespace LinuxSampler |
416 |
|
417 |
#endif // __LS_INSTRUMENTSDBUTILITIES_H__ |