diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/bookmarks/ephy-bookmarksbar-model.c | 583 | ||||
-rwxr-xr-x | src/bookmarks/ephy-bookmarksbar-model.h | 76 | ||||
-rw-r--r-- | src/bookmarks/ephy-bookmarksbar.c | 400 | ||||
-rw-r--r-- | src/bookmarks/ephy-bookmarksbar.h | 64 |
4 files changed, 1123 insertions, 0 deletions
diff --git a/src/bookmarks/ephy-bookmarksbar-model.c b/src/bookmarks/ephy-bookmarksbar-model.c new file mode 100755 index 000000000..34b80e004 --- /dev/null +++ b/src/bookmarks/ephy-bookmarksbar-model.c @@ -0,0 +1,583 @@ +/* + * Copyright (C) 2003-2004 Marco Pesenti Gritti + * Copyright (C) 2003-2004 Christian Persch + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "ephy-bookmarksbar-model.h" +#include "ephy-bookmarks.h" +#include "ephy-dnd.h" +#include "ephy-node-common.h" +#include "ephy-file-helpers.h" +#include "ephy-history.h" +#include "ephy-shell.h" +#include "ephy-string.h" +#include "ephy-debug.h" + +#include <string.h> +#include <glib/gi18n.h> + +#define EPHY_BOOKMARKSBARS_XML_FILE "epiphany-bookmarksbar.xml" +#define EPHY_BOOKMARKSBARS_XML_VERSION "1.0" + +enum +{ + PROP_0, + PROP_BOOKMARKS +}; + +enum +{ + URL, + NAME +}; + +#define EPHY_BOOKMARKSBAR_MODEL_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_BOOKMARKSBAR_MODEL, EphyBookmarksBarModelPrivate)) + +struct EphyBookmarksBarModelPrivate +{ + EphyBookmarks *bookmarks; + char *xml_file; + guint timeout; +}; + +static void ephy_bookmarksbar_model_class_init (EphyBookmarksBarModelClass *klass); +static void ephy_bookmarksbar_model_init (EphyBookmarksBarModel *t); + +static GObjectClass *parent_class = NULL; + +GType +ephy_bookmarksbar_model_get_type (void) +{ + static GType type = 0; + + if (type == 0) + { + static const GTypeInfo our_info = { + sizeof (EphyBookmarksBarModelClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) ephy_bookmarksbar_model_class_init, + NULL, + NULL, /* class_data */ + sizeof (EphyBookmarksBarModel), + 0, /* n_preallocs */ + (GInstanceInitFunc) ephy_bookmarksbar_model_init + }; + + type = g_type_register_static (EGG_TYPE_TOOLBARS_MODEL, + "EphyBookmarksBarModel", + &our_info, 0); + } + + return type; +} + +static gboolean +get_toolbar_and_item_pos (EphyBookmarksBarModel *model, + const char *name, + int *toolbar, + int *position) +{ + EggToolbarsModel *eggmodel = EGG_TOOLBARS_MODEL (model); + int n_toolbars, n_items; + int t,i; + + n_toolbars = egg_toolbars_model_n_toolbars (eggmodel); + + for (t = 0; t < n_toolbars; t++) + { + n_items = egg_toolbars_model_n_items (eggmodel, t); + + for (i = 0; i < n_items; i++) + { + const char *i_name; + gboolean is_separator; + + egg_toolbars_model_item_nth (eggmodel, t, + i, &is_separator, NULL, &i_name); + g_return_val_if_fail (i_name != NULL, FALSE); + + if (strcmp (i_name, name) == 0) + { + if (toolbar) *toolbar = t; + if (position) *position = i; + + return TRUE; + } + } + } + + return FALSE; +} + +static int +get_toolbar_pos (EphyBookmarksBarModel *model, + const char *name) +{ + EggToolbarsModel *eggmodel = EGG_TOOLBARS_MODEL (model); + int i, n_toolbars; + + n_toolbars = egg_toolbars_model_n_toolbars (eggmodel); + + for (i = 0; i < n_toolbars; i++) + { + const char *t_name; + + t_name = egg_toolbars_model_toolbar_nth (eggmodel, i); + if (strcmp (name, t_name) == 0) + { + return i; + } + } + + return -1; +} + +char * +ephy_bookmarksbar_model_get_action_name (EphyBookmarksBarModel *model, + long id) +{ + return g_strdup_printf ("GoBookmark-%ld", id); +} + +EphyNode * +ephy_bookmarksbar_model_get_node (EphyBookmarksBarModel *model, + const char *action_name) +{ + EphyBookmarks *bookmarks = EPHY_BOOKMARKSBAR_MODEL (model)->priv->bookmarks; + long node_id; + + if (!ephy_string_to_int (action_name + strlen ("GoBookmark-"), &node_id)) + { + return NULL; + } + + return ephy_bookmarks_get_from_id (bookmarks, node_id); +} + +void +ephy_bookmarksbar_model_add_bookmark (EphyBookmarksBarModel *model, + gboolean topic, + long id) +{ + char *name; + int toolbar_position; + + toolbar_position = get_toolbar_pos (model, "BookmarksBar"); + g_return_if_fail (toolbar_position != -1); + + name = ephy_bookmarksbar_model_get_action_name (model, id); + egg_toolbars_model_add_item (EGG_TOOLBARS_MODEL (model), + toolbar_position, -1, name, + topic ? EPHY_DND_TOPIC_TYPE : + EPHY_DND_URL_TYPE); + g_free (name); +} + +void +ephy_bookmarksbar_model_remove_bookmark (EphyBookmarksBarModel *model, + long id) +{ +/* char *action_name; + int toolbar, position; + + action_name = ephy_bookmarksbar_model_get_action_name (model, id); + g_return_if_fail (action_name != NULL); + + if (get_toolbar_and_item_pos (model, action_name, &toolbar, &position)) + { + egg_toolbars_model_remove_item (EGG_TOOLBARS_MODEL (model), + toolbar, position); + } + + g_free (action_name);*/ +} + +gboolean +ephy_bookmarksbar_model_has_bookmark (EphyBookmarksBarModel *model, + long id) +{ + char *action_name; + gboolean found; + int toolbar, pos; + + action_name = ephy_bookmarksbar_model_get_action_name (model, id); + g_return_val_if_fail (action_name != NULL, FALSE); + + found = get_toolbar_and_item_pos (model, action_name, &toolbar, &pos); + + g_free (action_name); + + return found; +} + +static gboolean +save_changes_idle (EphyBookmarksBarModel *model) +{ + LOG ("Saving bookmarks toolbars model") + + egg_toolbars_model_save + (EGG_TOOLBARS_MODEL (model), + model->priv->xml_file, + EPHY_BOOKMARKSBARS_XML_VERSION); + + model->priv->timeout = 0; + + /* don't run again */ + return FALSE; +} + +static void +save_changes (EphyBookmarksBarModel *model) +{ + if (model->priv->timeout == 0) + { + model->priv->timeout = + g_idle_add ((GSourceFunc) save_changes_idle, model); + } +} + +static void +update_flags_and_save_changes (EphyBookmarksBarModel *model) +{ + EggToolbarsModel *eggmodel = EGG_TOOLBARS_MODEL (model); + int i, n_toolbars; + int flag = 0; + + n_toolbars = egg_toolbars_model_n_toolbars (eggmodel); + + if (n_toolbars <= 1) + { + flag = EGG_TB_MODEL_NOT_REMOVABLE; + } + + for (i = 0; i < n_toolbars; i++) + { + const char *t_name; + + t_name = egg_toolbars_model_toolbar_nth (eggmodel, i); + g_return_if_fail (t_name != NULL); + + egg_toolbars_model_set_flags (eggmodel, flag, i); + } + + save_changes (model); +} + +static void +bookmark_destroy_cb (EphyNode *node, + EphyBookmarksBarModel *model) +{ + long id; + + id = ephy_node_get_id (node); + ephy_bookmarksbar_model_remove_bookmark (model, id); +} + +static void +item_added_cb (EphyBookmarksBarModel *model, + int toolbar_position, + int position) +{ + EphyNode *node; + const char *name; + gboolean is_separator; + + egg_toolbars_model_item_nth (EGG_TOOLBARS_MODEL (model), toolbar_position, + position, &is_separator, NULL, &name); + if (!is_separator && g_str_has_prefix (name, "GoBookmark-")) + { + node = ephy_bookmarksbar_model_get_node (model, name); + g_return_if_fail (node != NULL); + + ephy_node_signal_connect_object (node, + EPHY_NODE_DESTROY, + (EphyNodeCallback) bookmark_destroy_cb, + G_OBJECT (model)); + } + + save_changes (model); +} + +static gboolean +impl_add_item (EggToolbarsModel *eggmodel, + int toolbar_position, + int position, + const char *name, + const char *type) +{ + EphyBookmarksBarModel *model = EPHY_BOOKMARKSBAR_MODEL (eggmodel); + gboolean is_bookmark; + + is_bookmark = strcmp (type, EPHY_DND_TOPIC_TYPE) == 0 || + strcmp (type, EPHY_DND_URL_TYPE) == 0; + + if (!is_bookmark || !get_toolbar_and_item_pos (model, name, NULL, NULL)) + { + return EGG_TOOLBARS_MODEL_CLASS (parent_class)->add_item + (eggmodel, toolbar_position, position, name, type); + } + + return FALSE; +} + +static char * +impl_get_item_id (EggToolbarsModel *eggmodel, + const char *type, + const char *name) +{ + EphyBookmarksBarModel *model = EPHY_BOOKMARKSBAR_MODEL (eggmodel); + EphyBookmarks *bookmarks = model->priv->bookmarks; + + if (strcmp (type, EPHY_DND_TOPIC_TYPE) == 0) + { + EphyNode *topic; + + topic = ephy_bookmarks_find_keyword (bookmarks, name, FALSE); + if (topic == NULL) return NULL; + + return ephy_bookmarksbar_model_get_action_name + (model, ephy_node_get_id (topic)); + } + else if (strcmp (type, EPHY_DND_URL_TYPE) == 0) + { + EphyNode *node = NULL; + gchar **netscape_url; + + netscape_url = g_strsplit (name, "\n", 2); + if (!netscape_url || !netscape_url[URL]) return NULL; + + node = ephy_bookmarks_find_bookmark (bookmarks, netscape_url[URL]); + + if (!node) + { + /* Create the bookmark, it does not exist */ + EphyHistory *gh; + const char *icon; + const char *title; + + title = netscape_url[NAME]; + if (title == NULL || *title == '\0') + { + title = _("Untitled"); + } + + node = ephy_bookmarks_add (bookmarks, title, netscape_url[URL]); + + if (node != NULL) + { + gh = EPHY_HISTORY (ephy_embed_shell_get_global_history (embed_shell)); + icon = ephy_history_get_icon (gh, netscape_url[URL]); + + if (icon) + { + ephy_bookmarks_set_icon (bookmarks, netscape_url[URL], icon); + } + } + } + + g_strfreev (netscape_url); + + if (node == NULL) return NULL; + + return ephy_bookmarksbar_model_get_action_name + (model, ephy_node_get_id (node)); + } + + return EGG_TOOLBARS_MODEL_CLASS (parent_class)->get_item_id (eggmodel, type, name); +} + +static char * +impl_get_item_type (EggToolbarsModel *model, + GdkAtom type) +{ + if (gdk_atom_intern (EPHY_DND_TOPIC_TYPE, FALSE) == type) + { + return g_strdup (EPHY_DND_TOPIC_TYPE); + } + else if (gdk_atom_intern (EPHY_DND_URL_TYPE, FALSE) == type) + { + return g_strdup (EPHY_DND_URL_TYPE); + } + + return EGG_TOOLBARS_MODEL_CLASS (parent_class)->get_item_type (model, type); +} + +static void +load_toolbars (EphyBookmarksBarModel *model) +{ + EggToolbarsModel *eggmodel = EGG_TOOLBARS_MODEL (model); + gboolean success = FALSE; + + success = egg_toolbars_model_load (eggmodel, model->priv->xml_file); + LOG ("Loading the toolbars was %ssuccessful", success ? "" : "un") + + /* Try migration first: load the old layout, and remove every toolbar + * except the BookmarksBar toolbar + */ + if (success == FALSE) + { + char *old_xml; + int i, n_toolbars; + + old_xml = g_build_filename (ephy_dot_dir (), + "epiphany-toolbars.xml", + NULL); + success = egg_toolbars_model_load (eggmodel, old_xml); + g_free (old_xml); + + if (success) + { + n_toolbars = egg_toolbars_model_n_toolbars (eggmodel); + + for (i = n_toolbars - 1; i >= 0; i--) + { + const char *t_name; + + t_name = egg_toolbars_model_toolbar_nth (eggmodel, i); + g_return_if_fail (t_name != NULL); + + if (strcmp (t_name, "BookmarksBar") != 0) + { + egg_toolbars_model_remove_toolbar (eggmodel, i); + } + } + } + + LOG ("Migration was %ssuccessful", success ? "" : "un") + } + + /* Load default set */ + if (success == FALSE) + { + egg_toolbars_model_load + (eggmodel, ephy_file ("epiphany-bookmarksbar.xml")); + LOG ("Loading the default toolbars was %ssuccessful", success ? "" : "un") + } + + /* Ensure that we have a BookmarksBar */ + if (get_toolbar_pos (model, "BookmarksBar") == -1) + { + egg_toolbars_model_add_toolbar + (eggmodel, -1, "BookmarksBar"); + } +} + +static void +ephy_bookmarksbar_model_init (EphyBookmarksBarModel *model) +{ + model->priv = EPHY_BOOKMARKSBAR_MODEL_GET_PRIVATE (model); + + model->priv->xml_file = g_build_filename (ephy_dot_dir (), + EPHY_BOOKMARKSBARS_XML_FILE, + NULL); + + g_signal_connect_after (model, "item_added", + G_CALLBACK (item_added_cb), NULL); + g_signal_connect_after (model, "item_removed", + G_CALLBACK (save_changes), NULL); + g_signal_connect_after (model, "toolbar_added", + G_CALLBACK (update_flags_and_save_changes), NULL); + g_signal_connect_after (model, "toolbar_removed", + G_CALLBACK (update_flags_and_save_changes), NULL); +} + +static void +ephy_bookmarksbar_model_finalize (GObject *object) +{ + EphyBookmarksBarModel *model = EPHY_BOOKMARKSBAR_MODEL (object); + + if (model->priv->timeout != 0) + { + g_source_remove (model->priv->timeout); + model->priv->timeout = 0; + } + + g_free (model->priv->xml_file); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +ephy_bookmarksbar_model_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EphyBookmarksBarModel *model = EPHY_BOOKMARKSBAR_MODEL (object); + + switch (prop_id) + { + case PROP_BOOKMARKS: + /* we're owned by bookmarks, so don't g_object_ref() here */ + model->priv->bookmarks = g_value_get_object (value); + load_toolbars (model); + break; + } +} + +static void +ephy_bookmarksbar_model_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + /* no readable properties */ + g_assert_not_reached (); +} + +static void +ephy_bookmarksbar_model_class_init (EphyBookmarksBarModelClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + EggToolbarsModelClass *eggclass = EGG_TOOLBARS_MODEL_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = ephy_bookmarksbar_model_finalize; + object_class->set_property = ephy_bookmarksbar_model_set_property; + object_class->get_property = ephy_bookmarksbar_model_get_property; + + eggclass->add_item = impl_add_item; + eggclass->get_item_id = impl_get_item_id; + eggclass->get_item_type = impl_get_item_type; + + g_object_class_install_property (object_class, + PROP_BOOKMARKS, + g_param_spec_object ("bookmarks", + "Bookmarks", + "Bookmarks", + EPHY_TYPE_BOOKMARKS, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + + g_type_class_add_private (object_class, sizeof (EphyBookmarksBarModelPrivate)); +} + +EggToolbarsModel * +ephy_bookmarksbar_model_new (EphyBookmarks *bookmarks) +{ + return EGG_TOOLBARS_MODEL (g_object_new (EPHY_TYPE_BOOKMARKSBAR_MODEL, + "bookmarks", bookmarks, + NULL)); +} diff --git a/src/bookmarks/ephy-bookmarksbar-model.h b/src/bookmarks/ephy-bookmarksbar-model.h new file mode 100755 index 000000000..ac39f9325 --- /dev/null +++ b/src/bookmarks/ephy-bookmarksbar-model.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2003-2004 Marco Pesenti Gritti + * Copyright (C) 2003-2004 Christian Persch + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifndef EPHY_BOOKMARKSBAR_MODEL_H +#define EPHY_BOOKMARKSBAR_MODEL_H + +#include "egg-toolbars-model.h" +#include "ephy-bookmarks.h" + +G_BEGIN_DECLS + +#define EPHY_TYPE_BOOKMARKSBAR_MODEL (ephy_bookmarksbar_model_get_type ()) +#define EPHY_BOOKMARKSBAR_MODEL(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_BOOKMARKSBAR_MODEL, EphyBookmarksBarModel)) +#define EPHY_BOOKMARKSBAR_MODEL_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EPHY_TYPE_BOOKMARKSBAR_MODEL, EphyBookmarksBarModelClass)) +#define EPHY_IS_BOOKMARKSBAR_MODEL(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_BOOKMARKSBAR_MODEL)) +#define EPHY_IS_BOOKMARKSBAR_MODEL_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_BOOKMARKSBAR_MODEL)) +#define EPHY_BOOKMARKSBAR_MODEL_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_BOOKMARKSBAR_MODEL, EphyBookmarksBarModelClass)) + +typedef struct EphyBookmarksBarModelClass EphyBookmarksBarModelClass; +typedef struct EphyBookmarksBarModel EphyBookmarksBarModel; +typedef struct EphyBookmarksBarModelPrivate EphyBookmarksBarModelPrivate; + +struct EphyBookmarksBarModel +{ + EggToolbarsModel parent_object; + + /*< private >*/ + EphyBookmarksBarModelPrivate *priv; +}; + +struct EphyBookmarksBarModelClass +{ + EggToolbarsModelClass parent_class; +}; + +GType ephy_bookmarksbar_model_get_type (void); + +EggToolbarsModel *ephy_bookmarksbar_model_new (EphyBookmarks *bookmarks); + +char *ephy_bookmarksbar_model_get_action_name (EphyBookmarksBarModel *model, + long id); + +EphyNode *ephy_bookmarksbar_model_get_node (EphyBookmarksBarModel *model, + const char *action_name); + +void ephy_bookmarksbar_model_add_bookmark (EphyBookmarksBarModel *model, + gboolean topic, + long id); + +void ephy_bookmarksbar_model_remove_bookmark (EphyBookmarksBarModel *model, + long id); + +gboolean ephy_bookmarksbar_model_has_bookmark (EphyBookmarksBarModel *model, + long id); + +G_END_DECLS + +#endif diff --git a/src/bookmarks/ephy-bookmarksbar.c b/src/bookmarks/ephy-bookmarksbar.c new file mode 100644 index 000000000..144d7c25e --- /dev/null +++ b/src/bookmarks/ephy-bookmarksbar.c @@ -0,0 +1,400 @@ +/* + * Copyright (C) 2001, 2002 Jorn Baayen + * Copyright (C) 2003-2004 Marco Pesenti Gritti + * Copyright (C) 2003-2004 Christian Persch + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "ephy-bookmarksbar.h" +#include "ephy-bookmarksbar-model.h" +#include "ephy-bookmarks.h" +#include "ephy-shell.h" +#include "ephy-topic-action.h" +#include "ephy-bookmark-action.h" +#include "ephy-new-bookmark.h" +#include "ephy-stock-icons.h" +#include "ephy-dnd.h" +#include "ephy-debug.h" + +#include <gtk/gtkuimanager.h> +#include <gtk/gtktoolbar.h> +#include <glib/gi18n.h> +#include <string.h> + +static GtkTargetEntry drag_targets[] = +{ + { EGG_TOOLBAR_ITEM_TYPE, 0, 0 }, + { EPHY_DND_TOPIC_TYPE, 0, 1 }, + { EPHY_DND_URL_TYPE, 0, 2 } +}; +static int n_drag_targets = G_N_ELEMENTS (drag_targets); + +enum +{ + PROP_0, + PROP_BOOKMARKS, + PROP_WINDOW +}; + +static GObjectClass *parent_class = NULL; + +#define EPHY_BOOKMARKSBAR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_BOOKMARKSBAR, EphyBookmarksBarPrivate)) + +struct EphyBookmarksBarPrivate +{ + EphyWindow *window; + GtkActionGroup *action_group; + EphyBookmarks *bookmarks; + EggToolbarsModel *toolbars_model; +}; + +static void ephy_bookmarksbar_class_init (EphyBookmarksBarClass *klass); +static void ephy_bookmarksbar_init (EphyBookmarksBar *toolbar); + +GType +ephy_bookmarksbar_get_type (void) +{ + static GType type = 0; + + if (type == 0) + { + static const GTypeInfo our_info = + { + sizeof (EphyBookmarksBarClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) ephy_bookmarksbar_class_init, + NULL, + NULL, /* class_data */ + sizeof (EphyBookmarksBar), + 0, /* n_preallocs */ + (GInstanceInitFunc) ephy_bookmarksbar_init + }; + + type = g_type_register_static (EGG_TYPE_EDITABLE_TOOLBAR, + "EphyBookmarksBar", + &our_info, 0); + } + + return type; +} + +static void +go_location_cb (GtkAction *action, + char *location, + EphyBookmarksBar *toolbar) +{ + EphyWindow *window = toolbar->priv->window; + GdkEvent *event; + gboolean new_tab = FALSE; + + g_return_if_fail (window != NULL); + + event = gtk_get_current_event (); + if (event != NULL) + { + if (event->type == GDK_BUTTON_RELEASE) + { + guint modifiers, button, state; + + modifiers = gtk_accelerator_get_default_mod_mask (); + button = event->button.button; + state = event->button.state; + + /* middle-click or control-click */ + if ((button == 1 && ((state & modifiers) == GDK_CONTROL_MASK)) || + (button == 2)) + { + new_tab = TRUE; + } + } + + gdk_event_free (event); + } + + if (new_tab) + { + ephy_shell_new_tab (ephy_shell, window, + ephy_window_get_active_tab (window), + location, + EPHY_NEW_TAB_OPEN_PAGE | + EPHY_NEW_TAB_IN_EXISTING_WINDOW); + } + else + { + ephy_window_load_url (window, location); + } +} + +static void +bookmark_destroy_cb (EphyNode *node, + EphyBookmarksBar *toolbar) +{ + GtkAction *action; + char *name; + long id; + + id = ephy_node_get_id (node); + name = ephy_bookmarksbar_model_get_action_name + (EPHY_BOOKMARKSBAR_MODEL (toolbar->priv->toolbars_model), + id); + + action = gtk_action_group_get_action (toolbar->priv->action_group, name); + g_return_if_fail (action != NULL); + + gtk_action_group_remove_action (toolbar->priv->action_group, action); + + g_free (name); +} + +static void +ephy_bookmarksbar_action_request (EggEditableToolbar *eggtoolbar, + const char *name) +{ + EphyBookmarksBar *toolbar = EPHY_BOOKMARKSBAR (eggtoolbar); + GtkAction *action = NULL; + EphyNode *bmks, *topics; + + bmks = ephy_bookmarks_get_bookmarks (toolbar->priv->bookmarks); + topics = ephy_bookmarks_get_keywords (toolbar->priv->bookmarks); + + LOG ("Action request for action '%s'", name) + + if (g_str_has_prefix (name, "GoBookmark-")) + { + EphyNode *node; + + node = ephy_bookmarksbar_model_get_node + (EPHY_BOOKMARKSBAR_MODEL (toolbar->priv->toolbars_model), + name); + g_return_if_fail (node != NULL); + + if (ephy_node_has_child (topics, node)) + { + action = ephy_topic_action_new (name, ephy_node_get_id (node)); + } + else if (ephy_node_has_child (bmks, node)) + { + action = ephy_bookmark_action_new (name, ephy_node_get_id (node)); + } + + g_return_if_fail (action != NULL); + + g_signal_connect (action, "go_location", + G_CALLBACK (go_location_cb), toolbar); + gtk_action_group_add_action (toolbar->priv->action_group, action); + g_object_unref (action); + + ephy_node_signal_connect_object (node, + EPHY_NODE_DESTROY, + (EphyNodeCallback) bookmark_destroy_cb, + G_OBJECT (toolbar)); + } + + if (EGG_EDITABLE_TOOLBAR_CLASS (parent_class)->action_request) + { + EGG_EDITABLE_TOOLBAR_CLASS (parent_class)->action_request (eggtoolbar, name); + } +} + +static void +toolbar_added_cb (EggToolbarsModel *model, + int position, + EggEditableToolbar *toolbar) +{ + const char *t_name; + + t_name = egg_toolbars_model_toolbar_nth (model, position); + g_return_if_fail (t_name != NULL); + + egg_editable_toolbar_set_drag_dest + (toolbar, drag_targets, n_drag_targets, t_name); +} + +static void +ephy_bookmarksbar_set_window (EphyBookmarksBar *toolbar, + EphyWindow *window) +{ + EggToolbarsModel *model = toolbar->priv->toolbars_model; + GtkUIManager *manager = GTK_UI_MANAGER (window->ui_merge); + + g_return_if_fail (toolbar->priv->window == NULL); + g_return_if_fail (model != NULL); + + toolbar->priv->window = window; + + toolbar->priv->action_group = + gtk_action_group_new ("BookmarksToolbarActions"); + + gtk_ui_manager_insert_action_group (manager, + toolbar->priv->action_group, -1); + + g_object_set (G_OBJECT (toolbar), + "MenuMerge", manager, + "ToolbarsModel", model, + NULL); +} + +static void +ephy_bookmarksbar_realize (GtkWidget *widget) +{ + EggEditableToolbar *eggtoolbar = EGG_EDITABLE_TOOLBAR (widget); + EphyBookmarksBar *toolbar = EPHY_BOOKMARKSBAR (widget); + EggToolbarsModel *model = toolbar->priv->toolbars_model; + int i, n_toolbars; + + GTK_WIDGET_CLASS (parent_class)->realize (widget); + + g_signal_connect (model, "toolbar_added", + G_CALLBACK (toolbar_added_cb), toolbar); + + /* now that the toolbar has been constructed, set drag dests */ + n_toolbars = egg_toolbars_model_n_toolbars (model); + for (i = 0; i < n_toolbars; i++) + { + const char *t_name; + + t_name = egg_toolbars_model_toolbar_nth (model, i); + g_return_if_fail (t_name != NULL); + + egg_editable_toolbar_set_drag_dest + (eggtoolbar, drag_targets, n_drag_targets, t_name); + } +} + +static void +ephy_bookmarksbar_unrealize (GtkWidget *widget) +{ + EphyBookmarksBar *toolbar = EPHY_BOOKMARKSBAR (widget); + EggToolbarsModel *model = toolbar->priv->toolbars_model; + + g_signal_handlers_disconnect_by_func + (model, G_CALLBACK (toolbar_added_cb), toolbar); + + GTK_WIDGET_CLASS (parent_class)->unrealize (widget); +} + +static void +ephy_bookmarksbar_init (EphyBookmarksBar *toolbar) +{ + toolbar->priv = EPHY_BOOKMARKSBAR_GET_PRIVATE (toolbar); +} + +static void +ephy_bookmarksbar_finalize (GObject *object) +{ + EphyBookmarksBar *toolbar = EPHY_BOOKMARKSBAR (object); + + g_object_unref (toolbar->priv->action_group); + + g_signal_handlers_disconnect_by_func + (toolbar->priv->toolbars_model, + G_CALLBACK (toolbar_added_cb), + toolbar); + + LOG ("EphyBookmarksBar %p finalised", object) + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +ephy_bookmarksbar_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EphyBookmarksBar *toolbar = EPHY_BOOKMARKSBAR (object); + + switch (prop_id) + { + case PROP_BOOKMARKS: + toolbar->priv->bookmarks = g_value_get_object (value); + toolbar->priv->toolbars_model = + ephy_bookmarks_get_toolbars_model (toolbar->priv->bookmarks); + break; + case PROP_WINDOW: + ephy_bookmarksbar_set_window (toolbar, g_value_get_object (value)); + break; + } +} + +static void +ephy_bookmarksbar_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + /* no readable properties */ + g_assert_not_reached (); +} + +static void +ephy_bookmarksbar_class_init (EphyBookmarksBarClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + EggEditableToolbarClass *eet_class = EGG_EDITABLE_TOOLBAR_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = ephy_bookmarksbar_finalize; + object_class->set_property = ephy_bookmarksbar_set_property; + object_class->get_property = ephy_bookmarksbar_get_property; + + widget_class->realize = ephy_bookmarksbar_realize; + widget_class->unrealize = ephy_bookmarksbar_unrealize; + + eet_class->action_request = ephy_bookmarksbar_action_request; + + g_object_class_install_property (object_class, + PROP_WINDOW, + g_param_spec_object ("window", + "Window", + "Parent window", + EPHY_TYPE_WINDOW, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + + g_object_class_install_property (object_class, + PROP_BOOKMARKS, + g_param_spec_object ("bookmarks", + "Bookmarks", + "Bookmarks Model", + EPHY_TYPE_BOOKMARKS, + G_PARAM_WRITABLE | + G_PARAM_CONSTRUCT_ONLY)); + + g_type_class_add_private (object_class, sizeof(EphyBookmarksBarPrivate)); +} + +GtkWidget * +ephy_bookmarksbar_new (EphyWindow *window) +{ + EphyBookmarks *bookmarks; + + bookmarks = ephy_shell_get_bookmarks (ephy_shell); + + return GTK_WIDGET (g_object_new (EPHY_TYPE_BOOKMARKSBAR, + "bookmarks", bookmarks, + "window", window, + NULL)); +} diff --git a/src/bookmarks/ephy-bookmarksbar.h b/src/bookmarks/ephy-bookmarksbar.h new file mode 100644 index 000000000..2f4697fb9 --- /dev/null +++ b/src/bookmarks/ephy-bookmarksbar.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2002 Jorn Baayen + * Copyright (C) 2003-2004 Marco Pesenti Gritti + * Copyright (C) 2003-2004 Christian Persch + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifndef EPHY_BOOKMARKSBAR_H +#define EPHY_BOOKMARKSBAR_H + +#include "egg-editable-toolbar.h" +#include "ephy-window.h" + +#include <glib-object.h> +#include <glib.h> + +G_BEGIN_DECLS + +#define EPHY_TYPE_BOOKMARKSBAR (ephy_bookmarksbar_get_type ()) +#define EPHY_BOOKMARKSBAR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_BOOKMARKSBAR, EphyBookmarksBar)) +#define EPHY_BOOKMARKSBAR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EPHY_TYPE_BOOKMARKSBAR, EphyBookmarksBarClass)) +#define EPHY_IS_BOOKMARKSBAR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_BOOKMARKSBAR)) +#define EPHY_IS_BOOKMARKSBAR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_BOOKMARKSBAR)) +#define EPHY_BOOKMARKSBAR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_BOOKMARKSBAR, EphyBookmarksBarClass)) + +typedef struct EphyBookmarksBar EphyBookmarksBar; +typedef struct EphyBookmarksBarClass EphyBookmarksBarClass; +typedef struct EphyBookmarksBarPrivate EphyBookmarksBarPrivate; + +struct EphyBookmarksBar +{ + EggEditableToolbar parent_object; + + /*< private >*/ + EphyBookmarksBarPrivate *priv; +}; + +struct EphyBookmarksBarClass +{ + EggEditableToolbarClass parent_class; +}; + +GType ephy_bookmarksbar_get_type (void); + +GtkWidget *ephy_bookmarksbar_new (EphyWindow *window); + +G_END_DECLS + +#endif |