Make bookmarks a property of browser and app

This means we have removed the last remaining
global variable.
This commit is contained in:
Christian Dywan 2008-09-03 02:28:54 +02:00
parent 06d47bf3a9
commit c3f50388a1
5 changed files with 190 additions and 93 deletions

View file

@ -23,6 +23,7 @@
#include "gjs.h"
#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#if ENABLE_NLS
@ -481,6 +482,7 @@ main (int argc,
MidoriStartup load_on_startup;
gchar* homepage;
KatzeArray* search_engines;
KatzeXbelItem* bookmarks;
#if ENABLE_NLS
bindtextdomain (GETTEXT_PACKAGE, MIDORI_LOCALEDIR);
@ -696,12 +698,14 @@ main (int argc,
G_CALLBACK (midori_web_list_add_item_cb), NULL);
g_object_set (app, "settings", settings,
"bookmarks", bookmarks,
"trash", trash,
"search-engines", search_engines,
NULL);
MidoriBrowser* browser = g_object_new (MIDORI_TYPE_BROWSER,
"settings", settings,
"bookmarks", bookmarks,
"trash", trash,
"search-engines", search_engines,
NULL);

View file

@ -12,15 +12,7 @@
#ifndef __MAIN_H__
#define __MAIN_H__ 1
#include <katze/katze.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <glib/gi18n.h>
/* FIXME: Remove these globals */
KatzeXbelItem* bookmarks;
/* Custom stock items

View file

@ -32,6 +32,7 @@ struct _MidoriApp
GtkAccelGroup* accel_group;
MidoriWebSettings* settings;
KatzeXbelItem* bookmarks;
KatzeArray* trash;
KatzeArray* search_engines;
@ -47,10 +48,11 @@ enum
PROP_0,
PROP_SETTINGS,
PROP_BOOKMARKS,
PROP_TRASH,
PROP_SEARCH_ENGINES,
PROP_BROWSER,
PROP_BROWSER_COUNT,
PROP_SEARCH_ENGINES
PROP_BROWSER_COUNT
};
enum {
@ -85,6 +87,8 @@ midori_app_get_property (GObject* object,
static void
midori_app_class_init (MidoriAppClass* class)
{
GObjectClass* gobject_class;
signals[ADD_BROWSER] = g_signal_new (
"add-browser",
G_TYPE_FROM_CLASS (class),
@ -106,15 +110,14 @@ midori_app_class_init (MidoriAppClass* class)
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
GObjectClass* gobject_class = G_OBJECT_CLASS (class);
gobject_class = G_OBJECT_CLASS (class);
gobject_class->constructor = midori_app_constructor;
gobject_class->finalize = midori_app_finalize;
gobject_class->set_property = midori_app_set_property;
gobject_class->get_property = midori_app_get_property;
MidoriAppClass* midoriapp_class = MIDORI_APP_CLASS (class);
midoriapp_class->add_browser = midori_app_add_browser;
midoriapp_class->quit = midori_app_quit;
class->add_browser = midori_app_add_browser;
class->quit = midori_app_quit;
g_object_class_install_property (gobject_class,
PROP_SETTINGS,
@ -125,6 +128,15 @@ midori_app_class_init (MidoriAppClass* class)
MIDORI_TYPE_WEB_SETTINGS,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_BOOKMARKS,
g_param_spec_object (
"bookmarks",
_("Bookmarks"),
_("The bookmarks folder, containing all bookmarks"),
KATZE_TYPE_XBEL_ITEM,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_TRASH,
g_param_spec_object (
@ -134,6 +146,15 @@ midori_app_class_init (MidoriAppClass* class)
KATZE_TYPE_ARRAY,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_SEARCH_ENGINES,
g_param_spec_object (
"search-engines",
_("Search Engines"),
_("The list of search engines"),
KATZE_TYPE_ARRAY,
G_PARAM_READWRITE));
g_object_class_install_property (gobject_class,
PROP_BROWSER,
g_param_spec_object (
@ -151,15 +172,6 @@ midori_app_class_init (MidoriAppClass* class)
_("The current number of browsers"),
0, G_MAXUINT, 0,
G_PARAM_READABLE));
g_object_class_install_property (gobject_class,
PROP_SEARCH_ENGINES,
g_param_spec_object (
"search-engines",
_("Search Engines"),
_("The list of search engines"),
KATZE_TYPE_ARRAY,
G_PARAM_READWRITE));
}
static GObject*
@ -233,8 +245,9 @@ midori_app_init (MidoriApp* app)
app->accel_group = gtk_accel_group_new ();
app->settings = midori_web_settings_new ();
app->trash = katze_array_new (KATZE_TYPE_XBEL_ITEM);
app->search_engines = katze_array_new (KATZE_TYPE_ITEM);
app->bookmarks = NULL;
app->trash = NULL;
app->search_engines = NULL;
#if HAVE_UNIQUE
display_name = g_strdup (gdk_display_get_name (gdk_display_get_default ()));
@ -261,8 +274,14 @@ midori_app_finalize (GObject* object)
g_list_free (app->browsers);
g_object_unref (app->accel_group);
if (app->settings)
g_object_unref (app->settings);
if (app->bookmarks)
g_object_unref (app->bookmarks);
if (app->trash)
g_object_unref (app->trash);
if (app->search_engines)
g_object_unref (app->search_engines);
if (app->instance)
g_object_unref (app->instance);
@ -285,6 +304,11 @@ midori_app_set_property (GObject* object,
g_object_ref (app->settings);
/* FIXME: Propagate settings to all browsers */
break;
case PROP_BOOKMARKS:
katze_object_assign (app->bookmarks, g_value_get_object (value));
g_object_ref (app->bookmarks);
/* FIXME: Propagate bookmarks to all browsers */
break;
case PROP_TRASH:
katze_object_assign (app->trash, g_value_get_object (value));
g_object_ref (app->trash);
@ -314,18 +338,21 @@ midori_app_get_property (GObject* object,
case PROP_SETTINGS:
g_value_set_object (value, app->settings);
break;
case PROP_BOOKMARKS:
g_value_set_object (value, app->bookmarks);
break;
case PROP_TRASH:
g_value_set_object (value, app->trash);
break;
case PROP_SEARCH_ENGINES:
g_value_set_object (value, app->search_engines);
break;
case PROP_BROWSER:
g_value_set_object (value, app->browser);
break;
case PROP_BROWSER_COUNT:
g_value_set_uint (value, g_list_length (app->browsers));
break;
case PROP_SEARCH_ENGINES:
g_value_set_object (value, app->search_engines);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -338,6 +365,7 @@ midori_browser_focus_in_event_cb (MidoriBrowser* browser,
MidoriApp* app)
{
app->browser = browser;
g_object_notify (G_OBJECT (app), "browser");
return FALSE;
}
@ -348,6 +376,7 @@ midori_browser_new_window_cb (MidoriBrowser* browser,
{
MidoriBrowser* new_browser = g_object_new (MIDORI_TYPE_BROWSER,
"settings", app->settings,
"bookmarks", app->bookmarks,
"trash", app->trash,
"search-engines", app->search_engines,
NULL);

View file

@ -74,6 +74,7 @@ struct _MidoriBrowser
gchar* statusbar_text;
MidoriWebSettings* settings;
KatzeXbelItem* bookmarks;
GList* tab_titles;
GList* close_buttons;
@ -93,8 +94,9 @@ enum
PROP_URI,
PROP_TAB,
PROP_STATUSBAR,
PROP_SETTINGS,
PROP_STATUSBAR_TEXT,
PROP_SETTINGS,
PROP_BOOKMARKS,
PROP_TRASH,
PROP_SEARCH_ENGINES
};
@ -680,7 +682,7 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser,
/* FIXME: We want to choose a folder */
if (new_bookmark)
{
katze_xbel_folder_append_item (bookmarks, bookmark);
katze_xbel_folder_append_item (browser->bookmarks, bookmark);
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks);
GtkTreeModel* treemodel = gtk_tree_view_get_model (treeview);
GtkTreeIter iter;
@ -1278,23 +1280,6 @@ midori_browser_class_init (MidoriBrowserClass* class)
GTK_TYPE_STATUSBAR,
G_PARAM_READABLE));
/**
* MidoriBrowser:settings:
*
* An associated settings instance that is shared among all web views.
*
* Setting this value is propagated to every present web view. Also
* every newly created web view will use this instance automatically.
*/
g_object_class_install_property (gobject_class,
PROP_SETTINGS,
g_param_spec_object (
"settings",
_("Settings"),
_("The associated settings"),
MIDORI_TYPE_WEB_SETTINGS,
G_PARAM_READWRITE));
/**
* MidoriBrowser:statusbar-text:
*
@ -1315,6 +1300,42 @@ midori_browser_class_init (MidoriBrowserClass* class)
"",
flags));
/**
* MidoriBrowser:settings:
*
* An associated settings instance that is shared among all web views.
*
* Setting this value is propagated to every present web view. Also
* every newly created web view will use this instance automatically.
*
* If no settings are specified a default will be used.
*/
g_object_class_install_property (gobject_class,
PROP_SETTINGS,
g_param_spec_object (
"settings",
_("Settings"),
_("The associated settings"),
MIDORI_TYPE_WEB_SETTINGS,
G_PARAM_READWRITE));
/**
* MidoriBrowser:bookmarks:
*
* The bookmarks folder, containing all bookmarks.
*
* This is actually a reference to a bookmarks instance,
* so if bookmarks should be used it must be initially set.
*/
g_object_class_install_property (gobject_class,
PROP_BOOKMARKS,
g_param_spec_object (
"bookmarks",
_("Bookmarks"),
_("The bookmarks folder, containing all bookmarks"),
KATZE_TYPE_XBEL_ITEM,
G_PARAM_READWRITE));
/**
* MidoriBrowser:trash:
*
@ -1338,6 +1359,9 @@ midori_browser_class_init (MidoriBrowserClass* class)
* MidoriBrowser:search-engines:
*
* The list of search engines to be used for web search.
*
* This is actually a reference to a search engines instance,
* so if search engines should be used it must be initially set.
*/
g_object_class_install_property (gobject_class,
PROP_SEARCH_ENGINES,
@ -3174,6 +3198,11 @@ midori_browser_init (MidoriBrowser* browser)
GtkToolItem* toolitem;
GtkRcStyle* rcstyle;
browser->settings = midori_web_settings_new ();
browser->bookmarks = NULL;
browser->trash = NULL;
browser->search_engines = NULL;
/* Setup the window metrics */
g_signal_connect (browser, "realize",
G_CALLBACK (midori_browser_realize_cb), browser);
@ -3347,48 +3376,6 @@ midori_browser_init (MidoriBrowser* browser)
GTK_ICON_SIZE_MENU);
gtk_toolbar_set_style (GTK_TOOLBAR (browser->bookmarkbar),
GTK_TOOLBAR_BOTH_HORIZ);
_midori_browser_create_bookmark_menu (browser, bookmarks,
browser->menu_bookmarks);
for (i = 0; i < katze_xbel_folder_get_n_items (bookmarks); i++)
{
KatzeXbelItem* item = katze_xbel_folder_get_nth_item (bookmarks, i);
const gchar* title = katze_xbel_item_is_separator (item)
? "" : katze_xbel_item_get_title (item);
const gchar* desc = katze_xbel_item_is_separator (item)
? "" : katze_xbel_item_get_desc (item);
switch (katze_xbel_item_get_kind (item))
{
case KATZE_XBEL_ITEM_KIND_FOLDER:
toolitem = gtk_tool_button_new_from_stock (GTK_STOCK_DIRECTORY);
gtk_tool_button_set_label (GTK_TOOL_BUTTON (toolitem), title);
gtk_tool_item_set_is_important(toolitem, TRUE);
g_signal_connect (toolitem, "clicked",
G_CALLBACK (midori_browser_bookmarkbar_folder_activate_cb),
browser);
if (desc && *desc)
gtk_tool_item_set_tooltip_text (toolitem, desc);
g_object_set_data (G_OBJECT (toolitem), "KatzeXbelItem", item);
break;
case KATZE_XBEL_ITEM_KIND_BOOKMARK:
toolitem = gtk_tool_button_new_from_stock (STOCK_BOOKMARK);
gtk_tool_button_set_label (GTK_TOOL_BUTTON (toolitem), title);
gtk_tool_item_set_is_important(toolitem, TRUE);
g_signal_connect (toolitem, "clicked",
G_CALLBACK (midori_browser_menu_bookmarks_item_activate_cb),
browser);
if (desc && *desc)
gtk_tool_item_set_tooltip_text (toolitem, desc);
g_object_set_data (G_OBJECT (toolitem), "KatzeXbelItem", item);
break;
case KATZE_XBEL_ITEM_KIND_SEPARATOR:
toolitem = gtk_separator_tool_item_new ();
break;
default:
g_warning ("Unknown item kind");
}
gtk_toolbar_insert (GTK_TOOLBAR (browser->bookmarkbar), toolitem, -1);
}
sokoke_container_show_children (GTK_CONTAINER (browser->bookmarkbar));
gtk_box_pack_start (GTK_BOX (vbox), browser->bookmarkbar, FALSE, FALSE, 0);
/* Superuser warning */
@ -3431,7 +3418,6 @@ midori_browser_init (MidoriBrowser* browser)
(GtkTreeCellDataFunc)midori_browser_bookmarks_item_render_text_cb,
treeview, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
_tree_store_insert_folder (GTK_TREE_STORE (treestore), NULL, bookmarks);
g_object_unref (treestore);
g_object_connect (treeview,
"signal::row-activated",
@ -3452,6 +3438,7 @@ midori_browser_init (MidoriBrowser* browser)
gtk_widget_show_all (box);
GtkWidget* toolbar = gtk_ui_manager_get_widget (ui_manager,
"/toolbar_bookmarks");
_action_set_sensitive (browser, "BookmarkAdd", FALSE);
gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_MENU);
gtk_widget_show_all (toolbar);
midori_panel_append_page (MIDORI_PANEL (browser->panel),
@ -3648,6 +3635,8 @@ midori_browser_finalize (GObject* object)
if (browser->settings)
g_object_unref (browser->settings);
if (browser->bookmarks)
g_object_unref (browser->bookmarks);
if (browser->trash)
g_object_unref (browser->trash);
if (browser->search_engines)
@ -3826,6 +3815,78 @@ midori_browser_settings_notify (MidoriWebSettings* web_settings,
g_value_unset (&value);
}
static void
midori_browser_load_bookmarks (MidoriBrowser* browser)
{
guint i, n;
KatzeXbelItem* item;
const gchar* title;
const gchar* desc;
GtkToolItem* toolitem;
GtkTreeModel* treestore;
// FIXME: Clear bookmarks menu
// FIXME: Clear bookmarkbar
// FIXME: Clear bookmark panel
_action_set_sensitive (browser, "BookmarkAdd", FALSE);
if (!browser->bookmarks)
return;
_midori_browser_create_bookmark_menu (browser, browser->bookmarks,
browser->menu_bookmarks);
n = katze_xbel_folder_get_n_items (browser->bookmarks);
for (i = 0; i < n; i++)
{
item = katze_xbel_folder_get_nth_item (browser->bookmarks, i);
title = katze_xbel_item_is_separator (item)
? "" : katze_xbel_item_get_title (item);
desc = katze_xbel_item_is_separator (item)
? "" : katze_xbel_item_get_desc (item);
switch (katze_xbel_item_get_kind (item))
{
case KATZE_XBEL_ITEM_KIND_FOLDER:
toolitem = gtk_tool_button_new_from_stock (GTK_STOCK_DIRECTORY);
gtk_tool_button_set_label (GTK_TOOL_BUTTON (toolitem), title);
gtk_tool_item_set_is_important (toolitem, TRUE);
g_signal_connect (toolitem, "clicked",
G_CALLBACK (midori_browser_bookmarkbar_folder_activate_cb),
browser);
if (desc && *desc)
gtk_tool_item_set_tooltip_text (toolitem, desc);
g_object_set_data (G_OBJECT (toolitem), "KatzeXbelItem", item);
break;
case KATZE_XBEL_ITEM_KIND_BOOKMARK:
toolitem = gtk_tool_button_new_from_stock (STOCK_BOOKMARK);
gtk_tool_button_set_label (GTK_TOOL_BUTTON (toolitem), title);
gtk_tool_item_set_is_important (toolitem, TRUE);
g_signal_connect (toolitem, "clicked",
G_CALLBACK (midori_browser_menu_bookmarks_item_activate_cb),
browser);
if (desc && *desc)
gtk_tool_item_set_tooltip_text (toolitem, desc);
g_object_set_data (G_OBJECT (toolitem), "KatzeXbelItem", item);
break;
case KATZE_XBEL_ITEM_KIND_SEPARATOR:
toolitem = gtk_separator_tool_item_new ();
break;
default:
g_warning ("Unknown item kind");
}
gtk_toolbar_insert (GTK_TOOLBAR (browser->bookmarkbar), toolitem, -1);
}
sokoke_container_show_children (GTK_CONTAINER (browser->bookmarkbar));
treestore = gtk_tree_view_get_model (GTK_TREE_VIEW (browser->panel_bookmarks));
_tree_store_insert_folder (GTK_TREE_STORE (treestore),
NULL, browser->bookmarks);
midori_panel_bookmarks_cursor_or_row_changed_cb (
GTK_TREE_VIEW (browser->panel_bookmarks), browser);
_action_set_sensitive (browser, "BookmarkAdd", TRUE);
}
static void
midori_browser_set_property (GObject* object,
guint prop_id,
@ -3863,6 +3924,13 @@ midori_browser_set_property (GObject* object,
(GtkCallback) midori_web_view_set_settings,
browser->settings);
break;
case PROP_BOOKMARKS:
; /* FIXME: Disconnect handlers */
katze_object_assign (browser->bookmarks, g_value_get_object (value));
g_object_ref (browser->bookmarks);
midori_browser_load_bookmarks (browser);
/* FIXME: Connect to updates */
break;
case PROP_TRASH:
; /* FIXME: Disconnect handlers */
katze_object_assign (browser->trash, g_value_get_object (value));
@ -3923,6 +3991,9 @@ midori_browser_get_property (GObject* object,
case PROP_SETTINGS:
g_value_set_object (value, browser->settings);
break;
case PROP_BOOKMARKS:
g_value_set_object (value, browser->bookmarks);
break;
case PROP_TRASH:
g_value_set_object (value, browser->trash);
break;

View file

@ -20,11 +20,12 @@
#include "sokoke.h"
#include "compat.h"
#include <string.h>
#if HAVE_GIO
#include <gio/gio.h>
#endif
#include <glib/gi18n.h>
#include <webkit/webkit.h>
#include <string.h>
/* This is unstable API, so we need to declare it */
gchar*