From a5102a2b279084855d683cba60a75c8a01ab0bca Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sun, 9 May 2010 21:31:52 +0000 Subject: [PATCH] Remove all existing magnatune songs before reloading the database --- src/librarybackend.cpp | 5 +++-- src/magnatuneservice.cpp | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/librarybackend.cpp b/src/librarybackend.cpp index 40172b3e9..f3922122f 100644 --- a/src/librarybackend.cpp +++ b/src/librarybackend.cpp @@ -145,8 +145,9 @@ void LibraryBackend::RemoveDirectory(const Directory& dir) { SongList LibraryBackend::FindSongsInDirectory(int id) { QSqlDatabase db(db_->Connect()); - QSqlQuery q("SELECT ROWID, " + Song::kColumnSpec + - " FROM songs WHERE directory = :directory", db); + QSqlQuery q(QString("SELECT ROWID, " + Song::kColumnSpec + + " FROM %1 WHERE directory = :directory") + .arg(songs_table_), db); q.bindValue(":directory", id); q.exec(); if (db_->CheckErrors(q.lastError())) return SongList(); diff --git a/src/magnatuneservice.cpp b/src/magnatuneservice.cpp index fe6dea514..4355e0bf2 100644 --- a/src/magnatuneservice.cpp +++ b/src/magnatuneservice.cpp @@ -126,6 +126,7 @@ void MagnatuneService::ReloadDatabaseFinished() { root_->ClearNotify(); + // The XML file is compressed QtIOCompressor gzip(reply); gzip.setStreamFormat(QtIOCompressor::GzipFormat); if (!gzip.open(QIODevice::ReadOnly)) { @@ -133,8 +134,13 @@ void MagnatuneService::ReloadDatabaseFinished() { return; } - SongList songs; + // Remove all existing songs in the database + // FindSongsByDirectory isn't the nicest way to do it, but it's easy + SongList songs = library_backend_->FindSongsInDirectory(0); + library_backend_->DeleteSongs(songs); + songs.clear(); + // Parse the XML we got from Magnatune QXmlStreamReader reader(&gzip); while (!reader.atEnd()) { reader.readNext(); @@ -145,6 +151,7 @@ void MagnatuneService::ReloadDatabaseFinished() { } } + // Add the songs to the database library_backend_->AddOrUpdateSongs(songs); }