aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-navigation-action.c
blob: 8c158862193465436ea838800c435ee7aecc1853 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *
 *  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-navigation-action.h"
#include "ephy-window.h"
#include "ephy-string.h"
#include "ephy-favicon-cache.h"
#include "ephy-history.h"
#include "ephy-embed-shell.h"
#include "ephy-debug.h"

#include <gtk/gtkimage.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkimagemenuitem.h>
#include <gtk/gtkmenushell.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenutoolbutton.h>

#define NTH_DATA_KEY    "GoNTh"
#define URL_DATA_KEY    "GoURL"

#define EPHY_NAVIGATION_ACTION_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_NAVIGATION_ACTION, EphyNavigationActionPrivate))

struct EphyNavigationActionPrivate
{
    EphyWindow *window;
    EphyNavigationDirection direction;
};

enum
{
    PROP_0,
    PROP_DIRECTION,
    PROP_WINDOW
};

static void ephy_navigation_action_init       (EphyNavigationAction *action);
static void ephy_navigation_action_class_init (EphyNavigationActionClass *class);

static GObjectClass *parent_class = NULL;

GType
ephy_navigation_action_get_type (void)
{
    static GType type = 0;

    if (type == 0)
    {
        static const GTypeInfo type_info =
        {
            sizeof (EphyNavigationActionClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) ephy_navigation_action_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,
            sizeof (EphyNavigationAction),
            0, /* n_preallocs */
            (GInstanceInitFunc) ephy_navigation_action_init,
        };

        type = g_type_register_static (GTK_TYPE_ACTION,
                           "EphyNavigationAction",
                           &type_info, 0);
    }

    return type;
}

#define MAX_LENGTH 60

static GtkWidget *
new_history_menu_item (const char *origtext,
               const char *address)
{
    EphyFaviconCache *cache;
    EphyHistory *history;
    GtkWidget *item, *image;
    GdkPixbuf *icon = NULL;
    const char *icon_address;
    char *short_text;

    g_return_val_if_fail (address != NULL, NULL);

    /* FIXME: use ellipsisation in the menu item instead */
    short_text = ephy_string_shorten (origtext, MAX_LENGTH);
    item = gtk_image_menu_item_new_with_label (short_text);
    g_free (short_text);

    history = EPHY_HISTORY
        (ephy_embed_shell_get_global_history (embed_shell));
    icon_address = ephy_history_get_icon (history, address);

    cache = EPHY_FAVICON_CACHE
        (ephy_embed_shell_get_favicon_cache (embed_shell));
    icon = ephy_favicon_cache_get (cache, icon_address);

    if (icon != NULL)
    {
        image = gtk_image_new_from_pixbuf (icon);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
        gtk_widget_show (image);
        g_object_unref (icon);
    }

    gtk_widget_show (item);

    return item;
}

static void
activate_back_or_forward_menu_item_cb (GtkWidget *menuitem,
                       EphyWindow *window)
{
    EphyEmbed *embed;
    int go_nth;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    go_nth = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (menuitem), NTH_DATA_KEY));

    ephy_embed_shistory_go_nth (embed, go_nth);
}

static void
activate_up_menu_item_cb (GtkWidget *menuitem,
              EphyWindow *window)
{
    EphyEmbed *embed;
    char *url;

    embed = ephy_window_get_active_embed (window);
    g_return_if_fail (embed != NULL);

    url = g_object_get_data (G_OBJECT (menuitem), URL_DATA_KEY);
    g_return_if_fail (url != NULL);

    ephy_window_load_url (window, url);
}

static GtkMenuShell *
build_back_or_forward_menu (EphyNavigationAction *action)
{
    EphyWindow *window = action->priv->window;
    GtkMenuShell *menu;
    EphyEmbed *embed;
    int pos, count;
    int start, end;

    embed = ephy_window_get_active_embed (window);
    g_return_val_if_fail (embed != NULL, NULL);

    pos = ephy_embed_shistory_get_pos (embed);
    count = ephy_embed_shistory_n_items (embed);

    if (count == 0) return NULL;

    if (action->priv->direction == EPHY_NAVIGATION_DIRECTION_BACK)
    {
        start = pos - 1;
        end = -1;
    }
    else
    {
        start = pos + 1;
        end = count;
    }

    menu = GTK_MENU_SHELL (gtk_menu_new ());

    while (start != end)
    {
        GtkWidget *item;
        char *title = NULL, *url = NULL;

        ephy_embed_shistory_get_nth (embed, start, FALSE, &url, &title);

        if (url == NULL) continue;

        item = new_history_menu_item (title ? title : url, url);

        g_object_set_data (G_OBJECT (item), NTH_DATA_KEY,
                   GINT_TO_POINTER (start));
        g_signal_connect (item, "activate",
                  G_CALLBACK (activate_back_or_forward_menu_item_cb),
                  window);

        gtk_menu_shell_append (menu, item);
        gtk_widget_show_all (item);

        g_free (url);
        g_free (title);

        if (start < end)
        {
            start++;
        }
        else
        {
            start--;
        }
    }

    return menu;
}

static GtkMenuShell *
build_up_menu (EphyNavigationAction *action)
{
    EphyWindow *window = action->priv->window;
    EphyEmbed *embed;
    GtkMenuShell *menu;
    GtkWidget *item;
    GSList *list, *l;
    char *url;

    embed = ephy_window_get_active_embed (window);
    g_return_val_if_fail (embed != NULL, NULL);

    menu = GTK_MENU_SHELL (gtk_menu_new ());

    list = ephy_embed_get_go_up_list (embed);

    for (l = list; l != NULL; l = l->next)
    {
        url = l->data;

        if (url == NULL) continue;

        item = new_history_menu_item (url, url);

        g_object_set_data_full (G_OBJECT (item), URL_DATA_KEY, url,
                    (GDestroyNotify) g_free);
        g_signal_connect (item, "activate",
                  G_CALLBACK (activate_up_menu_item_cb), window);

        gtk_menu_shell_append (menu, item);
        gtk_widget_show (item);
    }

    /* the list data has been consumed */
    g_slist_free (list);

    return menu;
}

static void
menu_activated_cb (GtkMenuToolButton *button,
           EphyNavigationAction *action)
{
    GtkMenuShell *menu = NULL;

    LOG ("menu_activated_cb dir %d", action->priv->direction)

    switch (action->priv->direction)
    {
        case EPHY_NAVIGATION_DIRECTION_UP:
            menu = build_up_menu (action);
            break;
        case EPHY_NAVIGATION_DIRECTION_FORWARD:
        case EPHY_NAVIGATION_DIRECTION_BACK:
            menu = build_back_or_forward_menu (action);
            break;
        default:
            g_assert_not_reached ();
            break;
    }

    gtk_menu_tool_button_set_menu (button, menu);
}

static void
connect_proxy (GtkAction *action, GtkWidget *proxy)
{
    LOG ("Connect navigation action proxy")

    if (GTK_IS_MENU_TOOL_BUTTON (proxy))
    {
        GtkWidget *menu;

        /* set dummy menu so the arrow gets sensitive */
        menu = gtk_menu_new ();
        gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (proxy), menu);

        g_signal_connect (proxy, "show-menu",
                  G_CALLBACK (menu_activated_cb), action);
    }

    GTK_ACTION_CLASS (parent_class)->connect_proxy (action, proxy);
}

static void
ephy_navigation_action_init (EphyNavigationAction *action)
{
    action->priv = EPHY_NAVIGATION_ACTION_GET_PRIVATE (action);
}

static void
ephy_navigation_action_set_property (GObject *object,
                     guint prop_id,
                     const GValue *value,
                     GParamSpec *pspec)
{
    EphyNavigationAction *nav;

    nav = EPHY_NAVIGATION_ACTION (object);

    switch (prop_id)
    {
        case PROP_DIRECTION:
            nav->priv->direction = g_value_get_int (value);
            break;
        case PROP_WINDOW:
            nav->priv->window = EPHY_WINDOW (g_value_get_object (value));
            break;
    }
}

static void
ephy_navigation_action_get_property (GObject *object,
                     guint prop_id,
                     GValue *value,
                     GParamSpec *pspec)
{
    EphyNavigationAction *nav;

    nav = EPHY_NAVIGATION_ACTION (object);

    switch (prop_id)
    {
        case PROP_DIRECTION:
            g_value_set_int (value, nav->priv->direction);
            break;
        case PROP_WINDOW:
            g_value_set_object (value, nav->priv->window);
            break;
    }
}

static void
ephy_navigation_action_class_init (EphyNavigationActionClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);
    GtkActionClass *action_class = GTK_ACTION_CLASS (class);

    object_class->set_property = ephy_navigation_action_set_property;
    object_class->get_property = ephy_navigation_action_get_property;

    parent_class = g_type_class_peek_parent (class);

    action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
    action_class->connect_proxy = connect_proxy;

    g_object_class_install_property (object_class,
                     PROP_DIRECTION,
                     g_param_spec_int ("direction",
                               "Direction",
                               "Direction",
                               0,
                               G_MAXINT,
                               0,
                               G_PARAM_READWRITE));
    g_object_class_install_property (object_class,
                     PROP_WINDOW,
                     g_param_spec_object ("window",
                                  "Window",
                                  "The navigation window",
                                  G_TYPE_OBJECT,
                                  G_PARAM_READWRITE));

    g_type_class_add_private (object_class, sizeof (EphyNavigationActionPrivate));
}