Only apply collection directories changes on save

This commit is contained in:
Jonas Kvinge
2024-05-12 21:40:51 +02:00
parent 2953f9eefc
commit 76614bcde0
13 changed files with 283 additions and 124 deletions

View File

@@ -0,0 +1,63 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QStandardItemModel>
#include <QVariant>
#include <QString>
#include "core/iconloader.h"
#include "collectionsettingsdirectorymodel.h"
CollectionSettingsDirectoryModel::CollectionSettingsDirectoryModel(QObject *parent)
: QStandardItemModel(parent),
dir_icon_(IconLoader::Load(QStringLiteral("document-open-folder"))) {}
void CollectionSettingsDirectoryModel::AddDirectory(const QString &path) {
QStandardItem *item = new QStandardItem(path);
item->setIcon(dir_icon_);
appendRow(item);
paths_ << path;
}
void CollectionSettingsDirectoryModel::AddDirectories(const QStringList &paths) {
for (const QString &path : paths) {
AddDirectory(path);
}
}
void CollectionSettingsDirectoryModel::RemoveDirectory(const QModelIndex &idx) {
if (!idx.isValid()) return;
const QString path = data(idx).toString();
removeRow(idx.row());
if (paths_.contains(path)) {
paths_.removeAll(path);
}
}

View File

@@ -0,0 +1,48 @@
/*
* Strawberry Music Player
* Copyright 2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef COLLECTIONSETTINGSDIRECTORYMODEL_H
#define COLLECTIONSETTINGSDIRECTORYMODEL_H
#include "config.h"
#include <QStandardItemModel>
#include <QVariant>
#include <QString>
#include <QStringList>
#include <QIcon>
class CollectionSettingsDirectoryModel : public QStandardItemModel {
Q_OBJECT
public:
explicit CollectionSettingsDirectoryModel(QObject *parent = nullptr);
void AddDirectory(const QString &path);
void AddDirectories(const QStringList &paths);
void RemoveDirectory(const QModelIndex &idx);
QStringList paths() const { return paths_; }
private:
QIcon dir_icon_;
QStringList paths_;
};
#endif // COLLECTIONSETTINGSDIRECTORYMODEL_H

View File

@@ -2,7 +2,7 @@
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2018-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@
#include <QItemSelectionModel>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QFileDialog>
#include <QCheckBox>
#include <QLineEdit>
@@ -47,9 +48,12 @@
#include "utilities/strutils.h"
#include "utilities/timeutils.h"
#include "collection/collection.h"
#include "collection/collectionbackend.h"
#include "collection/collectionmodel.h"
#include "collection/collectiondirectory.h"
#include "collection/collectiondirectorymodel.h"
#include "collectionsettingspage.h"
#include "collectionsettingsdirectorymodel.h"
#include "playlist/playlistdelegates.h"
#include "settings/settingsdialog.h"
#include "settings/settingspage.h"
@@ -67,6 +71,9 @@ const int CollectionSettingsPage::kSettingsDiskCacheSizeDefault = 360;
CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog, QWidget *parent)
: SettingsPage(dialog, parent),
ui_(new Ui_CollectionSettingsPage),
collection_backend_(dialog->app()->collection_backend()),
collectionsettings_directory_model_(new CollectionSettingsDirectoryModel(this)),
collection_directory_model_(dialog->collection_directory_model()),
initialized_model_(false) {
ui_->setupUi(this);
@@ -74,7 +81,7 @@ CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog, QWidget *
// Icons
setWindowIcon(IconLoader::Load(QStringLiteral("library-music"), true, 0, 32));
ui_->add->setIcon(IconLoader::Load(QStringLiteral("document-open-folder")));
ui_->add_directory->setIcon(IconLoader::Load(QStringLiteral("document-open-folder")));
ui_->combobox_cache_size->addItem(QStringLiteral("KB"), static_cast<int>(CacheSizeUnit::KB));
ui_->combobox_cache_size->addItem(QStringLiteral("MB"), static_cast<int>(CacheSizeUnit::MB));
@@ -83,8 +90,8 @@ CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog, QWidget *
ui_->combobox_disk_cache_size->addItem(QStringLiteral("MB"), static_cast<int>(CacheSizeUnit::MB));
ui_->combobox_disk_cache_size->addItem(QStringLiteral("GB"), static_cast<int>(CacheSizeUnit::GB));
QObject::connect(ui_->add, &QPushButton::clicked, this, &CollectionSettingsPage::Add);
QObject::connect(ui_->remove, &QPushButton::clicked, this, &CollectionSettingsPage::Remove);
QObject::connect(ui_->add_directory, &QPushButton::clicked, this, &CollectionSettingsPage::AddDirectory);
QObject::connect(ui_->remove_directory, &QPushButton::clicked, this, &CollectionSettingsPage::RemoveDirectory);
#ifdef HAVE_SONGFINGERPRINTING
QObject::connect(ui_->song_tracking, &QCheckBox::toggled, this, &CollectionSettingsPage::SongTrackingToggled);
@@ -111,56 +118,6 @@ CollectionSettingsPage::CollectionSettingsPage(SettingsDialog *dialog, QWidget *
CollectionSettingsPage::~CollectionSettingsPage() { delete ui_; }
void CollectionSettingsPage::Add() {
Settings s;
s.beginGroup(kSettingsGroup);
QString path(s.value("last_path", QStandardPaths::writableLocation(QStandardPaths::MusicLocation)).toString());
path = QFileDialog::getExistingDirectory(this, tr("Add directory..."), path);
if (!path.isEmpty()) {
dialog()->collection_directory_model()->AddDirectory(path);
}
s.setValue("last_path", path);
set_changed();
}
void CollectionSettingsPage::Remove() {
dialog()->collection_directory_model()->RemoveDirectory(ui_->list->currentIndex());
set_changed();
}
void CollectionSettingsPage::CurrentRowChanged(const QModelIndex &idx) {
ui_->remove->setEnabled(idx.isValid());
}
void CollectionSettingsPage::SongTrackingToggled() {
ui_->mark_songs_unavailable->setEnabled(!ui_->song_tracking->isChecked());
if (ui_->song_tracking->isChecked()) {
ui_->mark_songs_unavailable->setChecked(true);
}
}
void CollectionSettingsPage::DiskCacheEnable(const int state) {
bool checked = state == Qt::Checked;
ui_->label_disk_cache_size->setEnabled(checked);
ui_->spinbox_disk_cache_size->setEnabled(checked);
ui_->combobox_disk_cache_size->setEnabled(checked);
ui_->label_disk_cache_in_use->setEnabled(checked);
ui_->disk_cache_in_use->setEnabled(checked);
ui_->button_clear_disk_cache->setEnabled(checked);
}
void CollectionSettingsPage::Load() {
if (!initialized_model_) {
@@ -168,12 +125,17 @@ void CollectionSettingsPage::Load() {
QObject::disconnect(ui_->list->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &CollectionSettingsPage::CurrentRowChanged);
}
ui_->list->setModel(dialog()->collection_directory_model());
ui_->list->setModel(collectionsettings_directory_model_);
initialized_model_ = true;
QObject::connect(ui_->list->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &CollectionSettingsPage::CurrentRowChanged);
}
ui_->list->model()->removeRows(0, ui_->list->model()->rowCount());
for (const QString &path : collection_directory_model_->paths()) {
collectionsettings_directory_model_->AddDirectory(path);
}
Settings s;
s.beginGroup(kSettingsGroup);
@@ -261,6 +223,69 @@ void CollectionSettingsPage::Save() {
s.endGroup();
for (const CollectionDirectory &dir : collection_directory_model_->directories()) {
if (!collectionsettings_directory_model_->paths().contains(dir.path)) {
collection_backend_->RemoveDirectoryAsync(dir);
}
}
for (const QString &path : collectionsettings_directory_model_->paths()) {
if (!collection_directory_model_->paths().contains(path)) {
collection_backend_->AddDirectoryAsync(path);
}
}
}
void CollectionSettingsPage::AddDirectory() {
Settings s;
s.beginGroup(kSettingsGroup);
QString path = s.value("last_path", QStandardPaths::writableLocation(QStandardPaths::MusicLocation)).toString();
path = QDir::cleanPath(QFileDialog::getExistingDirectory(this, tr("Add directory..."), path));
if (!path.isEmpty()) {
collectionsettings_directory_model_->AddDirectory(path);
}
s.setValue("last_path", path);
set_changed();
}
void CollectionSettingsPage::RemoveDirectory() {
collectionsettings_directory_model_->RemoveDirectory(ui_->list->currentIndex());
set_changed();
}
void CollectionSettingsPage::CurrentRowChanged(const QModelIndex &idx) {
ui_->remove_directory->setEnabled(idx.isValid());
}
void CollectionSettingsPage::SongTrackingToggled() {
ui_->mark_songs_unavailable->setEnabled(!ui_->song_tracking->isChecked());
if (ui_->song_tracking->isChecked()) {
ui_->mark_songs_unavailable->setChecked(true);
}
}
void CollectionSettingsPage::DiskCacheEnable(const int state) {
bool checked = state == Qt::Checked;
ui_->label_disk_cache_size->setEnabled(checked);
ui_->spinbox_disk_cache_size->setEnabled(checked);
ui_->combobox_disk_cache_size->setEnabled(checked);
ui_->label_disk_cache_in_use->setEnabled(checked);
ui_->disk_cache_in_use->setEnabled(checked);
ui_->button_clear_disk_cache->setEnabled(checked);
}
void CollectionSettingsPage::ClearPixmapDiskCache() {

View File

@@ -2,7 +2,7 @@
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
* Copyright 2018-2024, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -30,8 +30,13 @@
#include "settingspage.h"
#include "core/shared_ptr.h"
class QModelIndex;
class SettingsDialog;
class CollectionBackend;
class CollectionDirectoryModel;
class CollectionSettingsDirectoryModel;
class Ui_CollectionSettingsPage;
class CollectionSettingsPage : public SettingsPage {
@@ -61,8 +66,8 @@ class CollectionSettingsPage : public SettingsPage {
void Save() override;
private slots:
void Add();
void Remove();
void AddDirectory();
void RemoveDirectory();
void CurrentRowChanged(const QModelIndex &idx);
void SongTrackingToggled();
@@ -74,6 +79,9 @@ class CollectionSettingsPage : public SettingsPage {
private:
Ui_CollectionSettingsPage *ui_;
SharedPtr<CollectionBackend> collection_backend_;
CollectionSettingsDirectoryModel *collectionsettings_directory_model_;
CollectionDirectoryModel *collection_directory_model_;
bool initialized_model_;
};

View File

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>519</width>
<height>920</height>
<width>565</width>
<height>973</height>
</rect>
</property>
<property name="windowTitle">
@@ -39,7 +39,7 @@
<item>
<layout class="QVBoxLayout" name="layout_collection_folder_buttons">
<item>
<widget class="QPushButton" name="add">
<widget class="QPushButton" name="add_directory">
<property name="text">
<string>Add new folder...</string>
</property>
@@ -49,7 +49,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="remove">
<widget class="QPushButton" name="remove_directory">
<property name="text">
<string>Remove folder</string>
</property>
@@ -58,7 +58,7 @@
<item>
<spacer name="spacer_collection_buttons">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -167,7 +167,7 @@
<item>
<spacer name="spacer_expire_unavailable_songs">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -288,7 +288,7 @@ If there are no matches then it will use the largest image in the directory.</st
<item row="0" column="4">
<spacer name="spacer_cache_size">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -312,7 +312,7 @@ If there are no matches then it will use the largest image in the directory.</st
<item>
<spacer name="spacer_disk_cache">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -371,7 +371,7 @@ If there are no matches then it will use the largest image in the directory.</st
<item row="0" column="4">
<spacer name="spacer_disk_cache_size">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -415,7 +415,7 @@ If there are no matches then it will use the largest image in the directory.</st
<item>
<spacer name="spacer_disk_cache_in_use">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -476,7 +476,7 @@ If there are no matches then it will use the largest image in the directory.</st
<item>
<spacer name="spacer_save_stats">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@@ -502,8 +502,8 @@ If there are no matches then it will use the largest image in the directory.</st
</widget>
<tabstops>
<tabstop>list</tabstop>
<tabstop>add</tabstop>
<tabstop>remove</tabstop>
<tabstop>add_directory</tabstop>
<tabstop>remove_directory</tabstop>
<tabstop>startup_scan</tabstop>
<tabstop>monitor</tabstop>
<tabstop>song_tracking</tabstop>