initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / qt / qwkhistory.cpp
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 "QtWebPageProxy.h"
32 #include "qwkhistory_p.h"
33 #include "WebBackForwardList.h"
34 #include <WebKit2/WKArray.h>
35 #include <WebKit2/WKRetainPtr.h>
36 #include "WKBackForwardList.h"
37 #include "WKStringQt.h"
38 #include "WKURL.h"
39 #include "WKURLQt.h"
40
41 using namespace WebKit;
42
43 QWKHistoryItemPrivate::QWKHistoryItemPrivate(WKBackForwardListItemRef listItem)
44     : m_backForwardListItem(listItem)
45 {
46 }
47
48 QWKHistoryItemPrivate::~QWKHistoryItemPrivate()
49 {
50 }
51
52 QWKHistoryItem::QWKHistoryItem(const QWKHistoryItem& other)
53     : d(other.d) 
54 {
55 }
56
57 QWKHistoryItem& QWKHistoryItem::QWKHistoryItem::operator=(const QWKHistoryItem& other) 
58
59     d = other.d;
60     return *this; 
61 }
62
63 QWKHistoryItem::QWKHistoryItem(WKBackForwardListItemRef item)
64     : d(new QWKHistoryItemPrivate(item))
65 {
66 }
67
68 QWKHistoryItem::~QWKHistoryItem()
69 {
70 }
71
72 QString QWKHistoryItem::title() const
73 {
74     if (!d->m_backForwardListItem)
75         return QString();
76     WKRetainPtr<WKStringRef> title(AdoptWK, WKBackForwardListItemCopyTitle(d->m_backForwardListItem.get()));
77     return WKStringCopyQString(title.get());
78 }
79
80 QUrl QWKHistoryItem::url() const
81 {
82     if (!d->m_backForwardListItem)
83         return QUrl();
84     WKRetainPtr<WKURLRef> url(AdoptWK, WKBackForwardListItemCopyURL(d->m_backForwardListItem.get()));
85     return WKURLCopyQUrl(url.get());
86 }
87
88 QWKHistoryPrivate::QWKHistoryPrivate(QtWebPageProxy* page, WebKit::WebBackForwardList* list)
89     : m_page(page)
90     , m_backForwardList(list)
91 {
92 }
93
94 QWKHistory* QWKHistoryPrivate::createHistory(QtWebPageProxy* page, WebKit::WebBackForwardList* list)
95 {
96     QWKHistory* history = new QWKHistory();
97     history->d = new QWKHistoryPrivate(page, list);
98     return history;
99 }
100
101 QWKHistoryPrivate::~QWKHistoryPrivate()
102 {
103 }
104
105 QWKHistory::QWKHistory()
106 {
107 }
108
109 QWKHistory::~QWKHistory()
110 {
111     delete d;
112 }
113
114 int QWKHistory::backListCount() const
115 {
116     return WKBackForwardListGetBackListCount(toAPI(d->m_backForwardList));
117 }
118
119 int QWKHistory::forwardListCount() const
120 {
121     return WKBackForwardListGetForwardListCount(toAPI(d->m_backForwardList));
122 }
123
124 int QWKHistory::count() const
125 {
126     return backListCount() + forwardListCount();
127 }
128
129 QWKHistoryItem QWKHistory::currentItem() const
130 {
131     WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetCurrentItem(toAPI(d->m_backForwardList));
132     QWKHistoryItem item(itemRef.get());
133     return item;
134 }
135
136 QWKHistoryItem QWKHistory::backItem() const
137 {
138     WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetBackItem(toAPI(d->m_backForwardList));
139     QWKHistoryItem item(itemRef.get());
140     return item;
141 }
142
143 QWKHistoryItem QWKHistory::forwardItem() const
144 {
145     WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetForwardItem(toAPI(d->m_backForwardList));
146     QWKHistoryItem item(itemRef.get());
147     return item;
148 }
149
150 QWKHistoryItem QWKHistory::itemAt(int index) const
151 {
152     WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetItemAtIndex(toAPI(d->m_backForwardList), index);
153     QWKHistoryItem item(itemRef.get());
154     return item;
155 }
156
157 void QWKHistory::goToItemAt(int index) const
158 {
159     WKRetainPtr<WKBackForwardListItemRef> itemRef = WKBackForwardListGetItemAtIndex(toAPI(d->m_backForwardList), index);
160     if (itemRef && d->m_page) {
161         QWKHistoryItem item(itemRef.get());
162         WKPageGoToBackForwardListItem(d->m_page->pageRef(), item.d->m_backForwardListItem.get());
163     }
164 }
165
166 QList<QWKHistoryItem> QWKHistory::backItems(int maxItems) const
167 {
168     WKArrayRef arrayRef = WKBackForwardListCopyBackListWithLimit(toAPI(d->m_backForwardList), maxItems);
169     int size = WKArrayGetSize(arrayRef);
170     QList<QWKHistoryItem> itemList;
171     for (int i = 0; i < size; ++i) {
172         WKTypeRef wkHistoryItem = WKArrayGetItemAtIndex(arrayRef, i);
173         WKBackForwardListItemRef itemRef = static_cast<WKBackForwardListItemRef>(wkHistoryItem);
174         QWKHistoryItem item(itemRef);
175         itemList.append(item);
176     }
177     return itemList;
178 }
179
180 QList<QWKHistoryItem> QWKHistory::forwardItems(int maxItems) const
181 {
182     WKArrayRef arrayRef = WKBackForwardListCopyForwardListWithLimit(toAPI(d->m_backForwardList), maxItems);
183     int size = WKArrayGetSize(arrayRef);
184     QList<QWKHistoryItem> itemList;
185     for (int i = 0; i < size; ++i) {
186         WKTypeRef wkHistoryItem = WKArrayGetItemAtIndex(arrayRef, i);
187         WKBackForwardListItemRef itemRef = static_cast<WKBackForwardListItemRef>(wkHistoryItem);
188         QWKHistoryItem item(itemRef);
189         itemList.append(item);
190     }
191     return itemList;
192 }
193