logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://hacktivis.me/git/badwolf.git
commit: 720eb415a73893bf4c3894c10e27c66996f7e67e
parent 41cfd1ccae96dda3378b9666e38e70e547fe87f2
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 16 Dec 2019 14:35:37 +0100

Fix setting up completion

Diffstat:

Mbadwolf.c16++++++++++++++--
Mbookmarks.c24++++++++++++------------
Mbookmarks.h4++--
3 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/badwolf.c b/badwolf.c @@ -23,6 +23,7 @@ const gchar *version = VERSION; static gchar *web_extensions_directory; static int context_id_counter = 0; +GtkTreeModel *bookmarks_completion_model; static gboolean WebViewCb_close(WebKitWebView *webView, gpointer user_data); static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, @@ -738,9 +739,17 @@ new_browser(struct Window *window, const gchar *target_url, struct Client *old_b gtk_label_set_single_line_mode(GTK_LABEL(browser->statuslabel), TRUE); gtk_label_set_ellipsize(GTK_LABEL(browser->statuslabel), BADWOLF_STATUSLABEL_ELLIPSIZE); + if(bookmarks_completion_model != NULL) + { + GtkEntryCompletion *location_completion = gtk_entry_completion_new(); + GtkTreeModel *location_completion_model = bookmarks_completion_model; + + bookmarks_completion_setup(location_completion, location_completion_model); + gtk_entry_set_completion(GTK_ENTRY(browser->location), location_completion); + } + gtk_entry_set_text(GTK_ENTRY(browser->location), target_url); gtk_entry_set_input_purpose(GTK_ENTRY(browser->location), GTK_INPUT_PURPOSE_URL); - gtk_entry_set_completion(GTK_ENTRY(browser->location), location_completion); gtk_entry_set_placeholder_text(GTK_ENTRY(browser->search), _("search in current page")); @@ -933,7 +942,8 @@ main(int argc, char *argv[]) g_build_filename(g_get_user_data_dir(), "badwolf", "webkit-web-extension", NULL); fprintf(stderr, _("webkit-web-extension directory set to: %s\n"), web_extensions_directory); - location_completion_init(); + bookmarks_completion_model = bookmarks_completion_init(); + g_object_ref(bookmarks_completion_model); window->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); window->notebook = gtk_notebook_new(); @@ -1002,6 +1012,8 @@ main(int argc, char *argv[]) gtk_main(); + g_object_unref(bookmarks_completion_model); + #if 0 /* TRANSLATOR Ignore this entry. Done for forcing Unicode in xgettext. */ _("ΓΈ"); diff --git a/bookmarks.c b/bookmarks.c @@ -7,7 +7,7 @@ #include <gtk/gtk.h> #include <libxml/xpath.h> -gboolean +static gboolean location_completion_matches(GtkEntryCompletion *completion, const gchar *key, GtkTreeIter *iter, @@ -29,15 +29,15 @@ location_completion_matches(GtkEntryCompletion *completion, } void -location_completion_setup(GtkListStore *list_store) +bookmarks_completion_setup(GtkEntryCompletion *location_completion, GtkTreeModel *tree_model) { - gtk_entry_completion_set_model(location_completion, GTK_TREE_MODEL(list_store)); + gtk_entry_completion_set_model(location_completion, tree_model); gtk_entry_completion_set_text_column(location_completion, 0); gtk_entry_completion_set_match_func(location_completion, location_completion_matches, NULL, NULL); gtk_entry_completion_set_inline_selection(location_completion, BADWOLF_LOCATION_INLINE_SELECTION); } -void +static void location_completion_cleanup(xmlXPathObjectPtr xpathObj, xmlXPathContextPtr xpathCtx, xmlDocPtr doc) { if(xpathObj != NULL) xmlXPathFreeObject(xpathObj); @@ -45,7 +45,7 @@ location_completion_cleanup(xmlXPathObjectPtr xpathObj, xmlXPathContextPtr xpath if(doc != NULL) xmlFreeDoc(doc); } -GtkTreeIter +static GtkTreeIter load_xpath_results(GtkListStore *list_store, xmlNodeSetPtr nodes) { GtkTreeIter iter; @@ -65,8 +65,8 @@ load_xpath_results(GtkListStore *list_store, xmlNodeSetPtr nodes) return iter; } -void -location_completion_init() +GtkTreeModel * +bookmarks_completion_init() { const xmlChar *xpathExpr = "//bookmark/@href"; char *filename = g_build_filename(g_get_user_data_dir(), "badwolf", "bookmarks.xbel", NULL); @@ -76,7 +76,6 @@ location_completion_init() GtkTreeIter iter; GtkListStore *list_store = gtk_list_store_new(1, G_TYPE_STRING); - location_completion = gtk_entry_completion_new(); gtk_list_store_append(list_store, &iter); gtk_list_store_set(list_store, &iter, 0, homepage, -1); @@ -86,7 +85,7 @@ location_completion_init() { g_fprintf(stderr, "Bookmarks: unable to parse file \"%s\"\n", filename); location_completion_cleanup(xpathObj, xpathCtx, doc); - return; + return NULL; } xpathCtx = xmlXPathNewContext(doc); @@ -94,7 +93,7 @@ location_completion_init() { g_fprintf(stderr, "Bookmarks: unable to create new XPath context\n"); location_completion_cleanup(xpathObj, xpathCtx, doc); - return; + return NULL; } xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); @@ -102,13 +101,14 @@ location_completion_init() { g_fprintf(stderr, "Bookmarks: unable to evaluate xpath expression \"%s\"\n", xpathExpr); location_completion_cleanup(xpathObj, xpathCtx, doc); - return; + return NULL; } iter = load_xpath_results(list_store, xpathObj->nodesetval); location_completion_cleanup(xpathObj, xpathCtx, doc); - location_completion_setup(list_store); g_fprintf(stderr, "Bookmarks: Done.\n"); + + return GTK_TREE_MODEL(list_store); } diff --git a/bookmarks.h b/bookmarks.h @@ -1,3 +1,3 @@ #include <gtk/gtk.h> -GtkEntryCompletion *location_completion; -void location_completion_init(); +GtkTreeModel *bookmarks_completion_init(); +void bookmarks_completion_setup(GtkEntryCompletion *location_completion, GtkTreeModel *tree_model);