2 * Copyright (C) 2008 Jan Michael C. Alonzo
3 * Copyright (C) 2009 Igalia S.L.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #include "webkitwebbackforwardlist.h"
24 #include "BackForwardListImpl.h"
25 #include "HistoryItem.h"
27 #include "webkitglobalsprivate.h"
28 #include "webkitwebbackforwardlistprivate.h"
29 #include "webkitwebhistoryitem.h"
30 #include "webkitwebhistoryitemprivate.h"
31 #include "webkitwebview.h"
32 #include "webkitwebviewprivate.h"
36 * SECTION:webkitwebbackforwardlist
37 * @short_description: The history of a #WebKitWebView
38 * @see_also: #WebKitWebView, #WebKitWebHistoryItem
40 * <informalexample><programlisting>
41 * /<!-- -->* Get the WebKitWebBackForwardList from the WebKitWebView *<!-- -->/
42 * WebKitWebBackForwardList *back_forward_list = webkit_web_view_get_back_forward_list (my_web_view);
43 * WebKitWebHistoryItem *item = webkit_web_back_forward_list_get_current_item (back_forward_list);
45 * /<!-- -->* Do something with a WebKitWebHistoryItem *<!-- -->/
46 * g_print("%p", item);
48 * /<!-- -->* Control some parameters *<!-- -->/
49 * WebKitWebBackForwardList *back_forward_list = webkit_web_view_get_back_forward_list (my_web_view);
50 * webkit_web_back_forward_list_set_limit (back_forward_list, 30);
51 * </programlisting></informalexample>
55 using namespace WebKit;
57 struct _WebKitWebBackForwardListPrivate {
58 WebCore::BackForwardListImpl* backForwardList;
62 G_DEFINE_TYPE(WebKitWebBackForwardList, webkit_web_back_forward_list, G_TYPE_OBJECT);
64 static void webkit_web_back_forward_list_dispose(GObject* object)
66 WebKitWebBackForwardList* list = WEBKIT_WEB_BACK_FORWARD_LIST(object);
67 WebCore::BackForwardListImpl* backForwardList = core(list);
68 WebKitWebBackForwardListPrivate* priv = list->priv;
70 if (!priv->disposed) {
71 priv->disposed = true;
73 WebCore::HistoryItemVector items = backForwardList->entries();
74 GHashTable* table = webkit_history_items();
75 for (unsigned i = 0; i < items.size(); i++)
76 g_hash_table_remove(table, items[i].get());
79 G_OBJECT_CLASS(webkit_web_back_forward_list_parent_class)->dispose(object);
82 static void webkit_web_back_forward_list_class_init(WebKitWebBackForwardListClass* klass)
84 GObjectClass* object_class = G_OBJECT_CLASS(klass);
86 object_class->dispose = webkit_web_back_forward_list_dispose;
90 g_type_class_add_private(klass, sizeof(WebKitWebBackForwardListPrivate));
93 static void webkit_web_back_forward_list_init(WebKitWebBackForwardList* webBackForwardList)
95 webBackForwardList->priv = G_TYPE_INSTANCE_GET_PRIVATE(webBackForwardList, WEBKIT_TYPE_WEB_BACK_FORWARD_LIST, WebKitWebBackForwardListPrivate);
99 * webkit_web_back_forward_list_new_with_web_view: (skip)
100 * @web_view: the back forward list's #WebKitWebView
102 * Creates an instance of the back forward list with a controlling #WebKitWebView
104 * Return value: a #WebKitWebBackForwardList
106 * Deprecated: 1.3.4: Instances of #WebKitWebBackForwardList are
107 * created and owned by #WebKitWebView instances only.
109 WebKitWebBackForwardList* webkit_web_back_forward_list_new_with_web_view(WebKitWebView* webView)
111 g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
113 WebKitWebBackForwardList* webBackForwardList;
115 webBackForwardList = WEBKIT_WEB_BACK_FORWARD_LIST(g_object_new(WEBKIT_TYPE_WEB_BACK_FORWARD_LIST, NULL));
116 WebKitWebBackForwardListPrivate* priv = webBackForwardList->priv;
118 priv->backForwardList = static_cast<WebCore::BackForwardListImpl*>(core(webView)->backForwardList());
119 priv->backForwardList->setEnabled(TRUE);
121 return webBackForwardList;
125 * webkit_web_back_forward_list_go_forward:
126 * @web_back_forward_list: a #WebKitWebBackForwardList
128 * Steps forward in the back forward list
130 void webkit_web_back_forward_list_go_forward(WebKitWebBackForwardList* webBackForwardList)
132 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
134 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
135 if (backForwardList->enabled())
136 backForwardList->goForward();
140 * webkit_web_back_forward_list_go_back:
141 * @web_back_forward_list: a #WebKitWebBackForwardList
143 * Steps backward in the back forward list
145 void webkit_web_back_forward_list_go_back(WebKitWebBackForwardList* webBackForwardList)
147 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
149 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
150 if (backForwardList->enabled())
151 backForwardList->goBack();
155 * webkit_web_back_forward_list_contains_item:
156 * @web_back_forward_list: a #WebKitWebBackForwardList
157 * @history_item: (type WebKit.WebHistoryItem) (transfer none): the #WebKitWebHistoryItem to check
159 * Checks if @web_history_item is in the back forward list
161 * Return value: %TRUE if @web_history_item is in the back forward list, %FALSE if it doesn't
163 gboolean webkit_web_back_forward_list_contains_item(WebKitWebBackForwardList* webBackForwardList, WebKitWebHistoryItem* webHistoryItem)
165 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), FALSE);
166 g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem), FALSE);
168 WebCore::HistoryItem* historyItem = core(webHistoryItem);
170 g_return_val_if_fail(historyItem != NULL, FALSE);
172 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
174 return (backForwardList->enabled() ? backForwardList->containsItem(historyItem) : FALSE);
178 * webkit_web_back_forward_list_go_to_item:
179 * @web_back_forward_list: a #WebKitWebBackForwardList
180 * @history_item: (type WebKit.WebHistoryItem) (transfer none): the #WebKitWebHistoryItem to go to
182 * Go to the specified @web_history_item in the back forward list
184 void webkit_web_back_forward_list_go_to_item(WebKitWebBackForwardList* webBackForwardList, WebKitWebHistoryItem* webHistoryItem)
186 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
187 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
189 WebCore::HistoryItem* historyItem = core(webHistoryItem);
190 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
192 if (backForwardList->enabled() && historyItem)
193 backForwardList->goToItem(historyItem);
197 * webkit_web_back_forward_list_get_forward_list_with_limit:
198 * @web_back_forward_list: a #WebKitWebBackForwardList
199 * @limit: the number of items to retrieve
201 * Returns a list of items that succeed the current item, limited by @limit
203 * Return value: (element-type WebKit.WebHistoryItem) (transfer container): a #GList of items succeeding the current item, limited by @limit
205 GList* webkit_web_back_forward_list_get_forward_list_with_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
207 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
209 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
210 if (!backForwardList || !backForwardList->enabled())
213 WebCore::HistoryItemVector items;
214 GList* forwardItems = { 0 };
216 backForwardList->forwardListWithLimit(limit, items);
218 for (unsigned i = 0; i < items.size(); i++) {
219 WebKitWebHistoryItem* webHistoryItem = kit(items[i]);
220 forwardItems = g_list_prepend(forwardItems, webHistoryItem);
227 * webkit_web_back_forward_list_get_back_list_with_limit:
228 * @web_back_forward_list: a #WebKitWebBackForwardList
229 * @limit: the number of items to retrieve
231 * Returns a list of items that precede the current item, limited by @limit
233 * Return value: (element-type WebKit.WebHistoryItem) (transfer container): a #GList of items preceding the current item, limited by @limit
235 GList* webkit_web_back_forward_list_get_back_list_with_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
237 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
239 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
240 if (!backForwardList || !backForwardList->enabled())
243 WebCore::HistoryItemVector items;
244 GList* backItems = { 0 };
246 backForwardList->backListWithLimit(limit, items);
248 for (unsigned i = 0; i < items.size(); i++) {
249 WebKitWebHistoryItem* webHistoryItem = kit(items[i]);
250 backItems = g_list_prepend(backItems, webHistoryItem);
257 * webkit_web_back_forward_list_get_back_item:
258 * @web_back_forward_list: a #WebKitWebBackForwardList
260 * Returns the item that precedes the current item
262 * Return value: (type WebKit.WebHistoryItem) (transfer none): the #WebKitWebHistoryItem preceding the current item
264 WebKitWebHistoryItem* webkit_web_back_forward_list_get_back_item(WebKitWebBackForwardList* webBackForwardList)
266 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
268 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
269 if (!backForwardList || !backForwardList->enabled())
272 WebCore::HistoryItem* historyItem = backForwardList->backItem();
274 return (historyItem ? kit(historyItem) : NULL);
278 * webkit_web_back_forward_list_get_current_item:
279 * @web_back_forward_list: a #WebKitWebBackForwardList
281 * Returns the current item.
283 * Returns a NULL value if the back forward list is empty
285 * Return value: (type WebKit.WebHistoryItem) (transfer none): a #WebKitWebHistoryItem
287 WebKitWebHistoryItem* webkit_web_back_forward_list_get_current_item(WebKitWebBackForwardList* webBackForwardList)
289 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
291 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
292 if (!backForwardList || !backForwardList->enabled())
295 WebCore::HistoryItem* historyItem = backForwardList->currentItem();
297 return (historyItem ? kit(historyItem) : NULL);
301 * webkit_web_back_forward_list_get_forward_item:
302 * @web_back_forward_list: a #WebKitWebBackForwardList
304 * Returns the item that succeeds the current item.
306 * Returns a NULL value if there nothing that succeeds the current item
308 * Return value: (type WebKit.WebHistoryItem) (transfer none): a #WebKitWebHistoryItem
310 WebKitWebHistoryItem* webkit_web_back_forward_list_get_forward_item(WebKitWebBackForwardList* webBackForwardList)
312 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
314 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
315 if (!backForwardList || !backForwardList->enabled())
318 WebCore::HistoryItem* historyItem = backForwardList->forwardItem();
320 return (historyItem ? kit(historyItem) : NULL);
324 * webkit_web_back_forward_list_get_nth_item:
325 * @web_back_forward_list: a #WebKitWebBackForwardList
326 * @index: the index of the item
328 * Returns the item at a given index relative to the current item.
330 * Return value: (type WebKit.WebHistoryItem) (transfer none): the #WebKitWebHistoryItem located at the specified index relative to the current item
332 WebKitWebHistoryItem* webkit_web_back_forward_list_get_nth_item(WebKitWebBackForwardList* webBackForwardList, gint index)
334 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
336 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
337 if (!backForwardList)
340 WebCore::HistoryItem* historyItem = backForwardList->itemAtIndex(index);
342 return (historyItem ? kit(historyItem) : NULL);
346 * webkit_web_back_forward_list_get_back_length:
347 * @web_back_forward_list: a #WebKitWebBackForwardList
349 * Returns the number of items that preced the current item.
351 * Return value: a #gint corresponding to the number of items preceding the current item
353 gint webkit_web_back_forward_list_get_back_length(WebKitWebBackForwardList* webBackForwardList)
355 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
357 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
358 if (!backForwardList || !backForwardList->enabled())
361 return backForwardList->backListCount();
365 * webkit_web_back_forward_list_get_forward_length:
366 * @web_back_forward_list: a #WebKitWebBackForwardList
368 * Returns the number of items that succeed the current item.
370 * Return value: a #gint corresponding to the nuber of items succeeding the current item
372 gint webkit_web_back_forward_list_get_forward_length(WebKitWebBackForwardList* webBackForwardList)
374 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
376 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
377 if (!backForwardList || !backForwardList->enabled())
380 return backForwardList->forwardListCount();
384 * webkit_web_back_forward_list_get_limit:
385 * @web_back_forward_list: a #WebKitWebBackForwardList
387 * Returns the maximum limit of the back forward list.
389 * Return value: a #gint indicating the number of #WebKitWebHistoryItem the back forward list can hold
391 gint webkit_web_back_forward_list_get_limit(WebKitWebBackForwardList* webBackForwardList)
393 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
395 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
396 if (!backForwardList || !backForwardList->enabled())
399 return backForwardList->capacity();
403 * webkit_web_back_forward_list_set_limit:
404 * @web_back_forward_list: a #WebKitWebBackForwardList
405 * @limit: the limit to set the back forward list to
407 * Sets the maximum limit of the back forward list. If the back forward list
408 * exceeds its capacity, items will be removed everytime a new item has been
411 void webkit_web_back_forward_list_set_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
413 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
415 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
417 backForwardList->setCapacity(limit);
421 * webkit_web_back_forward_list_add_item:
422 * @web_back_forward_list: a #WebKitWebBackForwardList
423 * @history_item: (type WebKit.WebHistoryItem) (transfer none): the #WebKitWebHistoryItem to add
425 * Adds the item to the #WebKitWebBackForwardList.
427 * The @webBackForwardList will add a reference to the @webHistoryItem, so you
428 * don't need to keep a reference once you've added it to the list.
432 void webkit_web_back_forward_list_add_item(WebKitWebBackForwardList *webBackForwardList, WebKitWebHistoryItem *webHistoryItem)
434 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
436 g_object_ref(webHistoryItem);
438 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
439 WebCore::HistoryItem* historyItem = core(webHistoryItem);
441 backForwardList->addItem(historyItem);
445 * webkit_web_back_forward_list_clear:
446 * @web_back_forward_list: the #WebKitWebBackForwardList to be cleared
448 * Clears the @webBackForwardList by removing all its elements. Note that not even
449 * the current page is kept in list when cleared so you would have to add it later.
453 void webkit_web_back_forward_list_clear(WebKitWebBackForwardList* webBackForwardList)
455 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
457 WebCore::BackForwardListImpl* backForwardList = core(webBackForwardList);
458 if (!backForwardList || !backForwardList->enabled() || !backForwardList->entries().size())
461 // Clear the current list by setting capacity to 0
462 int capacity = backForwardList->capacity();
463 backForwardList->setCapacity(0);
464 backForwardList->setCapacity(capacity);
467 WebCore::BackForwardListImpl* WebKit::core(WebKitWebBackForwardList* webBackForwardList)
469 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
471 return webBackForwardList->priv ? webBackForwardList->priv->backForwardList : 0;