Skip to content

Commit

Permalink
Fix xdg-mime default registration (#227)
Browse files Browse the repository at this point in the history
* xdg-mime accepts the .desktop filename, not the full path

* add news

* Less invasive default registration, cleanup on removal

* clarify role of %f in the docs

* Update menuinst/platforms/linux.py
  • Loading branch information
jaimergp authored Aug 7, 2024
1 parent e5c7532 commit 145bb74
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
7 changes: 4 additions & 3 deletions docs/source/defining-shortcuts.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ Each operating system has a slightly different way of associating a file type to
Unix systems have the notion of MIME types, while Windows relies more on file name extensions.

- On Linux, use the `MimeType` option. Remember to add the `%f` (single file) or `%F` (several
files) placeholders to your command so the paths are passed adequately. If you are defining a new
MIME type, you must fill the `glob_patterns` field by mapping the new MIME type to the file
extensions you want to associate with it.
files) placeholders to your command so the paths are passed adequately. Otherwise, your shortcut
might be deemed invalid and won't show up in "Open With" menus or similar UI elements of your
desktop. If you are defining a new MIME type, you must fill the `glob_patterns` field by mapping
the new MIME type to the file extensions you want to associate with it.
- On Windows, use `file_extensions`. Remember to add the `%1` or `%*` placeholders to your command
so the path of the opened file(s) is passed adequately.
- On macOS, use `CFBundleDocumentTypes`. Requires no placeholder. The opened document will be
Expand Down
49 changes: 43 additions & 6 deletions menuinst/platforms/linux.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
"""
""" """

import os
import shlex
import shutil
import time
from configparser import ConfigParser
from logging import getLogger
from pathlib import Path
from subprocess import CalledProcessError
Expand Down Expand Up @@ -286,11 +286,48 @@ def _register_mime_types(self, mime_types: Iterable[str], register: bool = True)
if glob_pattern:
self._glob_pattern_for_mime_type(mime_type, glob_pattern, install=register)

mimeapps = self.menu.config_directory / "mimeapps.list"
if register:
xdg_mime = shutil.which("xdg-mime")
if not xdg_mime:
log.debug("xdg-mime not found, not registering mime types as default.")
logged_run([xdg_mime, "default", self.location, *mime_types])
config = ConfigParser(default_section=None)
if mimeapps.is_file():
config.read(mimeapps)
log.debug("Registering %s to %s...", mime_types, mimeapps)
if "Default Applications" not in config.sections():
config.add_section("Default Applications")
if "Added Associations" not in config.sections():
config.add_section("Added Associations")
defaults = config["Default Applications"]
added = config["Added Associations"]
for mime_type in mime_types:
if mime_type not in defaults:
# Do not override existing defaults
defaults[mime_type] = self.location.name
if mime_type in added and self.location.name not in added[mime_type]:
added[mime_type] = f"{added[mime_type]};{self.location.name}"
else:
added[mime_type] = self.location.name
with open(mimeapps, "w") as f:
config.write(f, space_around_delimiters=False)
elif mimeapps.is_file():
# Remove entries
config = ConfigParser(default_section=None)
config.read(mimeapps)
log.debug("Deregistering %s from %s...", mime_types, mimeapps)
for section_name in "Default Applications", "Added Associations":
if section_name not in config.sections():
continue
section = config[section_name]
for mimetype, desktop_files in section.items():
if self.location.name == desktop_files:
section.pop(mimetype)
elif self.location.name in desktop_files.split(";"):
section[mimetype] = ";".join(
[x for x in desktop_files.split(";") if x != self.location.name]
)
if not section.keys():
config.remove_section(section_name)
with open(mimeapps, "w") as f:
config.write(f, space_around_delimiters=False)

update_mime_database = shutil.which("update-mime-database")
if update_mime_database:
Expand Down
19 changes: 19 additions & 0 deletions news/227-xdg-mime-default
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fix default MIME type registration on Linux. (#226 via #227)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>

0 comments on commit 145bb74

Please sign in to comment.