summaryrefslogtreecommitdiff
path: root/src/searchengine.c
blob: cabbc76161d6ed7b7af9230bf4271d1d7faebbd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2013 Daniel Carl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

#include "main.h"
#include "searchengine.h"

extern VbCore vb;

typedef struct {
    char* handle;
    char* uri;
} Searchengine;

static GSList* searchengine_find(const char* handle);
static gboolean searchengine_is_valid_uri(const char* uri);
static void searchengine_free(Searchengine* se);


void searchengine_cleanup(void)
{
    if (vb.behave.searchengines) {
        g_slist_free_full(vb.behave.searchengines, (GDestroyNotify)searchengine_free);
    }
}

gboolean searchengine_add(const char* handle, const char* uri)
{
    /* validate if the uri contains only one %s sequence */
    if (!searchengine_is_valid_uri(uri)) {
        return FALSE;
    }
    Searchengine* s = g_new0(Searchengine, 1);

    s->handle = g_strdup(handle);
    s->uri    = g_strdup(uri);

    vb.behave.searchengines = g_slist_prepend(vb.behave.searchengines, s);

    return TRUE;
}

gboolean searchengine_remove(const char* handle)
{
    GSList* list = searchengine_find(handle);

    if (list) {
        searchengine_free((Searchengine*)list->data);
        vb.behave.searchengines = g_slist_delete_link(vb.behave.searchengines, list);

        return TRUE;
    }

    return FALSE;
}

gboolean searchengine_set_default(const char* handle)
{
    /* do not check if the search engin exists to be able to set the default
     * before defining the search engines */
    OVERWRITE_STRING(vb.behave.searchengine_default, handle);

    return TRUE;
}

char* searchengine_get_uri(const char* handle)
{
    const char* def = vb.behave.searchengine_default;
    GSList* l = NULL;

    if (handle && (l = searchengine_find(handle))) {
        return ((Searchengine*)l->data)->uri;
    } else if (def && (l = searchengine_find(def))) {
        return ((Searchengine*)l->data)->uri;
    }

    return NULL;
}

static GSList* searchengine_find(const char* handle)
{
    GSList* s;
    for (s = vb.behave.searchengines; s != NULL; s = s->next) {
        if (!strcmp(((Searchengine*)s->data)->handle, handle)) {
            return s;
        }
    }

    return NULL;
}

static gboolean searchengine_is_valid_uri(const char* uri)
{
    int count = 0;

    for (; *uri; uri++) {
        if (*uri == '%') {
            uri++;
            if (*uri == 's') {
                count++;
            }
        }
    }

    return count == 1;
}

static void searchengine_free(Searchengine* se)
{
    g_free(se->uri);
    g_free(se->handle);
    g_free(se);
}