2009-10-06 18:35:56 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
#define MAXCHARS 20
|
|
|
|
#define MINCHARS 2
|
|
|
|
|
2009-10-06 18:35:56 +00:00
|
|
|
#include <midori/midori.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2009-11-01 22:49:58 +00:00
|
|
|
#if HAVE_SQLITE
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#endif
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
static GHashTable* global_keys;
|
2009-10-06 18:35:56 +00:00
|
|
|
static gchar* jsforms;
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
static gboolean
|
2009-10-06 18:35:56 +00:00
|
|
|
formhistory_prepare_js ()
|
|
|
|
{
|
|
|
|
gchar* autosuggest;
|
|
|
|
gchar* style;
|
|
|
|
guint i;
|
2009-10-07 20:30:16 +00:00
|
|
|
gchar* file;
|
2009-10-06 18:35:56 +00:00
|
|
|
|
2009-10-20 21:54:26 +00:00
|
|
|
gchar* data_path = g_build_filename (MDATADIR, PACKAGE_NAME, "res", NULL);
|
2009-10-07 20:30:16 +00:00
|
|
|
file = g_build_filename (data_path,"/autosuggestcontrol.js",NULL);
|
|
|
|
if (!g_file_test (file, G_FILE_TEST_EXISTS))
|
|
|
|
return FALSE;
|
|
|
|
g_file_get_contents (file, &autosuggest, NULL, NULL);
|
2009-10-06 18:35:56 +00:00
|
|
|
g_strchomp (autosuggest);
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
file = g_build_filename (data_path,"/autosuggestcontrol.css",NULL);
|
|
|
|
if (!g_file_test (file, G_FILE_TEST_EXISTS))
|
|
|
|
return FALSE;
|
|
|
|
g_file_get_contents (file, &style, NULL, NULL);
|
2009-10-06 18:35:56 +00:00
|
|
|
g_strchomp (style);
|
|
|
|
i = 0;
|
|
|
|
while (style[i])
|
|
|
|
{
|
|
|
|
if (style[i] == '\n')
|
|
|
|
style[i] = ' ';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsforms = g_strdup_printf (
|
|
|
|
"%s"
|
|
|
|
"window.addEventListener (\"load\", function () { initSuggestions (); }, true);"
|
|
|
|
"window.addEventListener ('DOMContentLoaded',"
|
|
|
|
"function () {"
|
|
|
|
"var mystyle = document.createElement(\"style\");"
|
|
|
|
"mystyle.setAttribute(\"type\", \"text/css\");"
|
|
|
|
"mystyle.appendChild(document.createTextNode(\"%s\"));"
|
|
|
|
"var head = document.getElementsByTagName(\"head\")[0];"
|
|
|
|
"if (head) head.appendChild(mystyle);"
|
|
|
|
"else document.documentElement.insertBefore(mystyle, document.documentElement.firstChild);"
|
|
|
|
"}, true);",
|
|
|
|
autosuggest,
|
|
|
|
style);
|
|
|
|
g_strstrip (jsforms);
|
2009-10-07 20:30:16 +00:00
|
|
|
g_free (data_path);
|
|
|
|
g_free (file);
|
2009-10-06 18:35:56 +00:00
|
|
|
g_free (style);
|
|
|
|
g_free (autosuggest);
|
2009-10-07 20:30:16 +00:00
|
|
|
return TRUE;
|
2009-10-06 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
formhistory_build_js ()
|
|
|
|
{
|
2009-10-07 21:04:30 +00:00
|
|
|
gchar* suggestions = g_strdup ("");
|
2009-10-07 20:30:16 +00:00
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
2009-10-07 21:04:30 +00:00
|
|
|
gchar* script;
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
g_hash_table_iter_init (&iter, global_keys);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value))
|
2009-10-07 21:04:30 +00:00
|
|
|
{
|
|
|
|
gchar* _suggestions = g_strdup_printf ("%s arr[\"%s\"] = [%s]; ",
|
|
|
|
suggestions, (char*)key, (char*)value);
|
|
|
|
katze_assign (suggestions, _suggestions);
|
|
|
|
}
|
|
|
|
script = g_strdup_printf ("function FormSuggestions(eid) { "
|
|
|
|
"arr = new Array();"
|
|
|
|
"%s"
|
|
|
|
"this.suggestions = arr[eid]; }"
|
|
|
|
"%s",
|
|
|
|
suggestions,
|
|
|
|
jsforms);
|
|
|
|
g_free (suggestions);
|
2009-10-06 18:35:56 +00:00
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
static void
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_update_database (gpointer db,
|
|
|
|
const gchar* key,
|
|
|
|
const gchar* value)
|
|
|
|
{
|
|
|
|
#if HAVE_SQLITE
|
|
|
|
/* FIXME: Write keys to the database */
|
|
|
|
/* g_print (":: %s= %s\n", key, value); */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formhistory_update_main_hash (GHashTable* keys,
|
|
|
|
gpointer db)
|
2009-10-07 20:30:16 +00:00
|
|
|
{
|
|
|
|
GHashTableIter iter;
|
2009-10-12 16:21:12 +00:00
|
|
|
gchar* key;
|
|
|
|
gchar* value;
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
g_hash_table_iter_init (&iter, keys);
|
|
|
|
while (g_hash_table_iter_next (&iter, (gpointer)&key, (gpointer)&value))
|
|
|
|
{
|
2009-10-12 16:21:12 +00:00
|
|
|
guint length;
|
|
|
|
gchar* tmp;
|
|
|
|
|
|
|
|
if (!(value && *value))
|
|
|
|
continue;
|
|
|
|
length = strlen (value);
|
|
|
|
if (length > MAXCHARS || length < MINCHARS)
|
2009-10-07 21:04:30 +00:00
|
|
|
continue;
|
2009-10-07 20:30:16 +00:00
|
|
|
|
2009-11-01 22:49:58 +00:00
|
|
|
if ((tmp = g_hash_table_lookup (global_keys, (gpointer)key)))
|
2009-10-07 20:30:16 +00:00
|
|
|
{
|
|
|
|
gchar* rvalue = g_strdup_printf ("\"%s\"",value);
|
|
|
|
if (!g_regex_match_simple (rvalue, tmp,
|
|
|
|
G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))
|
|
|
|
{
|
2009-10-12 16:21:12 +00:00
|
|
|
gchar* new_value = g_strdup_printf ("%s%s,", tmp, rvalue);
|
2009-10-07 20:30:16 +00:00
|
|
|
g_hash_table_replace (global_keys, key, new_value);
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_update_database (db, key, value);
|
2009-10-07 20:30:16 +00:00
|
|
|
}
|
|
|
|
g_free (rvalue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-12 16:21:12 +00:00
|
|
|
gchar* new_value = g_strdup_printf ("\"%s\",",value);
|
2009-10-07 20:30:16 +00:00
|
|
|
g_hash_table_insert (global_keys, key, new_value);
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_update_database (db, key, value);
|
2009-10-07 20:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 18:35:56 +00:00
|
|
|
static void
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_session_request_queued_cb (SoupSession* session,
|
|
|
|
SoupMessage* msg,
|
|
|
|
MidoriExtension* extension)
|
2009-10-06 18:35:56 +00:00
|
|
|
{
|
|
|
|
gchar* method = katze_object_get_string (msg, "method");
|
2009-10-12 16:16:52 +00:00
|
|
|
if (method && !strncmp (method, "POST", 4))
|
2009-10-06 18:35:56 +00:00
|
|
|
{
|
|
|
|
SoupMessageBody* body = msg->request_body;
|
2009-10-12 16:16:52 +00:00
|
|
|
if (soup_message_body_get_accumulate (body))
|
|
|
|
{
|
2009-11-01 22:49:58 +00:00
|
|
|
SoupBuffer* buffer;
|
|
|
|
GHashTable* keys;
|
|
|
|
gpointer db;
|
|
|
|
|
|
|
|
buffer = soup_message_body_flatten (body);
|
|
|
|
keys = soup_form_decode (body->data);
|
|
|
|
|
|
|
|
db = g_object_get_data (G_OBJECT (extension), "formhistory-db");
|
|
|
|
formhistory_update_main_hash (keys, db);
|
2009-10-12 16:16:52 +00:00
|
|
|
soup_buffer_free (buffer);
|
2009-11-01 22:49:58 +00:00
|
|
|
g_hash_table_destroy (keys);
|
2009-10-12 16:16:52 +00:00
|
|
|
}
|
2009-10-06 18:35:56 +00:00
|
|
|
}
|
|
|
|
g_free (method);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
formhistory_window_object_cleared_cb (GtkWidget* web_view,
|
|
|
|
WebKitWebFrame* web_frame,
|
|
|
|
JSContextRef js_context,
|
|
|
|
JSObjectRef js_window)
|
|
|
|
{
|
|
|
|
webkit_web_view_execute_script (WEBKIT_WEB_VIEW (web_view),
|
|
|
|
formhistory_build_js ());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_add_tab_cb (MidoriBrowser* browser,
|
|
|
|
MidoriView* view,
|
|
|
|
MidoriExtension* extension)
|
2009-10-06 18:35:56 +00:00
|
|
|
{
|
|
|
|
GtkWidget* web_view = gtk_bin_get_child (GTK_BIN (view));
|
|
|
|
SoupSession *session = webkit_get_default_session ();
|
|
|
|
g_signal_connect (web_view, "window-object-cleared",
|
2009-11-01 22:49:58 +00:00
|
|
|
G_CALLBACK (formhistory_window_object_cleared_cb), NULL);
|
2009-10-06 18:35:56 +00:00
|
|
|
g_signal_connect (session, "request-queued",
|
2009-11-01 22:49:58 +00:00
|
|
|
G_CALLBACK (formhistory_session_request_queued_cb), extension);
|
2009-10-06 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formhistory_deactivate_cb (MidoriExtension* extension,
|
|
|
|
MidoriBrowser* browser);
|
|
|
|
|
|
|
|
static void
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_add_tab_foreach_cb (MidoriView* view,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
2009-10-06 18:35:56 +00:00
|
|
|
{
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_add_tab_cb (browser, view, extension);
|
2009-10-06 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formhistory_app_add_browser_cb (MidoriApp* app,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
midori_browser_foreach (browser,
|
2009-11-01 22:49:58 +00:00
|
|
|
(GtkCallback)formhistory_add_tab_foreach_cb, extension);
|
|
|
|
g_signal_connect (browser, "add-tab",
|
|
|
|
G_CALLBACK (formhistory_add_tab_cb), extension);
|
2009-10-06 18:35:56 +00:00
|
|
|
g_signal_connect (extension, "deactivate",
|
|
|
|
G_CALLBACK (formhistory_deactivate_cb), browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-11-01 22:49:58 +00:00
|
|
|
formhistory_deactivate_tabs (MidoriView* view,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
2009-10-06 18:35:56 +00:00
|
|
|
{
|
|
|
|
GtkWidget* web_view = gtk_bin_get_child (GTK_BIN (view));
|
2009-10-07 20:30:16 +00:00
|
|
|
SoupSession *session = webkit_get_default_session ();
|
2009-10-06 18:35:56 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
2009-11-01 22:49:58 +00:00
|
|
|
browser, formhistory_add_tab_cb, extension);
|
2009-10-06 18:35:56 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
2009-11-01 22:49:58 +00:00
|
|
|
web_view, formhistory_window_object_cleared_cb, NULL);
|
2009-10-07 20:30:16 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
2009-11-01 22:49:58 +00:00
|
|
|
session, formhistory_session_request_queued_cb, extension);
|
2009-10-06 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formhistory_deactivate_cb (MidoriExtension* extension,
|
|
|
|
MidoriBrowser* browser)
|
|
|
|
{
|
|
|
|
MidoriApp* app = midori_extension_get_app (extension);
|
2009-11-01 22:49:58 +00:00
|
|
|
#if HAVE_SQLITE
|
|
|
|
sqlite3* db;
|
|
|
|
#endif
|
2009-10-06 18:35:56 +00:00
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
extension, formhistory_deactivate_cb, browser);
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
app, formhistory_app_add_browser_cb, extension);
|
2009-11-01 22:49:58 +00:00
|
|
|
midori_browser_foreach (browser,
|
|
|
|
(GtkCallback)formhistory_deactivate_tabs, extension);
|
2009-10-07 20:30:16 +00:00
|
|
|
|
|
|
|
jsforms = "";
|
|
|
|
if (global_keys)
|
|
|
|
g_hash_table_destroy (global_keys);
|
2009-11-01 22:49:58 +00:00
|
|
|
|
|
|
|
#if HAVE_SQLITE
|
|
|
|
if ((db = g_object_get_data (G_OBJECT (extension), "formhistory-db")))
|
|
|
|
sqlite3_close (db);
|
|
|
|
#endif
|
2009-10-06 18:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
formhistory_activate_cb (MidoriExtension* extension,
|
|
|
|
MidoriApp* app)
|
|
|
|
{
|
2009-11-01 22:49:58 +00:00
|
|
|
#if HAVE_SQLITE
|
|
|
|
const gchar* config_dir;
|
|
|
|
gchar* filename;
|
|
|
|
sqlite3* db;
|
|
|
|
#endif
|
2009-10-06 18:35:56 +00:00
|
|
|
KatzeArray* browsers;
|
|
|
|
MidoriBrowser* browser;
|
|
|
|
guint i;
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
global_keys = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_free);
|
2009-11-01 22:49:58 +00:00
|
|
|
#if HAVE_SQLITE
|
|
|
|
config_dir = midori_extension_get_config_dir (extension);
|
|
|
|
katze_mkdir_with_parents (config_dir, 0700);
|
|
|
|
filename = g_build_filename (config_dir, "forms.db", NULL);
|
|
|
|
if (sqlite3_open (filename, &db))
|
|
|
|
{
|
|
|
|
g_warning (_("Failed to open database: %s\n"), sqlite3_errmsg (db));
|
|
|
|
sqlite3_close (db);
|
|
|
|
}
|
|
|
|
g_free (filename);
|
|
|
|
/* FIXME: Load keys from the database */
|
|
|
|
|
|
|
|
g_object_set_data (G_OBJECT (extension), "formhistory-db", db);
|
|
|
|
#endif
|
|
|
|
|
2009-10-06 18:35:56 +00:00
|
|
|
browsers = katze_object_get_object (app, "browsers");
|
|
|
|
i = 0;
|
|
|
|
while ((browser = katze_array_get_nth_item (browsers, i++)))
|
|
|
|
formhistory_app_add_browser_cb (app, browser, extension);
|
|
|
|
g_signal_connect (app, "add-browser",
|
|
|
|
G_CALLBACK (formhistory_app_add_browser_cb), extension);
|
|
|
|
|
|
|
|
g_object_unref (browsers);
|
|
|
|
}
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
#if G_ENABLE_DEBUG
|
|
|
|
/*
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>autosuggest testcase</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form method=post>
|
|
|
|
<p><input type="text" id="txt1" /></p>
|
|
|
|
<p><input type="text" name="txt2" /></p>
|
|
|
|
<input type=submit>
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html> */
|
|
|
|
#endif
|
|
|
|
|
2009-10-06 18:35:56 +00:00
|
|
|
MidoriExtension*
|
|
|
|
extension_init (void)
|
|
|
|
{
|
2009-10-07 20:30:16 +00:00
|
|
|
gboolean should_init = TRUE;
|
2009-11-01 22:49:58 +00:00
|
|
|
const gchar* ver;
|
2009-10-07 20:30:16 +00:00
|
|
|
gchar* desc;
|
2009-11-01 22:49:58 +00:00
|
|
|
MidoriExtension* extension;
|
|
|
|
|
2009-10-07 20:30:16 +00:00
|
|
|
if (formhistory_prepare_js ())
|
|
|
|
{
|
|
|
|
ver = "0.1";
|
|
|
|
desc = g_strdup (_("Stores history of entered form data"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
desc = g_strdup_printf (_("Not available: %s"),
|
|
|
|
_("Resource files not installed"));
|
|
|
|
ver = NULL;
|
|
|
|
should_init = FALSE;
|
|
|
|
}
|
2009-11-01 22:49:58 +00:00
|
|
|
|
|
|
|
extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
2009-10-06 18:35:56 +00:00
|
|
|
"name", _("Form history filler"),
|
2009-10-07 20:30:16 +00:00
|
|
|
"description", desc,
|
|
|
|
"version", ver,
|
2009-10-06 18:35:56 +00:00
|
|
|
"authors", "Alexander V. Butenko <a.butenka@gmail.com>",
|
|
|
|
NULL);
|
2009-10-07 20:30:16 +00:00
|
|
|
g_free (desc);
|
|
|
|
|
|
|
|
if (should_init)
|
|
|
|
g_signal_connect (extension, "activate",
|
|
|
|
G_CALLBACK (formhistory_activate_cb), NULL);
|
2009-10-06 18:35:56 +00:00
|
|
|
|
|
|
|
return extension;
|
|
|
|
}
|