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"
|
2014-02-06 14:48:00 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QHash>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "core/database.h"
|
2014-03-18 16:14:12 +01:00
|
|
|
#include "core/logging.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#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
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
using std::placeholders::_1;
|
|
|
|
using std::shared_ptr;
|
2011-01-19 16:36:40 +01:00
|
|
|
|
2010-11-20 21:30:21 +01:00
|
|
|
using smart_playlists::GeneratorPtr;
|
|
|
|
|
2011-04-27 00:06:58 +02:00
|
|
|
const int PlaylistBackend::kSongTableJoins = 4;
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
PlaylistBackend::PlaylistBackend(Application* app, QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent), app_(app), db_(app_->database()) {}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2013-05-11 23:59:51 +02:00
|
|
|
PlaylistBackend::PlaylistList PlaylistBackend::GetAllPlaylists() {
|
|
|
|
return GetPlaylists(GetPlaylists_All);
|
|
|
|
}
|
|
|
|
|
2012-10-31 07:04:22 +01:00
|
|
|
PlaylistBackend::PlaylistList PlaylistBackend::GetAllOpenPlaylists() {
|
2013-05-11 23:17:54 +02:00
|
|
|
return GetPlaylists(GetPlaylists_OpenInUi);
|
2012-10-31 07:04:22 +01:00
|
|
|
}
|
|
|
|
|
2013-05-11 23:17:54 +02:00
|
|
|
PlaylistBackend::PlaylistList PlaylistBackend::GetAllFavoritePlaylists() {
|
|
|
|
return GetPlaylists(GetPlaylists_Favorite);
|
2012-10-31 07:04:22 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
PlaylistBackend::PlaylistList PlaylistBackend::GetPlaylists(
|
|
|
|
GetPlaylistsFlags flags) {
|
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;
|
|
|
|
|
2013-05-11 23:59:51 +02:00
|
|
|
QStringList condition_list;
|
2013-05-11 23:17:54 +02:00
|
|
|
if (flags & GetPlaylists_OpenInUi) {
|
2013-05-11 23:59:51 +02:00
|
|
|
condition_list << "ui_order != -1";
|
2013-05-11 23:17:54 +02:00
|
|
|
}
|
|
|
|
if (flags & GetPlaylists_Favorite) {
|
2013-05-11 23:59:51 +02:00
|
|
|
condition_list << "is_favorite != 0";
|
2013-05-11 23:17:54 +02:00
|
|
|
}
|
2013-05-11 23:59:51 +02:00
|
|
|
QString condition;
|
|
|
|
if (!condition_list.isEmpty()) {
|
|
|
|
condition = " WHERE " + condition_list.join(" OR ");
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QSqlQuery q(
|
|
|
|
"SELECT ROWID, name, last_played, dynamic_playlist_type,"
|
|
|
|
" dynamic_playlist_data, dynamic_playlist_backend,"
|
|
|
|
" special_type, ui_path, is_favorite"
|
|
|
|
" FROM playlists"
|
|
|
|
" " +
|
|
|
|
condition + " ORDER BY ui_order",
|
|
|
|
db);
|
2010-05-20 23:21:55 +02:00
|
|
|
q.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(q)) return ret;
|
2010-05-20 23:21:55 +02:00
|
|
|
|
|
|
|
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();
|
2011-04-28 19:50:45 +02:00
|
|
|
p.special_type = q.value(6).toString();
|
2012-10-31 07:04:22 +01:00
|
|
|
p.ui_path = q.value(7).toString();
|
2013-05-11 23:17:54 +02:00
|
|
|
p.favorite = q.value(8).toBool();
|
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());
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QSqlQuery q(
|
|
|
|
"SELECT ROWID, name, last_played, dynamic_playlist_type,"
|
|
|
|
" dynamic_playlist_data, dynamic_playlist_backend,"
|
|
|
|
" special_type, ui_path, is_favorite"
|
|
|
|
" FROM playlists"
|
|
|
|
" WHERE ROWID=:id",
|
|
|
|
db);
|
2010-05-21 00:30:55 +02:00
|
|
|
q.bindValue(":id", id);
|
|
|
|
q.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(q)) return Playlist();
|
2010-05-21 00:30:55 +02:00
|
|
|
|
|
|
|
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();
|
2011-04-28 19:50:45 +02:00
|
|
|
p.special_type = q.value(6).toString();
|
2013-05-11 23:17:54 +02:00
|
|
|
p.ui_path = q.value(7).toString();
|
|
|
|
p.favorite = q.value(8).toBool();
|
2010-05-21 00:30:55 +02:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-12-07 02:41:31 +01:00
|
|
|
QSqlQuery PlaylistBackend::GetPlaylistRows(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());
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString query = "SELECT songs.ROWID, " + Song::JoinSpec("songs") +
|
|
|
|
","
|
|
|
|
" magnatune_songs.ROWID, " +
|
|
|
|
Song::JoinSpec("magnatune_songs") +
|
|
|
|
","
|
|
|
|
" jamendo_songs.ROWID, " +
|
|
|
|
Song::JoinSpec("jamendo_songs") +
|
|
|
|
","
|
|
|
|
" p.ROWID, " +
|
|
|
|
Song::JoinSpec("p") +
|
|
|
|
","
|
2011-04-27 00:06:58 +02:00
|
|
|
" 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";
|
2014-12-07 02:41:31 +01:00
|
|
|
QSqlQuery q(db);
|
|
|
|
// Forward iterations only may be faster
|
|
|
|
q.setForwardOnly(true);
|
|
|
|
q.prepare(query);
|
2010-05-09 02:10:26 +02:00
|
|
|
q.bindValue(":playlist", playlist);
|
|
|
|
q.exec();
|
|
|
|
|
2014-12-07 02:41:31 +01:00
|
|
|
return q;
|
2013-05-23 20:09:35 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 02:41:31 +01:00
|
|
|
QList<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) {
|
|
|
|
QSqlQuery q = GetPlaylistRows(playlist);
|
|
|
|
// Note that as this only accesses the query, not the db, we don't need the
|
|
|
|
// mutex.
|
|
|
|
if (db_->CheckErrors(q)) return QList<PlaylistItemPtr>();
|
2013-05-23 20:09:35 +02:00
|
|
|
|
|
|
|
// it's probable that we'll have a few songs associated with the
|
|
|
|
// same CUE so we're caching results of parsing CUEs
|
2014-02-06 14:48:00 +01:00
|
|
|
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
|
2014-12-07 02:41:31 +01:00
|
|
|
QList<PlaylistItemPtr> playlistitems;
|
|
|
|
while (q.next()) {
|
|
|
|
playlistitems << NewPlaylistItemFromQuery(SqlRow(q), state_ptr);
|
|
|
|
}
|
|
|
|
return playlistitems;
|
2013-05-23 20:09:35 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 02:41:31 +01:00
|
|
|
QList<Song> PlaylistBackend::GetPlaylistSongs(int playlist) {
|
|
|
|
QSqlQuery q = GetPlaylistRows(playlist);
|
|
|
|
// Note that as this only accesses the query, not the db, we don't need the
|
|
|
|
// mutex.
|
|
|
|
if (db_->CheckErrors(q)) return QList<Song>();
|
2013-05-23 20:09:35 +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
|
2014-02-06 14:48:00 +01:00
|
|
|
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
|
2014-12-07 02:41:31 +01:00
|
|
|
QList<Song> songs;
|
|
|
|
while (q.next()) {
|
|
|
|
songs << NewSongFromQuery(SqlRow(q), state_ptr);
|
|
|
|
}
|
|
|
|
return songs;
|
2010-08-03 20:57:17 +02:00
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
PlaylistItemPtr PlaylistBackend::NewPlaylistItemFromQuery(
|
|
|
|
const SqlRow& row, std::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
|
|
|
|
2014-02-07 16:34:20 +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
|
|
|
}
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
Song PlaylistBackend::NewSongFromQuery(
|
|
|
|
const SqlRow& row, std::shared_ptr<NewSongFromQueryState> state) {
|
2013-05-23 20:09:35 +02:00
|
|
|
return NewPlaylistItemFromQuery(row, state)->Metadata();
|
|
|
|
}
|
|
|
|
|
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.
|
2014-02-06 14:48:00 +01:00
|
|
|
PlaylistItemPtr PlaylistBackend::RestoreCueData(
|
|
|
|
PlaylistItemPtr item, std::shared_ptr<NewSongFromQueryState> state) {
|
2011-01-19 16:36:40 +01:00
|
|
|
// we need library to run a CueParser; also, this method applies only to
|
|
|
|
// file-type PlaylistItems
|
2014-02-07 16:34:20 +01:00
|
|
|
if (item->type() != "File") {
|
2011-01-19 16:36:40 +01:00
|
|
|
return item;
|
|
|
|
}
|
2012-02-12 14:41:50 +01:00
|
|
|
CueParser cue_parser(app_->library_backend());
|
2011-01-19 16:36:40 +01:00
|
|
|
|
|
|
|
Song song = item->Metadata();
|
|
|
|
// we're only interested in .cue songs here
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!song.has_cue()) {
|
2011-01-19 16:36:40 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString cue_path = song.cue_path();
|
|
|
|
// if .cue was deleted - reload the song
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!QFile::exists(cue_path)) {
|
2011-01-19 16:36:40 +01:00
|
|
|
item->Reload();
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
SongList song_list;
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&state->mutex_);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!state->cached_cues_.contains(cue_path)) {
|
2011-01-19 16:36:40 +01:00
|
|
|
QFile cue(cue_path);
|
|
|
|
cue.open(QIODevice::ReadOnly);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
song_list =
|
|
|
|
cue_parser.Load(&cue, cue_path, QDir(cue_path.section('/', 0, -2)));
|
2011-01-19 16:36:40 +01:00
|
|
|
state->cached_cues_[cue_path] = song_list;
|
|
|
|
} else {
|
|
|
|
song_list = state->cached_cues_[cue_path];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& from_list : song_list) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (from_list.url().toEncoded() == song.url().toEncoded() &&
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PlaylistBackend::SavePlaylistAsync(int playlist,
|
|
|
|
const PlaylistItemList& items,
|
2010-11-20 21:30:21 +01:00
|
|
|
int last_played, GeneratorPtr dynamic) {
|
2014-02-07 16:34:20 +01:00
|
|
|
metaObject()->invokeMethod(
|
|
|
|
this, "SavePlaylist", Qt::QueuedConnection, Q_ARG(int, playlist),
|
|
|
|
Q_ARG(PlaylistItemList, items), 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());
|
|
|
|
|
2014-03-18 16:14:12 +01:00
|
|
|
qLog(Debug) << "Saving playlist" << playlist;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
QSqlQuery clear("DELETE FROM playlist_items WHERE playlist = :playlist", db);
|
2014-02-07 16:34:20 +01:00
|
|
|
QSqlQuery insert(
|
|
|
|
"INSERT INTO playlist_items"
|
|
|
|
" (playlist, type, library_id, radio_service, " +
|
|
|
|
Song::kColumnSpec +
|
|
|
|
")"
|
|
|
|
" VALUES (:playlist, :type, :library_id, :radio_service, " +
|
|
|
|
Song::kBindSpec + ")",
|
|
|
|
db);
|
|
|
|
QSqlQuery update(
|
|
|
|
"UPDATE playlists SET "
|
|
|
|
" last_played=:last_played,"
|
|
|
|
" dynamic_playlist_type=:dynamic_type,"
|
|
|
|
" dynamic_playlist_data=:dynamic_data,"
|
|
|
|
" dynamic_playlist_backend=:dynamic_backend"
|
|
|
|
" 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();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(clear)) return;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
// Save the new ones
|
2014-02-10 14:29:07 +01:00
|
|
|
for (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();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(update)) return;
|
2010-05-21 00:30:55 +02:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
transaction.Commit();
|
|
|
|
}
|
2010-05-20 23:21:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int PlaylistBackend::CreatePlaylist(const QString& name,
|
2011-04-28 19:50:45 +02:00
|
|
|
const QString& special_type) {
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
2010-05-20 23:21:55 +02:00
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QSqlQuery q(
|
|
|
|
"INSERT INTO playlists (name, special_type)"
|
|
|
|
" VALUES (:name, :special_type)",
|
|
|
|
db);
|
2010-05-20 23:21:55 +02:00
|
|
|
q.bindValue(":name", name);
|
2011-04-28 19:50:45 +02:00
|
|
|
q.bindValue(":special_type", special_type);
|
2010-05-20 23:21:55 +02:00
|
|
|
q.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(q)) return -1;
|
2010-05-20 23:21:55 +02:00
|
|
|
|
|
|
|
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();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(delete_playlist)) return;
|
2010-05-20 23:21:55 +02:00
|
|
|
|
|
|
|
delete_items.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(delete_items)) return;
|
2010-05-20 23:21:55 +02:00
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
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
|
|
|
|
2013-05-11 23:17:54 +02:00
|
|
|
void PlaylistBackend::FavoritePlaylist(int id, bool is_favorite) {
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
2014-02-07 16:34:20 +01:00
|
|
|
QSqlQuery q("UPDATE playlists SET is_favorite=:is_favorite WHERE ROWID=:id",
|
|
|
|
db);
|
2013-05-11 23:17:54 +02:00
|
|
|
q.bindValue(":is_favorite", is_favorite ? 1 : 0);
|
|
|
|
q.bindValue(":id", id);
|
|
|
|
|
|
|
|
q.exec();
|
|
|
|
db_->CheckErrors(q);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
ScopedTransaction transaction(&db);
|
|
|
|
|
2012-10-31 07:04:22 +01:00
|
|
|
QSqlQuery q("UPDATE playlists SET ui_order=-1", db);
|
|
|
|
q.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(q)) return;
|
2012-10-31 07:04:22 +01:00
|
|
|
|
|
|
|
q = QSqlQuery("UPDATE playlists SET ui_order=:index WHERE ROWID=:id", db);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < ids.count(); ++i) {
|
2010-05-21 12:37:24 +02:00
|
|
|
q.bindValue(":index", i);
|
|
|
|
q.bindValue(":id", ids[i]);
|
|
|
|
q.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(q)) return;
|
2010-05-21 12:37:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|
2012-10-31 07:04:22 +01:00
|
|
|
|
|
|
|
void PlaylistBackend::SetPlaylistUiPath(int id, const QString& path) {
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
QSqlQuery q("UPDATE playlists SET ui_path=:path WHERE ROWID=:id", db);
|
|
|
|
|
|
|
|
ScopedTransaction transaction(&db);
|
|
|
|
|
|
|
|
q.bindValue(":path", path);
|
|
|
|
q.bindValue(":id", id);
|
|
|
|
q.exec();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (db_->CheckErrors(q)) return;
|
2012-10-31 07:04:22 +01:00
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|