A Dart package for parsing freedesktop (XDG) desktop entries on Linux.
- Obtain values by key, including action groups.
- Obtain localized values.
- Icon lookup.
This package provides the DesktopEntryKey
enum for convenience, but it doesn't make any assumptions
about value types and whether a key is required or not. All keys are considered optional.
import 'package:freedesktop_desktop_entry/freedesktop_desktop_entry.dart';
import 'dart:io';
final file = File("desktop-entry.desktop");
DesktopEntry desktopEntry = await DesktopEntry.parseFile(file);
LocalizedDesktopEntry localizedDesktopEntry = desktopEntry.localize(lang: 'fr', country: 'BE');
String? localizedComment = localizedDesktopEntry.entries[DesktopEntryKey.comment.string];
// OR
String? localizedComment = desktopEntry.entries[DesktopEntryKey.comment.string]?.localize(lang: 'fr', country: 'BE');
Unless you are only interested in a few fields, prefer localizing the entire desktop entry to avoid having to specify the locale every time.
The localize
methods will localize the values according to official locale matching rules, and
uses the default value if no locale is matched. This is most probably what you want to do.
String? name = desktopEntry.entries[DesktopEntryKey.name.string]?.value;
bool? terminal = desktopEntry.entries[DesktopEntryKey.terminal.string]?.value.getBoolean();
List<String>? keywords = desktopEntry.entries[DesktopEntryKey.keywords.string]?.value.getStringList();
bool? startupNotify = desktopEntry.entries['X-KDE-StartupNotify']?.value.getBoolean();
String? frenchComment = desktopEntry.entries[DesktopEntryKey.comment.string]?.localizedValues[Locale(lang: 'fr', country: 'BE')];
final themes = FreedesktopIconThemes();
File? file = await themes.findIcon(
IconQuery(
name: 'firefox',
size: 32,
scale: 2,
extensions: ['png'],
),
);
The first time you do an icon lookup, all installed themes on your system will be indexed.
If you want to preload the themes, call FreedesktopIconThemes.loadThemes
.
If new icons are installed, the next time you call FreedesktopIconThemes.findIcon
it will index all themes once again
before returning you an icon.
You can specify preferredThemes
in IconQuery
. This prioritizes the search to the list of theme names you've given before searching elsewhere.
These theme names must be directory names, not user-visible names. For example breeze-dark
and not Breeze Dark
.