aboutsummaryrefslogtreecommitdiffstats
path: root/src/bookmarks/ephy-open-tabs-action.c
blob: 137769ea1d3fda1ee67c4cee2dbd3beff5e5823a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *  Copyright (C) 2004 Peter Harvey <pah06@uow.edu.au>
 *
 *  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 of the License, 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 <gtk/gtktoolitem.h>
#include <glib/gi18n.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <string.h>

#include "ephy-open-tabs-action.h"
#include "ephy-bookmarks.h"
#include "ephy-node-common.h"
#include "ephy-link-action.h"
#include "ephy-link.h"

static void
activate_cb (GtkAction *action, gpointer dummy)
{
    EphyLink *link = g_object_get_data (G_OBJECT (action), "ephy-link");
    EphyNode *node = g_object_get_data (G_OBJECT (action), "ephy-node");
    GPtrArray *children = ephy_node_get_children (node);
    EphyTab *tab = 0;

    gint i;
    const char *url;

    for (i = 0; i < children->len; i++)
    {
        node = g_ptr_array_index (children, i);
        url = ephy_node_get_property_string (node, EPHY_NODE_BMK_PROP_LOCATION);
        tab = ephy_link_open (link, url, tab, EPHY_LINK_NEW_TAB);
    }
}

static void
node_added_cb (EphyNode *parent, EphyNode *child, GObject *object)
{
    GtkActionGroup *actions = GTK_ACTION_GROUP (object);
    GtkAction *action;
    char *name, *accel;
    
    name = ephy_open_tabs_action_name (child);
    g_return_if_fail (name);

    action = gtk_action_new (name, _("Open in New _Tabs"), "Open this topic in tabs", NULL);
    
    g_object_set_data (G_OBJECT (action), "ephy-node", child);
    g_object_set_data (G_OBJECT (action), "ephy-link", EPHY_LINK (object));
    
    g_signal_connect (G_OBJECT (action), "activate",
              G_CALLBACK (activate_cb), NULL);
    
    accel = g_strjoin ("/", "<Actions>",
               gtk_action_group_get_name (actions),
               name,
               NULL);
    gtk_action_set_accel_path (action, accel);
    gtk_action_group_add_action (actions, action);
    g_object_unref (action);
    g_free (accel);
    g_free (name);
}

static void
node_removed_cb (EphyNode *parent, EphyNode *child, guint index, GObject *object)
{
    char *name = ephy_open_tabs_action_name (child);
    if (name)
    {
        GtkActionGroup *actions = GTK_ACTION_GROUP (object);
        GtkAction *action = gtk_action_group_get_action (actions, name);
        if (action) gtk_action_group_remove_action (actions, action);
        g_free (name);
    }
}

GtkActionGroup *
ephy_open_tabs_group_new (EphyNode *node)
{
    GPtrArray *children = ephy_node_get_children (node);
    GObject *actions = G_OBJECT (ephy_link_action_group_new ("OpenTabsActions"));
    gint i;
    
    for (i = 0; i < children->len; i++)
      node_added_cb (node, g_ptr_array_index (children, i), actions);
    
    ephy_node_signal_connect_object (node, EPHY_NODE_CHILD_ADDED,
                     (EphyNodeCallback)node_added_cb,
                     actions);
    ephy_node_signal_connect_object (node, EPHY_NODE_CHILD_REMOVED,
                     (EphyNodeCallback)node_removed_cb,
                     actions);

    return GTK_ACTION_GROUP (actions);
}

char *
ephy_open_tabs_action_name (EphyNode *node)
{
    return g_strdup_printf("OpenTabs%u", ephy_node_get_id (node));
}