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 locale;
use strings;
// The information from a desktop entry file.
// Specification: §6
export type file = struct {
typ: str,
version: str,
@ -32,6 +34,8 @@ export type file = struct {
actions: []action,
};
// An additional application action.
// Specification: §11
export type action = struct {
key: str,
name: locale::string,
@ -40,15 +44,15 @@ export type action = struct {
};
// 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 scanne = scan(input);
defer finish(&scanne);
let scanne = desktop_entry::scan(input);
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)) {
case let err: error =>
file_finish(&file);
case let err: desktop_entry::error =>
finish(&file);
return err;
case void => void;
};
@ -58,7 +62,7 @@ export fn parse(input: io::handle) (file | error) = {
};
// Frees resources associated with a [[file]].
export fn file_finish(this: *file) void = {
export fn finish(this: *file) void = {
free(this.typ);
free(this.version);
locale_string_finish(this.name);
@ -86,7 +90,7 @@ export fn file_finish(this: *file) void = {
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 ";
if (entr.group == "Desktop Entry") {
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)) {
case let actio: *action => yield actio;
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") {
parse_set_locale_string(
&actio.name, entr.locale,
parse_localestring(entr.value)?);
desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "Icon") {
parse_set_locale_string(
&actio.icon, entr.locale,
parse_iconstring(entr.value)?);
desktop_entry::parse_iconstring(entr.value)?);
} else if (entr.key == "Exec") {
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 (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") {
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") {
parse_set_locale_string(
&this.name, entr.locale,
parse_localestring(entr.value)?);
desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "GenericName") {
parse_set_locale_string(
&this.generic_name, entr.locale,
parse_localestring(entr.value)?);
desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "NoDisplay") {
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") {
parse_set_locale_string(
&this.comment, entr.locale,
parse_localestring(entr.value)?);
desktop_entry::parse_localestring(entr.value)?);
} else if (entr.key == "Icon") {
parse_set_locale_string(
&this.icon, entr.locale,
parse_iconstring(entr.value)?);
desktop_entry::parse_iconstring(entr.value)?);
} else if (entr.key == "Hidden") {
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") {
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") {
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") {
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") {
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") {
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") {
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") {
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") {
if (parse_is_localized(entr)) return void;
let strings = parse_strings(entr.value)?;
let strings =desktop_entry::parse_strings(entr.value)?;
defer free(strings);
this.actions = alloc([], len(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") {
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") {
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") {
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") {
parse_set_locale_strings(
&this.keywords, entr.locale,
parse_localestrings(entr.value)?);
desktop_entry::parse_localestrings(entr.value)?);
} else if (entr.key == "StartupWMClass") {
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") {
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") {
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") {
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);
};

View File

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