aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-navigation-up-action.c
blob: 8934a9efa58ae3b462095c732347324ed946ec0b (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
/*
 *  Copyright © 2003, 2004 Marco Pesenti Gritti
 *  Copyright © 2003, 2004 Christian Persch
 *  Copyright © 2008 Jan Alonzo
 *  Copyright © 2009 Igalia S.L.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "ephy-navigation-up-action.h"

#include "ephy-debug.h"
#include "ephy-embed-container.h"
#include "ephy-embed-shell.h"
#include "ephy-embed-utils.h"
#include "ephy-gui.h"
#include "ephy-history.h"
#include "ephy-link.h"
#include "ephy-shell.h"
#include "ephy-type-builtins.h"
#include "ephy-window.h"

#include <gtk/gtk.h>
#include <webkit/webkit.h>

#define URL_DATA_KEY "GoURL"

static void ephy_navigation_up_action_init       (EphyNavigationUpAction *action);
static void ephy_navigation_up_action_class_init (EphyNavigationUpActionClass *klass);

G_DEFINE_TYPE (EphyNavigationUpAction, ephy_navigation_up_action, EPHY_TYPE_NAVIGATION_ACTION)

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

  window = _ephy_navigation_action_get_window (EPHY_NAVIGATION_ACTION (action));
  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (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_link_open (EPHY_LINK (action), url, NULL,
          ephy_gui_is_middle_click () ? EPHY_LINK_NEW_TAB : 0);
}

static void
select_menu_item_cb (GtkWidget *menuitem,
             EphyNavigationUpAction *action)
{
  const char *url;
  EphyWindow *window;
  EphyNavigationAction *nav_action;
  GtkWidget *statusbar;
  guint statusbar_cid;

  url = g_object_get_data (G_OBJECT (menuitem), URL_DATA_KEY);
  window = _ephy_navigation_action_get_window (EPHY_NAVIGATION_ACTION (action));
  statusbar = ephy_window_get_statusbar (window);
  g_return_if_fail (url != NULL);

  /* Update status bar */
  nav_action = EPHY_NAVIGATION_ACTION (action);
  statusbar_cid = _ephy_navigation_action_get_statusbar_context_id (nav_action);
  gtk_statusbar_push (GTK_STATUSBAR (statusbar), statusbar_cid, url);
}

static void
deselect_menu_item_cb (GtkWidget *menuitem,
               EphyNavigationAction *action)
{
  GtkWidget *statusbar;
  EphyWindow *window;
  EphyNavigationAction *nav_action;
  guint statusbar_cid;

  window = _ephy_navigation_action_get_window (EPHY_NAVIGATION_ACTION (action));
  statusbar = ephy_window_get_statusbar (window);

  /* Update status bar */
  nav_action = EPHY_NAVIGATION_ACTION (action);
  statusbar_cid = _ephy_navigation_action_get_statusbar_context_id (nav_action);
  gtk_statusbar_pop (GTK_STATUSBAR (statusbar), statusbar_cid);
}

static GtkWidget *
build_dropdown_menu (EphyNavigationAction *nav_action)
{
  EphyNavigationUpAction *action;
  EphyWindow *window;
  EphyEmbed *embed;
  EphyHistory *history;
  GtkMenuShell *menu;
  GtkWidget *item;
  GSList *list, *l;
  char *url;

  action = EPHY_NAVIGATION_UP_ACTION (nav_action);
  window = _ephy_navigation_action_get_window (nav_action);
  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
  g_return_val_if_fail (embed != NULL, NULL);

  menu = GTK_MENU_SHELL (gtk_menu_new ());
  history = EPHY_HISTORY (ephy_embed_shell_get_global_history (embed_shell));

  list = ephy_web_view_get_go_up_list (ephy_embed_get_web_view (embed));
  for (l = list; l != NULL; l = l->next) {
    EphyNode *node;
    const char *title = NULL;

    url = g_strdup (l->data);

    if (url == NULL) continue;

    node = ephy_history_get_page (history, url);
    if (node != NULL) {
      title = ephy_node_get_property_string (node, EPHY_NODE_PAGE_PROP_TITLE);
    }

    item = _ephy_navigation_action_new_history_menu_item (title ? title : 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), action);
    g_signal_connect (item, "select",
                      G_CALLBACK (select_menu_item_cb),
                      action);
    g_signal_connect (item, "deselect",
                      G_CALLBACK (deselect_menu_item_cb),
                      action);

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

  /* the list data has been consumed */
  g_slist_foreach (list, (GFunc) g_free, NULL);
  g_slist_free (list);

  return GTK_WIDGET (menu);
}

static void
action_activate (GtkAction *action)
{
  EphyWindow *window;
  EphyEmbed *embed;
  GSList *up_list;

  window = _ephy_navigation_action_get_window (EPHY_NAVIGATION_ACTION (action));
  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
  g_return_if_fail (embed != NULL);

  up_list = ephy_web_view_get_go_up_list (ephy_embed_get_web_view (embed));
  ephy_link_open (EPHY_LINK (action),
          up_list->data,
          NULL,
          ephy_gui_is_middle_click () ? EPHY_LINK_NEW_TAB : 0);
  g_slist_foreach (up_list, (GFunc) g_free, NULL);
  g_slist_free (up_list);
}

static void
ephy_navigation_up_action_init (EphyNavigationUpAction *action)
{
}

static void
ephy_navigation_up_action_class_init (EphyNavigationUpActionClass *klass)
{
  GtkActionClass *action_class = GTK_ACTION_CLASS (klass);
  EphyNavigationActionClass *nav_action_class = EPHY_NAVIGATION_ACTION_CLASS (klass);

  action_class->activate = action_activate;
  nav_action_class->build_dropdown_menu = build_dropdown_menu;
}