2009-06-29 Xan Lopez <xlopez@igalia.com>
[WebKit-https.git] / WebKit / gtk / webkit / webkitwebnavigationaction.cpp
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #include <wtf/Assertions.h>
23 #include "FrameLoaderTypes.h"
24
25 #include <glib/gi18n-lib.h>
26 #include "webkitwebnavigationaction.h"
27 #include "webkitprivate.h"
28 #include "webkitenumtypes.h"
29
30 #include <string.h>
31
32 /**
33  * SECTION:webkitwebnavigationaction
34  * @short_description: Object used to report details of navigation actions
35  *
36  * #WebKitWebNavigationAction is used in signals to provide details about
37  * what led the navigation to happen. This includes, for instance, if the user
38  * clicked a link to start that navigation, and what mouse button was used.
39  */
40
41 struct _WebKitWebNavigationActionPrivate {
42     WebKitWebNavigationReason reason;
43     gchar* originalUri;
44     gint button;
45     gint modifier_state;
46 };
47
48 #define WEBKIT_WEB_NAVIGATION_ACTION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_NAVIGATION_ACTION, WebKitWebNavigationActionPrivate))
49
50 enum  {
51     PROP_0,
52
53     PROP_REASON,
54     PROP_ORIGINAL_URI,
55     PROP_BUTTON,
56     PROP_MODIFIER_STATE
57 };
58
59 G_DEFINE_TYPE(WebKitWebNavigationAction, webkit_web_navigation_action, G_TYPE_OBJECT)
60
61
62 static void webkit_web_navigation_action_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
63 {
64     WebKitWebNavigationAction* navigationAction = WEBKIT_WEB_NAVIGATION_ACTION(object);
65
66     switch(propertyId) {
67     case PROP_REASON:
68         g_value_set_enum(value, webkit_web_navigation_action_get_reason(navigationAction));
69         break;
70     case PROP_ORIGINAL_URI:
71         g_value_set_string(value, webkit_web_navigation_action_get_original_uri(navigationAction));
72         break;
73     case PROP_BUTTON:
74         g_value_set_int(value, webkit_web_navigation_action_get_button(navigationAction));
75         break;
76     case PROP_MODIFIER_STATE:
77         g_value_set_int(value, webkit_web_navigation_action_get_modifier_state(navigationAction));
78         break;
79     default:
80         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
81         break;
82     }
83 }
84
85 static void webkit_web_navigation_action_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
86 {
87     WebKitWebNavigationAction* navigationAction = WEBKIT_WEB_NAVIGATION_ACTION(object);
88     WebKitWebNavigationActionPrivate* priv = navigationAction->priv;
89
90     switch(propertyId) {
91     case PROP_REASON:
92         webkit_web_navigation_action_set_reason(navigationAction, (WebKitWebNavigationReason)g_value_get_enum(value));
93         break;
94     case PROP_ORIGINAL_URI:
95         webkit_web_navigation_action_set_original_uri(navigationAction, g_value_get_string(value));
96         break;
97     case PROP_BUTTON:
98         priv->button = g_value_get_int(value);
99         break;
100     case PROP_MODIFIER_STATE:
101         priv->modifier_state = g_value_get_int(value);
102         break;
103     default:
104         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
105         break;
106     }
107 }
108
109 static void webkit_web_navigation_action_init(WebKitWebNavigationAction* navigationAction)
110 {
111     navigationAction->priv = WEBKIT_WEB_NAVIGATION_ACTION_GET_PRIVATE(navigationAction);
112 }
113
114 static void webkit_web_navigation_action_finalize(GObject* obj)
115 {
116     WebKitWebNavigationAction* navigationAction = WEBKIT_WEB_NAVIGATION_ACTION(obj);
117     WebKitWebNavigationActionPrivate* priv = navigationAction->priv;
118
119     g_free(priv->originalUri);
120
121     G_OBJECT_CLASS(webkit_web_navigation_action_parent_class)->finalize(obj);
122 }
123
124 static void webkit_web_navigation_action_class_init(WebKitWebNavigationActionClass* requestClass)
125 {
126     GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
127
128     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) == static_cast<int>(WebCore::NavigationTypeLinkClicked), navigation_type_link_clicked_enum_match);
129     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_FORM_SUBMITTED) == static_cast<int>(WebCore::NavigationTypeFormSubmitted), navigation_type_form_submitted_enum_match);
130     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_BACK_FORWARD) == static_cast<int>(WebCore::NavigationTypeBackForward), navigation_type_back_forward_enum_match);
131     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_RELOAD) == static_cast<int>(WebCore::NavigationTypeReload), navigation_type_reload_enum_match);
132     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_FORM_RESUBMITTED) == static_cast<int>(WebCore::NavigationTypeFormResubmitted), navigation_type_form_resubmitted_enum_match);
133     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_OTHER) == static_cast<int>(WebCore::NavigationTypeOther), navigation_type_other_enum_match);
134
135     objectClass->get_property = webkit_web_navigation_action_get_property;
136     objectClass->set_property = webkit_web_navigation_action_set_property;
137     objectClass->dispose = webkit_web_navigation_action_finalize;
138
139     /**
140      * WebKitWebNavigationAction:reason:
141      *
142      * The reason why this navigation is occuring.
143      *
144      * Since: 1.0.3
145      */
146     g_object_class_install_property(objectClass, PROP_REASON,
147                                     g_param_spec_enum("reason",
148                                                       _("Reason"),
149                                                       _("The reason why this navigation is occurring"),
150                                                       WEBKIT_TYPE_WEB_NAVIGATION_REASON,
151                                                       WEBKIT_WEB_NAVIGATION_REASON_OTHER,
152                                                       (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT)));
153
154     /**
155      * WebKitWebNavigationAction:original-uri:
156      *
157      * The URI that was requested as the target for the navigation.
158      *
159      * Since: 1.0.3
160      */
161     g_object_class_install_property(objectClass, PROP_ORIGINAL_URI,
162                                     g_param_spec_string("original-uri",
163                                                         _("Original URI"),
164                                                         _("The URI that was requested as the target for the navigation"),
165                                                         "",
166                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT)));
167     /**
168      * WebKitWebNavigationAction:button:
169      *
170      * The button used to click if the action was a mouse event.
171      *
172      * Since: 1.0.3
173      */
174     g_object_class_install_property(objectClass, PROP_BUTTON,
175                                     g_param_spec_int("button",
176                                                      _("Button"),
177                                                      _("The button used to click"),
178                                                      -1,
179                                                      G_MAXINT,
180                                                      -1,
181                                                      (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
182
183     /**
184      * WebKitWebNavigationAction:modifier-state:
185      *
186      * The state of the modifier keys when the action was requested.
187      * 
188      * Since: 1.0.3
189      */
190     g_object_class_install_property(objectClass, PROP_MODIFIER_STATE,
191                                     g_param_spec_int("modifier-state",
192                                                      _("Modifier state"),
193                                                      _("A bitmask representing the state of the modifier keys"),
194                                                      0,
195                                                      G_MAXINT,
196                                                      0,
197                                                      (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
198
199
200
201     g_type_class_add_private(requestClass, sizeof(WebKitWebNavigationActionPrivate));
202 }
203
204 /**
205  * webkit_web_navigation_action_get_reason:
206  * @navigationAction: a #WebKitWebNavigationAction
207  *
208  * Returns the reason why WebKit is requesting a navigation.
209  *
210  * Return value: a #WebKitWebNavigationReason
211  *
212  * Since: 1.0.3
213  */
214 WebKitWebNavigationReason webkit_web_navigation_action_get_reason(WebKitWebNavigationAction* navigationAction)
215 {
216     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), WEBKIT_WEB_NAVIGATION_REASON_OTHER);
217
218     return navigationAction->priv->reason;
219 }
220
221 /**
222  * webkit_web_navigation_action_set_reason:
223  * @navigationAction: a #WebKitWebNavigationAction
224  * @reason: a #WebKitWebNavigationReason
225  *
226  * Sets the reason why WebKit is requesting a navigation.
227  *
228  * Since: 1.0.3
229  */
230 void webkit_web_navigation_action_set_reason(WebKitWebNavigationAction* navigationAction, WebKitWebNavigationReason reason)
231 {
232     g_return_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction));
233
234     if (navigationAction->priv->reason == reason)
235         return;
236
237     navigationAction->priv->reason = reason;
238     g_object_notify(G_OBJECT(navigationAction), "reason");
239 }
240
241 /**
242  * webkit_web_navigation_action_get_original_uri:
243  * @navigationAction: a #WebKitWebNavigationAction
244  *
245  * Returns the URI that was originally requested. This may differ from the
246  * navigation target, for instance because of a redirect.
247  *
248  * Return value: the originally requested URI
249  *
250  * Since: 1.0.3
251  */
252 const gchar* webkit_web_navigation_action_get_original_uri(WebKitWebNavigationAction* navigationAction)
253 {
254     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), NULL);
255
256     return navigationAction->priv->originalUri;
257 }
258
259 /**
260  * webkit_web_navigation_action_set_original_uri:
261  * @navigationAction: a #WebKitWebNavigationAction
262  * @originalUri: a URI
263  *
264  * Sets the URI that was originally requested. This may differ from the
265  * navigation target, for instance because of a redirect.
266  *
267  * Since: 1.0.3
268  */
269 void webkit_web_navigation_action_set_original_uri(WebKitWebNavigationAction* navigationAction, const gchar* originalUri)
270 {
271     g_return_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction));
272     g_return_if_fail(originalUri);
273
274     if (navigationAction->priv->originalUri &&
275         (!strcmp(navigationAction->priv->originalUri, originalUri)))
276         return;
277
278     g_free(navigationAction->priv->originalUri);
279     navigationAction->priv->originalUri = g_strdup(originalUri);
280     g_object_notify(G_OBJECT(navigationAction), "original-uri");
281 }
282
283 /**
284  * webkit_web_navigation_action_get_button:
285  * @navigationAction: a #WebKitWebNavigationAction
286  *
287  * Returns the mouse button used to click if the action was a mouse event.
288  * Otherwise returns -1.
289  *
290  * Return value: the mouse button used to click
291  *
292  * Since: 1.0.3
293  */
294 gint webkit_web_navigation_action_get_button(WebKitWebNavigationAction* navigationAction)
295 {
296     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), -1);
297
298     return navigationAction->priv->button;
299 }
300
301 /**
302  * webkit_web_navigation_action_get_modifier_state:
303  * @navigationAction: a #WebKitWebNavigationAction
304  *
305  * Returns a bitmask with the the state of the modifier keys.
306  *
307  * Return value: a bitmask with the state of the modifier keys
308  *
309  * Since: 1.0.3
310  */
311 gint webkit_web_navigation_action_get_modifier_state(WebKitWebNavigationAction* navigationAction)
312 {
313     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), 0);
314
315     return navigationAction->priv->modifier_state;
316 }