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"
|
|
|
|
|
2021-03-20 19:00:42 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <glib.h>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <gpod/itdb.h>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QByteArray>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QList>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QImage>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QtDebug>
|
2020-08-31 18:38:54 +02:00
|
|
|
#include <QTemporaryFile>
|
2021-03-20 19:00:42 +01:00
|
|
|
#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"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "connecteddevice.h"
|
|
|
|
#include "gpoddevice.h"
|
|
|
|
#include "gpodloader.h"
|
|
|
|
|
|
|
|
class DeviceLister;
|
|
|
|
class DeviceManager;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-05-29 17:36:01 +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)
|
2018-02-27 18:06:05 +01:00
|
|
|
: ConnectedDevice(url, lister, unique_id, manager, app, database_id, first_time),
|
|
|
|
loader_(nullptr),
|
2019-09-07 23:30:35 +02:00
|
|
|
loader_thread_(nullptr),
|
|
|
|
db_(nullptr),
|
|
|
|
closing_(false) {}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-29 02:57:22 +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());
|
2019-09-07 23:30:35 +02:00
|
|
|
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
|
|
|
|
2018-12-29 02:57:22 +01:00
|
|
|
return true;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 19:56:37 +02:00
|
|
|
GPodDevice::~GPodDevice() {
|
2020-05-29 17:36:01 +02:00
|
|
|
|
2019-07-19 19:56:37 +02:00
|
|
|
if (loader_) {
|
|
|
|
loader_thread_->exit();
|
|
|
|
loader_->deleteLater();
|
|
|
|
loader_thread_->deleteLater();
|
2020-05-29 17:36:01 +02:00
|
|
|
loader_ = nullptr;
|
|
|
|
loader_thread_ = nullptr;
|
2019-07-19 19:56:37 +02:00
|
|
|
}
|
2020-05-29 17:36:01 +02:00
|
|
|
|
2019-07-19 19:56:37 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-01-21 18:58:54 +01:00
|
|
|
void GPodDevice::ConnectAsync() {
|
|
|
|
|
|
|
|
loader_thread_->start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
void GPodDevice::Close() {
|
|
|
|
|
|
|
|
closing_ = true;
|
|
|
|
|
|
|
|
if (IsLoading()) {
|
|
|
|
loader_->Abort();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ConnectedDevice::Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-21 18:58:54 +01:00
|
|
|
void GPodDevice::LoadFinished(Itdb_iTunesDB *db, 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;
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
if (closing_) {
|
|
|
|
ConnectedDevice::Close();
|
|
|
|
}
|
|
|
|
else {
|
2021-01-26 16:48:04 +01:00
|
|
|
emit DeviceConnectFinished(unique_id_, success);
|
2019-09-07 23:30:35 +02:00
|
|
|
}
|
2019-01-21 18:58:54 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-21 18:58:54 +01:00
|
|
|
void GPodDevice::LoaderError(const QString &message) { app_->AddError(message); }
|
|
|
|
|
2021-03-20 19:00:42 +01:00
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02:00
|
|
|
// Ensure only one "organize files" can be active at any one time
|
2018-02-27 18:06:05 +01:00
|
|
|
db_busy_.lock();
|
|
|
|
|
2021-03-20 19:00:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GPodDevice::StartCopy(QList<Song::FileType> *supported_filetypes) {
|
|
|
|
|
|
|
|
Start();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (supported_filetypes) GetSupportedFiletypes(supported_filetypes);
|
2021-03-20 19:00:42 +01:00
|
|
|
|
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_);
|
|
|
|
|
2019-01-26 17:18:26 +01:00
|
|
|
if (job.albumcover_) {
|
|
|
|
bool result = false;
|
|
|
|
if (!job.metadata_.image().isNull()) {
|
2021-03-20 19:00:42 +01:00
|
|
|
#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");
|
2021-03-20 19:00:42 +01:00
|
|
|
cover_file->setAutoRemove(true);
|
|
|
|
if (cover_file->open()) {
|
2021-03-20 19:23:33 +01:00
|
|
|
cover_file->close();
|
2020-08-31 18:38:54 +02:00
|
|
|
QImage image = job.metadata_.image();
|
2021-03-20 19:00:42 +01:00
|
|
|
if (image.save(cover_file->fileName(), "JPG")) {
|
|
|
|
result = itdb_track_set_thumbnails(track, QFile::encodeName(cover_file->fileName()));
|
|
|
|
if (result) {
|
|
|
|
cover_files_ << cover_file;
|
|
|
|
track->has_artwork = 1;
|
|
|
|
}
|
2020-08-31 18:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-26 17:18:26 +01:00
|
|
|
}
|
|
|
|
else if (!job.cover_source_.isEmpty()) {
|
2021-03-20 19:00:42 +01:00
|
|
|
result = itdb_track_set_thumbnails(track, QFile::encodeName(job.cover_source_));
|
2020-08-31 18:38:54 +02:00
|
|
|
if (result) track->has_artwork = 1;
|
2019-01-26 17:18:26 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
if (!result) {
|
2021-03-20 19:11:05 +01:00
|
|
|
qLog(Error) << "Failed to set album cover image";
|
2019-01-26 17:18:26 +01:00
|
|
|
}
|
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) {
|
2021-03-20 19:00:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-07 17:13:40 +02:00
|
|
|
// Put the track in the playlist, if one is specified
|
|
|
|
if (!job.playlist_.isEmpty()) {
|
|
|
|
// Does the playlist already exist?
|
|
|
|
auto itdbPlaylist = itdb_playlist_by_name(db_, job.playlist_.toUtf8().data());
|
|
|
|
if (itdbPlaylist == nullptr) {
|
|
|
|
// Create the playlist
|
|
|
|
itdbPlaylist = itdb_playlist_new(job.playlist_.toUtf8().data(), false);
|
|
|
|
itdb_playlist_add(db_, itdbPlaylist, -1);
|
|
|
|
}
|
|
|
|
// Playlist should exist so add the track to the playlist
|
|
|
|
itdb_playlist_add_track(itdbPlaylist, 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:00:42 +01:00
|
|
|
bool GPodDevice::WriteDatabase() {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-03-20 19:00:42 +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
|
|
|
|
2021-03-20 19:00:42 +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
|
|
|
}
|
|
|
|
|
2020-08-04 21:18:14 +02: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();
|
2021-03-20 19:00:42 +01:00
|
|
|
cover_files_.clear();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
db_busy_.unlock();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPodDevice::FinishCopy(bool success) {
|
2021-03-20 19:00:42 +01:00
|
|
|
|
|
|
|
if (success) success = WriteDatabase();
|
|
|
|
Finish(success);
|
2018-02-27 18:06:05 +01:00
|
|
|
ConnectedDevice::FinishCopy(success);
|
2021-03-20 19:00:42 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 19:00:42 +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;
|
|
|
|
if (!relative_to.isEmpty() && path.startsWith(relative_to))
|
|
|
|
ipod_filename.remove(0, relative_to.length() + (relative_to.endsWith('/') ? -1 : 0));
|
|
|
|
|
|
|
|
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
|
|
|
|
for (GList *playlists = db_->playlists ; playlists != nullptr ; playlists = playlists->next) {
|
|
|
|
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_);
|
|
|
|
|
|
|
|
if (!RemoveTrackFromITunesDb(job.metadata_.url().toLocalFile(), url_.path()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Remove the file
|
|
|
|
if (!QFile::remove(job.metadata_.url().toLocalFile())) return false;
|
|
|
|
|
|
|
|
// Remove it from our collection model
|
|
|
|
songs_to_remove_ << job.metadata_;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPodDevice::FinishDelete(bool success) {
|
2021-03-20 19:00:42 +01:00
|
|
|
|
|
|
|
if (success) success = WriteDatabase();
|
|
|
|
Finish(success);
|
2018-02-27 18:06:05 +01:00
|
|
|
ConnectedDevice::FinishDelete(success);
|
2021-03-20 19:00:42 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|