1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2007 Grigor Iliev * |
* Copyright (C) 2007, 2008 Grigor Iliev * |
4 |
* * |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
20 |
|
|
21 |
#include "InstrumentsDb.h" |
#include "InstrumentsDb.h" |
22 |
|
|
23 |
#if HAVE_SQLITE3 |
#include "../common/global_private.h" |
24 |
|
|
25 |
#include <iostream> |
#include <iostream> |
26 |
#include <sstream> |
#include <sstream> |
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
36 |
InstrumentsDb* InstrumentsDb::pInstrumentsDb = new InstrumentsDb; |
InstrumentsDb InstrumentsDb::instance; |
37 |
|
|
38 |
void InstrumentsDb::CreateInstrumentsDb(String File) { |
void InstrumentsDb::CreateInstrumentsDb(String File) { |
39 |
struct stat statBuf; |
struct stat statBuf; |
95 |
if (db != NULL) sqlite3_close(db); |
if (db != NULL) sqlite3_close(db); |
96 |
} |
} |
97 |
|
|
|
void InstrumentsDb::Destroy() { |
|
|
if (pInstrumentsDb != NULL) { |
|
|
delete pInstrumentsDb; |
|
|
pInstrumentsDb = NULL; |
|
|
} |
|
|
} |
|
|
|
|
98 |
void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) { |
void InstrumentsDb::AddInstrumentsDbListener(InstrumentsDb::Listener* l) { |
99 |
llInstrumentsDbListeners.AddListener(l); |
llInstrumentsDbListeners.AddListener(l); |
100 |
} |
} |
104 |
} |
} |
105 |
|
|
106 |
InstrumentsDb* InstrumentsDb::GetInstrumentsDb() { |
InstrumentsDb* InstrumentsDb::GetInstrumentsDb() { |
107 |
return pInstrumentsDb; |
return &instance; |
108 |
} |
} |
109 |
|
|
110 |
void InstrumentsDb::SetDbFile(String File) { |
void InstrumentsDb::SetDbFile(String File) { |
120 |
sqlite3* InstrumentsDb::GetDb() { |
sqlite3* InstrumentsDb::GetDb() { |
121 |
if ( db != NULL) return db; |
if ( db != NULL) return db; |
122 |
|
|
123 |
if (DbFile.empty()) DbFile = "/var/lib/linuxsampler/instruments.db"; |
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
124 |
|
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
125 |
|
if (DbFile.find("~") == 0) |
126 |
|
DbFile.replace(0, 1, getenv("HOME")); |
127 |
|
#endif |
128 |
int rc = sqlite3_open(DbFile.c_str(), &db); |
int rc = sqlite3_open(DbFile.c_str(), &db); |
129 |
if (rc) { |
if (rc) { |
130 |
sqlite3_close(db); |
sqlite3_close(db); |
1165 |
try { |
try { |
1166 |
riff = new RIFF::File(File); |
riff = new RIFF::File(File); |
1167 |
gig::File* gig = new gig::File(riff); |
gig::File* gig = new gig::File(riff); |
1168 |
|
gig->SetAutoLoad(false); // avoid time consuming samples scanning |
1169 |
|
|
1170 |
std::stringstream sql; |
std::stringstream sql; |
1171 |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
sql << "INSERT INTO instruments (dir_id,instr_name,instr_file,"; |
1621 |
return Dir.substr(0, i); |
return Dir.substr(0, i); |
1622 |
} |
} |
1623 |
|
|
1624 |
|
void InstrumentsDb::Format() { |
1625 |
|
DbInstrumentsMutex.Lock(); |
1626 |
|
if (db != NULL) { |
1627 |
|
sqlite3_close(db); |
1628 |
|
db = NULL; |
1629 |
|
} |
1630 |
|
|
1631 |
|
if (DbFile.empty()) DbFile = CONFIG_DEFAULT_INSTRUMENTS_DB_LOCATION; |
1632 |
|
String bkp = DbFile + ".bkp"; |
1633 |
|
remove(bkp.c_str()); |
1634 |
|
if (rename(DbFile.c_str(), bkp.c_str()) && errno != ENOENT) { |
1635 |
|
DbInstrumentsMutex.Unlock(); |
1636 |
|
throw Exception(String("Failed to backup database: ") + strerror(errno)); |
1637 |
|
} |
1638 |
|
|
1639 |
|
String f = DbFile; |
1640 |
|
DbFile = ""; |
1641 |
|
try { CreateInstrumentsDb(f); } |
1642 |
|
catch(Exception e) { |
1643 |
|
DbInstrumentsMutex.Unlock(); |
1644 |
|
throw e; |
1645 |
|
} |
1646 |
|
DbInstrumentsMutex.Unlock(); |
1647 |
|
|
1648 |
|
FireDirectoryCountChanged("/"); |
1649 |
|
FireInstrumentCountChanged("/"); |
1650 |
|
} |
1651 |
|
|
1652 |
void InstrumentsDb::CheckFileName(String File) { |
void InstrumentsDb::CheckFileName(String File) { |
1653 |
if (File.empty()) throw Exception("Invalid file name: " + File); |
if (File.empty()) throw Exception("Invalid file name: " + File); |
1654 |
} |
} |
1753 |
} |
} |
1754 |
|
|
1755 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |
|
|
|
|
#endif // HAVE_SQLITE3 |
|