From: Jean-François Guchens Date: Sat, 9 May 2009 14:41:01 +0000 (+0200) Subject: Allow adding speed dial shortcuts from the context menu X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd6c23cc7120c5106f1f3ef2a6b50cdd8236f821;p=midori Allow adding speed dial shortcuts from the context menu --- diff --git a/data/speeddial-body.html b/data/speeddial-body.html new file mode 100644 index 00000000..1ae3f1ef --- /dev/null +++ b/data/speeddial-body.html @@ -0,0 +1,71 @@ + + + +
+ +
+ +

1

+
+

+
+
+ +

2

+
+

+
+
+ +

3

+
+

+
+
+ +

4

+
+

+
+
+ +

5

+
+

+
+
+ +

6

+
+

+
+
+ +

7

+
+

+
+
+ +

8

+
+

+
+
+ +

9

+
+

+
+
+
+ + diff --git a/data/speeddial-head.html b/data/speeddial-head.html new file mode 100644 index 00000000..22f3924b --- /dev/null +++ b/data/speeddial-head.html @@ -0,0 +1,231 @@ + + + + + + {title} + + + + + + + diff --git a/data/tandoori-body.html b/data/tandoori-body.html deleted file mode 100644 index a99f3da5..00000000 --- a/data/tandoori-body.html +++ /dev/null @@ -1,71 +0,0 @@ - - - -
- -
- -

1

-
-

-
-
- -

2

-
-

-
-
- -

3

-
-

-
-
- -

4

-
-

-
-
- -

5

-
-

-
-
- -

6

-
-

-
-
- -

7

-
-

-
-
- -

8

-
-

-
-
- -

9

-
-

-
-
-
- - diff --git a/data/tandoori-head.html b/data/tandoori-head.html deleted file mode 100644 index e1051b79..00000000 --- a/data/tandoori-head.html +++ /dev/null @@ -1,216 +0,0 @@ - - - - - - {title} - - - - - - diff --git a/midori/midori-view.c b/midori/midori-view.c index 9649a455..b4a9f3f1 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -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 ( + "
[ \r\n\t]*"\ + "[ \r\n\t]*

"\ + "%s

.+

[ \r\n\t]*
[ \r\n\t]*

[ ]*

[ \r\t\n]*
", + slot_id, slot_id, (slot_id+1)); + + replace_by = g_strdup_printf ( + "
"\ + "
x

%s

", + 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_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, "

"); + + 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_tandoori_save + * 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); } diff --git a/wscript b/wscript index c05f0506..4fcb61c9 100644 --- a/wscript +++ b/wscript @@ -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']: