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

Diff of /linuxsampler/trunk/src/db/InstrumentsDb.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1187 by iliev, Wed May 16 14:22:26 2007 UTC revision 1717 by iliev, Sun Mar 16 17:43:20 2008 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   Copyright (C) 2007 Grigor Iliev                                       *   *   Copyright (C) 2007, 2008 Grigor Iliev                                 *
4   *                                                                         *   *                                                                         *
5   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 20  Line 20 
20    
21  #include "InstrumentsDb.h"  #include "InstrumentsDb.h"
22    
23  #if HAVE_SQLITE3  #include "../common/File.h"
24    #include "../common/global_private.h"
25    
26  #include <iostream>  #include <iostream>
27  #include <sstream>  #include <sstream>
28  #include <dirent.h>  #include <vector>
29  #include <errno.h>  #include <errno.h>
30  #include <fnmatch.h>  #include <fnmatch.h>
31  #include <ftw.h>  
32  #include "../common/Exception.h"  #include "../common/Exception.h"
33    
34  namespace LinuxSampler {  namespace LinuxSampler {
35    
36      void DbInstrument::Copy(const DbInstrument& Instr) {      InstrumentsDb InstrumentsDb::instance;
         if (this == &Instr) return;  
   
         InstrFile = Instr.InstrFile;  
         InstrNr = Instr.InstrNr;  
         FormatFamily = Instr.FormatFamily;  
         FormatVersion = Instr.FormatVersion;  
         Size = Instr.Size;  
         Created = Instr.Created;  
         Modified = Instr.Modified;  
         Description = Instr.Description;  
         IsDrum = Instr.IsDrum;  
         Product = Instr.Product;  
         Artists = Instr.Artists;  
         Keywords = Instr.Keywords;  
     }  
   
   
     void DbDirectory::Copy(const DbDirectory& Dir) {  
         if (this == &Dir) return;  
   
         Created = Dir.Created;  
         Modified = Dir.Modified;  
         Description = Dir.Description;  
     }  
   
     SearchQuery::SearchQuery() {  
         MinSize = -1;  
         MaxSize = -1;  
         InstrType = BOTH;  
     }  
   
     void SearchQuery::SetFormatFamilies(String s) {  
         if (s.length() == 0) return;  
         int i = 0;  
         int j = s.find(',', 0);  
           
         while (j != std::string::npos) {  
             FormatFamilies.push_back(s.substr(i, j - i));  
             i = j + 1;  
             j = s.find(',', i);  
         }  
           
         if (i < s.length()) FormatFamilies.push_back(s.substr(i));  
     }  
   
     void SearchQuery::SetSize(String s) {  
         String s2 = GetMin(s);  
         if (s2.length() > 0) MinSize = atoll(s2.c_str());  
         else MinSize = -1;  
           
         s2 = GetMax(s);  
         if (s2.length() > 0) MaxSize = atoll(s2.c_str());  
         else MaxSize = -1;  
     }  
   
     void SearchQuery::SetCreated(String s) {  
         CreatedAfter = GetMin(s);  
         CreatedBefore = GetMax(s);  
     }  
   
     void SearchQuery::SetModified(String s) {  
         ModifiedAfter = GetMin(s);  
         ModifiedBefore = GetMax(s);  
     }  
   
     String SearchQuery::GetMin(String s) {  
         if (s.length() < 3) return "";  
         if (s.at(0) == '.' && s.at(1) == '.') return "";  
         int i = s.find("..");  
         if (i == std::string::npos) return "";  
         return s.substr(0, i);  
     }  
   
     String SearchQuery::GetMax(String s) {  
         if (s.length() < 3) return "";  
         if (s.find("..", s.length() - 2) != std::string::npos) return "";  
         int i = s.find("..");  
         if (i == std::string::npos) return "";  
         return s.substr(i + 2);  
     }  
       
     bool InstrumentsDb::AbstractFinder::IsRegex(String Pattern) {  
         if(Pattern.find('?') != String::npos) return true;  
         if(Pattern.find('*') != String::npos) return true;  
         return false;  
     }  
   
     void InstrumentsDb::AbstractFinder::AddSql(String Col, String Pattern, std::stringstream& Sql) {  
         if (Pattern.length() == 0) return;  
   
         if (IsRegex(Pattern)) {  
             Sql << " AND " << Col << " regexp ?";  
             Params.push_back(Pattern);  
             return;  
         }  
   
         String buf;  
         std::vector<String> tokens;  
         std::vector<String> tokens2;  
         std::stringstream ss(Pattern);  
         while (ss >> buf) tokens.push_back(buf);  
   
         if (tokens.size() == 0) {  
             Sql << " AND " << Col << " LIKE ?";  
             Params.push_back("%" + Pattern + "%");  
             return;  
         }  
   
         bool b = false;  
         for (int i = 0; i < tokens.size(); i++) {  
             Sql << (i == 0 ? " AND (" : "");  
   
             for (int j = 0; j < tokens.at(i).length(); j++) {  
                 if (tokens.at(i).at(j) == '+') tokens.at(i).at(j) = ' ';  
             }  
   
             ss.clear();  
             ss.str("");  
             ss << tokens.at(i);  
   
             tokens2.clear();  
             while (ss >> buf) tokens2.push_back(buf);  
   
             if (b && tokens2.size() > 0) Sql << " OR ";  
             if (tokens2.size() > 1) Sql << "(";  
             for (int j = 0; j < tokens2.size(); j++) {  
                 if (j != 0) Sql << " AND ";  
                 Sql << Col << " LIKE ?";  
                 Params.push_back("%" + tokens2.at(j) + "%");  
                 b = true;  
             }  
             if (tokens2.size() > 1) Sql << ")";  
         }  
         if (!b) Sql << "0)";  
         else Sql << ")";  
     }  
   
     InstrumentsDb::DirectoryFinder::DirectoryFinder(SearchQuery* pQuery) : pDirectories(new std::vector<String>) {  
         pStmt = NULL;  
         this->pQuery = pQuery;  
         std::stringstream sql;  
         sql << "SELECT dir_name from instr_dirs WHERE parent_dir_id=?";  
   
         if (pQuery->CreatedAfter.length() != 0) {  
             sql << " AND created > ?";  
             Params.push_back(pQuery->CreatedAfter);  
         }  
         if (pQuery->CreatedBefore.length() != 0) {  
             sql << " AND created < ?";  
             Params.push_back(pQuery->CreatedBefore);  
         }  
         if (pQuery->ModifiedAfter.length() != 0) {  
             sql << " AND modified > ?";  
             Params.push_back(pQuery->ModifiedAfter);  
         }  
         if (pQuery->ModifiedBefore.length() != 0) {  
             sql << " AND modified < ?";  
             Params.push_back(pQuery->ModifiedBefore);  
         }  
   
         AddSql("dir_name", pQuery->Name, sql);  
         AddSql("description", pQuery->Description, sql);  
         SqlQuery = sql.str();  
   
         InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();  
   
         int res = sqlite3_prepare(idb->GetDb(), SqlQuery.c_str(), -1, &pStmt, NULL);  
         if (res != SQLITE_OK) {  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));  
         }  
   
         for(int i = 0; i < Params.size(); i++) {  
             idb->BindTextParam(pStmt, i + 2, Params.at(i));  
         }  
     }  
       
     InstrumentsDb::DirectoryFinder::~DirectoryFinder() {  
         if (pStmt != NULL) sqlite3_finalize(pStmt);  
     }  
   
     StringListPtr InstrumentsDb::DirectoryFinder::GetDirectories() {  
         return pDirectories;  
     }  
       
     void InstrumentsDb::DirectoryFinder::ProcessDirectory(String Path, int DirId) {  
         InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();  
         idb->BindIntParam(pStmt, 1, DirId);  
   
         String s = Path;  
         if(Path.compare("/") != 0) s += "/";  
         int res = sqlite3_step(pStmt);  
         while(res == SQLITE_ROW) {  
             pDirectories->push_back(s + ToString(sqlite3_column_text(pStmt, 0)));  
             res = sqlite3_step(pStmt);  
         }  
           
         if (res != SQLITE_DONE) {  
             sqlite3_finalize(pStmt);  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));  
         }  
   
         res = sqlite3_reset(pStmt);  
         if (res != SQLITE_OK) {  
             sqlite3_finalize(pStmt);  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));  
         }  
     }  
   
     InstrumentsDb::InstrumentFinder::InstrumentFinder(SearchQuery* pQuery) : pInstruments(new std::vector<String>) {  
         pStmt = NULL;  
         this->pQuery = pQuery;  
         std::stringstream sql;  
         sql << "SELECT instr_name from instruments WHERE dir_id=?";  
   
         if (pQuery->CreatedAfter.length() != 0) {  
             sql << " AND created > ?";  
             Params.push_back(pQuery->CreatedAfter);  
         }  
         if (pQuery->CreatedBefore.length() != 0) {  
             sql << " AND created < ?";  
             Params.push_back(pQuery->CreatedBefore);  
         }  
         if (pQuery->ModifiedAfter.length() != 0) {  
             sql << " AND modified > ?";  
             Params.push_back(pQuery->ModifiedAfter);  
         }  
         if (pQuery->ModifiedBefore.length() != 0) {  
             sql << " AND modified < ?";  
             Params.push_back(pQuery->ModifiedBefore);  
         }  
         if (pQuery->MinSize != -1) sql << " AND instr_size > " << pQuery->MinSize;  
         if (pQuery->MaxSize != -1) sql << " AND instr_size < " << pQuery->MaxSize;  
   
         if (pQuery->InstrType == SearchQuery::CHROMATIC) sql << " AND is_drum = 0";  
         else if (pQuery->InstrType == SearchQuery::DRUM) sql << " AND is_drum != 0";  
   
         if (pQuery->FormatFamilies.size() > 0) {  
             sql << " AND (format_family=?";  
             Params.push_back(pQuery->FormatFamilies.at(0));  
             for (int i = 1; i < pQuery->FormatFamilies.size(); i++) {  
                 sql << "OR format_family=?";  
                 Params.push_back(pQuery->FormatFamilies.at(i));  
             }  
             sql << ")";  
         }  
   
         AddSql("instr_name", pQuery->Name, sql);  
         AddSql("description", pQuery->Description, sql);  
         AddSql("product", pQuery->Product, sql);  
         AddSql("artists", pQuery->Artists, sql);  
         AddSql("keywords", pQuery->Keywords, sql);  
         SqlQuery = sql.str();  
   
         InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();  
   
         int res = sqlite3_prepare(idb->GetDb(), SqlQuery.c_str(), -1, &pStmt, NULL);  
         if (res != SQLITE_OK) {  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));  
         }  
   
         for(int i = 0; i < Params.size(); i++) {  
             idb->BindTextParam(pStmt, i + 2, Params.at(i));  
         }  
     }  
       
     InstrumentsDb::InstrumentFinder::~InstrumentFinder() {  
         if (pStmt != NULL) sqlite3_finalize(pStmt);  
     }  
       
     void InstrumentsDb::InstrumentFinder::ProcessDirectory(String Path, int DirId) {  
         InstrumentsDb* idb = InstrumentsDb::GetInstrumentsDb();  
         idb->BindIntParam(pStmt, 1, DirId);  
   
         String s = Path;  
         if(Path.compare("/") != 0) s += "/";  
         int res = sqlite3_step(pStmt);  
         while(res == SQLITE_ROW) {  
             pInstruments->push_back(s + ToString(sqlite3_column_text(pStmt, 0)));  
             res = sqlite3_step(pStmt);  
         }  
           
         if (res != SQLITE_DONE) {  
             sqlite3_finalize(pStmt);  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));  
         }  
   
         res = sqlite3_reset(pStmt);  
         if (res != SQLITE_OK) {  
             sqlite3_finalize(pStmt);  
             throw Exception("DB error: " + ToString(sqlite3_errmsg(idb->GetDb())));  
         }  
     }  
   
     StringListPtr InstrumentsDb::InstrumentFinder::GetInstruments() {  
         return pInstruments;  
     }  
   
     void InstrumentsDb::DirectoryCounter::ProcessDirectory(String Path, int DirId) {  
         count += InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(DirId);  
     }  
   
     void InstrumentsDb::InstrumentCounter::ProcessDirectory(String Path, int DirId) {  
         count += InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(DirId);  
     }  
   
     InstrumentsDb::DirectoryCopier::DirectoryCopier(String SrcParentDir, String DestDir) {  
         this->SrcParentDir = SrcParentDir;  
         this->DestDir = DestDir;  
   
         if (DestDir.at(DestDir.length() - 1) != '/') {  
             this->DestDir.append("/");  
         }  
         if (SrcParentDir.at(SrcParentDir.length() - 1) != '/') {  
             this->SrcParentDir.append("/");  
         }  
     }  
   
     void InstrumentsDb::DirectoryCopier::ProcessDirectory(String Path, int DirId) {  
         InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();  
   
         String dir = DestDir;  
         String subdir = Path;  
         if(subdir.length() > SrcParentDir.length()) {  
             subdir = subdir.substr(SrcParentDir.length());  
             dir += subdir;  
             db->AddDirectory(dir);  
         }  
   
         int dstDirId = db->GetDirectoryId(dir);  
         if(dstDirId == -1) throw Exception("Unkown DB directory: " + dir);  
         IntListPtr ids = db->GetInstrumentIDs(DirId);  
         for (int i = 0; i < ids->size(); i++) {  
             String name = db->GetInstrumentName(ids->at(i));  
             db->CopyInstrument(ids->at(i), name, dstDirId, dir);  
         }  
     }  
37    
38      InstrumentsDb* InstrumentsDb::pInstrumentsDb = new InstrumentsDb;      void InstrumentsDb::CreateInstrumentsDb(String FilePath) {
39            File f = File(FilePath);
40      void InstrumentsDb::CreateInstrumentsDb(String File) {          if (f.Exist()) {
41          struct stat statBuf;              throw Exception("File exists: " + FilePath);
         int res = stat(File.c_str(), &statBuf);  
         if (!res) {  
             throw Exception("File exists: " + File);  
42          }          }
43                    
44          GetInstrumentsDb()->SetDbFile(File);          GetInstrumentsDb()->SetDbFile(FilePath);
45    
46          String sql =          String sql =
47              "  CREATE TABLE instr_dirs (                                      "              "  CREATE TABLE instr_dirs (                                      "
# Line 395  namespace LinuxSampler { Line 57  namespace LinuxSampler {
57                    
58          GetInstrumentsDb()->ExecSql(sql);          GetInstrumentsDb()->ExecSql(sql);
59    
60          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, 0, '/');";          sql = "INSERT INTO instr_dirs (dir_id, parent_dir_id, dir_name) VALUES (0, -2, '/');";
61          GetInstrumentsDb()->ExecSql(sql);          GetInstrumentsDb()->ExecSql(sql);
62    
63          sql =          sql =
# Line 432  namespace LinuxSampler { Line 94  namespace LinuxSampler {
94          if (db != NULL) sqlite3_close(db);          if (db != NULL) sqlite3_close(db);
95      }      }
96            
     void InstrumentsDb::Destroy() {  
         if (pInstrumentsDb != NULL) {  
             delete pInstrumentsDb;  
             pInstrumentsDb = NULL;  
         }  
     }  
   
97      void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) {      void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) {
98          llInstrumentsDbListeners.AddListener(l);          llInstrumentsDbListeners.AddListener(l);
99      }      }
# Line 448  namespace LinuxSampler { Line 103  namespace LinuxSampler {
103      }      }
104            
105      InstrumentsDb* InstrumentsDb::GetInstrumentsDb() {      InstrumentsDb* InstrumentsDb::GetInstrumentsDb() {
106          return pInstrumentsDb;          return &instance;
107      }      }
108            
109      void InstrumentsDb::SetDbFile(String File) {      void InstrumentsDb::SetDbFile(String File) {
# Line 464  namespace LinuxSampler { Line 119  namespace LinuxSampler {
119      sqlite3* InstrumentsDb::GetDb() {      sqlite3* InstrumentsDb::GetDb() {
120          if ( db != NULL) return db;          if ( db != NULL) return db;
121    
122          if (DbFile.empty()) DbFile = "/var/lib/linuxsampler/instruments.db";          if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;
123                    #if defined(__APPLE__)  /* 20071224 Toshi Nagata  */
124                    if (DbFile.find("~") == 0)
125                            DbFile.replace(0, 1, getenv("HOME"));
126                    #endif
127          int rc = sqlite3_open(DbFile.c_str(), &db);          int rc = sqlite3_open(DbFile.c_str(), &db);
128          if (rc) {          if (rc) {
129              sqlite3_close(db);              sqlite3_close(db);
# Line 473  namespace LinuxSampler { Line 132  namespace LinuxSampler {
132          }          }
133          rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL);          rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, NULL, Regexp, NULL, NULL);
134          if (rc) { throw Exception("Failed to add user function for handling regular expressions."); }          if (rc) { throw Exception("Failed to add user function for handling regular expressions."); }
135    
136            // TODO: remove this in the next version
137            try {
138                int i = ExecSqlInt("SELECT parent_dir_id FROM instr_dirs WHERE dir_id=0");
139                // The parent ID of the root directory should be -2 now.
140                if(i != -2) ExecSql("UPDATE instr_dirs SET parent_dir_id=-2 WHERE dir_id=0");
141            } catch(Exception e) { }
142            ////////////////////////////////////////
143                    
144          return db;          return db;
145      }      }
# Line 486  namespace LinuxSampler { Line 153  namespace LinuxSampler {
153                    
154          int count = ExecSqlInt(sql.str());          int count = ExecSqlInt(sql.str());
155    
         // While the root dir has ID 0 and parent ID 0, the directory  
         // count for the root dir will be incorrect, so we should fix it.  
         if (count != -1 && DirId == 0) count--;  
156          return count;          return count;
157      }      }
158    
# Line 510  namespace LinuxSampler { Line 174  namespace LinuxSampler {
174              throw e;              throw e;
175          }          }
176          EndTransaction();          EndTransaction();
177          if (i == -1) throw Exception("Unkown DB directory: " + Dir);          if (i == -1) throw Exception("Unkown DB directory: " + toEscapedPath(Dir));
178                    
179          return i;          return i;
180      }      }
# Line 529  namespace LinuxSampler { Line 193  namespace LinuxSampler {
193          BeginTransaction();          BeginTransaction();
194          try {          try {
195              int dirId = GetDirectoryId(Dir);              int dirId = GetDirectoryId(Dir);
196              if(dirId == -1) throw Exception("Unknown DB directory: " + Dir);              if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
197    
198              StringListPtr pDirs;              StringListPtr pDirs;
199              if (Recursive) {              if (Recursive) {
# Line 552  namespace LinuxSampler { Line 216  namespace LinuxSampler {
216          std::stringstream sql;          std::stringstream sql;
217          sql << "SELECT dir_name FROM instr_dirs ";          sql << "SELECT dir_name FROM instr_dirs ";
218          sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0";          sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0";
219          return ExecSqlStringList(sql.str());          StringListPtr dirs = ExecSqlStringList(sql.str());
220    
221            for (int i = 0; i < dirs->size(); i++) {
222                for (int j = 0; j < dirs->at(i).length(); j++) {
223                    if (dirs->at(i).at(j) == '/') dirs->at(i).at(j) = '\0';
224                }
225            }
226    
227            return dirs;
228      }      }
229    
230      int InstrumentsDb::GetDirectoryId(String Dir) {      int InstrumentsDb::GetDirectoryId(String Dir) {
# Line 581  namespace LinuxSampler { Line 253  namespace LinuxSampler {
253    
254      int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) {      int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) {
255          dmsg(2,("InstrumentsDb: GetDirectoryId(ParentDirId=%d, DirName=%s)\n", ParentDirId, DirName.c_str()));          dmsg(2,("InstrumentsDb: GetDirectoryId(ParentDirId=%d, DirName=%s)\n", ParentDirId, DirName.c_str()));
256            DirName = toDbName(DirName);
257          std::stringstream sql;          std::stringstream sql;
258          sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id=";          sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id=";
259          sql << ParentDirId << " AND dir_name=?";          sql << ParentDirId << " AND dir_name=?";
# Line 633  namespace LinuxSampler { Line 306  namespace LinuxSampler {
306    
307              String dirName = GetFileName(Dir);              String dirName = GetFileName(Dir);
308              if(ParentDir.empty() || dirName.empty()) {              if(ParentDir.empty() || dirName.empty()) {
309                  throw Exception("Failed to add DB directory: " + Dir);                  throw Exception("Failed to add DB directory: " + toEscapedPath(Dir));
310              }              }
311    
312              int id = GetDirectoryId(ParentDir);              int id = GetDirectoryId(ParentDir);
313              if (id == -1) throw Exception("DB directory doesn't exist: " + ParentDir);              if (id == -1) throw Exception("DB directory doesn't exist: " + toEscapedPath(ParentDir));
314              int id2 = GetDirectoryId(id, dirName);              int id2 = GetDirectoryId(id, dirName);
315              if (id2 != -1) throw Exception("DB directory already exist: " + Dir);              if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(Dir));
316              id2 = GetInstrumentId(id, dirName);              id2 = GetInstrumentId(id, dirName);
317              if (id2 != -1) throw Exception("Instrument with that name exist: " + Dir);              if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(Dir));
318    
319              std::stringstream sql;              std::stringstream sql;
320              sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES (";              sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES (";
321              sql << id << ", ?)";              sql << id << ", ?)";
322    
323              ExecSql(sql.str(), dirName);              ExecSql(sql.str(), toDbName(dirName));
324          } catch (Exception e) {          } catch (Exception e) {
325              EndTransaction();              EndTransaction();
326              throw e;              throw e;
# Line 666  namespace LinuxSampler { Line 339  namespace LinuxSampler {
339          BeginTransaction();          BeginTransaction();
340          try {          try {
341              int dirId = GetDirectoryId(Dir);              int dirId = GetDirectoryId(Dir);
342              if (dirId == -1) throw Exception("Unknown DB directory: " + Dir);              if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
343              if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir);              if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir);
344              if(ParentDir.empty()) throw Exception("Unknown parent directory");              if(ParentDir.empty()) throw Exception("Unknown parent directory");
345              if (Force) RemoveDirectoryContent(dirId);              if (Force) RemoveDirectoryContent(dirId);
# Line 753  namespace LinuxSampler { Line 426  namespace LinuxSampler {
426    
427          try {          try {
428              int id = GetDirectoryId(Dir);              int id = GetDirectoryId(Dir);
429              if(id == -1) throw Exception("Unknown DB directory: " + Dir);              if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
430    
431              sqlite3_stmt *pStmt = NULL;              sqlite3_stmt *pStmt = NULL;
432              std::stringstream sql;              std::stringstream sql;
# Line 776  namespace LinuxSampler { Line 449  namespace LinuxSampler {
449                  if (res != SQLITE_DONE) {                  if (res != SQLITE_DONE) {
450                      throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));                      throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
451                  } else {                  } else {
452                      throw Exception("Unknown DB directory: " + Dir);                      throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
453                  }                  }
454              }              }
455                            
# Line 793  namespace LinuxSampler { Line 466  namespace LinuxSampler {
466      void InstrumentsDb::RenameDirectory(String Dir, String Name) {      void InstrumentsDb::RenameDirectory(String Dir, String Name) {
467          dmsg(2,("InstrumentsDb: RenameDirectory(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));          dmsg(2,("InstrumentsDb: RenameDirectory(Dir=%s,Name=%s)\n", Dir.c_str(), Name.c_str()));
468          CheckFileName(Name);          CheckFileName(Name);
469            String dbName = toDbName(Name);
470    
471          BeginTransaction();          BeginTransaction();
472          try {          try {
473              int dirId = GetDirectoryId(Dir);              int dirId = GetDirectoryId(Dir);
474              if (dirId == -1) throw Exception("Unknown DB directory: " + Dir);              if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedText(Dir));
475    
476              std::stringstream sql;              std::stringstream sql;
477              sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" <<  dirId;              sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" <<  dirId;
478    
479              int parent = ExecSqlInt(sql.str());              int parent = ExecSqlInt(sql.str());
480              if (parent == -1) throw Exception("Unknown parent directory: " + Dir);              if (parent == -1) throw Exception("Unknown parent directory: " + toEscapedPath(Dir));
481              if (GetDirectoryId(parent, Name) != -1) {  
482                  throw Exception("Cannot rename. Directory with that name already exists: " + Name);              if (GetDirectoryId(parent, dbName) != -1) {
483                    String s = toEscapedPath(Name);
484                    throw Exception("Cannot rename. Directory with that name already exists: " + s);
485              }              }
486    
487              if (GetInstrumentId(parent, Name) != -1) {              if (GetInstrumentId(parent, dbName) != -1) {
488                  throw Exception("Cannot rename. Instrument with that name exist: " + Dir);                  throw Exception("Cannot rename. Instrument with that name exist: " + toEscapedPath(Dir));
489              }              }
490    
491              sql.str("");              sql.str("");
492              sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId;              sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId;
493              ExecSql(sql.str(), Name);              ExecSql(sql.str(), dbName);
494          } catch (Exception e) {          } catch (Exception e) {
495              EndTransaction();              EndTransaction();
496              throw e;              throw e;
497          }          }
498    
499          EndTransaction();          EndTransaction();
500          FireDirectoryNameChanged(Dir, Name);          FireDirectoryNameChanged(Dir, toAbstractName(Name));
501      }      }
502    
503      void InstrumentsDb::MoveDirectory(String Dir, String Dst) {      void InstrumentsDb::MoveDirectory(String Dir, String Dst) {
# Line 834  namespace LinuxSampler { Line 510  namespace LinuxSampler {
510          BeginTransaction();          BeginTransaction();
511          try {          try {
512              int dirId = GetDirectoryId(Dir);              int dirId = GetDirectoryId(Dir);
513              if (dirId == -1) throw Exception("Unknown DB directory: " + Dir);              if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
514              int dstId = GetDirectoryId(Dst);              int dstId = GetDirectoryId(Dst);
515              if (dstId == -1) throw Exception("Unknown DB directory: " + Dst);              if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst));
516              if (dirId == dstId) {              if (dirId == dstId) {
517                  throw Exception("Cannot move directory to itself");                  throw Exception("Cannot move directory to itself");
518              }              }
# Line 852  namespace LinuxSampler { Line 528  namespace LinuxSampler {
528              String dirName = GetFileName(Dir);              String dirName = GetFileName(Dir);
529    
530              int id2 = GetDirectoryId(dstId, dirName);              int id2 = GetDirectoryId(dstId, dirName);
531              if (id2 != -1) throw Exception("DB directory already exist: " + dirName);              if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName));
532              id2 = GetInstrumentId(dstId, dirName);              id2 = GetInstrumentId(dstId, dirName);
533              if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName);              if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName));
534    
535              std::stringstream sql;              std::stringstream sql;
536              sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId;              sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId;
# Line 880  namespace LinuxSampler { Line 556  namespace LinuxSampler {
556          BeginTransaction();          BeginTransaction();
557          try {          try {
558              int dirId = GetDirectoryId(Dir);              int dirId = GetDirectoryId(Dir);
559              if (dirId == -1) throw Exception("Unknown DB directory: " + Dir);              if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
560              int dstId = GetDirectoryId(Dst);              int dstId = GetDirectoryId(Dst);
561              if (dstId == -1) throw Exception("Unknown DB directory: " + Dst);              if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst));
562              if (dirId == dstId) {              if (dirId == dstId) {
563                  throw Exception("Cannot copy directory to itself");                  throw Exception("Cannot copy directory to itself");
564              }              }
# Line 898  namespace LinuxSampler { Line 574  namespace LinuxSampler {
574              String dirName = GetFileName(Dir);              String dirName = GetFileName(Dir);
575    
576              int id2 = GetDirectoryId(dstId, dirName);              int id2 = GetDirectoryId(dstId, dirName);
577              if (id2 != -1) throw Exception("DB directory already exist: " + dirName);              if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName));
578              id2 = GetInstrumentId(dstId, dirName);              id2 = GetInstrumentId(dstId, dirName);
579              if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName);              if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName));
580    
581              DirectoryCopier directoryCopier(ParentDir, Dst);              DirectoryCopier directoryCopier(ParentDir, Dst);
582              DirectoryTreeWalk(Dir, &directoryCopier);              DirectoryTreeWalk(Dir, &directoryCopier);
# Line 918  namespace LinuxSampler { Line 594  namespace LinuxSampler {
594          BeginTransaction();          BeginTransaction();
595          try {          try {
596              int id = GetDirectoryId(Dir);              int id = GetDirectoryId(Dir);
597              if(id == -1) throw Exception("Unknown DB directory: " + Dir);              if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
598    
599              std::stringstream sql;              std::stringstream sql;
600              sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP ";              sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP ";
# Line 934  namespace LinuxSampler { Line 610  namespace LinuxSampler {
610          FireDirectoryInfoChanged(Dir);          FireDirectoryInfoChanged(Dir);
611      }      }
612    
613      void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index) {      int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) {
614            dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground));
615            if(!bBackground) {
616                switch (Mode) {
617                    case NON_RECURSIVE:
618                        AddInstrumentsNonrecursive(DbDir, FsDir);
619                        break;
620                    case RECURSIVE:
621                        AddInstrumentsRecursive(DbDir, FsDir);
622                        break;
623                    case FLAT:
624                        AddInstrumentsRecursive(DbDir, FsDir, true);
625                        break;
626                    default:
627                        throw Exception("Unknown scan mode");
628                }
629    
630                return -1;
631            }
632    
633            ScanJob job;
634            int jobId = Jobs.AddJob(job);
635            InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir));
636    
637            return jobId;
638        }
639        
640        int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
641            dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
642            if(!bBackground) {
643                AddInstruments(DbDir, FilePath, Index);
644                return -1;
645            }
646    
647            ScanJob job;
648            int jobId = Jobs.AddJob(job);
649            InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index));
650    
651            return jobId;
652        }
653    
654        void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) {
655          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index));          dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index));
656          if (DbDir.empty() || FilePath.empty()) return;          if (DbDir.empty() || FilePath.empty()) return;
657                    
658          DbInstrumentsMutex.Lock();          DbInstrumentsMutex.Lock();
659          try {          try {
660              int dirId = GetDirectoryId(DbDir);              int dirId = GetDirectoryId(DbDir);
661              if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir);              if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir));
662    
663              struct stat statBuf;              File f = File(FilePath);
664              int res = stat(FilePath.c_str(), &statBuf);              if (!f.Exist()) {
             if (res) {  
665                  std::stringstream ss;                  std::stringstream ss;
666                  ss << "Fail to stat `" << FilePath << "`: " << strerror(errno);                  ss << "Fail to stat `" << FilePath << "`: " << f.GetErrorMsg();
667                  throw Exception(ss.str());                  throw Exception(ss.str());
668              }              }
669    
670              if (S_ISREG(statBuf.st_mode)) {              if (!f.IsFile()) {
                 AddInstrumentsFromFile(DbDir, FilePath, Index);  
                 DbInstrumentsMutex.Unlock();  
                 return;  
             }  
   
             if (!S_ISDIR(statBuf.st_mode)) {  
                 DbInstrumentsMutex.Unlock();  
                 return;  
             }  
               
             if (Index != -1) {  
671                  std::stringstream ss;                  std::stringstream ss;
672                  ss << "`" << FilePath << "` is directory, not an instrument file";                  ss << "`" << FilePath << "` is not an instrument file";
673                  throw Exception(ss.str());                  throw Exception(ss.str());
674              }              }
675            
676              AddInstrumentsRecursive(DbDir, FilePath, false);              AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress);
677          } catch (Exception e) {          } catch (Exception e) {
678              DbInstrumentsMutex.Unlock();              DbInstrumentsMutex.Unlock();
679              throw e;              throw e;
# Line 977  namespace LinuxSampler { Line 682  namespace LinuxSampler {
682          DbInstrumentsMutex.Unlock();          DbInstrumentsMutex.Unlock();
683      }      }
684    
685      void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir) {      void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) {
686          dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s)\n", DbDir.c_str(), FsDir.c_str()));          dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s)\n", DbDir.c_str(), FsDir.c_str()));
687          if (DbDir.empty() || FsDir.empty()) return;          if (DbDir.empty() || FsDir.empty()) return;
688                    
689          DbInstrumentsMutex.Lock();          DbInstrumentsMutex.Lock();
690          try {          try {
691              int dirId = GetDirectoryId(DbDir);              int dirId = GetDirectoryId(DbDir);
692              if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir);              if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir));
693    
694              struct stat statBuf;              File f = File(FsDir);
695              int res = stat(FsDir.c_str(), &statBuf);              if (!f.Exist()) {
             if (res) {  
696                  std::stringstream ss;                  std::stringstream ss;
697                  ss << "Fail to stat `" << FsDir << "`: " << strerror(errno);                  ss << "Fail to stat `" << FsDir << "`: " << f.GetErrorMsg();
698                  throw Exception(ss.str());                  throw Exception(ss.str());
699              }              }
700    
701              if (!S_ISDIR(statBuf.st_mode)) {              if (!f.IsDirectory()) {
702                  throw Exception("Directory expected");                  throw Exception("Directory expected: " + FsDir);
703              }              }
704                            
705              if (FsDir.at(FsDir.length() - 1) != '/') FsDir.append("/");              if (FsDir.at(FsDir.length() - 1) != File::DirSeparator) {
706                    FsDir.push_back(File::DirSeparator);
             DIR* pDir = opendir(FsDir.c_str());  
             if (pDir == NULL) {  
                 std::stringstream ss;  
                 ss << "The scanning of directory `" << FsDir << "` failed: ";  
                 ss << strerror(errno);  
                 std::cerr << ss.str();  
                 DbInstrumentsMutex.Unlock();  
                 return;  
707              }              }
708                
709              struct dirent* pEnt = readdir(pDir);              try {
710              while (pEnt != NULL) {                  FileListPtr fileList = File::GetFiles(FsDir);
711                  if (pEnt->d_type != DT_REG) {                  for (int i = 0; i < fileList->size(); i++) {
712                      pEnt = readdir(pDir);                      AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress);
                     continue;  
713                  }                  }
714                } catch(Exception e) {
715                  AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name));                  e.PrintMessage();
716                  pEnt = readdir(pDir);                  DbInstrumentsMutex.Unlock();
717              }                  return;
   
             if (closedir(pDir)) {  
                 std::stringstream ss;  
                 ss << "Failed to close directory `" << FsDir << "`: ";  
                 ss << strerror(errno);  
                 std::cerr << ss.str();  
718              }              }
719          } catch (Exception e) {          } catch (Exception e) {
720              DbInstrumentsMutex.Unlock();              DbInstrumentsMutex.Unlock();
# Line 1035  namespace LinuxSampler { Line 724  namespace LinuxSampler {
724          DbInstrumentsMutex.Unlock();          DbInstrumentsMutex.Unlock();
725      }      }
726    
727      void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat) {      void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) {
728          dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat));          dmsg(2,("InstrumentsDb: AddInstrumentsRecursive(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat));
729          DirectoryScanner::Scan(DbDir, FsDir, Flat);          if (pProgress != NULL) {
730                InstrumentFileCounter c;
731                pProgress->SetTotalFileCount(c.Count(FsDir));
732            }
733    
734            DirectoryScanner d;
735            d.Scan(DbDir, FsDir, Flat, pProgress);
736      }      }
737    
738      int InstrumentsDb::GetInstrumentCount(int DirId) {      int InstrumentsDb::GetInstrumentCount(int DirId) {
# Line 1069  namespace LinuxSampler { Line 764  namespace LinuxSampler {
764          }          }
765          EndTransaction();          EndTransaction();
766    
767          if (i == -1) throw Exception("Unknown Db directory: " + Dir);          if (i == -1) throw Exception("Unknown Db directory: " + toEscapedPath(Dir));
768          return i;          return i;
769      }      }
770    
# Line 1085  namespace LinuxSampler { Line 780  namespace LinuxSampler {
780          BeginTransaction();          BeginTransaction();
781          try {          try {
782              int dirId = GetDirectoryId(Dir);              int dirId = GetDirectoryId(Dir);
783              if(dirId == -1) throw Exception("Unknown DB directory: " + Dir);              if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
784    
785              StringListPtr pInstrs;              StringListPtr pInstrs;
786    
# Line 1099  namespace LinuxSampler { Line 794  namespace LinuxSampler {
794                  sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId;                  sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId;
795    
796                  pInstrs = ExecSqlStringList(sql.str());                  pInstrs = ExecSqlStringList(sql.str());
797                    // Converting to abstract names
798                    for (int i = 0; i < pInstrs->size(); i++) {
799                        for (int j = 0; j < pInstrs->at(i).length(); j++) {
800                            if (pInstrs->at(i).at(j) == '/') pInstrs->at(i).at(j) = '\0';
801                        }
802                    }
803              }              }
804              EndTransaction();              EndTransaction();
805              return pInstrs;              return pInstrs;
# Line 1123  namespace LinuxSampler { Line 824  namespace LinuxSampler {
824          std::stringstream sql;          std::stringstream sql;
825          sql << "SELECT instr_id FROM instruments WHERE dir_id=";          sql << "SELECT instr_id FROM instruments WHERE dir_id=";
826          sql << DirId << " AND instr_name=?";          sql << DirId << " AND instr_name=?";
827          return ExecSqlInt(sql.str(), InstrName);          return ExecSqlInt(sql.str(), toDbName(InstrName));
828      }      }
829    
830      String InstrumentsDb::GetInstrumentName(int InstrId) {      String InstrumentsDb::GetInstrumentName(int InstrId) {
831          dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId));          dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId));
832          std::stringstream sql;          std::stringstream sql;
833          sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId;          sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId;
834          return ExecSqlString(sql.str());          return toAbstractName(ExecSqlString(sql.str()));
835      }      }
836            
837      void InstrumentsDb::RemoveInstrument(String Instr) {      void InstrumentsDb::RemoveInstrument(String Instr) {
# Line 1142  namespace LinuxSampler { Line 843  namespace LinuxSampler {
843          try {          try {
844              int instrId = GetInstrumentId(Instr);              int instrId = GetInstrumentId(Instr);
845              if(instrId == -1) {              if(instrId == -1) {
846                  throw Exception("The specified instrument does not exist: " + Instr);                  throw Exception("The specified instrument does not exist: " + toEscapedPath(Instr));
847              }              }
848              RemoveInstrument(instrId);              RemoveInstrument(instrId);
849          } catch (Exception e) {          } catch (Exception e) {
# Line 1177  namespace LinuxSampler { Line 878  namespace LinuxSampler {
878          BeginTransaction();          BeginTransaction();
879          try {          try {
880              int id = GetInstrumentId(Instr);              int id = GetInstrumentId(Instr);
881              if(id == -1) throw Exception("Unknown DB instrument: " + Instr);              if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
882              i = GetInstrumentInfo(id);              i = GetInstrumentInfo(id);
883          } catch (Exception e) {          } catch (Exception e) {
884              EndTransaction();              EndTransaction();
# Line 1236  namespace LinuxSampler { Line 937  namespace LinuxSampler {
937          BeginTransaction();          BeginTransaction();
938          try {          try {
939              int dirId = GetDirectoryId(GetDirectoryPath(Instr));              int dirId = GetDirectoryId(GetDirectoryPath(Instr));
940              if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr);              if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
941    
942              int instrId = GetInstrumentId(dirId, GetFileName(Instr));              int instrId = GetInstrumentId(dirId, GetFileName(Instr));
943              if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr);              if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
944    
945              if (GetInstrumentId(dirId, Name) != -1) {              if (GetInstrumentId(dirId, Name) != -1) {
946                  throw Exception("Cannot rename. Instrument with that name already exists: " + Name);                  String s = toEscapedPath(Name);
947                    throw Exception("Cannot rename. Instrument with that name already exists: " + s);
948              }              }
949    
950              if (GetDirectoryId(dirId, Name) != -1) {              if (GetDirectoryId(dirId, Name) != -1) {
951                  throw Exception("Cannot rename. Directory with that name already exists: " + Name);                  String s = toEscapedPath(Name);
952                    throw Exception("Cannot rename. Directory with that name already exists: " + s);
953              }              }
954    
955              std::stringstream sql;              std::stringstream sql;
956              sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId;              sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId;
957              ExecSql(sql.str(), Name);              ExecSql(sql.str(), toDbName(Name));
958          } catch (Exception e) {          } catch (Exception e) {
959              EndTransaction();              EndTransaction();
960              throw e;              throw e;
961          }          }
962          EndTransaction();          EndTransaction();
963          FireInstrumentNameChanged(Instr, Name);          FireInstrumentNameChanged(Instr, toAbstractName(Name));
964      }      }
965    
966      void InstrumentsDb::MoveInstrument(String Instr, String Dst) {      void InstrumentsDb::MoveInstrument(String Instr, String Dst) {
# Line 1267  namespace LinuxSampler { Line 970  namespace LinuxSampler {
970    
971          BeginTransaction();          BeginTransaction();
972          try {          try {
973              int dirId = GetDirectoryId(GetDirectoryPath(Instr));              int dirId = GetDirectoryId(ParentDir);
974              if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr);              if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
975    
976              String instrName = GetFileName(Instr);              String instrName = GetFileName(Instr);
977              int instrId = GetInstrumentId(dirId, instrName);              int instrId = GetInstrumentId(dirId, instrName);
978              if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr);              if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
979    
980              int dstId = GetDirectoryId(Dst);              int dstId = GetDirectoryId(Dst);
981              if (dstId == -1) throw Exception("Unknown DB directory: " + Dst);              if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst));
982              if (dirId == dstId) {              if (dirId == dstId) {
983                  EndTransaction();                  EndTransaction();
984                  return;                  return;
985              }              }
986    
987              if (GetInstrumentId(dstId, instrName) != -1) {              if (GetInstrumentId(dstId, instrName) != -1) {
988                  throw Exception("Cannot move. Instrument with that name already exists: " + instrName);                  String s = toEscapedPath(instrName);
989                    throw Exception("Cannot move. Instrument with that name already exists: " + s);
990              }              }
991    
992              if (GetDirectoryId(dstId, instrName) != -1) {              if (GetDirectoryId(dstId, instrName) != -1) {
993                  throw Exception("Cannot move. Directory with that name already exists: " + instrName);                  String s = toEscapedPath(instrName);
994                    throw Exception("Cannot move. Directory with that name already exists: " + s);
995              }              }
996    
997              std::stringstream sql;              std::stringstream sql;
# Line 1310  namespace LinuxSampler { Line 1015  namespace LinuxSampler {
1015          BeginTransaction();          BeginTransaction();
1016          try {          try {
1017              int dirId = GetDirectoryId(GetDirectoryPath(Instr));              int dirId = GetDirectoryId(GetDirectoryPath(Instr));
1018              if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr);              if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
1019    
1020              String instrName = GetFileName(Instr);              String instrName = GetFileName(Instr);
1021              int instrId = GetInstrumentId(dirId, instrName);              int instrId = GetInstrumentId(dirId, instrName);
1022              if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr);              if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
1023    
1024              int dstId = GetDirectoryId(Dst);              int dstId = GetDirectoryId(Dst);
1025              if (dstId == -1) throw Exception("Unknown DB directory: " + Dst);              if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst));
1026              if (dirId == dstId) {              if (dirId == dstId) {
1027                  EndTransaction();                  EndTransaction();
1028                  return;                  return;
1029              }              }
1030    
             if (GetInstrumentId(dstId, instrName) != -1) {  
                 throw Exception("Cannot copy. Instrument with that name already exists: " + instrName);  
             }  
   
             if (GetDirectoryId(dstId, instrName) != -1) {  
                 throw Exception("Cannot copy. Directory with that name already exists: " + instrName);  
             }  
   
1031              CopyInstrument(instrId, instrName, dstId, Dst);              CopyInstrument(instrId, instrName, dstId, Dst);
1032          } catch (Exception e) {          } catch (Exception e) {
1033              EndTransaction();              EndTransaction();
# Line 1341  namespace LinuxSampler { Line 1038  namespace LinuxSampler {
1038      }      }
1039    
1040      void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) {      void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) {
1041            if (GetInstrumentId(DstDirId, InstrName) != -1) {
1042                String s = toEscapedPath(InstrName);
1043                throw Exception("Cannot copy. Instrument with that name already exists: " + s);
1044            }
1045    
1046            if (GetDirectoryId(DstDirId, InstrName) != -1) {
1047                String s = toEscapedPath(InstrName);
1048                throw Exception("Cannot copy. Directory with that name already exists: " + s);
1049            }
1050    
1051          DbInstrument i = GetInstrumentInfo(InstrId);          DbInstrument i = GetInstrumentInfo(InstrId);
1052          sqlite3_stmt *pStmt = NULL;          sqlite3_stmt *pStmt = NULL;
1053          std::stringstream sql;          std::stringstream sql;
# Line 1354  namespace LinuxSampler { Line 1061  namespace LinuxSampler {
1061              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));              throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
1062          }          }
1063    
1064          BindTextParam(pStmt, 1, InstrName);          String s = toDbName(InstrName);
1065            BindTextParam(pStmt, 1, s);
1066          BindTextParam(pStmt, 2, i.InstrFile);          BindTextParam(pStmt, 2, i.InstrFile);
1067          BindTextParam(pStmt, 3, i.FormatFamily);          BindTextParam(pStmt, 3, i.FormatFamily);
1068          BindTextParam(pStmt, 4, i.FormatVersion);          BindTextParam(pStmt, 4, i.FormatVersion);
# Line 1379  namespace LinuxSampler { Line 1087  namespace LinuxSampler {
1087          BeginTransaction();          BeginTransaction();
1088          try {          try {
1089              int id = GetInstrumentId(Instr);              int id = GetInstrumentId(Instr);
1090              if(id == -1) throw Exception("Unknown DB instrument: " + Instr);              if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr));
1091    
1092              std::stringstream sql;              std::stringstream sql;
1093              sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP ";              sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP ";
# Line 1394  namespace LinuxSampler { Line 1102  namespace LinuxSampler {
1102          FireInstrumentInfoChanged(Instr);          FireInstrumentInfoChanged(Instr);
1103      }      }
1104    
1105      void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index) {      void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index, ScanProgress* pProgress) {
1106          dmsg(2,("InstrumentsDb: AddInstrumentsFromFile(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index));          dmsg(2,("InstrumentsDb: AddInstrumentsFromFile(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index));
1107                    
1108          if(File.length() < 4) return;          if(File.length() < 4) return;
1109                    
1110          try {          try {
1111              if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) {              if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) {
1112                  AddGigInstruments(DbDir, File, Index);                  if (pProgress != NULL) {
1113                        pProgress->SetStatus(0);
1114                        pProgress->CurrentFile = File;
1115                    }
1116    
1117                    AddGigInstruments(DbDir, File, Index, pProgress);
1118    
1119                    if (pProgress != NULL) {
1120                        pProgress->SetScannedFileCount(pProgress->GetScannedFileCount() + 1);
1121                    }
1122              }              }
1123          } catch(Exception e) {          } catch(Exception e) {
1124              std::cerr << e.Message() << std::endl;              e.PrintMessage();
1125          }          }
1126      }      }
1127    
1128      void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index) {      void InstrumentsDb::AddGigInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) {
1129          dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index));          dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index));
1130          int dirId = GetDirectoryId(DbDir);          int dirId = GetDirectoryId(DbDir);
1131          if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir);          if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir));
1132    
1133          struct stat statBuf;          File f = File(FilePath);
1134          int res = stat(File.c_str(), &statBuf);          if (!f.Exist()) {
         if (res) {  
1135              std::stringstream ss;              std::stringstream ss;
1136              ss << "Fail to stat `" << File << "`: " << strerror(errno);              ss << "Fail to stat `" << FilePath << "`: " << f.GetErrorMsg();
1137              throw Exception(ss.str());              throw Exception(ss.str());
1138          }          }
1139    
1140          if (!S_ISREG(statBuf.st_mode)) {          if (!f.IsFile()) {
1141              std::stringstream ss;              std::stringstream ss;
1142              ss << "`" << File << "` is not a regular file";              ss << "`" << FilePath << "` is not a regular file";
1143              throw Exception(ss.str());              throw Exception(ss.str());
1144          }          }
1145    
1146          RIFF::File* riff = NULL;          RIFF::File* riff = NULL;
1147          gig::File* gig = NULL;          gig::File* gig = NULL;
1148          try {          try {
1149              riff = new RIFF::File(File);              riff = new RIFF::File(FilePath);
1150              gig::File* gig = new gig::File(riff);              gig::File* gig = new gig::File(riff);
1151                            gig->SetAutoLoad(false); // avoid time consuming samples scanning
1152    
1153              std::stringstream sql;              std::stringstream sql;
1154              sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,";              sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,";
1155              sql << "instr_nr,format_family,format_version,instr_size,";              sql << "instr_nr,format_family,format_version,instr_size,";
1156              sql << "description,is_drum,product,artists,keywords) VALUES (";              sql << "description,is_drum,product,artists,keywords) VALUES (";
1157              sql << dirId << ",?,?,?,'GIG',?," << statBuf.st_size << ",?,?,?,?,?)";              sql << dirId << ",?,?,?,'GIG',?," << f.GetSize() << ",?,?,?,?,?)";
1158    
1159              sqlite3_stmt* pStmt = NULL;              sqlite3_stmt* pStmt = NULL;
1160    
# Line 1446  namespace LinuxSampler { Line 1163  namespace LinuxSampler {
1163                  throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));                  throw Exception("DB error: " + ToString(sqlite3_errmsg(db)));
1164              }              }
1165    
1166              BindTextParam(pStmt, 2, File);              String s = toEscapedFsPath(FilePath);
1167                BindTextParam(pStmt, 2, s);
1168              String ver = "";              String ver = "";
1169              if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major);              if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major);
1170              BindTextParam(pStmt, 4, ver);              BindTextParam(pStmt, 4, ver);
1171    
1172              if (Index == -1) {              if (Index == -1) {
1173                  int instrIndex = 0;                  int instrIndex = 0;
1174                    if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed
1175                  gig::Instrument* pInstrument = gig->GetFirstInstrument();                  gig::Instrument* pInstrument = gig->GetFirstInstrument();
1176                  while (pInstrument) {                  while (pInstrument) {
1177                      BindTextParam(pStmt, 7, gig->pInfo->Product);                      BindTextParam(pStmt, 7, gig->pInfo->Product);
1178                      BindTextParam(pStmt, 8, gig->pInfo->Artists);                      BindTextParam(pStmt, 8, gig->pInfo->Artists);
1179                      BindTextParam(pStmt, 9, gig->pInfo->Keywords);                      BindTextParam(pStmt, 9, gig->pInfo->Keywords);
1180                      AddGigInstrument(pStmt, DbDir, dirId, File, pInstrument, instrIndex);                      AddGigInstrument(pStmt, DbDir, dirId, FilePath, pInstrument, instrIndex);
1181    
1182                      instrIndex++;                      instrIndex++;
1183                      pInstrument = gig->GetNextInstrument();                      pInstrument = gig->GetNextInstrument();
1184                  }                  }
1185              } else {              } else {
1186                  gig::Instrument* pInstrument = gig->GetInstrument(Index);                  gig::Instrument* pInstrument;
1187                    if (pProgress == NULL) pInstrument = gig->GetInstrument(Index);
1188                    else pInstrument = gig->GetInstrument(Index, &(pProgress->GigFileProgress));
1189                  if (pInstrument != NULL) {                  if (pInstrument != NULL) {
1190                      BindTextParam(pStmt, 7, gig->pInfo->Product);                      BindTextParam(pStmt, 7, gig->pInfo->Product);
1191                      BindTextParam(pStmt, 8, gig->pInfo->Artists);                      BindTextParam(pStmt, 8, gig->pInfo->Artists);
1192                      BindTextParam(pStmt, 9, gig->pInfo->Keywords);                      BindTextParam(pStmt, 9, gig->pInfo->Keywords);
1193                      AddGigInstrument(pStmt, DbDir, dirId, File, pInstrument, Index);                      AddGigInstrument(pStmt, DbDir, dirId, FilePath, pInstrument, Index);
1194                  }                  }
1195              }              }
1196    
# Line 1480  namespace LinuxSampler { Line 1201  namespace LinuxSampler {
1201              if (gig != NULL) delete gig;              if (gig != NULL) delete gig;
1202              if (riff != NULL) delete riff;              if (riff != NULL) delete riff;
1203              std::stringstream ss;              std::stringstream ss;
1204              ss << "Failed to scan `" << File << "`: " << e.Message;              ss << "Failed to scan `" << FilePath << "`: " << e.Message;
1205                            
1206              throw Exception(ss.str());              throw Exception(ss.str());
1207          } catch (Exception e) {          } catch (Exception e) {
# Line 1490  namespace LinuxSampler { Line 1211  namespace LinuxSampler {
1211          } catch (...) {          } catch (...) {
1212              if (gig != NULL) delete gig;              if (gig != NULL) delete gig;
1213              if (riff != NULL) delete riff;              if (riff != NULL) delete riff;
1214              throw Exception("Failed to scan `" + File + "`");              throw Exception("Failed to scan `" + FilePath + "`");
1215          }          }
1216      }      }
1217    
1218      void InstrumentsDb::AddGigInstrument(sqlite3_stmt* pStmt, String DbDir, int DirId, String File, gig::Instrument* pInstrument, int Index) {      void InstrumentsDb::AddGigInstrument(sqlite3_stmt* pStmt, String DbDir, int DirId, String File, gig::Instrument* pInstrument, int Index) {
1219            dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index));
1220          String name = pInstrument->pInfo->Name;          String name = pInstrument->pInfo->Name;
1221          if (name == "") return;          if (name == "") return;
1222          name = GetUniqueInstrumentName(DirId, name);          name = GetUniqueInstrumentName(DirId, name);
# Line 1502  namespace LinuxSampler { Line 1224  namespace LinuxSampler {
1224          std::stringstream sql2;          std::stringstream sql2;
1225          sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND ";          sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND ";
1226          sql2 << "instr_nr=" << Index;          sql2 << "instr_nr=" << Index;
1227          if (ExecSqlInt(sql2.str(), File) > 0) return;          String s = toEscapedFsPath(File);
1228            if (ExecSqlInt(sql2.str(), s) > 0) return;
1229    
1230          BindTextParam(pStmt, 1, name);          BindTextParam(pStmt, 1, name);
1231          BindIntParam(pStmt, 3, Index);          BindIntParam(pStmt, 3, Index);
# Line 1531  namespace LinuxSampler { Line 1254  namespace LinuxSampler {
1254          FireInstrumentCountChanged(DbDir);          FireInstrumentCountChanged(DbDir);
1255      }      }
1256    
1257      void InstrumentsDb::DirectoryTreeWalk(String Path, DirectoryHandler* pHandler) {      void InstrumentsDb::DirectoryTreeWalk(String AbstractPath, DirectoryHandler* pHandler) {
1258          int DirId = GetDirectoryId(Path);          int DirId = GetDirectoryId(AbstractPath);
1259          if(DirId == -1) throw Exception("Unknown DB directory: " + Path);          if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(AbstractPath));
1260          DirectoryTreeWalk(pHandler, Path, DirId, 0);          DirectoryTreeWalk(pHandler, AbstractPath, DirId, 0);
1261      }      }
1262    
1263      void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String Path, int DirId, int Level) {      void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String AbstractPath, int DirId, int Level) {
1264          if(Level == 1000) throw Exception("Possible infinite loop detected");          if(Level == 1000) throw Exception("Possible infinite loop detected");
1265          pHandler->ProcessDirectory(Path, DirId);          pHandler->ProcessDirectory(AbstractPath, DirId);
1266                    
1267          String s;          String s;
1268          StringListPtr pDirs = GetDirectories(DirId);          StringListPtr pDirs = GetDirectories(DirId);
1269          for(int i = 0; i < pDirs->size(); i++) {          for(int i = 0; i < pDirs->size(); i++) {
1270              if (Path.length() == 1 && Path.at(0) == '/') s = "/" + pDirs->at(i);              if (AbstractPath.length() == 1 && AbstractPath.at(0) == '/') {
1271              else s = Path + "/" + pDirs->at(i);                  s = "/" + pDirs->at(i);
1272                } else {
1273                    s = AbstractPath + "/" + pDirs->at(i);
1274                }
1275              DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1);              DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1);
1276          }          }
1277      }      }
# Line 1557  namespace LinuxSampler { Line 1283  namespace LinuxSampler {
1283          BeginTransaction();          BeginTransaction();
1284          try {          try {
1285              int DirId = GetDirectoryId(Dir);              int DirId = GetDirectoryId(Dir);
1286              if(DirId == -1) throw Exception("Unknown DB directory: " + Dir);              if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
1287    
1288              if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder);              if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder);
1289              else directoryFinder.ProcessDirectory(Dir, DirId);              else directoryFinder.ProcessDirectory(Dir, DirId);
# Line 1577  namespace LinuxSampler { Line 1303  namespace LinuxSampler {
1303          BeginTransaction();          BeginTransaction();
1304          try {          try {
1305              int DirId = GetDirectoryId(Dir);              int DirId = GetDirectoryId(Dir);
1306              if(DirId == -1) throw Exception("Unknown DB directory: " + Dir);              if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir));
1307    
1308              if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder);              if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder);
1309              else instrumentFinder.ProcessDirectory(Dir, DirId);              else instrumentFinder.ProcessDirectory(Dir, DirId);
# Line 1879  namespace LinuxSampler { Line 1605  namespace LinuxSampler {
1605          return Dir.substr(0, i);          return Dir.substr(0, i);
1606      }      }
1607    
1608        void InstrumentsDb::Format() {
1609            DbInstrumentsMutex.Lock();
1610            if (db != NULL) {
1611                sqlite3_close(db);
1612                db = NULL;
1613            }
1614    
1615            if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION;
1616            String bkp = DbFile + ".bkp";
1617            remove(bkp.c_str());
1618            if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) {
1619                DbInstrumentsMutex.Unlock();
1620                throw Exception(String("Failed to backup database: ") + strerror(errno));
1621            }
1622            
1623            String f = DbFile;
1624            DbFile = "";
1625            try { CreateInstrumentsDb(f); }
1626            catch(Exception e) {
1627                DbInstrumentsMutex.Unlock();
1628                throw e;
1629            }
1630            DbInstrumentsMutex.Unlock();
1631            
1632            FireDirectoryCountChanged("/");
1633            FireInstrumentCountChanged("/");
1634        }
1635    
1636      void InstrumentsDb::CheckFileName(String File) {      void InstrumentsDb::CheckFileName(String File) {
1637          if (File.empty()) throw Exception("Invalid file name: " + File);          if (File.empty()) throw Exception("Invalid file name: " + File);
         if (File.find('/') != std::string::npos) {  
             throw Exception("Invalid file name: " + File);  
         }  
1638      }      }
1639    
1640      String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) {      String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) {
# Line 1901  namespace LinuxSampler { Line 1652  namespace LinuxSampler {
1652    
1653          throw Exception("Unable to find an unique name: " + Name);          throw Exception("Unable to find an unique name: " + Name);
1654      }      }
1655    
1656        String InstrumentsDb::toDbName(String AbstractName) {
1657            for (int i = 0; i < AbstractName.length(); i++) {
1658                if (AbstractName.at(i) == '\0') AbstractName.at(i) = '/';
1659            }
1660            return AbstractName;
1661        }
1662    
1663        String InstrumentsDb::toEscapedPath(String AbstractName) {
1664            for (int i = 0; i < AbstractName.length(); i++) {
1665                if (AbstractName.at(i) == '\0')      AbstractName.replace(i++, 1, "\\x2f");
1666                else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\");
1667                else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'");
1668                else if (AbstractName.at(i) == '"')  AbstractName.replace(i++, 1, "\\\"");
1669                else if (AbstractName.at(i) == '\r') AbstractName.replace(i++, 1, "\\r");
1670                else if (AbstractName.at(i) == '\n') AbstractName.replace(i++, 1, "\\n");
1671            }
1672            return AbstractName;
1673        }
1674            
1675        String InstrumentsDb::toEscapedText(String text) {
1676            for (int i = 0; i < text.length(); i++) {
1677                if (text.at(i) == '\\')      text.replace(i++, 1, "\\\\");
1678                else if (text.at(i) == '\'') text.replace(i++, 1, "\\'");
1679                else if (text.at(i) == '"')  text.replace(i++, 1, "\\\"");
1680                else if (text.at(i) == '\r') text.replace(i++, 1, "\\r");
1681                else if (text.at(i) == '\n') text.replace(i++, 1, "\\n");
1682            }
1683            return text;
1684        }
1685        
1686        String InstrumentsDb::toEscapedFsPath(String FsPath) {
1687            return toEscapedText(FsPath);
1688        }
1689        
1690        String InstrumentsDb::toAbstractName(String DbName) {
1691            for (int i = 0; i < DbName.length(); i++) {
1692                if (DbName.at(i) == '/') DbName.at(i) = '\0';
1693            }
1694            return DbName;
1695        }
1696    
1697      void InstrumentsDb::FireDirectoryCountChanged(String Dir) {      void InstrumentsDb::FireDirectoryCountChanged(String Dir) {
1698          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1699              llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir);              llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir);
1700          }          }
1701      }      }
1702        
1703      void InstrumentsDb::FireDirectoryInfoChanged(String Dir) {      void InstrumentsDb::FireDirectoryInfoChanged(String Dir) {
1704          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1705              llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir);              llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir);
1706          }          }
1707      }      }
1708        
1709      void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) {      void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) {
1710          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1711              llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName);              llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName);
1712          }          }
1713      }      }
1714        
1715      void InstrumentsDb::FireInstrumentCountChanged(String Dir) {      void InstrumentsDb::FireInstrumentCountChanged(String Dir) {
1716          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1717              llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir);              llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir);
1718          }          }
1719      }      }
1720        
1721      void InstrumentsDb::FireInstrumentInfoChanged(String Instr) {      void InstrumentsDb::FireInstrumentInfoChanged(String Instr) {
1722          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1723              llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr);              llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr);
1724          }          }
1725      }      }
1726        
1727      void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) {      void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) {
1728          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1729              llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName);              llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName);
1730          }          }
1731      }      }
       
   
     String DirectoryScanner::DbDir;  
     String DirectoryScanner::FsDir;  
     bool DirectoryScanner::Flat;  
   
     void DirectoryScanner::Scan(String DbDir, String FsDir, bool Flat) {  
         dmsg(2,("DirectoryScanner: Scan(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat));  
         if (DbDir.empty() || FsDir.empty()) throw Exception("Directory expected");  
           
         struct stat statBuf;  
         int res = stat(FsDir.c_str(), &statBuf);  
         if (res) {  
             std::stringstream ss;  
             ss << "Fail to stat `" << FsDir << "`: " << strerror(errno);  
             throw Exception(ss.str());  
         }  
1732    
1733          if (!S_ISDIR(statBuf.st_mode)) {      void InstrumentsDb::FireJobStatusChanged(int JobId) {
1734              throw Exception("Directory expected");          for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) {
1735          }              llInstrumentsDbListeners.GetListener(i)->JobStatusChanged(JobId);
           
         DirectoryScanner::DbDir = DbDir;  
         DirectoryScanner::FsDir = FsDir;  
         if (DbDir.at(DbDir.length() - 1) != '/') {  
             DirectoryScanner::DbDir.append("/");  
         }  
         if (FsDir.at(FsDir.length() - 1) != '/') {  
             DirectoryScanner::FsDir.append("/");  
1736          }          }
         DirectoryScanner::Flat = Flat;  
           
         ftw(FsDir.c_str(), FtwCallback, 10);  
1737      }      }
1738    
     int DirectoryScanner::FtwCallback(const char* fpath, const struct stat* sb, int typeflag) {  
         dmsg(2,("DirectoryScanner: FtwCallback(fpath=%s)\n", fpath));  
         if (typeflag != FTW_D) return 0;  
   
         String dir = DbDir;  
         if (!Flat) {  
             String subdir = fpath;  
             if(subdir.length() > FsDir.length()) {  
                 subdir = subdir.substr(FsDir.length());  
                 dir += subdir;  
             }  
         }  
           
         InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();  
         if (!db->DirectoryExist(dir)) db->AddDirectory(dir);  
   
         db->AddInstrumentsNonrecursive(dir, String(fpath));  
   
         return 0;  
     };  
   
1739  } // namespace LinuxSampler  } // namespace LinuxSampler
   
 #endif // HAVE_SQLITE3  

Legend:
Removed from v.1187  
changed lines
  Added in v.1717

  ViewVC Help
Powered by ViewVC