MimeAppsList

One `mimeapps.list` file: the defaults and the associations of file types with programs, read and written line by line.

In the namespace platform. Declared in src/libs/libplatform/platform/MimeAppsList.hpp.

Detailed description

The format is the one of the XDG MIME Applications Specification (https://specifications.freedesktop.org/mime-apps/latest/): three groups, `[Default Applications]`, `[Added Associations]` and `[Removed Associations]`, each with lines of the form `type=id1.desktop;id2.desktop;`.

The file is kept line by line (platform::KeyFile). A change rewrites the one line of its type, or adds one, and save() gives every other line, comment and empty line back in its order. It is not QSettings, which writes the whole file again in an order of its own.

lookupOrder() gives the files that the specification reads, most important first. platform::resolveMimeHandlers() reads them.

See also platform::DesktopEntry, platform::resolveMimeHandlers().

Member function documentation

addAssociation

void MimeAppsList::addAssociation(const QString &mimeType, const QString &desktopId)

Associates desktopId with mimeType in `[Added Associations]`, first, as `xdg-mime default` does. An id that is there already moves to the front.

addedAssociations

QStringList MimeAppsList::addedAssociations(const QString &mimeType) const

Returns the desktop ids of `[Added Associations]` for mimeType, most preferred first.

addedGroup

[static] QString MimeAppsList::addedGroup()

Returns the name of the group of added associations, `Added Associations`.

defaultApplications

QStringList MimeAppsList::defaultApplications(const QString &mimeType) const

Returns the desktop ids of `[Default Applications]` for mimeType, most preferred first.

defaultGroup

[static] QString MimeAppsList::defaultGroup()

Returns the name of the group of defaults, `Default Applications`.

existed

bool MimeAppsList::existed() const

Returns false when the last load() found no file.

fromText

[static] platform::MimeAppsList MimeAppsList::fromText(const QString &text)

Parses text. Returns the list.

load

bool MimeAppsList::load(const QString &path, QString *error = nullptr)

Reads the file at path, in place of what the list held.

A missing file is an empty list, as the specification says ("a missing file is equivalent to an empty file"): the call then returns true, and existed() returns false. Returns false, with error set when given, when the file exists and cannot be read.

lookupOrder

[static] QStringList MimeAppsList::lookupOrder(const QStringList &configFolders, const QStringList &applicationFolders, const QStringList &desktopNames)

Returns the `mimeapps.list` files in the order the specification reads them, most important first.

For each of configFolders, then for each of applicationFolders: `<desktop>-mimeapps.list` for each of desktopNames, in order, then `mimeapps.list`. The config folders are the user's folder, then the system's (`QStandardPaths::GenericConfigLocation`). The application folders are the `applications/` folders of the data folders (`QStandardPaths::ApplicationsLocation`). The desktop names are lowercased. A folder that is in a list twice is read once.

mimeTypes

QStringList MimeAppsList::mimeTypes(const QString &group) const

Returns the MIME types that group has a line for.

removeApplication

bool MimeAppsList::removeApplication(const QString &desktopId)

Removes desktopId from every line of `[Default Applications]` and `[Added Associations]`. A line with no id left is removed.

`[Removed Associations]` stays as it is: it holds the user's choices against other programs. Returns true when a line changed.

removedAssociations

QStringList MimeAppsList::removedAssociations(const QString &mimeType) const

Returns the desktop ids of `[Removed Associations]` for mimeType.

removedGroup

[static] QString MimeAppsList::removedGroup()

Returns the name of the group of removed associations, `Removed Associations`.

save

bool MimeAppsList::save(const QString &path, QString *error = nullptr) const

Writes the list to path, as a whole new file that replaces the old one at once.

The folder is made when it is missing. A file that exists and is read only, the user's way to keep a list as it is, is not written. The check reads the permission bits, not the access of this process, so a process that may write any file still keeps the user's choice.

Returns false, with error set when given, when the file was not written.

setDefaultApplication

void MimeAppsList::setDefaultApplication(const QString &mimeType, const QString &desktopId)

Makes desktopId the default for mimeType.

The id goes first in the type's line of `[Default Applications]`. The ids that were there stay after it, so the user's next choice is kept. The specification wants a default to be associated with its type too: the caller adds that with addAssociation().

text

QString MimeAppsList::text() const

Returns the file as text.

QListplatform::MimeHandler resolveMimeHandlers(const platform::MimeHandlerQuery &query)

Returns the programs that open query's type, the default first.

The rules are the XDG MIME Applications Specification's (https://specifications.freedesktop.org/mime-apps/latest/):

  • The lists are read in the order of MimeAppsList::lookupOrder().
  • The programs of the type are its added associations, then the desktop files whose `MimeType=` names it, less its removed associations, folder by folder (associations.html). An added and a removed association in one file: the added one counts, because the algorithm adds before it blacklists. A removal in a more important file than an addition wins.
  • Then the programs of each ancestor type, the closest first: "an association from a mimetype that is more specific will be listed before an association with a lower priority".
  • The default is the first id of a `[Default Applications]` line that is installed and associated with its type (default.html). It goes first. When no list names one, the first program is the default.
  • An entry with `Hidden=true`, or with a `TryExec` program that is not found, is not installed. A desktop file hides the files of the same id in less important folders.

query names the folders and gives the type's ancestors. Nothing here reads the environment, so a test passes fixture folders.

In this section