Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for desktop files in all subdirectories of app dirs #370

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions nwg_panel/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ def __get_app_dirs():


def __process_desktop_file(file_path):
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
except UnicodeDecodeError:
print(f"Warning: Invalid .desktop file '{file_path}'")
return
except OSError as e:
print(f"Warning: Unable to read .desktop file '{file_path}': {e}")
return

icon_name = None
app_names = []
startup_wm_class = None
Expand Down Expand Up @@ -70,10 +78,27 @@ def __process_desktop_file(file_path):


def __populate_caches():
for d in __get_app_dirs():
app_dirs = __get_app_dirs()
seen_dirs = set()
while app_dirs:
d = app_dirs.pop(0)
if d in seen_dirs:
continue
seen_dirs.add(d)
if os.path.isdir(d):
for file_name in os.listdir(d):
file_path = os.path.join(d, file_name)
try:
files = os.listdir(d)
except OSError as e:
print(f"Warning: Can't list files in directory '{d}': {e}")
continue

for file_name in files:
file_path = os.path.realpath(os.path.join(d, file_name))
if not os.path.exists(file_path):
continue
if os.path.isdir(file_path):
app_dirs.insert(0, file_path)
continue
__process_desktop_file(file_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could add

if not os.path.exists(file_path):
    continue

to avoid a crash on launch if the directory contains a broken symlink

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good idea. I haven't thought of that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pol-rivero I've also been thinking whether or not check if the file has a .desktop extension before passing it to __process_desktop_file method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had the same doubt when writing it originally, by decided not to check it for 2 reasons:

  1. It's not necessary. If our code works fine without this restriction, we shouldn't enforce it (see Robustness principle).
  2. The Desktop Entry Specification technically does allow desktop files to use extensions other than .desktop (notice the use of "Desktop entry files should have the .desktop extension" instead of "must" or equivalent).



Expand Down