parser improvements: unusual (maybe non-conforming) .desktop files, bugfixes for situation where '=' in value

This commit is contained in:
Albert S. 2019-08-23 23:39:03 +02:00
rodzic 65212a1aa7
commit 4c4b5280b9
1 zmienionych plików z 35 dodań i 36 usunięć

Wyświetl plik

@ -36,17 +36,17 @@ EntryConfig ConfigReader::readFromDesktopFile(const QString &path)
throw new std::runtime_error("Failed to open file"); throw new std::runtime_error("Failed to open file");
} }
QTextStream stream(&file); QTextStream stream(&file);
QString firstline = stream.readLine();
//There should be nothing preceding this group in the desktop entry file but possibly one or more comments. //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 //https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html#group-header
while(firstline[0] == '#' && !stream.atEnd()) QString firstLine;
do
{ {
firstline = stream.readLine(); firstLine = stream.readLine().trimmed();
} } while(!stream.atEnd() && ( firstLine.isEmpty() || firstLine[0] == '#') );
if(firstline != "[Desktop Entry]")
if(firstLine != "[Desktop Entry]")
{ {
throw ConfigFormatException(".desktop file does not start with [Desktop Entry]"); throw ConfigFormatException(".desktop file does not start with [Desktop Entry]: " + path.toStdString());
} }
while(!stream.atEnd()) while(!stream.atEnd())
@ -57,24 +57,23 @@ EntryConfig ConfigReader::readFromDesktopFile(const QString &path)
{ {
return result; return result;
} }
QStringList splitted = line.split("=");
if(splitted.length() >= 2) QString key = line.section('=',0,0).toLower();
{ QString args = line.section('=', 1);
QString key = splitted[0].toLower();
if(key == "name") if(key == "name")
{ {
if(result.name.length() == 0) if(result.name.length() == 0)
{ {
result.name = splitted[1]; result.name = args;
} }
} }
if(key == "icon") if(key == "icon")
{ {
result.icon = QIcon::fromTheme(splitted[1]); result.icon = QIcon::fromTheme(args);
} }
if(key == "exec") if(key == "exec")
{ {
QStringList arguments = splitted[1].split(" "); QStringList arguments = args.split(" ");
result.command = arguments[0]; result.command = arguments[0];
arguments = arguments.mid(1); arguments = arguments.mid(1);
@ -88,7 +87,7 @@ EntryConfig ConfigReader::readFromDesktopFile(const QString &path)
} }
} }
} }
}
} }