diff options
Add a simple way to fix the URL
It tries to load it as a file before trying a URL to avoid loading something meant as local.
Closes: https://gitlab.com/lanodan/badWolf/issues/6
1 files changed, 32 insertions, 3 deletions
| -rw-r--r-- | badwolf.c | 35 |
@@ -8,6 +8,7 @@ #include <glib/gprintf.h> /* g_fprintf() */ #include <gtk/gtk.h> #include <locale.h> /* LC_* */ +#include <stdlib.h> /* realpath() */ #include <webkit2/webkit2.h> const gchar *homepage = "https://hacktivis.me/projects/badwolf"; @@ -36,6 +37,7 @@ struct Client GtkWidget *search; }; +gchar *ensure_uri_scheme(gchar *text); static gboolean WebViewCb_close(WebKitWebView *webView, gpointer user_data); static gboolean commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser); @@ -80,6 +82,33 @@ static void notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data); gint get_tab_position(GtkContainer *notebook, GtkWidget *child); +gchar * +ensure_uri_scheme(gchar *text) +{ + if(text == NULL) return "about:blank"; + + char *scheme = g_uri_parse_scheme(text); + + if(scheme != NULL) + return text; + else + { + gchar *f; + char *path; + path = realpath(text, NULL); + + if(path != NULL) + { + f = g_strdup_printf("file://%s", path); + free(path); + } + else + f = g_strdup_printf("http://%s", text); + + return f; + } +} + static void badwolf_about_dialog(GtkWindow *main_window) { @@ -425,10 +454,10 @@ WebViewCb_create(WebKitWebView *related_web_view, static gboolean locationCb_activate(GtkEntry *location, gpointer user_data) { - const char *target_url; + char *target_url; struct Client *browser = (struct Client *)user_data; - target_url = gtk_entry_get_text(location); + target_url = ensure_uri_scheme((char *)gtk_entry_get_text(location)); if(target_url != NULL) webkit_web_view_load_uri(browser->webView, target_url); @@ -502,7 +531,7 @@ struct Client * new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view) { struct Client *browser = g_malloc(sizeof(struct Client)); - if(target_url == NULL) target_url = "about:blank"; + target_url = ensure_uri_scheme(target_url); browser->window = window; browser->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |