Add support for saving songs in a separate thread.

Updates issue #542
This commit is contained in:
John Maguire 2010-08-02 14:00:43 +00:00
parent be389a12f3
commit 992e0fb95e
6 changed files with 54 additions and 3 deletions

View File

@ -521,6 +521,7 @@ endif(APPLE)
list(APPEND OTHER_SOURCES
core/macglobalshortcutbackend.h
core/macglobalshortcutbackend.mm
core/modelfuturewatcher.h
devices/afcdevice.cpp
devices/afcdevice.h
devices/afcfile.cpp

View File

@ -0,0 +1,25 @@
#ifndef MODELFUTUREWATCHER_H
#define MODELFUTUREWATCHER_H
#include <QFutureWatcher>
#include <QModelIndex>
template <typename T>
class ModelFutureWatcher : public QFutureWatcher<T> {
public:
ModelFutureWatcher(const QModelIndex& index, QObject* parent = 0)
: QFutureWatcher<T>(parent),
index_(index) {
}
~ModelFutureWatcher() {
}
const QModelIndex& index() const { return index_; }
private:
QModelIndex index_;
};
#endif

View File

@ -50,6 +50,8 @@
#include <QTextCodec>
#include <QVariant>
#include <QtConcurrentRun>
#include <boost/scoped_ptr.hpp>
using boost::scoped_ptr;
@ -829,3 +831,12 @@ bool Song::Save() const {
return ret;
}
bool Song::Save(const Song& song) {
return song.Save();
}
QFuture<bool> Song::BackgroundSave() const {
QFuture<bool> future = QtConcurrent::run(&Song::Save, Song(*this));
return future;
}

View File

@ -17,6 +17,7 @@
#ifndef SONG_H
#define SONG_H
#include <QFuture>
#include <QHash>
#include <QImage>
#include <QList>
@ -185,6 +186,7 @@ class Song {
// Setters
bool IsEditable() const;
bool Save() const;
QFuture<bool> BackgroundSave() const;
void set_id(int id) { d->id_ = id; }
void set_valid(bool v) { d->valid_ = v; }
@ -225,6 +227,7 @@ class Song {
private:
void GuessFileType(TagLib::FileRef* fileref);
static bool Save(const Song& song);
// Helper methods for taglib
static void SetTextFrame(const QString& id, const QString& value,

View File

@ -22,6 +22,7 @@
#include "songloaderinserter.h"
#include "songmimedata.h"
#include "songplaylistitem.h"
#include "core/modelfuturewatcher.h"
#include "library/library.h"
#include "library/librarybackend.h"
#include "library/libraryplaylistitem.h"
@ -240,11 +241,20 @@ bool Playlist::setData(const QModelIndex &index, const QVariant &value, int) {
if(!set_column_value(song, (Column)index.column(), value))
return false;
song.Save();
item_at(row)->Reload();
QFuture<bool> future = song.BackgroundSave();
ModelFutureWatcher<bool>* watcher = new ModelFutureWatcher<bool>(index, this);
watcher->setFuture(future);
connect(watcher, SIGNAL(finished()), SLOT(SongSaveComplete()));
return true;
}
void Playlist::SongSaveComplete() {
ModelFutureWatcher<bool>* watcher = static_cast<ModelFutureWatcher<bool>*>(sender());
watcher->deleteLater();
const QModelIndex& index = watcher->index();
item_at(index.row())->Reload();
emit dataChanged(index, index);
emit EditingFinished(index);
return true;
}
int Playlist::current_index() const {

View File

@ -202,6 +202,7 @@ class Playlist : public QAbstractListModel {
void TracksDequeued();
void TracksEnqueued(const QModelIndex&, int begin, int end);
void QueueLayoutChanged();
void SongSaveComplete();
private:
PlaylistFilter* proxy_;