a772bacecfb5eed74f0cf4836a1acad406e9719a
[WebKit-https.git] / WebKit / gtk / webkit / webkitwebbackforwardlist.cpp
1 /*
2  * Copyright (C) 2008 Jan Michael C. Alonzo
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 "webkitwebbackforwardlist.h"
23 #include "webkitprivate.h"
24 #include "webkitwebhistoryitem.h"
25 #include "webkitwebview.h"
26
27 #include <glib.h>
28
29 #include "BackForwardList.h"
30 #include "HistoryItem.h"
31
32 /**
33  * SECTION:webkitwebbackforwardlist
34  * @short_description: The history of a #WebKitWebView
35  * @see_also: #WebKitWebView, #WebKitWebHistoryItem
36  *
37  * <informalexample><programlisting>
38  * /<!-- -->* Get the WebKitWebBackForwardList from the WebKitWebView *<!-- -->/
39  * WebKitWebBackForwardList *back_forward_list = webkit_web_view_get_back_forward_list (my_web_view);
40  * WebKitWebHistoryItem *item = webkit_web_back_forward_list_get_current_item (back_forward_list);
41  *
42  * /<!-- -->* Do something with a WebKitWebHistoryItem *<!-- -->/
43  * g_print("%p", item);
44  *
45  * /<!-- -->* Control some parameters *<!-- -->/
46  * WebKitWebBackForwardList *back_forward_list = webkit_web_view_get_back_forward_list (my_web_view);
47  * webkit_web_back_forward_list_set_limit (back_forward_list, 30);
48  * </programlisting></informalexample>
49  *
50  */
51
52 using namespace WebKit;
53
54 extern "C" {
55
56 struct _WebKitWebBackForwardListPrivate {
57     WebCore::BackForwardList* backForwardList;
58 };
59
60 #define WEBKIT_WEB_BACK_FORWARD_LIST_GET_PRIVATE(obj)    (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_BACK_FORWARD_LIST, WebKitWebBackForwardListPrivate))
61
62 G_DEFINE_TYPE(WebKitWebBackForwardList, webkit_web_back_forward_list, G_TYPE_OBJECT);
63
64 static void webkit_web_back_forward_list_class_init(WebKitWebBackForwardListClass* klass)
65 {
66     g_type_class_add_private(klass, sizeof(WebKitWebBackForwardListPrivate));
67 }
68
69 static void webkit_web_back_forward_list_init(WebKitWebBackForwardList* webBackForwardList)
70 {
71     webBackForwardList->priv = WEBKIT_WEB_BACK_FORWARD_LIST_GET_PRIVATE(webBackForwardList);
72 }
73
74 /**
75  * webkit_web_back_forward_list_new_with_web_view:
76  * @web_view: the back forward list's #WebKitWebView
77  *
78  * Creates an instance of the back forward list with a controlling #WebKitWebView
79  *
80  * Return value: a #WebKitWebBackForwardList
81  */
82 WebKitWebBackForwardList* webkit_web_back_forward_list_new_with_web_view(WebKitWebView* webView)
83 {
84     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
85
86     WebKitWebBackForwardList* webBackForwardList;
87
88     webBackForwardList = WEBKIT_WEB_BACK_FORWARD_LIST(g_object_new(WEBKIT_TYPE_WEB_BACK_FORWARD_LIST, NULL));
89     WebKitWebBackForwardListPrivate* priv = webBackForwardList->priv;
90
91     priv->backForwardList = core(webView)->backForwardList();
92     priv->backForwardList->setEnabled(TRUE);
93
94     return webBackForwardList;
95 }
96
97 /**
98  * webkit_web_back_forward_list_go_forward:
99  * @web_back_forward_list: a #WebKitWebBackForwardList
100  *
101  * Steps forward in the back forward list
102  */
103 void webkit_web_back_forward_list_go_forward(WebKitWebBackForwardList* webBackForwardList)
104 {
105     g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
106
107     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
108     if (backForwardList->enabled())
109         backForwardList->goForward();
110 }
111
112 /**
113  * webkit_web_back_forward_list_go_back:
114  * @web_back_forward_list: a #WebKitWebBackForwardList
115  *
116  * Steps backward in the back forward list
117  */
118 void webkit_web_back_forward_list_go_back(WebKitWebBackForwardList* webBackForwardList)
119 {
120     g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
121
122     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
123     if (backForwardList->enabled())
124         backForwardList->goBack();
125 }
126
127 /**
128  * webkit_web_back_forward_list_contains_item:
129  * @web_back_forward_list: a #WebKitWebBackForwardList
130  * @history_item: the #WebKitWebHistoryItem to check
131  *
132  * Checks if @web_history_item is in the back forward list
133  *
134  * Return: %TRUE if @web_history_item is in the back forward list, %FALSE if it doesn't
135  */
136 gboolean webkit_web_back_forward_list_contains_item(WebKitWebBackForwardList* webBackForwardList, WebKitWebHistoryItem* webHistoryItem)
137 {
138     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), FALSE);
139     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), FALSE);
140
141     WebCore::HistoryItem* historyItem = core(webHistoryItem);
142
143     g_return_val_if_fail(historyItem != NULL, FALSE);
144
145     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
146
147     return (backForwardList->enabled() ? backForwardList->containsItem(historyItem) : FALSE);
148 }
149
150 /**
151  * webkit_web_back_forward_list_go_to_item:
152  * @web_back_forward_list: a #WebKitWebBackForwardList
153  * @history_item: the #WebKitWebHistoryItem to go to
154  *
155  * Go to the specified @web_history_item in the back forward list
156  */
157 void webkit_web_back_forward_list_go_to_item(WebKitWebBackForwardList* webBackForwardList, WebKitWebHistoryItem* webHistoryItem)
158 {
159     g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
160     g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
161
162     WebCore::HistoryItem* historyItem = core(webHistoryItem);
163     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
164
165     if (backForwardList->enabled() && historyItem)
166         backForwardList->goToItem(historyItem);
167 }
168
169 /**
170  * webkit_web_back_forward_list_get_forward_list_with_limit:
171  * @web_back_forward_list: a #WebKitWebBackForwardList
172  * @limit: the number of items to retrieve
173  *
174  * Returns a list of items that succeed the current item, limited by @limit
175  *
176  * Return value: a #GList of items succeeding the current item, limited by @limit
177  */
178 GList* webkit_web_back_forward_list_get_forward_list_with_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
179 {
180     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
181
182     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
183     if (!backForwardList || !backForwardList->enabled())
184         return NULL;
185
186     WebCore::HistoryItemVector items;
187     GList* forwardItems = { 0 };
188
189     backForwardList->forwardListWithLimit(limit, items);
190
191     for (unsigned i = 0; i < items.size(); i++) {
192         WebKitWebHistoryItem* webHistoryItem = webkit_web_history_item_new_with_core_item(items[i]);
193         forwardItems = g_list_prepend(forwardItems, g_object_ref(webHistoryItem));
194     }
195
196     return forwardItems;
197 }
198
199 /**
200  * webkit_web_back_forward_list_get_back_list_with_limit:
201  * @web_back_forward_list: a #WebKitWebBackForwardList
202  * @limit: the number of items to retrieve
203  *
204  * Returns a list of items that precede the current item, limited by @limit
205  *
206  * Return value: a #GList of items preceding the current item, limited by @limit
207  */
208 GList* webkit_web_back_forward_list_get_back_list_with_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
209 {
210     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
211
212     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
213     if (!backForwardList || !backForwardList->enabled())
214         return NULL;
215
216     WebCore::HistoryItemVector items;
217     GList* backItems = { 0 };
218
219     backForwardList->backListWithLimit(limit, items);
220
221     for (unsigned i = 0; i < items.size(); i++) {
222         WebKitWebHistoryItem* webHistoryItem = webkit_web_history_item_new_with_core_item(items[i]);
223         backItems = g_list_prepend(backItems, g_object_ref(webHistoryItem));
224     }
225
226     return backItems;
227 }
228
229 /**
230  * webkit_web_back_forward_list_get_back_item:
231  * @web_back_forward_list: a #WebBackForwardList
232  *
233  * Returns the item that precedes the current item
234  *
235  * Return value: the #WebKitWebHistoryItem preceding the current item
236  */
237 WebKitWebHistoryItem* webkit_web_back_forward_list_get_back_item(WebKitWebBackForwardList* webBackForwardList)
238 {
239     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
240
241     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
242     if (!backForwardList || !backForwardList->enabled())
243         return NULL;
244
245     WebCore::HistoryItem* historyItem = backForwardList->backItem();
246
247     return (historyItem ? kit(historyItem) : NULL);
248 }
249
250 /**
251  * webkit_web_back_forward_list_get_current_item:
252  * @web_back_forward_list: a #WebKitWebBackForwardList
253  *
254  * Returns the current item.
255  *
256  * Returns a NULL value if the back forward list is empty
257  *
258  * Return value: a #WebKitWebHistoryItem
259  */
260 WebKitWebHistoryItem* webkit_web_back_forward_list_get_current_item(WebKitWebBackForwardList* webBackForwardList)
261 {
262     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
263
264     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
265     if (!backForwardList || !backForwardList->enabled())
266         return NULL;
267
268     WebCore::HistoryItem* historyItem = backForwardList->currentItem();
269
270     return (historyItem ? kit(historyItem) : NULL);
271 }
272
273 /**
274  * webkit_web_back_forward_list_get_forward_item:
275  * @web_back_forward_list: a #WebKitWebBackForwardList
276  *
277  * Returns the item that succeeds the current item.
278  *
279  * Returns a NULL value if there nothing that succeeds the current item
280  *
281  * Return value: a #WebKitWebHistoryItem
282  */
283 WebKitWebHistoryItem* webkit_web_back_forward_list_get_forward_item(WebKitWebBackForwardList* webBackForwardList)
284 {
285     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
286
287     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
288     if (!backForwardList || !backForwardList->enabled())
289         return NULL;
290
291     WebCore::HistoryItem* historyItem = backForwardList->forwardItem();
292
293     return (historyItem ? kit(historyItem) : NULL);
294 }
295
296 /**
297  * webkit_web_back_forward_list_get_nth_item:
298  * @web_back_forward_list: a #WebKitWebBackForwardList
299  * @index: the index of the item
300  *
301  * Returns the item at a given index relative to the current item.
302  *
303  * Return value: the #WebKitWebHistoryItem located at the specified index relative to the current item
304  */
305 WebKitWebHistoryItem* webkit_web_back_forward_list_get_nth_item(WebKitWebBackForwardList* webBackForwardList, gint index)
306 {
307     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
308
309     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
310     if (!backForwardList)
311         return NULL;
312
313     WebCore::HistoryItem* historyItem = backForwardList->itemAtIndex(index);
314
315     return (historyItem ? kit(historyItem) : NULL);
316 }
317
318 /**
319  * webkit_web_back_forward_list_get_back_length:
320  * @web_back_forward_list: a #WebKitWebBackForwardList
321  *
322  * Returns the number of items that preced the current item.
323  *
324  * Return value: a #gint corresponding to the number of items preceding the current item
325  */
326 gint webkit_web_back_forward_list_get_back_length(WebKitWebBackForwardList* webBackForwardList)
327 {
328     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
329
330     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
331     if (!backForwardList || !backForwardList->enabled())
332         return 0;
333
334     return backForwardList->backListCount();
335 }
336
337 /**
338  * webkit_web_back_forward_list_get_forward_length:
339  * @web_back_forward_list: a #WebKitWebBackForwardList
340  *
341  * Returns the number of items that succeed the current item.
342  *
343  * Return value: a #gint corresponding to the nuber of items succeeding the current item
344  */
345 gint webkit_web_back_forward_list_get_forward_length(WebKitWebBackForwardList* webBackForwardList)
346 {
347     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
348
349     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
350     if (!backForwardList || !backForwardList->enabled())
351         return 0;
352
353     return backForwardList->forwardListCount();
354 }
355
356 /**
357  * webkit_web_back_forward_list_get_limit:
358  * @web_back_forward_list: a #WebKitWebBackForwardList
359  *
360  * Returns the maximum limit of the back forward list.
361  *
362  * Return value: a #gint indicating the number of #WebHistoryItem the back forward list can hold
363  */
364 gint webkit_web_back_forward_list_get_limit(WebKitWebBackForwardList* webBackForwardList)
365 {
366     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
367
368     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
369     if (!backForwardList || !backForwardList->enabled())
370         return 0;
371
372     return backForwardList->capacity();
373 }
374
375 /**
376  * webkit_web_back_forward_list_set_limit:
377  * @web_back_forward_list: a #WebKitWebBackForwardList
378  * @limit: the limit to set the back forward list to
379  *
380  * Sets the maximum limit of the back forward list. If the back forward list
381  * exceeds its capacity, items will be removed everytime a new item has been
382  * added.
383  */
384 void webkit_web_back_forward_list_set_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
385 {
386     g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
387
388     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
389     if (backForwardList)
390         backForwardList->setCapacity(limit);
391 }
392
393 /**
394  * webkit_web_back_forward_list_add_item:
395  * @web_back_forward_list: a #WebKitWebBackForwardList
396  * @history_item: the #WebKitWebHistoryItem to add
397  *
398  * Adds the item to the #WebKitWebBackForwardList.
399  */
400 void webkit_web_back_forward_list_add_item(WebKitWebBackForwardList *webBackForwardList, WebKitWebHistoryItem *webHistoryItem)
401 {
402     g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
403
404     WebCore::BackForwardList* backForwardList = core(webBackForwardList);
405     WebCore::HistoryItem* historyItem = core(webHistoryItem);
406
407     backForwardList->addItem(historyItem);
408 }
409
410 } /* end extern "C" */
411
412 WebCore::BackForwardList* WebKit::core(WebKitWebBackForwardList* webBackForwardList)
413 {
414     g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
415
416     return webBackForwardList->priv ? webBackForwardList->priv->backForwardList : 0;
417 }