Allow adding speed dial shortcuts from the context menu

This commit is contained in:
Jean-François Guchens 2009-05-09 16:41:01 +02:00 committed by Christian Dywan
parent eaaaed01dc
commit bd6c23cc71
4 changed files with 225 additions and 50 deletions

View file

@ -66,6 +66,6 @@
<p> </p>
</div>
</div>
<div class="clear" />
<div class="clear"></div>
</body>
</html>

View file

@ -9,6 +9,7 @@
<head>
<title>{title}</title>
<script type="text/javascript" src="{res}/mootools.js"></script>
<style>
body {
text-align: center;
@ -69,7 +70,6 @@
font-size: 11px;
}
div.shortcut a {
border: 1px solid #ccc;
display: block;
@ -136,6 +136,10 @@
.cross:hover {
background-color: #ff7575;
}
.activated p {
cursor: text;
}
</style>
<script type="text/javascript">
@ -167,7 +171,7 @@
var getThumbnail = function (id, url)
{
console.log ("tandoori-get-thumbnail "+ id +" "+ url);
console.log ("speeddial-get-thumbnail " + id + " " + url);
return false;
}
@ -180,12 +184,23 @@
a.setProperty ('href', href);
var cross = new Element ('div', { 'html': 'x' });
cross.setProperty ('onclick', 'clearShortcut("' + id + '");');
cross.addClass ('cross');
cross.inject ($(id), 'top');
$(id).addClass ('activated');
cross.setProperty ('onclick', 'clearShortcut("' + id + '");');
cross.addClass ('cross');
cross.inject ($(id), 'top');
$(id).addClass ('activated');
var p = a.getNext();
p.setProperty('onclick', 'javascript:rename_shortcut("' + id + '");');
}
var rename_shortcut = function (id)
{
var name = prompt ("{enter_shortcut_name}", "");
if (!name) return;
$(id).getLast ().set ('html', name);
}
var clearShortcut = function (id)
{
@ -206,9 +221,9 @@
sc.removeClass ('activated');
a.empty ().set ('html', '<h1>' + num + '</h1><h4><span/></h4>');
a.setProperty ('href', '#');
p.empty ();
p.empty ().removeProperty ('onclick');
console.log ("tandoori-save");
console.log ("speeddial-save");
}
</script>

View file

@ -179,14 +179,15 @@ midori_view_settings_notify_cb (MidoriWebSettings* settings,
MidoriView* view);
static void
midori_view_tandoori_get_thumb (GtkWidget* web_view,
const gchar* message,
MidoriView* view);
midori_view_speeddial_get_thumb (GtkWidget* web_view,
const gchar* message,
MidoriView* view);
static void
midori_view_tandoori_save (GtkWidget* web_view);
midori_view_speeddial_save (GtkWidget* web_view);
static gchar*
midori_view_speeddial_get_next_free_slot (void);
static void
midori_view_class_init (MidoriViewClass* class)
@ -969,6 +970,99 @@ midori_web_view_menu_new_tab_activate_cb (GtkWidget* widget,
view->open_tabs_in_the_background);
}
static void
midori_web_view_menu_action_add_speeddial_cb (GtkWidget* widget,
MidoriView* view)
{
gchar* folder;
gchar* thumb;
gchar* filename;
gchar* replace_from;
gchar* replace_by;
/*gchar* uri = g_strdup (webkit_web_frame_get_uri (
webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (view->web_view))));
gchar* title = g_strdup (webkit_web_frame_get_title (
webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (view->web_view))));*/
gchar* uri = g_strdup (view->uri);
gchar* title = g_strdup (view->title);
gchar* slot_id = midori_view_speeddial_get_next_free_slot ();
if (slot_id == NULL)
{
g_free (uri);
g_free (title);
return;
}
if (strlen (title) > 15)
{
gchar* stripped = g_strndup (title, 15 * sizeof (gchar));
g_free (title);
title = g_strdup_printf ("%s...", stripped);
g_free (stripped);
}
folder = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME, "thumbs", NULL);
thumb = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
filename = g_build_filename (folder, thumb, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
{
GdkPixbuf* img;
GRegex* regex;
gchar* replace;
gchar* file_content;
gchar* encoded;
gchar* speeddial_body;
gchar* t_body_fname;
gsize sz;
t_body_fname = g_strdup_printf ("%s%s", g_get_user_config_dir (),
"/midori/speeddial-body.html");
g_file_get_contents (t_body_fname, &speeddial_body, NULL, NULL);
img = gdk_pixbuf_new_from_file_at_scale (filename, 160, 107, FALSE, NULL);
gdk_pixbuf_save_to_buffer (img, &file_content, &sz, "png", NULL, NULL);
encoded = g_base64_encode ((guchar *)file_content, sz);
replace_from = g_strdup_printf (
"<div class=\"(.+)\" id=\"%s\">[ \r\n\t]*"\
"<a href=\"#\" onclick=\""\
"javascript:return getAction\\('%s'\\);\".*>[ \r\n\t]*<h1>"\
"%s</h1><h4>.+</h4>[ \r\n\t]*</a>[ \r\n\t]*<p>[ ]*</p>[ \r\t\n]*</div>",
slot_id, slot_id, (slot_id+1));
replace_by = g_strdup_printf (
"<div class=\"\\1 activated\" id=\"%s\">"\
"<div onclick=\"clearShortcut"\
"('%s');\" class=\"cross\">x</div><a href=\"%s\" onclick="\
"\"javascript:return getAction('%s');\"><img src="\
"\"data:image/png;base64,%s\" /></a><p onclick="\
"\"javascript:rename_shortcut('%s');\">%s</p></div>",
slot_id, slot_id, uri, slot_id, encoded, slot_id, title);
regex = g_regex_new (replace_from, G_REGEX_MULTILINE, 0, NULL);
replace = g_regex_replace (regex, speeddial_body, -1,
1, replace_by, 0, NULL);
g_file_set_contents (t_body_fname, replace, -1, NULL);
g_object_unref (img);
g_regex_unref (regex);
g_free (encoded);
g_free (file_content);
g_free (t_body_fname);
g_free (speeddial_body);
g_free (replace_by);
g_free (replace_from);
g_free (replace);
}
g_free (thumb);
g_free (filename);
}
static void
midori_web_view_menu_new_window_activate_cb (GtkWidget* widget,
MidoriView* view)
@ -1212,15 +1306,26 @@ webkit_web_view_populate_popup_cb (WebKitWebView* web_view,
G_CALLBACK (midori_web_view_menu_action_activate_cb), view);
/* FIXME: Make this sensitive only when there is a tab to undo */
gtk_widget_show (menuitem);
menuitem = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem);
menuitem = gtk_image_menu_item_new_from_stock (STOCK_BOOKMARK_ADD, NULL);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
g_object_set_data (G_OBJECT (menuitem), "action", "BookmarkAdd");
g_signal_connect (menuitem, "activate",
G_CALLBACK (midori_web_view_menu_action_activate_cb), view);
gtk_widget_show (menuitem);
/* FIXME: This should be a proper action in the browser */
menuitem = gtk_image_menu_item_new_with_mnemonic (_("Add to customized homepage"));
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
g_object_set_data (G_OBJECT (menuitem), "action", "AddSpeeddial");
g_signal_connect (menuitem, "activate",
G_CALLBACK (midori_web_view_menu_action_add_speeddial_cb), view);
gtk_widget_show (menuitem);
menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SAVE_AS, NULL);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
g_object_set_data (G_OBJECT (menuitem), "action", "SaveAs");
@ -1231,6 +1336,7 @@ webkit_web_view_populate_popup_cb (WebKitWebView* web_view,
saving either. If that changes, we need to think of something. */
if (!midori_view_can_view_source (view))
gtk_widget_set_sensitive (menuitem, FALSE);
menuitem = gtk_image_menu_item_new_with_mnemonic (_("View _Source"));
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
g_object_set_data (G_OBJECT (menuitem), "action", "SourceView");
@ -1239,6 +1345,7 @@ webkit_web_view_populate_popup_cb (WebKitWebView* web_view,
gtk_widget_show (menuitem);
if (!midori_view_can_view_source (view))
gtk_widget_set_sensitive (menuitem, FALSE);
menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_PRINT, NULL);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
g_object_set_data (G_OBJECT (menuitem), "action", "Print");
@ -1425,10 +1532,10 @@ webkit_web_view_console_message_cb (GtkWidget* web_view,
const gchar* source_id,
MidoriView* view)
{
if (!strncmp (message, "tandoori-get-thumbnail", 22))
midori_view_tandoori_get_thumb (web_view, message, view);
else if (!strncmp (message, "tandoori-save", 13))
midori_view_tandoori_save (web_view);
if (!strncmp (message, "speeddial-get-thumbnail", 22))
midori_view_speeddial_get_thumb (web_view, message, view);
else if (!strncmp (message, "speeddial-save", 13))
midori_view_speeddial_save (web_view);
else
g_signal_emit (view, signals[CONSOLE_MESSAGE], 0, message, line, source_id);
return TRUE;
@ -1918,36 +2025,36 @@ midori_view_set_uri (MidoriView* view,
SoupServer* res_server;
guint port;
gchar* res_root;
gchar* tandoori_head;
gchar* tandoori_body;
gchar* tandoori_html;
gchar* speeddial_head;
gchar* speeddial_body;
gchar* speeddial_html;
gchar* t_body_fname;
gchar* location_entry_search;
katze_assign (view->uri, g_strdup (""));
g_file_get_contents (DATADIR "/midori/res/tandoori-head.html",
&tandoori_head, NULL, NULL);
g_file_get_contents (DATADIR "/midori/res/speeddial-head.html",
&speeddial_head, NULL, NULL);
res_server = sokoke_get_res_server ();
port = soup_server_get_port (res_server);
res_root = g_strdup_printf ("http://localhost:%d/res", port);
t_body_fname = g_strconcat (g_get_user_config_dir (),
"/midori/tandoori-body.html", NULL);
"/midori/speeddial-body.html", NULL);
if (!g_file_test (t_body_fname, G_FILE_TEST_EXISTS))
{
g_file_get_contents (DATADIR "/midori/res/tandoori-body.html",
&tandoori_body, NULL, NULL );
g_file_set_contents (t_body_fname, tandoori_body, -1, NULL );
g_file_get_contents (DATADIR "/midori/res/speeddial-body.html",
&speeddial_body, NULL, NULL );
g_file_set_contents (t_body_fname, speeddial_body, -1, NULL );
}
else
g_file_get_contents (t_body_fname, &tandoori_body, NULL, NULL);
g_file_get_contents (t_body_fname, &speeddial_body, NULL, NULL);
tandoori_html = g_strconcat (tandoori_head, tandoori_body, NULL);
speeddial_html = g_strconcat (speeddial_head, speeddial_body, NULL);
g_object_get (view->settings, "location-entry-search",
&location_entry_search, NULL);
data = sokoke_replace_variables (tandoori_html,
data = sokoke_replace_variables (speeddial_html,
"{res}", res_root,
"{title}", _("Blank page"),
"{search_uri}", &location_entry_search,
@ -1970,9 +2077,9 @@ midori_view_set_uri (MidoriView* view,
g_free (res_root);
g_free (data);
g_free (tandoori_html);
g_free (tandoori_head);
g_free (tandoori_body);
g_free (speeddial_html);
g_free (speeddial_head);
g_free (speeddial_body);
g_free (t_body_fname);
}
/* This is not prefectly elegant, but creating an
@ -2858,9 +2965,17 @@ midori_view_execute_script (MidoriView* view,
return TRUE;
}
/**
* midori_view_speeddial_inject_thumb
* @view: a #MidoriView
* @filename: filename of the thumbnail
* @dom_id: Id of the shortcut on speeddial page in wich to inject content
* @url: url of the shortcut
*/
static void
midori_view_tandoori_inject_thumb (MidoriView* view,
gchar* filename,
midori_view_speeddial_inject_thumb (MidoriView* view,
gchar* filename,
gchar* dom_id,
gchar* url)
{
@ -2877,13 +2992,13 @@ midori_view_tandoori_inject_thumb (MidoriView* view,
gdk_pixbuf_save_to_buffer (img, &file_content, &sz, "png", NULL, NULL);
encoded = g_base64_encode ( (guchar *)file_content, sz );
/** Call Javascript function to replace shortcut content **/
/* Call Javascript function to replace shortcut's content */
js = g_strdup_printf ("setThumbnail('%s','%s','%s');", dom_id, encoded, url);
webkit_web_view_execute_script (WEBKIT_WEB_VIEW (view->web_view), js);
free (js);
g_object_unref (img);
midori_view_tandoori_save (view->web_view);
midori_view_speeddial_save (view->web_view);
g_free (url);
g_free (dom_id);
@ -2891,16 +3006,21 @@ midori_view_tandoori_inject_thumb (MidoriView* view,
g_free (file_content);
}
/**
* midori_view_tandoori_save
* midori_view_speeddial_save
* @web_view: a #WebkitView
* @message: Console log data
*
* Load a thumbnail, and sets the DOM
* Load a thumbnail, and set the DOM
*
* message[0] == console message call
* message[1] == shortcut id in the DOM
* message[2] == shortcut uri
*
**/
static void
midori_view_tandoori_get_thumb (GtkWidget* web_view,
midori_view_speeddial_get_thumb (GtkWidget* web_view,
const gchar* message,
MidoriView* view)
{
@ -2909,35 +3029,75 @@ midori_view_tandoori_get_thumb (GtkWidget* web_view,
gchar* filename;
gchar** t_data = g_strsplit (message," ", 4);
/* t_data[1] = div id and t_data[2] = shortcut's url */
if (t_data[1] == NULL || t_data[2] == NULL )
return;
folder = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME, "thumbs", NULL);
folder = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME,
"thumbs", NULL);
thumb = g_compute_checksum_for_string (G_CHECKSUM_MD5, t_data[2], -1);
filename = g_build_filename (folder, thumb, NULL);
g_free (thumb);
midori_view_tandoori_inject_thumb (view, filename,
midori_view_speeddial_inject_thumb (view, filename,
g_strdup (t_data[1]), g_strdup (t_data[2]));
g_strfreev (t_data);
}
/**
* midori_view_tandoori_save
* midori_view_speeddial_get_next_free_slot
*
* Returns: Id of the next shortcut slot on the speeddial page
*/
static gchar*
midori_view_speeddial_get_next_free_slot (void)
{
gchar* speeddial_body;
gchar* body_fname;
gchar* slot_id = NULL;
gchar* p = NULL;
body_fname = g_strdup_printf ("%s%s", g_get_user_config_dir (),
"/midori/speeddial-body.html");
if (!g_file_test (body_fname, G_FILE_TEST_EXISTS))
{
g_file_get_contents (DATADIR "/midori/res/speeddial-body.html",
&speeddial_body, NULL, NULL);
g_file_set_contents (body_fname, speeddial_body, -1, NULL);
}
else
g_file_get_contents (body_fname, &speeddial_body, NULL, NULL);
p = g_strstr_len (speeddial_body, -1, "<h1>");
if (p != NULL)
{
p = g_strndup ((p+4), 1);
slot_id = g_strdup_printf ("s%s", p);
g_free (p);
}
g_free (body_fname);
g_free (speeddial_body);
return slot_id;
}
/**
* midori_view_speeddial_save
* @web_view: a #WebkitView
*
* Save tandoori DOM structure to body template
* Save speeddial DOM structure to body template
*
**/
static void
midori_view_tandoori_save (GtkWidget* web_view)
midori_view_speeddial_save (GtkWidget* web_view)
{
JSContextRef js_context = webkit_web_frame_get_global_context (
webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view)));
gchar* newdom = sokoke_js_script_eval (js_context,"document.body.innerHTML", NULL);
gchar* fname = g_strconcat (g_get_user_config_dir (), "/midori/tandoori-body.html", NULL);
g_file_set_contents ( fname, newdom, -1, NULL );
gchar* fname = g_strconcat (g_get_user_config_dir (), "/midori/speeddial-body.html", NULL);
g_file_set_contents (fname, newdom, -1, NULL);
g_free (fname);
g_free (newdom);
}

View file

@ -339,8 +339,8 @@ def build (bld):
else:
Utils.pprint ('BLUE', "logo-shade could not be rasterized.")
bld.install_files ('${DATADIR}/' + APPNAME + '/res', 'data/error.html')
bld.install_files ('${DATADIR}/' + APPNAME + '/res', 'data/tandoori-head.html')
bld.install_files ('${DATADIR}/' + APPNAME + '/res', 'data/tandoori-body.html')
bld.install_files ('${DATADIR}/' + APPNAME + '/res', 'data/speeddial-head.html')
bld.install_files ('${DATADIR}/' + APPNAME + '/res', 'data/speeddial-body.html')
bld.install_files ('${DATADIR}/' + APPNAME + '/res', 'data/mootools.js')
if Options.commands['check']: