From f03e247bd89258fcfa04fea9ab901a5fe979fd55 Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 6 Sep 2020 19:40:46 +0200 Subject: [PATCH] merge configreader/configprovider into entryprovider settings-related methods that don't have any relation to entries will be moved to a seperate file settingsprovider --- config.h | 59 ------------------ configprovider.cpp | 25 -------- configprovider.h | 18 ------ config.cpp => entryprovider.cpp | 104 +++++++++++++++++++------------- entryprovider.h | 46 ++++++++++++++ 5 files changed, 109 insertions(+), 143 deletions(-) delete mode 100644 config.h delete mode 100644 configprovider.cpp delete mode 100644 configprovider.h rename config.cpp => entryprovider.cpp (59%) create mode 100644 entryprovider.h diff --git a/config.h b/config.h deleted file mode 100644 index 3395e5d..0000000 --- a/config.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2018-2019 Albert S. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#ifndef CONFIG_H -#define CONFIG_H -#include -#include -#include -#include -#include -#include -#include -class EntryConfig -{ -public: - QString key; - QString name; - QString command; - QStringList arguments; - QIcon icon; - int row=0; - int col=0; - - - -}; - -class ConfigReader -{ -private: - QStringList configPaths; - EntryConfig readFromFile(const QString &path); - EntryConfig readFromDesktopFile(const QString &path); - QStringList desktopIgnoreArgs; -public: - ConfigReader(QStringList paths); - QVector readConfig(); -}; - -class ConfigFormatException : public std::runtime_error -{ -public: - ConfigFormatException() : std::runtime_error("Error in configuration file, misformated line?") {}; - ConfigFormatException(const std::string &str) : std::runtime_error(str) {}; -}; - -#endif diff --git a/configprovider.cpp b/configprovider.cpp deleted file mode 100644 index 743c3f4..0000000 --- a/configprovider.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "configprovider.h" - -ConfigProvider::ConfigProvider(QString configDirPath, QSettings &settings) -{ - this->settings = &settings; - this->configDirPath = configDirPath; -} - -QVector ConfigProvider::getUserEntries() -{ - ConfigReader reader({this->configDirPath}); - return reader.readConfig(); -} - -QVector ConfigProvider::getSystemEntries() -{ - QStringList systemApplicationsPaths = settings->value("sysAppsPaths", "/usr/share/applications/").toStringList(); - ConfigReader systemConfigReader(systemApplicationsPaths); - return systemConfigReader.readConfig(); -} - -bool ConfigProvider::singleInstanceMode() -{ - return settings->value("singleInstance", true).toBool(); -} diff --git a/configprovider.h b/configprovider.h deleted file mode 100644 index 3c3e549..0000000 --- a/configprovider.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef CONFIGPROVIDER_H -#define CONFIGPROVIDER_H -#include -#include "config.h" - -class ConfigProvider -{ -private: - QSettings *settings; - QString configDirPath; -public: - ConfigProvider(QString configDirPath, QSettings &settings); - QVector getUserEntries(); - QVector getSystemEntries(); - bool singleInstanceMode(); -}; - -#endif // CONFIGPROVIDER_H diff --git a/config.cpp b/entryprovider.cpp similarity index 59% rename from config.cpp rename to entryprovider.cpp index 757da9b..99172a4 100644 --- a/config.cpp +++ b/entryprovider.cpp @@ -1,48 +1,35 @@ -/* - * Copyright (c) 2018-2020 Albert S. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#include "config.h" -#include +#include "entryprovider.h" #include +#include #include - -ConfigReader::ConfigReader(QStringList paths) +EntryProvider::EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths) { - this->configPaths = paths; - desktopIgnoreArgs << "%F" << "%f" << "%U" << "%u"; + this->userEntriesDirsPaths = userEntriesDirsPaths; + this->systemEntriesDirsPaths = systemEntriesDirsPaths; + _desktopIgnoreArgs << "%F" + << "%f" + << "%U" + << "%u"; } - -EntryConfig ConfigReader::readFromDesktopFile(const QString &path) +EntryConfig EntryProvider::readFromDesktopFile(const QString &path) { EntryConfig result; QFile file(path); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - //TODO: better exception class + // TODO: better exception class throw new std::runtime_error("Failed to open file"); } QTextStream stream(&file); - //There should be nothing preceding this group in the desktop entry file but possibly one or more comments. - //https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html#group-header + // There should be nothing preceding this group in the desktop entry file but possibly one or more comments. + // https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html#group-header QString firstLine; do { firstLine = stream.readLine().trimmed(); - } while(!stream.atEnd() && ( firstLine.isEmpty() || firstLine[0] == '#') ); + } while(!stream.atEnd() && (firstLine.isEmpty() || firstLine[0] == '#')); if(firstLine != "[Desktop Entry]") { @@ -52,13 +39,13 @@ EntryConfig ConfigReader::readFromDesktopFile(const QString &path) while(!stream.atEnd()) { QString line = stream.readLine(); - //new group, so we are finished with [Desktop Entry] + // new group, so we are finished with [Desktop Entry] if(line.startsWith("[") && line.endsWith("]")) { return result; } - QString key = line.section('=',0,0).toLower(); + QString key = line.section('=', 0, 0).toLower(); QString args = line.section('=', 1); if(key == "name") { @@ -81,7 +68,7 @@ EntryConfig ConfigReader::readFromDesktopFile(const QString &path) { for(QString &arg : arguments) { - if(!desktopIgnoreArgs.contains(arg)) + if(!_desktopIgnoreArgs.contains(arg)) { result.arguments.append(arg); } @@ -93,13 +80,14 @@ EntryConfig ConfigReader::readFromDesktopFile(const QString &path) } /* qsrun own's config file */ -EntryConfig ConfigReader::readFromFile(const QString &path) +EntryConfig EntryProvider::readFromFile(const QString &path) { EntryConfig result; + EntryConfig inheritedConfig; QFile file(path); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - //TODO: better exception class + // TODO: better exception class throw new std::runtime_error("Failed to open file"); } QTextStream stream(&file); @@ -165,18 +153,45 @@ EntryConfig ConfigReader::readFromFile(const QString &path) } if(key == "key") { - //QKeySequence sequence(splitted[1]); - //result.keySequence = sequence; + // QKeySequence sequence(splitted[1]); + // result.keySequence = sequence; result.key = splitted[1].toLower(); } + if(key == "inherit") + { + inheritedConfig = readFromDesktopFile(resolveEntryPath(splitted[1])); + } } return result; } -QVector ConfigReader::readConfig() +QString EntryProvider::resolveEntryPath(QString path) +{ + if(path.trimmed().isEmpty()) + { + return {}; + } + if(path[0] == '/') + { + return path; + } + QStringList paths = this->userEntriesDirsPaths + this->systemEntriesDirsPaths; + for(QString &configPath : paths) + { + QDir dir(configPath); + QString desktopFilePath = dir.absoluteFilePath(path); + if(QFileInfo::exists(desktopFilePath)) + { + return desktopFilePath; + } + } + return {}; +} + +QVector EntryProvider::readConfig(QStringList paths) { QVector result; - for(QString &configPath : configPaths) + for(QString &configPath : paths) { QDirIterator it(configPath, QDirIterator::Subdirectories); while(it.hasNext()) @@ -189,7 +204,6 @@ QVector ConfigReader::readConfig() if(suffix == "desktop") { result.append(readFromDesktopFile(path)); - } if(suffix == "qsrun") { @@ -197,8 +211,16 @@ QVector ConfigReader::readConfig() } } } - } - return result; } + +QVector EntryProvider::getUserEntries() +{ + return readConfig(this->userEntriesDirsPaths); +} + +QVector EntryProvider::getSystemEntries() +{ + return readConfig(this->systemEntriesDirsPaths); +} diff --git a/entryprovider.h b/entryprovider.h new file mode 100644 index 0000000..ccbb99e --- /dev/null +++ b/entryprovider.h @@ -0,0 +1,46 @@ +#ifndef ENTRYPROVIDER_H +#define ENTRYPROVIDER_H +#include +#include + +class ConfigFormatException : public std::runtime_error +{ + public: + ConfigFormatException() : std::runtime_error("Error in configuration file, misformated line?") + { + } + ConfigFormatException(const std::string &str) : std::runtime_error(str) + { + } +}; + +class EntryConfig +{ + public: + QString key; + QString name; + QString command; + QStringList arguments; + QIcon icon; + int row = 0; + int col = 0; +}; + +class EntryProvider +{ + protected: + QStringList _desktopIgnoreArgs; + QStringList userEntriesDirsPaths; + QStringList systemEntriesDirsPaths; + EntryConfig readFromFile(const QString &path); + EntryConfig readFromDesktopFile(const QString &path); + QVector readConfig(QStringList paths); + QString resolveEntryPath(QString path); + + public: + EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths); + QVector getUserEntries(); + QVector getSystemEntries(); +}; + +#endif // ENTRYPROVIDER_H