2 * Copyright (C) 2008 Jan Michael C. Alonzo
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.
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.
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.
22 #include "webkitwebbackforwardlist.h"
23 #include "webkitprivate.h"
24 #include "webkitwebhistoryitem.h"
25 #include "webkitwebview.h"
29 #include "BackForwardList.h"
30 #include "HistoryItem.h"
33 * SECTION:webkitwebbackforwardlist
34 * @short_description: The history of a #WebKitWebView
35 * @see_also: #WebKitWebView, #WebKitWebHistoryItem
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);
42 * /<!-- -->* Do something with a WebKitWebHistoryItem *<!-- -->/
43 * g_print("%p", item);
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>
52 using namespace WebKit;
56 struct _WebKitWebBackForwardListPrivate {
57 WebCore::BackForwardList* backForwardList;
60 #define WEBKIT_WEB_BACK_FORWARD_LIST_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_BACK_FORWARD_LIST, WebKitWebBackForwardListPrivate))
62 G_DEFINE_TYPE(WebKitWebBackForwardList, webkit_web_back_forward_list, G_TYPE_OBJECT);
64 static void webkit_web_back_forward_list_class_init(WebKitWebBackForwardListClass* klass)
66 g_type_class_add_private(klass, sizeof(WebKitWebBackForwardListPrivate));
69 static void webkit_web_back_forward_list_init(WebKitWebBackForwardList* webBackForwardList)
71 webBackForwardList->priv = WEBKIT_WEB_BACK_FORWARD_LIST_GET_PRIVATE(webBackForwardList);
75 * webkit_web_back_forward_list_new_with_web_view:
76 * @web_view: the back forward list's #WebKitWebView
78 * Creates an instance of the back forward list with a controlling #WebKitWebView
80 * Return value: a #WebKitWebBackForwardList
82 WebKitWebBackForwardList* webkit_web_back_forward_list_new_with_web_view(WebKitWebView* webView)
84 g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
86 WebKitWebBackForwardList* webBackForwardList;
88 webBackForwardList = WEBKIT_WEB_BACK_FORWARD_LIST(g_object_new(WEBKIT_TYPE_WEB_BACK_FORWARD_LIST, NULL));
89 WebKitWebBackForwardListPrivate* priv = webBackForwardList->priv;
91 priv->backForwardList = core(webView)->backForwardList();
92 priv->backForwardList->setEnabled(TRUE);
94 return webBackForwardList;
98 * webkit_web_back_forward_list_go_forward:
99 * @web_back_forward_list: a #WebKitWebBackForwardList
101 * Steps forward in the back forward list
103 void webkit_web_back_forward_list_go_forward(WebKitWebBackForwardList* webBackForwardList)
105 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
107 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
108 if (backForwardList->enabled())
109 backForwardList->goForward();
113 * webkit_web_back_forward_list_go_back:
114 * @web_back_forward_list: a #WebKitWebBackForwardList
116 * Steps backward in the back forward list
118 void webkit_web_back_forward_list_go_back(WebKitWebBackForwardList* webBackForwardList)
120 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
122 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
123 if (backForwardList->enabled())
124 backForwardList->goBack();
128 * webkit_web_back_forward_list_contains_item:
129 * @web_back_forward_list: a #WebKitWebBackForwardList
130 * @history_item: the #WebKitWebHistoryItem to check
132 * Checks if @web_history_item is in the back forward list
134 * Return: %TRUE if @web_history_item is in the back forward list, %FALSE if it doesn't
136 gboolean webkit_web_back_forward_list_contains_item(WebKitWebBackForwardList* webBackForwardList, WebKitWebHistoryItem* webHistoryItem)
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);
141 WebCore::HistoryItem* historyItem = core(webHistoryItem);
143 g_return_val_if_fail(historyItem != NULL, FALSE);
145 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
147 return (backForwardList->enabled() ? backForwardList->containsItem(historyItem) : FALSE);
151 * webkit_web_back_forward_list_go_to_item:
152 * @web_back_forward_list: a #WebKitWebBackForwardList
153 * @history_item: the #WebKitWebHistoryItem to go to
155 * Go to the specified @web_history_item in the back forward list
157 void webkit_web_back_forward_list_go_to_item(WebKitWebBackForwardList* webBackForwardList, WebKitWebHistoryItem* webHistoryItem)
159 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
160 g_return_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(webHistoryItem));
162 WebCore::HistoryItem* historyItem = core(webHistoryItem);
163 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
165 if (backForwardList->enabled() && historyItem)
166 backForwardList->goToItem(historyItem);
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
174 * Returns a list of items that succeed the current item, limited by @limit
176 * Return value: a #GList of items succeeding the current item, limited by @limit
178 GList* webkit_web_back_forward_list_get_forward_list_with_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
180 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
182 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
183 if (!backForwardList || !backForwardList->enabled())
186 WebCore::HistoryItemVector items;
187 GList* forwardItems = { 0 };
189 backForwardList->forwardListWithLimit(limit, items);
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));
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
204 * Returns a list of items that precede the current item, limited by @limit
206 * Return value: a #GList of items preceding the current item, limited by @limit
208 GList* webkit_web_back_forward_list_get_back_list_with_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
210 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
212 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
213 if (!backForwardList || !backForwardList->enabled())
216 WebCore::HistoryItemVector items;
217 GList* backItems = { 0 };
219 backForwardList->backListWithLimit(limit, items);
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));
230 * webkit_web_back_forward_list_get_back_item:
231 * @web_back_forward_list: a #WebBackForwardList
233 * Returns the item that precedes the current item
235 * Return value: the #WebKitWebHistoryItem preceding the current item
237 WebKitWebHistoryItem* webkit_web_back_forward_list_get_back_item(WebKitWebBackForwardList* webBackForwardList)
239 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
241 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
242 if (!backForwardList || !backForwardList->enabled())
245 WebCore::HistoryItem* historyItem = backForwardList->backItem();
247 return (historyItem ? kit(historyItem) : NULL);
251 * webkit_web_back_forward_list_get_current_item:
252 * @web_back_forward_list: a #WebKitWebBackForwardList
254 * Returns the current item.
256 * Returns a NULL value if the back forward list is empty
258 * Return value: a #WebKitWebHistoryItem
260 WebKitWebHistoryItem* webkit_web_back_forward_list_get_current_item(WebKitWebBackForwardList* webBackForwardList)
262 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
264 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
265 if (!backForwardList || !backForwardList->enabled())
268 WebCore::HistoryItem* historyItem = backForwardList->currentItem();
270 return (historyItem ? kit(historyItem) : NULL);
274 * webkit_web_back_forward_list_get_forward_item:
275 * @web_back_forward_list: a #WebKitWebBackForwardList
277 * Returns the item that succeeds the current item.
279 * Returns a NULL value if there nothing that succeeds the current item
281 * Return value: a #WebKitWebHistoryItem
283 WebKitWebHistoryItem* webkit_web_back_forward_list_get_forward_item(WebKitWebBackForwardList* webBackForwardList)
285 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
287 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
288 if (!backForwardList || !backForwardList->enabled())
291 WebCore::HistoryItem* historyItem = backForwardList->forwardItem();
293 return (historyItem ? kit(historyItem) : NULL);
297 * webkit_web_back_forward_list_get_nth_item:
298 * @web_back_forward_list: a #WebKitWebBackForwardList
299 * @index: the index of the item
301 * Returns the item at a given index relative to the current item.
303 * Return value: the #WebKitWebHistoryItem located at the specified index relative to the current item
305 WebKitWebHistoryItem* webkit_web_back_forward_list_get_nth_item(WebKitWebBackForwardList* webBackForwardList, gint index)
307 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
309 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
310 if (!backForwardList)
313 WebCore::HistoryItem* historyItem = backForwardList->itemAtIndex(index);
315 return (historyItem ? kit(historyItem) : NULL);
319 * webkit_web_back_forward_list_get_back_length:
320 * @web_back_forward_list: a #WebKitWebBackForwardList
322 * Returns the number of items that preced the current item.
324 * Return value: a #gint corresponding to the number of items preceding the current item
326 gint webkit_web_back_forward_list_get_back_length(WebKitWebBackForwardList* webBackForwardList)
328 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
330 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
331 if (!backForwardList || !backForwardList->enabled())
334 return backForwardList->backListCount();
338 * webkit_web_back_forward_list_get_forward_length:
339 * @web_back_forward_list: a #WebKitWebBackForwardList
341 * Returns the number of items that succeed the current item.
343 * Return value: a #gint corresponding to the nuber of items succeeding the current item
345 gint webkit_web_back_forward_list_get_forward_length(WebKitWebBackForwardList* webBackForwardList)
347 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
349 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
350 if (!backForwardList || !backForwardList->enabled())
353 return backForwardList->forwardListCount();
357 * webkit_web_back_forward_list_get_limit:
358 * @web_back_forward_list: a #WebKitWebBackForwardList
360 * Returns the maximum limit of the back forward list.
362 * Return value: a #gint indicating the number of #WebHistoryItem the back forward list can hold
364 gint webkit_web_back_forward_list_get_limit(WebKitWebBackForwardList* webBackForwardList)
366 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), 0);
368 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
369 if (!backForwardList || !backForwardList->enabled())
372 return backForwardList->capacity();
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
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
384 void webkit_web_back_forward_list_set_limit(WebKitWebBackForwardList* webBackForwardList, gint limit)
386 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
388 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
390 backForwardList->setCapacity(limit);
394 * webkit_web_back_forward_list_add_item:
395 * @web_back_forward_list: a #WebKitWebBackForwardList
396 * @history_item: the #WebKitWebHistoryItem to add
398 * Adds the item to the #WebKitWebBackForwardList.
400 void webkit_web_back_forward_list_add_item(WebKitWebBackForwardList *webBackForwardList, WebKitWebHistoryItem *webHistoryItem)
402 g_return_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList));
404 WebCore::BackForwardList* backForwardList = core(webBackForwardList);
405 WebCore::HistoryItem* historyItem = core(webHistoryItem);
407 backForwardList->addItem(historyItem);
410 } /* end extern "C" */
412 WebCore::BackForwardList* WebKit::core(WebKitWebBackForwardList* webBackForwardList)
414 g_return_val_if_fail(WEBKIT_IS_WEB_BACK_FORWARD_LIST(webBackForwardList), NULL);
416 return webBackForwardList->priv ? webBackForwardList->priv->backForwardList : 0;