/[svn]/linuxsampler/trunk/src/db/InstrumentsDb.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/db/InstrumentsDb.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1161 - (show annotations) (download) (as text)
Mon Apr 16 15:51:18 2007 UTC (16 years, 11 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 27848 byte(s)
* Implemented instruments database

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.h"
22
23 #if HAVE_SQLITE3
24
25 #ifndef __LS_INSTRUMENTSDB_H__
26 #define __LS_INSTRUMENTSDB_H__
27
28 #include <sqlite3.h>
29 #include <vector>
30 #include <sys/stat.h>
31 #include <gig.h>
32 #include "../common/Mutex.h"
33 #include "../EventListeners.h"
34
35 namespace LinuxSampler {
36
37 class DbInstrument {
38 public:
39 String InstrFile;
40 int InstrNr;
41 String FormatFamily;
42 String FormatVersion;
43 long long int Size;
44 String Created;
45 String Modified;
46 String Description;
47 bool IsDrum;
48 String Product;
49 String Artists;
50 String Keywords;
51
52 DbInstrument() { }
53 DbInstrument(const DbInstrument& Instr) { Copy(Instr); }
54 void operator=(const DbInstrument& Instr) { Copy(Instr); }
55 void Copy(const DbInstrument&);
56 };
57
58 class DbDirectory {
59 public:
60 String Created;
61 String Modified;
62 String Description;
63
64 DbDirectory() { }
65 DbDirectory(const DbDirectory& Dir) { Copy(Dir); }
66 void operator=(const DbDirectory& Dir) { Copy(Dir); }
67 void Copy(const DbDirectory&);
68 };
69
70 typedef std::auto_ptr<std::vector<int> > IntListPtr;
71 typedef std::auto_ptr<std::vector<String> > StringListPtr;
72
73 /**
74 * @brief Provides access to the instruments database.
75 */
76 class InstrumentsDb {
77 public:
78
79 /**
80 * This class is used as a listener, which is notified when
81 * changes to the instruments database are made.
82 */
83 class Listener {
84 public:
85 /**
86 * Invoked when the number of instrument directories
87 * in a specific directory has changed.
88 * @param Dir The absolute pathname of the directory in
89 * which the number of directories is changed.
90 */
91 virtual void DirectoryCountChanged(String Dir) = 0;
92
93 /**
94 * Invoked when the settings of an instrument directory
95 * are changed.
96 * @param Dir The absolute pathname of the directory
97 * whose settings are changed.
98 */
99 virtual void DirectoryInfoChanged(String Dir) = 0;
100
101 /**
102 * Invoked when an instrument directory is renamed.
103 * @param Dir The old absolute pathname of the directory.
104 * @param NewName The new name of the directory.
105 */
106 virtual void DirectoryNameChanged(String Dir, String NewName) = 0;
107
108 /**
109 * Invoked when the number of instruments
110 * in a specific directory has changed.
111 * @param Dir The absolute pathname of the directory in
112 * which the number of instruments is changed.
113 */
114 virtual void InstrumentCountChanged(String Dir) = 0;
115
116 /**
117 * Invoked when the settings of an instrument are changed.
118 * @param Instr The absolute pathname of the instrument
119 * whose settings are changed.
120 */
121 virtual void InstrumentInfoChanged(String Instr) = 0;
122
123 /**
124 * Invoked when an instrument is renamed.
125 * @param Instr The old absolute pathname of the instrument.
126 * @param NewName The new name of the directory.
127 */
128 virtual void InstrumentNameChanged(String Instr, String NewName) = 0;
129 };
130
131 /**
132 * Registers the specified listener to be notified
133 * when changes to the instruments database are made.
134 */
135 void AddInstrumentsDbListener(InstrumentsDb::Listener* l);
136
137 /**
138 * Removes the specified listener.
139 */
140 void RemoveInstrumentsDbListener(InstrumentsDb::Listener* l);
141
142 /**
143 * This method is used to access the instruments database.
144 */
145 static InstrumentsDb* GetInstrumentsDb();
146
147 /**
148 * Sets the absolute path name of the instruments database file to use.
149 * The instruments database file location should be set before
150 * any attempt to access the database and once set could not be changed.
151 * @throws Exception - if an empty string is provided or if
152 * the method is called more than once.
153 */
154 void SetDbFile(String File);
155
156 /**
157 * Gets the number of directories in the specified directory.
158 * @param Dir The absolute path name of the directory.
159 * @throws Exception - if database error occurs, or
160 * the specified path name is invalid.
161 * @returns The number of directories in the specified directory
162 * or -1 if the directory doesn't exist.
163 */
164 int GetDirectoryCount(String Dir);
165
166 /**
167 * Gets the list of directories in the specified directory.
168 * @param Dir The absolute path name of the directory.
169 * @throws Exception - if database error occurs, or
170 * the specified path name is invalid.
171 */
172 StringListPtr GetDirectories(String Dir);
173
174 /**
175 * Adds the specified directory to the database.
176 * @param Dir The absolute path name of the directory to add.
177 * @throws Exception - if database error occurs, or the
178 * specified path is invalid.
179 */
180 void AddDirectory(String Dir);
181
182 /**
183 * Removes the specified directory from the database.
184 * @param Dir The absolute path name of the directory to remove.
185 * @throws Exception - If the specified path is invalid, or
186 * Force is false and the specified directory is
187 * not empty, or if database error occurs.
188 */
189 void RemoveDirectory(String Dir, bool Force = false);
190
191 /**
192 * Determines whether the specified directory exists in the database.
193 * @throws Exception - If database error occurs.
194 */
195 bool DirectoryExist(String Dir);
196
197 /**
198 * Gets information about the specified directory.
199 * @param Dir The absolute path name of the directory.
200 * @throws Exception - if database error occurs, or if
201 * the specified directory is not found.
202 */
203 DbDirectory GetDirectoryInfo(String Dir);
204
205 /**
206 * Renames the specified directory.
207 * @param Dir The absolute path name of the directory to rename.
208 * @param Name The new name for the directory.
209 * @throws Exception - In case the given directory does not exists,
210 * or the specified name is not a valid name,
211 * or if a directory with name equal to the new name already
212 * exists, or if database error occurs.
213 */
214 void RenameDirectory(String Dir, String Name);
215
216 /**
217 * Moves the specified directory into the specified location.
218 * @param Dir The absolute path name of the directory to move.
219 * @param Dst The location where the directory will be moved to.
220 * @throws Exception - In case a given directory does not exists,
221 * or if a directory with name equal to the directory name already
222 * exists in the specified destination directory, or if database error
223 * occurs. Error is also thrown when trying to move a directory to a
224 * subdirectory of itself.
225 */
226 void MoveDirectory(String Dir, String Dst);
227
228 /**
229 * Changes the description of the specified directory.
230 * @throws Exception - if database error occurs, or if
231 * the specified directory is not found.
232 */
233 void SetDirectoryDescription(String Dir, String Desc);
234
235 /**
236 * Adds the instruments in the specified file or
237 * directory to the specified instruments database directory.
238 * @param DbDir The absolute path name of a directory in the
239 * instruments database in which only the new instruments
240 * (that are not already in the database) will be added.
241 * @param FilePath The absolute path name of a file or
242 * directory in the file system. In case a directory is
243 * supplied, all supported instruments in the specified directory
244 * will be added to the instruments database, including the
245 * instruments in the subdirectories. The respective subdirectory
246 * structure will be recreated in the supplied database directory.
247 * @param Index The index of the instrument (in the given
248 * instrument file) to add. If -1 is specified, all instruments in
249 * the supplied instrument file will be added. Error is thrown if
250 * a directory is supplied and Index is not equal to -1.
251 * @throws Exception if the operation failed.
252 */
253 void AddInstruments(String DbDir, String FilePath, int Index = -1);
254
255 /**
256 * Adds all supported instruments in the specified directory
257 * to the specified instruments database directory. The
258 * instruments in the subdirectories will not be processed
259 * @param DbDir The absolute path name of a directory in the
260 * instruments database in which only the new instruments
261 * (that are not already in the database) will be added.
262 * @param FsDir The absolute path name of a directory in the file
263 * system.
264 * @throws Exception if the operation failed.
265 */
266 void AddInstrumentsNonrecursive(String DbDir, String FsDir);
267
268 /**
269 * Adds all supported instruments in the specified file system
270 * direcotry to the specified instruments database directory,
271 * including the instruments in the subdirectories of the
272 * supplied directory.
273 * @param DbDir The absolute path name of a directory in the
274 * instruments database in which only the new instruments
275 * (that are not already in the database) will be added.
276 * @param FsDir The absolute path name of an existing
277 * directory in the file system.
278 * @param Flat If true, the respective subdirectory structure will
279 * not be recreated in the instruments database and all instruments
280 * will be added directly in the specified database directory.
281 * @throws Exception if the operation failed.
282 */
283 void AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat = false);
284
285 /**
286 * Gets the number of instruments in the specified directory.
287 * @param Dir The absolute path name of the directory.
288 * @throws Exception - if database error occurs, or
289 * the specified path name is invalid.
290 * @returns The number of instruments in the specified directory
291 * or -1 if the directory doesn't exist.
292 */
293 int GetInstrumentCount(String Dir);
294
295 /**
296 * Gets the list of instruments in the specified directory.
297 * @param Dir The absolute path name of the directory.
298 * @throws Exception - if database error occurs, or
299 * the specified path name is invalid.
300 */
301 StringListPtr GetInstruments(String Dir);
302
303 /**
304 * Removes the specified instrument from the database.
305 * @param Instr The absolute path name of the instrument to remove.
306 * @throws Exception - If the specified instrument does not exist,
307 * or database error occurs.
308 */
309 void RemoveInstrument(String Instr);
310
311 /**
312 * Gets information about the specified instrument.
313 * @param Instr The absolute path name of the instrument.
314 * @throws Exception - if database error occurs, or if
315 * the specified instrument is not found.
316 */
317 DbInstrument GetInstrumentInfo(String Instr);
318
319 /**
320 * Renames the specified instrument.
321 * @param Instr The absolute path name of the instrument to rename.
322 * @param Name The new name for the instrument.
323 * @throws Exception - In case the given instrument does not exists,
324 * or the specified name is not a valid name, or if an instrument
325 * with name equal to the new name already exists, or
326 * if database error occurs.
327 */
328 void RenameInstrument(String Instr, String Name);
329
330 /**
331 * Moves the specified instrument into the specified directory.
332 * @param Instr The absolute path name of the instrument to move.
333 * @param Dst The directory where the instrument will be moved to.
334 * @throws Exception - In case the given directory or instrument
335 * does not exist, or if an instrument with name equal to the name
336 * of the specified instrument already exists in the specified
337 * destination directory, or if database error occurs.
338 */
339 void MoveInstrument(String Instr, String Dst);
340
341 /**
342 * Changes the description of the specified instrument.
343 * @throws Exception - if database error occurs, or if
344 * the specified instrument is not found.
345 */
346 void SetInstrumentDescription(String Instr, String Desc);
347
348 /**
349 * Closes the database connection if opened and deletes
350 * the InstrumentsDb instance.
351 */
352 static void Destroy();
353
354
355 private:
356 sqlite3* db;
357 String DbFile;
358 static InstrumentsDb* pInstrumentsDb;
359 Mutex DbInstrumentsMutex;
360 ListenerList<InstrumentsDb::Listener*> llInstrumentsDbListeners;
361
362 InstrumentsDb();
363 ~InstrumentsDb();
364
365 /**
366 * Gets the instruments database. If the database is not
367 * opened, a connection to the database is established first.
368 * @returns The instruments database.
369 * @throws Exception if fail to open the database.
370 */
371 sqlite3* GetDb();
372
373 /**
374 * Gets the number of directories in the directory
375 * with ID DirId.
376 * @returns The number of directories in the specified directory
377 * or -1 if the directory doesn't exist.
378 */
379 int GetDirectoryCount(int DirId);
380
381 /**
382 * Gets a list containing the IDs of all directories in
383 * the specified instrument directory.
384 * @returns The IDs of all directories in the specified directory.
385 * @throws Exception - if database error occurs.
386 */
387 IntListPtr GetDirectoryIDs(int DirId);
388
389 /**
390 * Gets the directory ID.
391 * @param Dir The absolute path name of the directory.
392 * @returns The directory ID or -1 if the directory is not found.
393 * @throws Exception - if database error occurs.
394 */
395 int GetDirectoryId(String Dir);
396
397 /**
398 * Gets the directory ID.
399 * @param ParentDirId The ID of the parent directory.
400 * @param DirName The directory name.
401 * @throws Exception - if database error occurs.
402 * @returns The ID of the specified directory
403 * or -1 if the directory doesn't exist.
404 */
405 int GetDirectoryId(int ParentDirId, String DirName);
406
407 /**
408 * Removes the specified directory from the database.
409 * @param DirId The ID of the directory to remove.
410 * @throws Exception - If the specified directory is not empty.
411 */
412 void RemoveDirectory(int DirId);
413
414 /**
415 * Removes all directories in the specified directory.
416 * Do NOT call this method before ensuring that all
417 * directories in the specified directory are empty.
418 * @param DirId The ID of the directory.
419 * @throws Exception - if at least one of the directories in the
420 * specified directory is not empty or if database error occurs.
421 * @see IsDirectoryEmpty
422 */
423 void RemoveAllDirectories(int DirId);
424
425 /**
426 * Determines whether the specified directory is empty.
427 * If the directory doesn't exist the return value is false.
428 * @throws Exception - if database error occurs.
429 */
430 bool IsDirectoryEmpty(int DirId);
431
432 /**
433 * Removes the content of the specified directory from the database.
434 * @param DirId The ID of the directory.
435 * @param Level Used to prevent stack overflow, which may occur
436 * due to large or infinite recursive loop.
437 * @throws Exception - If database error occurs.
438 */
439 void RemoveDirectoryContent(int DirId, int Level = 0);
440
441 /**
442 * Gets the ID of the specified database instrument.
443 * @param Instr The absolute path name of the instrument.
444 * @returns The instrument ID or -1 if the instrument is not found.
445 * @throws Exception - if database error occurs.
446 */
447 int GetInstrumentId(String Instr);
448
449 /**
450 * Gets the ID of the specified database instrument.
451 * @param DirId The ID of the directory containing the instrument.
452 * @param InstrName The name of the instrument.
453 * @returns The instrument ID or -1 if the instrument is not found.
454 * @throws Exception - if database error occurs.
455 */
456 int GetInstrumentId(int DirId, String InstrName);
457
458 /**
459 * Removes the specified instrument from the database.
460 * @param InstrId The ID of the instrument to remove.
461 * @throws Exception - If the specified instrument does not exist.
462 */
463 void RemoveInstrument(int InstrId);
464
465 /**
466 * Removes all instruments in the specified directory.
467 * @param DirId The ID of the directory.
468 * @throws Exception - if database error occurs.
469 */
470 void RemoveAllInstruments(int DirId);
471
472 /**
473 * Gets the number of instruments in the directory
474 * with ID DirId.
475 * @returns The number of instruments in the specified directory
476 * or -1 if the directory doesn't exist.
477 */
478 int GetInstrumentCount(int DirId);
479
480 /**
481 * Gets a list containing the IDs of all instruments in
482 * the specified instrument directory.
483 * @returns The IDs of all instruments in the specified directory.
484 * @throws Exception - if database error occurs.
485 */
486 IntListPtr GetInstrumentIDs(int DirId);
487
488 /**
489 * Adds the instruments in the specified file
490 * to the specified instruments database directory.
491 * @param DbDir The absolute path name of a directory in the
492 * instruments database in which only the new instruments
493 * (that are not already in the database) will be added.
494 * @param File The absolute path name of a file in the file system.
495 * @param Index The index of the instrument (in the given
496 * instrument file) to add. If -1 is specified, all instruments in
497 * the supplied instrument file will be added.
498 * @throws Exception if the operation failed.
499 */
500 void AddInstrumentsFromFile(String DbDir, String File, int Index = -1);
501
502 /**
503 * Adds the specified GIG instrument(s) to the specified location
504 * in the instruments database.
505 * @param DbDir The instruments database directory
506 * in which the instrument will be added.
507 * @param File The absolute path name of the instrument file.
508 * @param Index The index of the instrument (in the given
509 * instrument file) to add. If -1 is specified, all instruments in
510 * the supplied instrument file will be added.
511 * @throws Exception if the operation failed.
512 */
513 void AddGigInstruments(String DbDir, String File, int Index = -1);
514
515 /**
516 * Adds the specified GIG instrument.
517 * @throws Exception if the operation failed.
518 */
519 void AddGigInstrument(sqlite3_stmt* pStmt, String DbDir, int DirId, String File, ::gig::Instrument* pInstrument, int Index);
520
521 /**
522 * Used to execute SQL commands which return empty result set.
523 */
524 void ExecSql(String Sql);
525
526 /**
527 * Used to execute SQL commands which return empty result set.
528 */
529 void ExecSql(String Sql, String Param);
530
531 /**
532 * Used to execute SQL commands which returns integer.
533 */
534 int ExecSqlInt(String Sql);
535
536 /**
537 * Used to execute SQL commands which returns integer.
538 */
539 int ExecSqlInt(String Sql, String Param);
540
541 /**
542 * Used to execute SQL commands which returns integer.
543 */
544 String ExecSqlString(String Sql);
545
546 /**
547 * Used to execute SQL commands which returns integer list.
548 */
549 IntListPtr ExecSqlIntList(String Sql);
550
551 /**
552 * Used to execute SQL commands which returns string list.
553 */
554 StringListPtr ExecSqlStringList(String Sql);
555
556 /**
557 * Binds the specified text parameter.
558 */
559 void BindTextParam(sqlite3_stmt* pStmt, int Index, String Text);
560
561 /**
562 * Binds the specified integer parameter.
563 */
564 void BindIntParam(sqlite3_stmt* pStmt, int Index, int Param);
565
566 /**
567 * Checks whether an instrument with the specified name already
568 * exists in the specified directory and if so a new unique name
569 * is returnded. The new name is generated by adding the suffix
570 * [<nr>] to the end of the name , where <nr> is a number between
571 * 2 and 1000.
572 * throws Exception if cannot find an unique name. This is done
573 * because it is highly unlikely that this can happen, so it is
574 * supposed that this is due to a bug or an infinite loop.
575 */
576 String GetUniqueInstrumentName(int DirId, String Name);
577
578 void FireDirectoryCountChanged(String Dir);
579 void FireDirectoryInfoChanged(String Dir);
580 void FireDirectoryNameChanged(String Dir, String NewName);
581 void FireInstrumentCountChanged(String Dir);
582 void FireInstrumentInfoChanged(String Instr);
583 void FireInstrumentNameChanged(String Instr, String NewName);
584
585 /**
586 * Strips the non-directory suffix from the file name. If the string
587 * ends with `/' only the last character is removed. If the string
588 * doesn't starts with `/' charater, an empty string is returned.
589 */
590 static String GetDirectoryPath(String File);
591
592 /**
593 * Returns the file name extracted from the specified absolute path
594 * name. If the string doesn't starts with `/' or ends with `/',
595 * an empty string is returned.
596 */
597 static String GetFileName(String Path);
598
599 /**
600 * Strips the last directory from the specified path name. If the
601 * string doesn't starts with `/' an empty string is returned.
602 */
603 static String GetParentDirectory(String Dir);
604
605 /**
606 * Checks whether the specified path is valid.
607 * @throws Exception - if the specified path is invalid.
608 */
609 static void CheckPathName(String Path);
610
611 /**
612 * Checks whether the specified file name is valid.
613 * @throws Exception - if the specified file name is invalid.
614 */
615 static void CheckFileName(String File);
616 };
617
618 /**
619 * This class is used for recursive
620 */
621 class DirectoryScanner {
622 public:
623 /**
624 * Recursively scans all subdirectories of the specified
625 * directory and adds the supported instruments to the database.
626 * @throws Exception - if the specified directories are invalid.
627 */
628 static void Scan(String DbDir, String FsDir, bool Flat);
629
630 private:
631 static String DbDir;
632 static String FsDir;
633 static bool Flat;
634 static int FtwCallback(const char* fpath, const struct stat* sb, int typeflag);
635 };
636
637 } // namespace LinuxSampler
638
639 #endif // __LS_INSTRUMENTSDB_H__
640
641 #endif // HAVE_SQLITE3

  ViewVC Help
Powered by ViewVC