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"
|
2010-11-20 21:30:21 +01:00
|
|
|
#include "smartplaylists/generator.h"
|
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
|
|
|
|
2010-11-20 21:30:21 +01:00
|
|
|
using smart_playlists::GeneratorPtr;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
using boost::shared_ptr;
|
|
|
|
|
2010-06-02 17:58:07 +02:00
|
|
|
PlaylistBackend::PlaylistBackend(QObject* parent)
|
|
|
|
: QObject(parent)
|
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"
|
|
|
|
" FROM playlists", db);
|
2010-05-20 23:21:55 +02:00
|
|
|
q.exec();
|
|
|
|
if (db_->CheckErrors(q.lastError()))
|
|
|
|
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();
|
|
|
|
if (db_->CheckErrors(q.lastError()))
|
|
|
|
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-05-10 16:19:43 +02:00
|
|
|
QSqlQuery q("SELECT songs.ROWID, " + Song::JoinSpec("songs") + ","
|
|
|
|
" magnatune_songs.ROWID, " + Song::JoinSpec("magnatune_songs") + ","
|
2010-11-25 23:04:23 +01:00
|
|
|
" jamendo_songs.ROWID, " + Song::JoinSpec("jamendo_songs") + ","
|
2010-05-09 02:10:26 +02:00
|
|
|
" p.type, p.url, p.title, p.artist, p.album, p.length,"
|
|
|
|
" p.radio_service"
|
|
|
|
" FROM playlist_items AS p"
|
|
|
|
" LEFT JOIN songs"
|
|
|
|
" ON p.library_id = songs.ROWID"
|
2010-05-10 16:19:43 +02:00
|
|
|
" LEFT JOIN magnatune_songs"
|
|
|
|
" ON p.library_id = magnatune_songs.ROWID"
|
2010-11-27 17:14:09 +01:00
|
|
|
" LEFT JOIN jamendo.songs AS jamendo_songs"
|
2010-11-25 23:04:23 +01:00
|
|
|
" ON p.library_id = jamendo_songs.ROWID"
|
2010-05-09 02:10:26 +02:00
|
|
|
" WHERE p.playlist = :playlist", db);
|
|
|
|
q.bindValue(":playlist", playlist);
|
|
|
|
q.exec();
|
|
|
|
if (db_->CheckErrors(q.lastError()))
|
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
|
|
|
|
2010-08-03 20:57:17 +02:00
|
|
|
return QtConcurrent::mapped(rows, &PlaylistBackend::NewSongFromQuery);
|
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-10-24 17:34:47 +02:00
|
|
|
PlaylistItemPtr PlaylistBackend::NewSongFromQuery(const SqlRow& row) {
|
2010-11-25 23:04:23 +01:00
|
|
|
// The song tables get joined first, plus one each for the song ROWIDs
|
|
|
|
const int playlist_row = (Song::kColumns.count() + 1) * 3;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-10-24 17:34:47 +02:00
|
|
|
PlaylistItemPtr item(
|
2010-08-03 20:57:17 +02:00
|
|
|
PlaylistItem::NewFromType(row.value(playlist_row).toString()));
|
|
|
|
if (item) {
|
|
|
|
item->InitFromQuery(row);
|
|
|
|
}
|
|
|
|
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"
|
|
|
|
" (playlist, type, library_id, url, title, artist, album,"
|
|
|
|
" length, radio_service)"
|
|
|
|
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", 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();
|
|
|
|
if (db_->CheckErrors(clear.lastError()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Save the new ones
|
2010-10-24 17:34:47 +02:00
|
|
|
foreach (PlaylistItemPtr item, items) {
|
2010-05-09 02:10:26 +02:00
|
|
|
insert.bindValue(0, playlist);
|
|
|
|
item->BindToQuery(&insert);
|
|
|
|
|
|
|
|
insert.exec();
|
|
|
|
db_->CheckErrors(insert.lastError());
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
if (db_->CheckErrors(update.lastError()))
|
|
|
|
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();
|
|
|
|
if (db_->CheckErrors(q.lastError()))
|
|
|
|
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();
|
|
|
|
if (db_->CheckErrors(delete_playlist.lastError()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete_items.exec();
|
|
|
|
if (db_->CheckErrors(delete_items.lastError()))
|
|
|
|
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();
|
|
|
|
db_->CheckErrors(q.lastError());
|
|
|
|
}
|
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();
|
|
|
|
if (db_->CheckErrors(q.lastError()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|