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