strawberry-audio-player-win.../src/device/gpodloader.cpp

123 lines
3.4 KiB
C++
Raw Permalink Normal View History

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:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <glib.h>
2018-02-27 18:06:05 +01:00
#include <gpod/itdb.h>
#include <QObject>
2018-02-27 18:06:05 +01:00
#include <QDir>
#include <QByteArray>
#include <QString>
2018-02-27 18:06:05 +01:00
2024-10-22 18:12:33 +02:00
#include "includes/shared_ptr.h"
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "core/song.h"
#include "core/taskmanager.h"
2023-07-21 05:25:57 +02:00
#include "collection/collectionbackend.h"
#include "gpodloader.h"
2018-02-27 18:06:05 +01:00
2024-10-22 18:12:33 +02:00
GPodLoader::GPodLoader(const QString &mount_point,
const SharedPtr<TaskManager> task_manager,
const SharedPtr<CollectionBackend> backend,
const SharedPtr<ConnectedDevice> device,
QObject *parent)
2021-06-20 19:04:08 +02:00
: QObject(parent),
2018-02-27 18:06:05 +01:00
device_(device),
mount_point_(mount_point),
2023-02-18 14:09:27 +01:00
type_(Song::FileType::Unknown),
2018-02-27 18:06:05 +01:00
task_manager_(task_manager),
backend_(backend),
abort_(false) {
2018-02-27 18:06:05 +01:00
original_thread_ = thread();
}
2021-06-21 15:38:58 +02:00
GPodLoader::~GPodLoader() = default;
2018-02-27 18:06:05 +01:00
void GPodLoader::LoadDatabase() {
int task_id = task_manager_->StartTask(tr("Loading iPod database"));
2024-08-25 01:06:30 +02:00
Q_EMIT TaskStarted(task_id);
2018-02-27 18:06:05 +01:00
Itdb_iTunesDB *db = TryLoad();
moveToThread(original_thread_);
task_manager_->SetTaskFinished(task_id);
2024-08-25 01:06:30 +02:00
Q_EMIT LoadFinished(db, !abort_);
}
Itdb_iTunesDB *GPodLoader::TryLoad() {
2018-02-27 18:06:05 +01:00
// Load the iTunes database
2022-09-12 22:39:08 +02:00
const QByteArray mountpoint = QDir::toNativeSeparators(mount_point_).toLocal8Bit();
2018-02-27 18:06:05 +01:00
GError *error = nullptr;
2022-09-12 22:39:08 +02:00
Itdb_iTunesDB *db = itdb_parse(mountpoint.constData(), &error);
2018-02-27 18:06:05 +01:00
// Check for errors
if (!db) {
if (error) {
qLog(Error) << "loading database failed:" << error->message;
2024-08-25 01:06:30 +02:00
Q_EMIT Error(QString::fromUtf8(error->message));
2018-02-27 18:06:05 +01:00
g_error_free(error);
}
else {
2024-08-25 01:06:30 +02:00
Q_EMIT Error(tr("An error occurred loading the iTunes database"));
2018-02-27 18:06:05 +01:00
}
return db;
2018-02-27 18:06:05 +01:00
}
// Convert all the tracks from libgpod structs into Song classes
const QString prefix = path_prefix_.isEmpty() ? QDir::fromNativeSeparators(mount_point_) : path_prefix_;
SongList songs;
for (GList *tracks = db->tracks; tracks != nullptr; tracks = tracks->next) {
if (abort_) break;
2018-02-27 18:06:05 +01:00
Itdb_Track *track = static_cast<Itdb_Track*>(tracks->data);
2023-02-18 14:09:27 +01:00
Song song(Song::Source::Device);
2018-02-27 18:06:05 +01:00
song.InitFromItdb(track, prefix);
song.set_directory_id(1);
2023-02-18 14:09:27 +01:00
if (type_ != Song::FileType::Unknown) song.set_filetype(type_);
2018-02-27 18:06:05 +01:00
songs << song;
}
// Need to remove all the existing songs in the database first
backend_->DeleteSongs(backend_->FindSongsInDirectory(1));
if (!abort_) {
// Add the songs we've just loaded
backend_->AddOrUpdateSongs(songs);
}
2018-02-27 18:06:05 +01:00
// This is done in the loader thread so close the unique DB connection.
backend_->Close();
return db;
2018-02-27 18:06:05 +01:00
}