1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007 Grigor Iliev * |
* Copyright (C) 2007-2009 Grigor Iliev, Benno Senoner * |
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 * |
20 |
|
|
21 |
#include "InstrumentsDb.h" |
#include "InstrumentsDb.h" |
22 |
|
|
23 |
#if HAVE_SQLITE3 |
#include "../common/File.h" |
24 |
|
#include "../common/Path.h" |
25 |
|
#include "../common/global_private.h" |
26 |
|
|
27 |
#include <iostream> |
#include <iostream> |
28 |
#include <sstream> |
#include <sstream> |
29 |
#include <dirent.h> |
#include <vector> |
30 |
#include <errno.h> |
#include <errno.h> |
31 |
|
#ifndef WIN32 |
32 |
#include <fnmatch.h> |
#include <fnmatch.h> |
33 |
#include <ftw.h> |
#endif |
34 |
|
|
35 |
#include "../common/Exception.h" |
#include "../common/Exception.h" |
36 |
|
|
37 |
namespace LinuxSampler { |
namespace LinuxSampler { |
38 |
|
|
39 |
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); |
|
|
} |
|
40 |
|
|
41 |
int dstDirId = db->GetDirectoryId(dir); |
void InstrumentsDb::CreateInstrumentsDb(String FilePath) { |
42 |
if(dstDirId == -1) throw Exception("Unkown DB directory: " + dir); |
File f = File(FilePath); |
43 |
IntListPtr ids = db->GetInstrumentIDs(DirId); |
if (f.Exist()) { |
44 |
for (int i = 0; i < ids->size(); i++) { |
throw Exception("File exists: " + FilePath); |
|
String name = db->GetInstrumentName(ids->at(i)); |
|
|
db->CopyInstrument(ids->at(i), name, dstDirId, dir); |
|
|
} |
|
|
} |
|
|
|
|
|
InstrumentsDb* InstrumentsDb::pInstrumentsDb = new InstrumentsDb; |
|
|
|
|
|
void InstrumentsDb::CreateInstrumentsDb(String File) { |
|
|
struct stat statBuf; |
|
|
int res = stat(File.c_str(), &statBuf); |
|
|
if (!res) { |
|
|
throw Exception("File exists: " + File); |
|
45 |
} |
} |
46 |
|
|
47 |
GetInstrumentsDb()->SetDbFile(File); |
GetInstrumentsDb()->SetDbFile(FilePath); |
48 |
|
|
49 |
String sql = |
String sql = |
50 |
" CREATE TABLE instr_dirs ( " |
" CREATE TABLE instr_dirs ( " |
60 |
|
|
61 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
62 |
|
|
63 |
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, '/');"; |
64 |
GetInstrumentsDb()->ExecSql(sql); |
GetInstrumentsDb()->ExecSql(sql); |
65 |
|
|
66 |
sql = |
sql = |
89 |
|
|
90 |
InstrumentsDb::InstrumentsDb() { |
InstrumentsDb::InstrumentsDb() { |
91 |
db = NULL; |
db = NULL; |
|
DbInstrumentsMutex = Mutex(); |
|
92 |
InTransaction = false; |
InTransaction = false; |
93 |
} |
} |
94 |
|
|
96 |
if (db != NULL) sqlite3_close(db); |
if (db != NULL) sqlite3_close(db); |
97 |
} |
} |
98 |
|
|
|
void InstrumentsDb::Destroy() { |
|
|
if (pInstrumentsDb != NULL) { |
|
|
delete pInstrumentsDb; |
|
|
pInstrumentsDb = NULL; |
|
|
} |
|
|
} |
|
|
|
|
99 |
void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) { |
void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) { |
100 |
llInstrumentsDbListeners.AddListener(l); |
llInstrumentsDbListeners.AddListener(l); |
101 |
} |
} |
105 |
} |
} |
106 |
|
|
107 |
InstrumentsDb* InstrumentsDb::GetInstrumentsDb() { |
InstrumentsDb* InstrumentsDb::GetInstrumentsDb() { |
108 |
return pInstrumentsDb; |
return &instance; |
109 |
} |
} |
110 |
|
|
111 |
void InstrumentsDb::SetDbFile(String File) { |
void InstrumentsDb::SetDbFile(String File) { |
121 |
sqlite3* InstrumentsDb::GetDb() { |
sqlite3* InstrumentsDb::GetDb() { |
122 |
if ( db != NULL) return db; |
if ( db != NULL) return db; |
123 |
|
|
124 |
if (DbFile.empty()) DbFile = "/var/lib/linuxsampler/instruments.db"; |
if (DbFile.empty()) { |
125 |
|
#ifndef WIN32 |
126 |
|
DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
127 |
|
#else |
128 |
|
char *userprofile = getenv("USERPROFILE"); |
129 |
|
if(userprofile) { |
130 |
|
DbFile = userprofile; |
131 |
|
DbFile += "\\.linuxsampler\\instruments.db"; |
132 |
|
} |
133 |
|
else { |
134 |
|
// in case USERPROFILE is not set (which should not occur) |
135 |
|
DbFile = "instruments.db"; |
136 |
|
} |
137 |
|
#endif |
138 |
|
} |
139 |
|
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
140 |
|
if (DbFile.find("~") == 0) |
141 |
|
DbFile.replace(0, 1, getenv("HOME")); |
142 |
|
#endif |
143 |
int rc = sqlite3_open(DbFile.c_str(), &db); |
int rc = sqlite3_open(DbFile.c_str(), &db); |
144 |
if (rc) { |
if (rc) { |
145 |
sqlite3_close(db); |
sqlite3_close(db); |
146 |
db = NULL; |
db = NULL; |
147 |
throw Exception("Cannot open instruments database: " + DbFile); |
throw Exception("Cannot open instruments database: " + DbFile); |
148 |
} |
} |
149 |
|
#ifndef WIN32 |
150 |
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); |
151 |
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."); } |
152 |
|
#endif |
153 |
|
|
154 |
|
// TODO: remove this in the next version |
155 |
|
try { |
156 |
|
int i = ExecSqlInt("SELECT parent_dir_id FROM instr_dirs WHERE dir_id=0"); |
157 |
|
// The parent ID of the root directory should be -2 now. |
158 |
|
if(i != -2) ExecSql("UPDATE instr_dirs SET parent_dir_id=-2 WHERE dir_id=0"); |
159 |
|
} catch(Exception e) { } |
160 |
|
//////////////////////////////////////// |
161 |
|
|
162 |
return db; |
return db; |
163 |
} |
} |
171 |
|
|
172 |
int count = ExecSqlInt(sql.str()); |
int count = ExecSqlInt(sql.str()); |
173 |
|
|
|
// 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--; |
|
174 |
return count; |
return count; |
175 |
} |
} |
176 |
|
|
192 |
throw e; |
throw e; |
193 |
} |
} |
194 |
EndTransaction(); |
EndTransaction(); |
195 |
if (i == -1) throw Exception("Unkown DB directory: " + Dir); |
if (i == -1) throw Exception("Unkown DB directory: " + toEscapedPath(Dir)); |
196 |
|
|
197 |
return i; |
return i; |
198 |
} |
} |
211 |
BeginTransaction(); |
BeginTransaction(); |
212 |
try { |
try { |
213 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
214 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
215 |
|
|
216 |
StringListPtr pDirs; |
StringListPtr pDirs; |
217 |
if (Recursive) { |
if (Recursive) { |
234 |
std::stringstream sql; |
std::stringstream sql; |
235 |
sql << "SELECT dir_name FROM instr_dirs "; |
sql << "SELECT dir_name FROM instr_dirs "; |
236 |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
sql << "WHERE parent_dir_id=" << DirId << " AND dir_id!=0"; |
237 |
return ExecSqlStringList(sql.str()); |
StringListPtr dirs = ExecSqlStringList(sql.str()); |
238 |
|
|
239 |
|
for (int i = 0; i < dirs->size(); i++) { |
240 |
|
for (int j = 0; j < dirs->at(i).length(); j++) { |
241 |
|
if (dirs->at(i).at(j) == '/') dirs->at(i).at(j) = '\0'; |
242 |
|
} |
243 |
|
} |
244 |
|
|
245 |
|
return dirs; |
246 |
} |
} |
247 |
|
|
248 |
int InstrumentsDb::GetDirectoryId(String Dir) { |
int InstrumentsDb::GetDirectoryId(String Dir) { |
271 |
|
|
272 |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
int InstrumentsDb::GetDirectoryId(int ParentDirId, String DirName) { |
273 |
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())); |
274 |
|
DirName = toDbName(DirName); |
275 |
std::stringstream sql; |
std::stringstream sql; |
276 |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
sql << "SELECT dir_id FROM instr_dirs WHERE parent_dir_id="; |
277 |
sql << ParentDirId << " AND dir_name=?"; |
sql << ParentDirId << " AND dir_name=?"; |
278 |
return ExecSqlInt(sql.str(), DirName); |
return ExecSqlInt(sql.str(), DirName); |
279 |
} |
} |
280 |
|
|
281 |
|
int InstrumentsDb::GetDirectoryId(int InstrId) { |
282 |
|
dmsg(2,("InstrumentsDb: GetDirectoryId(InstrId=%d)\n", InstrId)); |
283 |
|
std::stringstream sql; |
284 |
|
sql << "SELECT dir_id FROM instruments WHERE instr_id=" << InstrId; |
285 |
|
return ExecSqlInt(sql.str()); |
286 |
|
} |
287 |
|
|
288 |
String InstrumentsDb::GetDirectoryName(int DirId) { |
String InstrumentsDb::GetDirectoryName(int DirId) { |
289 |
String sql = "SELECT dir_name FROM instr_dirs WHERE dir_id=" + ToString(DirId); |
String sql = "SELECT dir_name FROM instr_dirs WHERE dir_id=" + ToString(DirId); |
290 |
String name = ExecSqlString(sql); |
String name = ExecSqlString(sql); |
309 |
path = "/" + path; |
path = "/" + path; |
310 |
break; |
break; |
311 |
} |
} |
312 |
path = GetDirectoryName(DirId) + path; |
path = GetDirectoryName(DirId) + "/" + path; |
313 |
DirId = GetParentDirectoryId(DirId); |
DirId = GetParentDirectoryId(DirId); |
314 |
} |
} |
315 |
|
|
317 |
|
|
318 |
return path; |
return path; |
319 |
} |
} |
320 |
|
|
321 |
|
StringListPtr InstrumentsDb::GetInstrumentsByFile(String File) { |
322 |
|
dmsg(2,("InstrumentsDb: GetInstrumentsByFile(File=%s)\n", File.c_str())); |
323 |
|
|
324 |
|
StringListPtr instrs(new std::vector<String>); |
325 |
|
|
326 |
|
BeginTransaction(); |
327 |
|
try { |
328 |
|
File = toEscapedFsPath(File); |
329 |
|
IntListPtr ids = ExecSqlIntList("SELECT instr_id FROM instruments WHERE instr_file=?", File); |
330 |
|
|
331 |
|
for (int i = 0; i < ids->size(); i++) { |
332 |
|
String name = GetInstrumentName(ids->at(i)); |
333 |
|
String dir = GetDirectoryPath(GetDirectoryId(ids->at(i))); |
334 |
|
instrs->push_back(dir + name); |
335 |
|
} |
336 |
|
} catch (Exception e) { |
337 |
|
EndTransaction(); |
338 |
|
throw e; |
339 |
|
} |
340 |
|
EndTransaction(); |
341 |
|
|
342 |
|
return instrs; |
343 |
|
} |
344 |
|
|
345 |
void InstrumentsDb::AddDirectory(String Dir) { |
void InstrumentsDb::AddDirectory(String Dir) { |
346 |
dmsg(2,("InstrumentsDb: AddDirectory(Dir=%s)\n", Dir.c_str())); |
dmsg(2,("InstrumentsDb: AddDirectory(Dir=%s)\n", Dir.c_str())); |
355 |
|
|
356 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
357 |
if(ParentDir.empty() || dirName.empty()) { |
if(ParentDir.empty() || dirName.empty()) { |
358 |
throw Exception("Failed to add DB directory: " + Dir); |
throw Exception("Failed to add DB directory: " + toEscapedPath(Dir)); |
359 |
} |
} |
360 |
|
|
361 |
int id = GetDirectoryId(ParentDir); |
int id = GetDirectoryId(ParentDir); |
362 |
if (id == -1) throw Exception("DB directory doesn't exist: " + ParentDir); |
if (id == -1) throw Exception("DB directory doesn't exist: " + toEscapedPath(ParentDir)); |
363 |
int id2 = GetDirectoryId(id, dirName); |
int id2 = GetDirectoryId(id, dirName); |
364 |
if (id2 != -1) throw Exception("DB directory already exist: " + Dir); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(Dir)); |
365 |
id2 = GetInstrumentId(id, dirName); |
id2 = GetInstrumentId(id, dirName); |
366 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + Dir); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(Dir)); |
367 |
|
|
368 |
std::stringstream sql; |
std::stringstream sql; |
369 |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
sql << "INSERT INTO instr_dirs (parent_dir_id, dir_name) VALUES ("; |
370 |
sql << id << ", ?)"; |
sql << id << ", ?)"; |
371 |
|
|
372 |
ExecSql(sql.str(), dirName); |
ExecSql(sql.str(), toDbName(dirName)); |
373 |
} catch (Exception e) { |
} catch (Exception e) { |
374 |
EndTransaction(); |
EndTransaction(); |
375 |
throw e; |
throw e; |
388 |
BeginTransaction(); |
BeginTransaction(); |
389 |
try { |
try { |
390 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
391 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
392 |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
if (dirId == 0) throw Exception("Cannot delete the root directory: " + Dir); |
393 |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
if(ParentDir.empty()) throw Exception("Unknown parent directory"); |
394 |
if (Force) RemoveDirectoryContent(dirId); |
if (Force) RemoveDirectoryContent(dirId); |
475 |
|
|
476 |
try { |
try { |
477 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
478 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
479 |
|
|
480 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
481 |
std::stringstream sql; |
std::stringstream sql; |
498 |
if (res != SQLITE_DONE) { |
if (res != SQLITE_DONE) { |
499 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
500 |
} else { |
} else { |
501 |
throw Exception("Unknown DB directory: " + Dir); |
throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
502 |
} |
} |
503 |
} |
} |
504 |
|
|
515 |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
void InstrumentsDb::RenameDirectory(String Dir, String Name) { |
516 |
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())); |
517 |
CheckFileName(Name); |
CheckFileName(Name); |
518 |
|
String dbName = toDbName(Name); |
519 |
|
|
520 |
BeginTransaction(); |
BeginTransaction(); |
521 |
try { |
try { |
522 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
523 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedText(Dir)); |
524 |
|
|
525 |
std::stringstream sql; |
std::stringstream sql; |
526 |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
sql << "SELECT parent_dir_id FROM instr_dirs WHERE dir_id=" << dirId; |
527 |
|
|
528 |
int parent = ExecSqlInt(sql.str()); |
int parent = ExecSqlInt(sql.str()); |
529 |
if (parent == -1) throw Exception("Unknown parent directory: " + Dir); |
if (parent == -1) throw Exception("Unknown parent directory: " + toEscapedPath(Dir)); |
530 |
if (GetDirectoryId(parent, Name) != -1) { |
|
531 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
if (GetDirectoryId(parent, dbName) != -1) { |
532 |
|
String s = toEscapedPath(Name); |
533 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
534 |
} |
} |
535 |
|
|
536 |
if (GetInstrumentId(parent, Name) != -1) { |
if (GetInstrumentId(parent, dbName) != -1) { |
537 |
throw Exception("Cannot rename. Instrument with that name exist: " + Dir); |
throw Exception("Cannot rename. Instrument with that name exist: " + toEscapedPath(Dir)); |
538 |
} |
} |
539 |
|
|
540 |
sql.str(""); |
sql.str(""); |
541 |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
sql << "UPDATE instr_dirs SET dir_name=? WHERE dir_id=" << dirId; |
542 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), dbName); |
543 |
} catch (Exception e) { |
} catch (Exception e) { |
544 |
EndTransaction(); |
EndTransaction(); |
545 |
throw e; |
throw e; |
546 |
} |
} |
547 |
|
|
548 |
EndTransaction(); |
EndTransaction(); |
549 |
FireDirectoryNameChanged(Dir, Name); |
FireDirectoryNameChanged(Dir, toAbstractName(Name)); |
550 |
} |
} |
551 |
|
|
552 |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
void InstrumentsDb::MoveDirectory(String Dir, String Dst) { |
559 |
BeginTransaction(); |
BeginTransaction(); |
560 |
try { |
try { |
561 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
562 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
563 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
564 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
565 |
if (dirId == dstId) { |
if (dirId == dstId) { |
566 |
throw Exception("Cannot move directory to itself"); |
throw Exception("Cannot move directory to itself"); |
567 |
} |
} |
577 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
578 |
|
|
579 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
580 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
581 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
582 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
583 |
|
|
584 |
std::stringstream sql; |
std::stringstream sql; |
585 |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
sql << "UPDATE instr_dirs SET parent_dir_id=" << dstId; |
605 |
BeginTransaction(); |
BeginTransaction(); |
606 |
try { |
try { |
607 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
608 |
if (dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if (dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
609 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
610 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
611 |
if (dirId == dstId) { |
if (dirId == dstId) { |
612 |
throw Exception("Cannot copy directory to itself"); |
throw Exception("Cannot copy directory to itself"); |
613 |
} |
} |
623 |
String dirName = GetFileName(Dir); |
String dirName = GetFileName(Dir); |
624 |
|
|
625 |
int id2 = GetDirectoryId(dstId, dirName); |
int id2 = GetDirectoryId(dstId, dirName); |
626 |
if (id2 != -1) throw Exception("DB directory already exist: " + dirName); |
if (id2 != -1) throw Exception("DB directory already exist: " + toEscapedPath(dirName)); |
627 |
id2 = GetInstrumentId(dstId, dirName); |
id2 = GetInstrumentId(dstId, dirName); |
628 |
if (id2 != -1) throw Exception("Instrument with that name exist: " + dirName); |
if (id2 != -1) throw Exception("Instrument with that name exist: " + toEscapedPath(dirName)); |
629 |
|
|
630 |
DirectoryCopier directoryCopier(ParentDir, Dst); |
DirectoryCopier directoryCopier(ParentDir, Dst); |
631 |
DirectoryTreeWalk(Dir, &directoryCopier); |
DirectoryTreeWalk(Dir, &directoryCopier); |
643 |
BeginTransaction(); |
BeginTransaction(); |
644 |
try { |
try { |
645 |
int id = GetDirectoryId(Dir); |
int id = GetDirectoryId(Dir); |
646 |
if(id == -1) throw Exception("Unknown DB directory: " + Dir); |
if(id == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
647 |
|
|
648 |
std::stringstream sql; |
std::stringstream sql; |
649 |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instr_dirs SET description=?,modified=CURRENT_TIMESTAMP "; |
659 |
FireDirectoryInfoChanged(Dir); |
FireDirectoryInfoChanged(Dir); |
660 |
} |
} |
661 |
|
|
662 |
void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index) { |
int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground, bool insDir) { |
663 |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); |
664 |
|
if(!bBackground) { |
665 |
|
switch (Mode) { |
666 |
|
case NON_RECURSIVE: |
667 |
|
AddInstrumentsNonrecursive(DbDir, FsDir, insDir); |
668 |
|
break; |
669 |
|
case RECURSIVE: |
670 |
|
AddInstrumentsRecursive(DbDir, FsDir, false, insDir); |
671 |
|
break; |
672 |
|
case FLAT: |
673 |
|
AddInstrumentsRecursive(DbDir, FsDir, true, insDir); |
674 |
|
break; |
675 |
|
default: |
676 |
|
throw Exception("Unknown scan mode"); |
677 |
|
} |
678 |
|
|
679 |
|
return -1; |
680 |
|
} |
681 |
|
|
682 |
|
ScanJob job; |
683 |
|
int jobId = Jobs.AddJob(job); |
684 |
|
InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir, insDir)); |
685 |
|
|
686 |
|
return jobId; |
687 |
|
} |
688 |
|
|
689 |
|
int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { |
690 |
|
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); |
691 |
|
if(!bBackground) { |
692 |
|
AddInstruments(DbDir, false, FilePath, Index); |
693 |
|
return -1; |
694 |
|
} |
695 |
|
|
696 |
|
ScanJob job; |
697 |
|
int jobId = Jobs.AddJob(job); |
698 |
|
InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index, false)); |
699 |
|
|
700 |
|
return jobId; |
701 |
|
} |
702 |
|
|
703 |
|
void InstrumentsDb::AddInstruments(String DbDir, bool insDir, String FilePath, int Index, ScanProgress* pProgress) { |
704 |
|
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index)); |
705 |
if (DbDir.empty() || FilePath.empty()) return; |
if (DbDir.empty() || FilePath.empty()) return; |
706 |
|
|
707 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
708 |
try { |
try { |
709 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
710 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedText(DbDir)); |
711 |
|
|
712 |
struct stat statBuf; |
File f = File(FilePath); |
713 |
int res = stat(FilePath.c_str(), &statBuf); |
if (!f.Exist()) { |
|
if (res) { |
|
714 |
std::stringstream ss; |
std::stringstream ss; |
715 |
ss << "Fail to stat `" << FilePath << "`: " << strerror(errno); |
ss << "Fail to stat `" << FilePath << "`: " << f.GetErrorMsg(); |
716 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
717 |
} |
} |
718 |
|
|
719 |
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) { |
|
720 |
std::stringstream ss; |
std::stringstream ss; |
721 |
ss << "`" << FilePath << "` is directory, not an instrument file"; |
ss << "`" << FilePath << "` is not an instrument file"; |
722 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
723 |
} |
} |
724 |
|
|
725 |
AddInstrumentsRecursive(DbDir, FilePath, false); |
String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; |
726 |
|
AddInstrumentsFromFile(dir, FilePath, Index, pProgress); |
727 |
} catch (Exception e) { |
} catch (Exception e) { |
728 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
729 |
throw e; |
throw e; |
732 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
733 |
} |
} |
734 |
|
|
735 |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir) { |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) { |
736 |
dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s)\n", DbDir.c_str(), FsDir.c_str())); |
dmsg(2,("InstrumentsDb: AddInstrumentsNonrecursive(DbDir=%s,FsDir=%s,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), insDir)); |
737 |
if (DbDir.empty() || FsDir.empty()) return; |
if (DbDir.empty() || FsDir.empty()) return; |
738 |
|
|
739 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
740 |
try { |
try { |
741 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
742 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
743 |
|
|
744 |
struct stat statBuf; |
File f = File(FsDir); |
745 |
int res = stat(FsDir.c_str(), &statBuf); |
if (!f.Exist()) { |
|
if (res) { |
|
746 |
std::stringstream ss; |
std::stringstream ss; |
747 |
ss << "Fail to stat `" << FsDir << "`: " << strerror(errno); |
ss << "Fail to stat `" << FsDir << "`: " << f.GetErrorMsg(); |
748 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
749 |
} |
} |
750 |
|
|
751 |
if (!S_ISDIR(statBuf.st_mode)) { |
if (!f.IsDirectory()) { |
752 |
throw Exception("Directory expected"); |
throw Exception("Directory expected: " + FsDir); |
753 |
} |
} |
754 |
|
|
755 |
if (FsDir.at(FsDir.length() - 1) != '/') FsDir.append("/"); |
if (FsDir.at(FsDir.length() - 1) != File::DirSeparator) { |
756 |
|
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; |
|
757 |
} |
} |
758 |
|
|
759 |
struct dirent* pEnt = readdir(pDir); |
try { |
760 |
while (pEnt != NULL) { |
FileListPtr fileList = File::GetFiles(FsDir); |
761 |
if (pEnt->d_type != DT_REG) { |
for (int i = 0; i < fileList->size(); i++) { |
762 |
pEnt = readdir(pDir); |
String dir = insDir ? PrepareSubdirectory(DbDir, fileList->at(i)) : DbDir; |
763 |
continue; |
AddInstrumentsFromFile(dir, FsDir + fileList->at(i), -1, pProgress); |
764 |
} |
} |
765 |
|
} catch(Exception e) { |
766 |
AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name)); |
e.PrintMessage(); |
767 |
pEnt = readdir(pDir); |
DbInstrumentsMutex.Unlock(); |
768 |
} |
return; |
|
|
|
|
if (closedir(pDir)) { |
|
|
std::stringstream ss; |
|
|
ss << "Failed to close directory `" << FsDir << "`: "; |
|
|
ss << strerror(errno); |
|
|
std::cerr << ss.str(); |
|
769 |
} |
} |
770 |
} catch (Exception e) { |
} catch (Exception e) { |
771 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
775 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
776 |
} |
} |
777 |
|
|
778 |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat) { |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) { |
779 |
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,insDir=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat, insDir)); |
780 |
DirectoryScanner::Scan(DbDir, FsDir, Flat); |
if (pProgress != NULL) { |
781 |
|
InstrumentFileCounter c; |
782 |
|
pProgress->SetTotalFileCount(c.Count(FsDir)); |
783 |
|
} |
784 |
|
|
785 |
|
DirectoryScanner d; |
786 |
|
d.Scan(DbDir, FsDir, Flat, insDir, pProgress); |
787 |
} |
} |
788 |
|
|
789 |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
815 |
} |
} |
816 |
EndTransaction(); |
EndTransaction(); |
817 |
|
|
818 |
if (i == -1) throw Exception("Unknown Db directory: " + Dir); |
if (i == -1) throw Exception("Unknown Db directory: " + toEscapedPath(Dir)); |
819 |
return i; |
return i; |
820 |
} |
} |
821 |
|
|
831 |
BeginTransaction(); |
BeginTransaction(); |
832 |
try { |
try { |
833 |
int dirId = GetDirectoryId(Dir); |
int dirId = GetDirectoryId(Dir); |
834 |
if(dirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(dirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
835 |
|
|
836 |
StringListPtr pInstrs; |
StringListPtr pInstrs; |
837 |
|
|
845 |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
sql << "SELECT instr_name FROM instruments WHERE dir_id=" << dirId; |
846 |
|
|
847 |
pInstrs = ExecSqlStringList(sql.str()); |
pInstrs = ExecSqlStringList(sql.str()); |
848 |
|
// Converting to abstract names |
849 |
|
for (int i = 0; i < pInstrs->size(); i++) { |
850 |
|
for (int j = 0; j < pInstrs->at(i).length(); j++) { |
851 |
|
if (pInstrs->at(i).at(j) == '/') pInstrs->at(i).at(j) = '\0'; |
852 |
|
} |
853 |
|
} |
854 |
} |
} |
855 |
EndTransaction(); |
EndTransaction(); |
856 |
return pInstrs; |
return pInstrs; |
875 |
std::stringstream sql; |
std::stringstream sql; |
876 |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
sql << "SELECT instr_id FROM instruments WHERE dir_id="; |
877 |
sql << DirId << " AND instr_name=?"; |
sql << DirId << " AND instr_name=?"; |
878 |
return ExecSqlInt(sql.str(), InstrName); |
return ExecSqlInt(sql.str(), toDbName(InstrName)); |
879 |
} |
} |
880 |
|
|
881 |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
String InstrumentsDb::GetInstrumentName(int InstrId) { |
882 |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
dmsg(2,("InstrumentsDb: GetInstrumentName(InstrId=%d)\n", InstrId)); |
883 |
std::stringstream sql; |
std::stringstream sql; |
884 |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
sql << "SELECT instr_name FROM instruments WHERE instr_id=" << InstrId; |
885 |
return ExecSqlString(sql.str()); |
return toAbstractName(ExecSqlString(sql.str())); |
886 |
} |
} |
887 |
|
|
888 |
void InstrumentsDb::RemoveInstrument(String Instr) { |
void InstrumentsDb::RemoveInstrument(String Instr) { |
894 |
try { |
try { |
895 |
int instrId = GetInstrumentId(Instr); |
int instrId = GetInstrumentId(Instr); |
896 |
if(instrId == -1) { |
if(instrId == -1) { |
897 |
throw Exception("The specified instrument does not exist: " + Instr); |
throw Exception("The specified instrument does not exist: " + toEscapedPath(Instr)); |
898 |
} |
} |
899 |
RemoveInstrument(instrId); |
RemoveInstrument(instrId); |
900 |
} catch (Exception e) { |
} catch (Exception e) { |
929 |
BeginTransaction(); |
BeginTransaction(); |
930 |
try { |
try { |
931 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
932 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
933 |
i = GetInstrumentInfo(id); |
i = GetInstrumentInfo(id); |
934 |
} catch (Exception e) { |
} catch (Exception e) { |
935 |
EndTransaction(); |
EndTransaction(); |
988 |
BeginTransaction(); |
BeginTransaction(); |
989 |
try { |
try { |
990 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
991 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
992 |
|
|
993 |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
int instrId = GetInstrumentId(dirId, GetFileName(Instr)); |
994 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
995 |
|
|
996 |
if (GetInstrumentId(dirId, Name) != -1) { |
if (GetInstrumentId(dirId, Name) != -1) { |
997 |
throw Exception("Cannot rename. Instrument with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
998 |
|
throw Exception("Cannot rename. Instrument with that name already exists: " + s); |
999 |
} |
} |
1000 |
|
|
1001 |
if (GetDirectoryId(dirId, Name) != -1) { |
if (GetDirectoryId(dirId, Name) != -1) { |
1002 |
throw Exception("Cannot rename. Directory with that name already exists: " + Name); |
String s = toEscapedPath(Name); |
1003 |
|
throw Exception("Cannot rename. Directory with that name already exists: " + s); |
1004 |
} |
} |
1005 |
|
|
1006 |
std::stringstream sql; |
std::stringstream sql; |
1007 |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
sql << "UPDATE instruments SET instr_name=? WHERE instr_id=" << instrId; |
1008 |
ExecSql(sql.str(), Name); |
ExecSql(sql.str(), toDbName(Name)); |
1009 |
} catch (Exception e) { |
} catch (Exception e) { |
1010 |
EndTransaction(); |
EndTransaction(); |
1011 |
throw e; |
throw e; |
1012 |
} |
} |
1013 |
EndTransaction(); |
EndTransaction(); |
1014 |
FireInstrumentNameChanged(Instr, Name); |
FireInstrumentNameChanged(Instr, toAbstractName(Name)); |
1015 |
} |
} |
1016 |
|
|
1017 |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
void InstrumentsDb::MoveInstrument(String Instr, String Dst) { |
1021 |
|
|
1022 |
BeginTransaction(); |
BeginTransaction(); |
1023 |
try { |
try { |
1024 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(ParentDir); |
1025 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1026 |
|
|
1027 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
1028 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
1029 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1030 |
|
|
1031 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1032 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1033 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1034 |
EndTransaction(); |
EndTransaction(); |
1035 |
return; |
return; |
1036 |
} |
} |
1037 |
|
|
1038 |
if (GetInstrumentId(dstId, instrName) != -1) { |
if (GetInstrumentId(dstId, instrName) != -1) { |
1039 |
throw Exception("Cannot move. Instrument with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
1040 |
|
throw Exception("Cannot move. Instrument with that name already exists: " + s); |
1041 |
} |
} |
1042 |
|
|
1043 |
if (GetDirectoryId(dstId, instrName) != -1) { |
if (GetDirectoryId(dstId, instrName) != -1) { |
1044 |
throw Exception("Cannot move. Directory with that name already exists: " + instrName); |
String s = toEscapedPath(instrName); |
1045 |
|
throw Exception("Cannot move. Directory with that name already exists: " + s); |
1046 |
} |
} |
1047 |
|
|
1048 |
std::stringstream sql; |
std::stringstream sql; |
1066 |
BeginTransaction(); |
BeginTransaction(); |
1067 |
try { |
try { |
1068 |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
int dirId = GetDirectoryId(GetDirectoryPath(Instr)); |
1069 |
if (dirId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (dirId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1070 |
|
|
1071 |
String instrName = GetFileName(Instr); |
String instrName = GetFileName(Instr); |
1072 |
int instrId = GetInstrumentId(dirId, instrName); |
int instrId = GetInstrumentId(dirId, instrName); |
1073 |
if (instrId == -1) throw Exception("Unknown DB instrument: " + Instr); |
if (instrId == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1074 |
|
|
1075 |
int dstId = GetDirectoryId(Dst); |
int dstId = GetDirectoryId(Dst); |
1076 |
if (dstId == -1) throw Exception("Unknown DB directory: " + Dst); |
if (dstId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dst)); |
1077 |
if (dirId == dstId) { |
if (dirId == dstId) { |
1078 |
EndTransaction(); |
EndTransaction(); |
1079 |
return; |
return; |
1080 |
} |
} |
1081 |
|
|
|
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); |
|
|
} |
|
|
|
|
1082 |
CopyInstrument(instrId, instrName, dstId, Dst); |
CopyInstrument(instrId, instrName, dstId, Dst); |
1083 |
} catch (Exception e) { |
} catch (Exception e) { |
1084 |
EndTransaction(); |
EndTransaction(); |
1089 |
} |
} |
1090 |
|
|
1091 |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
void InstrumentsDb::CopyInstrument(int InstrId, String InstrName, int DstDirId, String DstDir) { |
1092 |
|
if (GetInstrumentId(DstDirId, InstrName) != -1) { |
1093 |
|
String s = toEscapedPath(InstrName); |
1094 |
|
throw Exception("Cannot copy. Instrument with that name already exists: " + s); |
1095 |
|
} |
1096 |
|
|
1097 |
|
if (GetDirectoryId(DstDirId, InstrName) != -1) { |
1098 |
|
String s = toEscapedPath(InstrName); |
1099 |
|
throw Exception("Cannot copy. Directory with that name already exists: " + s); |
1100 |
|
} |
1101 |
|
|
1102 |
DbInstrument i = GetInstrumentInfo(InstrId); |
DbInstrument i = GetInstrumentInfo(InstrId); |
1103 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1104 |
std::stringstream sql; |
std::stringstream sql; |
1112 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1113 |
} |
} |
1114 |
|
|
1115 |
BindTextParam(pStmt, 1, InstrName); |
String s = toDbName(InstrName); |
1116 |
|
BindTextParam(pStmt, 1, s); |
1117 |
BindTextParam(pStmt, 2, i.InstrFile); |
BindTextParam(pStmt, 2, i.InstrFile); |
1118 |
BindTextParam(pStmt, 3, i.FormatFamily); |
BindTextParam(pStmt, 3, i.FormatFamily); |
1119 |
BindTextParam(pStmt, 4, i.FormatVersion); |
BindTextParam(pStmt, 4, i.FormatVersion); |
1138 |
BeginTransaction(); |
BeginTransaction(); |
1139 |
try { |
try { |
1140 |
int id = GetInstrumentId(Instr); |
int id = GetInstrumentId(Instr); |
1141 |
if(id == -1) throw Exception("Unknown DB instrument: " + Instr); |
if(id == -1) throw Exception("Unknown DB instrument: " + toEscapedPath(Instr)); |
1142 |
|
|
1143 |
std::stringstream sql; |
std::stringstream sql; |
1144 |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
sql << "UPDATE instruments SET description=?,modified=CURRENT_TIMESTAMP "; |
1153 |
FireInstrumentInfoChanged(Instr); |
FireInstrumentInfoChanged(Instr); |
1154 |
} |
} |
1155 |
|
|
1156 |
void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index) { |
void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index, ScanProgress* pProgress) { |
1157 |
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)); |
1158 |
|
|
1159 |
if(File.length() < 4) return; |
if(File.length() < 4) return; |
1160 |
|
|
1161 |
try { |
try { |
1162 |
if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) { |
if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) { |
1163 |
AddGigInstruments(DbDir, File, Index); |
if (pProgress != NULL) { |
1164 |
|
pProgress->SetStatus(0); |
1165 |
|
pProgress->CurrentFile = File; |
1166 |
|
} |
1167 |
|
|
1168 |
|
AddGigInstruments(DbDir, File, Index, pProgress); |
1169 |
|
|
1170 |
|
if (pProgress != NULL) { |
1171 |
|
pProgress->SetScannedFileCount(pProgress->GetScannedFileCount() + 1); |
1172 |
|
} |
1173 |
} |
} |
1174 |
} catch(Exception e) { |
} catch(Exception e) { |
1175 |
std::cerr << e.Message() << std::endl; |
e.PrintMessage(); |
1176 |
} |
} |
1177 |
} |
} |
1178 |
|
|
1179 |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index) { |
void InstrumentsDb::AddGigInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { |
1180 |
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)); |
1181 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
1182 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + toEscapedPath(DbDir)); |
1183 |
|
|
1184 |
struct stat statBuf; |
File f = File(FilePath); |
1185 |
int res = stat(File.c_str(), &statBuf); |
if (!f.Exist()) { |
|
if (res) { |
|
1186 |
std::stringstream ss; |
std::stringstream ss; |
1187 |
ss << "Fail to stat `" << File << "`: " << strerror(errno); |
ss << "Fail to stat `" << FilePath << "`: " << f.GetErrorMsg(); |
1188 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1189 |
} |
} |
1190 |
|
|
1191 |
if (!S_ISREG(statBuf.st_mode)) { |
if (!f.IsFile()) { |
1192 |
std::stringstream ss; |
std::stringstream ss; |
1193 |
ss << "`" << File << "` is not a regular file"; |
ss << "`" << FilePath << "` is not a regular file"; |
1194 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1195 |
} |
} |
1196 |
|
|
1197 |
|
bool unlocked = false; |
1198 |
RIFF::File* riff = NULL; |
RIFF::File* riff = NULL; |
1199 |
gig::File* gig = NULL; |
gig::File* gig = NULL; |
1200 |
try { |
try { |
1201 |
riff = new RIFF::File(File); |
riff = new RIFF::File(FilePath); |
1202 |
gig::File* gig = new gig::File(riff); |
gig::File* gig = new gig::File(riff); |
1203 |
|
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
1204 |
|
|
1205 |
std::stringstream sql; |
std::stringstream sql; |
1206 |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
1207 |
sql << "instr_nr,format_family,format_version,instr_size,"; |
sql << "instr_nr,format_family,format_version,instr_size,"; |
1208 |
sql << "description,is_drum,product,artists,keywords) VALUES ("; |
sql << "description,is_drum,product,artists,keywords) VALUES ("; |
1209 |
sql << dirId << ",?,?,?,'GIG',?," << statBuf.st_size << ",?,?,?,?,?)"; |
sql << dirId << ",?,?,?,'GIG',?," << f.GetSize() << ",?,?,?,?,?)"; |
1210 |
|
|
1211 |
sqlite3_stmt* pStmt = NULL; |
sqlite3_stmt* pStmt = NULL; |
1212 |
|
|
1215 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1216 |
} |
} |
1217 |
|
|
1218 |
BindTextParam(pStmt, 2, File); |
String s = FilePath; |
1219 |
|
|
1220 |
|
#if WIN32 |
1221 |
|
for (int i = 0; i < s.length(); i++) { |
1222 |
|
if (s[i] == '\\') s[i] = '/'; |
1223 |
|
} |
1224 |
|
#endif |
1225 |
|
|
1226 |
|
s = toEscapedFsPath(s); |
1227 |
|
BindTextParam(pStmt, 2, s); |
1228 |
String ver = ""; |
String ver = ""; |
1229 |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
if (gig->pVersion != NULL) ver = ToString(gig->pVersion->major); |
1230 |
BindTextParam(pStmt, 4, ver); |
BindTextParam(pStmt, 4, ver); |
1231 |
|
|
1232 |
if (Index == -1) { |
if (Index == -1) { |
1233 |
int instrIndex = 0; |
int instrIndex = 0; |
1234 |
|
// Assume that it's locked and should be unlocked at this point |
1235 |
|
// to be able to use the database from another threads |
1236 |
|
if (!InTransaction) { |
1237 |
|
DbInstrumentsMutex.Unlock(); |
1238 |
|
unlocked = true; |
1239 |
|
} else { |
1240 |
|
std::cerr << "Shouldn't be in transaction when adding instruments." << std::endl; |
1241 |
|
} |
1242 |
|
|
1243 |
|
if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed |
1244 |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
1245 |
|
|
1246 |
|
if (!InTransaction) DbInstrumentsMutex.Lock(); |
1247 |
while (pInstrument) { |
while (pInstrument) { |
1248 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1249 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1250 |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
1251 |
AddGigInstrument(pStmt, DbDir, dirId, File, pInstrument, instrIndex); |
AddGigInstrument(pStmt, DbDir, dirId, FilePath, pInstrument, instrIndex); |
1252 |
|
|
1253 |
instrIndex++; |
instrIndex++; |
1254 |
pInstrument = gig->GetNextInstrument(); |
pInstrument = gig->GetNextInstrument(); |
1255 |
} |
} |
1256 |
} else { |
} else { |
1257 |
gig::Instrument* pInstrument = gig->GetInstrument(Index); |
gig::Instrument* pInstrument; |
1258 |
|
if (pProgress == NULL) pInstrument = gig->GetInstrument(Index); |
1259 |
|
else pInstrument = gig->GetInstrument(Index, &(pProgress->GigFileProgress)); |
1260 |
if (pInstrument != NULL) { |
if (pInstrument != NULL) { |
1261 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1262 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1263 |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
BindTextParam(pStmt, 9, gig->pInfo->Keywords); |
1264 |
AddGigInstrument(pStmt, DbDir, dirId, File, pInstrument, Index); |
AddGigInstrument(pStmt, DbDir, dirId, FilePath, pInstrument, Index); |
1265 |
} |
} |
1266 |
} |
} |
1267 |
|
|
1271 |
} catch (RIFF::Exception e) { |
} catch (RIFF::Exception e) { |
1272 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1273 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1274 |
|
if (unlocked) DbInstrumentsMutex.Lock(); |
1275 |
std::stringstream ss; |
std::stringstream ss; |
1276 |
ss << "Failed to scan `" << File << "`: " << e.Message; |
ss << "Failed to scan `" << FilePath << "`: " << e.Message; |
1277 |
|
|
1278 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
1279 |
} catch (Exception e) { |
} catch (Exception e) { |
1280 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1281 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1282 |
|
if (unlocked) DbInstrumentsMutex.Lock(); |
1283 |
throw e; |
throw e; |
1284 |
} catch (...) { |
} catch (...) { |
1285 |
if (gig != NULL) delete gig; |
if (gig != NULL) delete gig; |
1286 |
if (riff != NULL) delete riff; |
if (riff != NULL) delete riff; |
1287 |
throw Exception("Failed to scan `" + File + "`"); |
if (unlocked) DbInstrumentsMutex.Lock(); |
1288 |
|
throw Exception("Failed to scan `" + FilePath + "`"); |
1289 |
} |
} |
1290 |
} |
} |
1291 |
|
|
1292 |
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) { |
1293 |
|
dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); |
1294 |
String name = pInstrument->pInfo->Name; |
String name = pInstrument->pInfo->Name; |
1295 |
if (name == "") return; |
if (name == "") return; |
1296 |
name = GetUniqueInstrumentName(DirId, name); |
name = GetUniqueName(DirId, name); |
1297 |
|
|
1298 |
std::stringstream sql2; |
std::stringstream sql2; |
1299 |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
1300 |
sql2 << "instr_nr=" << Index; |
sql2 << "instr_nr=" << Index; |
1301 |
if (ExecSqlInt(sql2.str(), File) > 0) return; |
String s = toEscapedFsPath(File); |
1302 |
|
if (ExecSqlInt(sql2.str(), s) > 0) return; |
1303 |
|
|
1304 |
BindTextParam(pStmt, 1, name); |
BindTextParam(pStmt, 1, name); |
1305 |
BindIntParam(pStmt, 3, Index); |
BindIntParam(pStmt, 3, Index); |
1328 |
FireInstrumentCountChanged(DbDir); |
FireInstrumentCountChanged(DbDir); |
1329 |
} |
} |
1330 |
|
|
1331 |
void InstrumentsDb::DirectoryTreeWalk(String Path, DirectoryHandler* pHandler) { |
void InstrumentsDb::DirectoryTreeWalk(String AbstractPath, DirectoryHandler* pHandler) { |
1332 |
int DirId = GetDirectoryId(Path); |
int DirId = GetDirectoryId(AbstractPath); |
1333 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Path); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(AbstractPath)); |
1334 |
DirectoryTreeWalk(pHandler, Path, DirId, 0); |
DirectoryTreeWalk(pHandler, AbstractPath, DirId, 0); |
1335 |
} |
} |
1336 |
|
|
1337 |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String Path, int DirId, int Level) { |
void InstrumentsDb::DirectoryTreeWalk(DirectoryHandler* pHandler, String AbstractPath, int DirId, int Level) { |
1338 |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
if(Level == 1000) throw Exception("Possible infinite loop detected"); |
1339 |
pHandler->ProcessDirectory(Path, DirId); |
pHandler->ProcessDirectory(AbstractPath, DirId); |
1340 |
|
|
1341 |
String s; |
String s; |
1342 |
StringListPtr pDirs = GetDirectories(DirId); |
StringListPtr pDirs = GetDirectories(DirId); |
1343 |
for(int i = 0; i < pDirs->size(); i++) { |
for(int i = 0; i < pDirs->size(); i++) { |
1344 |
if (Path.length() == 1 && Path.at(0) == '/') s = "/" + pDirs->at(i); |
if (AbstractPath.length() == 1 && AbstractPath.at(0) == '/') { |
1345 |
else s = Path + "/" + pDirs->at(i); |
s = "/" + pDirs->at(i); |
1346 |
|
} else { |
1347 |
|
s = AbstractPath + "/" + pDirs->at(i); |
1348 |
|
} |
1349 |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
DirectoryTreeWalk(pHandler, s, GetDirectoryId(DirId, pDirs->at(i)), Level + 1); |
1350 |
} |
} |
1351 |
} |
} |
1357 |
BeginTransaction(); |
BeginTransaction(); |
1358 |
try { |
try { |
1359 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1360 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1361 |
|
|
1362 |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &directoryFinder); |
1363 |
else directoryFinder.ProcessDirectory(Dir, DirId); |
else directoryFinder.ProcessDirectory(Dir, DirId); |
1377 |
BeginTransaction(); |
BeginTransaction(); |
1378 |
try { |
try { |
1379 |
int DirId = GetDirectoryId(Dir); |
int DirId = GetDirectoryId(Dir); |
1380 |
if(DirId == -1) throw Exception("Unknown DB directory: " + Dir); |
if(DirId == -1) throw Exception("Unknown DB directory: " + toEscapedPath(Dir)); |
1381 |
|
|
1382 |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
if (Recursive) DirectoryTreeWalk(Dir, &instrumentFinder); |
1383 |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
else instrumentFinder.ProcessDirectory(Dir, DirId); |
1389 |
|
|
1390 |
return instrumentFinder.GetInstruments(); |
return instrumentFinder.GetInstruments(); |
1391 |
} |
} |
1392 |
|
|
1393 |
|
StringListPtr InstrumentsDb::FindLostInstrumentFiles() { |
1394 |
|
dmsg(2,("InstrumentsDb: FindLostInstrumentFiles()\n")); |
1395 |
|
|
1396 |
|
BeginTransaction(); |
1397 |
|
try { |
1398 |
|
StringListPtr files = ExecSqlStringList("SELECT DISTINCT instr_file FROM instruments"); |
1399 |
|
StringListPtr result(new std::vector<String>); |
1400 |
|
for (int i = 0; i < files->size(); i++) { |
1401 |
|
File f(toNonEscapedFsPath(files->at(i))); |
1402 |
|
if (!f.Exist()) result->push_back(files->at(i)); |
1403 |
|
} |
1404 |
|
return result; |
1405 |
|
} catch (Exception e) { |
1406 |
|
EndTransaction(); |
1407 |
|
throw e; |
1408 |
|
} |
1409 |
|
EndTransaction(); |
1410 |
|
} |
1411 |
|
|
1412 |
|
void InstrumentsDb::SetInstrumentFilePath(String OldPath, String NewPath) { |
1413 |
|
if (OldPath == NewPath) return; |
1414 |
|
StringListPtr instrs; |
1415 |
|
BeginTransaction(); |
1416 |
|
try { |
1417 |
|
std::vector<String> params(2); |
1418 |
|
params[0] = toEscapedFsPath(NewPath); |
1419 |
|
params[1] = toEscapedFsPath(OldPath); |
1420 |
|
instrs = GetInstrumentsByFile(OldPath); |
1421 |
|
ExecSql("UPDATE instruments SET instr_file=? WHERE instr_file=?", params); |
1422 |
|
} catch (Exception e) { |
1423 |
|
EndTransaction(); |
1424 |
|
throw e; |
1425 |
|
} |
1426 |
|
EndTransaction(); |
1427 |
|
|
1428 |
|
for (int i = 0; i < instrs->size(); i++) { |
1429 |
|
FireInstrumentInfoChanged(instrs->at(i)); |
1430 |
|
} |
1431 |
|
} |
1432 |
|
|
1433 |
void InstrumentsDb::BeginTransaction() { |
void InstrumentsDb::BeginTransaction() { |
1434 |
dmsg(2,("InstrumentsDb: BeginTransaction(InTransaction=%d)\n", InTransaction)); |
dmsg(2,("InstrumentsDb: BeginTransaction(InTransaction=%d)\n", InTransaction)); |
1490 |
|
|
1491 |
void InstrumentsDb::ExecSql(String Sql) { |
void InstrumentsDb::ExecSql(String Sql) { |
1492 |
dmsg(2,("InstrumentsDb: ExecSql(Sql=%s)\n", Sql.c_str())); |
dmsg(2,("InstrumentsDb: ExecSql(Sql=%s)\n", Sql.c_str())); |
1493 |
sqlite3_stmt *pStmt = NULL; |
std::vector<String> Params; |
1494 |
|
ExecSql(Sql, Params); |
|
int res = sqlite3_prepare(GetDb(), Sql.c_str(), -1, &pStmt, NULL); |
|
|
if (res != SQLITE_OK) { |
|
|
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
|
|
} |
|
|
|
|
|
res = sqlite3_step(pStmt); |
|
|
if(res != SQLITE_DONE) { |
|
|
sqlite3_finalize(pStmt); |
|
|
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
|
|
} |
|
|
|
|
|
sqlite3_finalize(pStmt); |
|
1495 |
} |
} |
1496 |
|
|
1497 |
void InstrumentsDb::ExecSql(String Sql, String Param) { |
void InstrumentsDb::ExecSql(String Sql, String Param) { |
1498 |
dmsg(2,("InstrumentsDb: ExecSql(Sql=%s,Param=%s)\n", Sql.c_str(), Param.c_str())); |
dmsg(2,("InstrumentsDb: ExecSql(Sql=%s,Param=%s)\n", Sql.c_str(), Param.c_str())); |
1499 |
|
std::vector<String> Params; |
1500 |
|
Params.push_back(Param); |
1501 |
|
ExecSql(Sql, Params); |
1502 |
|
} |
1503 |
|
|
1504 |
|
void InstrumentsDb::ExecSql(String Sql, std::vector<String>& Params) { |
1505 |
|
dmsg(2,("InstrumentsDb: ExecSql(Sql=%s,Params)\n", Sql.c_str())); |
1506 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1507 |
|
|
1508 |
int res = sqlite3_prepare(GetDb(), Sql.c_str(), -1, &pStmt, NULL); |
int res = sqlite3_prepare(GetDb(), Sql.c_str(), -1, &pStmt, NULL); |
1511 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1512 |
} |
} |
1513 |
|
|
1514 |
BindTextParam(pStmt, 1, Param); |
for(int i = 0; i < Params.size(); i++) { |
1515 |
|
BindTextParam(pStmt, i + 1, Params[i]); |
1516 |
|
} |
1517 |
|
|
1518 |
res = sqlite3_step(pStmt); |
res = sqlite3_step(pStmt); |
1519 |
if (res != SQLITE_DONE) { |
if (res != SQLITE_DONE) { |
1595 |
} |
} |
1596 |
|
|
1597 |
IntListPtr InstrumentsDb::ExecSqlIntList(String Sql) { |
IntListPtr InstrumentsDb::ExecSqlIntList(String Sql) { |
1598 |
|
dmsg(2,("InstrumentsDb: ExecSqlIntList(Sql=%s)\n", Sql.c_str())); |
1599 |
|
std::vector<String> Params; |
1600 |
|
return ExecSqlIntList(Sql, Params); |
1601 |
|
} |
1602 |
|
|
1603 |
|
IntListPtr InstrumentsDb::ExecSqlIntList(String Sql, String Param) { |
1604 |
|
dmsg(2,("InstrumentsDb: ExecSqlIntList(Sql=%s,Param=%s)\n", Sql.c_str(), Param.c_str())); |
1605 |
|
std::vector<String> Params; |
1606 |
|
Params.push_back(Param); |
1607 |
|
return ExecSqlIntList(Sql, Params); |
1608 |
|
} |
1609 |
|
|
1610 |
|
IntListPtr InstrumentsDb::ExecSqlIntList(String Sql, std::vector<String>& Params) { |
1611 |
|
dmsg(2,("InstrumentsDb: ExecSqlIntList(Sql=%s)\n", Sql.c_str())); |
1612 |
IntListPtr intList(new std::vector<int>); |
IntListPtr intList(new std::vector<int>); |
1613 |
|
|
1614 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1618 |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
throw Exception("DB error: " + ToString(sqlite3_errmsg(db))); |
1619 |
} |
} |
1620 |
|
|
1621 |
|
for(int i = 0; i < Params.size(); i++) { |
1622 |
|
BindTextParam(pStmt, i + 1, Params[i]); |
1623 |
|
} |
1624 |
|
|
1625 |
res = sqlite3_step(pStmt); |
res = sqlite3_step(pStmt); |
1626 |
while(res == SQLITE_ROW) { |
while(res == SQLITE_ROW) { |
1627 |
intList->push_back(sqlite3_column_int(pStmt, 0)); |
intList->push_back(sqlite3_column_int(pStmt, 0)); |
1639 |
} |
} |
1640 |
|
|
1641 |
StringListPtr InstrumentsDb::ExecSqlStringList(String Sql) { |
StringListPtr InstrumentsDb::ExecSqlStringList(String Sql) { |
1642 |
|
dmsg(2,("InstrumentsDb: ExecSqlStringList(Sql=%s)\n", Sql.c_str())); |
1643 |
StringListPtr stringList(new std::vector<String>); |
StringListPtr stringList(new std::vector<String>); |
1644 |
|
|
1645 |
sqlite3_stmt *pStmt = NULL; |
sqlite3_stmt *pStmt = NULL; |
1683 |
} |
} |
1684 |
} |
} |
1685 |
|
|
1686 |
|
#ifndef WIN32 |
1687 |
void InstrumentsDb::Regexp(sqlite3_context* pContext, int argc, sqlite3_value** ppValue) { |
void InstrumentsDb::Regexp(sqlite3_context* pContext, int argc, sqlite3_value** ppValue) { |
1688 |
if (argc != 2) return; |
if (argc != 2) return; |
1689 |
|
|
1694 |
sqlite3_result_int(pContext, 1); |
sqlite3_result_int(pContext, 1); |
1695 |
} |
} |
1696 |
} |
} |
1697 |
|
#endif |
1698 |
|
|
1699 |
String InstrumentsDb::GetDirectoryPath(String File) { |
String InstrumentsDb::GetDirectoryPath(String File) { |
1700 |
if (File.empty()) return String(""); |
if (File.empty()) return String(""); |
1737 |
return Dir.substr(0, i); |
return Dir.substr(0, i); |
1738 |
} |
} |
1739 |
|
|
1740 |
|
void InstrumentsDb::Format() { |
1741 |
|
DbInstrumentsMutex.Lock(); |
1742 |
|
if (db != NULL) { |
1743 |
|
sqlite3_close(db); |
1744 |
|
db = NULL; |
1745 |
|
} |
1746 |
|
|
1747 |
|
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
1748 |
|
String bkp = DbFile + ".bkp"; |
1749 |
|
remove(bkp.c_str()); |
1750 |
|
if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) { |
1751 |
|
DbInstrumentsMutex.Unlock(); |
1752 |
|
throw Exception(String("Failed to backup database: ") + strerror(errno)); |
1753 |
|
} |
1754 |
|
|
1755 |
|
String f = DbFile; |
1756 |
|
DbFile = ""; |
1757 |
|
try { CreateInstrumentsDb(f); } |
1758 |
|
catch(Exception e) { |
1759 |
|
DbInstrumentsMutex.Unlock(); |
1760 |
|
throw e; |
1761 |
|
} |
1762 |
|
DbInstrumentsMutex.Unlock(); |
1763 |
|
|
1764 |
|
FireDirectoryCountChanged("/"); |
1765 |
|
FireInstrumentCountChanged("/"); |
1766 |
|
} |
1767 |
|
|
1768 |
void InstrumentsDb::CheckFileName(String File) { |
void InstrumentsDb::CheckFileName(String File) { |
1769 |
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); |
|
|
} |
|
1770 |
} |
} |
1771 |
|
|
1772 |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
String InstrumentsDb::GetUniqueName(int DirId, String Name) { |
1773 |
dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str())); |
dmsg(2,("InstrumentsDb: GetUniqueInstrumentName(DirId=%d,Name=%s)\n", DirId, Name.c_str())); |
1774 |
|
|
1775 |
if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; |
if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; |
1785 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1786 |
} |
} |
1787 |
|
|
1788 |
|
String InstrumentsDb::PrepareSubdirectory(String DbDir, String FsPath) { |
1789 |
|
std::string dir = Path::getBaseName(FsPath); |
1790 |
|
dir = toAbstractName(dir); |
1791 |
|
if(dir.empty()) dir = "New Directory"; |
1792 |
|
dir = GetUniqueName(GetDirectoryId(DbDir), dir); |
1793 |
|
dir = AppendNode(DbDir, dir); |
1794 |
|
AddDirectory(dir); |
1795 |
|
return dir; |
1796 |
|
} |
1797 |
|
|
1798 |
|
String InstrumentsDb::AppendNode(String DbDir, String Node) { |
1799 |
|
if(DbDir.length() == 1 && DbDir.at(0) == '/') return DbDir + Node; |
1800 |
|
if(DbDir.at(DbDir.length() - 1) == '/') return DbDir + Node; |
1801 |
|
return DbDir + "/" + Node; |
1802 |
|
} |
1803 |
|
|
1804 |
|
String InstrumentsDb::toDbName(String AbstractName) { |
1805 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1806 |
|
if (AbstractName.at(i) == '\0') AbstractName.at(i) = '/'; |
1807 |
|
} |
1808 |
|
return AbstractName; |
1809 |
|
} |
1810 |
|
|
1811 |
|
String InstrumentsDb::toEscapedPath(String AbstractName) { |
1812 |
|
for (int i = 0; i < AbstractName.length(); i++) { |
1813 |
|
if (AbstractName.at(i) == '\0') AbstractName.replace(i++, 1, "\\x2f"); |
1814 |
|
else if (AbstractName.at(i) == '\\') AbstractName.replace(i++, 1, "\\\\"); |
1815 |
|
else if (AbstractName.at(i) == '\'') AbstractName.replace(i++, 1, "\\'"); |
1816 |
|
else if (AbstractName.at(i) == '"') AbstractName.replace(i++, 1, "\\\""); |
1817 |
|
else if (AbstractName.at(i) == '\r') AbstractName.replace(i++, 1, "\\r"); |
1818 |
|
else if (AbstractName.at(i) == '\n') AbstractName.replace(i++, 1, "\\n"); |
1819 |
|
} |
1820 |
|
return AbstractName; |
1821 |
|
} |
1822 |
|
|
1823 |
|
String InstrumentsDb::toEscapedText(String text) { |
1824 |
|
for (int i = 0; i < text.length(); i++) { |
1825 |
|
if (text.at(i) == '\\') text.replace(i++, 1, "\\\\"); |
1826 |
|
else if (text.at(i) == '\'') text.replace(i++, 1, "\\'"); |
1827 |
|
else if (text.at(i) == '"') text.replace(i++, 1, "\\\""); |
1828 |
|
else if (text.at(i) == '\r') text.replace(i++, 1, "\\r"); |
1829 |
|
else if (text.at(i) == '\n') text.replace(i++, 1, "\\n"); |
1830 |
|
} |
1831 |
|
return text; |
1832 |
|
} |
1833 |
|
|
1834 |
|
String InstrumentsDb::toNonEscapedText(String text) { |
1835 |
|
String sb; |
1836 |
|
for (int i = 0; i < text.length(); i++) { |
1837 |
|
char c = text.at(i); |
1838 |
|
if(c == '\\') { |
1839 |
|
if(i >= text.length()) { |
1840 |
|
std::cerr << "Broken escape sequence!" << std::endl; |
1841 |
|
break; |
1842 |
|
} |
1843 |
|
char c2 = text.at(++i); |
1844 |
|
if(c2 == '\'') sb.push_back('\''); |
1845 |
|
else if(c2 == '"') sb.push_back('"'); |
1846 |
|
else if(c2 == '\\') sb.push_back('\\'); |
1847 |
|
else if(c2 == 'r') sb.push_back('\r'); |
1848 |
|
else if(c2 == 'n') sb.push_back('\n'); |
1849 |
|
else std::cerr << "Unknown escape sequence \\" << c2 << std::endl; |
1850 |
|
} else { |
1851 |
|
sb.push_back(c); |
1852 |
|
} |
1853 |
|
} |
1854 |
|
return sb; |
1855 |
|
} |
1856 |
|
|
1857 |
|
String InstrumentsDb::toEscapedFsPath(String FsPath) { |
1858 |
|
return toEscapedText(FsPath); |
1859 |
|
} |
1860 |
|
|
1861 |
|
String InstrumentsDb::toNonEscapedFsPath(String FsPath) { |
1862 |
|
return toNonEscapedText(FsPath); |
1863 |
|
} |
1864 |
|
|
1865 |
|
String InstrumentsDb::toAbstractName(String DbName) { |
1866 |
|
for (int i = 0; i < DbName.length(); i++) { |
1867 |
|
if (DbName.at(i) == '/') DbName.at(i) = '\0'; |
1868 |
|
} |
1869 |
|
return DbName; |
1870 |
|
} |
1871 |
|
|
1872 |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
1873 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1874 |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
1875 |
} |
} |
1876 |
} |
} |
1877 |
|
|
1878 |
void InstrumentsDb::FireDirectoryInfoChanged(String Dir) { |
void InstrumentsDb::FireDirectoryInfoChanged(String Dir) { |
1879 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1880 |
llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir); |
1881 |
} |
} |
1882 |
} |
} |
1883 |
|
|
1884 |
void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) { |
void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) { |
1885 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1886 |
llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName); |
llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName); |
1887 |
} |
} |
1888 |
} |
} |
1889 |
|
|
1890 |
void InstrumentsDb::FireInstrumentCountChanged(String Dir) { |
void InstrumentsDb::FireInstrumentCountChanged(String Dir) { |
1891 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1892 |
llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir); |
1893 |
} |
} |
1894 |
} |
} |
1895 |
|
|
1896 |
void InstrumentsDb::FireInstrumentInfoChanged(String Instr) { |
void InstrumentsDb::FireInstrumentInfoChanged(String Instr) { |
1897 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1898 |
llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr); |
llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr); |
1899 |
} |
} |
1900 |
} |
} |
1901 |
|
|
1902 |
void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) { |
void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) { |
1903 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1904 |
llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName); |
llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName); |
1905 |
} |
} |
1906 |
} |
} |
|
|
|
1907 |
|
|
1908 |
String DirectoryScanner::DbDir; |
void InstrumentsDb::FireJobStatusChanged(int JobId) { |
1909 |
String DirectoryScanner::FsDir; |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1910 |
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("/"); |
|
1911 |
} |
} |
|
DirectoryScanner::Flat = Flat; |
|
|
|
|
|
ftw(FsDir.c_str(), FtwCallback, 10); |
|
1912 |
} |
} |
1913 |
|
|
|
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; |
|
|
}; |
|
|
|
|
1914 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
|
|
|
|
#endif // HAVE_SQLITE3 |
|