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 "BackForwardListImpl.h"
26 #include "IconDatabaseBase.h"
31 #include "PageGroup.h"
32 #include "PlatformString.h"
34 #include <QSharedData>
38 InitialHistoryVersion = 1,
39 DefaultHistoryVersion = InitialHistoryVersion
43 \class QWebHistoryItem
45 \brief The QWebHistoryItem class represents one item in the history of a QWebPage
49 Each QWebHistoryItem instance represents an entry in the history stack of a Web page,
50 containing information about the page, its location, and when it was last visited.
52 The following table shows the properties of the page held by the history item, and
53 the functions used to access them.
56 \header \o Function \o Description
57 \row \o title() \o The page title.
58 \row \o url() \o The location of the page.
59 \row \o originalUrl() \o The URL used to access the page.
60 \row \o lastVisited() \o The date and time of the user's last visit to the page.
61 \row \o icon() \o The icon associated with the page that was provided by the server.
62 \row \o userData() \o The user specific data that was stored with the history item.
65 \note QWebHistoryItem objects are value based, but \e{explicitly shared}. Changing
66 a QWebHistoryItem instance by calling setUserData() will change all copies of that
69 \sa QWebHistory, QWebPage::history(), QWebHistoryInterface
73 Constructs a history item from \a other. The new item and \a other
74 will share their data, and modifying either this item or \a other will
75 modify both instances.
77 QWebHistoryItem::QWebHistoryItem(const QWebHistoryItem &other)
83 Assigns the \a other history item to this. This item and \a other
84 will share their data, and modifying either this item or \a other will
85 modify both instances.
87 QWebHistoryItem &QWebHistoryItem::operator=(const QWebHistoryItem &other)
94 Destroys the history item.
96 QWebHistoryItem::~QWebHistoryItem()
101 Returns the original URL associated with the history item.
105 QUrl QWebHistoryItem::originalUrl() const
108 return d->item->originalURL();
114 Returns the URL associated with the history item.
116 \sa originalUrl(), title(), lastVisited()
118 QUrl QWebHistoryItem::url() const
121 return d->item->url();
127 Returns the title of the page associated with the history item.
129 \sa icon(), url(), lastVisited()
131 QString QWebHistoryItem::title() const
134 return d->item->title();
140 Returns the date and time that the page associated with the item was last visited.
142 \sa title(), icon(), url()
144 QDateTime QWebHistoryItem::lastVisited() const
146 //FIXME : this will be wrong unless we correctly set lastVisitedTime ourselves
148 return QDateTime::fromTime_t((uint)d->item->lastVisitedTime());
154 Returns the icon associated with the history item.
156 \sa title(), url(), lastVisited()
158 QIcon QWebHistoryItem::icon() const
161 return *WebCore::iconDatabase().synchronousIconForPageURL(d->item->url(), WebCore::IntSize(16, 16))->nativeImageForCurrentFrame();
168 Returns the user specific data that was stored with the history item.
172 QVariant QWebHistoryItem::userData() const
175 return d->item->userData();
182 Stores user specific data \a userData with the history item.
184 \note All copies of this item will be modified.
188 void QWebHistoryItem::setUserData(const QVariant& userData)
191 d->item->setUserData(userData);
197 QWebHistoryItem::QWebHistoryItem(QWebHistoryItemPrivate *priv)
204 Returns whether this is a valid history item.
206 bool QWebHistoryItem::isValid() const
214 \brief The QWebHistory class represents the history of a QWebPage
218 Each QWebPage instance contains a history of visited pages that can be accessed
219 by QWebPage::history(). QWebHistory represents this history and makes it possible
222 The history uses the concept of a \e{current item}, dividing the pages visited
223 into those that can be visited by navigating \e back and \e forward using the
224 back() and forward() functions. The current item can be obtained by calling
225 currentItem(), and an arbitrary item in the history can be made the current
226 item by passing it to goToItem().
228 A list of items describing the pages that can be visited by going back can be
229 obtained by calling the backItems() function; similarly, items describing the
230 pages ahead of the current page can be obtained with the forwardItems() function.
231 The total list of items is obtained with the items() function.
233 Just as with containers, functions are available to examine the history in terms
234 of a list. Arbitrary items in the history can be obtained with itemAt(), the total
235 number of items is given by count(), and the history can be cleared with the
238 QWebHistory's state can be saved to a QDataStream using the >> operator and loaded
239 by using the << operator.
241 \sa QWebHistoryItem, QWebHistoryInterface, QWebPage
245 QWebHistory::QWebHistory()
250 QWebHistory::~QWebHistory()
260 void QWebHistory::clear()
262 //shortcut to private BackForwardListImpl
263 WebCore::BackForwardListImpl* lst = d->lst;
265 //clear visited links
266 WebCore::Page* page = static_cast<WebCore::BackForwardListImpl*>(lst)->page();
267 if (page && page->groupPtr())
268 page->groupPtr()->removeVisitedLinks();
270 //if count() == 0 then just return
271 if (!lst->entries().size())
274 RefPtr<WebCore::HistoryItem> current = lst->currentItem();
275 int capacity = lst->capacity();
278 lst->setCapacity(capacity); //revert capacity
279 lst->addItem(current.get()); //insert old current item
280 lst->goToItem(current.get()); //and set it as current again
282 d->page()->updateNavigationActions();
286 Returns a list of all items currently in the history.
290 QList<QWebHistoryItem> QWebHistory::items() const
292 const WebCore::HistoryItemVector &items = d->lst->entries();
294 QList<QWebHistoryItem> ret;
295 for (unsigned i = 0; i < items.size(); ++i) {
296 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
297 ret.append(QWebHistoryItem(priv));
303 Returns the list of items in the backwards history list.
304 At most \a maxItems entries are returned.
308 QList<QWebHistoryItem> QWebHistory::backItems(int maxItems) const
310 WebCore::HistoryItemVector items(maxItems);
311 d->lst->backListWithLimit(maxItems, items);
313 QList<QWebHistoryItem> ret;
314 for (unsigned i = 0; i < items.size(); ++i) {
315 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
316 ret.append(QWebHistoryItem(priv));
322 Returns the list of items in the forward history list.
323 At most \a maxItems entries are returned.
327 QList<QWebHistoryItem> QWebHistory::forwardItems(int maxItems) const
329 WebCore::HistoryItemVector items(maxItems);
330 d->lst->forwardListWithLimit(maxItems, items);
332 QList<QWebHistoryItem> ret;
333 for (unsigned i = 0; i < items.size(); ++i) {
334 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
335 ret.append(QWebHistoryItem(priv));
341 Returns true if there is an item preceding the current item in the history;
342 otherwise returns false.
346 bool QWebHistory::canGoBack() const
348 return d->lst->backListCount() > 0;
352 Returns true if we have an item to go forward to; otherwise returns false.
356 bool QWebHistory::canGoForward() const
358 return d->lst->forwardListCount() > 0;
362 Set the current item to be the previous item in the history and goes to the
363 corresponding page; i.e., goes back one history item.
365 \sa forward(), goToItem()
367 void QWebHistory::back()
370 WebCore::Page* page = static_cast<WebCore::BackForwardListImpl*>(d->lst)->page();
371 page->goToItem(d->lst->backItem(), WebCore::FrameLoadTypeIndexedBackForward);
376 Sets the current item to be the next item in the history and goes to the
377 corresponding page; i.e., goes forward one history item.
379 \sa back(), goToItem()
381 void QWebHistory::forward()
383 if (canGoForward()) {
384 WebCore::Page* page = static_cast<WebCore::BackForwardListImpl*>(d->lst)->page();
385 page->goToItem(d->lst->forwardItem(), WebCore::FrameLoadTypeIndexedBackForward);
390 Sets the current item to be the specified \a item in the history and goes to the page.
392 \sa back(), forward()
394 void QWebHistory::goToItem(const QWebHistoryItem &item)
396 WebCore::Page* page = static_cast<WebCore::BackForwardListImpl*>(d->lst)->page();
397 page->goToItem(item.d->item, WebCore::FrameLoadTypeIndexedBackForward);
401 Returns the item before the current item in the history.
403 QWebHistoryItem QWebHistory::backItem() const
405 WebCore::HistoryItem *i = d->lst->backItem();
406 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
407 return QWebHistoryItem(priv);
411 Returns the current item in the history.
413 QWebHistoryItem QWebHistory::currentItem() const
415 WebCore::HistoryItem *i = d->lst->currentItem();
416 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
417 return QWebHistoryItem(priv);
421 Returns the item after the current item in the history.
423 QWebHistoryItem QWebHistory::forwardItem() const
425 WebCore::HistoryItem *i = d->lst->forwardItem();
426 QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
427 return QWebHistoryItem(priv);
432 Returns the index of the current item in history.
434 int QWebHistory::currentItemIndex() const
436 return d->lst->backListCount();
440 Returns the item at index \a i in the history.
442 QWebHistoryItem QWebHistory::itemAt(int i) const
444 QWebHistoryItemPrivate *priv;
445 if (i < 0 || i >= count())
446 priv = new QWebHistoryItemPrivate(0);
448 WebCore::HistoryItem *item = d->lst->entries()[i].get();
449 priv = new QWebHistoryItemPrivate(item);
451 return QWebHistoryItem(priv);
455 Returns the total number of items in the history.
457 int QWebHistory::count() const
459 return d->lst->entries().size();
464 Returns the maximum number of items in the history.
466 \sa setMaximumItemCount()
468 int QWebHistory::maximumItemCount() const
470 return d->lst->capacity();
475 Sets the maximum number of items in the history to \a count.
477 \sa maximumItemCount()
479 void QWebHistory::setMaximumItemCount(int count)
481 d->lst->setCapacity(count);
486 \fn QDataStream& operator<<(QDataStream& stream, const QWebHistory& history)
489 \brief The operator<< function streams a history into a data stream.
491 It saves the \a history into the specified \a stream.
494 QDataStream& operator<<(QDataStream& target, const QWebHistory& history)
496 QWebHistoryPrivate* d = history.d;
498 int version = DefaultHistoryVersion;
501 target << history.count() << history.currentItemIndex();
503 const WebCore::HistoryItemVector &items = d->lst->entries();
504 for (unsigned i = 0; i < items.size(); i++)
505 items[i].get()->saveState(target, version);
511 \fn QDataStream& operator>>(QDataStream& stream, QWebHistory& history)
515 \brief The operator>> function loads a history from a data stream.
517 Loads a QWebHistory from the specified \a stream into the given \a history.
520 QDataStream& operator>>(QDataStream& source, QWebHistory& history)
522 QWebHistoryPrivate* d = history.d;
531 source >> count >> currentIndex;
534 // only if there are elements
536 // after clear() is new clear HistoryItem (at the end we had to remove it)
537 WebCore::HistoryItem* nullItem = d->lst->currentItem();
538 for (int i = 0; i < count; i++) {
539 WTF::PassRefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create();
540 item->restoreState(source, version);
541 d->lst->addItem(item);
543 d->lst->removeItem(nullItem);
544 // Update the HistoryController.
545 static_cast<WebCore::BackForwardListImpl*>(history.d->lst)->page()->mainFrame()->loader()->history()->setCurrentItem(history.d->lst->entries()[currentIndex].get());
546 history.goToItem(history.itemAt(currentIndex));
550 d->page()->updateNavigationActions();
555 QWebPagePrivate* QWebHistoryPrivate::page()
557 return QWebFramePrivate::kit(static_cast<WebCore::BackForwardListImpl*>(lst)->page()->mainFrame())->page()->handle();
560 WebCore::HistoryItem* QWebHistoryItemPrivate::core(const QWebHistoryItem* q)