From 1d25ee78ed750eb375f9b8d002c0033dbf2bc02c Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 14 Aug 2010 10:32:47 +0000 Subject: [PATCH] Support removing files from ipods --- src/devices/afcdevice.cpp | 17 +++++++++++++++-- src/devices/gpoddevice.cpp | 26 ++++++++++++++++++++------ src/devices/gpoddevice.h | 2 ++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/devices/afcdevice.cpp b/src/devices/afcdevice.cpp index 4823001e0..2688fc5a9 100644 --- a/src/devices/afcdevice.cpp +++ b/src/devices/afcdevice.cpp @@ -150,6 +150,19 @@ void AfcDevice::FinaliseDatabase() { } } -bool AfcDevice::DeleteFromStorage(const Song &metadata) { - return false; +bool AfcDevice::DeleteFromStorage(const Song& metadata) { + const QString path = QUrl(metadata.filename()).path(); + + if (!RemoveTrackFromITunesDb(path)) + return false; + + // Remove the file + iMobileDeviceConnection connection(url_.host()); + if (afc_remove_path(connection.afc(), path.toUtf8().constData()) != AFC_E_SUCCESS) + return false; + + // Remove it from our library model + songs_to_remove_ << metadata; + + return true; } diff --git a/src/devices/gpoddevice.cpp b/src/devices/gpoddevice.cpp index ed780f18a..4acf186c3 100644 --- a/src/devices/gpoddevice.cpp +++ b/src/devices/gpoddevice.cpp @@ -163,23 +163,27 @@ void GPodDevice::StartDelete() { StartCopy(); } -bool GPodDevice::DeleteFromStorage(const Song& metadata) { - Q_ASSERT(db_); +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('/', ':'); // 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(tracks->data); - itdb_filename_ipod2fs(t->ipod_path); - if (url_.path() + t->ipod_path == metadata.filename()) { + qDebug() << ipod_filename << t->ipod_path; + if (t->ipod_path == ipod_filename) { track = t; break; } } if (track == NULL) { - qWarning() << "Couldn't find song" << metadata.filename() << "in iTunesDB"; + qWarning() << "Couldn't find song" << path << "in iTunesDB"; return false; } @@ -195,8 +199,18 @@ bool GPodDevice::DeleteFromStorage(const Song& metadata) { // Remove the track from the database, this frees the struct too itdb_track_remove(track); + return true; +} + +bool GPodDevice::DeleteFromStorage(const Song& metadata) { + Q_ASSERT(db_); + + if (!RemoveTrackFromITunesDb(metadata.filename(), url_.path())) + return false; + // Remove the file - QFile::remove(metadata.filename()); + if (!QFile::remove(metadata.filename())) + return false; // Remove it from our library model songs_to_remove_ << metadata; diff --git a/src/devices/gpoddevice.h b/src/devices/gpoddevice.h index 08dc131c4..5ec87373d 100644 --- a/src/devices/gpoddevice.h +++ b/src/devices/gpoddevice.h @@ -56,6 +56,8 @@ protected slots: protected: Itdb_Track* AddTrackToITunesDb(const Song& metadata); void AddTrackToModel(Itdb_Track* track, const QString& prefix); + bool RemoveTrackFromITunesDb(const QString& path, + const QString& relative_to = QString()); virtual void FinaliseDatabase() {} private: