Pass shared_ptrs to music storage things to the organiser thread so they won't get deleted if the device is disconnected

This commit is contained in:
David Sansome 2010-08-09 21:50:46 +00:00
parent 43a27979dc
commit 5e00eb11de
13 changed files with 43 additions and 21 deletions

@ -24,7 +24,8 @@
const int DeleteFiles::kBatchSize = 50;
DeleteFiles::DeleteFiles(TaskManager* task_manager, MusicStorage* storage)
DeleteFiles::DeleteFiles(TaskManager* task_manager,
boost::shared_ptr<MusicStorage> storage)
: thread_(NULL),
task_manager_(task_manager),
storage_(storage),
@ -35,6 +36,9 @@ DeleteFiles::DeleteFiles(TaskManager* task_manager, MusicStorage* storage)
original_thread_ = thread();
}
DeleteFiles::~DeleteFiles() {
}
void DeleteFiles::Start(const SongList& songs) {
if (thread_)
return;

@ -19,6 +19,8 @@
#include <QObject>
#include <boost/shared_ptr.hpp>
#include "song.h"
class MusicStorage;
@ -28,7 +30,8 @@ class DeleteFiles : public QObject {
Q_OBJECT
public:
DeleteFiles(TaskManager* task_manager, MusicStorage* storage);
DeleteFiles(TaskManager* task_manager, boost::shared_ptr<MusicStorage> storage);
~DeleteFiles();
static const int kBatchSize;
@ -42,7 +45,7 @@ private:
QThread* thread_;
QThread* original_thread_;
TaskManager* task_manager_;
MusicStorage* storage_;
boost::shared_ptr<MusicStorage> storage_;
SongList songs_;

@ -21,6 +21,8 @@
#include <QMetaType>
#include <boost/shared_ptr.hpp>
class MusicStorage {
public:
MusicStorage();
@ -48,5 +50,6 @@ public:
};
Q_DECLARE_METATYPE(MusicStorage*);
Q_DECLARE_METATYPE(boost::shared_ptr<MusicStorage>);
#endif // MUSICSTORAGE_H

@ -25,7 +25,8 @@
const int Organise::kBatchSize = 10;
Organise::Organise(TaskManager* task_manager, MusicStorage* destination,
Organise::Organise(TaskManager* task_manager,
boost::shared_ptr<MusicStorage> destination,
const OrganiseFormat &format, bool copy, bool overwrite,
const QStringList& files, bool eject_after)
: thread_(NULL),

@ -19,6 +19,8 @@
#include <QObject>
#include <boost/shared_ptr.hpp>
#include "organiseformat.h"
class MusicStorage;
@ -28,7 +30,8 @@ class Organise : public QObject {
Q_OBJECT
public:
Organise(TaskManager* task_manager, MusicStorage* destination,
Organise(TaskManager* task_manager,
boost::shared_ptr<MusicStorage> destination,
const OrganiseFormat& format, bool copy, bool overwrite,
const QStringList& files, bool eject_after);
@ -43,7 +46,7 @@ private:
QThread* thread_;
QThread* original_thread_;
TaskManager* task_manager_;
MusicStorage* destination_;
boost::shared_ptr<MusicStorage> destination_;
const OrganiseFormat format_;
const bool copy_;

@ -251,7 +251,7 @@ QVariant DeviceManager::data(const QModelIndex& index, int role) const {
const_cast<DeviceManager*>(this)->Connect(index.row());
if (!info.device_)
return QVariant();
return QVariant::fromValue<MusicStorage*>(info.device_.get());
return QVariant::fromValue<boost::shared_ptr<MusicStorage> >(info.device_);
case Role_MountPath:
if (!info.device_)

@ -345,7 +345,8 @@ void DeviceView::Delete() {
QMessageBox::Yes, QMessageBox::Cancel) != QMessageBox::Yes)
return;
MusicStorage* storage = device_index.data(MusicStorage::Role_Storage).value<MusicStorage*>();
boost::shared_ptr<MusicStorage> storage =
device_index.data(MusicStorage::Role_Storage).value<boost::shared_ptr<MusicStorage> >();
DeleteFiles* delete_files = new DeleteFiles(manager_->task_manager(), storage);
delete_files->Start(GetSelectedSongs());

@ -31,14 +31,13 @@ LibraryDirectoryModel::LibraryDirectoryModel(LibraryBackend* backend, QObject* p
}
LibraryDirectoryModel::~LibraryDirectoryModel() {
qDeleteAll(storage_);
}
void LibraryDirectoryModel::DirectoryDiscovered(const Directory &dir) {
QStandardItem* item = new QStandardItem(dir.path);
item->setData(dir.id, kIdRole);
item->setIcon(dir_icon_);
storage_ << new FilesystemMusicStorage(dir.path);
storage_ << boost::shared_ptr<MusicStorage>(new FilesystemMusicStorage(dir.path));
appendRow(item);
}
@ -46,7 +45,7 @@ void LibraryDirectoryModel::DirectoryDeleted(const Directory &dir) {
for (int i=0 ; i<rowCount() ; ++i) {
if (item(i, 0)->data(kIdRole).toInt() == dir.id) {
removeRow(i);
delete storage_.takeAt(i);
storage_.removeAt(i);
break;
}
}

@ -17,8 +17,10 @@
#ifndef LIBRARYDIRECTORYMODEL_H
#define LIBRARYDIRECTORYMODEL_H
#include <QStandardItemModel>
#include <QIcon>
#include <QStandardItemModel>
#include <boost/shared_ptr.hpp>
#include "directory.h"
@ -48,7 +50,7 @@ class LibraryDirectoryModel : public QStandardItemModel {
QIcon dir_icon_;
LibraryBackend* backend_;
QList<MusicStorage*> storage_;
QList<boost::shared_ptr<MusicStorage> > storage_;
};
#endif // LIBRARYDIRECTORYMODEL_H

@ -300,8 +300,10 @@ void LibraryView::Delete() {
// We can cheat and always take the storage of the first directory, since
// they'll all be FilesystemMusicStorage in a library and deleting doesn't
// check the actual directory.
MusicStorage* storage = library_->directory_model()->index(0, 0).data(
MusicStorage::Role_Storage).value<MusicStorage*>();
boost::shared_ptr<MusicStorage> storage =
library_->directory_model()->index(0, 0).data(MusicStorage::Role_Storage)
.value<boost::shared_ptr<MusicStorage> >();
DeleteFiles* delete_files = new DeleteFiles(task_manager_, storage);
delete_files->Start(songs);
}

@ -177,11 +177,12 @@ void OrganiseDialog::InsertTag(const QString &tag) {
void OrganiseDialog::UpdatePreviews() {
const QModelIndex destination = ui_->destination->model()->index(
ui_->destination->currentIndex(), 0);
MusicStorage* storage = NULL;
boost::shared_ptr<MusicStorage> storage;
bool has_local_destination = false;
if (destination.isValid()) {
storage = destination.data(MusicStorage::Role_Storage).value<MusicStorage*>();
storage = destination.data(MusicStorage::Role_Storage)
.value<boost::shared_ptr<MusicStorage> >();
if (storage) {
has_local_destination = !storage->LocalPath().isEmpty();
}
@ -274,8 +275,9 @@ void OrganiseDialog::accept() {
const QModelIndex destination = ui_->destination->model()->index(
ui_->destination->currentIndex(), 0);
MusicStorage* storage =
destination.data(MusicStorage::Role_Storage).value<MusicStorage*>();
boost::shared_ptr<MusicStorage> storage =
destination.data(MusicStorage::Role_Storage)
.value<boost::shared_ptr<MusicStorage> >();
// It deletes itself when it's finished.
const bool copy = ui_->aftercopying->currentIndex() == 0;

@ -63,7 +63,6 @@ FileView::FileView(QWidget* parent)
FileView::~FileView() {
delete ui_;
delete storage_;
}
void FileView::SetPath(const QString& path) {

@ -22,7 +22,10 @@
#include <QUrl>
#include <QModelIndex>
#include <boost/shared_ptr.hpp>
class FilesystemMusicStorage;
class MusicStorage;
class TaskManager;
class Ui_FileView;
@ -89,7 +92,7 @@ class FileView : public QWidget {
QUndoStack* undo_stack_;
TaskManager* task_manager_;
FilesystemMusicStorage* storage_;
boost::shared_ptr<MusicStorage> storage_;
};
#endif // FILEVIEW_H