strawberry-audio-player-win.../src/device/gpoddevice.cpp

391 lines
10 KiB
C++
Raw Normal View History

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:10:03 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <memory>
#include <glib.h>
2018-02-27 18:06:05 +01:00
#include <gpod/itdb.h>
#include <QtGlobal>
#include <QThread>
#include <QMutex>
#include <QByteArray>
2018-02-27 18:06:05 +01:00
#include <QDir>
#include <QFile>
#include <QList>
#include <QString>
#include <QUrl>
2020-02-09 02:29:35 +01:00
#include <QImage>
#include <QTemporaryFile>
#include <QStandardPaths>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "core/application.h"
#include "collection/collectionbackend.h"
#include "collection/collectionmodel.h"
#include "connecteddevice.h"
#include "gpoddevice.h"
#include "gpodloader.h"
class DeviceLister;
class DeviceManager;
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
GPodDevice::GPodDevice(const QUrl &url, DeviceLister *lister, const QString &unique_id, DeviceManager *manager, Application *app, const int database_id, const bool first_time, QObject *parent)
2021-07-11 07:40:57 +02:00
: ConnectedDevice(url, lister, unique_id, manager, app, database_id, first_time, parent),
2018-02-27 18:06:05 +01:00
loader_(nullptr),
loader_thread_(nullptr),
db_(nullptr),
closing_(false) {}
2018-02-27 18:06:05 +01:00
bool GPodDevice::Init() {
2018-02-27 18:06:05 +01:00
InitBackendDirectory(url_.path(), first_time_);
model_->Init();
loader_ = new GPodLoader(url_.path(), app_->task_manager(), backend_, shared_from_this());
loader_thread_ = new QThread();
2018-02-27 18:06:05 +01:00
loader_->moveToThread(loader_thread_);
2021-01-26 16:48:04 +01:00
QObject::connect(loader_, &GPodLoader::Error, this, &GPodDevice::LoaderError);
QObject::connect(loader_, &GPodLoader::TaskStarted, this, &GPodDevice::TaskStarted);
QObject::connect(loader_, &GPodLoader::LoadFinished, this, &GPodDevice::LoadFinished);
QObject::connect(loader_thread_, &QThread::started, loader_, &GPodLoader::LoadDatabase);
2018-02-27 18:06:05 +01:00
return true;
2018-02-27 18:06:05 +01:00
}
2019-07-19 19:56:37 +02:00
GPodDevice::~GPodDevice() {
2019-07-19 19:56:37 +02:00
if (loader_) {
loader_thread_->exit();
loader_->deleteLater();
loader_thread_->deleteLater();
loader_ = nullptr;
loader_thread_ = nullptr;
2019-07-19 19:56:37 +02:00
}
2019-07-19 19:56:37 +02:00
}
2018-02-27 18:06:05 +01:00
void GPodDevice::ConnectAsync() {
loader_thread_->start();
}
void GPodDevice::Close() {
closing_ = true;
if (IsLoading()) {
loader_->Abort();
}
else {
ConnectedDevice::Close();
}
}
2021-06-12 20:53:23 +02:00
void GPodDevice::LoadFinished(Itdb_iTunesDB *db, const bool success) {
2018-02-27 18:06:05 +01:00
QMutexLocker l(&db_mutex_);
db_ = db;
db_wait_cond_.wakeAll();
2019-01-05 23:11:16 +01:00
if (loader_thread_) {
loader_thread_->quit();
loader_thread_->wait(1000);
loader_thread_->deleteLater();
loader_thread_ = nullptr;
}
2018-02-27 18:06:05 +01:00
loader_->deleteLater();
loader_ = nullptr;
if (closing_) {
ConnectedDevice::Close();
}
else {
2021-01-26 16:48:04 +01:00
emit DeviceConnectFinished(unique_id_, success);
}
2018-02-27 18:06:05 +01:00
}
void GPodDevice::LoaderError(const QString &message) { app_->AddError(message); }
void GPodDevice::Start() {
2018-02-27 18:06:05 +01:00
{
// Wait for the database to be loaded
QMutexLocker l(&db_mutex_);
if (!db_) db_wait_cond_.wait(&db_mutex_);
}
// Ensure only one "organize files" can be active at any one time
2018-02-27 18:06:05 +01:00
db_busy_.lock();
}
bool GPodDevice::StartCopy(QList<Song::FileType> *supported_filetypes) {
Start();
2018-02-27 18:06:05 +01:00
if (supported_filetypes) GetSupportedFiletypes(supported_filetypes);
2018-02-27 18:06:05 +01:00
return true;
}
Itdb_Track *GPodDevice::AddTrackToITunesDb(const Song &metadata) {
// Create the track
Itdb_Track *track = itdb_track_new();
metadata.ToItdb(track);
// Add it to the DB and the master playlist
// The DB takes ownership of the track
itdb_track_add(db_, track, -1);
Itdb_Playlist *mpl = itdb_playlist_mpl(db_);
itdb_playlist_add_track(mpl, track, -1);
return track;
}
void GPodDevice::AddTrackToModel(Itdb_Track *track, const QString &prefix) {
// Add it to our CollectionModel
Song metadata_on_device;
metadata_on_device.InitFromItdb(track, prefix);
metadata_on_device.set_directory_id(1);
songs_to_add_ << metadata_on_device;
}
bool GPodDevice::CopyToStorage(const CopyJob &job) {
Q_ASSERT(db_);
Itdb_Track *track = AddTrackToITunesDb(job.metadata_);
if (job.albumcover_) {
bool result = false;
if (!job.cover_image_.isNull()) {
#ifdef Q_OS_LINUX
QString temp_path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/organize";
#else
QString temp_path = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
#endif
if (!QDir(temp_path).exists()) QDir().mkpath(temp_path);
2021-03-20 19:11:05 +01:00
std::shared_ptr<QTemporaryFile> cover_file = std::make_shared<QTemporaryFile>(temp_path + "/track-albumcover-XXXXXX.jpg");
cover_file->setAutoRemove(true);
if (cover_file->open()) {
2021-03-20 19:23:33 +01:00
cover_file->close();
const QImage &image = job.cover_image_;
if (image.save(cover_file->fileName(), "JPG")) {
2022-09-12 22:39:08 +02:00
const QByteArray filename = QFile::encodeName(cover_file->fileName());
result = itdb_track_set_thumbnails(track, filename.constData());
if (result) {
cover_files_ << cover_file;
track->has_artwork = 1;
}
}
else {
qLog(Error) << "Failed to save" << cover_file->fileName() << cover_file->errorString();
}
}
else {
qLog(Error) << "Failed to open" << cover_file->fileName() << cover_file->errorString();
}
}
else if (!job.cover_source_.isEmpty()) {
2022-09-12 22:39:08 +02:00
const QByteArray filename = QFile::encodeName(job.cover_source_);
result = itdb_track_set_thumbnails(track, filename.constData());
if (result) track->has_artwork = 1;
}
else {
result = true;
}
if (!result) {
2021-03-20 19:11:05 +01:00
qLog(Error) << "Failed to set album cover image";
}
2019-01-24 19:13:57 +01:00
}
2018-02-27 18:06:05 +01:00
// Copy the file
GError *error = nullptr;
itdb_cp_track_to_ipod(track, QDir::toNativeSeparators(job.source_).toLocal8Bit().constData(), &error);
if (error) {
qLog(Error) << "Copying failed:" << error->message;
2018-02-27 18:06:05 +01:00
app_->AddError(QString::fromUtf8(error->message));
g_error_free(error);
// Need to remove the track from the db again
itdb_track_remove(track);
return false;
}
// Put the track in the playlist, if one is specified
if (!job.playlist_.isEmpty()) {
// Does the playlist already exist?
2022-09-12 22:39:08 +02:00
QByteArray playlist_name = job.playlist_.toUtf8();
Itdb_Playlist *playlist = itdb_playlist_by_name(db_, playlist_name.data());
if (!playlist) {
// Create the playlist
2022-09-12 22:39:08 +02:00
playlist = itdb_playlist_new(playlist_name.data(), false);
itdb_playlist_add(db_, playlist, -1);
}
// Playlist should exist so add the track to the playlist
2022-09-12 22:39:08 +02:00
itdb_playlist_add_track(playlist, track, -1);
}
2018-02-27 18:06:05 +01:00
AddTrackToModel(track, url_.path());
// Remove the original if it was requested
if (job.remove_original_) {
QFile::remove(job.source_);
}
return true;
}
bool GPodDevice::WriteDatabase() {
2018-02-27 18:06:05 +01:00
// Write the itunes database
GError *error = nullptr;
itdb_write(db_, &error);
cover_files_.clear();
if (error) {
qLog(Error) << "Writing database failed:" << error->message;
app_->AddError(QString::fromUtf8(error->message));
g_error_free(error);
return false;
}
else return true;
2018-02-27 18:06:05 +01:00
}
void GPodDevice::Finish(const bool success) {
// Update the collection model
if (success) {
if (!songs_to_add_.isEmpty()) backend_->AddOrUpdateSongs(songs_to_add_);
if (!songs_to_remove_.isEmpty()) backend_->DeleteSongs(songs_to_remove_);
2018-02-27 18:06:05 +01:00
}
// This is done in the organize thread so close the unique DB connection.
2019-07-25 17:56:28 +02:00
backend_->Close();
2018-02-27 18:06:05 +01:00
songs_to_add_.clear();
songs_to_remove_.clear();
cover_files_.clear();
2018-02-27 18:06:05 +01:00
db_busy_.unlock();
}
void GPodDevice::FinishCopy(bool success) {
if (success) success = WriteDatabase();
Finish(success);
2018-02-27 18:06:05 +01:00
ConnectedDevice::FinishCopy(success);
2018-02-27 18:06:05 +01:00
}
void GPodDevice::StartDelete() { Start(); }
2018-02-27 18:06:05 +01:00
bool GPodDevice::RemoveTrackFromITunesDb(const QString &path, const QString &relative_to) {
QString ipod_filename = path;
2021-08-23 21:21:08 +02:00
if (!relative_to.isEmpty() && path.startsWith(relative_to)) {
2018-02-27 18:06:05 +01:00
ipod_filename.remove(0, relative_to.length() + (relative_to.endsWith('/') ? -1 : 0));
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
ipod_filename.replace('/', ':');
// Find the track in the itdb, identify it by its filename
Itdb_Track *track = nullptr;
for (GList *tracks = db_->tracks; tracks != nullptr; tracks = tracks->next) {
Itdb_Track *t = static_cast<Itdb_Track*>(tracks->data);
if (t->ipod_path == ipod_filename) {
track = t;
break;
}
}
if (track == nullptr) {
qLog(Warning) << "Couldn't find song" << path << "in iTunesDB";
return false;
}
// Remove the track from all playlists
2021-08-23 21:21:08 +02:00
for (GList *playlists = db_->playlists; playlists != nullptr; playlists = playlists->next) {
2018-02-27 18:06:05 +01:00
Itdb_Playlist *playlist = static_cast<Itdb_Playlist*>(playlists->data);
if (itdb_playlist_contains_track(playlist, track)) {
itdb_playlist_remove_track(playlist, track);
}
}
// Remove the track from the database, this frees the struct too
itdb_track_remove(track);
return true;
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
}
bool GPodDevice::DeleteFromStorage(const DeleteJob &job) {
Q_ASSERT(db_);
2021-08-23 21:21:08 +02:00
if (!RemoveTrackFromITunesDb(job.metadata_.url().toLocalFile(), url_.path())) {
2018-02-27 18:06:05 +01:00
return false;
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
// Remove the file
2021-08-23 21:21:08 +02:00
if (!QFile::remove(job.metadata_.url().toLocalFile())) {
return false;
}
2018-02-27 18:06:05 +01:00
// Remove it from our collection model
songs_to_remove_ << job.metadata_;
return true;
}
void GPodDevice::FinishDelete(bool success) {
if (success) success = WriteDatabase();
Finish(success);
2018-02-27 18:06:05 +01:00
ConnectedDevice::FinishDelete(success);
2018-02-27 18:06:05 +01:00
}
bool GPodDevice::GetSupportedFiletypes(QList<Song::FileType> *ret) {
2018-09-08 12:38:02 +02:00
*ret << Song::FileType_MP4;
*ret << Song::FileType_MPEG;
2018-02-27 18:06:05 +01:00
return true;
}