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