2 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
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.
21 #include "qwebhistory.h"
22 #include "qwebhistory_p.h"
23 #include "qwebframe_p.h"
25 #include "PlatformString.h"
29 #include "PageGroup.h"
31 #include <QSharedData>
35 InitialHistoryVersion = 1,
36 DefaultHistoryVersion = InitialHistoryVersion
40 \class QWebHistoryItem
42 \brief The QWebHistoryItem class represents one item in the history of a QWebPage
46 Each QWebHistoryItem instance represents an entry in the history stack of a Web page,
47 containing information about the page, its location, and when it was last visited.
49 The following table shows the properties of the page held by the history item, and
50 the functions used to access them.
53 \header \o Function \o Description
54 \row \o title() \o The page title.
55 \row \o url() \o The location of the page.
56 \row \o originalUrl() \o The URL used to access the page.
57 \row \o lastVisited() \o The date and time of the user's last visit to the page.
58 \row \o icon() \o The icon associated with the page that was provided by the server.
59 \row \o userData() \o The user specific data that was stored with the history item.
62 \note QWebHistoryItem objects are value based, but \e{explicitly shared}. Changing
63 a QWebHistoryItem instance by calling setUserData() will change all copies of that
66 \sa QWebHistory, QWebPage::history(), QWebHistoryInterface
70 Constructs a history item from \a other. The new item and \a other
71 will share their data, and modifying either this item or \a other will
72 modify both instances.
74 QWebHistoryItem::QWebHistoryItem(const QWebHistoryItem &other)
80 Assigns the \a other history item to this. This item and \a other
81 will share their data, and modifying either this item or \a other will
82 modify both instances.
84 QWebHistoryItem &QWebHistoryItem::operator=(const QWebHistoryItem &other)
91 Destroys the history item.
93 QWebHistoryItem::~QWebHistoryItem()
98 Returns the original URL associated with the history item.
102 QUrl QWebHistoryItem::originalUrl() const
105 return QUrl(d->item->originalURL().string());
111 Returns the URL associated with the history item.
113 \sa originalUrl(), title(), lastVisited()
115 QUrl QWebHistoryItem::url() const
118 return QUrl(d->item->url().string());
124 Returns the title of the page associated with the history item.
126 \sa icon(), url(), lastVisited()
128 QString QWebHistoryItem::title() const
131 return d->item->title();
137 Returns the date and time that the page associated with the item was last visited.
139 \sa title(), icon(), url()
141 QDateTime QWebHistoryItem::lastVisited() const
143 //FIXME : this will be wrong unless we correctly set lastVisitedTime ourselves
145 return QDateTime::fromTime_t((uint)d->item->lastVisitedTime());
151 Returns the icon associated with the history item.
153 \sa title(), url(), lastVisited()
155 QIcon QWebHistoryItem::icon() const
158 return *d->item->icon()->nativeImageForCurrentFrame();
164 Returns the user specific data that was stored with the history item.
168 QVariant QWebHistoryItem::userData() const
171 return d->item->userData();
178 Stores user specific data \a userData with the history item.
180 \note All copies of this item will be modified.
184 void QWebHistoryItem::setUserData(const QVariant& userData)
187 d->item->setUserData(userData);
193 QWebHistoryItem::QWebHistoryItem(QWebHistoryItemPrivate *priv)
200 Returns whether this is a valid history item.
202 bool QWebHistoryItem::isValid() const
210 \brief The QWebHistory class represents the history of a QWebPage
214 Each QWebPage instance contains a history of visited pages that can be accessed
215 by QWebPage::history(). QWebHistory represents this history and makes it possible
218 The history uses the concept of a \e{current item}, dividing the pages visited
219 into those that can be visited by navigating \e back and \e forward using the
220 back() and forward() functions. The current item can be obtained by calling
221 currentItem(), and an arbitrary item in the history can be made the current
222 item by passing it to goToItem().
224 A list of items describing the pages that can be visited by going back can be
225 obtained by calling the backItems() function; similarly, items describing the
226 pages ahead of the current page can be obtained with the forwardItems() function.
227 The total list of items is obtained with the items() function.
229 Just as with containers, functions are available to examine the history in terms
230 of a list. Arbitrary items in the history can be obtained with itemAt(), the total
231 number of items is given by count(), and the history can be cleared with the
234 QWebHistory's state can be saved to a QDataStream using the >> operator and loaded
235 by using the << operator.
237 \sa QWebHistoryItem, QWebHistoryInterface, QWebPage
241 QWebHistory::QWebHistory()
246 QWebHistory::~QWebHistory()
256 void QWebHistory::clear()
258 //shortcut to private BackForwardList
259 WebCore::BackForwardList* lst = d->lst;
261 //clear visited links
262 WebCore::Page* page = lst->page();
263 if (page && page->groupPtr())
264 page->groupPtr()->removeVisitedLinks();
266 //if count() == 0 then just return
267 if (!lst->entries().size())
270 RefPtr<WebCore::HistoryItem> current = lst->currentItem();
271 int capacity = lst->capacity();
274 lst->setCapacity(capacity); //revert capacity
275 lst->addItem(current.get()); //insert old current item
276 lst->goToItem(current.get()); //and set it as current again
278 d->page()->updateNavigationActions();
282 Returns a list of all items currently in the history.
286 QList<QWebHistoryItem> QWebHistory::items() const
288 const WebCore::HistoryItemVector &items = d->lst->entries();
290 QList<QWebHistoryItem> ret;
291 for (unsigned i = 0; i < items.size(); ++i) {
292 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
293 ret.append(QWebHistoryItem(priv));
299 Returns the list of items in the backwards history list.
300 At most \a maxItems entries are returned.
304 QList<QWebHistoryItem> QWebHistory::backItems(int maxItems) const
306 WebCore::HistoryItemVector items(maxItems);
307 d->lst->backListWithLimit(maxItems, items);
309 QList<QWebHistoryItem> ret;
310 for (unsigned i = 0; i < items.size(); ++i) {
311 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
312 ret.append(QWebHistoryItem(priv));
318 Returns the list of items in the forward history list.
319 At most \a maxItems entries are returned.
323 QList<QWebHistoryItem> QWebHistory::forwardItems(int maxItems) const
325 WebCore::HistoryItemVector items(maxItems);
326 d->lst->forwardListWithLimit(maxItems, items);
328 QList<QWebHistoryItem> ret;
329 for (unsigned i = 0; i < items.size(); ++i) {
330 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
331 ret.append(QWebHistoryItem(priv));
337 Returns true if there is an item preceding the current item in the history;
338 otherwise returns false.
342 bool QWebHistory::canGoBack() const
344 return d->lst->backListCount() > 0;
348 Returns true if we have an item to go forward to; otherwise returns false.
352 bool QWebHistory::canGoForward() const
354 return d->lst->forwardListCount() > 0;
358 Set the current item to be the previous item in the history and goes to the
359 corresponding page; i.e., goes back one history item.
361 \sa forward(), goToItem()
363 void QWebHistory::back()
367 WebCore::Page* page = d->lst->page();
368 page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward);
373 Sets the current item to be the next item in the history and goes to the
374 corresponding page; i.e., goes forward one history item.
376 \sa back(), goToItem()
378 void QWebHistory::forward()
380 if (canGoForward()) {
382 WebCore::Page* page = d->lst->page();
383 page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward);
388 Sets the current item to be the specified \a item in the history and goes to the page.
390 \sa back(), forward()
392 void QWebHistory::goToItem(const QWebHistoryItem &item)
394 d->lst->goToItem(item.d->item);
395 WebCore::Page* page = d->lst->page();
396 page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward);
400 Returns the item before the current item in the history.
402 QWebHistoryItem QWebHistory::backItem() const
404 WebCore::HistoryItem *i = d->lst->backItem();
405 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
406 return QWebHistoryItem(priv);
410 Returns the current item in the history.
412 QWebHistoryItem QWebHistory::currentItem() const
414 WebCore::HistoryItem *i = d->lst->currentItem();
415 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
416 return QWebHistoryItem(priv);
420 Returns the item after the current item in the history.
422 QWebHistoryItem QWebHistory::forwardItem() const
424 WebCore::HistoryItem *i = d->lst->forwardItem();
425 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
426 return QWebHistoryItem(priv);
431 Returns the index of the current item in history.
433 int QWebHistory::currentItemIndex() const
435 return d->lst->backListCount();
439 Returns the item at index \a i in the history.
441 QWebHistoryItem QWebHistory::itemAt(int i) const
443 QWebHistoryItemPrivate *priv;
444 if (i < 0 || i >= count())
445 priv = new QWebHistoryItemPrivate(0);
447 WebCore::HistoryItem *item = d->lst->entries()[i].get();
448 priv = new QWebHistoryItemPrivate(item);
450 return QWebHistoryItem(priv);
454 Returns the total number of items in the history.
456 int QWebHistory::count() const
458 return d->lst->entries().size();
463 Returns the maximum number of items in the history.
465 \sa setMaximumItemCount()
467 int QWebHistory::maximumItemCount() const
469 return d->lst->capacity();
474 Sets the maximum number of items in the history to \a count.
476 \sa maximumItemCount()
478 void QWebHistory::setMaximumItemCount(int count)
480 d->lst->setCapacity(count);
485 \fn QDataStream& operator<<(QDataStream& stream, const QWebHistory& history)
488 \brief The operator<< function streams a history into a data stream.
490 It saves the \a history into the specified \a stream.
493 QDataStream& operator<<(QDataStream& target, const QWebHistory& history)
495 QWebHistoryPrivate* d = history.d;
497 int version = DefaultHistoryVersion;
500 target << history.count() << history.currentItemIndex();
502 const WebCore::HistoryItemVector &items = d->lst->entries();
503 for (unsigned i = 0; i < items.size(); i++)
504 items[i].get()->saveState(target, version);
510 \fn QDataStream& operator>>(QDataStream& stream, QWebHistory& history)
514 \brief The operator>> function loads a history from a data stream.
516 Loads a QWebHistory from the specified \a stream into the given \a history.
519 QDataStream& operator>>(QDataStream& source, QWebHistory& history)
521 QWebHistoryPrivate* d = history.d;
530 source >> count >> currentIndex;
533 // only if there are elements
535 // after clear() is new clear HistoryItem (at the end we had to remove it)
536 WebCore::HistoryItem* nullItem = d->lst->currentItem();
537 for (int i = 0; i < count; i++) {
538 WTF::PassRefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create();
539 item->restoreState(source, version);
540 d->lst->addItem(item);
542 d->lst->removeItem(nullItem);
543 history.goToItem(history.itemAt(currentIndex));
547 d->page()->updateNavigationActions();
552 QWebPagePrivate* QWebHistoryPrivate::page()
554 return QWebFramePrivate::kit(lst->page()->mainFrame())->page()->handle();