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) { |
597 |
FireDirectoryInfoChanged(Dir); |
FireDirectoryInfoChanged(Dir); |
598 |
} |
} |
599 |
|
|
600 |
void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index) { |
int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) { |
601 |
|
dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground)); |
602 |
|
if(!bBackground) { |
603 |
|
switch (Mode) { |
604 |
|
case NON_RECURSIVE: |
605 |
|
AddInstrumentsNonrecursive(DbDir, FsDir); |
606 |
|
break; |
607 |
|
case RECURSIVE: |
608 |
|
AddInstrumentsRecursive(DbDir, FsDir); |
609 |
|
break; |
610 |
|
case FLAT: |
611 |
|
AddInstrumentsRecursive(DbDir, FsDir, true); |
612 |
|
break; |
613 |
|
default: |
614 |
|
throw Exception("Unknown scan mode"); |
615 |
|
} |
616 |
|
|
617 |
|
return -1; |
618 |
|
} |
619 |
|
|
620 |
|
ScanJob job; |
621 |
|
int jobId = Jobs.AddJob(job); |
622 |
|
InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir)); |
623 |
|
|
624 |
|
return jobId; |
625 |
|
} |
626 |
|
|
627 |
|
int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { |
628 |
|
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); |
629 |
|
if(!bBackground) { |
630 |
|
AddInstruments(DbDir, FilePath, Index); |
631 |
|
return -1; |
632 |
|
} |
633 |
|
|
634 |
|
ScanJob job; |
635 |
|
int jobId = Jobs.AddJob(job); |
636 |
|
InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index)); |
637 |
|
|
638 |
|
return jobId; |
639 |
|
} |
640 |
|
|
641 |
|
void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { |
642 |
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)); |
643 |
if (DbDir.empty() || FilePath.empty()) return; |
if (DbDir.empty() || FilePath.empty()) return; |
644 |
|
|
655 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
656 |
} |
} |
657 |
|
|
658 |
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) { |
|
659 |
std::stringstream ss; |
std::stringstream ss; |
660 |
ss << "`" << FilePath << "` is directory, not an instrument file"; |
ss << "`" << FilePath << "` is not an instrument file"; |
661 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
662 |
} |
} |
663 |
|
|
664 |
AddInstrumentsRecursive(DbDir, FilePath, false); |
AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress); |
665 |
} catch (Exception e) { |
} catch (Exception e) { |
666 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
667 |
throw e; |
throw e; |
670 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
671 |
} |
} |
672 |
|
|
673 |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir) { |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) { |
674 |
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())); |
675 |
if (DbDir.empty() || FsDir.empty()) return; |
if (DbDir.empty() || FsDir.empty()) return; |
676 |
|
|
710 |
continue; |
continue; |
711 |
} |
} |
712 |
|
|
713 |
AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name)); |
AddInstrumentsFromFile(DbDir, FsDir + String(pEnt->d_name), -1, pProgress); |
714 |
pEnt = readdir(pDir); |
pEnt = readdir(pDir); |
715 |
} |
} |
716 |
|
|
728 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
729 |
} |
} |
730 |
|
|
731 |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat) { |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { |
732 |
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)); |
733 |
DirectoryScanner::Scan(DbDir, FsDir, Flat); |
if (pProgress != NULL) { |
734 |
|
pProgress->SetTotalFileCount(InstrumentFileCounter::Count(FsDir)); |
735 |
|
} |
736 |
|
|
737 |
|
DirectoryScanner::Scan(DbDir, FsDir, Flat, pProgress); |
738 |
} |
} |
739 |
|
|
740 |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
1091 |
FireInstrumentInfoChanged(Instr); |
FireInstrumentInfoChanged(Instr); |
1092 |
} |
} |
1093 |
|
|
1094 |
void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index) { |
void InstrumentsDb::AddInstrumentsFromFile(String DbDir, String File, int Index, ScanProgress* pProgress) { |
1095 |
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)); |
1096 |
|
|
1097 |
if(File.length() < 4) return; |
if(File.length() < 4) return; |
1098 |
|
|
1099 |
try { |
try { |
1100 |
if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) { |
if(!strcasecmp(".gig", File.substr(File.length() - 4).c_str())) { |
1101 |
AddGigInstruments(DbDir, File, Index); |
if (pProgress != NULL) { |
1102 |
|
pProgress->SetStatus(0); |
1103 |
|
pProgress->CurrentFile = File; |
1104 |
|
} |
1105 |
|
|
1106 |
|
AddGigInstruments(DbDir, File, Index, pProgress); |
1107 |
|
|
1108 |
|
if (pProgress != NULL) { |
1109 |
|
pProgress->SetScannedFileCount(pProgress->GetScannedFileCount() + 1); |
1110 |
|
} |
1111 |
} |
} |
1112 |
} catch(Exception e) { |
} catch(Exception e) { |
1113 |
std::cerr << e.Message() << std::endl; |
std::cerr << e.Message() << std::endl; |
1114 |
} |
} |
1115 |
} |
} |
1116 |
|
|
1117 |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index) { |
void InstrumentsDb::AddGigInstruments(String DbDir, String File, int Index, ScanProgress* pProgress) { |
1118 |
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)); |
1119 |
int dirId = GetDirectoryId(DbDir); |
int dirId = GetDirectoryId(DbDir); |
1120 |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
if (dirId == -1) throw Exception("Invalid DB directory: " + DbDir); |
1138 |
try { |
try { |
1139 |
riff = new RIFF::File(File); |
riff = new RIFF::File(File); |
1140 |
gig::File* gig = new gig::File(riff); |
gig::File* gig = new gig::File(riff); |
1141 |
|
|
1142 |
std::stringstream sql; |
std::stringstream sql; |
1143 |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
1144 |
sql << "instr_nr,format_family,format_version,instr_size,"; |
sql << "instr_nr,format_family,format_version,instr_size,"; |
1159 |
|
|
1160 |
if (Index == -1) { |
if (Index == -1) { |
1161 |
int instrIndex = 0; |
int instrIndex = 0; |
1162 |
|
if (pProgress != NULL) gig->GetInstrument(0, &(pProgress->GigFileProgress)); // TODO: this workaround should be fixed |
1163 |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
gig::Instrument* pInstrument = gig->GetFirstInstrument(); |
1164 |
while (pInstrument) { |
while (pInstrument) { |
1165 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1171 |
pInstrument = gig->GetNextInstrument(); |
pInstrument = gig->GetNextInstrument(); |
1172 |
} |
} |
1173 |
} else { |
} else { |
1174 |
gig::Instrument* pInstrument = gig->GetInstrument(Index); |
gig::Instrument* pInstrument; |
1175 |
|
if (pProgress == NULL) pInstrument = gig->GetInstrument(Index); |
1176 |
|
else pInstrument = gig->GetInstrument(Index, &(pProgress->GigFileProgress)); |
1177 |
if (pInstrument != NULL) { |
if (pInstrument != NULL) { |
1178 |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
BindTextParam(pStmt, 7, gig->pInfo->Product); |
1179 |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
BindTextParam(pStmt, 8, gig->pInfo->Artists); |
1610 |
|
|
1611 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1612 |
} |
} |
1613 |
|
|
1614 |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
void InstrumentsDb::FireDirectoryCountChanged(String Dir) { |
1615 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1616 |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryCountChanged(Dir); |
1617 |
} |
} |
1618 |
} |
} |
1619 |
|
|
1620 |
void InstrumentsDb::FireDirectoryInfoChanged(String Dir) { |
void InstrumentsDb::FireDirectoryInfoChanged(String Dir) { |
1621 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1622 |
llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->DirectoryInfoChanged(Dir); |
1623 |
} |
} |
1624 |
} |
} |
1625 |
|
|
1626 |
void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) { |
void InstrumentsDb::FireDirectoryNameChanged(String Dir, String NewName) { |
1627 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1628 |
llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName); |
llInstrumentsDbListeners.GetListener(i)->DirectoryNameChanged(Dir, NewName); |
1629 |
} |
} |
1630 |
} |
} |
1631 |
|
|
1632 |
void InstrumentsDb::FireInstrumentCountChanged(String Dir) { |
void InstrumentsDb::FireInstrumentCountChanged(String Dir) { |
1633 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1634 |
llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir); |
llInstrumentsDbListeners.GetListener(i)->InstrumentCountChanged(Dir); |
1635 |
} |
} |
1636 |
} |
} |
1637 |
|
|
1638 |
void InstrumentsDb::FireInstrumentInfoChanged(String Instr) { |
void InstrumentsDb::FireInstrumentInfoChanged(String Instr) { |
1639 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1640 |
llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr); |
llInstrumentsDbListeners.GetListener(i)->InstrumentInfoChanged(Instr); |
1641 |
} |
} |
1642 |
} |
} |
1643 |
|
|
1644 |
void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) { |
void InstrumentsDb::FireInstrumentNameChanged(String Instr, String NewName) { |
1645 |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1646 |
llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName); |
llInstrumentsDbListeners.GetListener(i)->InstrumentNameChanged(Instr, NewName); |
1647 |
} |
} |
1648 |
} |
} |
|
|
|
|
|
|
|
String DirectoryScanner::DbDir; |
|
|
String DirectoryScanner::FsDir; |
|
|
bool DirectoryScanner::Flat; |
|
|
|
|
|
void DirectoryScanner::Scan(String DbDir, String FsDir, bool Flat) { |
|
|
dmsg(2,("DirectoryScanner: Scan(DbDir=%s,FsDir=%s,Flat=%d)\n", DbDir.c_str(), FsDir.c_str(), Flat)); |
|
|
if (DbDir.empty() || FsDir.empty()) throw Exception("Directory expected"); |
|
|
|
|
|
struct stat statBuf; |
|
|
int res = stat(FsDir.c_str(), &statBuf); |
|
|
if (res) { |
|
|
std::stringstream ss; |
|
|
ss << "Fail to stat `" << FsDir << "`: " << strerror(errno); |
|
|
throw Exception(ss.str()); |
|
|
} |
|
1649 |
|
|
1650 |
if (!S_ISDIR(statBuf.st_mode)) { |
void InstrumentsDb::FireJobStatusChanged(int JobId) { |
1651 |
throw Exception("Directory expected"); |
for (int i = 0; i < llInstrumentsDbListeners.GetListenerCount(); i++) { |
1652 |
} |
llInstrumentsDbListeners.GetListener(i)->JobStatusChanged(JobId); |
|
|
|
|
DirectoryScanner::DbDir = DbDir; |
|
|
DirectoryScanner::FsDir = FsDir; |
|
|
if (DbDir.at(DbDir.length() - 1) != '/') { |
|
|
DirectoryScanner::DbDir.append("/"); |
|
|
} |
|
|
if (FsDir.at(FsDir.length() - 1) != '/') { |
|
|
DirectoryScanner::FsDir.append("/"); |
|
1653 |
} |
} |
|
DirectoryScanner::Flat = Flat; |
|
|
|
|
|
ftw(FsDir.c_str(), FtwCallback, 10); |
|
1654 |
} |
} |
1655 |
|
|
|
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; |
|
|
}; |
|
|
|
|
1656 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
1657 |
|
|
1658 |
#endif // HAVE_SQLITE3 |
#endif // HAVE_SQLITE3 |