Skip to content

Commit

Permalink
open-uri: basic support for proposed intent
Browse files Browse the repository at this point in the history
Add support for reading URI handlers from a proposed addendum to
the intents-spec in the form:

```ini
[org.freedesktop.UriHandler]
Patterns=*.openstreetmap/node/*;*.openstreetmap/way/*;
```
  • Loading branch information
andyholmes committed Jun 24, 2024
1 parent fe0ee65 commit 027cb6a
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/open-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,15 @@ typedef struct
GStrv hosts;
GArray *ports;
GStrv paths;
GStrv patterns;
} UriHandler;

static void
uri_handler_free (UriHandler *handler)
{
g_assert (handler != NULL);

g_clear_pointer (&handler->patterns, g_strfreev);
g_clear_pointer (&handler->schemes, g_strfreev);
g_clear_pointer (&handler->hosts, g_strfreev);
g_clear_pointer (&handler->paths, g_strfreev);
Expand All @@ -523,6 +525,35 @@ uri_handler_free (UriHandler *handler)
/*
* Temporary deserialization
*/
#define URI_HANDLER_GROUP "org.freedesktop.UriHandler"
#define URI_HANDLER_PATTERNS_KEY "Patterns"

static GPtrArray *
uri_handler_deserialize_patterns (GKeyFile *keyfile)
{
GPtrArray *ret = NULL;
g_auto (GStrv) patterns = NULL;

g_assert (keyfile != NULL);

patterns = g_key_file_get_string_list (keyfile,
URI_HANDLER_GROUP,
URI_HANDLER_PATTERNS_KEY,
NULL, NULL);

if (patterns != NULL && patterns[0] != NULL)
{
UriHandler *handler = NULL;

ret = g_ptr_array_new_with_free_func ((GDestroyNotify)uri_handler_free);
handler = g_new0 (UriHandler, 1);
handler->patterns = g_steal_pointer (&patterns);
g_ptr_array_add (ret, handler);
}

return ret;
}

static GPtrArray *
uri_handler_deserialize_sections (GKeyFile *keyfile)
{
Expand All @@ -531,6 +562,7 @@ uri_handler_deserialize_sections (GKeyFile *keyfile)

g_assert (keyfile != NULL);

ret = g_ptr_array_new_with_free_func ((GDestroyNotify)uri_handler_free);
groups = g_key_file_get_groups (keyfile, NULL);
for (size_t i = 0; groups[i] != NULL; i++)
{
Expand Down Expand Up @@ -609,7 +641,15 @@ uri_handler_load_keyfiles (void)
if (!g_key_file_load_from_file (keyfile, filepath, G_KEY_FILE_NONE, NULL))
continue;

handlers = uri_handler_deserialize_sections (keyfile);
if (g_key_file_has_group (keyfile, "org.freedesktop.UriHandler"))
{
handlers = uri_handler_deserialize_patterns (keyfile);
}
else
{
handlers = uri_handler_deserialize_sections (keyfile);
}

if (handlers != NULL && handlers->len > 0)
{
g_autofree char *basename = NULL;
Expand All @@ -635,6 +675,18 @@ uri_handler_match (UriHandler *handler,
const char *scheme = NULL;
const char *host = NULL;

/* Simple pattern matching */
if (handler->patterns != NULL)
{
g_autofree char *uri_str = g_uri_to_string (uri);

for (unsigned int i = 0; handler->patterns[i] != NULL; i++)
{
if (g_pattern_match_simple (handler->patterns[i], uri_str))
return TRUE;
}
}

scheme = g_uri_get_scheme (uri);
if (scheme != NULL && handler->schemes != NULL)
{
Expand Down

0 comments on commit 027cb6a

Please sign in to comment.