2010-07-04 22:52:45 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-07-04 22:52:45 +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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-09 23:32:25 +02:00
|
|
|
#include "connecteddevice.h"
|
2010-07-04 22:52:45 +02:00
|
|
|
#include "gpodloader.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-07-04 22:52:45 +02:00
|
|
|
#include "core/song.h"
|
|
|
|
#include "core/taskmanager.h"
|
|
|
|
#include "library/librarybackend.h"
|
|
|
|
|
|
|
|
#include <gpod/itdb.h>
|
|
|
|
|
2010-09-09 23:49:51 +02:00
|
|
|
#include <QDir>
|
2010-07-04 22:52:45 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GPodLoader::GPodLoader(const QString& mount_point, TaskManager* task_manager,
|
|
|
|
LibraryBackend* backend,
|
|
|
|
std::shared_ptr<ConnectedDevice> device)
|
|
|
|
: QObject(nullptr),
|
|
|
|
device_(device),
|
|
|
|
mount_point_(mount_point),
|
|
|
|
type_(Song::Type_Unknown),
|
|
|
|
task_manager_(task_manager),
|
|
|
|
backend_(backend) {
|
2010-07-24 19:41:18 +02:00
|
|
|
original_thread_ = thread();
|
2010-07-04 22:52:45 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GPodLoader::~GPodLoader() {}
|
2010-08-09 23:32:25 +02:00
|
|
|
|
2010-07-04 22:52:45 +02:00
|
|
|
void GPodLoader::LoadDatabase() {
|
|
|
|
int task_id = task_manager_->StartTask(tr("Loading iPod database"));
|
|
|
|
emit TaskStarted(task_id);
|
|
|
|
|
|
|
|
// Load the iTunes database
|
2014-02-06 16:49:49 +01:00
|
|
|
GError* error = nullptr;
|
2014-02-07 16:34:20 +01:00
|
|
|
Itdb_iTunesDB* db =
|
|
|
|
itdb_parse(QDir::toNativeSeparators(mount_point_).toLocal8Bit(), &error);
|
2010-07-04 22:52:45 +02:00
|
|
|
|
|
|
|
// Check for errors
|
|
|
|
if (!db) {
|
2010-08-09 21:24:17 +02:00
|
|
|
if (error) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Error) << "loading database failed:" << error->message;
|
2010-08-09 21:24:17 +02:00
|
|
|
emit Error(QString::fromUtf8(error->message));
|
|
|
|
g_error_free(error);
|
|
|
|
} else {
|
|
|
|
emit Error(tr("An error occurred loading the iTunes database"));
|
|
|
|
}
|
|
|
|
|
2010-07-04 22:52:45 +02:00
|
|
|
task_manager_->SetTaskFinished(task_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert all the tracks from libgpod structs into Song classes
|
2011-04-28 14:27:53 +02:00
|
|
|
const QString prefix = path_prefix_.isEmpty()
|
2014-02-07 16:34:20 +01:00
|
|
|
? QDir::fromNativeSeparators(mount_point_)
|
|
|
|
: path_prefix_;
|
2011-04-28 14:27:53 +02:00
|
|
|
|
2010-07-04 22:52:45 +02:00
|
|
|
SongList songs;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (GList* tracks = db->tracks; tracks != nullptr; tracks = tracks->next) {
|
2010-07-04 22:52:45 +02:00
|
|
|
Itdb_Track* track = static_cast<Itdb_Track*>(tracks->data);
|
|
|
|
|
|
|
|
Song song;
|
2011-04-28 14:27:53 +02:00
|
|
|
song.InitFromItdb(track, prefix);
|
2010-07-04 22:52:45 +02:00
|
|
|
song.set_directory_id(1);
|
2010-09-09 23:49:51 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (type_ != Song::Type_Unknown) song.set_filetype(type_);
|
2010-07-04 22:52:45 +02:00
|
|
|
songs << song;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2010-07-24 19:41:18 +02:00
|
|
|
moveToThread(original_thread_);
|
|
|
|
|
2010-07-04 22:52:45 +02:00
|
|
|
task_manager_->SetTaskFinished(task_id);
|
2010-07-24 19:41:18 +02:00
|
|
|
emit LoadFinished(db);
|
2010-07-04 22:52:45 +02:00
|
|
|
}
|