| 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 | |
| 19 | #include "playlistbrowser.h" |
| 20 | #include "playlistwidget.h" |
| 21 | #include "collectiondb.h" |
| 22 | #include "track.h" |
| 23 | |
| 24 | #include <QtXml> |
| 25 | #include <QListWidget> |
| 26 | #include <QPushButton> |
| 27 | #include <QVBoxLayout> |
| 28 | #include <QInputDialog> |
| 29 | |
| 30 | class Track; |
| 31 | |
| 32 | class PlaylistBrowsertPrivate |
| 33 | { |
| 34 | public: |
| 35 | QListWidget* listPlaylists; |
| 36 | PlaylistWidget* currentPlaylist; |
| 37 | CollectionDB* database; |
| 38 | QString directory; |
| 39 | |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | PlaylistBrowser::PlaylistBrowser(QWidget *parent) : |
| 44 | QWidget(parent) |
| 45 | { |
| 46 | p = new PlaylistBrowsertPrivate; |
| 47 | |
| 48 | QPushButton* pushSave =new QPushButton(); |
| 49 | pushSave->setGeometry(QRect(1,1,60,25)); |
| 50 | pushSave->setMaximumWidth(60); |
| 51 | pushSave->setMinimumWidth(60); |
| 52 | pushSave->setText("+"); |
| 53 | QFont pushFont = pushSave->font(); |
| 54 | pushFont.setBold(true); |
| 55 | pushFont.setPointSize(pushFont.pointSize()+4); |
| 56 | pushSave->setFont(pushFont); |
| 57 | |
| 58 | pushSave->setStyleSheet("QPushButton { border: none; padding-top: -3px; margin-left: 8px;max-height: 20px; margin-right: 28px;}"); |
| 59 | pushSave->setToolTip(tr( "Add a new list and store current tracks" )); |
| 60 | connect( pushSave,SIGNAL(clicked()),this, SLOT(onPushSave())); |
| 61 | |
| 62 | QVBoxLayout *mainLayout = new QVBoxLayout; |
| 63 | QWidget *headWidget = new QWidget(this); |
| 64 | headWidget->setMaximumHeight(35); |
| 65 | headWidget->setMinimumHeight(25); |
| 66 | |
| 67 | QHBoxLayout *headWidgetLayout = new QHBoxLayout; |
| 68 | headWidgetLayout->setMargin(0); |
| 69 | headWidgetLayout->setSpacing(1); |
| 70 | headWidgetLayout->setAlignment(Qt::AlignRight); |
| 71 | |
| 72 | headWidgetLayout->addWidget(pushSave); |
| 73 | headWidget->setLayout(headWidgetLayout); |
| 74 | |
| 75 | p->listPlaylists = new QListWidget(); |
| 76 | p->listPlaylists->setAttribute(Qt::WA_MacShowFocusRect, false); |
| 77 | |
| 78 | p->database = new CollectionDB(); |
| 79 | p->database->executeSql( "PRAGMA synchronous = OFF;" ); |
| 80 | |
| 81 | updateLists(); |
| 82 | |
| 83 | headWidget->raise(); |
| 84 | mainLayout->addWidget(headWidget); |
| 85 | mainLayout->addWidget(p->listPlaylists); |
| 86 | |
| 87 | mainLayout->setMargin(0); |
| 88 | mainLayout->setSpacing(0); |
| 89 | |
| 90 | setMaximumWidth(400); |
| 91 | |
| 92 | p->directory = ""; |
| 93 | |
| 94 | setLayout(mainLayout); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | PlaylistBrowser::~PlaylistBrowser() |
| 99 | { |
| 100 | QSettings settings; |
| 101 | //settings.setValue("",p->); |
| 102 | delete p; |
| 103 | } |
| 104 | |
| 105 | void PlaylistBrowser::updateLists() |
| 106 | { |
| 107 | // Read config values |
| 108 | QSettings settings; |
| 109 | p->directory = settings.value("editPlaylistRoot","").toString(); |
| 110 | |
| 111 | // Insert dynamic lists |
| 112 | PlaylistWidget* list; |
| 113 | QListWidgetItem* itm; |
| 114 | p->listPlaylists->clear(); |
| 115 | |
| 116 | //Top Tracks List |
| 117 | list = new PlaylistWidget(p->listPlaylists); |
| 118 | list->setName(tr("Top Tracks")); |
| 119 | list->setObjectName("TopTracks"); |
| 120 | list->setDescription(tr("Most played tracks")); |
| 121 | list->setRemovable(false); |
| 122 | connect(list,SIGNAL(activated()),this,SLOT(loadDatabaseList())); |
| 123 | connect(list,SIGNAL(started()),this,SLOT(playDatabaseList())); |
| 124 | |
| 125 | itm = new QListWidgetItem(p->listPlaylists); |
| 126 | |
| 127 | itm->setSizeHint(QSize(0,70)); |
| 128 | p->listPlaylists->addItem(itm); |
| 129 | p->listPlaylists->setItemWidget(itm,list); |
| 130 | |
| 131 | //Last Tracks List |
| 132 | list = new PlaylistWidget(p->listPlaylists); |
| 133 | list->setName(tr("Last Tracks")); |
| 134 | list->setObjectName("LastTracks"); |
| 135 | list->setDescription(tr("Recently played tracks")); |
| 136 | list->setRemovable(false); |
| 137 | connect(list,SIGNAL(activated()),this,SLOT(loadDatabaseList())); |
| 138 | connect(list,SIGNAL(started()),this,SLOT(playDatabaseList())); |
| 139 | |
| 140 | itm = new QListWidgetItem(p->listPlaylists); |
| 141 | |
| 142 | itm->setSizeHint(QSize(0,70)); |
| 143 | p->listPlaylists->addItem(itm); |
| 144 | p->listPlaylists->setItemWidget(itm,list); |
| 145 | |
| 146 | //Favorites Tracks List |
| 147 | list = new PlaylistWidget(p->listPlaylists); |
| 148 | list->setName(tr("Favorites Tracks")); |
| 149 | list->setObjectName("FavoritesTracks"); |
| 150 | list->setDescription(tr("High rated tracks")); |
| 151 | list->setRemovable(false); |
| 152 | connect(list,SIGNAL(activated()),this,SLOT(loadDatabaseList())); |
| 153 | connect(list,SIGNAL(started()),this,SLOT(playDatabaseList())); |
| 154 | |
| 155 | itm = new QListWidgetItem(p->listPlaylists); |
| 156 | |
| 157 | itm->setSizeHint(QSize(0,70)); |
| 158 | p->listPlaylists->addItem(itm); |
| 159 | p->listPlaylists->setItemWidget(itm,list); |
| 160 | |
| 161 | // read stored lists |
| 162 | QList<QStringList> listData = p->database->selectPlaylistData(); |
| 163 | foreach ( QStringList data, listData) { |
| 164 | QString name = data[0]; |
| 165 | int count = data[1].toInt(); |
| 166 | int sum = data[2].toInt(); |
| 167 | QDateTime date(QDateTime::fromTime_t( data[3].toInt() )); |
| 168 | |
| 169 | qDebug() << Q_FUNC_INFO << "add playlist: " << name; |
| 170 | list = new PlaylistWidget(p->listPlaylists); |
| 171 | list->setName( name ); |
| 172 | list->setObjectName( name ); |
| 173 | list->setDescription( date.toString("yyyy-MM-dd") + " " |
| 174 | + QString::number(count) + " " + tr("tracks") + " " |
| 175 | + Track::prettyTime( sum ,true) + " " + tr("hours")); |
| 176 | connect(list,SIGNAL(activated()),this,SLOT(loadDatabaseList())); |
| 177 | connect(list,SIGNAL(started()),this,SLOT(playDatabaseList())); |
| 178 | connect(list,SIGNAL(deleted()),this,SLOT(removeDatabaseList())); |
| 179 | |
| 180 | itm = new QListWidgetItem(p->listPlaylists); |
| 181 | |
| 182 | itm->setSizeHint(QSize(0,70)); |
| 183 | p->listPlaylists->addItem(itm); |
| 184 | p->listPlaylists->setItemWidget(itm,list); |
| 185 | } |
| 186 | |
| 187 | //// read saved lists |
| 188 | //QDir rDir( p->directory ); |
| 189 | //rDir.setFilter(QDir::Files | QDir::NoDotDot | QDir::NoDot | QDir::Readable); |
| 190 | //QStringList filters; |
| 191 | // filters << "*.xspf"; |
| 192 | //rDir.setNameFilters(filters); |
| 193 | //QFileInfoList filelist = rDir.entryInfoList(); |
| 194 | |
| 195 | //Q_FOREACH (const QFileInfo fi, filelist) { |
| 196 | // if ( fi.isFile() ) { |
| 197 | // qDebug() << Q_FUNC_INFO << "add playlist: " << fi.fileName(); |
| 198 | // list = new PlaylistWidget(p->listPlaylists); |
| 199 | // list->setName(fi.fileName().replace(".xspf","")); |
| 200 | // list->setObjectName(fi.fileName()); |
| 201 | |
| 202 | // QPair<int,int> count = readFileValues( p->directory+"/"+fi.fileName()); |
| 203 | // list->setDescription( fi.lastModified().toString("yyyy-MM-dd") + " " |
| 204 | // + QString::number(count.first) + " " + tr("tracks") + " " |
| 205 | // + Track::prettyTime( count.second ,true) + " " + tr("hours")); |
| 206 | // connect(list,SIGNAL(activated()),this,SLOT(loadFileList())); |
| 207 | // connect(list,SIGNAL(started()),this,SLOT(playFileList())); |
| 208 | // connect(list,SIGNAL(deleted()),this,SLOT(removeFileList())); |
| 209 | |
| 210 | // itm = new QListWidgetItem(p->listPlaylists); |
| 211 | |
| 212 | // itm->setSizeHint(QSize(0,70)); |
| 213 | // p->listPlaylists->addItem(itm); |
| 214 | // p->listPlaylists->setItemWidget(itm,list); |
| 215 | // } |
| 216 | |
| 217 | //} |
| 218 | |
| 219 | |
| 220 | } |
| 221 | |
| 222 | void PlaylistBrowser::playDatabaseList() |
| 223 | { |
| 224 | qDebug() << Q_FUNC_INFO ; |
| 225 | |
| 226 | if(PlaylistWidget* item = qobject_cast<PlaylistWidget*>(QObject::sender())){ |
| 227 | |
| 228 | onSelectionChanged(item); |
| 229 | |
| 230 | emit selectionStarted(selectedTracks()); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void PlaylistBrowser::loadDatabaseList() |
| 235 | { |
| 236 | qDebug() << Q_FUNC_INFO ; |
| 237 | |
| 238 | if(PlaylistWidget* item = qobject_cast<PlaylistWidget*>(QObject::sender())){ |
| 239 | onSelectionChanged(item); |
| 240 | |
| 241 | emit selectionChanged(selectedTracks()); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void PlaylistBrowser::removeDatabaseList() |
| 247 | { |
| 248 | qDebug() << Q_FUNC_INFO ; |
| 249 | |
| 250 | if(PlaylistWidget* item = qobject_cast<PlaylistWidget*>(QObject::sender())){ |
| 251 | |
| 252 | QString senderName = item->objectName(); |
| 253 | |
| 254 | p->database->removePlaylist(senderName); |
| 255 | updateLists(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | QList<Track*> PlaylistBrowser::selectedTracks() |
| 260 | { |
| 261 | QString senderName = p->currentPlaylist->objectName(); |
| 262 | QList<QStringList> selectedTags; |
| 263 | |
| 264 | //Retrieve songs from database |
| 265 | if (senderName == "TopTracks") |
| 266 | selectedTags = p->database->selectHotTracks(); |
| 267 | else if (senderName == "FavoritesTracks") |
| 268 | selectedTags = p->database->selectFavoritesTracks(); |
| 269 | else if (senderName == "LastTracks") |
| 270 | selectedTags = p->database->selectLastTracks(); |
| 271 | else |
| 272 | selectedTags = p->database->selectPlaylistTracks(senderName); |
| 273 | |
| 274 | QList<Track*> tracks; |
| 275 | |
| 276 | tracks.clear(); |
| 277 | |
| 278 | qDebug() << Q_FUNC_INFO << "Song count: " << selectedTags.count(); |
| 279 | |
| 280 | //add tags to this track list |
| 281 | foreach ( QStringList tag, selectedTags) { |
| 282 | tracks.append( new Track(tag)); |
| 283 | } |
| 284 | |
| 285 | return tracks ; |
| 286 | } |
| 287 | |
| 288 | void PlaylistBrowser::onSelectionChanged(PlaylistWidget* item) |
| 289 | { |
| 290 | p->currentPlaylist = item; |
| 291 | for (int d=0;d<p->listPlaylists->count();d++) |
| 292 | ((PlaylistWidget*)p->listPlaylists->itemWidget(p->listPlaylists->item(d)))->deactivate(); |
| 293 | |
| 294 | item->activate(); |
| 295 | } |
| 296 | |
| 297 | void PlaylistBrowser::onPushSave() |
| 298 | { |
| 299 | |
| 300 | // QFileDialog dialog(this); |
| 301 | // dialog.setDefaultSuffix("xspf"); |
| 302 | // QString fileName = dialog.getSaveFileName(this, |
| 303 | // tr("Save Play List"), p->directory, |
| 304 | // tr("Playlists (*.xspf);;All Files (*)")); |
| 305 | |
| 306 | // // Bad workaround for Linux (Mac and Windows work with defaultSuffix) |
| 307 | // if (!fileName.endsWith(".xspf")) |
| 308 | // fileName += ".xspf"; |
| 309 | |
| 310 | // emit savePlaylists(fileName); |
| 311 | |
| 312 | QInputDialog* dialog = new QInputDialog(this, Qt::Dialog); |
| 313 | dialog->setWindowTitle( tr("Save Play List") ); |
| 314 | dialog->setLabelText( tr("Enter a name for the new list") ); |
| 315 | dialog->resize(350,100); |
| 316 | dialog->exec(); |
| 317 | QString listName = dialog->textValue(); |
| 318 | if ( !listName.isEmpty() ) |
| 319 | emit storePlaylists(listName); |
| 320 | } |
| 321 | |
| 322 | // obsolate: save list into files |
| 323 | |
| 324 | QList<Track*> PlaylistBrowser::readFileList(QString filename) |
| 325 | { |
| 326 | QFile file( filename ); |
| 327 | QList<Track*> tracks; |
| 328 | |
| 329 | tracks.clear(); |
| 330 | |
| 331 | if( file.open( QFile::ReadOnly ) ) |
| 332 | { |
| 333 | QTextStream stream( &file ); |
| 334 | |
| 335 | stream.setCodec( QTextCodec::codecForName("utf8") ); |
| 336 | QDomDocument d; |
| 337 | if( !d.setContent(stream.readAll()) ) { qDebug() << "Could not load XML\n"; return tracks; } |
| 338 | |
| 339 | QDomNode n = d.namedItem( "playlist" ).namedItem( "trackList" ).firstChild(); |
| 340 | |
| 341 | const QString TRACK( "track" ); //so we don't construct the QStrings all the time |
| 342 | const QString URL( "url" ); |
| 343 | const QString CURRENT( "current" ); |
| 344 | const QString NEXT( "next" ); |
| 345 | |
| 346 | |
| 347 | while( !n.isNull() ) |
| 348 | { |
| 349 | if( n.nodeName() == TRACK ) { |
| 350 | const QDomElement e = n.toElement(); |
| 351 | if( e.isNull() ) { |
| 352 | qDebug() << "Element '" << n.nodeName() << "' is null, skipping."; |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | //qDebug() << "Add from xml url='" << e.attribute( URL ); |
| 357 | Track *track = new Track(); |
| 358 | track->setUrl( QUrl::fromLocalFile(e.namedItem("location").firstChild().nodeValue())); |
| 359 | track->setArtist( e.namedItem("creator").firstChild().nodeValue()); |
| 360 | track->setTitle( e.namedItem("title").firstChild().nodeValue()); |
| 361 | track->setAlbum( e.namedItem("album").firstChild().nodeValue()); |
| 362 | track->setTracknumber( e.namedItem("trackNum").firstChild().nodeValue()); |
| 363 | track->setGenre( e.namedItem("extension").toElement().attribute( "genre" )); |
| 364 | track->setYear( e.namedItem("extension").toElement().attribute( "year" )); |
| 365 | track->setLength( e.namedItem("duration").firstChild().nodeValue()); |
| 366 | track->setCounter("0"); |
| 367 | if ( e.namedItem("extension").toElement().attribute( "isAutoDjSelection" ) =="1" ) |
| 368 | track->setFlags( track->flags() | Track::isAutoDjSelection ); |
| 369 | if ( e.namedItem("extension").toElement().attribute( "isOnFirstPlayer" ) =="1" ) |
| 370 | track->setFlags( track->flags() | Track::isOnFirstPlayer ); |
| 371 | if ( e.namedItem("extension").toElement().attribute( "isOnSecondPlayer" ) =="1" ) |
| 372 | track->setFlags( track->flags() | Track::isOnSecondPlayer ); |
| 373 | track->setRate( e.namedItem("extension").toElement().attribute("Rating").toInt() ); |
| 374 | |
| 375 | tracks.append(track); |
| 376 | |
| 377 | } |
| 378 | n = n.nextSibling(); |
| 379 | } |
| 380 | } |
| 381 | file.close(); |
| 382 | |
| 383 | qDebug() << "End " << Q_FUNC_INFO; |
| 384 | |
| 385 | return tracks; |
| 386 | } |
| 387 | |
| 388 | QPair<int,int> PlaylistBrowser::readFileValues(QString filename) |
| 389 | { |
| 390 | QFile file( filename ); |
| 391 | QPair<int,int> pair; |
| 392 | |
| 393 | int duration=0; |
| 394 | int count=0; |
| 395 | |
| 396 | if( file.open( QFile::ReadOnly ) ) |
| 397 | { |
| 398 | QTextStream stream( &file ); |
| 399 | |
| 400 | stream.setCodec( QTextCodec::codecForName("utf8") ); |
| 401 | QDomDocument d; |
| 402 | if( !d.setContent(stream.readAll()) ) { qDebug() << "Could not load XML\n"; return pair; } |
| 403 | |
| 404 | QDomNode n = d.namedItem( "playlist" ).namedItem( "trackList" ).firstChild(); |
| 405 | |
| 406 | const QString TRACK( "track" ); |
| 407 | |
| 408 | |
| 409 | while( !n.isNull() ) |
| 410 | { |
| 411 | if( n.nodeName() == TRACK ) { |
| 412 | const QDomElement e = n.toElement(); |
| 413 | if( e.isNull() ) { |
| 414 | qDebug() << "Element '" << n.nodeName() << "' is null, skipping."; |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | duration+= e.namedItem("duration").firstChild().nodeValue().toInt(); |
| 419 | count++; |
| 420 | |
| 421 | } |
| 422 | n = n.nextSibling(); |
| 423 | } |
| 424 | } |
| 425 | file.close(); |
| 426 | |
| 427 | pair.first = count; |
| 428 | pair.second = duration; |
| 429 | |
| 430 | qDebug() << "End " << Q_FUNC_INFO; |
| 431 | |
| 432 | return pair; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | void PlaylistBrowser::playFileList() |
| 437 | { |
| 438 | qDebug() << Q_FUNC_INFO ; |
| 439 | |
| 440 | if(PlaylistWidget* item = qobject_cast<PlaylistWidget*>(QObject::sender())){ |
| 441 | |
| 442 | onSelectionChanged(item); |
| 443 | |
| 444 | QString senderName = item->objectName(); |
| 445 | |
| 446 | emit selectionStarted( readFileList( p->directory+"/"+senderName) ); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void PlaylistBrowser::loadFileList() |
| 451 | { |
| 452 | qDebug() << Q_FUNC_INFO ; |
| 453 | |
| 454 | if(PlaylistWidget* item = qobject_cast<PlaylistWidget*>(QObject::sender())){ |
| 455 | |
| 456 | onSelectionChanged(item); |
| 457 | |
| 458 | QString senderName = item->objectName(); |
| 459 | |
| 460 | //Retrieve songs from file |
| 461 | emit selectionChanged( readFileList( p->directory+"/"+senderName) ); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void PlaylistBrowser::removeFileList() |
| 466 | { |
| 467 | qDebug() << Q_FUNC_INFO ; |
| 468 | |
| 469 | if(PlaylistWidget* item = qobject_cast<PlaylistWidget*>(QObject::sender())){ |
| 470 | |
| 471 | QString senderName = item->objectName(); |
| 472 | |
| 473 | QFile::remove( p->directory+"/"+senderName ); |
| 474 | updateLists(); |
| 475 | } |
| 476 | } |