diff --git a/kcms/baloo/filteredfoldermodel.h b/kcms/baloo/filteredfoldermodel.h --- a/kcms/baloo/filteredfoldermodel.h +++ b/kcms/baloo/filteredfoldermodel.h @@ -1,6 +1,7 @@ /* * This file is part of the KDE Baloo project * Copyright (C) 2014 Vishesh Handa + * Copyright (c) 2020 Benjamin Port * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -23,15 +24,15 @@ #include +class BalooSettings; + class FilteredFolderModel : public QAbstractListModel { Q_OBJECT public: - explicit FilteredFolderModel(QObject* parent); + explicit FilteredFolderModel(BalooSettings *settings, QObject *parent); - void setDirectoryList(const QStringList& includeDirs, const QStringList& exclude); QStringList includeFolders() const; - QStringList excludeFolders() const; enum Roles { Folder = Qt::UserRole + 1, @@ -44,9 +45,9 @@ Q_INVOKABLE void addFolder(const QString& folder); Q_INVOKABLE void removeFolder(int row); QHash roleNames() const override; -Q_SIGNALS: - void folderAdded(); - void folderRemoved(); + +public slots: + void updateDirectoryList(); private: QString folderDisplayName(const QString& url) const; @@ -62,10 +63,7 @@ */ QString iconName(QString path) const; - /** - * @brief Widget with the list of directories. - * - */ + BalooSettings *m_settings; QStringList m_mountPoints; QStringList m_excludeList; }; diff --git a/kcms/baloo/filteredfoldermodel.cpp b/kcms/baloo/filteredfoldermodel.cpp --- a/kcms/baloo/filteredfoldermodel.cpp +++ b/kcms/baloo/filteredfoldermodel.cpp @@ -2,6 +2,7 @@ * This file is part of the KDE Baloo Project * Copyright (C) 2014 Vishesh Handa * Copyright (C) 2019 Tomaz Canabrava + * Copyright (c) 2020 Benjamin Port * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -33,6 +34,8 @@ #include #include +#include "baloo/baloosettings.h" + namespace { QStringList addTrailingSlashes(const QStringList& input) { QStringList output = input; @@ -52,12 +55,13 @@ } } -FilteredFolderModel::FilteredFolderModel(QObject* parent) +FilteredFolderModel::FilteredFolderModel(BalooSettings *settings, QObject *parent) : QAbstractListModel(parent) + , m_settings(settings) { } -void FilteredFolderModel::setDirectoryList(const QStringList& include, const QStringList& exclude) +void FilteredFolderModel::updateDirectoryList() { beginResetModel(); @@ -79,16 +83,16 @@ m_mountPoints.append(QDir::homePath()); m_mountPoints = addTrailingSlashes(m_mountPoints); - QStringList includeList = addTrailingSlashes(include); + QStringList includeList = addTrailingSlashes(m_settings->folders()); - m_excludeList = addTrailingSlashes(exclude); + m_excludeList = addTrailingSlashes(m_settings->excludedFolders()); // This algorithm seems bogus. verify later. for (const QString& mountPath : m_mountPoints) { if (includeList.contains(mountPath)) continue; - if (exclude.contains(mountPath)) + if (m_settings->excludedFolders().contains(mountPath)) continue; if (!m_excludeList.contains(mountPath)) { @@ -135,11 +139,6 @@ return mountPointSet.values(); } -QStringList FilteredFolderModel::excludeFolders() const -{ - return m_excludeList; -} - QString FilteredFolderModel::fetchMountPoint(const QString& url) const { QString mountPoint; @@ -155,22 +154,29 @@ void FilteredFolderModel::addFolder(const QString& url) { - if (m_excludeList.contains(url)) { + auto excluded = m_settings->excludedFolders(); + if (excluded.contains(url)) { return; } - beginResetModel(); - m_excludeList.append(QUrl(url).toLocalFile()); - std::sort(std::begin(m_excludeList), std::end(m_excludeList)); - endResetModel(); - Q_EMIT folderAdded(); + excluded.append(QUrl(url).toLocalFile()); + std::sort(std::begin(excluded), std::end(excluded)); + m_settings->setExcludedFolders(excluded); } void FilteredFolderModel::removeFolder(int row) { - beginRemoveRows(QModelIndex(), row, row); - m_excludeList.removeAt(row); - endRemoveRows(); - Q_EMIT folderRemoved(); + auto url = m_excludeList.at(row); + auto excluded = addTrailingSlashes(m_settings->excludedFolders()); + auto included = addTrailingSlashes(m_settings->folders()); + if (excluded.contains(url)) { + excluded.removeAll(url); + std::sort(std::begin(excluded), std::end(excluded)); + m_settings->setExcludedFolders(excluded); + } else if (m_mountPoints.contains(url) && !included.contains(url)) { + included.append(url); + std::sort(std::begin(included), std::end(included)); + m_settings->setFolders(included); + } } diff --git a/kcms/baloo/kcm.h b/kcms/baloo/kcm.h --- a/kcms/baloo/kcm.h +++ b/kcms/baloo/kcm.h @@ -1,6 +1,7 @@ /* This file is part of the KDE Project Copyright (c) 2007 Sebastian Trueg Copyright (c) 2012-2014 Vishesh Handa + Copyright (c) 2020 Benjamin Port This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -20,50 +21,43 @@ #ifndef _BALOO_FILE_KCM_H_ #define _BALOO_FILE_KCM_H_ -#include +#include #include "filteredfoldermodel.h" +class BalooSettings; + namespace Baloo { -class ServerConfigModule : public KQuickAddons::ConfigModule +class ServerConfigModule : public KQuickAddons::ManagedConfigModule { Q_OBJECT Q_PROPERTY(FilteredFolderModel *filteredModel READ filteredModel CONSTANT) - Q_PROPERTY(bool indexing READ indexing WRITE setIndexing NOTIFY indexingChanged) - Q_PROPERTY(bool fileContents READ fileContents WRITE setFileContents NOTIFY fileContentsChanged) + Q_PROPERTY(BalooSettings *balooSettings READ balooSettings CONSTANT) public: ServerConfigModule(QObject* parent, const QVariantList& args); virtual ~ServerConfigModule() override; - bool indexing() const; - void setIndexing(bool indexing); - Q_SIGNAL void indexingChanged(bool indexing); - - bool fileContents() const; - void setFileContents(bool fileContents); - Q_SIGNAL void fileContentsChanged(bool fileContents); - + BalooSettings *balooSettings() const; FilteredFolderModel *filteredModel() const; public Q_SLOTS: void load() override; void save() override; - void defaults() override; private: /** * @brief Check if all mount points are in the excluded from indexing list. * * @return True if all mount points are excluded. False otherwise. */ bool allMountPointsExcluded(); + + BalooSettings *m_settings; FilteredFolderModel *m_filteredFolderModel; bool m_previouslyEnabled; - bool m_indexing; - bool m_fileContents; }; } diff --git a/kcms/baloo/kcm.cpp b/kcms/baloo/kcm.cpp --- a/kcms/baloo/kcm.cpp +++ b/kcms/baloo/kcm.cpp @@ -1,6 +1,7 @@ /* This file is part of the KDE Project Copyright (c) 2007-2010 Sebastian Trueg Copyright (c) 2012-2014 Vishesh Handa + Copyright (c) 2020 Benjamin Port This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -23,7 +24,6 @@ #include #include #include -#include #include #include @@ -36,26 +36,19 @@ #include #include +#include K_PLUGIN_FACTORY_WITH_JSON(KCMColorsFactory, "kcm_baloofile.json", registerPlugin();) -namespace -{ - QStringList defaultFolders() - { - return { QDir::homePath() }; - } -} // namespace - using namespace Baloo; ServerConfigModule::ServerConfigModule(QObject* parent, const QVariantList& args) - : KQuickAddons::ConfigModule(parent, args) - , m_filteredFolderModel(new FilteredFolderModel(this)) - , m_indexing(false) - , m_fileContents(false) + : KQuickAddons::ManagedConfigModule(parent, args) + , m_settings(new BalooSettings(this)) + , m_filteredFolderModel(new FilteredFolderModel(m_settings, this)) { qmlRegisterType(); + qmlRegisterType(); KAboutData* about = new KAboutData( QStringLiteral("kcm_baloofile"), i18n("File Search"), @@ -69,44 +62,32 @@ setAboutData(about); setButtons(Help | Apply | Default); - connect(m_filteredFolderModel, &FilteredFolderModel::folderAdded, this, [this]{ setNeedsSave(true); }); - connect(m_filteredFolderModel, &FilteredFolderModel::folderRemoved, this, [this]{ setNeedsSave(true); }); + connect(m_settings, &BalooSettings::excludedFoldersChanged, m_filteredFolderModel, &FilteredFolderModel::updateDirectoryList); + connect(m_settings, &BalooSettings::foldersChanged, m_filteredFolderModel, &FilteredFolderModel::updateDirectoryList); + m_filteredFolderModel->updateDirectoryList(); } ServerConfigModule::~ServerConfigModule() { } void ServerConfigModule::load() { - Baloo::IndexerConfig config; - m_indexing = config.fileIndexingEnabled(); - m_previouslyEnabled = m_indexing; - m_fileContents = !config.onlyBasicIndexing(); - - m_filteredFolderModel->setDirectoryList(config.includeFolders(), config.excludeFolders()); - - emit indexingChanged(m_indexing); - emit fileContentsChanged(m_fileContents); + ManagedConfigModule::load(); + m_previouslyEnabled = m_settings->indexingEnabled(); } void ServerConfigModule::save() { - bool enabled = m_indexing && !allMountPointsExcluded(); + ManagedConfigModule::save(); Baloo::IndexerConfig config; - config.setFileIndexingEnabled(enabled); - config.setIncludeFolders(m_filteredFolderModel->includeFolders()); - config.setExcludeFolders(m_filteredFolderModel->excludeFolders()); - config.setOnlyBasicIndexing(!m_fileContents); - config.setFirstRun(false); - - if (m_previouslyEnabled != enabled) { - config.setFirstRun(true); - } + config.setFirstRun(m_previouslyEnabled != m_settings->indexingEnabled()); + + m_previouslyEnabled = m_settings->indexingEnabled(); // Start Baloo - if (enabled) { + if (m_settings->indexingEnabled() && !allMountPointsExcluded()) { const QString exe = QStandardPaths::findExecutable(QStringLiteral("baloo_file")); QProcess::startDetached(exe, QStringList()); } @@ -129,51 +110,24 @@ config.refresh(); } -void ServerConfigModule::defaults() -{ - m_filteredFolderModel->setDirectoryList(defaultFolders(), QStringList()); -} - -bool ServerConfigModule::indexing() const -{ - return m_indexing; -} - -bool ServerConfigModule::fileContents() const +FilteredFolderModel *ServerConfigModule::filteredModel() const { - return m_fileContents; -} - -FilteredFolderModel *ServerConfigModule::filteredModel() const { return m_filteredFolderModel; } -void ServerConfigModule::setIndexing(bool indexing) -{ - if (m_indexing != indexing) { - m_indexing = indexing; - Q_EMIT indexingChanged(indexing); - setNeedsSave(true); - } -} - -void ServerConfigModule::setFileContents(bool fileContents) -{ - if (m_fileContents != fileContents) { - m_fileContents = fileContents; - Q_EMIT fileContentsChanged(fileContents); - setNeedsSave(true); - } -} - bool ServerConfigModule::allMountPointsExcluded() { QStringList mountPoints; for (const QStorageInfo &si : QStorageInfo::mountedVolumes()) { mountPoints.append(si.rootPath()); } - return m_filteredFolderModel->excludeFolders().toSet() == mountPoints.toSet(); + return m_settings->excludedFolders().toSet() == mountPoints.toSet(); +} + +BalooSettings *ServerConfigModule::balooSettings() const +{ + return m_settings; } #include "kcm.moc" diff --git a/kcms/baloo/package/contents/ui/main.qml b/kcms/baloo/package/contents/ui/main.qml --- a/kcms/baloo/package/contents/ui/main.qml +++ b/kcms/baloo/package/contents/ui/main.qml @@ -42,18 +42,19 @@ QQC2.CheckBox { id: fileSearchEnabled text: i18n("Enable File Search") - checked: kcm.indexing + enabled: !kcm.balooSettings.isImmutable("indexingEnabled") + checked: kcm.balooSettings.indexingEnabled onCheckStateChanged: { - kcm.indexing = checked + kcm.balooSettings.indexingEnabled = checked } } QQC2.CheckBox { id: indexFileContents text: i18n("Also index file content") - enabled: fileSearchEnabled.checked - checked: kcm.fileContents - onCheckStateChanged: kcm.fileContents = checked + enabled: fileSearchEnabled.checked && !kcm.balooSettings.isImmutable("onlyBasicIndexing") + checked: !kcm.balooSettings.onlyBasicIndexing + onCheckStateChanged: kcm.balooSettings.onlyBasicIndexing = !checked } Item { Layout.preferredHeight: Kirigami.Units.gridUnit