1/*
2 * Copyright (C) 2010 Juha Savolainen (juha.savolainen@weego.fi)
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *  notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24
25#include "config.h"
26#include "qwkhistory.h"
27
28#include <QSharedData>
29#include <QString>
30#include <QUrl>
31#include "qwkhistory_p.h"
32#include "WebBackForwardList.h"
33#include <WebKit2/WKArray.h>
34#include <WebKit2/WKRetainPtr.h>
35#include "WKBackForwardList.h"
36#include "WKStringQt.h"
37#include "WKURL.h"
38#include "WKURLQt.h"
39
40using namespace WebKit;
41
42QWKHistoryItemPrivate::QWKHistoryItemPrivate(WKBackForwardListItemRef listItem)
43    : m_backForwardListItem(listItem)
44{
45}
46
47QWKHistoryItemPrivate::~QWKHistoryItemPrivate()
48{
49}
50
51QWKHistoryItem::QWKHistoryItem(const QWKHistoryItem& other)
52    : d(other.d)
53{
54}
55
56QWKHistoryItem& QWKHistoryItem::QWKHistoryItem::operator=(const QWKHistoryItem& other)
57{
58    d = other.d;
59    return *this;
60}
61
62QWKHistoryItem::QWKHistoryItem(WKBackForwardListItemRef item)
63    : d(new QWKHistoryItemPrivate(item))
64{
65}
66
67QWKHistoryItem::~QWKHistoryItem()
68{
69}
70
71QString QWKHistoryItem::title() const
72{
73    if (!d->m_backForwardListItem)
74        return QString();
75    WKRetainPtr<WKStringRef> title = WKBackForwardListItemCopyTitle(d->m_backForwardListItem.get());
76    return WKStringCopyQString(title.get());
77}
78
79QUrl QWKHistoryItem::url() const
80{
81    if (!d->m_backForwardListItem)
82        return QUrl();
83    WKRetainPtr<WKURLRef> url = WKBackForwardListItemCopyURL(d->m_backForwardListItem.get());
84    return WKURLCopyQUrl(url.get());
85}
86
87QWKHistoryPrivate::QWKHistoryPrivate(WebKit::WebBackForwardList* list)
88    : m_backForwardList(list)
89{
90}
91
92QWKHistory* QWKHistoryPrivate::createHistory(WebKit::WebBackForwardList* list)
93{
94    QWKHistory* history = new QWKHistory();
95    history->d = new QWKHistoryPrivate(list);
96    return history;
97}
98
99QWKHistoryPrivate::~QWKHistoryPrivate()
100{
101}
102
103QWKHistory::QWKHistory()
104{
105}
106
107QWKHistory::~QWKHistory()
108{
109    delete d;
110}
111
112int QWKHistory::backListCount() const
113{
114    return WKBackForwardListGetBackListCount(toAPI(d->m_backForwardList));
115}
116
117int QWKHistory::forwardListCount() const
118{
119    return WKBackForwardListGetForwardListCount(toAPI(d->m_backForwardList));
120}
121
122int QWKHistory::count() const
123{
124    return backListCount() + forwardListCount();
125}
126
127QWKHistoryItem QWKHistory::currentItem() const
128{
129    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetCurrentItem(toAPI(d->m_backForwardList));
130    QWKHistoryItem item(itemRef.get());
131    return item;
132}
133
134QWKHistoryItem QWKHistory::backItem() const
135{
136    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetBackItem(toAPI(d->m_backForwardList));
137    QWKHistoryItem item(itemRef.get());
138    return item;
139}
140
141QWKHistoryItem QWKHistory::forwardItem() const
142{
143    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetForwardItem(toAPI(d->m_backForwardList));
144    QWKHistoryItem item(itemRef.get());
145    return item;
146}
147
148QWKHistoryItem QWKHistory::itemAt(int index) const
149{
150    WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetItemAtIndex(toAPI(d->m_backForwardList), index);
151    QWKHistoryItem item(itemRef.get());
152    return item;
153}
154
155QList<QWKHistoryItem> QWKHistory::backItems(int maxItems) const
156{
157    WKArrayRef arrayRef = WKBackForwardListCopyBackListWithLimit(toAPI(d->m_backForwardList), maxItems);
158    int size = WKArrayGetSize(arrayRef);
159    QList<QWKHistoryItem> itemList;
160    for (int i = 0; i < size; ++i) {
161        WKTypeRef wkHistoryItem = WKArrayGetItemAtIndex(arrayRef, i);
162        WKBackForwardListItemRef itemRef = static_cast<WKBackForwardListItemRef>(wkHistoryItem);
163        QWKHistoryItem item(itemRef);
164        itemList.append(item);
165    }
166    return itemList;
167}
168
169QList<QWKHistoryItem> QWKHistory::forwardItems(int maxItems) const
170{
171    WKArrayRef arrayRef = WKBackForwardListCopyForwardListWithLimit(toAPI(d->m_backForwardList), maxItems);
172    int size = WKArrayGetSize(arrayRef);
173    QList<QWKHistoryItem> itemList;
174    for (int i = 0; i < size; ++i) {
175        WKTypeRef wkHistoryItem = WKArrayGetItemAtIndex(arrayRef, i);
176        WKBackForwardListItemRef itemRef = static_cast<WKBackForwardListItemRef>(wkHistoryItem);
177        QWKHistoryItem item(itemRef);
178        itemList.append(item);
179    }
180    return itemList;
181}
182
183