| 1 | /* |
| 2 | Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "djsession.h" |
| 19 | #include "dj.h" |
| 20 | #include "track.h" |
| 21 | |
| 22 | #if QT_VERSION >= 0x050000 |
| 23 | #include <QtConcurrent/QtConcurrent> |
| 24 | #else |
| 25 | #include <QtConcurrentRun> |
| 26 | #endif |
| 27 | #include <QtXml> |
| 28 | |
| 29 | struct DjSessionPrivate { |
| 30 | QMutex mutex1; |
| 31 | Dj* currentDj; |
| 32 | int minCount; |
| 33 | CollectionDB* database; |
| 34 | QList<Track*> playList1_Tracks; |
| 35 | QList<Track*> playList2_Tracks; |
| 36 | QPair<int, int> playList1_Info; |
| 37 | QPair<int, int> playList2_Info; |
| 38 | QStringList seenUrls; |
| 39 | bool isEnabledAutoDJCount; |
| 40 | }; |
| 41 | |
| 42 | DjSession::DjSession() |
| 43 | : p(new DjSessionPrivate) |
| 44 | { |
| 45 | p->database = new CollectionDB(); |
| 46 | p->minCount = 10; |
| 47 | p->currentDj = nullptr; |
| 48 | p->isEnabledAutoDJCount = false; |
| 49 | } |
| 50 | |
| 51 | DjSession::~DjSession() |
| 52 | { |
| 53 | delete p; |
| 54 | } |
| 55 | |
| 56 | void DjSession::setCurrentDj(Dj* dj) |
| 57 | { |
| 58 | // retire current Auto-DJ |
| 59 | if (p->currentDj) |
| 60 | disconnect(p->currentDj, SIGNAL(filterChanged(Filter*)), |
| 61 | this, SLOT(on_dj_filterChanged(Filter*))); |
| 62 | // hire new DJ |
| 63 | p->currentDj = dj; |
| 64 | connect(p->currentDj, SIGNAL(filterChanged(Filter*)), |
| 65 | this, SLOT(on_dj_filterChanged(Filter*))); |
| 66 | } |
| 67 | |
| 68 | Dj* DjSession::currentDj() |
| 69 | { |
| 70 | return p->currentDj; |
| 71 | } |
| 72 | |
| 73 | void DjSession::searchTracks() |
| 74 | { |
| 75 | // init qrand |
| 76 | QTime time = QTime::currentTime(); |
| 77 | qsrand((uint)time.msec()); |
| 78 | |
| 79 | // how many tracks are needed |
| 80 | p->mutex1.lock(); |
| 81 | int diffCount1 = p->minCount - p->playList1_Tracks.count(); |
| 82 | int diffCount2 = p->minCount - p->playList2_Tracks.count(); |
| 83 | int needed = (diffCount1 > 0) ? diffCount1 : 0; |
| 84 | needed = (diffCount2 > 0) ? needed + diffCount2 : needed; |
| 85 | |
| 86 | qDebug() << Q_FUNC_INFO << " need " << diffCount1 << " tracks left and " << diffCount2 << " tracks right "; |
| 87 | qDebug() << Q_FUNC_INFO << " needed together: " << needed; |
| 88 | |
| 89 | // retrieve new random tracks for both playlists |
| 90 | QList<Track*> tracks1; |
| 91 | QList<Track*> tracks2; |
| 92 | for (int i = 0; i < needed; i++) { |
| 93 | if (i % 2 == 0) { |
| 94 | if (diffCount1 > 0) { |
| 95 | tracks1.append(getRandomTrack()); |
| 96 | diffCount1--; |
| 97 | } else |
| 98 | tracks2.append(getRandomTrack()); |
| 99 | } else { |
| 100 | if (diffCount2 > 0) { |
| 101 | tracks2.append(getRandomTrack()); |
| 102 | diffCount2--; |
| 103 | } else |
| 104 | tracks1.append(getRandomTrack()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // new tracks available, trigger fill up of playlists |
| 109 | qDebug() << Q_FUNC_INFO << " provide " << tracks1.count() << " tracks left and " << tracks2.count() << " tracks right "; |
| 110 | |
| 111 | // emit if needed |
| 112 | if (tracks1.count() > 0) |
| 113 | emit foundTracks_Playlist1(tracks1); |
| 114 | if (tracks2.count() > 0) |
| 115 | emit foundTracks_Playlist2(tracks2); |
| 116 | |
| 117 | p->mutex1.unlock(); |
| 118 | } |
| 119 | |
| 120 | /*ToDo: |
| 121 | - Genre: Combobox with distinct all genres |
| 122 | */ |
| 123 | Track* DjSession::getRandomTrack() |
| 124 | { |
| 125 | if (p->currentDj == nullptr) |
| 126 | return nullptr; |
| 127 | |
| 128 | Track* track = nullptr; |
| 129 | int i = 0; |
| 130 | |
| 131 | Filter* f = p->currentDj->requestFilter(); |
| 132 | int maxCount = 0; |
| 133 | |
| 134 | do { |
| 135 | |
| 136 | track = new Track(p->database->getRandomEntry(f->path(), f->genre(), f->artist())); |
| 137 | |
| 138 | if (maxCount == 0) |
| 139 | maxCount = p->database->lastMaxCount(); |
| 140 | i++; |
| 141 | } while ((track->prettyLength() == "?" |
| 142 | || track->containIn(p->playList1_Tracks) |
| 143 | || track->containIn(p->playList2_Tracks) |
| 144 | || p->seenUrls.contains(track->url().toString())) |
| 145 | && i < maxCount * 3); |
| 146 | if (i >= maxCount * 3) |
| 147 | qDebug() << Q_FUNC_INFO << " no new track found."; |
| 148 | else |
| 149 | qDebug() << Q_FUNC_INFO << i << " iterations to found a new track " << i; |
| 150 | |
| 151 | f->setCount(maxCount); |
| 152 | f->setLength(p->database->lastLengthSum()); |
| 153 | p->seenUrls.append(track->url().toString()); |
| 154 | p->seenUrls.removeDuplicates(); |
| 155 | |
| 156 | track->setFlags(track->flags() | Track::isAutoDjSelection); |
| 157 | return track; |
| 158 | } |
| 159 | |
| 160 | void DjSession::updatePlaylists() |
| 161 | { |
| 162 | QFuture<void> future = QtConcurrent::run(this, &DjSession::searchTracks); |
| 163 | } |
| 164 | |
| 165 | void DjSession::forceTracks(QList<Track*> tracks) |
| 166 | { |
| 167 | int count1 = p->playList1_Tracks.count(); |
| 168 | int count2 = p->playList2_Tracks.count(); |
| 169 | |
| 170 | qDebug() << Q_FUNC_INFO << count1 << " tracks left and " << count2 << " tracks right "; |
| 171 | |
| 172 | // distribute tracks to both playlists |
| 173 | QList<Track*> tracks1; |
| 174 | QList<Track*> tracks2; |
| 175 | for (int i = 0; i < tracks.count(); i++) { |
| 176 | |
| 177 | if (tracks.at(i)->flags().testFlag(Track::isOnFirstPlayer)) { |
| 178 | tracks1.append(tracks.at(i)); |
| 179 | count1++; |
| 180 | } else if (tracks.at(i)->flags().testFlag(Track::isOnSecondPlayer)) { |
| 181 | |
| 182 | tracks2.append(tracks.at(i)); |
| 183 | count2++; |
| 184 | } else if (count1 < count2) { |
| 185 | |
| 186 | tracks1.append(tracks.at(i)); |
| 187 | count1++; |
| 188 | } else { |
| 189 | tracks2.append(tracks.at(i)); |
| 190 | count2++; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // new tracks available, trigger fill up of playlists |
| 195 | qDebug() << Q_FUNC_INFO << " provide " << tracks1.count() << " tracks left and " << tracks2.count() << " tracks right "; |
| 196 | |
| 197 | // emit if needed |
| 198 | if (tracks1.count() > 0) |
| 199 | emit foundTracks_Playlist1(tracks1); |
| 200 | if (tracks2.count() > 0) |
| 201 | emit foundTracks_Playlist2(tracks2); |
| 202 | } |
| 203 | |
| 204 | void DjSession::playDefaultList() |
| 205 | { |
| 206 | qDebug() << Q_FUNC_INFO; |
| 207 | |
| 208 | QList<QStringList> selectedTags; |
| 209 | |
| 210 | //Retrieve songs from database |
| 211 | selectedTags = p->database->selectPlaylistTracks("defaultKnowthelist"); |
| 212 | |
| 213 | QList<Track*> tracks; |
| 214 | |
| 215 | tracks.clear(); |
| 216 | |
| 217 | qDebug() << Q_FUNC_INFO << "Song count: " << selectedTags.count(); |
| 218 | |
| 219 | //add tags to this track list |
| 220 | foreach (QStringList tag, selectedTags) { |
| 221 | tracks.append(new Track(tag)); |
| 222 | } |
| 223 | |
| 224 | forceTracks(tracks); |
| 225 | } |
| 226 | |
| 227 | void DjSession::on_dj_filterChanged(Filter* f) |
| 228 | { |
| 229 | qDebug() << Q_FUNC_INFO; |
| 230 | int cnt = p->database->getCount(f->path(), f->genre(), f->artist()); |
| 231 | f->setLength(p->database->lastLengthSum()); |
| 232 | f->setCount(cnt); |
| 233 | summariseCount(); |
| 234 | } |
| 235 | |
| 236 | void DjSession::onResetStats() |
| 237 | { |
| 238 | p->database->resetSongCounter(); |
| 239 | } |
| 240 | |
| 241 | void DjSession::onTrackFinished(Track* track) |
| 242 | { |
| 243 | if (track && (p->isEnabledAutoDJCount || !track->flags().testFlag(Track::isAutoDjSelection))) { |
| 244 | p->database->incSongCounter(track->url().toLocalFile()); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void DjSession::onTrackPropertyChanged(Track* track) |
| 249 | { |
| 250 | if (track) { |
| 251 | p->database->setSongRate(track->url().toLocalFile(), track->rate()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void DjSession::onTracksChanged_Playlist1(QList<Track*> tracks) |
| 256 | { |
| 257 | p->playList1_Info.second = 0; |
| 258 | p->playList1_Tracks = tracks; |
| 259 | foreach (Track* track, p->playList1_Tracks) { |
| 260 | Track::Options flags = track->flags(); |
| 261 | flags |= Track::isOnFirstPlayer; |
| 262 | flags &= ~Track::isOnSecondPlayer; |
| 263 | track->setFlags(flags); |
| 264 | p->playList1_Info.second += track->length(); |
| 265 | } |
| 266 | p->playList1_Info.first = p->playList1_Tracks.count(); |
| 267 | Q_EMIT changed_Playlist1(p->playList1_Info); |
| 268 | } |
| 269 | |
| 270 | void DjSession::onTracksChanged_Playlist2(QList<Track*> tracks) |
| 271 | { |
| 272 | p->playList2_Info.second = 0; |
| 273 | p->playList2_Tracks = tracks; |
| 274 | foreach (Track* track, p->playList2_Tracks) { |
| 275 | Track::Options flags = track->flags(); |
| 276 | flags &= ~Track::isOnFirstPlayer; |
| 277 | flags |= Track::isOnSecondPlayer; |
| 278 | track->setFlags(flags); |
| 279 | p->playList2_Info.second += track->length(); |
| 280 | } |
| 281 | p->playList2_Info.first = p->playList2_Tracks.count(); |
| 282 | Q_EMIT changed_Playlist2(p->playList2_Info); |
| 283 | } |
| 284 | |
| 285 | void DjSession::storePlaylists(const QString& name, bool replace) |
| 286 | { |
| 287 | qDebug() << Q_FUNC_INFO << " Start"; |
| 288 | |
| 289 | QList<Track*> listToStore; |
| 290 | listToStore.append(p->playList1_Tracks); |
| 291 | listToStore.append(p->playList2_Tracks); |
| 292 | |
| 293 | if (replace) |
| 294 | p->database->executeSql(QString("DELETE FROM playlists WHERE name ='%1';") |
| 295 | .arg(p->database->escapeString(name))); |
| 296 | |
| 297 | int n = 0; |
| 298 | QList<Track*>::Iterator i = listToStore.begin(); |
| 299 | while (i != listToStore.end()) { |
| 300 | |
| 301 | QString command = QString("INSERT OR REPLACE INTO playlists " |
| 302 | "( url, name, length, flags, norder, changedate ) " |
| 303 | "VALUES('%1','%2', %3, %4, %5, strftime('%s', 'now'));") |
| 304 | .arg(p->database->escapeString((*i)->url().toLocalFile())) |
| 305 | .arg(p->database->escapeString(name)) |
| 306 | .arg((*i)->length()) |
| 307 | .arg((*i)->flags()) |
| 308 | .arg(n); |
| 309 | |
| 310 | p->database->executeSql(command); |
| 311 | i++; |
| 312 | n++; |
| 313 | } |
| 314 | |
| 315 | Q_EMIT savedPlaylists(); |
| 316 | qDebug() << Q_FUNC_INFO << " Insert finish"; |
| 317 | } |
| 318 | |
| 319 | void DjSession::summariseCount() |
| 320 | { |
| 321 | QString res; |
| 322 | int count = 0; |
| 323 | int length = 0; |
| 324 | QStringList artists; |
| 325 | int filterCount = p->currentDj->filters().count(); |
| 326 | for (int i = 0; i < filterCount; i++) { |
| 327 | Filter* f = p->currentDj->filters().at(i); |
| 328 | res += f->description(); |
| 329 | count += f->count(); |
| 330 | length += f->length(); |
| 331 | } |
| 332 | p->currentDj->setLengthTracks(length); |
| 333 | p->currentDj->setDescription(res); |
| 334 | p->currentDj->setCountTracks(count); |
| 335 | } |
| 336 | |
| 337 | bool DjSession::isEnabledAutoDJCount() |
| 338 | { |
| 339 | return p->isEnabledAutoDJCount; |
| 340 | } |
| 341 | |
| 342 | void DjSession::setIsEnabledAutoDJCount(bool value) |
| 343 | { |
| 344 | p->isEnabledAutoDJCount = value; |
| 345 | } |
| 346 | |
| 347 | int DjSession::minCount() |
| 348 | { |
| 349 | return p->minCount; |
| 350 | } |
| 351 | |
| 352 | void DjSession::setMinCount(int value) |
| 353 | { |
| 354 | p->minCount = value; |
| 355 | } |
| 356 | |
| 357 | // obsolate: keep this just in case we need an export function later |
| 358 | void DjSession::savePlaylists(const QString& filename) |
| 359 | { |
| 360 | qDebug() << Q_FUNC_INFO << "BEGIN "; |
| 361 | QFile file(filename); |
| 362 | |
| 363 | if (!file.open(QFile::WriteOnly)) |
| 364 | return; |
| 365 | |
| 366 | QList<Track*> listToSave; |
| 367 | listToSave.append(p->playList1_Tracks); |
| 368 | listToSave.append(p->playList2_Tracks); |
| 369 | |
| 370 | QDomDocument newdoc; |
| 371 | QDomElement playlistElem = newdoc.createElement("playlist"); |
| 372 | playlistElem.setAttribute("version", "1"); |
| 373 | playlistElem.setAttribute("xmlns", "http://xspf.org/ns/0/"); |
| 374 | newdoc.appendChild(playlistElem); |
| 375 | |
| 376 | QDomElement elem = newdoc.createElement("creator"); |
| 377 | QDomText t = newdoc.createTextNode("Knowthelist"); |
| 378 | elem.appendChild(t); |
| 379 | playlistElem.appendChild(elem); |
| 380 | |
| 381 | QDomElement listElem = newdoc.createElement("trackList"); |
| 382 | |
| 383 | QList<Track*>::Iterator i = listToSave.begin(); |
| 384 | while (i != listToSave.end()) { |
| 385 | |
| 386 | QDomElement trackElem = newdoc.createElement("track"); |
| 387 | |
| 388 | QDomElement extElem = newdoc.createElement("extension"); |
| 389 | |
| 390 | if ((*i)->flags().testFlag(Track::isAutoDjSelection)) |
| 391 | extElem.setAttribute("isAutoDjSelection", "1"); |
| 392 | if ((*i)->flags().testFlag(Track::isOnFirstPlayer)) |
| 393 | extElem.setAttribute("isOnFirstPlayer", "1"); |
| 394 | if ((*i)->flags().testFlag(Track::isOnSecondPlayer)) |
| 395 | extElem.setAttribute("isOnSecondPlayer", "1"); |
| 396 | extElem.setAttribute("Rating", (*i)->rate()); |
| 397 | |
| 398 | QStringList tag = (*i)->tagList(); |
| 399 | |
| 400 | for (int x = 0; x < tag.count(); ++x) { |
| 401 | if (x == 4 || x == 5) { |
| 402 | extElem.setAttribute(Track::tagNameList.at(x), tag.at(x)); |
| 403 | } else { |
| 404 | QDomElement elem = newdoc.createElement(Track::tagNameList.at(x)); |
| 405 | QDomText t = newdoc.createTextNode(tag.at(x)); |
| 406 | elem.appendChild(t); |
| 407 | trackElem.appendChild(elem); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | trackElem.appendChild(extElem); |
| 412 | listElem.appendChild(trackElem); |
| 413 | |
| 414 | i++; |
| 415 | } |
| 416 | |
| 417 | playlistElem.appendChild(listElem); |
| 418 | |
| 419 | QTextStream stream(&file); |
| 420 | stream.setCodec("UTF-8"); |
| 421 | stream << "<?xml version=\"1.0\" encoding=\"utf8\"?>\n"; |
| 422 | stream << newdoc.toString(); |
| 423 | file.close(); |
| 424 | |
| 425 | Q_EMIT savedPlaylists(); |
| 426 | qDebug() << Q_FUNC_INFO << "END "; |
| 427 | } |