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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "devicemanager.h"
|
|
|
|
#include "gpoddevice.h"
|
|
|
|
#include "gpodloader.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2010-07-24 22:43:51 +02:00
|
|
|
#include "library/librarybackend.h"
|
2010-07-04 22:52:45 +02:00
|
|
|
#include "library/librarymodel.h"
|
|
|
|
|
2010-09-09 23:49:51 +02:00
|
|
|
#include <QDir>
|
2010-07-24 22:43:51 +02:00
|
|
|
#include <QFile>
|
2012-02-13 21:44:04 +01:00
|
|
|
#include <QThread>
|
2010-07-04 22:52:45 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include <gpod/itdb.h>
|
|
|
|
|
|
|
|
GPodDevice::GPodDevice(
|
2010-07-17 16:22:07 +02:00
|
|
|
const QUrl& url, DeviceLister* lister,
|
|
|
|
const QString& unique_id, DeviceManager* manager,
|
2012-02-12 14:41:50 +01:00
|
|
|
Application* app,
|
2010-07-17 16:22:07 +02:00
|
|
|
int database_id, bool first_time)
|
2012-02-12 14:41:50 +01:00
|
|
|
: ConnectedDevice(url, lister, unique_id, manager, app, database_id, first_time),
|
2010-07-04 22:52:45 +02:00
|
|
|
loader_thread_(new QThread(this)),
|
2010-08-14 17:57:05 +02:00
|
|
|
loader_(NULL),
|
2010-07-24 19:41:18 +02:00
|
|
|
db_(NULL)
|
2010-07-04 22:52:45 +02:00
|
|
|
{
|
2010-08-08 19:41:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GPodDevice::Init() {
|
|
|
|
InitBackendDirectory(url_.path(), first_time_);
|
2010-07-04 22:52:45 +02:00
|
|
|
model_->Init();
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
loader_ = new GPodLoader(url_.path(), app_->task_manager(), backend_,
|
2010-08-09 23:32:25 +02:00
|
|
|
shared_from_this());
|
2010-07-04 22:52:45 +02:00
|
|
|
loader_->moveToThread(loader_thread_);
|
|
|
|
|
|
|
|
connect(loader_, SIGNAL(Error(QString)), SIGNAL(Error(QString)));
|
|
|
|
connect(loader_, SIGNAL(TaskStarted(int)), SIGNAL(TaskStarted(int)));
|
2010-07-24 19:41:18 +02:00
|
|
|
connect(loader_, SIGNAL(LoadFinished(Itdb_iTunesDB*)), SLOT(LoadFinished(Itdb_iTunesDB*)));
|
2010-07-04 22:52:45 +02:00
|
|
|
connect(loader_thread_, SIGNAL(started()), loader_, SLOT(LoadDatabase()));
|
|
|
|
loader_thread_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
GPodDevice::~GPodDevice() {
|
2010-07-24 18:58:14 +02:00
|
|
|
}
|
|
|
|
|
2010-07-24 19:41:18 +02:00
|
|
|
void GPodDevice::LoadFinished(Itdb_iTunesDB* db) {
|
|
|
|
QMutexLocker l(&db_mutex_);
|
|
|
|
db_ = db;
|
|
|
|
db_wait_cond_.wakeAll();
|
2010-07-24 18:58:14 +02:00
|
|
|
|
2010-07-24 19:41:18 +02:00
|
|
|
loader_->deleteLater();
|
|
|
|
loader_ = NULL;
|
|
|
|
}
|
2010-07-04 22:52:45 +02:00
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
bool GPodDevice::StartCopy(QList<Song::FileType>* supported_filetypes) {
|
2010-07-24 19:41:18 +02:00
|
|
|
{
|
|
|
|
// Wait for the database to be loaded
|
|
|
|
QMutexLocker l(&db_mutex_);
|
|
|
|
if (!db_)
|
|
|
|
db_wait_cond_.wait(&db_mutex_);
|
2010-07-24 18:58:14 +02:00
|
|
|
}
|
2010-07-24 19:41:18 +02:00
|
|
|
|
|
|
|
// Ensure only one "organise files" can be active at any one time
|
2010-07-31 18:12:16 +02:00
|
|
|
db_busy_.lock();
|
2010-08-30 14:22:15 +02:00
|
|
|
|
|
|
|
if (supported_filetypes)
|
|
|
|
GetSupportedFiletypes(supported_filetypes);
|
|
|
|
return true;
|
2010-07-04 22:52:45 +02:00
|
|
|
}
|
|
|
|
|
2010-08-08 19:41:06 +02:00
|
|
|
Itdb_Track* GPodDevice::AddTrackToITunesDb(const Song& metadata) {
|
2010-07-24 18:58:14 +02:00
|
|
|
// Create the track
|
|
|
|
Itdb_Track* track = itdb_track_new();
|
|
|
|
metadata.ToItdb(track);
|
|
|
|
|
|
|
|
// Add it to the DB and the master playlist
|
|
|
|
// The DB takes ownership of the track
|
2010-07-24 19:41:18 +02:00
|
|
|
itdb_track_add(db_, track, -1);
|
|
|
|
Itdb_Playlist* mpl = itdb_playlist_mpl(db_);
|
2010-07-24 18:58:14 +02:00
|
|
|
itdb_playlist_add_track(mpl, track, -1);
|
|
|
|
|
2010-08-08 19:41:06 +02:00
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPodDevice::AddTrackToModel(Itdb_Track* track, const QString& prefix) {
|
|
|
|
// Add it to our LibraryModel
|
|
|
|
Song metadata_on_device;
|
2011-04-28 14:27:53 +02:00
|
|
|
metadata_on_device.InitFromItdb(track, prefix);
|
2010-08-08 19:41:06 +02:00
|
|
|
metadata_on_device.set_directory_id(1);
|
|
|
|
songs_to_add_ << metadata_on_device;
|
|
|
|
}
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
bool GPodDevice::CopyToStorage(const CopyJob& job) {
|
2010-08-08 19:41:06 +02:00
|
|
|
Q_ASSERT(db_);
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
Itdb_Track* track = AddTrackToITunesDb(job.metadata_);
|
2010-08-08 19:41:06 +02:00
|
|
|
|
2010-07-24 18:58:14 +02:00
|
|
|
// Copy the file
|
|
|
|
GError* error = NULL;
|
2010-09-09 23:49:51 +02:00
|
|
|
itdb_cp_track_to_ipod(track, QDir::toNativeSeparators(job.source_)
|
|
|
|
.toLocal8Bit().constData(), &error);
|
2010-07-24 18:58:14 +02:00
|
|
|
if (error) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Error) << "copying failed:" << error->message;
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->AddError(QString::fromUtf8(error->message));
|
2010-07-24 18:58:14 +02:00
|
|
|
g_error_free(error);
|
|
|
|
|
|
|
|
// Need to remove the track from the db again
|
|
|
|
itdb_track_remove(track);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-08 19:41:06 +02:00
|
|
|
AddTrackToModel(track, url_.path());
|
2010-07-24 22:43:51 +02:00
|
|
|
|
|
|
|
// Remove the original if it was requested
|
2010-08-28 23:55:30 +02:00
|
|
|
if (job.remove_original_) {
|
|
|
|
QFile::remove(job.source_);
|
2010-07-24 22:43:51 +02:00
|
|
|
}
|
|
|
|
|
2010-07-19 21:56:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-11 20:47:53 +02:00
|
|
|
void GPodDevice::WriteDatabase(bool success) {
|
2010-08-10 21:42:43 +02:00
|
|
|
if (success) {
|
|
|
|
// Write the itunes database
|
|
|
|
GError* error = NULL;
|
|
|
|
itdb_write(db_, &error);
|
|
|
|
if (error) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Error) << "writing database failed:" << error->message;
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->AddError(QString::fromUtf8(error->message));
|
2010-08-10 21:42:43 +02:00
|
|
|
g_error_free(error);
|
|
|
|
} else {
|
|
|
|
FinaliseDatabase();
|
|
|
|
|
|
|
|
// Update the library model
|
|
|
|
if (!songs_to_add_.isEmpty())
|
|
|
|
backend_->AddOrUpdateSongs(songs_to_add_);
|
|
|
|
if (!songs_to_remove_.isEmpty())
|
|
|
|
backend_->DeleteSongs(songs_to_remove_);
|
|
|
|
}
|
2010-07-24 18:58:14 +02:00
|
|
|
}
|
|
|
|
|
2010-07-24 22:43:51 +02:00
|
|
|
songs_to_add_.clear();
|
2010-08-01 13:01:07 +02:00
|
|
|
songs_to_remove_.clear();
|
2010-07-31 18:12:16 +02:00
|
|
|
db_busy_.unlock();
|
|
|
|
}
|
|
|
|
|
2010-08-11 20:47:53 +02:00
|
|
|
void GPodDevice::FinishCopy(bool success) {
|
|
|
|
WriteDatabase(success);
|
|
|
|
ConnectedDevice::FinishCopy(success);
|
|
|
|
}
|
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
void GPodDevice::StartDelete() {
|
2010-08-30 14:22:15 +02:00
|
|
|
StartCopy(NULL);
|
2010-07-31 18:12:16 +02:00
|
|
|
}
|
|
|
|
|
2010-08-14 12:32:47 +02:00
|
|
|
bool GPodDevice::RemoveTrackFromITunesDb(const QString& path, const QString& relative_to) {
|
|
|
|
QString ipod_filename = path;
|
|
|
|
if (!relative_to.isEmpty() && path.startsWith(relative_to))
|
|
|
|
ipod_filename.remove(0, relative_to.length() + (relative_to.endsWith('/') ? -1 : 0));
|
|
|
|
|
|
|
|
ipod_filename.replace('/', ':');
|
2010-07-31 18:12:16 +02:00
|
|
|
|
|
|
|
// Find the track in the itdb, identify it by its filename
|
|
|
|
Itdb_Track* track = NULL;
|
|
|
|
for (GList* tracks = db_->tracks ; tracks != NULL ; tracks = tracks->next) {
|
|
|
|
Itdb_Track* t = static_cast<Itdb_Track*>(tracks->data);
|
|
|
|
|
2010-08-14 12:32:47 +02:00
|
|
|
if (t->ipod_path == ipod_filename) {
|
2010-07-31 18:12:16 +02:00
|
|
|
track = t;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track == NULL) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Couldn't find song" << path << "in iTunesDB";
|
2010-07-31 18:12:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the track from all playlists
|
|
|
|
for (GList* playlists = db_->playlists ; playlists != NULL ; playlists = playlists->next) {
|
|
|
|
Itdb_Playlist* playlist = static_cast<Itdb_Playlist*>(playlists->data);
|
|
|
|
|
|
|
|
if (itdb_playlist_contains_track(playlist, track)) {
|
|
|
|
itdb_playlist_remove_track(playlist, track);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the track from the database, this frees the struct too
|
|
|
|
itdb_track_remove(track);
|
|
|
|
|
2010-08-14 12:32:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-28 23:55:30 +02:00
|
|
|
bool GPodDevice::DeleteFromStorage(const DeleteJob& job) {
|
2010-08-14 12:32:47 +02:00
|
|
|
Q_ASSERT(db_);
|
|
|
|
|
2011-04-28 14:27:53 +02:00
|
|
|
if (!RemoveTrackFromITunesDb(job.metadata_.url().toLocalFile(), url_.path()))
|
2010-08-14 12:32:47 +02:00
|
|
|
return false;
|
|
|
|
|
2010-07-31 18:12:16 +02:00
|
|
|
// Remove the file
|
2011-04-28 14:27:53 +02:00
|
|
|
if (!QFile::remove(job.metadata_.url().toLocalFile()))
|
2010-08-14 12:32:47 +02:00
|
|
|
return false;
|
2010-07-31 18:12:16 +02:00
|
|
|
|
|
|
|
// Remove it from our library model
|
2010-08-28 23:55:30 +02:00
|
|
|
songs_to_remove_ << job.metadata_;
|
2010-07-31 18:12:16 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-10 21:42:43 +02:00
|
|
|
void GPodDevice::FinishDelete(bool success) {
|
2010-08-11 20:47:53 +02:00
|
|
|
WriteDatabase(success);
|
|
|
|
ConnectedDevice::FinishDelete(success);
|
2010-07-24 18:58:14 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 14:22:15 +02:00
|
|
|
bool GPodDevice::GetSupportedFiletypes(QList<Song::FileType>* ret) {
|
|
|
|
*ret << Song::Type_Mp4;
|
|
|
|
*ret << Song::Type_Mpeg;
|
|
|
|
return true;
|
2010-08-29 16:49:54 +02:00
|
|
|
}
|