oopapidocs
2.0
|
00001 #include "dbconnectionsettings.h" 00002 #include "playlistdb.h" 00003 #include "metadatatable.h" 00004 #include "dbutils.h" 00005 #include <QSqlRecord> 00006 #include <QSqlQuery> 00007 #include <QSqlDriver> 00008 #include <QStringList> 00009 #include <QSqlField> 00010 #include <QDebug> 00011 #include <QSqlError> 00012 #include <QFileInfo> 00013 #include <QDir> 00014 00015 00016 const QString PlayListDb::TABLEPREFIX("dbplaylist_"); 00017 00018 00019 00020 00021 bool PlayListDb::clear(QString playListName) { 00022 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00023 QSqlQuery drop(QString("delete from %1").arg(tableName(playListName))); 00024 // QSqlQuery drop(QString("drop table %1").arg(tableName(playListName))); 00025 if (!drop.isActive()) { 00026 qDebug() << "Failed: " << drop.lastError().text(); 00027 qDebug() << " " << drop.lastQuery(); 00028 return false; 00029 } 00030 return true; 00031 } 00032 00033 void PlayListDb::loadAndStore(QString fileName, QString playListName) { 00034 QFileInfo fi(fileName); 00035 QString basePath = fi.absolutePath(); 00036 if (!basePath.endsWith("/")) basePath += "/"; 00037 QFile f(fileName); 00038 f.open(QIODevice::ReadOnly); 00039 QTextStream is(&f); 00040 while (!is.atEnd()) { 00041 QString current = is.readLine(); 00042 if (current.startsWith("#")) continue; 00043 QFileInfo fi(current); 00044 if (!fi.isAbsolute()) { 00045 fi = QFileInfo(QDir(basePath), current); 00046 } 00047 addFile(playListName, fi.absoluteFilePath()); 00048 } 00049 } 00050 00051 QStringList PlayListDb::playLists() const { 00052 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00053 QStringList sl; 00054 foreach (QString s, db.tables()) 00055 if (s.startsWith(TABLEPREFIX)) { 00056 sl << s.mid(TABLEPREFIX.length()); 00057 } 00058 return sl; 00059 } 00060 00061 00062 QStringList PlayListDb::playList(QString playListName) { 00063 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00064 QStringList sl; 00065 QSqlQuery findQuery(QString("select FileName from %1 order by pid").arg(tableName(playListName))); 00066 if (!findQuery.isActive()) { 00067 qDebug() << db.lastError().text(); 00068 qDebug() << "Unable to execute: " << findQuery.lastQuery(); 00069 } 00070 while (findQuery.next()) { 00071 sl << findQuery.value(0).toString(); 00072 } 00073 return sl; 00074 } 00075 00076 QString PlayListDb::takeFirst(QString playListName) { 00077 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00078 QSqlQuery findQuery(QString("select FileName from %1 order by pid limit 1") 00079 .arg(tableName(playListName))); 00080 if (!findQuery.isActive()) return QString(); 00081 findQuery.next(); 00082 QString path = findQuery.value(0).toString(); 00083 removeFile(playListName, path); 00084 return path; 00085 } 00086 00087 QString PlayListDb::last(QString playListName) { 00088 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00089 QSqlQuery findQuery(QString("select FileName from %1 order by pid DESC limit 1") 00090 .arg(tableName(playListName))); 00091 if (!findQuery.isActive()) return QString(); 00092 findQuery.next(); 00093 return findQuery.value(0).toString(); 00094 } 00095 00096 QString PlayListDb::tableName(QString playListName) { 00097 playListName = playListName.replace(QRegExp("[\\s\\W]+"), QString()); 00098 return QString("dbplaylist_%1").arg(playListName); 00099 } 00100 00101 bool PlayListDb::removeEntry(QString playList, int pid) { 00102 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00103 QString tn = tableName(playList); 00104 QSqlRecord record = db.record(tn); 00105 Q_ASSERT(!record.isEmpty()); 00106 QString queryString=QString("delete from %1 where pid=%2") 00107 .arg(tn).arg(pid); 00108 QSqlQuery q(queryString); 00109 qDebug() << queryString; 00110 return q.isActive(); 00111 } 00112 00113 bool PlayListDb::removeFile(QString playList, QString path) { 00114 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00115 QString tn = tableName(playList); 00116 QSqlRecord record = db.record(tn); 00117 Q_ASSERT(!record.isEmpty()); 00118 QString queryString=QString("delete from %1 where FileName=:fileName order by pid limit 1") 00119 .arg(tn); 00120 if (db.driverName() == "QSQLITE") { 00121 queryString=QString("delete from %1 where FileName=:fileName").arg(tn); 00122 } 00123 QSqlQuery q; 00124 q.prepare(queryString); 00125 q.bindValue("fileName", path); 00126 if (!q.exec()) { 00127 qDebug() << q.lastError().text() << q.lastQuery(); 00128 return false; 00129 } 00130 return true; 00131 } 00132 00133 void PlayListDb::shuffle(QString playListName) { 00134 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00135 QString tn = tableName(playListName); 00136 QStringList sl; 00137 QSqlQuery findQuery(QString("select FileName from %1 order by RAND()").arg(tableName(playListName))); 00138 if (!findQuery.isActive()) { 00139 qDebug() << db.lastError().text(); 00140 qDebug() << "Unable to execute: " << findQuery.lastQuery(); 00141 } 00142 while (findQuery.next()) { 00143 sl << findQuery.value(0).toString(); 00144 } 00145 clear(playListName); 00146 foreach (const QString& fn, sl) { 00147 addFile(playListName, fn); 00148 } 00149 } 00150 00151 bool PlayListDb::addFile(QString playListName, QString fileName, bool replace) { 00152 QFileInfo fi(fileName); 00153 00154 if (!fi.exists()) { 00155 qDebug() << "file does not exist:" << fileName; 00156 return false; 00157 } 00158 00159 QSqlDatabase db = DbConnectionSettings::lastSaved(); 00160 if (!db.isOpen()) db.open(); 00161 QString tn = tableName(playListName); 00162 QSqlRecord record = db.record(tn); 00163 if (record.isEmpty()) { 00164 QString createString =QString("create table if not exists %1 " 00165 "(pid INTEGER AUTO_INCREMENT PRIMARY KEY, FileName text) DEFAULT CHARSET utf8").arg(tn) ; 00166 if (db.driverName() == "QSQLITE") 00167 createString = QString("create table if not exists %1 " 00168 "(pid INTEGER PRIMARY KEY, FileName text)").arg(tn) ; 00169 00170 QSqlQuery createTable(createString); 00171 if (!createTable.isActive()) { 00172 qDebug() << "Failed:" << createTable.lastQuery() << createTable.lastError().text(); 00173 abort(); 00174 } 00175 } 00176 QString fn = fi.absoluteFilePath(); 00177 00178 if (replace) { 00179 removeFile(playListName, fn); 00180 } 00181 // Insert into the DB 00182 QString queryString = QString("INSERT INTO %1 (FileName) VALUES(:fileName)").arg(tn); 00183 QSqlQuery q; 00184 if (!q.prepare(queryString)) { 00185 qDebug() << "prepare failed: " << q.lastError().text(); 00186 return false; 00187 } 00188 q.bindValue("fileName", fn); 00189 00190 if (!q.exec()) { 00191 qDebug() << "failed to insert: " << q.lastQuery() << q.lastError().text(); 00192 return false; 00193 } 00194 // refresh metadata 00195 MetaDataTable::instance()->hasMetaData(fn, true); 00196 return true; 00197 } 00198 00199 void PlayListDb::rename (QString oldName, QString newName) { 00200 if (tableName(oldName) == tableName(newName)) return; 00201 QStringList sl = playList(oldName); 00202 clear(newName); 00203 foreach (QString n, sl) { 00204 addFile(newName, n); 00205 } 00206 clear(oldName); 00207 qDebug() << "renamed " << oldName << " to " << newName; 00208 }