Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a factory class for Directory objects. #6561

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/metatypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void RegisterMetaTypes() {
qRegisterMetaType<GstElement*>("GstElement*");
qRegisterMetaType<GstEngine::OutputDetails>("GstEngine::OutputDetails");
qRegisterMetaType<GstEnginePipeline*>("GstEnginePipeline*");
qRegisterMetaType<DirectoryManager*>("DirectoryManager*");
qRegisterMetaType<PlaylistItemList>("PlaylistItemList");
qRegisterMetaType<PlaylistItemPtr>("PlaylistItemPtr");
qRegisterMetaType<PodcastEpisodeList>("PodcastEpisodeList");
Expand Down
11 changes: 6 additions & 5 deletions src/devices/connecteddevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ConnectedDevice::ConnectedDevice(const QUrl& url, DeviceLister* lister,
unique_id_(unique_id),
database_id_(database_id),
manager_(manager),
directory_manager_(new DirectoryManager),
model_(nullptr),
song_count_(0) {
qLog(Info) << "connected" << url << unique_id << first_time;
Expand All @@ -61,12 +62,12 @@ ConnectedDevice::ConnectedDevice(const QUrl& url, DeviceLister* lister,
model_ = new LibraryModel(backend_, app_, this);
}

ConnectedDevice::~ConnectedDevice() {}
ConnectedDevice::~ConnectedDevice() { delete directory_manager_; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not to use the unique_ptr?


void ConnectedDevice::InitBackendDirectory(const QString& mount_point,
bool first_time, bool rewrite_path) {
if (first_time || backend_->GetAllDirectories().isEmpty()) {
backend_->AddDirectory(mount_point);
if (first_time || backend_->GetAllDirectories(directory_manager_).isEmpty()) {
backend_->AddDirectory(directory_manager_, mount_point);
} else {
if (rewrite_path) {
// This is a bit of a hack. The device might not be mounted at the same
Expand All @@ -76,7 +77,7 @@ void ConnectedDevice::InitBackendDirectory(const QString& mount_point,

// Get the directory it was mounted at last time. Devices only have one
// directory (the root).
Directory dir = backend_->GetAllDirectories()[0];
Directory dir = backend_->GetAllDirectories(directory_manager_)[0];
if (dir.path != mount_point) {
// The directory is different, commence the munging.
qLog(Info) << "Changing path from" << dir.path << "to" << mount_point;
Expand All @@ -85,7 +86,7 @@ void ConnectedDevice::InitBackendDirectory(const QString& mount_point,
}

// Load the directory properly now
backend_->LoadDirectoriesAsync();
backend_->LoadDirectoriesAsync(directory_manager_);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/devices/connecteddevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Application;
class Database;
class DeviceLister;
class DeviceManager;
class DirectoryManager;
class LibraryBackend;
class LibraryModel;

Expand All @@ -42,7 +43,7 @@ class ConnectedDevice : public QObject,
ConnectedDevice(const QUrl& url, DeviceLister* lister,
const QString& unique_id, DeviceManager* manager,
Application* app, int database_id, bool first_time);
~ConnectedDevice();
virtual ~ConnectedDevice();

virtual void Init() = 0;
virtual void ConnectAsync();
Expand Down Expand Up @@ -84,6 +85,7 @@ class ConnectedDevice : public QObject,
DeviceManager* manager_;

std::shared_ptr<LibraryBackend> backend_;
DirectoryManager* directory_manager_;
LibraryModel* model_;

int song_count_;
Expand Down
11 changes: 11 additions & 0 deletions src/library/directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ Q_DECLARE_METATYPE(Directory)
typedef QList<Directory> DirectoryList;
Q_DECLARE_METATYPE(DirectoryList)

struct DirectoryManager {
DirectoryManager() {}
virtual ~DirectoryManager() {}
virtual Directory GetDirectory(const QString& path) const {
Directory dir;
dir.path = path;
return dir;
}
};
Q_DECLARE_METATYPE(DirectoryManager)

struct Subdirectory {
Subdirectory() : directory_id(-1), mtime(0) {}

Expand Down
5 changes: 3 additions & 2 deletions src/library/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Library::Library(Application* app, QObject* parent)
: QObject(parent),
app_(app),
backend_(nullptr),
directory_manager_(new DirectoryManager),
model_(nullptr),
watcher_(nullptr),
watcher_thread_(nullptr),
Expand All @@ -57,7 +58,7 @@ Library::Library(Application* app, QObject* parent)
using smart_playlists::SearchTerm;

model_ = new LibraryModel(backend_, app_, this);
dir_model_ = new LibraryDirectoryModel(backend_, this);
dir_model_ = new LibraryDirectoryModel(backend_, directory_manager_, this);
model_->set_show_smart_playlists(true);
model_->set_default_smart_playlists(
LibraryModel::DefaultGenerators()
Expand Down Expand Up @@ -172,7 +173,7 @@ void Library::Init() {
connect(app_->player(), SIGNAL(Stopped()), SLOT(Stopped()));

// This will start the watcher checking for updates
backend_->LoadDirectoriesAsync();
backend_->LoadDirectoriesAsync(directory_manager_.get());
}

void Library::IncrementalScan() { watcher_->IncrementalScanAsync(); }
Expand Down
2 changes: 2 additions & 0 deletions src/library/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

class Application;
class Database;
class DirectoryManager;
class LibraryBackend;
class LibraryModel;
class LibraryDirectoryModel;
Expand Down Expand Up @@ -80,6 +81,7 @@ class Library : public QObject {
private:
Application* app_;
std::shared_ptr<LibraryBackend> backend_;
std::shared_ptr<DirectoryManager> directory_manager_;
LibraryModel* model_;
LibraryDirectoryModel* dir_model_;

Expand Down
23 changes: 13 additions & 10 deletions src/library/librarybackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ void LibraryBackend::Init(Database* db, const QString& songs_table,
subdirs_table_ = subdirs_table;
}

void LibraryBackend::LoadDirectoriesAsync() {
metaObject()->invokeMethod(this, "LoadDirectories", Qt::QueuedConnection);
void LibraryBackend::LoadDirectoriesAsync(DirectoryManager* directory_manager) {
metaObject()->invokeMethod(this, "LoadDirectories", Qt::QueuedConnection,
Q_ARG(DirectoryManager*, directory_manager));
}

void LibraryBackend::UpdateTotalSongCountAsync() {
Expand Down Expand Up @@ -99,8 +100,8 @@ void LibraryBackend::UpdateSongsRatingAsync(const QList<int>& ids,
Q_ARG(float, rating));
}

void LibraryBackend::LoadDirectories() {
DirectoryList dirs = GetAllDirectories();
void LibraryBackend::LoadDirectories(DirectoryManager* info) {
DirectoryList dirs = GetAllDirectories(info);

QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
Expand Down Expand Up @@ -155,7 +156,9 @@ void LibraryBackend::ChangeDirPath(int id, const QString& old_path,
t.Commit();
}

DirectoryList LibraryBackend::GetAllDirectories() {
DirectoryList LibraryBackend::GetAllDirectories(
DirectoryManager* directory_manager) {
Q_ASSERT(directory_manager);
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());

Expand All @@ -167,9 +170,8 @@ DirectoryList LibraryBackend::GetAllDirectories() {
if (db_->CheckErrors(q)) return ret;

while (q.next()) {
Directory dir;
Directory dir = directory_manager->GetDirectory(q.value(1).toString());
dir.id = q.value(0).toInt();
dir.path = q.value(1).toString();

ret << dir;
}
Expand Down Expand Up @@ -217,7 +219,9 @@ void LibraryBackend::UpdateTotalSongCount() {
emit TotalSongCountUpdated(q.value(0).toInt());
}

void LibraryBackend::AddDirectory(const QString& path) {
void LibraryBackend::AddDirectory(DirectoryManager* directory_manager,
const QString& path) {
Q_ASSERT(directory_manager);
QString canonical_path = QFileInfo(path).canonicalFilePath();
QString db_path = canonical_path;

Expand All @@ -238,8 +242,7 @@ void LibraryBackend::AddDirectory(const QString& path) {
q.exec();
if (db_->CheckErrors(q)) return;

Directory dir;
dir.path = canonical_path;
Directory dir = directory_manager->GetDirectory(canonical_path);
dir.id = q.lastInsertId().toInt();

emit DirectoryDiscovered(dir, SubdirectoryList());
Expand Down
15 changes: 8 additions & 7 deletions src/library/librarybackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ class LibraryBackendInterface : public QObject {
virtual QString songs_table() const = 0;

// Get a list of directories in the library. Emits DirectoriesDiscovered.
virtual void LoadDirectoriesAsync() = 0;
virtual void LoadDirectoriesAsync(DirectoryManager* directory_manager) = 0;

// Counts the songs in the library. Emits TotalSongCountUpdated
virtual void UpdateTotalSongCountAsync() = 0;

virtual SongList FindSongsInDirectory(int id) = 0;
virtual SubdirectoryList SubdirsInDirectory(int id) = 0;
virtual DirectoryList GetAllDirectories() = 0;
virtual DirectoryList GetAllDirectories(DirectoryManager* info) = 0;
virtual void ChangeDirPath(int id, const QString& old_path,
const QString& new_path) = 0;

Expand Down Expand Up @@ -118,7 +118,8 @@ class LibraryBackendInterface : public QObject {
// songs.
virtual Song GetSongByUrl(const QUrl& url, qint64 beginning = 0) = 0;

virtual void AddDirectory(const QString& path) = 0;
virtual void AddDirectory(DirectoryManager* directory_manager,
const QString& path) = 0;
virtual void RemoveDirectory(int dir_id) = 0;

virtual bool ExecQuery(LibraryQuery* q) = 0;
Expand All @@ -142,14 +143,14 @@ class LibraryBackend : public LibraryBackendInterface {
QString subdirs_table() const { return subdirs_table_; }

// Get a list of directories in the library. Emits DirectoriesDiscovered.
void LoadDirectoriesAsync();
void LoadDirectoriesAsync(DirectoryManager* directory_manager);

// Counts the songs in the library. Emits TotalSongCountUpdated
void UpdateTotalSongCountAsync();

SongList FindSongsInDirectory(int id);
SubdirectoryList SubdirsInDirectory(int id);
DirectoryList GetAllDirectories();
DirectoryList GetAllDirectories(DirectoryManager* directory_manager);
void ChangeDirPath(int id, const QString& old_path, const QString& new_path);

QStringList GetAll(const QString& column,
Expand Down Expand Up @@ -186,7 +187,7 @@ class LibraryBackend : public LibraryBackendInterface {
SongList GetSongsByUrl(const QUrl& url);
Song GetSongByUrl(const QUrl& url, qint64 beginning = 0);

void AddDirectory(const QString& path);
void AddDirectory(DirectoryManager* directory_manager, const QString& path);
void RemoveDirectory(int dir_id);

bool ExecQuery(LibraryQuery* q);
Expand All @@ -203,7 +204,7 @@ class LibraryBackend : public LibraryBackendInterface {
void DeleteAll();

public slots:
void LoadDirectories();
void LoadDirectories(DirectoryManager* directory_manager);
void UpdateTotalSongCount();
void AddOrUpdateSongs(const SongList& songs);
void UpdateMTimesOnly(const SongList& songs);
Expand Down
8 changes: 5 additions & 3 deletions src/library/librarydirectorymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
#include "ui/iconloader.h"

LibraryDirectoryModel::LibraryDirectoryModel(
std::shared_ptr<LibraryBackend> backend, QObject* parent)
std::shared_ptr<LibraryBackend> backend,
std::shared_ptr<DirectoryManager> directory_manager, QObject* parent)
: QStandardItemModel(parent),
dir_icon_(IconLoader::Load("document-open-folder", IconLoader::Base)),
backend_(backend) {
backend_(backend),
directory_manager_(directory_manager) {
connect(backend_.get(),
SIGNAL(DirectoryDiscovered(Directory, SubdirectoryList)),
SLOT(DirectoryDiscovered(Directory)));
Expand Down Expand Up @@ -67,7 +69,7 @@ void LibraryDirectoryModel::DirectoryDeleted(int dir_id) {
void LibraryDirectoryModel::AddDirectory(const QString& path) {
if (!backend_) return;

backend_->AddDirectory(path);
backend_->AddDirectory(directory_manager_.get(), path);
}

void LibraryDirectoryModel::RemoveDirectory(const QModelIndex& index) {
Expand Down
2 changes: 2 additions & 0 deletions src/library/librarydirectorymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class LibraryDirectoryModel : public QStandardItemModel {

public:
LibraryDirectoryModel(std::shared_ptr<LibraryBackend> backend,
std::shared_ptr<DirectoryManager> directory_manager,
QObject* parent = nullptr);
~LibraryDirectoryModel();

Expand All @@ -51,6 +52,7 @@ class LibraryDirectoryModel : public QStandardItemModel {

QIcon dir_icon_;
std::shared_ptr<LibraryBackend> backend_;
std::shared_ptr<DirectoryManager> directory_manager_;
QList<std::shared_ptr<MusicStorage>> storage_;
};

Expand Down