~midori/midori/trunk

« back to all changes in this revision

Viewing changes to extensions/formhistory/formhistory-js-frontend.c

  • Committer: Tarmac
  • Author(s): Christian Dywan
  • Date: 2013-06-19 21:00:46 UTC
  • mfrom: (6216.1.10 midori.butcher)
  • Revision ID: tarmac-20130619210046-i3yv8cnkyjuafubb
Bump WebKit requirement to 1.8.3 and drop support for earlier versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright (C) 2009-2012 Alexander Butenko <a.butenka@gmail.com>
3
 
 Copyright (C) 2009-2012 Christian Dywan <christian@twotoasts.de>
4
 
 
5
 
 This library is free software; you can redistribute it and/or
6
 
 modify it under the terms of the GNU Lesser General Public
7
 
 License as published by the Free Software Foundation; either
8
 
 version 2.1 of the License, or (at your option) any later version.
9
 
*/
10
 
#include "formhistory-frontend.h"
11
 
#ifdef FORMHISTORY_USE_JS
12
 
 
13
 
FormHistoryPriv*
14
 
formhistory_private_new ()
15
 
{
16
 
    FormHistoryPriv* priv = g_slice_new (FormHistoryPriv);
17
 
    return priv;
18
 
}
19
 
 
20
 
gboolean
21
 
formhistory_construct_popup_gui (FormHistoryPriv* priv)
22
 
{
23
 
    gchar* autosuggest;
24
 
    gchar* style;
25
 
    guint i;
26
 
    gchar* file;
27
 
 
28
 
    file = midori_app_find_res_filename ("autosuggestcontrol.js");
29
 
    if (!g_file_get_contents (file, &autosuggest, NULL, NULL))
30
 
    {
31
 
        g_free (file);
32
 
        return FALSE;
33
 
    }
34
 
    g_strchomp (autosuggest);
35
 
 
36
 
    katze_assign (file, midori_app_find_res_filename ("autosuggestcontrol.css"));
37
 
    if (!g_file_get_contents (file, &style, NULL, NULL))
38
 
    {
39
 
        g_free (file);
40
 
        return FALSE;
41
 
    }
42
 
    g_strchomp (style);
43
 
    g_free (file);
44
 
 
45
 
    i = 0;
46
 
    while (style[i])
47
 
    {
48
 
        if (style[i] == '\n')
49
 
            style[i] = ' ';
50
 
        i++;
51
 
    }
52
 
 
53
 
    priv->jsforms = g_strdup_printf (
54
 
         "%s"
55
 
         "window.addEventListener ('DOMContentLoaded',"
56
 
         "function () {"
57
 
         "   if (document.getElementById('formhistory'))"
58
 
         "       return;"
59
 
         "   if (!initSuggestions ())"
60
 
         "       return;"
61
 
         "   var mystyle = document.createElement('style');"
62
 
         "   mystyle.setAttribute('type', 'text/css');"
63
 
         "   mystyle.setAttribute('id', 'formhistory');"
64
 
         "   mystyle.appendChild(document.createTextNode('%s'));"
65
 
         "   var head = document.getElementsByTagName('head')[0];"
66
 
         "   if (head) head.appendChild(mystyle);"
67
 
         "}, true);",
68
 
         autosuggest,
69
 
         style);
70
 
    g_strstrip (priv->jsforms);
71
 
    g_free (style);
72
 
    g_free (autosuggest);
73
 
    return TRUE;
74
 
}
75
 
 
76
 
void
77
 
formhistory_setup_suggestions (WebKitWebView*   web_view,
78
 
                               JSContextRef     js_context,
79
 
                               MidoriExtension* extension)
80
 
{
81
 
    GString* suggestions;
82
 
    FormHistoryPriv* priv;
83
 
    static sqlite3_stmt* stmt;
84
 
    const char* sqlcmd;
85
 
    const unsigned char* key;
86
 
    const unsigned char* value;
87
 
 
88
 
    gint result, pos;
89
 
 
90
 
    priv = g_object_get_data (G_OBJECT (extension), "priv");
91
 
    if (!priv->db)
92
 
        return;
93
 
 
94
 
    if (!stmt)
95
 
    {
96
 
        sqlcmd = "SELECT DISTINCT group_concat(value,'\",\"'), field FROM forms \
97
 
                         GROUP BY field ORDER BY field";
98
 
        sqlite3_prepare_v2 (priv->db, sqlcmd, strlen (sqlcmd) + 1, &stmt, NULL);
99
 
    }
100
 
    result = sqlite3_step (stmt);
101
 
    if (result != SQLITE_ROW)
102
 
    {
103
 
        if (result == SQLITE_ERROR)
104
 
            g_print (_("Failed to select suggestions\n"));
105
 
        sqlite3_reset (stmt);
106
 
        return;
107
 
    }
108
 
    suggestions = g_string_new (
109
 
        "function FormSuggestions(eid) { "
110
 
        "arr = new Array();");
111
 
 
112
 
    while (result == SQLITE_ROW)
113
 
    {
114
 
        pos++;
115
 
        value = sqlite3_column_text (stmt, 0);
116
 
        key = sqlite3_column_text (stmt, 1);
117
 
        if (value)
118
 
        {
119
 
            g_string_append_printf (suggestions, " arr[\"%s\"] = [\"%s\"]; ",
120
 
                                (gchar*)key, (gchar*)value);
121
 
        }
122
 
        result = sqlite3_step (stmt);
123
 
    }
124
 
    g_string_append (suggestions, "this.suggestions = arr[eid]; }");
125
 
    g_string_append (suggestions, priv->jsforms);
126
 
    sokoke_js_script_eval (js_context, suggestions->str, NULL);
127
 
    g_string_free (suggestions, TRUE);
128
 
}
129
 
 
130
 
void
131
 
formhistory_private_destroy (FormHistoryPriv *priv)
132
 
{
133
 
    if (priv->db)
134
 
    {
135
 
        sqlite3_close (priv->db);
136
 
        priv->db = NULL;
137
 
    }
138
 
    katze_assign (priv->jsforms, NULL);
139
 
    g_slice_free (FormHistoryPriv, priv);
140
 
}
141
 
#endif