21 |
#include "InstrumentsDb.h" |
#include "InstrumentsDb.h" |
22 |
|
|
23 |
#include "../common/File.h" |
#include "../common/File.h" |
24 |
|
#include "../common/Path.h" |
25 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
26 |
|
|
27 |
#include <iostream> |
#include <iostream> |
642 |
FireDirectoryInfoChanged(Dir); |
FireDirectoryInfoChanged(Dir); |
643 |
} |
} |
644 |
|
|
645 |
int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground) { |
int InstrumentsDb::AddInstruments(ScanMode Mode, String DbDir, String FsDir, bool bBackground, bool insDir) { |
646 |
dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground)); |
dmsg(2,("InstrumentsDb: AddInstruments(Mode=%d,DbDir=%s,FsDir=%s,bBackground=%d,insDir=%d)\n", Mode, DbDir.c_str(), FsDir.c_str(), bBackground, insDir)); |
647 |
if(!bBackground) { |
if(!bBackground) { |
648 |
switch (Mode) { |
switch (Mode) { |
649 |
case NON_RECURSIVE: |
case NON_RECURSIVE: |
650 |
AddInstrumentsNonrecursive(DbDir, FsDir); |
AddInstrumentsNonrecursive(DbDir, FsDir, insDir); |
651 |
break; |
break; |
652 |
case RECURSIVE: |
case RECURSIVE: |
653 |
AddInstrumentsRecursive(DbDir, FsDir); |
AddInstrumentsRecursive(DbDir, FsDir, false, insDir); |
654 |
break; |
break; |
655 |
case FLAT: |
case FLAT: |
656 |
AddInstrumentsRecursive(DbDir, FsDir, true); |
AddInstrumentsRecursive(DbDir, FsDir, true, insDir); |
657 |
break; |
break; |
658 |
default: |
default: |
659 |
throw Exception("Unknown scan mode"); |
throw Exception("Unknown scan mode"); |
664 |
|
|
665 |
ScanJob job; |
ScanJob job; |
666 |
int jobId = Jobs.AddJob(job); |
int jobId = Jobs.AddJob(job); |
667 |
InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir)); |
InstrumentsDbThread.Execute(new AddInstrumentsJob(jobId, Mode, DbDir, FsDir, insDir)); |
668 |
|
|
669 |
return jobId; |
return jobId; |
670 |
} |
} |
672 |
int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { |
int InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, bool bBackground) { |
673 |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground)); |
674 |
if(!bBackground) { |
if(!bBackground) { |
675 |
AddInstruments(DbDir, FilePath, Index); |
AddInstruments(DbDir, false, FilePath, Index); |
676 |
return -1; |
return -1; |
677 |
} |
} |
678 |
|
|
679 |
ScanJob job; |
ScanJob job; |
680 |
int jobId = Jobs.AddJob(job); |
int jobId = Jobs.AddJob(job); |
681 |
InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index)); |
InstrumentsDbThread.Execute(new AddInstrumentsFromFileJob(jobId, DbDir, FilePath, Index, false)); |
682 |
|
|
683 |
return jobId; |
return jobId; |
684 |
} |
} |
685 |
|
|
686 |
void InstrumentsDb::AddInstruments(String DbDir, String FilePath, int Index, ScanProgress* pProgress) { |
void InstrumentsDb::AddInstruments(String DbDir, bool insDir, String FilePath, int Index, ScanProgress* pProgress) { |
687 |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddInstruments(DbDir=%s,insDir=%d,FilePath=%s,Index=%d)\n", DbDir.c_str(), insDir, FilePath.c_str(), Index)); |
688 |
if (DbDir.empty() || FilePath.empty()) return; |
if (DbDir.empty() || FilePath.empty()) return; |
689 |
|
|
690 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
705 |
throw Exception(ss.str()); |
throw Exception(ss.str()); |
706 |
} |
} |
707 |
|
|
708 |
AddInstrumentsFromFile(DbDir, FilePath, Index, pProgress); |
String dir = insDir ? PrepareSubdirectory(DbDir, FilePath) : DbDir; |
709 |
|
AddInstrumentsFromFile(dir, FilePath, Index, pProgress); |
710 |
} catch (Exception e) { |
} catch (Exception e) { |
711 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
712 |
throw e; |
throw e; |
715 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
716 |
} |
} |
717 |
|
|
718 |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, ScanProgress* pProgress) { |
void InstrumentsDb::AddInstrumentsNonrecursive(String DbDir, String FsDir, bool insDir, ScanProgress* pProgress) { |
719 |
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)); |
720 |
if (DbDir.empty() || FsDir.empty()) return; |
if (DbDir.empty() || FsDir.empty()) return; |
721 |
|
|
722 |
DbInstrumentsMutex.Lock(); |
DbInstrumentsMutex.Lock(); |
742 |
try { |
try { |
743 |
FileListPtr fileList = File::GetFiles(FsDir); |
FileListPtr fileList = File::GetFiles(FsDir); |
744 |
for (int i = 0; i < fileList->size(); i++) { |
for (int i = 0; i < fileList->size(); i++) { |
745 |
AddInstrumentsFromFile(DbDir, FsDir + fileList->at(i), -1, pProgress); |
String dir = insDir ? PrepareSubdirectory(DbDir, fileList->at(i)) : DbDir; |
746 |
|
AddInstrumentsFromFile(dir, FsDir + fileList->at(i), -1, pProgress); |
747 |
} |
} |
748 |
} catch(Exception e) { |
} catch(Exception e) { |
749 |
e.PrintMessage(); |
e.PrintMessage(); |
758 |
DbInstrumentsMutex.Unlock(); |
DbInstrumentsMutex.Unlock(); |
759 |
} |
} |
760 |
|
|
761 |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, ScanProgress* pProgress) { |
void InstrumentsDb::AddInstrumentsRecursive(String DbDir, String FsDir, bool Flat, bool insDir, ScanProgress* pProgress) { |
762 |
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)); |
763 |
if (pProgress != NULL) { |
if (pProgress != NULL) { |
764 |
InstrumentFileCounter c; |
InstrumentFileCounter c; |
765 |
pProgress->SetTotalFileCount(c.Count(FsDir)); |
pProgress->SetTotalFileCount(c.Count(FsDir)); |
766 |
} |
} |
767 |
|
|
768 |
DirectoryScanner d; |
DirectoryScanner d; |
769 |
d.Scan(DbDir, FsDir, Flat, pProgress); |
d.Scan(DbDir, FsDir, Flat, insDir, pProgress); |
770 |
} |
} |
771 |
|
|
772 |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
int InstrumentsDb::GetInstrumentCount(int DirId) { |
1253 |
dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); |
dmsg(2,("InstrumentsDb: AddGigInstrument(DbDir=%s,DirId=%d,File=%s,Index=%d)\n", DbDir.c_str(), DirId, File.c_str(), Index)); |
1254 |
String name = pInstrument->pInfo->Name; |
String name = pInstrument->pInfo->Name; |
1255 |
if (name == "") return; |
if (name == "") return; |
1256 |
name = GetUniqueInstrumentName(DirId, name); |
name = GetUniqueName(DirId, name); |
1257 |
|
|
1258 |
std::stringstream sql2; |
std::stringstream sql2; |
1259 |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
sql2 << "SELECT COUNT(*) FROM instruments WHERE instr_file=? AND "; |
1727 |
if (File.empty()) throw Exception("Invalid file name: " + File); |
if (File.empty()) throw Exception("Invalid file name: " + File); |
1728 |
} |
} |
1729 |
|
|
1730 |
String InstrumentsDb::GetUniqueInstrumentName(int DirId, String Name) { |
String InstrumentsDb::GetUniqueName(int DirId, String Name) { |
1731 |
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())); |
1732 |
|
|
1733 |
if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; |
if (GetInstrumentId(DirId, Name) == -1 && GetDirectoryId(DirId, Name) == -1) return Name; |
1742 |
|
|
1743 |
throw Exception("Unable to find an unique name: " + Name); |
throw Exception("Unable to find an unique name: " + Name); |
1744 |
} |
} |
1745 |
|
|
1746 |
|
String InstrumentsDb::PrepareSubdirectory(String DbDir, String FsPath) { |
1747 |
|
std::string dir = Path::getBaseName(FsPath); |
1748 |
|
dir = toAbstractName(dir); |
1749 |
|
if(dir.empty()) dir = "New Directory"; |
1750 |
|
dir = GetUniqueName(GetDirectoryId(DbDir), dir); |
1751 |
|
dir = AppendNode(DbDir, dir); |
1752 |
|
AddDirectory(dir); |
1753 |
|
return dir; |
1754 |
|
} |
1755 |
|
|
1756 |
|
String InstrumentsDb::AppendNode(String DbDir, String Node) { |
1757 |
|
if(DbDir.length() == 1 && DbDir.at(0) == '/') return DbDir + Node; |
1758 |
|
if(DbDir.at(DbDir.length() - 1) == '/') return DbDir + Node; |
1759 |
|
return DbDir + "/" + Node; |
1760 |
|
} |
1761 |
|
|
1762 |
String InstrumentsDb::toDbName(String AbstractName) { |
String InstrumentsDb::toDbName(String AbstractName) { |
1763 |
for (int i = 0; i < AbstractName.length(); i++) { |
for (int i = 0; i < AbstractName.length(); i++) { |