2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
2019-07-24 19:16:51 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
2019-07-24 19:16:51 +02:00
|
|
|
#include <QThread>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QIODevice>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QFile>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QString>
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
|
|
|
|
#include "core/database.h"
|
2021-09-09 21:45:46 +02:00
|
|
|
#include "core/sqlquery.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/scopedtransaction.h"
|
|
|
|
#include "devicedatabasebackend.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-06-26 20:06:11 +02:00
|
|
|
const int DeviceDatabaseBackend::kDeviceSchemaVersion = 4;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-07-11 07:40:57 +02:00
|
|
|
DeviceDatabaseBackend::DeviceDatabaseBackend(QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
db_(nullptr),
|
|
|
|
original_thread_(nullptr) {
|
2019-07-24 19:16:51 +02:00
|
|
|
|
|
|
|
original_thread_ = thread();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void DeviceDatabaseBackend::Init(Database *db) { db_ = db; }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-24 19:16:51 +02:00
|
|
|
void DeviceDatabaseBackend::Close() {
|
|
|
|
|
|
|
|
if (db_) {
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
db_->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceDatabaseBackend::ExitAsync() {
|
2023-05-03 20:08:51 +02:00
|
|
|
QMetaObject::invokeMethod(this, &DeviceDatabaseBackend::Exit, Qt::QueuedConnection);
|
2019-07-24 19:16:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceDatabaseBackend::Exit() {
|
|
|
|
|
2021-09-27 19:09:18 +02:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
2019-07-24 19:16:51 +02:00
|
|
|
Close();
|
|
|
|
moveToThread(original_thread_);
|
|
|
|
emit ExitFinished();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
|
|
|
|
|
|
|
|
DeviceList ret;
|
2019-08-02 22:58:30 +02:00
|
|
|
DeviceList old_devices;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-25 17:56:28 +02:00
|
|
|
{
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
2021-09-09 21:45:46 +02:00
|
|
|
SqlQuery q(db);
|
2019-08-02 22:58:30 +02:00
|
|
|
q.prepare("SELECT ROWID, unique_id, friendly_name, size, icon, schema_version, transcode_mode, transcode_format FROM devices");
|
2021-09-26 21:38:06 +02:00
|
|
|
if (!q.Exec()) {
|
2021-09-09 21:45:46 +02:00
|
|
|
db_->ReportErrors(q);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-07-25 17:56:28 +02:00
|
|
|
|
|
|
|
while (q.next()) {
|
|
|
|
Device dev;
|
|
|
|
dev.id_ = q.value(0).toInt();
|
|
|
|
dev.unique_id_ = q.value(1).toString();
|
|
|
|
dev.friendly_name_ = q.value(2).toString();
|
|
|
|
dev.size_ = q.value(3).toLongLong();
|
|
|
|
dev.icon_name_ = q.value(4).toString();
|
2019-08-02 22:58:30 +02:00
|
|
|
int schema_version = q.value(5).toInt();
|
2022-06-13 00:23:42 +02:00
|
|
|
dev.transcode_mode_ = static_cast<MusicStorage::TranscodeMode>(q.value(6).toInt());
|
|
|
|
dev.transcode_format_ = static_cast<Song::FileType>(q.value(7).toInt());
|
2021-07-11 09:49:38 +02:00
|
|
|
if (schema_version < kDeviceSchemaVersion) { // Device is using old schema, drop it.
|
2019-08-02 22:58:30 +02:00
|
|
|
old_devices << dev;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret << dev;
|
|
|
|
}
|
2019-07-25 17:56:28 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2019-07-25 17:56:28 +02:00
|
|
|
|
2021-03-21 04:47:11 +01:00
|
|
|
for (const Device &dev : old_devices) {
|
2019-08-02 22:58:30 +02:00
|
|
|
RemoveDevice(dev.id_);
|
|
|
|
}
|
|
|
|
|
2019-07-25 17:56:28 +02:00
|
|
|
Close();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int DeviceDatabaseBackend::AddDevice(const Device &device) {
|
|
|
|
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
ScopedTransaction t(&db);
|
|
|
|
|
|
|
|
// Insert the device into the devices table
|
2021-09-09 21:45:46 +02:00
|
|
|
SqlQuery q(db);
|
2018-02-27 18:06:05 +01:00
|
|
|
q.prepare("INSERT INTO devices (unique_id, friendly_name, size, icon, transcode_mode, transcode_format) VALUES (:unique_id, :friendly_name, :size, :icon, :transcode_mode, :transcode_format)");
|
2021-09-09 21:45:46 +02:00
|
|
|
q.BindValue(":unique_id", device.unique_id_);
|
|
|
|
q.BindValue(":friendly_name", device.friendly_name_);
|
|
|
|
q.BindValue(":size", device.size_);
|
|
|
|
q.BindValue(":icon", device.icon_name_);
|
2023-02-18 14:09:27 +01:00
|
|
|
q.BindValue(":transcode_mode", static_cast<int>(device.transcode_mode_));
|
|
|
|
q.BindValue(":transcode_format", static_cast<int>(device.transcode_format_));
|
2021-09-09 21:45:46 +02:00
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
int id = q.lastInsertId().toInt();
|
|
|
|
|
|
|
|
// Create the songs tables for the device
|
2018-08-29 22:17:23 +02:00
|
|
|
QString filename(":/schema/device-schema.sql");
|
2018-02-27 18:06:05 +01:00
|
|
|
QFile schema_file(filename);
|
2021-08-09 23:32:26 +02:00
|
|
|
if (!schema_file.open(QIODevice::ReadOnly)) {
|
|
|
|
qFatal("Couldn't open schema file %s: %s", filename.toUtf8().constData(), schema_file.errorString().toUtf8().constData());
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
QString schema = QString::fromUtf8(schema_file.readAll());
|
|
|
|
schema.replace("%deviceid", QString::number(id));
|
|
|
|
|
|
|
|
db_->ExecSchemaCommands(db, schema, 0, true);
|
|
|
|
|
|
|
|
t.Commit();
|
2019-07-24 19:16:51 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
return id;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void DeviceDatabaseBackend::RemoveDevice(const int id) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
ScopedTransaction t(&db);
|
|
|
|
|
|
|
|
// Remove the device from the devices table
|
2023-03-18 02:20:03 +01:00
|
|
|
{
|
|
|
|
SqlQuery q(db);
|
|
|
|
q.prepare("DELETE FROM devices WHERE ROWID=:id");
|
|
|
|
q.BindValue(":id", id);
|
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
return;
|
|
|
|
}
|
2021-09-09 21:45:46 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Remove the songs tables for the device
|
2023-03-18 02:20:03 +01:00
|
|
|
{
|
|
|
|
SqlQuery q(db);
|
|
|
|
q.prepare(QString("DROP TABLE device_%1_songs").arg(id));
|
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SqlQuery q(db);
|
|
|
|
q.prepare(QString("DROP TABLE device_%1_fts").arg(id));
|
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SqlQuery q(db);
|
|
|
|
q.prepare(QString("DROP TABLE device_%1_directories").arg(id));
|
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SqlQuery q(db);
|
|
|
|
q.prepare(QString("DROP TABLE device_%1_subdirectories").arg(id));
|
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
t.Commit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void DeviceDatabaseBackend::SetDeviceOptions(const int id, const QString &friendly_name, const QString &icon_name, const MusicStorage::TranscodeMode mode, const Song::FileType format) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
2021-09-09 21:45:46 +02:00
|
|
|
SqlQuery q(db);
|
2018-02-27 18:06:05 +01:00
|
|
|
q.prepare(
|
|
|
|
"UPDATE devices"
|
|
|
|
" SET friendly_name=:friendly_name,"
|
|
|
|
" icon=:icon_name,"
|
|
|
|
" transcode_mode=:transcode_mode,"
|
|
|
|
" transcode_format=:transcode_format"
|
|
|
|
" WHERE ROWID=:id");
|
2021-09-09 21:45:46 +02:00
|
|
|
q.BindValue(":friendly_name", friendly_name);
|
|
|
|
q.BindValue(":icon_name", icon_name);
|
2023-02-18 14:09:27 +01:00
|
|
|
q.BindValue(":transcode_mode", static_cast<int>(mode));
|
|
|
|
q.BindValue(":transcode_format", static_cast<int>(format));
|
2021-09-09 21:45:46 +02:00
|
|
|
q.BindValue(":id", id);
|
|
|
|
if (!q.Exec()) {
|
|
|
|
db_->ReportErrors(q);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|