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:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <libmtp.h>
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QUrl>
|
2018-03-10 13:02:56 +01:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
#include "core/shared_ptr.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/taskmanager.h"
|
|
|
|
#include "core/song.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "collection/collectionbackend.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "mtpconnection.h"
|
|
|
|
#include "mtploader.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
using std::make_unique;
|
|
|
|
|
|
|
|
MtpLoader::MtpLoader(const QUrl &url, SharedPtr<TaskManager> task_manager, SharedPtr<CollectionBackend> backend, QObject *parent)
|
2021-06-20 19:04:08 +02:00
|
|
|
: QObject(parent),
|
2018-02-27 18:06:05 +01:00
|
|
|
url_(url),
|
|
|
|
task_manager_(task_manager),
|
2019-04-08 18:46:11 +02:00
|
|
|
backend_(backend),
|
2019-09-07 23:30:35 +02:00
|
|
|
abort_(false) {
|
2018-02-27 18:06:05 +01:00
|
|
|
original_thread_ = thread();
|
|
|
|
}
|
|
|
|
|
2021-06-21 15:38:58 +02:00
|
|
|
MtpLoader::~MtpLoader() = default;
|
2018-12-29 02:57:22 +01:00
|
|
|
|
2019-01-21 18:58:54 +01:00
|
|
|
bool MtpLoader::Init() { return true; }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
void MtpLoader::LoadDatabase() {
|
|
|
|
|
|
|
|
int task_id = task_manager_->StartTask(tr("Loading MTP device"));
|
|
|
|
emit TaskStarted(task_id);
|
|
|
|
|
2019-01-21 18:58:54 +01:00
|
|
|
bool success = TryLoad();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
moveToThread(original_thread_);
|
|
|
|
|
|
|
|
task_manager_->SetTaskFinished(task_id);
|
2019-09-07 23:30:35 +02:00
|
|
|
emit LoadFinished(success, connection_.release());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MtpLoader::TryLoad() {
|
|
|
|
|
2023-07-21 05:55:24 +02:00
|
|
|
connection_ = make_unique<MtpConnection>(url_);
|
2019-07-19 19:56:37 +02:00
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
if (!connection_ || !connection_->is_valid()) {
|
2019-07-12 20:32:30 +02:00
|
|
|
emit Error(tr("Error connecting MTP device %1").arg(url_.toString()));
|
2019-01-21 18:58:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Load the list of songs on the device
|
|
|
|
SongList songs;
|
2021-06-12 20:53:23 +02:00
|
|
|
LIBMTP_track_t *tracks = LIBMTP_Get_Tracklisting_With_Callback(connection_->device(), nullptr, nullptr);
|
2018-02-27 18:06:05 +01:00
|
|
|
while (tracks) {
|
2019-09-07 23:30:35 +02:00
|
|
|
|
|
|
|
if (abort_) break;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
LIBMTP_track_t *track = tracks;
|
|
|
|
|
2023-02-18 14:09:27 +01:00
|
|
|
Song song(Song::Source::Device);
|
2018-02-27 18:06:05 +01:00
|
|
|
song.InitFromMTP(track, url_.host());
|
2024-01-24 19:21:02 +01:00
|
|
|
if (song.is_valid() && !song.title().isEmpty()) {
|
2019-07-19 19:56:37 +02:00
|
|
|
song.set_directory_id(1);
|
|
|
|
songs << song;
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
tracks = tracks->next;
|
|
|
|
LIBMTP_destroy_track_t(track);
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
if (!abort_) {
|
|
|
|
// Need to remove all the existing songs in the database first
|
|
|
|
backend_->DeleteSongs(backend_->FindSongsInDirectory(1));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
// Add the songs we've just loaded
|
|
|
|
backend_->AddOrUpdateSongs(songs);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
// This is done in the loader thread so close the unique DB connection.
|
2019-07-24 19:16:51 +02:00
|
|
|
backend_->Close();
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
return !abort_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|