2020-09-06 19:40:46 +02:00
|
|
|
#include "entryprovider.h"
|
2018-01-03 09:52:05 +01:00
|
|
|
#include <QDebug>
|
2020-09-06 19:40:46 +02:00
|
|
|
#include <QDirIterator>
|
2018-01-03 09:52:05 +01:00
|
|
|
#include <QTextStream>
|
|
|
|
|
2020-09-06 19:40:46 +02:00
|
|
|
EntryProvider::EntryProvider(QStringList userEntriesDirsPaths, QStringList systemEntriesDirsPaths)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-06 19:40:46 +02:00
|
|
|
this->userEntriesDirsPaths = userEntriesDirsPaths;
|
|
|
|
this->systemEntriesDirsPaths = systemEntriesDirsPaths;
|
|
|
|
_desktopIgnoreArgs << "%F"
|
|
|
|
<< "%f"
|
|
|
|
<< "%U"
|
|
|
|
<< "%u";
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 19:40:46 +02:00
|
|
|
EntryConfig EntryProvider::readFromDesktopFile(const QString &path)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
EntryConfig result;
|
|
|
|
QFile file(path);
|
2020-09-06 19:40:46 +02:00
|
|
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-06 19:40:46 +02:00
|
|
|
// TODO: better exception class
|
2018-01-03 09:52:05 +01:00
|
|
|
throw new std::runtime_error("Failed to open file");
|
|
|
|
}
|
|
|
|
QTextStream stream(&file);
|
2020-09-06 19:40:46 +02:00
|
|
|
// 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
|
2019-08-23 23:39:03 +02:00
|
|
|
QString firstLine;
|
|
|
|
do
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-08-23 23:39:03 +02:00
|
|
|
firstLine = stream.readLine().trimmed();
|
2020-09-06 19:40:46 +02:00
|
|
|
} while(!stream.atEnd() && (firstLine.isEmpty() || firstLine[0] == '#'));
|
2019-08-23 23:39:03 +02:00
|
|
|
|
|
|
|
if(firstLine != "[Desktop Entry]")
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-08-23 23:39:03 +02:00
|
|
|
throw ConfigFormatException(".desktop file does not start with [Desktop Entry]: " + path.toStdString());
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while(!stream.atEnd())
|
|
|
|
{
|
|
|
|
QString line = stream.readLine();
|
2020-09-06 19:40:46 +02:00
|
|
|
// new group, so we are finished with [Desktop Entry]
|
2018-01-17 22:36:57 +01:00
|
|
|
if(line.startsWith("[") && line.endsWith("]"))
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2019-08-23 23:39:03 +02:00
|
|
|
|
2020-09-06 19:40:46 +02:00
|
|
|
QString key = line.section('=', 0, 0).toLower();
|
2019-08-23 23:39:03 +02:00
|
|
|
QString args = line.section('=', 1);
|
|
|
|
if(key == "name")
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-08-23 23:39:03 +02:00
|
|
|
if(result.name.length() == 0)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-08-23 23:39:03 +02:00
|
|
|
result.name = args;
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2019-08-23 23:39:03 +02:00
|
|
|
}
|
|
|
|
if(key == "icon")
|
|
|
|
{
|
|
|
|
result.icon = QIcon::fromTheme(args);
|
|
|
|
}
|
|
|
|
if(key == "exec")
|
|
|
|
{
|
|
|
|
QStringList arguments = args.split(" ");
|
2018-01-03 09:52:05 +01:00
|
|
|
|
2019-08-23 23:39:03 +02:00
|
|
|
result.command = arguments[0];
|
|
|
|
arguments = arguments.mid(1);
|
|
|
|
if(arguments.length() > 1)
|
|
|
|
{
|
|
|
|
for(QString &arg : arguments)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-06 19:40:46 +02:00
|
|
|
if(!_desktopIgnoreArgs.contains(arg))
|
2018-01-17 22:36:57 +01:00
|
|
|
{
|
2019-08-23 23:39:03 +02:00
|
|
|
result.arguments.append(arg);
|
2018-01-17 22:36:57 +01:00
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* qsrun own's config file */
|
2020-09-06 19:40:46 +02:00
|
|
|
EntryConfig EntryProvider::readFromFile(const QString &path)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
EntryConfig result;
|
2020-09-06 19:40:46 +02:00
|
|
|
EntryConfig inheritedConfig;
|
2018-01-03 09:52:05 +01:00
|
|
|
QFile file(path);
|
2020-09-06 19:40:46 +02:00
|
|
|
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-06 19:40:46 +02:00
|
|
|
// TODO: better exception class
|
2018-01-03 09:52:05 +01:00
|
|
|
throw new std::runtime_error("Failed to open file");
|
|
|
|
}
|
|
|
|
QTextStream stream(&file);
|
|
|
|
while(!stream.atEnd())
|
|
|
|
{
|
|
|
|
QString line = stream.readLine();
|
|
|
|
QStringList splitted = line.split(" ");
|
|
|
|
if(splitted.length() < 2)
|
|
|
|
{
|
2019-06-09 13:06:14 +02:00
|
|
|
throw new ConfigFormatException("misformated line in .qsrun config file " + path.toStdString());
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
QString key = splitted[0];
|
|
|
|
if(key == "arguments")
|
|
|
|
{
|
2019-06-09 13:06:14 +02:00
|
|
|
auto args = splitted.mid(1);
|
|
|
|
QString merged;
|
|
|
|
for(QString &str : args)
|
|
|
|
{
|
|
|
|
if(str.startsWith('"') && !str.endsWith("'"))
|
|
|
|
{
|
|
|
|
merged += str.mid(1) + " ";
|
|
|
|
}
|
|
|
|
else if(str.endsWith('"'))
|
|
|
|
{
|
|
|
|
str.chop(1);
|
|
|
|
merged += str;
|
|
|
|
result.arguments.append(merged);
|
|
|
|
merged = "";
|
|
|
|
}
|
|
|
|
else if(merged != "")
|
|
|
|
{
|
|
|
|
merged += str + " ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.arguments.append(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(merged != "")
|
|
|
|
{
|
|
|
|
throw ConfigFormatException("non-closed \" in config file " + path.toStdString());
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
if(key == "name")
|
|
|
|
{
|
|
|
|
result.name = splitted[1];
|
|
|
|
}
|
|
|
|
if(key == "icon")
|
|
|
|
{
|
|
|
|
result.icon = QIcon(splitted[1]);
|
|
|
|
}
|
|
|
|
if(key == "row")
|
|
|
|
{
|
2019-08-24 09:19:49 +02:00
|
|
|
result.row = splitted[1].toInt();
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
if(key == "col")
|
|
|
|
{
|
|
|
|
result.col = splitted[1].toInt();
|
|
|
|
}
|
|
|
|
if(key == "command")
|
|
|
|
{
|
|
|
|
result.command = splitted[1];
|
|
|
|
}
|
|
|
|
if(key == "key")
|
|
|
|
{
|
2020-09-06 19:40:46 +02:00
|
|
|
// QKeySequence sequence(splitted[1]);
|
|
|
|
// result.keySequence = sequence;
|
2018-01-03 09:52:05 +01:00
|
|
|
result.key = splitted[1].toLower();
|
|
|
|
}
|
2020-09-06 19:40:46 +02:00
|
|
|
if(key == "inherit")
|
|
|
|
{
|
|
|
|
inheritedConfig = readFromDesktopFile(resolveEntryPath(splitted[1]));
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
2020-09-06 21:37:15 +02:00
|
|
|
return result.update(inheritedConfig);
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-06 19:40:46 +02:00
|
|
|
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)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
|
|
|
QVector<EntryConfig> result;
|
2020-09-06 19:40:46 +02:00
|
|
|
for(QString &configPath : paths)
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-09-01 20:48:39 +02:00
|
|
|
QDirIterator it(configPath, QDirIterator::Subdirectories);
|
2019-05-30 10:57:33 +02:00
|
|
|
while(it.hasNext())
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2019-05-30 10:57:33 +02:00
|
|
|
QString path = it.next();
|
|
|
|
QFileInfo info(path);
|
|
|
|
if(info.isFile())
|
2018-01-03 09:52:05 +01:00
|
|
|
{
|
2020-09-01 21:50:24 +02:00
|
|
|
QString suffix = info.suffix();
|
2019-05-30 10:57:33 +02:00
|
|
|
if(suffix == "desktop")
|
|
|
|
{
|
|
|
|
result.append(readFromDesktopFile(path));
|
|
|
|
}
|
|
|
|
if(suffix == "qsrun")
|
|
|
|
{
|
|
|
|
result.append(readFromFile(path));
|
|
|
|
}
|
2018-01-03 09:52:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-06 19:40:46 +02:00
|
|
|
|
|
|
|
QVector<EntryConfig> EntryProvider::getUserEntries()
|
|
|
|
{
|
|
|
|
return readConfig(this->userEntriesDirsPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<EntryConfig> EntryProvider::getSystemEntries()
|
|
|
|
{
|
|
|
|
return readConfig(this->systemEntriesDirsPaths);
|
|
|
|
}
|
2020-09-06 21:37:15 +02:00
|
|
|
|
|
|
|
template <class T> void assignIfDestDefault(T &dest, const T &source)
|
|
|
|
{
|
|
|
|
if(dest == T())
|
|
|
|
{
|
|
|
|
dest = source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EntryConfig &EntryConfig::update(const EntryConfig &o)
|
|
|
|
{
|
|
|
|
assignIfDestDefault(this->arguments, o.arguments);
|
|
|
|
assignIfDestDefault(this->col, o.col);
|
|
|
|
assignIfDestDefault(this->command, o.command);
|
|
|
|
if(this->icon.isNull())
|
|
|
|
{
|
|
|
|
this->icon = o.icon;
|
|
|
|
}
|
|
|
|
assignIfDestDefault(this->key, o.key);
|
|
|
|
assignIfDestDefault(this->name, o.name);
|
|
|
|
assignIfDestDefault(this->row, o.row);
|
|
|
|
return *this;
|
|
|
|
}
|