Pass around SpeedDial and move messages in there

This commit is contained in:
Christian Dywan 2012-09-01 02:08:48 +02:00
parent 9b09760839
commit bc1ff81410
6 changed files with 107 additions and 168 deletions

View file

@ -2508,7 +2508,7 @@ main (int argc,
"trash", trash, "trash", trash,
"search-engines", search_engines, "search-engines", search_engines,
"history", history, "history", history,
"speed-dial", dial->keyfile, "speed-dial", dial,
NULL); NULL);
g_object_unref (history); g_object_unref (history);
g_object_unref (search_engines); g_object_unref (search_engines);

View file

@ -100,7 +100,7 @@ struct _MidoriBrowser
KatzeArray* trash; KatzeArray* trash;
KatzeArray* search_engines; KatzeArray* search_engines;
KatzeArray* history; KatzeArray* history;
GKeyFile* speeddial; MidoriSpeedDial* dial;
gboolean show_tabs; gboolean show_tabs;
gboolean show_navigationbar; gboolean show_navigationbar;
@ -1239,26 +1239,19 @@ midori_browser_add_speed_dial (MidoriBrowser* browser)
{ {
GdkPixbuf* img; GdkPixbuf* img;
GtkWidget* view = midori_browser_get_current_tab (browser); GtkWidget* view = midori_browser_get_current_tab (browser);
gchar* slot_id = midori_speed_dial_get_next_free_slot_fk (browser->speeddial);
gchar* uri;
gchar* title;
if (slot_id == NULL)
return;
uri = g_strdup (midori_view_get_display_uri (MIDORI_VIEW (view)));
title = g_strdup (midori_view_get_display_title (MIDORI_VIEW (view)));
if ((img = midori_view_get_snapshot (MIDORI_VIEW (view), 240, 160))) if ((img = midori_view_get_snapshot (MIDORI_VIEW (view), 240, 160)))
{ {
gchar* slot_id = midori_speed_dial_get_next_free_slot (browser->dial);
gchar* dial_id = g_strdup_printf ("Dial %s", slot_id + 1); gchar* dial_id = g_strdup_printf ("Dial %s", slot_id + 1);
midori_speed_dial_add_fk (dial_id, uri, title, img, browser->speeddial); midori_speed_dial_add (browser->dial, dial_id,
midori_view_get_display_uri (MIDORI_VIEW (view)),
midori_view_get_display_title (MIDORI_VIEW (view)), img);
g_free (dial_id); g_free (dial_id);
midori_view_save_speed_dial_config (MIDORI_VIEW (view), browser->speeddial); midori_view_save_speed_dial_config (MIDORI_VIEW (view));
g_object_unref (img); g_object_unref (img);
}
g_free (uri);
g_free (title);
g_free (slot_id); g_free (slot_id);
}
} }
@ -2408,16 +2401,16 @@ midori_browser_class_init (MidoriBrowserClass* class)
* The speed dial configuration file. * The speed dial configuration file.
* *
* Since: 0.3.4 * Since: 0.3.4
* Since 0.4.7 this is a Midori.SpeedDial instance.
*/ */
g_object_class_install_property (gobject_class, g_object_class_install_property (gobject_class,
PROP_SPEED_DIAL, PROP_SPEED_DIAL,
g_param_spec_pointer ( g_param_spec_pointer (
"speed-dial", "speed-dial",
"Speeddial", "Speeddial",
"Pointer to key-value object with speed dial items", "Speed dial",
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/** /**
* MidoriBrowser:show-tabs: * MidoriBrowser:show-tabs:
* *
@ -6270,7 +6263,7 @@ midori_browser_init (MidoriBrowser* browser)
browser->bookmarks = NULL; browser->bookmarks = NULL;
browser->trash = NULL; browser->trash = NULL;
browser->search_engines = NULL; browser->search_engines = NULL;
browser->speeddial = NULL; browser->dial = NULL;
/* Setup the window metrics */ /* Setup the window metrics */
g_signal_connect (browser, "realize", g_signal_connect (browser, "realize",
@ -6761,7 +6754,7 @@ midori_browser_finalize (GObject* object)
katze_object_assign (browser->trash, NULL); katze_object_assign (browser->trash, NULL);
katze_object_assign (browser->search_engines, NULL); katze_object_assign (browser->search_engines, NULL);
katze_object_assign (browser->history, NULL); katze_object_assign (browser->history, NULL);
browser->speeddial = NULL; browser->dial = NULL;
katze_assign (browser->news_aggregator, NULL); katze_assign (browser->news_aggregator, NULL);
@ -7535,7 +7528,7 @@ midori_browser_set_property (GObject* object,
midori_browser_set_history (browser, g_value_get_object (value)); midori_browser_set_history (browser, g_value_get_object (value));
break; break;
case PROP_SPEED_DIAL: case PROP_SPEED_DIAL:
browser->speeddial = g_value_get_pointer (value); browser->dial = g_value_get_pointer (value);
break; break;
case PROP_SHOW_TABS: case PROP_SHOW_TABS:
browser->show_tabs = g_value_get_boolean (value); browser->show_tabs = g_value_get_boolean (value);
@ -7607,7 +7600,7 @@ midori_browser_get_property (GObject* object,
g_value_set_object (value, browser->history); g_value_set_object (value, browser->history);
break; break;
case PROP_SPEED_DIAL: case PROP_SPEED_DIAL:
g_value_set_pointer (value, browser->speeddial); g_value_set_pointer (value, browser->dial);
break; break;
case PROP_SHOW_TABS: case PROP_SHOW_TABS:
g_value_set_boolean (value, browser->show_tabs); g_value_set_boolean (value, browser->show_tabs);

View file

@ -20,9 +20,12 @@ namespace Sokoke {
namespace Midori { namespace Midori {
public class SpeedDial : GLib.Object { public class SpeedDial : GLib.Object {
string filename;
public GLib.KeyFile keyfile; public GLib.KeyFile keyfile;
string? html = null;
public SpeedDial (string filename, string? fallback = null) { public SpeedDial (string new_filename, string? fallback = null) {
filename = new_filename;
keyfile = new GLib.KeyFile (); keyfile = new GLib.KeyFile ();
try { try {
keyfile.load_from_file (filename, GLib.KeyFileFlags.NONE); keyfile.load_from_file (filename, GLib.KeyFileFlags.NONE);
@ -91,7 +94,7 @@ namespace Midori {
} }
} }
public static string get_next_free_slot_fk (KeyFile keyfile) { public string get_next_free_slot () {
uint slot_count = 0; uint slot_count = 0;
foreach (string tile in keyfile.get_groups ()) { foreach (string tile in keyfile.get_groups ()) {
try { try {
@ -111,7 +114,7 @@ namespace Midori {
return "s%u".printf (slot_count + 1); return "s%u".printf (slot_count + 1);
} }
public static void add_fk (string id, string uri, string title, Gdk.Pixbuf img, KeyFile keyfile) { public void add (string id, string uri, string title, Gdk.Pixbuf img) {
keyfile.set_string (id, "uri", uri); keyfile.set_string (id, "uri", uri);
keyfile.set_string (id, "title", title); keyfile.set_string (id, "title", title);
@ -126,8 +129,11 @@ namespace Midori {
} }
} }
public static string? get_html_fk (KeyFile? keyfile, public unowned string get_html (bool close_buttons_left, GLib.Object view) throws Error {
bool close_buttons_left, GLib.Object view, bool load_missing) throws Error { bool load_missing = true;
if (html != null)
return html;
string? head = null; string? head = null;
string filename = Paths.get_res_filename ("speeddial-head.html"); string filename = Paths.get_res_filename ("speeddial-head.html");
@ -222,10 +228,61 @@ namespace Midori {
""", """,
slot_count + 1, slot_count + 1, _("Click to add a shortcut")); slot_count + 1, slot_count + 1, _("Click to add a shortcut"));
markup.append_printf ("</div>\n</body>\n</html>\n"); markup.append_printf ("</div>\n</body>\n</html>\n");
return markup.str; html = markup.str;
}
else
html = "";
return html;
} }
return null; public void save_message (string message) throws Error {
string msg = message.substring (16, -1);
string[] parts = msg.split (" ", 4);
string action = parts[0];
if (action == "add" || action == "rename"
|| action == "delete" || action == "swap") {
uint slot_id = parts[1].to_int () + 1;
string dial_id = "Dial %u".printf (slot_id);
if (action == "delete") {
string uri = keyfile.get_string (dial_id, "uri");
string file_path = Sokoke.build_thumbnail_path (uri);
keyfile.remove_group (dial_id);
FileUtils.unlink (file_path);
}
else if (action == "add") {
keyfile.set_string (dial_id, "uri", parts[2]);
/* FIXME midori_view_speed_dial_get_thumb (view, dial_id, parts[2]); */
}
else if (action == "rename") {
uint offset = parts[0].length + parts[1].length + 2;
string title = msg.substring (offset, -1);
keyfile.set_string (dial_id, "title", title);
}
else if (action == "swap") {
uint slot2_id = parts[2].to_int () + 1;
string dial2_id = "Dial %u".printf (slot2_id);
string uri = keyfile.get_string (dial_id, "uri");
string title = keyfile.get_string (dial_id, "title");
string uri2 = keyfile.get_string (dial2_id, "uri");
string title2 = keyfile.get_string (dial2_id, "title");
keyfile.set_string (dial_id, "uri", uri2);
keyfile.set_string (dial2_id, "uri", uri);
keyfile.set_string (dial_id, "title", title2);
keyfile.set_string (dial2_id, "title", title);
}
}
}
public void save () throws Error {
html = null;
FileUtils.set_contents (filename, keyfile.to_data ());
} }
} }
} }

View file

@ -225,7 +225,6 @@ enum {
static guint signals[LAST_SIGNAL]; static guint signals[LAST_SIGNAL];
static gchar* speeddial_markup = NULL;
static GtkWidget* thumb_view = NULL; static GtkWidget* thumb_view = NULL;
static GList* thumb_queue = NULL; static GList* thumb_queue = NULL;
@ -253,10 +252,6 @@ midori_view_settings_notify_cb (MidoriWebSettings* settings,
GParamSpec* pspec, GParamSpec* pspec,
MidoriView* view); MidoriView* view);
static void
midori_view_speed_dial_save (MidoriView* view,
const gchar* message);
static void static void
midori_view_class_init (MidoriViewClass* class) midori_view_class_init (MidoriViewClass* class)
{ {
@ -3314,7 +3309,13 @@ webkit_web_view_console_message_cb (GtkWidget* web_view,
return FALSE; return FALSE;
if (!strncmp (message, "speed_dial-save", 13)) if (!strncmp (message, "speed_dial-save", 13))
midori_view_speed_dial_save (view, message); {
MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial");
midori_speed_dial_save_message (dial, message, NULL);
midori_view_save_speed_dial_config (view);
}
else else
g_signal_emit (view, signals[CONSOLE_MESSAGE], 0, message, line, source_id); g_signal_emit (view, signals[CONSOLE_MESSAGE], 0, message, line, source_id);
return TRUE; return TRUE;
@ -4225,17 +4226,6 @@ list_about_uris (GString* markup)
valid_about_uris[i], valid_about_uris[i]); valid_about_uris[i], valid_about_uris[i]);
} }
static gchar*
prepare_speed_dial_html (MidoriView* view,
gboolean load_missing)
{
MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
GKeyFile* key_file = katze_object_get_object (browser, "speed-dial");
return midori_speed_dial_get_html_fk (key_file,
katze_object_get_boolean (view->settings, "close-buttons-left"),
G_OBJECT (view), load_missing, NULL);
}
/** /**
* midori_view_set_uri: * midori_view_set_uri:
@ -4263,6 +4253,9 @@ midori_view_set_uri (MidoriView* view,
{ {
if (!uri || !strcmp (uri, "") || !strcmp (uri, "about:blank")) if (!uri || !strcmp (uri, "") || !strcmp (uri, "about:blank"))
{ {
MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial");
const gchar* html;
#ifdef G_ENABLE_DEBUG #ifdef G_ENABLE_DEBUG
GTimer* timer = NULL; GTimer* timer = NULL;
@ -4275,11 +4268,10 @@ midori_view_set_uri (MidoriView* view,
katze_item_set_meta_string (view->item, "mime-type", NULL); katze_item_set_meta_string (view->item, "mime-type", NULL);
katze_item_set_meta_integer (view->item, "delay", -1); katze_item_set_meta_integer (view->item, "delay", -1);
if (speeddial_markup == NULL) html = midori_speed_dial_get_html (dial,
speeddial_markup = prepare_speed_dial_html (view, TRUE); katze_object_get_boolean (view->settings, "close-buttons-left"),
G_OBJECT (view), NULL);
midori_view_load_alternate_string (view, midori_view_load_alternate_string (view, html, "about:blank", NULL);
speeddial_markup ? speeddial_markup : "", "about:blank", NULL);
#ifdef G_ENABLE_DEBUG #ifdef G_ENABLE_DEBUG
if (midori_debug ("startup")) if (midori_debug ("startup"))
@ -6180,18 +6172,19 @@ thumb_view_load_status_cb (WebKitWebView* thumb_view_,
#if HAVE_OFFSCREEN #if HAVE_OFFSCREEN
GdkPixbuf* pixbuf_scaled; GdkPixbuf* pixbuf_scaled;
#endif #endif
gchar* file_path;
gchar* thumb_dir;
gchar* spec; gchar* spec;
gchar* url; gchar* url;
gchar* dial_id; gchar* dial_id;
MidoriBrowser* browser; MidoriBrowser* browser;
GKeyFile* key_file; MidoriSpeedDial* dial;
const gchar* title; const gchar* title;
if (webkit_web_view_get_load_status (thumb_view_) != WEBKIT_LOAD_FINISHED) if (webkit_web_view_get_load_status (thumb_view_) != WEBKIT_LOAD_FINISHED)
return; return;
browser = midori_browser_get_for_widget (GTK_WIDGET (view));
dial = katze_object_get_object (browser, "speed-dial");
spec = g_object_get_data (G_OBJECT (thumb_view), "spec"); spec = g_object_get_data (G_OBJECT (thumb_view), "spec");
url = strstr (spec, "|") + 1; url = strstr (spec, "|") + 1;
dial_id = g_strndup (spec, url - spec - 1); dial_id = g_strndup (spec, url - spec - 1);
@ -6205,24 +6198,11 @@ thumb_view_load_status_cb (WebKitWebView* thumb_view_,
gtk_widget_realize (thumb_view); gtk_widget_realize (thumb_view);
img = midori_view_web_view_get_snapshot (thumb_view, 240, 160); img = midori_view_web_view_get_snapshot (thumb_view, 240, 160);
#endif #endif
file_path = sokoke_build_thumbnail_path (url);
thumb_dir = g_build_path (G_DIR_SEPARATOR_S, midori_paths_get_cache_dir (), "thumbnails", NULL);
if (!g_file_test (thumb_dir, G_FILE_TEST_EXISTS))
katze_mkdir_with_parents (thumb_dir, 0700);
gdk_pixbuf_save (img, file_path, "png", NULL, "compression", "7", NULL);
g_object_unref (img);
g_free (file_path);
g_free (thumb_dir);
browser = midori_browser_get_for_widget (GTK_WIDGET (view));
g_object_get (browser, "speed-dial", &key_file, NULL);
title = webkit_web_view_get_title (WEBKIT_WEB_VIEW (thumb_view)); title = webkit_web_view_get_title (WEBKIT_WEB_VIEW (thumb_view));
g_key_file_set_string (key_file, dial_id, "title", title ? title : url); midori_speed_dial_add (dial, dial_id, url, title ? title : url, img);
midori_view_save_speed_dial_config (view, key_file); g_object_unref (img);
midori_view_save_speed_dial_config (view);
thumb_queue = g_list_remove (thumb_queue, spec); thumb_queue = g_list_remove (thumb_queue, spec);
if (thumb_queue != NULL) if (thumb_queue != NULL)
@ -6310,110 +6290,16 @@ midori_view_speed_dial_get_thumb (MidoriView* view,
webkit_web_view_load_uri (WEBKIT_WEB_VIEW (thumb_view), url); webkit_web_view_load_uri (WEBKIT_WEB_VIEW (thumb_view), url);
} }
/**
* midori_view_speed_dial_save
* @view: a #MidoriView
* @message: message from JavaScript
*
* Save speed_dial settings
*
**/
static void
midori_view_speed_dial_save (MidoriView* view,
const gchar* message)
{
gchar* action;
GKeyFile* key_file;
MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
gchar* msg = g_strdup (message + 16);
gchar** parts = g_strsplit (msg, " ", 4);
g_object_get (browser, "speed-dial", &key_file, NULL);
action = parts[0];
if (g_str_equal (action, "add") || g_str_equal (action, "rename")
|| g_str_equal (action, "delete") || g_str_equal (action, "swap"))
{
gchar* tmp = g_strdup (parts[1] + 1);
guint slot_id = atoi (tmp);
gchar* dial_id = g_strdup_printf ("Dial %d", slot_id);
g_free (tmp);
if (g_str_equal (action, "delete"))
{
gchar* uri = g_key_file_get_string (key_file, dial_id, "uri", NULL);
gchar* file_path = sokoke_build_thumbnail_path (uri);
g_key_file_remove_group (key_file, dial_id, NULL);
g_unlink (file_path);
g_free (uri);
g_free (file_path);
}
else if (g_str_equal (action, "add"))
{
g_key_file_set_string (key_file, dial_id, "uri", parts[2]);
midori_view_speed_dial_get_thumb (view, dial_id, parts[2]);
}
else if (g_str_equal (action, "rename"))
{
guint offset = strlen (parts[0]) + strlen (parts[1]) + 2;
gchar* title = g_strdup (msg + offset);
g_key_file_set_string (key_file, dial_id, "title", title);
g_free (title);
}
else if (g_str_equal (action, "swap"))
{
gchar* tmp1 = g_strdup (parts[2] + 1);
guint slot2_id = atoi (tmp1);
gchar* dial2_id = g_strdup_printf ("Dial %d", slot2_id);
gchar* uri, *uri2, *title, *title2;
g_free (tmp1);
uri = g_key_file_get_string (key_file, dial_id, "uri", NULL);
title = g_key_file_get_string (key_file, dial_id, "title", NULL);
uri2 = g_key_file_get_string (key_file, dial2_id, "uri", NULL);
title2 = g_key_file_get_string (key_file, dial2_id, "title", NULL);
g_key_file_set_string (key_file, dial_id, "uri", uri2);
g_key_file_set_string (key_file, dial2_id, "uri", uri);
g_key_file_set_string (key_file, dial_id, "title", title2);
g_key_file_set_string (key_file, dial2_id, "title", title);
g_free (uri);
g_free (uri2);
g_free (title);
g_free (title2);
g_free (dial2_id);
}
g_free (dial_id);
}
midori_view_save_speed_dial_config (view, key_file);
g_free (msg);
g_free (action);
}
void void
midori_view_save_speed_dial_config (MidoriView* view, midori_view_save_speed_dial_config (MidoriView* view)
GKeyFile* key_file)
{ {
gchar* config_file;
guint i = 0; guint i = 0;
MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view)); MidoriBrowser* browser = midori_browser_get_for_widget (GTK_WIDGET (view));
MidoriSpeedDial* dial = katze_object_get_object (browser, "speed-dial");
GtkWidget* tab; GtkWidget* tab;
config_file = g_build_filename (midori_paths_get_config_dir (), "speeddial", NULL); midori_speed_dial_save (dial, NULL);
sokoke_key_file_save_to_file (key_file, config_file, NULL);
g_free (config_file);
katze_assign (speeddial_markup, prepare_speed_dial_html (view, FALSE));
while ((tab = midori_browser_get_nth_tab (browser, i++))) while ((tab = midori_browser_get_nth_tab (browser, i++)))
if (midori_view_is_blank (MIDORI_VIEW (tab))) if (midori_view_is_blank (MIDORI_VIEW (tab)))
midori_view_reload (MIDORI_VIEW (tab), FALSE); midori_view_reload (MIDORI_VIEW (tab), FALSE);
} }

View file

@ -291,8 +291,7 @@ midori_view_add_info_bar (MidoriView* view,
...); ...);
void void
midori_view_save_speed_dial_config (MidoriView* view, midori_view_save_speed_dial_config (MidoriView* view);
GKeyFile* key_file);
const gchar* const gchar*
midori_view_fallback_extension (MidoriView* view, midori_view_fallback_extension (MidoriView* view,

View file

@ -9,8 +9,10 @@ midori/midori-browser.c
midori/midori-extension.c midori/midori-extension.c
midori/midori-locationaction.c midori/midori-locationaction.c
midori/midori-panel.c midori/midori-panel.c
midori/midori-settings.vala
midori/midori-websettings.c midori/midori-websettings.c
midori/midori-view.c midori/midori-view.c
midori/midori-speeddial.vala
midori/midori-preferences.c midori/midori-preferences.c
midori/midori-searchaction.c midori/midori-searchaction.c
midori/sokoke.c midori/sokoke.c
@ -27,6 +29,8 @@ katze/katze-item.c
katze/katze-array.c katze/katze-array.c
katze/katze-arrayaction.c katze/katze-arrayaction.c
katze/katze-preferences.c katze/katze-preferences.c
katze/midori-uri.vala
katze/midori-paths.vala
extensions/adblock.c extensions/adblock.c
extensions/addons.c extensions/addons.c
extensions/colorful-tabs.c extensions/colorful-tabs.c