20 |
|
|
21 |
#include "InstrumentsDb.h" |
#include "InstrumentsDb.h" |
22 |
|
|
23 |
#if HAVE_SQLITE3 |
#include "../common/global_private.h" |
24 |
|
|
25 |
#include <iostream> |
#include <iostream> |
26 |
#include <sstream> |
#include <sstream> |
27 |
|
#include <vector> |
28 |
#include <dirent.h> |
#include <dirent.h> |
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 |
|
|
|
void DbInstrument::Copy(const DbInstrument& Instr) { |
|
|
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); |
|
|
} |
|
|
} |
|
|
|
|
36 |
InstrumentsDb* InstrumentsDb::pInstrumentsDb = new InstrumentsDb; |
InstrumentsDb* InstrumentsDb::pInstrumentsDb = new InstrumentsDb; |
37 |
|
|
38 |
void InstrumentsDb::CreateInstrumentsDb(String File) { |
void InstrumentsDb::CreateInstrumentsDb(String File) { |
58 |
|
|
59 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
60 |
|
|
61 |
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, '/');"; |
62 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
63 |
|
|
64 |
sql = |
sql = |
127 |
sqlite3* InstrumentsDb::GetDb() { |
sqlite3* InstrumentsDb::GetDb() { |
128 |
if ( db != NULL) return db; |
if ( db != NULL) return db; |
129 |
|
|
130 |
if (DbFile.empty()) DbFile = "/var/lib/linuxsampler/instruments.db"; |
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
131 |
|
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
132 |
|
if (DbFile.find("~") == 0) |
133 |
|
DbFile.replace(0, 1, getenv("HOME")); |
134 |
|
#endif |
135 |
int rc = sqlite3_open(DbFile.c_str(), &db); |
int rc = sqlite3_open(DbFile.c_str(), &db); |
136 |
if (rc) { |
if (rc) { |
137 |
sqlite3_close(db); |
sqlite3_close(db); |
140 |
} |
} |
141 |
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); |
142 |
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."); } |
143 |
|
|
144 |
|
// TODO: remove this in the next version |
145 |
|
try { |
146 |
|
int i = ExecSqlInt("SELECT parent_dir_id FROM instr_dirs WHERE dir_id=0"); |
147 |
|
// The parent ID of the root directory should be -2 now. |
148 |
|
if(i != -2) ExecSql("UPDATE instr_dirs SET parent_dir_id=-2 WHERE dir_id=0"); |
149 |
|
} catch(Exception e) { } |
150 |
|
//////////////////////////////////////// |
151 |
|
|
152 |
return db; |
return db; |
153 |
} |
} |
161 |
|
|
162 |
int count = ExecSqlInt(sql.str()); |
int count = ExecSqlInt(sql.str()); |
163 |
|
|
|
// 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--; |
|
164 |
return count; |
return count; |
165 |
} |
} |
166 |
|
|
182 |
throw e; |
throw e; |
183 |
} |
} |
184 |
EndTransaction(); |
EndTransaction(); |
185 |
if (i == -1) throw Exception("Unkown DB directory: " + Dir); |
if (i == -1) throw Exception("Unkown DB directory: " + toEscapedPath(Dir)); |
186 |
|
|
187 |
return i; |
return i; |
188 |
} |
} |
201 |
BeginTransaction(); |
BeginTransaction(); |
202 |
try { |
try { |
203 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
204 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
205 |
|
|
206 |
StringListPtr pDirs; |
StringListPtr pDirs; |
207 |
if (Recursive) { |
if (Recursive) { |
224 |
std::stringstream sql; |
std::stringstream sql; |
225 |
sql << "SELECT dir_name FROM instr_dirs "; |
sql << "SELECT dir_name FROM instr_dirs "; |
226 |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
227 |
return ExecSqlStringList(sql.str()); |
StringListPtr dirs = ExecSqlStringList(sql.str()); |
228 |
|
|
229 |
|
for (int i = 0; i < dirs->size(); i++) { |
230 |
|
for (int j = 0; j < dirs->at(i).length(); j++) { |
231 |
|
if (dirs->at(i).at(j) == '/') dirs->at(i).at(j) = '\0'; |
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
|
return dirs; |
236 |
} |
} |
237 |
|
|
238 |
int InstrumentsDb::GetDirectoryId(String Dir) { |
int InstrumentsDb::GetDirectoryId(String Dir) { |
261 |
|
|
262 |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
263 |
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())); |
264 |
|
DirName = toDbName(DirName); |
265 |
std::stringstream sql; |
std::stringstream sql; |
266 |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
267 |
sql << ParentDirId << " AND dir_name=?"; |
sql << ParentDirId << " AND dir_name=?"; |
314 |
|
|
315 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
316 |
if(ParentDir.empty() || dirName.empty()) { |
if(ParentDir.empty() || dirName.empty()) { |
317 |
throw Exception("Failed to add DB directory: " + Dir); |
throw Exception("Failed to add DB directory: " + toEscapedPath(Dir)); |
318 |
} |
} |
319 |
|
|
320 |
int id = GetDirectoryId(ParentDir); |
int id = GetDirectoryId(ParentDir); |
321 |
if (id == -1) throw Exception("DB directory doesn't exist: " + ParentDir); |
if (id == -1) throw Exception("DB directory doesn't exist: " + toEscapedPath(ParentDir)); |
322 |
int id2 = GetDirectoryId(id, dirName); |
int id2 = GetDirectoryId(id, dirName); |
323 |
if (id2 != -1) throw Exception("DB directory already exist: " + Dir); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(Dir)); |
324 |
id2 = GetInstrumentId(id, dirName); |
id2 = GetInstrumentId(id, dirName); |
325 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + Dir); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(Dir)); |
326 |
|
|
327 |
std::stringstream sql; |
std::stringstream sql; |
328 |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
329 |
sql << id << ", ?)"; |
sql << id << ", ?)"; |
330 |
|
|
331 |
ExecSql(sql.str(), dirName); |
ExecSql(sql.str(), toDbName(dirName)); |
332 |
} catch (Exception e) { |
} catch (Exception e) { |
333 |
EndTransaction(); |
EndTransaction(); |
334 |
throw e; |
throw e; |
347 |
BeginTransaction(); |
BeginTransaction(); |
348 |
try { |
try { |
349 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
350 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
351 |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
352 |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
353 |
if (Force) RemoveDirectoryContent(dirId); |
if (Force) RemoveDirectoryContent(dirId); |
434 |
|
|
435 |
try { |
try { |
436 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
437 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
438 |
|
|
439 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
440 |
std::stringstream sql; |
std::stringstream sql; |
457 |
if (res != SQLITE_DONE) { |
if (res != SQLITE_DONE) { |
458 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
459 |
} else { |
} else { |
460 |
throw Exception("Unknown DB directory: " + Dir); |
throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
461 |
} |
} |
462 |
} |
} |
463 |
|
|
474 |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
475 |
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())); |
476 |
CheckFileName(Name); |
CheckFileName(Name); |
477 |
|
String dbName = toDbName(Name); |
478 |
|
|
479 |
BeginTransaction(); |
BeginTransaction(); |
480 |
try { |
try { |
481 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
482 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedText(Dir)); |
483 |
|
|
484 |
std::stringstream sql; |
std::stringstream sql; |
485 |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
486 |
|
|
487 |
int parent = ExecSqlInt(sql.str()); |
int parent = ExecSqlInt(sql.str()); |
488 |
if (parent == -1) throw Exception("Unknown parent directory: " + Dir); |
if (parent == -1) throw Exception("Unknown parent directory: " + toEscapedPath(Dir)); |
489 |
if (GetDirectoryId(parent, Name) != -1) { |
|
490 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
if (GetDirectoryId(parent, dbName) != -1) { |
491 |
|
String s = toEscapedPath(Name); |
492 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
493 |
} |
} |
494 |
|
|
495 |
if (GetInstrumentId(parent, Name) != -1) { |
if (GetInstrumentId(parent, dbName) != -1) { |
496 |
throw Exception("Cannot rename. Instrument with that name exist: " + Dir); |
throw Exception("Cannot rename. Instrument with that name exist: " + toEscapedPath(Dir)); |
497 |
} |
} |
498 |
|
|
499 |
sql.str(""); |
sql.str(""); |
500 |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
501 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), dbName); |
502 |
} catch (Exception e) { |
} catch (Exception e) { |
503 |
EndTransaction(); |
EndTransaction(); |
504 |
throw e; |
throw e; |
505 |
} |
} |
506 |
|
|
507 |
EndTransaction(); |
EndTransaction(); |
508 |
FireDirectoryNameChanged(Dir, Name); |
FireDirectoryNameChanged(Dir, toAbstractName(Name)); |
509 |
} |
} |
510 |
|
|
511 |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
518 |
BeginTransaction(); |
BeginTransaction(); |
519 |
try { |
try { |
520 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
521 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
522 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
523 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
524 |
if (dirId == dstId) { |
if (dirId == dstId) { |
525 |
throw Exception("Cannot move directory to itself"); |
throw Exception("Cannot move directory to itself"); |
526 |
} |
} |
536 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
537 |
|
|
538 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
539 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
540 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
541 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
542 |
|
|
543 |
std::stringstream sql; |
std::stringstream sql; |
544 |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
564 |
BeginTransaction(); |
BeginTransaction(); |
565 |
try { |
try { |
566 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
567 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
568 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
569 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
570 |
if (dirId == dstId) { |
if (dirId == dstId) { |
571 |
throw Exception("Cannot copy directory to itself"); |
throw Exception("Cannot copy directory to itself"); |
572 |
} |
} |
582 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
583 |
|
|
584 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
585 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
586 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
587 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
588 |
|
|
589 |
DirectoryCopier directoryCopier(ParentDir, Dst); |
DirectoryCopier directoryCopier(ParentDir, Dst); |
590 |
DirectoryTreeWalk(Dir, &directoryCopier); |
DirectoryTreeWalk(Dir, &directoryCopier); |
602 |
BeginTransaction(); |
BeginTransaction(); |
603 |
try { |
try { |
604 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
605 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
606 |
|
|
607 |
std::stringstream sql; |
std::stringstream sql; |
608 |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
618 |
FireDirectoryInfoChanged(Dir); |
FireDirectoryInfoChanged(Dir); |
619 |
} |
} |
620 |
|
|
621 |
void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index) { |
int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) { |
622 |
|
dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground)); |
623 |
|
if(!bBackground) { |
624 |
|
switch (Mode) { |
625 |
|
case NON_RECURSIVE: |
626 |
|
AddInstrumentsNonrecursive(DbDir, FsDir); |
627 |
|
break; |
628 |
|
case RECURSIVE: |
629 |
|
AddInstrumentsRecursive(DbDir, FsDir); |
630 |
|
break; |
631 |
|
case FLAT: |
632 |
|
AddInstrumentsRecursive(DbDir, FsDir, true); |
633 |
|
break; |
634 |
|
default: |
635 |
|
throw Exception("Unknown scan mode"); |
636 |
|
} |
637 |
|
|
638 |
|
return -1; |
639 |
|
} |
640 |
|
|
641 |
|
ScanJob job; |
642 |
|
int jobId = Jobs.AddJob(job); |
643 |
|
InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir)); |
644 |
|
|
645 |
|
return jobId; |
646 |
|
} |
647 |
|
|
648 |
|
int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { |
649 |
|
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); |
650 |
|
if(!bBackground) { |
651 |
|
AddInstruments(DbDir, FilePath, Index); |
652 |
|
return -1; |
653 |
|
} |
654 |
|
|
655 |
|
ScanJob job; |
656 |
|
int jobId = Jobs.AddJob(job); |
657 |
|
InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index)); |
658 |
|
|
659 |
|
return jobId; |
660 |
|
} |
661 |
|
|
662 |
|
void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { |
663 |
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)); |
664 |
if (DbDir.empty() || FilePath.empty()) return; |
if (DbDir.empty() || FilePath.empty()) return; |
665 |
|
|
666 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
667 |
try { |
try { |
668 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
669 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir)); |
670 |
|
|
671 |
struct stat statBuf; |
struct stat statBuf; |
672 |
int res = stat(FilePath.c_str(), &statBuf); |
int res = stat(FilePath.c_str(), &statBuf); |
676 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
677 |
} |
} |
678 |
|
|
679 |
if (S_ISREG(statBuf.st_mode)) { |
if (!S_ISREG(statBuf.st_mode)) { |
|
AddInstrumentsFromFile(DbDir, FilePath, Index); |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
return; |
|
|
} |
|
|
|
|
|
if (!S_ISDIR(statBuf.st_mode)) { |
|
|
DbInstrumentsMutex.Unlock(); |
|
|
return; |
|
|
} |
|
|
|
|
|
if (Index != -1) { |
|
680 |
std::stringstream ss; |
std::stringstream ss; |
681 |
ss << "`" << FilePath << "` is directory, not an instrument file"; |
ss << "`" << FilePath << "` is not an instrument file"; |
682 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
683 |
} |
} |
684 |
|
|
685 |
AddInstrumentsRecursive(DbDir, FilePath, false); |
AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress); |
686 |
} catch (Exception e) { |
} catch (Exception e) { |
687 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
688 |
throw e; |
throw e; |
691 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
692 |
} |
} |
693 |
|
|
694 |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir) { |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) { |
695 |
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())); |
696 |
if (DbDir.empty() || FsDir.empty()) return; |
if (DbDir.empty() || FsDir.empty()) return; |
697 |
|
|
698 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
699 |
try { |
try { |
700 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
701 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
702 |
|
|
703 |
struct stat statBuf; |
struct stat statBuf; |
704 |
int res = stat(FsDir.c_str(), &statBuf); |
int res = stat(FsDir.c_str(), &statBuf); |
731 |
continue; |
continue; |
732 |
} |
} |
733 |
|
|
734 |
AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name)); |
AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name), -1, pProgress); |
735 |
pEnt = readdir(pDir); |
pEnt = readdir(pDir); |
736 |
} |
} |
737 |
|
|
749 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
750 |
} |
} |
751 |
|
|
752 |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat) { |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { |
753 |
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)); |
754 |
DirectoryScanner::Scan(DbDir, FsDir, Flat); |
if (pProgress != NULL) { |
755 |
|
pProgress->SetTotalFileCount(InstrumentFileCounter::Count(FsDir)); |
756 |
|
} |
757 |
|
|
758 |
|
DirectoryScanner::Scan(DbDir, FsDir, Flat, pProgress); |
759 |
} |
} |
760 |
|
|
761 |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
787 |
} |
} |
788 |
EndTransaction(); |
EndTransaction(); |
789 |
|
|
790 |
if (i == -1) throw Exception("Unknown Db directory: " + Dir); |
if (i == -1) throw Exception("Unknown Db directory: " + toEscapedPath(Dir)); |
791 |
return i; |
return i; |
792 |
} |
} |
793 |
|
|
803 |
BeginTransaction(); |
BeginTransaction(); |
804 |
try { |
try { |
805 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
806 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
807 |
|
|
808 |
StringListPtr pInstrs; |
StringListPtr pInstrs; |
809 |
|
|
817 |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
818 |
|
|
819 |
pInstrs = ExecSqlStringList(sql.str()); |
pInstrs = ExecSqlStringList(sql.str()); |
820 |
|
// Converting to abstract names |
821 |
|
for (int i = 0; i < pInstrs->size(); i++) { |
822 |
|
for (int j = 0; j < pInstrs->at(i).length(); j++) { |
823 |
|
if (pInstrs->at(i).at(j) == '/') pInstrs->at(i).at(j) = '\0'; |
824 |
|
} |
825 |
|
} |
826 |
} |
} |
827 |
EndTransaction(); |
EndTransaction(); |
828 |
return pInstrs; |
return pInstrs; |
847 |
std::stringstream sql; |
std::stringstream sql; |
848 |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
849 |
sql << DirId << " AND instr_name=?"; |
sql << DirId << " AND instr_name=?"; |
850 |
return ExecSqlInt(sql.str(), InstrName); |
return ExecSqlInt(sql.str(), toDbName(InstrName)); |
851 |
} |
} |
852 |
|
|
853 |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
854 |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
855 |
std::stringstream sql; |
std::stringstream sql; |
856 |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
857 |
return ExecSqlString(sql.str()); |
return toAbstractName(ExecSqlString(sql.str())); |
858 |
} |
} |
859 |
|
|
860 |
void InstrumentsDb::RemoveInstrument(String Instr) { |
void InstrumentsDb::RemoveInstrument(String Instr) { |
866 |
try { |
try { |
867 |
int instrId = GetInstrumentId(Instr); |
int instrId = GetInstrumentId(Instr); |
868 |
if(instrId == -1) { |
if(instrId == -1) { |
869 |
throw Exception("The specified instrument does not exist: " + Instr); |
throw Exception("The specified instrument does not exist: " + toEscapedPath(Instr)); |
870 |
} |
} |
871 |
RemoveInstrument(instrId); |
RemoveInstrument(instrId); |
872 |
} catch (Exception e) { |
} catch (Exception e) { |
901 |
BeginTransaction(); |
BeginTransaction(); |
902 |
try { |
try { |
903 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
904 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
905 |
i = GetInstrumentInfo(id); |
i = GetInstrumentInfo(id); |
906 |
} catch (Exception e) { |
} catch (Exception e) { |
907 |
EndTransaction(); |
EndTransaction(); |
960 |
BeginTransaction(); |
BeginTransaction(); |
961 |
try { |
try { |
962 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
963 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
964 |
|
|
965 |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
966 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
967 |
|
|
968 |
if (GetInstrumentId(dirId, Name) != -1) { |
if (GetInstrumentId(dirId, Name) != -1) { |
969 |
throw Exception("Cannot rename. Instrument with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
970 |
|
throw Exception("Cannot rename. Instrument with that name already exists: " + s); |
971 |
} |
} |
972 |
|
|
973 |
if (GetDirectoryId(dirId, Name) != -1) { |
if (GetDirectoryId(dirId, Name) != -1) { |
974 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
975 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
976 |
} |
} |
977 |
|
|
978 |
std::stringstream sql; |
std::stringstream sql; |
979 |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
980 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), toDbName(Name)); |
981 |
} catch (Exception e) { |
} catch (Exception e) { |
982 |
EndTransaction(); |
EndTransaction(); |
983 |
throw e; |
throw e; |
984 |
} |
} |
985 |
EndTransaction(); |
EndTransaction(); |
986 |
FireInstrumentNameChanged(Instr, Name); |
FireInstrumentNameChanged(Instr, toAbstractName(Name)); |
987 |
} |
} |
988 |
|
|
989 |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
993 |
|
|
994 |
BeginTransaction(); |
BeginTransaction(); |
995 |
try { |
try { |
996 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(ParentDir); |
997 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
998 |
|
|
999 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
1000 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
1001 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1002 |
|
|
1003 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1004 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1005 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1006 |
EndTransaction(); |
EndTransaction(); |
1007 |
return; |
return; |
1008 |
} |
} |
1009 |
|
|
1010 |
if (GetInstrumentId(dstId, instrName) != -1) { |
if (GetInstrumentId(dstId, instrName) != -1) { |
1011 |
throw Exception("Cannot move. Instrument with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
1012 |
|
throw Exception("Cannot move. Instrument with that name already exists: " + s); |
1013 |
} |
} |
1014 |
|
|
1015 |
if (GetDirectoryId(dstId, instrName) != -1) { |
if (GetDirectoryId(dstId, instrName) != -1) { |
1016 |
throw Exception("Cannot move. Directory with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
1017 |
|
throw Exception("Cannot move. Directory with that name already exists: " + s); |
1018 |
} |
} |
1019 |
|
|
1020 |
std::stringstream sql; |
std::stringstream sql; |
1038 |
BeginTransaction(); |
BeginTransaction(); |
1039 |
try { |
try { |
1040 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
1041 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1042 |
|
|
1043 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
1044 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
1045 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1046 |
|
|
1047 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1048 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1049 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1050 |
EndTransaction(); |
EndTransaction(); |
1051 |
return; |
return; |
1052 |
} |
} |
1053 |
|
|
|
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); |
|
|
} |
|
|
|
|
1054 |
CopyInstrument(instrId, instrName, dstId, Dst); |
CopyInstrument(instrId, instrName, dstId, Dst); |
1055 |
} catch (Exception e) { |
} catch (Exception e) { |
1056 |
EndTransaction(); |
EndTransaction(); |
1061 |
} |
} |
1062 |
|
|
1063 |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
1064 |
|
if (GetInstrumentId(DstDirId, InstrName) != -1) { |
1065 |
|
String s = toEscapedPath(InstrName); |
1066 |
|
throw Exception("Cannot copy. Instrument with that name already exists: " + s); |
1067 |
|
} |
1068 |
|
|
1069 |
|
if (GetDirectoryId(DstDirId, InstrName) != -1) { |
1070 |
|
String s = toEscapedPath(InstrName); |
1071 |
|
throw Exception("Cannot copy. Directory with that name already exists: " + s); |
1072 |
|
} |
1073 |
|
|
1074 |
DbInstrument i = GetInstrumentInfo(InstrId); |
DbInstrument i = GetInstrumentInfo(InstrId); |
1075 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1076 |
std::stringstream sql; |
std::stringstream sql; |
1084 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1085 |
} |
} |
1086 |
|
|
1087 |
BindTextParam(pStmt, 1, InstrName); |
String s = toDbName(InstrName); |
1088 |
|
BindTextParam(pStmt, 1, s); |
1089 |
BindTextParam(pStmt, 2, i.InstrFile); |
BindTextParam(pStmt, 2, i.InstrFile); |
1090 |
BindTextParam(pStmt, 3, i.FormatFamily); |
BindTextParam(pStmt, 3, i.FormatFamily); |
1091 |
BindTextParam(pStmt, 4, i.FormatVersion); |
BindTextParam(pStmt, 4, i.FormatVersion); |
1110 |
BeginTransaction(); |
BeginTransaction(); |
1111 |
try { |
try { |
1112 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
1113 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1114 |
|
|
1115 |
std::stringstream sql; |
std::stringstream sql; |
1116 |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
1125 |
FireInstrumentInfoChanged(Instr); |
FireInstrumentInfoChanged(Instr); |
1126 |
} |
} |
1127 |
|
|
1128 |
void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index) { |
void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index, ScanProgress* pProgress) { |
1129 |
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)); |
1130 |
|
|
1131 |
if(File.length() < 4) return; |
if(File.length() < 4) return; |
1132 |
|
|
1133 |
try { |
try { |
1134 |
if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) { |
if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) { |
1135 |
AddGigInstruments(DbDir, File, Index); |
if (pProgress != NULL) { |
1136 |
|
pProgress->SetStatus(0); |
1137 |
|
pProgress->CurrentFile = File; |
1138 |
|
} |
1139 |
|
|
1140 |
|
AddGigInstruments(DbDir, File, Index, pProgress); |
1141 |
|
|
1142 |
|
if (pProgress != NULL) { |
1143 |
|
pProgress->SetScannedFileCount(pProgress->GetScannedFileCount() + 1); |
1144 |
|
} |
1145 |
} |
} |
1146 |
} catch(Exception e) { |
} catch(Exception e) { |
1147 |
std::cerr << e.Message() << std::endl; |
std::cerr << e.Message() << std::endl; |
1148 |
} |
} |
1149 |
} |
} |
1150 |
|
|
1151 |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index) { |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index, ScanProgress* pProgress) { |
1152 |
dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddGigInstruments(DbDir=%s,File=%s,Index=%d)\n", DbDir.c_str(), File.c_str(), Index)); |
1153 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
1154 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
1155 |
|
|
1156 |
struct stat statBuf; |
struct stat statBuf; |
1157 |
int res = stat(File.c_str(), &statBuf); |
int res = stat(File.c_str(), &statBuf); |
1172 |
try { |
try { |
1173 |
riff = new RIFF::File(File); |
riff = new RIFF::File(File); |
1174 |
gig::File* gig = new gig::File(riff); |
gig::File* gig = new gig::File(riff); |
1175 |
|
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
1176 |
|
|
1177 |
std::stringstream sql; |
std::stringstream sql; |
1178 |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
1179 |
sql << "instr_nr,format_family,format_version,instr_size,"; |
sql << "instr_nr,format_family,format_version,instr_size,"; |
1187 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1188 |
} |
} |
1189 |
|
|
1190 |
BindTextParam(pStmt, 2, File); |
String s = toEscapedFsPath(File); |
1191 |
|
BindTextParam(pStmt, 2, s); |
1192 |
String ver = ""; |
String ver = ""; |
1193 |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
1194 |
BindTextParam(pStmt, 4, ver); |
BindTextParam(pStmt, 4, ver); |
1195 |
|
|
1196 |
if (Index == -1) { |
if (Index == -1) { |
1197 |
int instrIndex = 0; |
int instrIndex = 0; |
1198 |
|
if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed |
1199 |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
1200 |
while (pInstrument) { |
while (pInstrument) { |
1201 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1207 |
pInstrument = gig->GetNextInstrument(); |
pInstrument = gig->GetNextInstrument(); |
1208 |
} |
} |
1209 |
} else { |
} else { |
1210 |
gig::Instrument* pInstrument = gig->GetInstrument(Index); |
gig::Instrument* pInstrument; |
1211 |
|
if (pProgress == NULL) pInstrument = gig->GetInstrument(Index); |
1212 |
|
else pInstrument = gig->GetInstrument(Index, &(pProgress->GigFileProgress)); |
1213 |
if (pInstrument != NULL) { |
if (pInstrument != NULL) { |
1214 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1215 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1247 |
std::stringstream sql2; |
std::stringstream sql2; |
1248 |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
1249 |
sql2 << "instr_nr=" << Index; |
sql2 << "instr_nr=" << Index; |
1250 |
if (ExecSqlInt(sql2.str(), File) > 0) return; |
String s = toEscapedFsPath(File); |
1251 |
|
if (ExecSqlInt(sql2.str(), s) > 0) return; |
1252 |
|
|
1253 |
BindTextParam(pStmt, 1, name); |
BindTextParam(pStmt, 1, name); |
1254 |
BindIntParam(pStmt, 3, Index); |
BindIntParam(pStmt, 3, Index); |
1277 |
FireInstrumentCountChanged(DbDir); |
FireInstrumentCountChanged(DbDir); |
1278 |
} |
} |
1279 |
|
|
1280 |
void InstrumentsDb::DirectoryTreeWalk(String Path, DirectoryHandler* pHandler) { |
void InstrumentsDb::DirectoryTreeWalk(String AbstractPath, DirectoryHandler* pHandler) { |
1281 |
int DirId = GetDirectoryId(Path); |
int DirId = GetDirectoryId(AbstractPath); |
1282 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Path); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(AbstractPath)); |
1283 |
DirectoryTreeWalk(pHandler, Path, DirId, 0); |
DirectoryTreeWalk(pHandler, AbstractPath, DirId, 0); |
1284 |
} |
} |
1285 |
|
|
1286 |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String Path, int DirId, int Level) { |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String AbstractPath, int DirId, int Level) { |
1287 |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
1288 |
pHandler->ProcessDirectory(Path, DirId); |
pHandler->ProcessDirectory(AbstractPath, DirId); |
1289 |
|
|
1290 |
String s; |
String s; |
1291 |
StringListPtr pDirs = GetDirectories(DirId); |
StringListPtr pDirs = GetDirectories(DirId); |
1292 |
for(int i = 0; i < pDirs->size(); i++) { |
for(int i = 0; i < pDirs->size(); i++) { |
1293 |
if (Path.length() == 1 && Path.at(0) == '/') s = "/" + pDirs->at(i); |
if (AbstractPath.length() == 1 && AbstractPath.at(0) == '/') { |
1294 |
else s = Path + "/" + pDirs->at(i); |
s = "/" + pDirs->at(i); |
1295 |
|
} else { |
1296 |
|
s = AbstractPath + "/" + pDirs->at(i); |
1297 |
|
} |
1298 |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
1299 |
} |
} |
1300 |
} |
} |
1306 |
BeginTransaction(); |
BeginTransaction(); |
1307 |
try { |
try { |
1308 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1309 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1310 |
|
|
1311 |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
1312 |
else directoryFinder.ProcessDirectory(Dir, DirId); |
else directoryFinder.ProcessDirectory(Dir, DirId); |
1326 |
BeginTransaction(); |
BeginTransaction(); |
1327 |
try { |
try { |
1328 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1329 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1330 |
|
|
1331 |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
1332 |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
1628 |
return Dir.substr(0, i); |
return Dir.substr(0, i); |
1629 |
} |
} |
1630 |
|
|
1631 |
|
void InstrumentsDb::Format() { |
1632 |
|
DbInstrumentsMutex.Lock(); |
1633 |
|
if (db != NULL) { |
1634 |
|
sqlite3_close(db); |
1635 |
|
db = NULL; |
1636 |
|
} |
1637 |
|
|
1638 |
|
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
1639 |
|
String bkp = DbFile + ".bkp"; |
1640 |
|
remove(bkp.c_str()); |
1641 |
|
if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) { |
1642 |
|
DbInstrumentsMutex.Unlock(); |
1643 |
|
throw Exception(String("Failed to backup database: ") + strerror(errno)); |
1644 |
|
} |
1645 |
|
|
1646 |
|
String f = DbFile; |
1647 |
|
DbFile = ""; |
1648 |
|
try { CreateInstrumentsDb(f); } |
1649 |
|
catch(Exception e) { |
1650 |
|
DbInstrumentsMutex.Unlock(); |
1651 |
|
throw e; |
1652 |
|
} |
1653 |
|
DbInstrumentsMutex.Unlock(); |
1654 |
|
|
1655 |
|
FireDirectoryCountChanged("/"); |
1656 |
|
FireInstrumentCountChanged("/"); |
1657 |
|
} |
1658 |
|
|
1659 |
void InstrumentsDb::CheckFileName(String File) { |
void InstrumentsDb::CheckFileName(String File) { |
1660 |
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); |
|
|
} |
|
1661 |
} |
} |
1662 |
|
|
1663 |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
1675 |
|
|
1676 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1677 |
} |
} |
1678 |
|
|
1679 |
|
String InstrumentsDb::toDbName(String AbstractName) { |
1680 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1681 |
|
if (AbstractName.at(i) == '\0') AbstractName.at(i) = '/'; |
1682 |
|
} |
1683 |
|
return AbstractName; |
1684 |
|
} |
1685 |
|
|
1686 |
|
String InstrumentsDb::toEscapedPath(String AbstractName) { |
1687 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1688 |
|
if (AbstractName.at(i) == '\0') AbstractName.replace(i++, 1, "\\x2f"); |
1689 |
|
else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\"); |
1690 |
|
else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'"); |
1691 |
|
else if (AbstractName.at(i) == '"') AbstractName.replace(i++, 1, "\\\""); |
1692 |
|
else if (AbstractName.at(i) == '\r') AbstractName.replace(i++, 1, "\\r"); |
1693 |
|
else if (AbstractName.at(i) == '\n') AbstractName.replace(i++, 1, "\\n"); |
1694 |
|
} |
1695 |
|
return AbstractName; |
1696 |
|
} |
1697 |
|
|
1698 |
|
String InstrumentsDb::toEscapedText(String text) { |
1699 |
|
for (int i = 0; i < text.length(); i++) { |
1700 |
|
if (text.at(i) == '\\') text.replace(i++, 1, "\\\\"); |
1701 |
|
else if (text.at(i) == '\'') text.replace(i++, 1, "\\'"); |
1702 |
|
else if (text.at(i) == '"') text.replace(i++, 1, "\\\""); |
1703 |
|
else if (text.at(i) == '\r') text.replace(i++, 1, "\\r"); |
1704 |
|
else if (text.at(i) == '\n') text.replace(i++, 1, "\\n"); |
1705 |
|
} |
1706 |
|
return text; |
1707 |
|
} |
1708 |
|
|
1709 |
|
String InstrumentsDb::toEscapedFsPath(String FsPath) { |
1710 |
|
return toEscapedText(FsPath); |
1711 |
|
} |
1712 |
|
|
1713 |
|
String InstrumentsDb::toAbstractName(String DbName) { |
1714 |
|
for (int i = 0; i < DbName.length(); i++) { |
1715 |
|
if (DbName.at(i) == '/') DbName.at(i) = '\0'; |
1716 |
|
} |
1717 |
|
return DbName; |
1718 |
|
} |
1719 |
|
|
1720 |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
1721 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1722 |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
1723 |
} |
} |
1724 |
} |
} |
1725 |
|
|
1726 |
void InstrumentsDb::FireDirectoryInfoChanged(String Dir) { |
void InstrumentsDb::FireDirectoryInfoChanged(String Dir) { |
1727 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1728 |
llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir); |
1729 |
} |
} |
1730 |
} |
} |
1731 |
|
|
1732 |
void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) { |
void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) { |
1733 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1734 |
llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName); |
llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName); |
1735 |
} |
} |
1736 |
} |
} |
1737 |
|
|
1738 |
void InstrumentsDb::FireInstrumentCountChanged(String Dir) { |
void InstrumentsDb::FireInstrumentCountChanged(String Dir) { |
1739 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1740 |
llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir); |
1741 |
} |
} |
1742 |
} |
} |
1743 |
|
|
1744 |
void InstrumentsDb::FireInstrumentInfoChanged(String Instr) { |
void InstrumentsDb::FireInstrumentInfoChanged(String Instr) { |
1745 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1746 |
llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr); |
llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr); |
1747 |
} |
} |
1748 |
} |
} |
1749 |
|
|
1750 |
void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) { |
void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) { |
1751 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1752 |
llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName); |
llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName); |
1753 |
} |
} |
1754 |
} |
} |
|
|
|
1755 |
|
|
1756 |
String DirectoryScanner::DbDir; |
void InstrumentsDb::FireJobStatusChanged(int JobId) { |
1757 |
String DirectoryScanner::FsDir; |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1758 |
bool DirectoryScanner::Flat; |
llInstrumentsDbListeners.GetListener(i)->JobStatusChanged(JobId); |
|
|
|
|
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()); |
|
|
} |
|
|
|
|
|
if (!S_ISDIR(statBuf.st_mode)) { |
|
|
throw Exception("Directory expected"); |
|
|
} |
|
|
|
|
|
DirectoryScanner::DbDir = DbDir; |
|
|
DirectoryScanner::FsDir = FsDir; |
|
|
if (DbDir.at(DbDir.length() - 1) != '/') { |
|
|
DirectoryScanner::DbDir.append("/"); |
|
|
} |
|
|
if (FsDir.at(FsDir.length() - 1) != '/') { |
|
|
DirectoryScanner::FsDir.append("/"); |
|
1759 |
} |
} |
|
DirectoryScanner::Flat = Flat; |
|
|
|
|
|
ftw(FsDir.c_str(), FtwCallback, 10); |
|
1760 |
} |
} |
1761 |
|
|
|
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; |
|
|
}; |
|
|
|
|
1762 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
|
|
|
|
#endif // HAVE_SQLITE3 |
|