ConfigReader: support multiple dirs

This commit is contained in:
Albert S. 2019-05-30 10:57:33 +02:00
parent 3970019fac
commit 8484e88a27
3 changed files with 25 additions and 35 deletions

View File

@ -18,9 +18,10 @@
#include <QDebug> #include <QDebug>
#include <QTextStream> #include <QTextStream>
ConfigReader::ConfigReader(QString directory)
ConfigReader::ConfigReader(QStringList paths)
{ {
this->configDirectory = directory; this->configPaths = paths;
desktopIgnoreArgs << "%F" << "%f" << "%U" << "%u"; desktopIgnoreArgs << "%F" << "%f" << "%U" << "%u";
} }
@ -153,24 +154,28 @@ EntryConfig ConfigReader::readFromFile(const QString &path)
QVector<EntryConfig> ConfigReader::readConfig() QVector<EntryConfig> ConfigReader::readConfig()
{ {
QVector<EntryConfig> result; QVector<EntryConfig> result;
QDirIterator it(this->configDirectory); for(QString &configPath : configPaths)
while(it.hasNext())
{ {
QString path = it.next(); QDirIterator it(configPath);
QFileInfo info(path); while(it.hasNext())
if(info.isFile())
{ {
QString suffix = info.completeSuffix(); QString path = it.next();
if(suffix == "desktop") QFileInfo info(path);
if(info.isFile())
{ {
result.append(readFromDesktopFile(path)); QString suffix = info.completeSuffix();
if(suffix == "desktop")
{
result.append(readFromDesktopFile(path));
} }
if(suffix == "qsrun") if(suffix == "qsrun")
{ {
result.append(readFromFile(path)); result.append(readFromFile(path));
}
} }
} }
} }
return result; return result;

View File

@ -40,12 +40,12 @@ class EntryConfig
class ConfigReader class ConfigReader
{ {
private: private:
QString configDirectory; QStringList configPaths;
EntryConfig readFromFile(const QString &path); EntryConfig readFromFile(const QString &path);
EntryConfig readFromDesktopFile(const QString &path); EntryConfig readFromDesktopFile(const QString &path);
QStringList desktopIgnoreArgs; QStringList desktopIgnoreArgs;
public: public:
ConfigReader(QString path); ConfigReader(QStringList paths);
QVector<EntryConfig> readConfig(); QVector<EntryConfig> readConfig();
}; };

View File

@ -37,20 +37,18 @@ int main(int argc, char *argv[])
} }
qRegisterMetaType<QVector<QString> >("QVector<QString>"); qRegisterMetaType<QVector<QString> >("QVector<QString>");
QDir dir(configDirectoryPath); QDir dir;
if(!dir.exists(configDirectoryPath)) if(!dir.exists(configDirectoryPath))
{ {
QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found!"); QMessageBox::warning(nullptr, "Directory not found", configDirectoryPath + " was not found!");
return 1; return 1;
} }
QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat); QSettings settings(configDirectoryPath + "qsrun.config", QSettings::NativeFormat);
QString systemApplicationsPath = settings.value("General/systemApplicationsPath", "/usr/share/applications/").toString();
QVector<EntryConfig> configs; QVector<EntryConfig> configs;
try try
{ {
ConfigReader reader(configDirectoryPath); ConfigReader reader({configDirectoryPath});
configs = reader.readConfig(); configs = reader.readConfig();
} }
catch(std::exception &e) catch(std::exception &e)
@ -59,23 +57,10 @@ int main(int argc, char *argv[])
} }
Window w(configs); Window w(configs);
/*
* TODO: Reconsider the need
* QFuture<void> future = QtConcurrent::run([&w] {
ConfigReader systemConfigReader("/usr/share/applications/");
QList<EntryConfig> systemconfigs = systemConfigReader.readConfig();
if(systemconfigs.count() > 0)
{
w.setSystemConfig(systemconfigs);
w.systemConfigReady();
}
});*/
try try
{ {
ConfigReader systemConfigReader(systemApplicationsPath); QStringList systemApplicationsPaths = settings.value("sysAppsPaths", "/usr/share/applications/").toStringList();
ConfigReader systemConfigReader(systemApplicationsPaths);
QVector<EntryConfig> systemconfigs = systemConfigReader.readConfig(); QVector<EntryConfig> systemconfigs = systemConfigReader.readConfig();
if(systemconfigs.count() > 0) if(systemconfigs.count() > 0)
{ {