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