| 1 | /* |
| 2 | Copyright (C) 2004 Max Howell <[email protected]> |
| 3 | Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU Lesser General Public License as published |
| 7 | by the Free Software Foundation; either version 2.1 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "collectionupdater.h" |
| 20 | |
| 21 | #include "collectiondb.h" |
| 22 | |
| 23 | #if QT_VERSION >= 0x050000 |
| 24 | #include <QtConcurrent/QtConcurrent> |
| 25 | #else |
| 26 | #include <QtConcurrentRun> |
| 27 | #endif |
| 28 | |
| 29 | class CollectionUpdaterPrivate { |
| 30 | public: |
| 31 | bool doMonitor; |
| 32 | QStringList dirs; |
| 33 | bool isStoped; |
| 34 | QPoint startPos; |
| 35 | bool openContext; |
| 36 | bool dragLocked; |
| 37 | QTimer* timer; |
| 38 | bool incremental; |
| 39 | CollectionDB* collectionDB; |
| 40 | QMutex mutex; |
| 41 | }; |
| 42 | |
| 43 | CollectionUpdater::CollectionUpdater() |
| 44 | { |
| 45 | p = new CollectionUpdaterPrivate; |
| 46 | |
| 47 | QSettings settings; |
| 48 | |
| 49 | p->doMonitor = settings.value("Monitor").toBool(); |
| 50 | p->dirs = settings.value("Dirs").toStringList(); |
| 51 | |
| 52 | p->collectionDB = new CollectionDB(); |
| 53 | if (!p->collectionDB) |
| 54 | qWarning() << Q_FUNC_INFO << "Could not open SQLite database\n"; |
| 55 | |
| 56 | //optimization for speeding up SQLite |
| 57 | p->collectionDB->executeSql("PRAGMA synchronous = OFF;"); |
| 58 | |
| 59 | if (!p->collectionDB->isDbValid()) { |
| 60 | qDebug() << "Rebuilding database!" << endl; |
| 61 | p->collectionDB->dropTables(); |
| 62 | p->collectionDB->createTables(); |
| 63 | p->collectionDB->dropStatsTable(); |
| 64 | p->collectionDB->createStatsTable(); |
| 65 | scan(); |
| 66 | } |
| 67 | |
| 68 | p->timer = new QTimer(this); |
| 69 | p->timer->setInterval(600000); //1000 * 60 * 10 = 10min |
| 70 | connect(p->timer, SIGNAL(timeout()), this, SLOT(monitor())); |
| 71 | if (p->doMonitor) { |
| 72 | monitor(); |
| 73 | p->timer->start(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | CollectionUpdater::~CollectionUpdater() |
| 78 | { |
| 79 | delete p; |
| 80 | } |
| 81 | |
| 82 | void CollectionUpdater::setDoMonitor(bool value) |
| 83 | { |
| 84 | p->doMonitor = value; |
| 85 | if (p->doMonitor) |
| 86 | p->timer->start(); |
| 87 | else |
| 88 | p->timer->stop(); |
| 89 | } |
| 90 | |
| 91 | void CollectionUpdater::stop() |
| 92 | { |
| 93 | p->isStoped = true; |
| 94 | } |
| 95 | |
| 96 | void CollectionUpdater::setDirectoryList(QStringList dirs, bool force) |
| 97 | { |
| 98 | if (p->dirs != dirs || force) { |
| 99 | p->dirs = dirs; |
| 100 | scan(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void CollectionUpdater::monitor() |
| 105 | { |
| 106 | qDebug() << Q_FUNC_INFO; |
| 107 | |
| 108 | p->incremental = true; |
| 109 | p->isStoped = false; |
| 110 | |
| 111 | QStringList folders; |
| 112 | |
| 113 | QList<QStringList> entries = p->collectionDB->selectSql("SELECT dir, changedate FROM directories;"); |
| 114 | |
| 115 | foreach (QStringList entry, entries) { |
| 116 | QString dir(entry[0]); |
| 117 | QString changedate(entry[1]); |
| 118 | QFileInfo fi(dir); |
| 119 | |
| 120 | if (fi.exists()) { |
| 121 | if (QString::number(fi.lastModified().toTime_t()) != changedate) { |
| 122 | folders << dir; |
| 123 | qDebug() << "Collection dir changed: " << dir; |
| 124 | } |
| 125 | } else { |
| 126 | // this folder has been removed |
| 127 | folders << dir; |
| 128 | qDebug() << "Collection dir removed: " << dir; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (!folders.isEmpty()) |
| 133 | QFuture<void> future = QtConcurrent::run(this, &CollectionUpdater::asynchronScan, folders); |
| 134 | } |
| 135 | |
| 136 | void CollectionUpdater::scan() |
| 137 | { |
| 138 | p->incremental = false; |
| 139 | p->isStoped = false; |
| 140 | QFuture<void> future = QtConcurrent::run(this, &CollectionUpdater::asynchronScan, p->dirs); |
| 141 | } |
| 142 | |
| 143 | void CollectionUpdater::asynchronScan(QStringList dirs) |
| 144 | { |
| 145 | qDebug() << Q_FUNC_INFO << dirs.count() << "dirs" << endl; |
| 146 | |
| 147 | // avoid multiple runs |
| 148 | QMutexLocker locker(&p->mutex); |
| 149 | |
| 150 | Q_EMIT progressChanged(1); |
| 151 | |
| 152 | if (!p->incremental) |
| 153 | p->collectionDB->purgeDirCache(); |
| 154 | |
| 155 | QStringList entries; |
| 156 | int dirCount = dirs.count(); |
| 157 | |
| 158 | //iterate over all folders |
| 159 | for (int i = 0; i < dirCount; i++) { |
| 160 | Q_EMIT progressChanged(((i + 1) * 10) / dirCount); |
| 161 | readDir(dirs[i], entries); |
| 162 | } |
| 163 | |
| 164 | if (!entries.empty()) { |
| 165 | Q_EMIT progressChanged(10); |
| 166 | readTags(entries); |
| 167 | } |
| 168 | Q_EMIT progressChanged(100); |
| 169 | |
| 170 | if (!entries.empty()) |
| 171 | Q_EMIT changesDone(); |
| 172 | } |
| 173 | |
| 174 | void CollectionUpdater::readDir(const QString& dir, QStringList& entries) |
| 175 | { |
| 176 | //update dir statistics for rescanning purposes |
| 177 | QFileInfo fi(dir); |
| 178 | |
| 179 | if (fi.exists()) |
| 180 | p->collectionDB->updateDirStats(dir, (long)fi.lastModified().toTime_t()); |
| 181 | else { |
| 182 | if (p->incremental) { |
| 183 | p->collectionDB->removeSongsInDir(dir); |
| 184 | p->collectionDB->removeDirFromCollection(dir); |
| 185 | } |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | QDir rDir(dir); |
| 190 | rDir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotDot | QDir::NoDot | QDir::NoSymLinks | QDir::Readable); |
| 191 | QFileInfoList list = rDir.entryInfoList(); |
| 192 | |
| 193 | Q_FOREACH (const QFileInfo fi, list) { |
| 194 | if (fi.isDir()) { |
| 195 | if (!p->incremental || !p->collectionDB->isDirInCollection(fi.absoluteFilePath())) |
| 196 | readDir(fi.absoluteFilePath(), entries); |
| 197 | } else if (fi.isFile()) |
| 198 | entries << fi.absoluteFilePath(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void CollectionUpdater::readTags(const QStringList& entries) |
| 203 | { |
| 204 | qDebug() << Q_FUNC_INFO << " Start"; |
| 205 | |
| 206 | QUrl url; |
| 207 | p->collectionDB->createTables(true); |
| 208 | |
| 209 | int entriesCount = entries.count(); |
| 210 | for (int i = 0; i < entriesCount; i++) { |
| 211 | if (!(i % 20)) |
| 212 | Q_EMIT progressChanged(((i * 90) / entriesCount) + 10); |
| 213 | |
| 214 | url = QUrl::fromLocalFile(entries[i]); |
| 215 | |
| 216 | Track track(url); |
| 217 | |
| 218 | if (track.isValid()) { |
| 219 | |
| 220 | QString command = QString("INSERT INTO tags_temp " |
| 221 | "( url, dir, artist, title, album, genre, year, length, track ) " |
| 222 | "VALUES('%1','%2',%3,'%4',%5,%6,%7,%8,%9);") |
| 223 | .arg(p->collectionDB->escapeString(track.url().toLocalFile())) |
| 224 | .arg(p->collectionDB->escapeString(track.dirPath())) |
| 225 | .arg(p->collectionDB->escapeString(QString::number(p->collectionDB->getValueID("artist", track.artist(), true, !p->incremental)))) |
| 226 | .arg(p->collectionDB->escapeString(track.title())) |
| 227 | .arg(p->collectionDB->escapeString(QString::number(p->collectionDB->getValueID("album", track.album(), true, !p->incremental)))) |
| 228 | .arg(p->collectionDB->escapeString(QString::number(p->collectionDB->getValueID("genre", track.genre(), true, !p->incremental)))) |
| 229 | .arg(p->collectionDB->escapeString(QString::number(p->collectionDB->getValueID("year", track.year(), true, !p->incremental)))) |
| 230 | .arg(p->collectionDB->escapeString(QString::number(track.length()))) |
| 231 | .arg(p->collectionDB->escapeString(track.tracknumber())); |
| 232 | |
| 233 | p->collectionDB->executeSql(command); |
| 234 | |
| 235 | //stop the process? |
| 236 | if (p->isStoped) |
| 237 | i = entries.count(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | qDebug() << Q_FUNC_INFO << " Insert finish"; |
| 242 | |
| 243 | //update database only if not stoped |
| 244 | if (!p->isStoped) { |
| 245 | // let's lock the database (will block other threads) |
| 246 | p->collectionDB->executeSql("BEGIN TRANSACTION;"); |
| 247 | |
| 248 | // remove tables and recreate them (quicker than DELETE FROM) |
| 249 | if (!p->incremental) { |
| 250 | p->collectionDB->dropTables(); |
| 251 | p->collectionDB->createTables(); |
| 252 | } else { |
| 253 | // remove old entries from database, only |
| 254 | for (int i = 0; i < p->dirs.count(); i++) |
| 255 | p->collectionDB->removeSongsInDir(p->dirs[i]); |
| 256 | } |
| 257 | |
| 258 | // rename tables |
| 259 | p->collectionDB->moveTempTables(); |
| 260 | |
| 261 | // remove temp tables and unlock database |
| 262 | p->collectionDB->dropTables(true); |
| 263 | p->collectionDB->executeSql("END TRANSACTION;"); |
| 264 | } else { |
| 265 | |
| 266 | qDebug() << Q_FUNC_INFO << " Stop"; |
| 267 | } |
| 268 | |
| 269 | qDebug() << Q_FUNC_INFO << " End"; |
| 270 | } |