diff options
Diffstat (limited to 'WebKit/qt/Api/qwebhistory.cpp')
| -rw-r--r-- | WebKit/qt/Api/qwebhistory.cpp | 196 |
1 files changed, 84 insertions, 112 deletions
diff --git a/WebKit/qt/Api/qwebhistory.cpp b/WebKit/qt/Api/qwebhistory.cpp index 1c1c72a..d852012 100644 --- a/WebKit/qt/Api/qwebhistory.cpp +++ b/WebKit/qt/Api/qwebhistory.cpp @@ -20,6 +20,7 @@ #include "config.h" #include "qwebhistory.h" #include "qwebhistory_p.h" +#include "qwebframe_p.h" #include "PlatformString.h" #include "Image.h" @@ -30,11 +31,18 @@ #include <QSharedData> #include <QDebug> +enum { + InitialHistoryVersion = 1, + DefaultHistoryVersion = InitialHistoryVersion +}; + /*! \class QWebHistoryItem \since 4.4 \brief The QWebHistoryItem class represents one item in the history of a QWebPage + \inmodule QtWebKit + Each QWebHistoryItem instance represents an entry in the history stack of a Web page, containing information about the page, its location, and when it was last visited. @@ -201,6 +209,8 @@ bool QWebHistoryItem::isValid() const \since 4.4 \brief The QWebHistory class represents the history of a QWebPage + \inmodule QtWebKit + Each QWebPage instance contains a history of visited pages that can be accessed by QWebPage::history(). QWebHistory represents this history and makes it possible to navigate it. @@ -221,7 +231,8 @@ bool QWebHistoryItem::isValid() const number of items is given by count(), and the history can be cleared with the clear() function. - QWebHistory's state can be saved with saveState() and loaded with restoreState(). + QWebHistory's state can be saved to a QDataStream using the >> operator and loaded + by using the << operator. \sa QWebHistoryItem, QWebHistoryInterface, QWebPage */ @@ -244,17 +255,27 @@ QWebHistory::~QWebHistory() */ void QWebHistory::clear() { - RefPtr<WebCore::HistoryItem> current = d->lst->currentItem(); - int capacity = d->lst->capacity(); - d->lst->setCapacity(0); + //shortcut to private BackForwardList + WebCore::BackForwardList* lst = d->lst; - WebCore::Page* page = d->lst->page(); + //clear visited links + WebCore::Page* page = lst->page(); if (page && page->groupPtr()) page->groupPtr()->removeVisitedLinks(); - d->lst->setCapacity(capacity); - d->lst->addItem(current.get()); - d->lst->goToItem(current.get()); + //if count() == 0 then just return + if (!lst->entries().size()) + return; + + RefPtr<WebCore::HistoryItem> current = lst->currentItem(); + int capacity = lst->capacity(); + lst->setCapacity(0); + + lst->setCapacity(capacity); //revert capacity + lst->addItem(current.get()); //insert old current item + lst->goToItem(current.get()); //and set it as current again + + d->page()->updateNavigationActions(); } /*! @@ -267,7 +288,7 @@ QList<QWebHistoryItem> QWebHistory::items() const const WebCore::HistoryItemVector &items = d->lst->entries(); QList<QWebHistoryItem> ret; - for (int i = 0; i < items.size(); ++i) { + for (unsigned i = 0; i < items.size(); ++i) { QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get()); ret.append(QWebHistoryItem(priv)); } @@ -286,7 +307,7 @@ QList<QWebHistoryItem> QWebHistory::backItems(int maxItems) const d->lst->backListWithLimit(maxItems, items); QList<QWebHistoryItem> ret; - for (int i = 0; i < items.size(); ++i) { + for (unsigned i = 0; i < items.size(); ++i) { QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get()); ret.append(QWebHistoryItem(priv)); } @@ -305,7 +326,7 @@ QList<QWebHistoryItem> QWebHistory::forwardItems(int maxItems) const d->lst->forwardListWithLimit(maxItems, items); QList<QWebHistoryItem> ret; - for (int i = 0; i < items.size(); ++i) { + for (unsigned i = 0; i < items.size(); ++i) { QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get()); ret.append(QWebHistoryItem(priv)); } @@ -341,9 +362,11 @@ bool QWebHistory::canGoForward() const */ void QWebHistory::back() { - d->lst->goBack(); - WebCore::Page* page = d->lst->page(); - page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward); + if (canGoBack()) { + d->lst->goBack(); + WebCore::Page* page = d->lst->page(); + page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward); + } } /*! @@ -354,9 +377,11 @@ void QWebHistory::back() */ void QWebHistory::forward() { - d->lst->goForward(); - WebCore::Page* page = d->lst->page(); - page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward); + if (canGoForward()) { + d->lst->goForward(); + WebCore::Page* page = d->lst->page(); + page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward); + } } /*! @@ -456,128 +481,75 @@ void QWebHistory::setMaximumItemCount(int count) } /*! - \enum QWebHistory::HistoryStateVersion + \since 4.6 + \fn QDataStream& operator<<(QDataStream& stream, const QWebHistory& history) + \relates QWebHistory - This enum describes the versions available for QWebHistory's saveState() function: + \brief The operator<< function streams a history into a data stream. - \value HistoryVersion_1 Version 1 (Qt 4.6) - \value DefaultHistoryVersion The current default version in 1. + It saves the \a history into the specified \a stream. */ +QDataStream& operator<<(QDataStream& target, const QWebHistory& history) +{ + QWebHistoryPrivate* d = history.d; + + int version = DefaultHistoryVersion; + + target << version; + target << history.count() << history.currentItemIndex(); + + const WebCore::HistoryItemVector &items = d->lst->entries(); + for (unsigned i = 0; i < items.size(); i++) + items[i].get()->saveState(target, version); + + return target; +} + /*! + \fn QDataStream& operator>>(QDataStream& stream, QWebHistory& history) + \relates QWebHistory \since 4.6 - Restores the state of QWebHistory from the given \a buffer. Returns true - if the history was successfully restored; otherwise returns false. + \brief The operator>> function loads a history from a data stream. - \sa saveState() + Loads a QWebHistory from the specified \a stream into the given \a history. */ -bool QWebHistory::restoreState(const QByteArray& buffer) + +QDataStream& operator>>(QDataStream& source, QWebHistory& history) { - QDataStream stream(buffer); + QWebHistoryPrivate* d = history.d; + int version; - bool result = false; - stream >> version; - switch (version) { - case HistoryVersion_1: { + source >> version; + + if (version == 1) { int count; int currentIndex; - stream >> count >> currentIndex; + source >> count >> currentIndex; - clear(); + history.clear(); // only if there are elements if (count) { // after clear() is new clear HistoryItem (at the end we had to remove it) - WebCore::HistoryItem *nullItem = d->lst->currentItem(); - for (int i = 0;i < count;i++) { + WebCore::HistoryItem* nullItem = d->lst->currentItem(); + for (int i = 0; i < count; i++) { WTF::PassRefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create(); - item->restoreState(stream, version); + item->restoreState(source, version); d->lst->addItem(item); } d->lst->removeItem(nullItem); - goToItem(itemAt(currentIndex)); - result = stream.status() == QDataStream::Ok; + history.goToItem(history.itemAt(currentIndex)); } - break; - } - default: {} // result is false; } - return result; -}; + d->page()->updateNavigationActions(); -/*! - \since 4.6 - Saves the state of this QWebHistory into a QByteArray. - - Saves the current state of this QWebHistory. The version number, \a version, is - stored as part of the data. - - To restore the saved state, pass the return value to restoreState(). - - \sa restoreState() -*/ -QByteArray QWebHistory::saveState(HistoryStateVersion version) const -{ - QByteArray buffer; - QDataStream stream(&buffer, QIODevice::WriteOnly); - stream << version; - - switch (version) { - case HistoryVersion_1: { - stream << count() << currentItemIndex(); - - const WebCore::HistoryItemVector &items = d->lst->entries(); - for (int i = 0; i < items.size(); i++) - items[i].get()->saveState(stream, version); - - if (stream.status() != QDataStream::Ok) - buffer = QByteArray(); // make buffer isNull()==true and isEmpty()==true - break; - } - default: - buffer.clear(); - - } - - return buffer; -} - -/*! - \since 4.6 - \fn QDataStream& operator<<(QDataStream& stream, const QWebHistory& history) - \relates QWebHistory - - Saves the given \a history into the specified \a stream. This is a convenience function - and is equivalent to calling the saveState() method. - - \sa QWebHistory::saveState() -*/ - -QDataStream& operator<<(QDataStream& stream, const QWebHistory& history) -{ - return stream << history.saveState(); + return source; } -/*! - \fn QDataStream& operator>>(QDataStream& stream, QWebHistory& history) - \relates QWebHistory - \since 4.6 - - Loads a QWebHistory from the specified \a stream into the given \a history. - This is a convenience function and it is equivalent to calling the restoreState() - method. - - \sa QWebHistory::restoreState() -*/ - -QDataStream& operator>>(QDataStream& stream, QWebHistory& history) +QWebPagePrivate* QWebHistoryPrivate::page() { - QByteArray buffer; - stream >> buffer; - history.restoreState(buffer); - return stream; + return QWebFramePrivate::kit(lst->page()->mainFrame())->page()->handle(); } - - |
