1/*
2 * Copyright (C) 2010 Juha Savolainen (juha.savolainen@weego.fi)
3 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1.  Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *  notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*/
25
26#include "config.h"
27#include "qwebnavigationhistory_p.h"
28
29#include "WKBackForwardList.h"
30#include "WKStringQt.h"
31#include "WKURL.h"
32#include "WKURLQt.h"
33
34#include "qwebnavigationhistory_p_p.h"
35#include <QString>
36#include <QUrl>
37#include <QtQml/QQmlEngine>
38#include <WebKit2/WKArray.h>
39#include <WebKit2/WKBackForwardListItem.h>
40#include <WebKit2/WKBase.h>
41#include <WebKit2/WKPage.h>
42#include <WebKit2/WKRetainPtr.h>
43#include <wtf/PassOwnPtr.h>
44
45using namespace WebKit;
46
47QWebNavigationListModelPrivate::QWebNavigationListModelPrivate(WKBackForwardListRef list)
48    : m_backForwardList(list)
49    , indexSign(0)
50{
51}
52
53QWebNavigationListModel* QWebNavigationListModelPrivate::createWebNavigationModel(WKBackForwardListRef list)
54{
55    QWebNavigationListModel* model = new QWebNavigationListModel();
56    model->d = new QWebNavigationListModelPrivate(list);
57    return model;
58}
59
60
61QWebNavigationHistoryPrivate::QWebNavigationHistoryPrivate(WKPageRef page)
62    : m_page(page)
63    , m_backForwardList(WKPageGetBackForwardList(page))
64    , m_backNavigationModel(adoptPtr(QWebNavigationListModelPrivate::createWebNavigationModel(m_backForwardList.get())))
65    , m_forwardNavigationModel(adoptPtr(QWebNavigationListModelPrivate::createWebNavigationModel(m_backForwardList.get())))
66{
67    m_backNavigationModel->d->count = &WKBackForwardListGetBackListCount;
68    m_backNavigationModel->d->indexSign = -1;
69    m_forwardNavigationModel->d->count = &WKBackForwardListGetForwardListCount;
70    m_forwardNavigationModel->d->indexSign = 1;
71}
72
73QWebNavigationHistory* QWebNavigationHistoryPrivate::createHistory(WKPageRef page)
74{
75    QWebNavigationHistory* history = new QWebNavigationHistory();
76    history->d = new QWebNavigationHistoryPrivate(page);
77    return history;
78}
79
80void QWebNavigationHistoryPrivate::reset()
81{
82    m_backNavigationModel->reset();
83    m_forwardNavigationModel->reset();
84}
85
86void QWebNavigationHistoryPrivate::goBackTo(int index)
87{
88    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetItemAtIndex(m_backForwardList.get(), -(index + 1));
89    if (itemRef && m_page)
90        WKPageGoToBackForwardListItem(m_page.get(), itemRef.get());
91}
92
93void QWebNavigationHistoryPrivate::goForwardTo(int index)
94{
95    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetItemAtIndex(m_backForwardList.get(), index + 1);
96    if (itemRef && m_page)
97        WKPageGoToBackForwardListItem(m_page.get(), itemRef.get());
98}
99
100QHash<int, QByteArray> QWebNavigationListModel::roleNames() const
101{
102    QHash<int, QByteArray> roles;
103    roles[QWebNavigationHistory::UrlRole] = "url";
104    roles[QWebNavigationHistory::TitleRole] = "title";
105    return roles;
106}
107
108QWebNavigationListModel::~QWebNavigationListModel()
109{
110    delete d;
111}
112
113int QWebNavigationListModel::rowCount(const QModelIndex&) const
114{
115    return d->count(d->m_backForwardList.get());
116}
117
118QVariant QWebNavigationListModel::data(const QModelIndex& index, int role) const
119{
120    if (!index.isValid())
121        return QVariant();
122
123    if (role < QWebNavigationHistory::UrlRole || role > QWebNavigationHistory::TitleRole)
124        return QVariant();
125
126    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetItemAtIndex(d->m_backForwardList.get(), (index.row() + 1) * d->indexSign);
127    if (role == QWebNavigationHistory::UrlRole) {
128        WKRetainPtr<WKURLRef> url(AdoptWK, WKBackForwardListItemCopyURL(itemRef.get()));
129        return WKURLCopyQUrl(url.get());
130    }
131
132    if (role == QWebNavigationHistory::TitleRole) {
133        WKRetainPtr<WKStringRef> title(AdoptWK, WKBackForwardListItemCopyTitle(itemRef.get()));
134        return WKStringCopyQString(title.get());
135    }
136
137    return QVariant();
138}
139
140void QWebNavigationListModel::reset()
141{
142    beginResetModel();
143    endResetModel();
144}
145
146QWebNavigationHistory::QWebNavigationHistory()
147    : QObject()
148{
149}
150
151QWebNavigationHistory::~QWebNavigationHistory()
152{
153    delete d;
154}
155
156QWebNavigationListModel* QWebNavigationHistory::backItems() const
157{
158    return d->m_backNavigationModel.get();
159}
160
161QWebNavigationListModel* QWebNavigationHistory::forwardItems() const
162{
163    return d->m_forwardNavigationModel.get();
164}
165
166#include "moc_qwebnavigationhistory_p.cpp"
167