2010-05-09 02:10:26 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "playlistbackend.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "core/database.h"
|
|
|
|
#include "core/scopedtransaction.h"
|
|
|
|
#include "core/song.h"
|
2010-11-27 20:11:36 +01:00
|
|
|
#include "library/librarybackend.h"
|
2010-08-03 20:57:17 +02:00
|
|
|
#include "library/sqlrow.h"
|
2011-01-19 16:36:40 +01:00
|
|
|
#include "playlist/songplaylistitem.h"
|
|
|
|
#include "playlistparsers/cueparser.h"
|
2010-11-20 21:30:21 +01:00
|
|
|
#include "smartplaylists/generator.h"
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2011-01-19 16:36:40 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QHash>
|
|
|
|
#include <QMutexLocker>
|
2010-05-09 02:10:26 +02:00
|
|
|
#include <QSqlQuery>
|
2010-08-03 20:57:17 +02:00
|
|
|
#include <QtConcurrentMap>
|
|
|
|
#include <QtDebug>
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2011-01-19 16:36:40 +01:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2010-11-20 21:30:21 +01:00
|
|
|
using smart_playlists::GeneratorPtr;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
using boost::shared_ptr;
|
|
|
|
|
2011-04-27 00:06:58 +02:00
|
|
|
const int PlaylistBackend::kSongTableJoins = 4;
|
|
|
|
|
2010-06-02 17:58:07 +02:00
|
|
|
PlaylistBackend::PlaylistBackend(QObject* parent)
|
2011-01-19 16:36:40 +01:00
|
|
|
: QObject(parent),
|
|
|
|
library_(NULL)
|
2010-05-09 02:10:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-19 16:36:40 +01:00
|
|
|
void PlaylistBackend::SetLibrary(LibraryBackend* library) {
|
|
|
|
library_ = library;
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
PlaylistBackend::PlaylistList PlaylistBackend::GetAllPlaylists() {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-20 23:21:55 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
PlaylistList ret;
|
|
|
|
|
2010-11-27 20:11:36 +01:00
|
|
|
QSqlQuery q("SELECT ROWID, name, last_played, dynamic_playlist_type,"
|
|
|
|
" dynamic_playlist_data, dynamic_playlist_backend"
|
2011-02-10 21:50:57 +01:00
|
|
|
" FROM playlists"
|
|
|
|
" ORDER BY ui_order", db);
|
2010-05-20 23:21:55 +02:00
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q))
|
2010-05-20 23:21:55 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
while (q.next()) {
|
|
|
|
Playlist p;
|
|
|
|
p.id = q.value(0).toInt();
|
|
|
|
p.name = q.value(1).toString();
|
2010-05-21 00:30:55 +02:00
|
|
|
p.last_played = q.value(2).toInt();
|
2010-11-27 20:11:36 +01:00
|
|
|
p.dynamic_type = q.value(3).toString();
|
|
|
|
p.dynamic_data = q.value(4).toByteArray();
|
|
|
|
p.dynamic_backend = q.value(5).toString();
|
2010-05-20 23:21:55 +02:00
|
|
|
ret << p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 00:30:55 +02:00
|
|
|
PlaylistBackend::Playlist PlaylistBackend::GetPlaylist(int id) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-21 00:30:55 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
2010-11-20 21:30:21 +01:00
|
|
|
QSqlQuery q("SELECT ROWID, name, last_played, dynamic_playlist_type,"
|
2010-11-27 20:11:36 +01:00
|
|
|
" dynamic_playlist_data, dynamic_playlist_backend"
|
2010-11-20 21:30:21 +01:00
|
|
|
" FROM playlists"
|
2010-05-21 00:30:55 +02:00
|
|
|
" WHERE ROWID=:id", db);
|
|
|
|
q.bindValue(":id", id);
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q))
|
2010-05-21 00:30:55 +02:00
|
|
|
return Playlist();
|
|
|
|
|
|
|
|
q.next();
|
|
|
|
|
|
|
|
Playlist p;
|
|
|
|
p.id = q.value(0).toInt();
|
|
|
|
p.name = q.value(1).toString();
|
|
|
|
p.last_played = q.value(2).toInt();
|
2010-11-20 21:30:21 +01:00
|
|
|
p.dynamic_type = q.value(3).toString();
|
|
|
|
p.dynamic_data = q.value(4).toByteArray();
|
2010-11-27 20:11:36 +01:00
|
|
|
p.dynamic_backend = q.value(5).toString();
|
2010-05-21 00:30:55 +02:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-10-24 17:34:47 +02:00
|
|
|
QFuture<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-09 02:10:26 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
2010-12-28 16:36:01 +01:00
|
|
|
QString query = "SELECT songs.ROWID, " + Song::JoinSpec("songs") + ","
|
|
|
|
" magnatune_songs.ROWID, " + Song::JoinSpec("magnatune_songs") + ","
|
|
|
|
" jamendo_songs.ROWID, " + Song::JoinSpec("jamendo_songs") + ","
|
2011-04-27 00:06:58 +02:00
|
|
|
" p.ROWID, " + Song::JoinSpec("p") + ","
|
|
|
|
" p.type, p.radio_service"
|
2010-12-28 16:36:01 +01:00
|
|
|
" FROM playlist_items AS p"
|
|
|
|
" LEFT JOIN songs"
|
|
|
|
" ON p.library_id = songs.ROWID"
|
|
|
|
" LEFT JOIN magnatune_songs"
|
|
|
|
" ON p.library_id = magnatune_songs.ROWID"
|
|
|
|
" LEFT JOIN jamendo.songs AS jamendo_songs"
|
|
|
|
" ON p.library_id = jamendo_songs.ROWID"
|
|
|
|
" WHERE p.playlist = :playlist";
|
|
|
|
QSqlQuery q(query, db);
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
q.bindValue(":playlist", playlist);
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q))
|
2010-10-24 17:34:47 +02:00
|
|
|
return QFuture<PlaylistItemPtr>();
|
2010-08-03 20:57:17 +02:00
|
|
|
|
|
|
|
QList<SqlRow> rows;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
while (q.next()) {
|
2010-08-03 20:57:17 +02:00
|
|
|
rows << SqlRow(q);
|
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2011-01-19 16:36:40 +01:00
|
|
|
// it's probable that we'll have a few songs associated with the
|
|
|
|
// same CUE so we're caching results of parsing CUEs
|
|
|
|
boost::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
|
|
|
|
return QtConcurrent::mapped(rows, boost::bind(&PlaylistBackend::NewSongFromQuery, this, _1, state_ptr));
|
2010-08-03 20:57:17 +02:00
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2011-01-19 16:36:40 +01:00
|
|
|
PlaylistItemPtr PlaylistBackend::NewSongFromQuery(const SqlRow& row, boost::shared_ptr<NewSongFromQueryState> state) {
|
2010-11-25 23:04:23 +01:00
|
|
|
// The song tables get joined first, plus one each for the song ROWIDs
|
2011-04-27 00:06:58 +02:00
|
|
|
const int playlist_row = (Song::kColumns.count() + 1) * kSongTableJoins;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2011-01-19 16:36:40 +01:00
|
|
|
PlaylistItemPtr item(PlaylistItem::NewFromType(row.value(playlist_row).toString()));
|
2010-08-03 20:57:17 +02:00
|
|
|
if (item) {
|
|
|
|
item->InitFromQuery(row);
|
2011-01-19 16:36:40 +01:00
|
|
|
return RestoreCueData(item, state);
|
|
|
|
} else {
|
|
|
|
return item;
|
2010-08-03 20:57:17 +02:00
|
|
|
}
|
2011-01-19 16:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If song had a CUE and the CUE still exists, the metadata from it will
|
|
|
|
// be applied here.
|
|
|
|
PlaylistItemPtr PlaylistBackend::RestoreCueData(PlaylistItemPtr item, boost::shared_ptr<NewSongFromQueryState> state) {
|
|
|
|
// we need library to run a CueParser; also, this method applies only to
|
|
|
|
// file-type PlaylistItems
|
|
|
|
if(!library_ || item->type() != "File") {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
CueParser cue_parser(library_);
|
|
|
|
|
|
|
|
Song song = item->Metadata();
|
|
|
|
// we're only interested in .cue songs here
|
|
|
|
if(!song.has_cue()) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString cue_path = song.cue_path();
|
|
|
|
// if .cue was deleted - reload the song
|
|
|
|
if(!QFile::exists(cue_path)) {
|
|
|
|
item->Reload();
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
SongList song_list;
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&state->mutex_);
|
|
|
|
|
|
|
|
if(!state->cached_cues_.contains(cue_path)) {
|
|
|
|
QFile cue(cue_path);
|
|
|
|
cue.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
song_list = cue_parser.Load(&cue, cue_path, QDir(cue_path.section('/', 0, -2)));
|
|
|
|
state->cached_cues_[cue_path] = song_list;
|
|
|
|
} else {
|
|
|
|
song_list = state->cached_cues_[cue_path];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach(const Song& from_list, song_list) {
|
|
|
|
if(from_list.filename() == song.filename() &&
|
2011-02-13 19:34:30 +01:00
|
|
|
from_list.beginning_nanosec() == song.beginning_nanosec()) {
|
2011-01-19 16:36:40 +01:00
|
|
|
// we found a matching section; replace the input
|
|
|
|
// item with a new one containing CUE metadata
|
|
|
|
return PlaylistItemPtr(new SongPlaylistItem(from_list));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// there's no such section in the related .cue -> reload the song
|
|
|
|
item->Reload();
|
2010-08-03 20:57:17 +02:00
|
|
|
return item;
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 00:30:55 +02:00
|
|
|
void PlaylistBackend::SavePlaylistAsync(int playlist, const PlaylistItemList &items,
|
2010-11-20 21:30:21 +01:00
|
|
|
int last_played, GeneratorPtr dynamic) {
|
2010-05-09 02:10:26 +02:00
|
|
|
metaObject()->invokeMethod(this, "SavePlaylist", Qt::QueuedConnection,
|
|
|
|
Q_ARG(int, playlist),
|
2010-05-21 00:30:55 +02:00
|
|
|
Q_ARG(PlaylistItemList, items),
|
2010-11-20 21:30:21 +01:00
|
|
|
Q_ARG(int, last_played),
|
|
|
|
Q_ARG(smart_playlists::GeneratorPtr, dynamic));
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 00:30:55 +02:00
|
|
|
void PlaylistBackend::SavePlaylist(int playlist, const PlaylistItemList& items,
|
2010-11-20 21:30:21 +01:00
|
|
|
int last_played, GeneratorPtr dynamic) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-09 02:10:26 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
QSqlQuery clear("DELETE FROM playlist_items WHERE playlist = :playlist", db);
|
|
|
|
QSqlQuery insert("INSERT INTO playlist_items"
|
2011-04-27 00:06:58 +02:00
|
|
|
" (playlist, type, library_id, radio_service, " +
|
|
|
|
Song::kColumnSpec + ")"
|
|
|
|
" VALUES (:playlist, :type, :library_id, :radio_service, " +
|
|
|
|
Song::kBindSpec + ")", db);
|
2010-11-20 21:30:21 +01:00
|
|
|
QSqlQuery update("UPDATE playlists SET "
|
|
|
|
" last_played=:last_played,"
|
|
|
|
" dynamic_playlist_type=:dynamic_type,"
|
2010-11-27 20:11:36 +01:00
|
|
|
" dynamic_playlist_data=:dynamic_data,"
|
|
|
|
" dynamic_playlist_backend=:dynamic_backend"
|
2010-05-21 00:30:55 +02:00
|
|
|
" WHERE ROWID=:playlist", db);
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
ScopedTransaction transaction(&db);
|
|
|
|
|
|
|
|
// Clear the existing items in the playlist
|
|
|
|
clear.bindValue(":playlist", playlist);
|
|
|
|
clear.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(clear))
|
2010-05-09 02:10:26 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Save the new ones
|
2010-10-24 17:34:47 +02:00
|
|
|
foreach (PlaylistItemPtr item, items) {
|
2011-04-27 00:06:58 +02:00
|
|
|
insert.bindValue(":playlist", playlist);
|
2010-05-09 02:10:26 +02:00
|
|
|
item->BindToQuery(&insert);
|
|
|
|
|
|
|
|
insert.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
db_->CheckErrors(insert);
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 00:30:55 +02:00
|
|
|
// Update the last played track number
|
|
|
|
update.bindValue(":last_played", last_played);
|
2010-11-20 21:30:21 +01:00
|
|
|
if (dynamic) {
|
|
|
|
update.bindValue(":dynamic_type", dynamic->type());
|
|
|
|
update.bindValue(":dynamic_data", dynamic->Save());
|
2010-11-27 20:11:36 +01:00
|
|
|
update.bindValue(":dynamic_backend", dynamic->library()->songs_table());
|
2010-11-20 21:30:21 +01:00
|
|
|
} else {
|
|
|
|
update.bindValue(":dynamic_type", QString());
|
|
|
|
update.bindValue(":dynamic_data", QByteArray());
|
2010-11-27 20:11:36 +01:00
|
|
|
update.bindValue(":dynamic_backend", QString());
|
2010-11-20 21:30:21 +01:00
|
|
|
}
|
|
|
|
update.bindValue(":playlist", playlist);
|
2010-05-21 00:30:55 +02:00
|
|
|
update.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(update))
|
2010-05-21 00:30:55 +02:00
|
|
|
return;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
transaction.Commit();
|
|
|
|
}
|
2010-05-20 23:21:55 +02:00
|
|
|
|
|
|
|
int PlaylistBackend::CreatePlaylist(const QString &name) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-20 23:21:55 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
QSqlQuery q("INSERT INTO playlists (name) VALUES (:name)", db);
|
|
|
|
q.bindValue(":name", name);
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q))
|
2010-05-20 23:21:55 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return q.lastInsertId().toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistBackend::RemovePlaylist(int id) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-20 23:21:55 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
QSqlQuery delete_playlist("DELETE FROM playlists WHERE ROWID=:id", db);
|
|
|
|
QSqlQuery delete_items("DELETE FROM playlist_items WHERE playlist=:id", db);
|
|
|
|
|
|
|
|
delete_playlist.bindValue(":id", id);
|
|
|
|
delete_items.bindValue(":id", id);
|
|
|
|
|
|
|
|
ScopedTransaction transaction(&db);
|
|
|
|
|
|
|
|
delete_playlist.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(delete_playlist))
|
2010-05-20 23:21:55 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
delete_items.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(delete_items))
|
2010-05-20 23:21:55 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistBackend::RenamePlaylist(int id, const QString &new_name) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-20 23:21:55 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
QSqlQuery q("UPDATE playlists SET name=:name WHERE ROWID=:id", db);
|
|
|
|
q.bindValue(":name", new_name);
|
|
|
|
q.bindValue(":id", id);
|
|
|
|
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
db_->CheckErrors(q);
|
2010-05-20 23:21:55 +02:00
|
|
|
}
|
2010-05-21 12:37:24 +02:00
|
|
|
|
|
|
|
void PlaylistBackend::SetPlaylistOrder(const QList<int>& ids) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-21 12:37:24 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
QSqlQuery q("UPDATE playlists SET ui_order=:index WHERE ROWID=:id", db);
|
|
|
|
|
|
|
|
ScopedTransaction transaction(&db);
|
|
|
|
|
|
|
|
for (int i=0 ; i<ids.count() ; ++i) {
|
|
|
|
q.bindValue(":index", i);
|
|
|
|
q.bindValue(":id", ids[i]);
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q))
|
2010-05-21 12:37:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|