Skip to content

Commit

Permalink
open-uri: drop the temporary JSON format
Browse files Browse the repository at this point in the history
This is not necessary anymore, so revert to using a static struct.
  • Loading branch information
andyholmes committed Apr 19, 2024
1 parent a3b805a commit 4806ad1
Showing 1 changed file with 53 additions and 55 deletions.
108 changes: 53 additions & 55 deletions src/open-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,57 @@ should_use_default_app (const char *scheme,
return FALSE;
}

static struct
{
const char *app_id;
const char * const *domains;
} app_handlers[] = {
{
.app_id = "org.gnome.Maps",
.domains = (const char *[]){
"openstreetmap.org/?lat=",
"openstreetmap.org/?#map=",
"openstreetmap.org/#map=",
"openstreetmap.org/node/",
"openstreetmap.org/way/",
"map.apple.com",
NULL,
},
},
{
.app_id = "org.gnome.Fractal",
.domains = (const char *[]){
"matrix.to/#/#",
NULL,
},
},
{
.app_id = "org.gnome.Software",
.domains = (const char *[]){
"flathub.org/apps",
NULL,
},
},
{
.app_id = "io.freetubeapp.FreeTube",
.domains = (const char *[]){
"youtu.be",
"youtube.com/watch?v=",
NULL,
},
},
{
.app_id = "org.jitsi.jitsi-meet",
.domains = (const char *[]){
"meet.jit.si",
NULL,
},
},
};

static gboolean
domain_handler_match (GStrv domains,
GUri *uri)
domain_handler_match (const char * const *domains,
GUri *uri)
{
const char *host = NULL;
const char *path = NULL;
Expand Down Expand Up @@ -541,51 +589,6 @@ domain_handler_match (GStrv domains,
return FALSE;
}

static GHashTable *
load_handlers_json (void)
{
g_autoptr(JsonParser) parser = NULL;
g_autofree char *filename = NULL;
JsonObject *handlers_json = NULL;
JsonObjectIter iter;
const char *app_id = NULL;
JsonNode *entries = NULL;
GHashTable *ret = NULL;

ret = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify)g_strfreev);

filename = g_build_filename (g_get_user_data_dir (),
"xdg-handlers.json",
NULL);

parser = json_parser_new_immutable ();
if (!json_parser_load_from_file (parser, filename, NULL))
return ret;

handlers_json = json_node_get_object (json_parser_get_root (parser));
if (handlers_json == NULL)
return ret;

json_object_iter_init_ordered (&iter, handlers_json);
while (json_object_iter_next_ordered (&iter, &app_id, &entries))
{
JsonArray *domains = json_node_get_array (entries);
unsigned int n_domains = json_array_get_length (domains);
g_autoptr(GStrvBuilder) builder = NULL;

builder = g_strv_builder_new ();
for (unsigned int i = 0; i < n_domains; i++)
g_strv_builder_add (builder, json_array_get_string_element (domains, i));

g_hash_table_replace (ret, g_strdup (app_id), g_strv_builder_end (builder));
}

return ret;
}

static void
find_patterned_choices (const char *source_id,
const char *uri,
Expand All @@ -594,10 +597,7 @@ find_patterned_choices (const char *source_id,
{
g_autoptr(GUri) guri = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GHashTable) apps = NULL;
GHashTableIter iter;
const char *app_id = NULL;
GStrv domains = NULL;
g_autoptr(GStrvBuilder) builder = NULL;
guint n_choices = 0;

Expand All @@ -611,18 +611,16 @@ find_patterned_choices (const char *source_id,
}

builder = g_strv_builder_new ();
apps = load_handlers_json ();

g_hash_table_iter_init (&iter, apps);
while (g_hash_table_iter_next (&iter, (void **)&app_id, (void **)&domains))
for (size_t i = 0; i < G_N_ELEMENTS (app_handlers); i++)
{
if (g_strcmp0 (source_id, app_id) == 0)
if (g_strcmp0 (source_id, app_handlers[i].app_id) == 0)
{
g_debug ("Skipping handler for originating app %s", app_id);
continue;
}

if (domain_handler_match (domains, guri))
if (domain_handler_match (app_handlers[i].domains, guri))
{
g_debug ("Matching handler for %s (%s)", uri, app_id);
g_strv_builder_add (builder, app_id);
Expand Down

0 comments on commit 4806ad1

Please sign in to comment.