2010-07-04 01:00:07 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-07-04 01:00:07 +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 "devicedatabasebackend.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QVariant>
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "core/database.h"
|
|
|
|
#include "core/scopedtransaction.h"
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
const int DeviceDatabaseBackend::kDeviceSchemaVersion = 0;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DeviceDatabaseBackend::DeviceDatabaseBackend(QObject* parent)
|
|
|
|
: QObject(parent) {}
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceDatabaseBackend::Init(Database* db) { db_ = db; }
|
2010-07-04 01:00:07 +02:00
|
|
|
|
|
|
|
DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
DeviceList ret;
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QSqlQuery q(db);
|
2020-09-18 16:15:19 +02:00
|
|
|
q.prepare(
|
|
|
|
"SELECT ROWID, unique_id, friendly_name, size, icon,"
|
2014-02-07 16:34:20 +01:00
|
|
|
" transcode_mode, transcode_format"
|
2015-04-11 22:52:31 +02:00
|
|
|
" FROM devices");
|
2010-07-04 01:00:07 +02:00
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q)) return ret;
|
2010-07-04 01:00:07 +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();
|
2010-08-29 21:22:21 +02:00
|
|
|
dev.transcode_mode_ = MusicStorage::TranscodeMode(q.value(5).toInt());
|
2010-08-29 17:32:36 +02:00
|
|
|
dev.transcode_format_ = Song::FileType(q.value(6).toInt());
|
2010-07-04 01:00:07 +02:00
|
|
|
ret << dev;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-29 17:32:36 +02:00
|
|
|
int DeviceDatabaseBackend::AddDevice(const Device& device) {
|
2010-07-04 01:00:07 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
ScopedTransaction t(&db);
|
|
|
|
|
|
|
|
// Insert the device into the devices table
|
2015-04-11 22:52:31 +02:00
|
|
|
QSqlQuery q(db);
|
2020-09-18 16:15:19 +02:00
|
|
|
q.prepare(
|
|
|
|
"INSERT INTO devices ("
|
2014-02-07 16:34:20 +01:00
|
|
|
" unique_id, friendly_name, size, icon,"
|
|
|
|
" transcode_mode, transcode_format)"
|
|
|
|
" VALUES (:unique_id, :friendly_name, :size, :icon,"
|
2015-04-11 22:52:31 +02:00
|
|
|
" :transcode_mode, :transcode_format)");
|
2010-07-04 01:00:07 +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_);
|
2010-08-29 17:32:36 +02:00
|
|
|
q.bindValue(":transcode_mode", device.transcode_mode_);
|
|
|
|
q.bindValue(":transcode_format", device.transcode_format_);
|
2010-07-04 01:00:07 +02:00
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q)) return -1;
|
2010-07-04 01:00:07 +02:00
|
|
|
int id = q.lastInsertId().toInt();
|
|
|
|
|
|
|
|
// Create the songs tables for the device
|
2010-10-17 13:22:34 +02:00
|
|
|
QString filename(":schema/device-schema.sql");
|
2010-07-04 01:00:07 +02:00
|
|
|
QFile schema_file(filename);
|
|
|
|
if (!schema_file.open(QIODevice::ReadOnly))
|
|
|
|
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
|
|
|
|
QString schema = QString::fromUtf8(schema_file.readAll());
|
|
|
|
schema.replace("%deviceid", QString::number(id));
|
|
|
|
|
2013-03-10 07:58:09 +01:00
|
|
|
db_->ExecSchemaCommands(db, schema, 0, true);
|
2010-07-04 01:00:07 +02:00
|
|
|
|
|
|
|
t.Commit();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceDatabaseBackend::RemoveDevice(int id) {
|
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
|
|
|
ScopedTransaction t(&db);
|
|
|
|
|
|
|
|
// Remove the device from the devices table
|
2015-04-11 22:52:31 +02:00
|
|
|
QSqlQuery q(db);
|
|
|
|
q.prepare("DELETE FROM devices WHERE ROWID=:id");
|
2010-07-04 01:00:07 +02:00
|
|
|
q.bindValue(":id", id);
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
if (db_->CheckErrors(q)) return;
|
2010-07-04 01:00:07 +02:00
|
|
|
|
|
|
|
// Remove the songs tables for the device
|
2010-07-04 14:56:49 +02:00
|
|
|
db.exec(QString("DROP TABLE device_%1_songs").arg(id));
|
2010-07-04 01:00:07 +02:00
|
|
|
db.exec(QString("DROP TABLE device_%1_fts").arg(id));
|
2010-07-04 14:56:49 +02:00
|
|
|
db.exec(QString("DROP TABLE device_%1_directories").arg(id));
|
|
|
|
db.exec(QString("DROP TABLE device_%1_subdirectories").arg(id));
|
2010-07-04 01:00:07 +02:00
|
|
|
|
|
|
|
t.Commit();
|
|
|
|
}
|
2010-07-04 17:18:37 +02:00
|
|
|
|
2010-08-29 17:32:36 +02:00
|
|
|
void DeviceDatabaseBackend::SetDeviceOptions(int id,
|
2014-02-07 16:34:20 +01:00
|
|
|
const QString& friendly_name,
|
|
|
|
const QString& icon_name,
|
|
|
|
MusicStorage::TranscodeMode mode,
|
|
|
|
Song::FileType format) {
|
2010-07-04 17:18:37 +02:00
|
|
|
QMutexLocker l(db_->Mutex());
|
|
|
|
QSqlDatabase db(db_->Connect());
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
QSqlQuery q(db);
|
2020-09-18 16:15:19 +02:00
|
|
|
q.prepare(
|
|
|
|
"UPDATE devices"
|
2014-02-07 16:34:20 +01:00
|
|
|
" SET friendly_name=:friendly_name,"
|
|
|
|
" icon=:icon_name,"
|
|
|
|
" transcode_mode=:transcode_mode,"
|
|
|
|
" transcode_format=:transcode_format"
|
2015-04-11 22:52:31 +02:00
|
|
|
" WHERE ROWID=:id");
|
2010-07-04 17:18:37 +02:00
|
|
|
q.bindValue(":friendly_name", friendly_name);
|
|
|
|
q.bindValue(":icon_name", icon_name);
|
2010-08-29 17:32:36 +02:00
|
|
|
q.bindValue(":transcode_mode", mode);
|
|
|
|
q.bindValue(":transcode_format", format);
|
2010-07-04 17:18:37 +02:00
|
|
|
q.bindValue(":id", id);
|
|
|
|
q.exec();
|
2011-02-05 14:43:04 +01:00
|
|
|
db_->CheckErrors(q);
|
2010-07-04 17:18:37 +02:00
|
|
|
}
|