Skip to content

Commit

Permalink
Add support for alternative desktop entries names in profiles
Browse files Browse the repository at this point in the history
For example, Chromium uses either `chromium-browser.desktop` or
`chromium.desktop` depending on a distro.

Add support for defining desktop entry name as a list. When some
code wants to get a path to the desktop entry iterate over the
list and return the first file that exists. If none exist return
None.
  • Loading branch information
igo95862 committed May 25, 2024
1 parent 7af3ba1 commit 57a8265
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
5 changes: 4 additions & 1 deletion data/usr-share/bubblejail/profiles/chromium.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# SPDX-FileCopyrightText: 2020 igo95862

# Chromium
dot_desktop_path = "/usr/share/applications/chromium.desktop"
dot_desktop_path = [
"/usr/share/applications/chromium-browser.desktop",
"/usr/share/applications/chromium.desktop",
]
description='''
Chromium web browser
'''
Expand Down
5 changes: 4 additions & 1 deletion data/usr-share/bubblejail/profiles/firefox.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# SPDX-FileCopyrightText: 2020 igo95862

# Firefox on X11
dot_desktop_path = "/usr/share/applications/firefox.desktop"
dot_desktop_path = [
"/usr/share/applications/firefox.desktop",
"/usr/share/applications/firefox-esr.desktop",
]
is_gtk_application = true
description='''
Firefox web browser.
Expand Down
5 changes: 4 additions & 1 deletion data/usr-share/bubblejail/profiles/firefox_wayland.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# SPDX-FileCopyrightText: 2020 igo95862

# Firefox on Wayland
dot_desktop_path = "/usr/share/applications/firefox.desktop"
dot_desktop_path = [
"/usr/share/applications/firefox.desktop",
"/usr/share/applications/firefox-esr.desktop",
]
is_gtk_application = true
description='''
Firefox using wayland display protocol.
Expand Down
13 changes: 7 additions & 6 deletions src/bubblejail/bubblejail_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def create_new_instance(
instance = BubblejailInstance(instance_directory)

if create_dot_desktop:
if profile.dot_desktop_path is not None:
if profile.desktop_entries_paths:
cls.overwrite_desktop_entry_for_profile(
instance_name=new_name,
profile_object=profile,
Expand Down Expand Up @@ -233,21 +233,22 @@ def overwrite_desktop_entry_for_profile(
elif profile_object is not None:
# 3. Profile was passed directly
profile = profile_object
dot_desktop_path = profile.dot_desktop_path
dot_desktop_path = profile.find_desktop_entry()
elif profile_name is not None:
# 4. Profile name was passed
profile = cls.profile_get(profile_name)
dot_desktop_path = profile.dot_desktop_path
dot_desktop_path = profile.find_desktop_entry()
elif instance.metadata_creation_profile_name is not None:
# 5. Use the profile name saved in meta data
profile = cls.profile_get(instance.metadata_creation_profile_name)
dot_desktop_path = profile.dot_desktop_path
dot_desktop_path = profile.find_desktop_entry()
else:
raise RuntimeError('No profile or desktop entry specified')

if dot_desktop_path is None:
raise TypeError('Desktop entry path can\'t be None.',
dot_desktop_path)
raise RuntimeError(
"Couldn't resolve desktop entry path."
)

new_dot_desktop = IniFile.IniFile(
filename=str(dot_desktop_path))
Expand Down
42 changes: 23 additions & 19 deletions src/bubblejail/bubblejail_gui_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,22 +575,24 @@ def can_be_created(self) -> Tuple[bool, str]:
except BubblejailInstanceNotFoundError:
...

if self.current_profile is not None:
if self.current_profile.dot_desktop_path is not None and \
not self.current_profile.dot_desktop_path.is_file():
warn_text = (
'⚠ WARNING \n'
'Desktop entry does not exist\n'
'Maybe you don\'t have application installed?'
)
return False, warn_text
else:
return True, (
f"{self.current_profile.description}\n"
f"Import tips: {self.current_profile.import_tips}"
)

return True, 'Create empty profile'
if self.current_profile is None:
return True, 'Create empty profile'

if (
self.current_profile.desktop_entries_paths
and self.current_profile.find_desktop_entry() is None
):
warn_text = (
'⚠ WARNING \n'
'Desktop entry does not exist\n'
'Maybe you don\'t have application installed?'
)
return False, warn_text
else:
return True, (
f"{self.current_profile.description}\n"
f"Import tips: {self.current_profile.import_tips}"
)

def refresh_create_button(self) -> None:
is_allowed, new_text = self.can_be_created()
Expand All @@ -603,10 +605,12 @@ def selection_changed(self, new_text: str) -> None:
self.current_profile = None
else:
self.current_profile = BubblejailDirectories.profile_get(new_text)
if self.current_profile is not None and \
self.current_profile.dot_desktop_path is not None:
if (
self.current_profile is not None
and self.current_profile.desktop_entries_paths
):
self.name_widget.line_edit.setText(
self.current_profile.dot_desktop_path.stem
self.current_profile.desktop_entries_paths[0].stem
)

self.refresh_create_button()
Expand Down
26 changes: 23 additions & 3 deletions src/bubblejail/bubblejail_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,39 @@ async def edit_config_in_editor(self) -> None:
class BubblejailProfile:
def __init__(
self,
dot_desktop_path: str | None = None,
dot_desktop_path: list[str] | str | None = None,
is_gtk_application: bool = False,
services: ServicesConfDictType | None = None,
description: str = 'No description',
import_tips: str = 'None',
) -> None:
self.dot_desktop_path = (Path(dot_desktop_path)
if dot_desktop_path is not None else None)

match dot_desktop_path:
case list():
self.desktop_entries_paths = [
Path(x) for x in dot_desktop_path
]
case str():
self.desktop_entries_paths = [Path(dot_desktop_path)]
case None:
self.desktop_entries_paths = []
case _:
raise TypeError(
"Desktop entry path be a str, list[str] or None "
"not {dot_desktop_path!r}"
)
self.is_gtk_application = is_gtk_application
self.config = BubblejailInstanceConfig(services)
self.description = description
self.import_tips = import_tips

def find_desktop_entry(self) -> Path | None:
for path in self.desktop_entries_paths:
if path.exists():
return path

return None


class BubblejailInstanceMetadata:
def __init__(
Expand Down

0 comments on commit 57a8265

Please sign in to comment.