2010-07-04 22:52:45 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
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 "gpodloader.h"
|
|
|
|
#include "core/song.h"
|
|
|
|
#include "core/taskmanager.h"
|
|
|
|
#include "library/librarybackend.h"
|
|
|
|
|
|
|
|
#include <gpod/itdb.h>
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
GPodLoader::GPodLoader(const QString& mount_point, TaskManager* task_manager,
|
|
|
|
LibraryBackend* backend, QObject *parent)
|
|
|
|
: QObject(parent),
|
|
|
|
mount_point_(mount_point),
|
2010-08-08 15:27:36 +02:00
|
|
|
type_(Song::Type_Unknown),
|
2010-07-04 22:52:45 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void GPodLoader::LoadDatabase() {
|
|
|
|
int task_id = task_manager_->StartTask(tr("Loading iPod database"));
|
|
|
|
emit TaskStarted(task_id);
|
|
|
|
|
|
|
|
// Load the iTunes database
|
|
|
|
GError* error = NULL;
|
|
|
|
Itdb_iTunesDB* db = itdb_parse(mount_point_.toLocal8Bit(), &error);
|
|
|
|
|
|
|
|
// Check for errors
|
|
|
|
if (!db) {
|
|
|
|
qDebug() << "GPodLoader error:" << error->message;
|
|
|
|
emit Error(QString::fromUtf8(error->message));
|
|
|
|
g_error_free(error);
|
|
|
|
task_manager_->SetTaskFinished(task_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert all the tracks from libgpod structs into Song classes
|
|
|
|
SongList songs;
|
|
|
|
for (GList* tracks = db->tracks ; tracks != NULL ; tracks = tracks->next) {
|
|
|
|
Itdb_Track* track = static_cast<Itdb_Track*>(tracks->data);
|
|
|
|
|
|
|
|
Song song;
|
|
|
|
song.InitFromItdb(track);
|
|
|
|
song.set_directory_id(1);
|
2010-08-01 20:09:38 +02:00
|
|
|
song.set_filename((path_prefix_.isEmpty() ? mount_point_ : path_prefix_) +
|
|
|
|
song.filename());
|
2010-08-08 15:27:36 +02: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
|
|
|
}
|