Commit 661b40c9 authored by Benjamin Port's avatar Benjamin Port

KCM Baloo: Migrate to KConfigXT and add immutability

Summary: File indexing and Content file indexing benefits from immutability. But exclude/include list don't benefits from it because the UI represent both with the same list

Reviewers: ervin, crossi, meven, #plasma

Subscribers: meven, crossi, ervin, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D26401
parent 05d380ac
......@@ -2,6 +2,7 @@
* This file is part of the KDE Baloo Project
* Copyright (C) 2014 Vishesh Handa <me@vhanda.in>
* Copyright (C) 2019 Tomaz Canabrava <tcanabrava@kde.org>
* Copyright (c) 2020 Benjamin Port <benjamin.port@enioka.com>
*
* 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 <KLocalizedString>
#include <QStringList>
#include "baloo/baloosettings.h"
namespace {
QStringList addTrailingSlashes(const QStringList& input) {
QStringList output = input;
......@@ -52,12 +55,13 @@ namespace {
}
}
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 @@ void FilteredFolderModel::setDirectoryList(const QStringList& include, const QSt
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 @@ QStringList FilteredFolderModel::includeFolders() const
return mountPointSet.values();
}
QStringList FilteredFolderModel::excludeFolders() const
{
return m_excludeList;
}
QString FilteredFolderModel::fetchMountPoint(const QString& url) const
{
QString mountPoint;
......@@ -155,22 +154,29 @@ QString FilteredFolderModel::fetchMountPoint(const QString& url) const
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);
}
}
......
/*
* This file is part of the KDE Baloo project
* Copyright (C) 2014 Vishesh Handa <me@vhanda.in>
* Copyright (c) 2020 Benjamin Port <benjamin.port@enioka.com>
*
* 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 <QAbstractListModel>
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 @@ public:
Q_INVOKABLE void addFolder(const QString& folder);
Q_INVOKABLE void removeFolder(int row);
QHash<int, QByteArray> roleNames() const override;
Q_SIGNALS:
void folderAdded();
void folderRemoved();
public slots:
void updateDirectoryList();
private:
QString folderDisplayName(const QString& url) const;
......@@ -62,10 +63,7 @@ private:
*/
QString iconName(QString path) const;
/**
* @brief Widget with the list of directories.
*
*/
BalooSettings *m_settings;
QStringList m_mountPoints;
QStringList m_excludeList;
};
......
/* This file is part of the KDE Project
Copyright (c) 2007-2010 Sebastian Trueg <trueg@kde.org>
Copyright (c) 2012-2014 Vishesh Handa <me@vhanda.in>
Copyright (c) 2020 Benjamin Port <benjamin.port@enioka.com>
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 <KPluginFactory>
#include <KPluginLoader>
#include <KAboutData>
#include <QDebug>
#include <QStandardPaths>
#include <KLocalizedString>
......@@ -36,26 +36,19 @@
#include <QStorageInfo>
#include <Baloo/IndexerConfig>
#include <baloo/baloosettings.h>
K_PLUGIN_FACTORY_WITH_JSON(KCMColorsFactory, "kcm_baloofile.json", registerPlugin<Baloo::ServerConfigModule>();)
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<FilteredFolderModel>();
qmlRegisterType<BalooSettings>();
KAboutData* about = new KAboutData(
QStringLiteral("kcm_baloofile"), i18n("File Search"),
......@@ -69,8 +62,9 @@ ServerConfigModule::ServerConfigModule(QObject* parent, const QVariantList& args
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()
......@@ -79,34 +73,21 @@ 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,43 +110,11 @@ void ServerConfigModule::save()
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;
......@@ -173,7 +122,12 @@ bool ServerConfigModule::allMountPointsExcluded()
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"
/* This file is part of the KDE Project
Copyright (c) 2007 Sebastian Trueg <trueg@kde.org>
Copyright (c) 2012-2014 Vishesh Handa <me@vhanda.in>
Copyright (c) 2020 Benjamin Port <benjamin.port@enioka.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -20,38 +21,31 @@
#ifndef _BALOO_FILE_KCM_H_
#define _BALOO_FILE_KCM_H_
#include <KQuickAddons/ConfigModule>
#include <KQuickAddons/ManagedConfigModule>
#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:
/**
......@@ -60,10 +54,10 @@ private:
* @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;
};
}
......
......@@ -42,18 +42,19 @@ KCM.SimpleKCM {
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment