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 2019-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 <libmtp.h>
|
2020-06-14 23:54:18 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include <QThread>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QList>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QtDebug>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "collection/collectionmodel.h"
|
|
|
|
#include "collection/collectionbackend.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/musicstorage.h"
|
|
|
|
#include "connecteddevice.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "mtpdevice.h"
|
|
|
|
#include "mtploader.h"
|
2019-09-07 23:50:36 +02:00
|
|
|
#include "mtpconnection.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
|
|
|
|
class DeviceLister;
|
|
|
|
class DeviceManager;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-10-17 17:29:09 +02:00
|
|
|
bool MtpDevice::sInitializedLibMTP = false;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2021-06-20 19:04:08 +02:00
|
|
|
MtpDevice::MtpDevice(const QUrl &url, DeviceLister *lister, const QString &unique_id, DeviceManager *manager, Application *app, const int database_id, const bool first_time, QObject *parent)
|
|
|
|
: ConnectedDevice(url, lister, unique_id, manager, app, database_id, first_time, parent),
|
2019-09-07 23:30:35 +02:00
|
|
|
loader_(nullptr),
|
|
|
|
loader_thread_(nullptr),
|
|
|
|
closing_(false) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-10-17 17:29:09 +02:00
|
|
|
if (!sInitializedLibMTP) {
|
2018-02-27 18:06:05 +01:00
|
|
|
LIBMTP_Init();
|
2020-10-17 17:29:09 +02:00
|
|
|
sInitializedLibMTP = true;
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-19 19:56:37 +02:00
|
|
|
MtpDevice::~MtpDevice() {
|
2019-09-07 23:30:35 +02:00
|
|
|
|
2019-07-19 19:56:37 +02:00
|
|
|
if (loader_) {
|
|
|
|
loader_thread_->exit();
|
|
|
|
loader_->deleteLater();
|
|
|
|
loader_ = nullptr;
|
|
|
|
db_busy_.unlock();
|
|
|
|
loader_thread_->deleteLater();
|
|
|
|
}
|
2019-09-07 23:30:35 +02:00
|
|
|
|
2019-07-19 19:56:37 +02:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-29 02:57:22 +01:00
|
|
|
bool MtpDevice::Init() {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
InitBackendDirectory("/", first_time_, false);
|
2019-01-21 18:58:54 +01:00
|
|
|
model_->Init();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
loader_ = new MtpLoader(url_, app_->task_manager(), backend_);
|
|
|
|
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_, &MtpLoader::Error, this, &MtpDevice::LoaderError);
|
|
|
|
QObject::connect(loader_, &MtpLoader::TaskStarted, this, &MtpDevice::TaskStarted);
|
|
|
|
QObject::connect(loader_, &MtpLoader::LoadFinished, this, &MtpDevice::LoadFinished);
|
|
|
|
QObject::connect(loader_thread_, &QThread::started, loader_, &MtpLoader::LoadDatabase);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-01-21 18:58:54 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
void MtpDevice::ConnectAsync() {
|
2019-07-19 19:56:37 +02:00
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
db_busy_.lock();
|
|
|
|
loader_thread_->start();
|
2019-07-19 19:56:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
void MtpDevice::Close() {
|
2019-01-21 18:58:54 +01:00
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
closing_ = true;
|
|
|
|
|
|
|
|
if (IsLoading()) {
|
|
|
|
loader_->Abort();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ConnectedDevice::Close();
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void MtpDevice::LoadFinished(const bool success, MtpConnection *connection) {
|
2019-09-07 23:30:35 +02:00
|
|
|
|
|
|
|
connection_.reset(connection);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-07-11 00:28:19 +02:00
|
|
|
loader_thread_->exit();
|
2018-02-27 18:06:05 +01:00
|
|
|
loader_->deleteLater();
|
|
|
|
loader_ = nullptr;
|
|
|
|
db_busy_.unlock();
|
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
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void MtpDevice::LoaderError(const QString &message) {
|
2019-07-19 19:56:37 +02:00
|
|
|
app_->AddError(message);
|
|
|
|
}
|
2019-01-21 18:58:54 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
bool MtpDevice::StartCopy(QList<Song::FileType> *supported_types) {
|
|
|
|
|
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();
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
if (!connection_ || !connection_->is_valid()) return false;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Did the caller want a list of supported types?
|
|
|
|
if (supported_types) {
|
|
|
|
if (!GetSupportedFiletypes(supported_types, connection_->device())) {
|
|
|
|
FinishCopy(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ProgressCallback(uint64_t const sent, uint64_t const total, void const *const data) {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
const MusicStorage::CopyJob *job = reinterpret_cast<const MusicStorage::CopyJob*>(data);
|
|
|
|
job->progress_(float(sent) / total);
|
|
|
|
|
|
|
|
return 0;
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MtpDevice::CopyToStorage(const CopyJob &job) {
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
if (!connection_ || !connection_->is_valid()) return false;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Convert metadata
|
|
|
|
LIBMTP_track_t track;
|
|
|
|
job.metadata_.ToMTP(&track);
|
|
|
|
|
|
|
|
// Send the file
|
|
|
|
int ret = LIBMTP_Send_Track_From_File(connection_->device(), job.source_.toUtf8().constData(), &track, ProgressCallback, &job);
|
|
|
|
if (ret != 0) return false;
|
|
|
|
|
|
|
|
// Add it to our CollectionModel
|
2019-07-19 19:56:37 +02:00
|
|
|
Song metadata_on_device(Song::Source_Device);
|
2018-02-27 18:06:05 +01:00
|
|
|
metadata_on_device.InitFromMTP(&track, url_.host());
|
|
|
|
metadata_on_device.set_directory_id(1);
|
2019-07-19 19:56:37 +02:00
|
|
|
metadata_on_device.set_artist(metadata_on_device.effective_albumartist());
|
|
|
|
metadata_on_device.set_albumartist("");
|
2018-02-27 18:06:05 +01:00
|
|
|
songs_to_add_ << metadata_on_device;
|
|
|
|
|
|
|
|
// Remove the original if requested
|
|
|
|
if (job.remove_original_) {
|
|
|
|
if (!QFile::remove(job.source_)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void MtpDevice::FinishCopy(const bool success) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
if (!songs_to_add_.isEmpty()) backend_->AddOrUpdateSongs(songs_to_add_);
|
|
|
|
if (!songs_to_remove_.isEmpty()) backend_->DeleteSongs(songs_to_remove_);
|
|
|
|
}
|
|
|
|
|
|
|
|
songs_to_add_.clear();
|
|
|
|
songs_to_remove_.clear();
|
|
|
|
|
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
|
|
|
db_busy_.unlock();
|
|
|
|
|
|
|
|
ConnectedDevice::FinishCopy(success);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MtpDevice::StartDelete() { StartCopy(nullptr); }
|
|
|
|
|
|
|
|
bool MtpDevice::DeleteFromStorage(const DeleteJob &job) {
|
|
|
|
|
2021-06-22 14:02:37 +02:00
|
|
|
if (!connection_ || !connection_->is_valid()) return false;
|
2019-07-19 19:56:37 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Extract the ID from the song's URL
|
|
|
|
QString filename = job.metadata_.url().path();
|
|
|
|
filename.remove('/');
|
|
|
|
|
|
|
|
bool ok = false;
|
|
|
|
uint32_t id = filename.toUInt(&ok);
|
|
|
|
if (!ok) return false;
|
|
|
|
|
|
|
|
// Remove the file
|
|
|
|
int ret = LIBMTP_Delete_Object(connection_->device(), id);
|
|
|
|
if (ret != 0) return false;
|
|
|
|
|
|
|
|
// Remove it from our collection model
|
|
|
|
songs_to_remove_ << job.metadata_;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void MtpDevice::FinishDelete(const bool success) { FinishCopy(success); }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
bool MtpDevice::GetSupportedFiletypes(QList<Song::FileType> *ret) {
|
|
|
|
|
|
|
|
QMutexLocker l(&db_busy_);
|
|
|
|
MtpConnection connection(url_);
|
2019-07-19 19:56:37 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!connection.is_valid()) {
|
|
|
|
qLog(Warning) << "Error connecting to MTP device, couldn't get list of supported filetypes";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetSupportedFiletypes(ret, connection.device());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MtpDevice::GetSupportedFiletypes(QList<Song::FileType> *ret, LIBMTP_mtpdevice_t *device) {
|
|
|
|
|
|
|
|
uint16_t *list = nullptr;
|
|
|
|
uint16_t length = 0;
|
|
|
|
|
2021-06-22 13:54:58 +02:00
|
|
|
if (LIBMTP_Get_Supported_Filetypes(device, &list, &length) != 0 || !list || !length)
|
2018-02-27 18:06:05 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
switch (LIBMTP_filetype_t(list[i])) {
|
2018-09-08 12:38:02 +02:00
|
|
|
case LIBMTP_FILETYPE_WAV: *ret << Song::FileType_WAV; break;
|
2018-02-27 18:06:05 +01:00
|
|
|
case LIBMTP_FILETYPE_MP2:
|
2018-09-08 12:38:02 +02:00
|
|
|
case LIBMTP_FILETYPE_MP3: *ret << Song::FileType_MPEG; break;
|
|
|
|
case LIBMTP_FILETYPE_WMA: *ret << Song::FileType_ASF; break;
|
2018-02-27 18:06:05 +01:00
|
|
|
case LIBMTP_FILETYPE_MP4:
|
|
|
|
case LIBMTP_FILETYPE_M4A:
|
2018-09-08 12:38:02 +02:00
|
|
|
case LIBMTP_FILETYPE_AAC: *ret << Song::FileType_MP4; break;
|
2018-02-27 18:06:05 +01:00
|
|
|
case LIBMTP_FILETYPE_FLAC:
|
2018-09-08 12:38:02 +02:00
|
|
|
*ret << Song::FileType_FLAC;
|
|
|
|
*ret << Song::FileType_OggFlac;
|
2018-02-27 18:06:05 +01:00
|
|
|
break;
|
|
|
|
case LIBMTP_FILETYPE_OGG:
|
2018-09-08 12:38:02 +02:00
|
|
|
*ret << Song::FileType_OggVorbis;
|
|
|
|
*ret << Song::FileType_OggSpeex;
|
|
|
|
*ret << Song::FileType_OggFlac;
|
2018-02-27 18:06:05 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qLog(Error) << "Unknown MTP file format" << LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t(list[i]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(list);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|