1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/*
Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "qwebhistory.h"
#include "qwebhistory_p.h"
#include "qwebframe_p.h"
#include "PlatformString.h"
#include "Image.h"
#include "KURL.h"
#include "Page.h"
#include "PageGroup.h"
#include <QSharedData>
#include <QDebug>
/*!
\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.
The following table shows the properties of the page held by the history item, and
the functions used to access them.
\table
\header \o Function \o Description
\row \o title() \o The page title.
\row \o url() \o The location of the page.
\row \o originalUrl() \o The URL used to access the page.
\row \o lastVisited() \o The date and time of the user's last visit to the page.
\row \o icon() \o The icon associated with the page that was provided by the server.
\row \o userData() \o The user specific data that was stored with the history item.
\endtable
\note QWebHistoryItem objects are value based, but \e{explicitly shared}. Changing
a QWebHistoryItem instance by calling setUserData() will change all copies of that
instance.
\sa QWebHistory, QWebPage::history(), QWebHistoryInterface
*/
/*!
Constructs a history item from \a other. The new item and \a other
will share their data, and modifying either this item or \a other will
modify both instances.
*/
QWebHistoryItem::QWebHistoryItem(const QWebHistoryItem &other)
: d(other.d)
{
}
/*!
Assigns the \a other history item to this. This item and \a other
will share their data, and modifying either this item or \a other will
modify both instances.
*/
QWebHistoryItem &QWebHistoryItem::operator=(const QWebHistoryItem &other)
{
d = other.d;
return *this;
}
/*!
Destroys the history item.
*/
QWebHistoryItem::~QWebHistoryItem()
{
}
/*!
Returns the original URL associated with the history item.
\sa url()
*/
QUrl QWebHistoryItem::originalUrl() const
{
if (d->item)
return QUrl(d->item->originalURL().string());
return QUrl();
}
/*!
Returns the URL associated with the history item.
\sa originalUrl(), title(), lastVisited()
*/
QUrl QWebHistoryItem::url() const
{
if (d->item)
return QUrl(d->item->url().string());
return QUrl();
}
/*!
Returns the title of the page associated with the history item.
\sa icon(), url(), lastVisited()
*/
QString QWebHistoryItem::title() const
{
if (d->item)
return d->item->title();
return QString();
}
/*!
Returns the date and time that the page associated with the item was last visited.
\sa title(), icon(), url()
*/
QDateTime QWebHistoryItem::lastVisited() const
{
//FIXME : this will be wrong unless we correctly set lastVisitedTime ourselves
if (d->item)
return QDateTime::fromTime_t((uint)d->item->lastVisitedTime());
return QDateTime();
}
/*!
Returns the icon associated with the history item.
\sa title(), url(), lastVisited()
*/
QIcon QWebHistoryItem::icon() const
{
if (d->item)
return *d->item->icon()->nativeImageForCurrentFrame();
return QIcon();
}
/*!
\since 4.5
Returns the user specific data that was stored with the history item.
\sa setUserData()
*/
QVariant QWebHistoryItem::userData() const
{
if (d->item)
return d->item->userData();
return QVariant();
}
/*!
\since 4.5
Stores user specific data \a userData with the history item.
\note All copies of this item will be modified.
\sa userData()
*/
void QWebHistoryItem::setUserData(const QVariant& userData)
{
if (d->item)
d->item->setUserData(userData);
}
/*!*
\internal
*/
QWebHistoryItem::QWebHistoryItem(QWebHistoryItemPrivate *priv)
{
d = priv;
}
/*!
\since 4.5
Returns whether this is a valid history item.
*/
bool QWebHistoryItem::isValid() const
{
return d->item;
}
/*!
\class QWebHistory
\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.
The history uses the concept of a \e{current item}, dividing the pages visited
into those that can be visited by navigating \e back and \e forward using the
back() and forward() functions. The current item can be obtained by calling
currentItem(), and an arbitrary item in the history can be made the current
item by passing it to goToItem().
A list of items describing the pages that can be visited by going back can be
obtained by calling the backItems() function; similarly, items describing the
pages ahead of the current page can be obtained with the forwardItems() function.
The total list of items is obtained with the items() function.
Just as with containers, functions are available to examine the history in terms
of a list. Arbitrary items in the history can be obtained with itemAt(), the total
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().
\sa QWebHistoryItem, QWebHistoryInterface, QWebPage
*/
QWebHistory::QWebHistory()
: d(0)
{
}
QWebHistory::~QWebHistory()
{
delete d;
}
/*!
Clears the history.
\sa count(), items()
*/
void QWebHistory::clear()
{
//shortcut to private BackForwardList
WebCore::BackForwardList* lst = d->lst;
//clear visited links
WebCore::Page* page = lst->page();
if (page && page->groupPtr())
page->groupPtr()->removeVisitedLinks();
//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();
}
/*!
Returns a list of all items currently in the history.
\sa count(), clear()
*/
QList<QWebHistoryItem> QWebHistory::items() const
{
const WebCore::HistoryItemVector &items = d->lst->entries();
QList<QWebHistoryItem> ret;
for (unsigned i = 0; i < items.size(); ++i) {
QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
ret.append(QWebHistoryItem(priv));
}
return ret;
}
/*!
Returns the list of items in the backwards history list.
At most \a maxItems entries are returned.
\sa forwardItems()
*/
QList<QWebHistoryItem> QWebHistory::backItems(int maxItems) const
{
WebCore::HistoryItemVector items(maxItems);
d->lst->backListWithLimit(maxItems, items);
QList<QWebHistoryItem> ret;
for (unsigned i = 0; i < items.size(); ++i) {
QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
ret.append(QWebHistoryItem(priv));
}
return ret;
}
/*!
Returns the list of items in the forward history list.
At most \a maxItems entries are returned.
\sa backItems()
*/
QList<QWebHistoryItem> QWebHistory::forwardItems(int maxItems) const
{
WebCore::HistoryItemVector items(maxItems);
d->lst->forwardListWithLimit(maxItems, items);
QList<QWebHistoryItem> ret;
for (unsigned i = 0; i < items.size(); ++i) {
QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(items[i].get());
ret.append(QWebHistoryItem(priv));
}
return ret;
}
/*!
Returns true if there is an item preceding the current item in the history;
otherwise returns false.
\sa canGoForward()
*/
bool QWebHistory::canGoBack() const
{
return d->lst->backListCount() > 0;
}
/*!
Returns true if we have an item to go forward to; otherwise returns false.
\sa canGoBack()
*/
bool QWebHistory::canGoForward() const
{
return d->lst->forwardListCount() > 0;
}
/*!
Set the current item to be the previous item in the history and goes to the
corresponding page; i.e., goes back one history item.
\sa forward(), goToItem()
*/
void QWebHistory::back()
{
if (canGoBack()) {
d->lst->goBack();
WebCore::Page* page = d->lst->page();
page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward);
}
}
/*!
Sets the current item to be the next item in the history and goes to the
corresponding page; i.e., goes forward one history item.
\sa back(), goToItem()
*/
void QWebHistory::forward()
{
if (canGoForward()) {
d->lst->goForward();
WebCore::Page* page = d->lst->page();
page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward);
}
}
/*!
Sets the current item to be the specified \a item in the history and goes to the page.
\sa back(), forward()
*/
void QWebHistory::goToItem(const QWebHistoryItem &item)
{
d->lst->goToItem(item.d->item);
WebCore::Page* page = d->lst->page();
page->goToItem(currentItem().d->item, WebCore::FrameLoadTypeIndexedBackForward);
}
/*!
Returns the item before the current item in the history.
*/
QWebHistoryItem QWebHistory::backItem() const
{
WebCore::HistoryItem *i = d->lst->backItem();
QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
return QWebHistoryItem(priv);
}
/*!
Returns the current item in the history.
*/
QWebHistoryItem QWebHistory::currentItem() const
{
WebCore::HistoryItem *i = d->lst->currentItem();
QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
return QWebHistoryItem(priv);
}
/*!
Returns the item after the current item in the history.
*/
QWebHistoryItem QWebHistory::forwardItem() const
{
WebCore::HistoryItem *i = d->lst->forwardItem();
QWebHistoryItemPrivate *priv = new QWebHistoryItemPrivate(i);
return QWebHistoryItem(priv);
}
/*!
\since 4.5
Returns the index of the current item in history.
*/
int QWebHistory::currentItemIndex() const
{
return d->lst->backListCount();
}
/*!
Returns the item at index \a i in the history.
*/
QWebHistoryItem QWebHistory::itemAt(int i) const
{
QWebHistoryItemPrivate *priv;
if (i < 0 || i >= count())
priv = new QWebHistoryItemPrivate(0);
else {
WebCore::HistoryItem *item = d->lst->entries()[i].get();
priv = new QWebHistoryItemPrivate(item);
}
return QWebHistoryItem(priv);
}
/*!
Returns the total number of items in the history.
*/
int QWebHistory::count() const
{
return d->lst->entries().size();
}
/*!
\since 4.5
Returns the maximum number of items in the history.
\sa setMaximumItemCount()
*/
int QWebHistory::maximumItemCount() const
{
return d->lst->capacity();
}
/*!
\since 4.5
Sets the maximum number of items in the history to \a count.
\sa maximumItemCount()
*/
void QWebHistory::setMaximumItemCount(int count)
{
d->lst->setCapacity(count);
}
/*!
\enum QWebHistory::HistoryStateVersion
This enum describes the versions available for QWebHistory's saveState() function:
\value HistoryVersion_1 Version 1 (Qt 4.6)
\value DefaultHistoryVersion The current default version in 1.
*/
/*!
\since 4.6
Restores the state of QWebHistory from the given \a buffer. Returns true
if the history was successfully restored; otherwise returns false.
\sa saveState()
*/
bool QWebHistory::restoreState(const QByteArray& buffer)
{
QDataStream stream(buffer);
int version;
bool result = false;
stream >> version;
switch (version) {
case HistoryVersion_1: {
int count;
int currentIndex;
stream >> count >> currentIndex;
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++) {
WTF::PassRefPtr<WebCore::HistoryItem> item = WebCore::HistoryItem::create();
item->restoreState(stream, version);
d->lst->addItem(item);
}
d->lst->removeItem(nullItem);
goToItem(itemAt(currentIndex));
result = stream.status() == QDataStream::Ok;
}
break;
}
default: {} // result is false;
}
d->page()->updateNavigationActions();
return result;
};
/*!
\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 (unsigned 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
\brief The operator<< function streams a history into a data stream.
It saves the \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();
}
/*!
\fn QDataStream& operator>>(QDataStream& stream, QWebHistory& history)
\relates QWebHistory
\since 4.6
\brief The operator>> function loads a history from a data stream.
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)
{
QByteArray buffer;
stream >> buffer;
history.restoreState(buffer);
return stream;
}
QWebPagePrivate* QWebHistoryPrivate::page()
{
return QWebFramePrivate::kit(lst->page()->mainFrame())->page()->handle();
}
|