merge configreader/configprovider into entryprovider

settings-related methods that don't have any relation
to entries will be moved to a seperate file
settingsprovider
This commit is contained in:
Albert S. 2020-09-06 19:40:46 +02:00
parent 5b99d764f0
commit f03e247bd8
5 changed files with 109 additions and 143 deletions

View File

@ -1,59 +0,0 @@
/*
* Copyright (c) 2018-2019 Albert S. <mail at quitesimple dot org>
*
* 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 <exception>
#include <QFile>
#include <QDir>
#include <QString>
#include <QStringList>
#include <QIcon>
#include <QKeySequence>
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<EntryConfig> 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

View File

@ -1,25 +0,0 @@
#include "configprovider.h"
ConfigProvider::ConfigProvider(QString configDirPath, QSettings &settings)
{
this->settings = &settings;
this->configDirPath = configDirPath;
}
QVector<EntryConfig> ConfigProvider::getUserEntries()
{
ConfigReader reader({this->configDirPath});
return reader.readConfig();
}
QVector<EntryConfig> 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();
}

View File

@ -1,18 +0,0 @@
#ifndef CONFIGPROVIDER_H
#define CONFIGPROVIDER_H
#include <QSettings>
#include "config.h"
class ConfigProvider
{
private:
QSettings *settings;
QString configDirPath;
public:
ConfigProvider(QString configDirPath, QSettings &settings);
QVector<EntryConfig> getUserEntries();
QVector<EntryConfig> getSystemEntries();
bool singleInstanceMode();
};
#endif // CONFIGPROVIDER_H

View File

@ -1,48 +1,35 @@
/*
* Copyright (c) 2018-2020 Albert S. <mail at quitesimple dot org>
*
* 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 <QDirIterator>
#include "entryprovider.h"
#include <QDebug>
#include <QDirIterator>
#include <QTextStream>
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<EntryConfig> 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<EntryConfig> EntryProvider::readConfig(QStringList paths)
{
QVector<EntryConfig> result;
for(QString &configPath : configPaths)
for(QString &configPath : paths)
{
QDirIterator it(configPath, QDirIterator::Subdirectories);
while(it.hasNext())
@ -189,7 +204,6 @@ QVector<EntryConfig> ConfigReader::readConfig()
if(suffix == "desktop")
{
result.append(readFromDesktopFile(path));
}
if(suffix == "qsrun")
{
@ -197,8 +211,16 @@ QVector<EntryConfig> ConfigReader::readConfig()
}
}
}
}
return result;
}
QVector<EntryConfig> EntryProvider::getUserEntries()
{
return readConfig(this->userEntriesDirsPaths);
}
QVector<EntryConfig> EntryProvider::getSystemEntries()
{
return readConfig(this->systemEntriesDirsPaths);
}

46
entryprovider.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef ENTRYPROVIDER_H
#define ENTRYPROVIDER_H
#include <QIcon>
#include <QSettings>
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<EntryConfig> readConfig(QStringList paths);
QString resolveEntryPath(QString path);
public:
EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths);
QVector<EntryConfig> getUserEntries();
QVector<EntryConfig> getSystemEntries();
};
#endif // ENTRYPROVIDER_H