Moved some functionality from format::desktop_entry to xdg::desktop_entry

This commit is contained in:
Sasha Koshka 2024-10-22 17:49:28 -04:00
parent af3a694024
commit 69eaf63fe8
2 changed files with 46 additions and 41 deletions

View File

@ -1,8 +1,10 @@
use format::desktop_entry;
use io; use io;
use locale; use locale;
use strings; use strings;
// The information from a desktop entry file. // The information from a desktop entry file.
// Specification: §6
export type file = struct { export type file = struct {
typ: str, typ: str,
version: str, version: str,
@ -32,6 +34,8 @@ export type file = struct {
actions: []action, actions: []action,
}; };
// An additional application action.
// Specification: §11
export type action = struct { export type action = struct {
key: str, key: str,
name: locale::string, name: locale::string,
@ -40,15 +44,15 @@ export type action = struct {
}; };
// Parses a desktop entry file. Use [[file_finish]] to get rid of it. // Parses a desktop entry file. Use [[file_finish]] to get rid of it.
export fn parse(input: io::handle) (file | error) = { export fn parse(input: io::handle) (file | desktop_entry::error) = {
let file = file { ... }; let file = file { ... };
let scanne = scan(input); let scanne = desktop_entry::scan(input);
defer finish(&scanne); defer desktop_entry::finish(&scanne);
for (let entr => next_entry(&scanne)?) { for (let entr => desktop_entry::next_entry(&scanne)?) {
match(parse_handle_entry(&file, entr)) { match(parse_handle_entry(&file, entr)) {
case let err: error => case let err: desktop_entry::error =>
file_finish(&file); finish(&file);
return err; return err;
case void => void; case void => void;
}; };
@ -58,7 +62,7 @@ export fn parse(input: io::handle) (file | error) = {
}; };
// Frees resources associated with a [[file]]. // Frees resources associated with a [[file]].
export fn file_finish(this: *file) void = { export fn finish(this: *file) void = {
free(this.typ); free(this.typ);
free(this.version); free(this.version);
locale_string_finish(this.name); locale_string_finish(this.name);
@ -86,7 +90,7 @@ export fn file_finish(this: *file) void = {
free(this.actions); free(this.actions);
}; };
fn parse_handle_entry(this: *file, entr: entry) (void | error) = { fn parse_handle_entry(this: *file, entr: desktop_entry::entry) (void | desktop_entry::error) = {
const desktop_action_prefix = "Desktop Action "; const desktop_action_prefix = "Desktop Action ";
if (entr.group == "Desktop Entry") { if (entr.group == "Desktop Entry") {
return parse_handle_desktop_entry_entry(this, entr); return parse_handle_desktop_entry_entry(this, entr);
@ -96,7 +100,7 @@ fn parse_handle_entry(this: *file, entr: entry) (void | error) = {
}; };
}; };
fn parse_handle_desktop_action_entry(this: *file, entr: entry, key: str) (void | error) = { fn parse_handle_desktop_action_entry(this: *file, entr: desktop_entry::entry, key: str) (void | desktop_entry::error) = {
let actio = match(parse_find_action(this, key)) { let actio = match(parse_find_action(this, key)) {
case let actio: *action => yield actio; case let actio: *action => yield actio;
case void => return void; case void => return void;
@ -104,70 +108,70 @@ fn parse_handle_desktop_action_entry(this: *file, entr: entry, key: str) (void |
if (entr.key == "Name") { if (entr.key == "Name") {
parse_set_locale_string( parse_set_locale_string(
&actio.name, entr.locale, &actio.name, entr.locale,
parse_localestring(entr.value)?); desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "Icon") { } else if (entr.key == "Icon") {
parse_set_locale_string( parse_set_locale_string(
&actio.icon, entr.locale, &actio.icon, entr.locale,
parse_iconstring(entr.value)?); desktop_entry::parse_iconstring(entr.value)?);
} else if (entr.key == "Exec") { } else if (entr.key == "Exec") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
actio.exec = parse_string(entr.value)?; actio.exec = desktop_entry::parse_string(entr.value)?;
}; };
}; };
fn parse_handle_desktop_entry_entry(this: *file, entr: entry) (void | error) = { fn parse_handle_desktop_entry_entry(this: *file, entr: desktop_entry::entry) (void | desktop_entry::error) = {
if (entr.key == "Type") { if (entr.key == "Type") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.typ = parse_string(entr.value)?; this.typ = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "Version") { } else if (entr.key == "Version") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.version = parse_string(entr.value)?; this.version = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "Name") { } else if (entr.key == "Name") {
parse_set_locale_string( parse_set_locale_string(
&this.name, entr.locale, &this.name, entr.locale,
parse_localestring(entr.value)?); desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "GenericName") { } else if (entr.key == "GenericName") {
parse_set_locale_string( parse_set_locale_string(
&this.generic_name, entr.locale, &this.generic_name, entr.locale,
parse_localestring(entr.value)?); desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "NoDisplay") { } else if (entr.key == "NoDisplay") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.no_display = parse_boolean(entr.value)?; this.no_display = desktop_entry::parse_boolean(entr.value)?;
} else if (entr.key == "Comment") { } else if (entr.key == "Comment") {
parse_set_locale_string( parse_set_locale_string(
&this.comment, entr.locale, &this.comment, entr.locale,
parse_localestring(entr.value)?); desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "Icon") { } else if (entr.key == "Icon") {
parse_set_locale_string( parse_set_locale_string(
&this.icon, entr.locale, &this.icon, entr.locale,
parse_iconstring(entr.value)?); desktop_entry::parse_iconstring(entr.value)?);
} else if (entr.key == "Hidden") { } else if (entr.key == "Hidden") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.hidden = parse_boolean(entr.value)?; this.hidden = desktop_entry::parse_boolean(entr.value)?;
} else if (entr.key == "OnlyShowIn") { } else if (entr.key == "OnlyShowIn") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.only_show_in = parse_strings(entr.value)?; this.only_show_in = desktop_entry::parse_strings(entr.value)?;
} else if (entr.key == "NotShowIn") { } else if (entr.key == "NotShowIn") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.not_show_in = parse_strings(entr.value)?; this.not_show_in = desktop_entry::parse_strings(entr.value)?;
} else if (entr.key == "DBusActivatable") { } else if (entr.key == "DBusActivatable") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.dbus_activatable = parse_boolean(entr.value)?; this.dbus_activatable = desktop_entry::parse_boolean(entr.value)?;
} else if (entr.key == "TryExec") { } else if (entr.key == "TryExec") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.try_exec = parse_string(entr.value)?; this.try_exec = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "Exec") { } else if (entr.key == "Exec") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.exec = parse_string(entr.value)?; this.exec = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "Path") { } else if (entr.key == "Path") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.path = parse_string(entr.value)?; this.path = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "Terminal") { } else if (entr.key == "Terminal") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.terminal = parse_boolean(entr.value)?; this.terminal = desktop_entry::parse_boolean(entr.value)?;
} else if (entr.key == "Actions") { } else if (entr.key == "Actions") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
let strings = parse_strings(entr.value)?; let strings =desktop_entry::parse_strings(entr.value)?;
defer free(strings); defer free(strings);
this.actions = alloc([], len(strings)); this.actions = alloc([], len(strings));
for (let string .. strings) { for (let string .. strings) {
@ -178,33 +182,33 @@ fn parse_handle_desktop_entry_entry(this: *file, entr: entry) (void | error) = {
}; };
} else if (entr.key == "MimeType") { } else if (entr.key == "MimeType") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.mime_type = parse_strings(entr.value)?; this.mime_type = desktop_entry::parse_strings(entr.value)?;
} else if (entr.key == "Categories") { } else if (entr.key == "Categories") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.categories = parse_strings(entr.value)?; this.categories = desktop_entry::parse_strings(entr.value)?;
} else if (entr.key == "Implements") { } else if (entr.key == "Implements") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.implements = parse_strings(entr.value)?; this.implements = desktop_entry::parse_strings(entr.value)?;
} else if (entr.key == "Keywords") { } else if (entr.key == "Keywords") {
parse_set_locale_strings( parse_set_locale_strings(
&this.keywords, entr.locale, &this.keywords, entr.locale,
parse_localestrings(entr.value)?); desktop_entry::parse_localestrings(entr.value)?);
} else if (entr.key == "StartupWMClass") { } else if (entr.key == "StartupWMClass") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.startup_wm_class = parse_string(entr.value)?; this.startup_wm_class = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "URL") { } else if (entr.key == "URL") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.url = parse_string(entr.value)?; this.url = desktop_entry::parse_string(entr.value)?;
} else if (entr.key == "PrefersNonDefaultGPU") { } else if (entr.key == "PrefersNonDefaultGPU") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.prefers_non_default_gpu = parse_boolean(entr.value)?; this.prefers_non_default_gpu = desktop_entry::parse_boolean(entr.value)?;
} else if (entr.key == "SingleMainWindow") { } else if (entr.key == "SingleMainWindow") {
if (parse_is_localized(entr)) return void; if (parse_is_localized(entr)) return void;
this.single_main_window = parse_boolean(entr.value)?; this.single_main_window = desktop_entry::parse_boolean(entr.value)?;
}; };
}; };
fn parse_is_localized(entr: entry) bool = { fn parse_is_localized(entr: desktop_entry::entry) bool = {
return !locale::equal(entr.locale, locale::c); return !locale::equal(entr.locale, locale::c);
}; };

View File

@ -1,3 +1,4 @@
use format::desktop_entry;
use fmt; use fmt;
use io; use io;
use locale; use locale;
@ -7,7 +8,7 @@ use os;
let file = os::open("format/desktop_entry/test_data/foo_full.desktop")!; let file = os::open("format/desktop_entry/test_data/foo_full.desktop")!;
defer io::close(file)!; defer io::close(file)!;
let file = parse(file)!; let file = parse(file)!;
defer file_finish(&file); defer finish(&file);
assert(file.typ == "Application"); assert(file.typ == "Application");
assert(file.version == "1.0"); assert(file.version == "1.0");
@ -16,7 +17,7 @@ use os;
])); ]));
assert(locale_string_equal(file.generic_name, [ assert(locale_string_equal(file.generic_name, [
(locale::c, "Foo Viewer"), (locale::c, "Foo Viewer"),
(locale::parse("xx_XX.UTF-8")!, "Sneep glorp"), (locale::parse("xx_XX")!, "Sneep glorp"),
])); ]));
assert(file.no_display == false); assert(file.no_display == false);
assert(locale_string_equal(file.comment, [ assert(locale_string_equal(file.comment, [
@ -58,7 +59,7 @@ use os;
assert(locale_string_equal(actio.name, [ assert(locale_string_equal(actio.name, [
(locale::c, "Create a new Foo!"), (locale::c, "Create a new Foo!"),
(locale::parse("en_US")!, "Create a new Foo!"), (locale::parse("en_US")!, "Create a new Foo!"),
(locale::parse("xx_XX.UTF-8")!, "Zweep zoop flooble glorp"), (locale::parse("xx_XX")!, "Zweep zoop flooble glorp"),
])); ]));
assert(locale_string_equal(actio.icon, [ assert(locale_string_equal(actio.icon, [
(locale::c, "fooview-new"), (locale::c, "fooview-new"),