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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
#include "mtploader.h"
|
|
|
|
|
|
|
|
#include <libmtp.h>
|
|
|
|
|
2010-08-14 17:57:05 +02:00
|
|
|
#include "connecteddevice.h"
|
|
|
|
#include "core/song.h"
|
|
|
|
#include "core/taskmanager.h"
|
|
|
|
#include "library/librarybackend.h"
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "mtpconnection.h"
|
2010-08-14 17:57:05 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
MtpLoader::MtpLoader(const QUrl& url, TaskManager* task_manager,
|
2020-01-12 08:34:35 +01:00
|
|
|
std::shared_ptr<LibraryBackend> backend,
|
2014-02-07 16:34:20 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> device)
|
|
|
|
: QObject(nullptr),
|
|
|
|
device_(device),
|
|
|
|
url_(url),
|
|
|
|
task_manager_(task_manager),
|
|
|
|
backend_(backend) {
|
2010-08-14 17:57:05 +02:00
|
|
|
original_thread_ = thread();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
MtpLoader::~MtpLoader() {}
|
2010-08-14 17:57:05 +02:00
|
|
|
|
|
|
|
void MtpLoader::LoadDatabase() {
|
|
|
|
int task_id = task_manager_->StartTask(tr("Loading MTP device"));
|
|
|
|
emit TaskStarted(task_id);
|
|
|
|
|
2019-01-15 22:10:05 +01:00
|
|
|
bool success = TryLoad();
|
2010-08-14 17:57:05 +02:00
|
|
|
|
|
|
|
moveToThread(original_thread_);
|
|
|
|
|
|
|
|
task_manager_->SetTaskFinished(task_id);
|
2019-01-15 22:10:05 +01:00
|
|
|
emit LoadFinished(success);
|
2010-08-14 17:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MtpLoader::TryLoad() {
|
2010-09-02 23:20:27 +02:00
|
|
|
MtpConnection dev(url_);
|
2010-08-14 18:39:45 +02:00
|
|
|
if (!dev.is_valid()) {
|
2010-08-14 17:57:05 +02:00
|
|
|
emit Error(tr("Error connecting MTP device"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the list of songs on the device
|
|
|
|
SongList songs;
|
2014-02-07 16:34:20 +01:00
|
|
|
LIBMTP_track_t* tracks =
|
|
|
|
LIBMTP_Get_Tracklisting_With_Callback(dev.device(), nullptr, nullptr);
|
2010-08-14 17:57:05 +02:00
|
|
|
while (tracks) {
|
|
|
|
LIBMTP_track_t* track = tracks;
|
|
|
|
|
|
|
|
Song song;
|
2011-04-28 14:27:53 +02:00
|
|
|
song.InitFromMTP(track, url_.host());
|
2010-08-14 17:57:05 +02:00
|
|
|
song.set_directory_id(1);
|
|
|
|
songs << song;
|
|
|
|
|
|
|
|
tracks = tracks->next;
|
|
|
|
LIBMTP_destroy_track_t(track);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to remove all the existing songs in the database first
|
|
|
|
backend_->DeleteSongs(backend_->FindSongsInDirectory(1));
|
|
|
|
|
|
|
|
// Add the songs we've just loaded
|
|
|
|
backend_->AddOrUpdateSongs(songs);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|