2010-08-14 17:57:05 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-08-14 17:57:05 +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 "devicemanager.h"
|
2010-08-14 18:39:45 +02:00
|
|
|
#include "mtpconnection.h"
|
2010-08-14 17:57:05 +02:00
|
|
|
#include "mtpdevice.h"
|
|
|
|
#include "mtploader.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-08-14 17:57:05 +02:00
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/librarymodel.h"
|
|
|
|
|
|
|
|
#include <libmtp.h>
|
|
|
|
|
2010-08-14 18:39:45 +02:00
|
|
|
#include <QFile>
|
2012-02-13 21:44:04 +01:00
|
|
|
#include <QThread>
|
2010-08-14 18:39:45 +02:00
|
|
|
|
2010-08-14 17:57:05 +02:00
|
|
|
bool MtpDevice::sInitialisedLibMTP = false;
|
|
|
|
|
|
|
|
MtpDevice::MtpDevice(const QUrl& url, DeviceLister* lister,
|
|
|
|
const QString& unique_id, DeviceManager* manager,
|
2012-02-12 14:41:50 +01:00
|
|
|
Application* app,
|
2010-08-14 17:57:05 +02:00
|
|
|
int database_id, bool first_time)
|
2012-02-12 14:41:50 +01:00
|
|
|
: ConnectedDevice(url, lister, unique_id, manager, app, database_id, first_time),
|
2010-08-14 17:57:05 +02:00
|
|
|
loader_thread_(new QThread(this)),
|
|
|
|
loader_(NULL)
|
|
|
|
{
|
|
|
|
if (!sInitialisedLibMTP) {
|
|
|
|
LIBMTP_Init();
|
|
|
|
sInitialisedLibMTP = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-14 18:39:45 +02:00
|
|
|
MtpDevice::~MtpDevice() {
|
|
|
|
}
|
|
|
|
|
2010-08-14 17:57:05 +02:00
|
|
|
void MtpDevice::Init() {
|
|
|
|
InitBackendDirectory("/", first_time_, false);
|
|
|
|
model_->Init();
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
loader_ = new MtpLoader(url_, app_->task_manager(), backend_,
|
2010-08-14 17:57:05 +02:00
|
|
|
shared_from_this());
|
|
|
|
loader_->moveToThread(loader_thread_);
|
|
|
|
|
|
|
|
connect(loader_, SIGNAL(Error(QString)), SIGNAL(Error(QString)));
|
|
|
|
connect(loader_, SIGNAL(TaskStarted(int)), SIGNAL(TaskStarted(int)));
|
|
|
|
connect(loader_, SIGNAL(LoadFinished()), SLOT(LoadFinished()));
|
|
|
|
connect(loader_thread_, SIGNAL(started()), loader_, SLOT(LoadDatabase()));
|
2010-08-29 15:49:24 +02:00
|
|
|
|
|
|
|
db_busy_.lock();
|
2010-08-14 17:57:05 +02:00
|
|
|
loader_thread_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MtpDevice::LoadFinished() {
|
|
|
|
loader_->deleteLater();
|
|
|
|
loader_ = NULL;
|
2010-08-29 15:49:24 +02:00
|
|
|
db_busy_.unlock();
|
2010-08-14 17:57:05 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
bool MtpDevice::StartCopy(QList<Song::FileType>* supported_types) {
|
2010-08-14 18:39:45 +02:00
|
|
|
// Ensure only one "organise files" can be active at any one time
|
|
|
|
db_busy_.lock();
|
|
|
|
|
|
|
|
// Connect to the device
|
2010-09-03 00:35:00 +02:00
|
|
|
connection_.reset(new MtpConnection(url_));
|
2010-08-30 14:22:15 +02: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;
|
2010-08-14 18:39:45 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
static int ProgressCallback(uint64_t const sent, uint64_t const total,
|
|
|
|
void const* const data) {
|
|
|
|
const MusicStorage::CopyJob* job =
|
|
|
|
reinterpret_cast<const MusicStorage::CopyJob*>(data);
|
|
|
|
job->progress_(float(sent) / total);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MtpDevice::CopyToStorage(const CopyJob& job) {
|
2010-08-29 21:22:21 +02:00
|
|
|
if (!connection_->is_valid())
|
|
|
|
return false;
|
|
|
|
|
2010-08-14 18:39:45 +02:00
|
|
|
// Convert metadata
|
|
|
|
LIBMTP_track_t track;
|
2010-08-28 23:55:30 +02:00
|
|
|
job.metadata_.ToMTP(&track);
|
2010-08-14 18:39:45 +02:00
|
|
|
|
|
|
|
// Send the file
|
|
|
|
int ret = LIBMTP_Send_Track_From_File(
|
2010-08-28 23:55:30 +02:00
|
|
|
connection_->device(), job.source_.toUtf8().constData(), &track,
|
|
|
|
ProgressCallback, &job);
|
2010-08-14 18:39:45 +02:00
|
|
|
if (ret != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Add it to our LibraryModel
|
|
|
|
Song metadata_on_device;
|
2011-04-28 14:27:53 +02:00
|
|
|
metadata_on_device.InitFromMTP(&track, url_.host());
|
2010-08-14 18:39:45 +02:00
|
|
|
metadata_on_device.set_directory_id(1);
|
|
|
|
songs_to_add_ << metadata_on_device;
|
|
|
|
|
|
|
|
// Remove the original if requested
|
2010-08-28 23:55:30 +02:00
|
|
|
if (job.remove_original_) {
|
|
|
|
if (!QFile::remove(job.source_))
|
2010-08-14 18:39:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MtpDevice::FinishCopy(bool success) {
|
|
|
|
if (success) {
|
|
|
|
if (!songs_to_add_.isEmpty())
|
|
|
|
backend_->AddOrUpdateSongs(songs_to_add_);
|
2010-08-14 19:06:38 +02:00
|
|
|
if (!songs_to_remove_.isEmpty())
|
|
|
|
backend_->DeleteSongs(songs_to_remove_);
|
2010-08-14 18:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
songs_to_add_.clear();
|
|
|
|
songs_to_remove_.clear();
|
|
|
|
|
|
|
|
connection_.reset();
|
|
|
|
|
|
|
|
db_busy_.unlock();
|
2010-08-30 14:28:03 +02:00
|
|
|
|
|
|
|
ConnectedDevice::FinishCopy(success);
|
2010-08-14 18:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MtpDevice::StartDelete() {
|
2010-08-30 14:22:15 +02:00
|
|
|
StartCopy(NULL);
|
2010-08-14 17:57:05 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
bool MtpDevice::DeleteFromStorage(const DeleteJob& job) {
|
2010-08-14 19:06:38 +02:00
|
|
|
// Extract the ID from the song's URL
|
2011-04-28 14:27:53 +02:00
|
|
|
QString filename = job.metadata_.url().path();
|
2010-08-14 19:06:38 +02:00
|
|
|
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 library model
|
2010-08-28 23:55:30 +02:00
|
|
|
songs_to_remove_ << job.metadata_;
|
2010-08-14 19:06:38 +02:00
|
|
|
|
|
|
|
return true;
|
2010-08-14 17:57:05 +02:00
|
|
|
}
|
2010-08-14 18:39:45 +02:00
|
|
|
|
|
|
|
void MtpDevice::FinishDelete(bool success) {
|
2010-08-14 19:06:38 +02:00
|
|
|
FinishCopy(success);
|
2010-08-14 18:39:45 +02:00
|
|
|
}
|
2010-08-29 02:08:39 +02:00
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
bool MtpDevice::GetSupportedFiletypes(QList<Song::FileType>* ret) {
|
2010-08-29 15:49:24 +02:00
|
|
|
QMutexLocker l(&db_busy_);
|
2010-09-03 00:35:00 +02:00
|
|
|
MtpConnection connection(url_);
|
2010-08-29 21:22:21 +02:00
|
|
|
if (!connection.is_valid()) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Error connecting to MTP device, couldn't get list of supported filetypes";
|
2010-08-30 14:22:15 +02:00
|
|
|
return false;
|
2010-08-29 21:22:21 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
return GetSupportedFiletypes(ret, connection.device());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MtpDevice::GetSupportedFiletypes(QList<Song::FileType>* ret, LIBMTP_mtpdevice_t* device) {
|
|
|
|
uint16_t* list = NULL;
|
|
|
|
uint16_t length = 0;
|
|
|
|
|
|
|
|
if (LIBMTP_Get_Supported_Filetypes(device, &list, &length)
|
2010-08-29 02:08:39 +02:00
|
|
|
|| !list || !length)
|
2010-08-30 14:22:15 +02:00
|
|
|
return false;
|
2010-08-29 02:08:39 +02:00
|
|
|
|
|
|
|
for (int i=0 ; i<length ; ++i) {
|
|
|
|
switch (LIBMTP_filetype_t(list[i])) {
|
2010-08-30 14:22:15 +02:00
|
|
|
case LIBMTP_FILETYPE_WAV: *ret << Song::Type_Wav; break;
|
2010-08-29 02:08:39 +02:00
|
|
|
case LIBMTP_FILETYPE_MP2:
|
2010-08-30 14:22:15 +02:00
|
|
|
case LIBMTP_FILETYPE_MP3: *ret << Song::Type_Mpeg; break;
|
|
|
|
case LIBMTP_FILETYPE_WMA: *ret << Song::Type_Asf; break;
|
2010-08-29 02:08:39 +02:00
|
|
|
case LIBMTP_FILETYPE_MP4:
|
|
|
|
case LIBMTP_FILETYPE_M4A:
|
2010-08-30 14:22:15 +02:00
|
|
|
case LIBMTP_FILETYPE_AAC: *ret << Song::Type_Mp4; break;
|
2010-08-29 02:08:39 +02:00
|
|
|
case LIBMTP_FILETYPE_FLAC:
|
2010-08-30 14:22:15 +02:00
|
|
|
*ret << Song::Type_Flac;
|
|
|
|
*ret << Song::Type_OggFlac;
|
2010-08-29 02:08:39 +02:00
|
|
|
break;
|
|
|
|
case LIBMTP_FILETYPE_OGG:
|
2010-08-30 14:22:15 +02:00
|
|
|
*ret << Song::Type_OggVorbis;
|
|
|
|
*ret << Song::Type_OggSpeex;
|
|
|
|
*ret << Song::Type_OggFlac;
|
2010-08-29 02:08:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Error) << "Unknown MTP file format" <<
|
2010-08-29 02:08:39 +02:00
|
|
|
LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t(list[i]));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(list);
|
2010-08-30 14:22:15 +02:00
|
|
|
return true;
|
2010-08-29 02:08:39 +02:00
|
|
|
}
|