Restructures searchengine lookup.
authorDaniel Carl <danielcarl@gmx.de>
Fri, 5 Apr 2013 12:18:38 +0000 (14:18 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Fri, 5 Apr 2013 12:18:38 +0000 (14:18 +0200)
Fixe no more working searching for multiple search terms.

src/main.c
src/searchengine.c

index 7493b1e..60f047a 100644 (file)
@@ -126,7 +126,7 @@ gboolean vb_eval_script(WebKitWebFrame *frame, char *script, char *file, char **
 
 gboolean vb_load_uri(const Arg *arg)
 {
-    char *uri, *rp, *path = arg->s;
+    char *uri = NULL, *rp, *path = arg->s;
     struct stat st;
 
     if (!path) {
@@ -138,32 +138,20 @@ gboolean vb_load_uri(const Arg *arg)
         path = vb.config.home_page;
     }
 
-    /* check if the path is a file path */
-    if (stat(path, &st) == 0) {
+    if (g_strrstr(path, "://")) {
+        uri = g_strdup(path);
+    } else if (stat(path, &st) == 0) {
+        /* check if the path is a file path */
         rp  = realpath(path, NULL);
         uri = g_strdup_printf("file://%s", rp);
         free(rp);
-    } else if (!strchr(path, '.' && !strchr(path, '/'))) {
-        char *part  = NULL, *tmpl  = NULL, *query = NULL;
-
-        /* look up for a searchengine with handle */
-        if ((part = strchr(path, ' '))) {
-            *part = '\0';
-            tmpl  = searchengine_get_uri(path);
-            query = soup_uri_encode(part + 1, "&");
-        } else {
-            tmpl  = searchengine_get_uri(NULL);
-            query = soup_uri_encode(path, "&");
-        }
+    } else if (!strchr(path, '.')) {
+        /* use a searchengine */
+        uri = searchengine_get_uri(path);
+    }
 
-        if (tmpl) {
-            uri = g_strdup_printf(tmpl, query);
-        } else {
-            uri = g_strrstr(path, "://") ? g_strdup(path) : g_strdup_printf("http://%s", path);
-        }
-        g_free(query);
-    } else {
-        uri = g_strrstr(path, "://") ? g_strdup(path) : g_strdup_printf("http://%s", path);
+    if (!uri) {
+        uri = g_strdup_printf("http://%s", path);
     }
 
     /* change state to normal mode */
@@ -826,7 +814,7 @@ static void vb_init_files(void)
     char *path = util_get_config_dir();
 
     if (vb.custom_config) {
-        char *rp = realpath(vb.custom_config, NULL); 
+        char *rp = realpath(vb.custom_config, NULL);
         vb.files[FILES_CONFIG] = g_strdup(rp);
         free(rp);
     } else {
index eb97e3e..5f59d60 100644 (file)
@@ -78,15 +78,36 @@ gboolean searchengine_set_default(const char *handle)
     return TRUE;
 }
 
-char *searchengine_get_uri(const char *handle)
+/**
+ * Retrieves the search uri for given query string.
+ * Not that the memory of the returned uri must be freed.
+ */
+char *searchengine_get_uri(const char *string)
 {
-    const char *def = vb.behave.searchengine_default;
     GSList *l = NULL;
+    char *p = NULL, *tmpl = NULL, *query = NULL, *uri = NULL;
+
+    if ((p = strchr(string, ' '))) {
+        *p = '\0';
+        /* is the first word the handle? */
+        if ((l = searchengine_find(string))) {
+            tmpl  = ((Searchengine*)l->data)->uri;
+            query = soup_uri_encode(p + 1, "&");
+        } else {
+            *p = ' ';
+        }
+    }
+
+    if (!tmpl && (l = searchengine_find(vb.behave.searchengine_default))) {
+        tmpl  = ((Searchengine*)l->data)->uri;
+        query = soup_uri_encode(string, "&");
+    }
+
+    if (tmpl) {
+        uri = g_strdup_printf(tmpl, query);
+        g_free(query);
 
-    if (handle && (l = searchengine_find(handle))) {
-        return ((Searchengine*)l->data)->uri;
-    } else if (def && (l = searchengine_find(def))) {
-        return ((Searchengine*)l->data)->uri;
+        return uri;
     }
 
     return NULL;