KeyFile
A file of the XDG key-file format that keeps every line it does not change.
In the namespace platform. Declared in src/libs/libplatform/platform/KeyFile.hpp.
Detailed description
The format is the one of the Desktop Entry Specification ("Basic format of the file"): `[Group]` lines, `Key=Value` lines, comment lines that start with `#`, and empty lines. A desktop entry and a `mimeapps.list` file both use it.
Each line is kept as it was read. setRawValue() replaces the line of a key, or adds one line at the end of its group, and remove() takes one line away. text() then gives every other line back as it was, comments and order included. A line that is no group, entry, comment or empty line is kept too, and invalidLineCount() counts it.
A value is kept raw. unescape() undoes the escapes of the string type, and unescapeList() splits a list at its `;` separators.
See also platform::DesktopEntry, platform::MimeAppsList.
Member function documentation
contains
bool KeyFile::contains(const QString &group, const QString &key) const
Returns true when group has the key key.
escape
[static] QString KeyFile::escape(const QString &value)
Returns value escaped as the string type wants it.
The reverse of unescape(): a backslash, a newline, a tab and a carriage return are escaped, and so is a space at the start, which the reader would otherwise drop.
escapeList
[static] QString KeyFile::escapeList(const QStringList &values)
Returns values as a raw list value: each item escaped, a `;` after each, the last one included, as `xdg-mime` writes it.
fromFile
[static] platform::KeyFile KeyFile::fromFile(const QString &path, bool *found = nullptr, QString *error = nullptr)
Reads the file at path as UTF-8.
A missing file is an empty key file: found, when given, is then set to false, and the call is no error. error, when given, says why a file that exists could not be read. Returns the parsed file.
fromText
[static] platform::KeyFile KeyFile::fromText(const QString &text)
Reads text, one line at a time.
A carriage return at the end of a line is not part of the line. Returns the parsed file.
groups
QStringList KeyFile::groups() const
Returns the names of the groups, in the order of the file. A name that is in the file twice is listed once.
hasGroup
bool KeyFile::hasGroup(const QString &group) const
Returns true when the file has a group called group.
invalidLineCount
int KeyFile::invalidLineCount() const
Returns the number of lines that are no group, entry, comment or empty line. They are kept as they are.
isEmpty
bool KeyFile::isEmpty() const
Returns true when the file has no line.
keys
QStringList KeyFile::keys(const QString &group) const
Returns the keys of group, in the order of the file, with their locale suffixes.
rawValue
QString KeyFile::rawValue(const QString &group, const QString &key) const
Returns the raw value of key in group: the text after the `=`, without the spaces around it, and not unescaped.
When the group has the key twice, the first line counts. Returns an empty string when there is no such key.
See also setRawValue().
remove
bool KeyFile::remove(const QString &group, const QString &key)
Removes every line of key in group.
Returns true when a line was removed. The group line stays, even when the group is empty then.
setRawValue
void KeyFile::setRawValue(const QString &group, const QString &key, const QString &rawValue)
Sets key of group to rawValue, which must be escaped already.
The line of the key is written again when there is one. Else one line is added after the last entry of the group, or, for a missing group, a group line and the entry at the end of the file. No other line changes.
See also rawValue().
text
QString KeyFile::text() const
Returns the file as text: every line as it was read, with the changes made since.
unescape
[static] QString KeyFile::unescape(const QString &raw)
Returns raw with the escapes of the string type undone.
The specification's escapes are a backslash before `s` (a space), `n`, `t`, `r`, or a second backslash. Any other backslash stays with the character after it, because the `Exec` key and the lists have escapes of their own that come after these.
unescapeList
[static] QStringList KeyFile::unescapeList(const QString &raw)
Returns the items of the list value raw.
The items are separated by `;`, and a backslash before a `;` makes it a semicolon inside an item. Each item is unescaped, and its spaces at both ends are removed. An empty item, such as the one after the last separator, is no item.