#include "bookmarks.h" #include "badwolf.h" #include "config.h" #include /* g_fprintf() */ #include #include gboolean location_completion_matches(GtkEntryCompletion *completion, const gchar *key, GtkTreeIter *iter, gpointer user_data) { (void)user_data; gchar *buffer; gchar *pattern; gboolean result; pattern = g_strdup_printf("*%s*", key); gtk_tree_model_get(gtk_entry_completion_get_model(completion), iter, 0, &buffer, -1); result = g_pattern_match_simple(pattern, buffer); g_free(buffer); g_free(pattern); return result; } void location_completion_setup(GtkListStore *list_store) { gtk_entry_completion_set_model(location_completion, GTK_TREE_MODEL(list_store)); 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 location_completion_cleanup(xmlXPathObjectPtr xpathObj, xmlXPathContextPtr xpathCtx, xmlDocPtr doc) { if(xpathObj != NULL) xmlXPathFreeObject(xpathObj); if(xpathCtx != NULL) xmlXPathFreeContext(xpathCtx); if(doc != NULL) xmlFreeDoc(doc); } GtkTreeIter load_xpath_results(GtkListStore *list_store, xmlNodeSetPtr nodes) { GtkTreeIter iter; int size; size = (nodes) ? nodes->nodeNr : 0; g_fprintf(stderr, "Bookmarks: Found %d bookmarks.\n", size); for(int i = 0; i < size; i++) if(nodes->nodeTab[i]) { gtk_list_store_append(list_store, &iter); gtk_list_store_set( list_store, &iter, 0, (char *)xmlXPathCastNodeToString(nodes->nodeTab[i]), -1); } return iter; } void location_completion_init() { const xmlChar *xpathExpr = "//bookmark/@href"; char *filename = g_build_filename(g_get_user_data_dir(), "badwolf", "bookmarks.xbel", NULL); xmlDocPtr doc = NULL; xmlXPathContextPtr xpathCtx = NULL; xmlXPathObjectPtr xpathObj = NULL; 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); g_fprintf(stderr, "Bookmarks: loading at %s\n", filename); doc = xmlParseFile(filename); if(doc == NULL) { g_fprintf(stderr, "Bookmarks: unable to parse file \"%s\"\n", filename); location_completion_cleanup(xpathObj, xpathCtx, doc); return; } xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { g_fprintf(stderr, "Bookmarks: unable to create new XPath context\n"); location_completion_cleanup(xpathObj, xpathCtx, doc); return; } xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx); if(xpathObj == NULL) { g_fprintf(stderr, "Bookmarks: unable to evaluate xpath expression \"%s\"\n", xpathExpr); location_completion_cleanup(xpathObj, xpathCtx, doc); return; } 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"); }