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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
/* This file is part of the KDE project
Copyright (C) 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "konq_historymgr.h"
#include <dcopclient.h>
#include <kapplication.h>
#include <kdebug.h>
#include <ksavefile.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <zlib.h>
#include "konqbookmarkmanager.h"
const TQ_UINT32 KonqHistoryManager::s_historyVersion = 3;
KonqHistoryManager::KonqHistoryManager( TQObject *parent, const char *name )
: KParts::HistoryProvider( parent, name ),
KonqHistoryComm( "KonqHistoryManager" )
{
m_updateTimer = new TQTimer( this );
// defaults
KConfig *config = KGlobal::config();
KConfigGroupSaver cs( config, "HistorySettings" );
m_maxCount = config->readNumEntry( "Maximum of History entries", 500 );
m_maxCount = TQMAX( 1, m_maxCount );
m_maxAgeDays = config->readNumEntry( "Maximum age of History entries", 90);
m_history.setAutoDelete( true );
m_filename = locateLocal( "data",
TQString::tqfromLatin1("konqueror/konq_history" ));
if ( !kapp->dcopClient()->isAttached() )
kapp->dcopClient()->attach();
// take care of the completion object
m_pCompletion = new KCompletion;
m_pCompletion->setOrder( KCompletion::Weighted );
// and load the history
loadHistory();
connect( m_updateTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotEmitUpdated() ));
}
KonqHistoryManager::~KonqHistoryManager()
{
delete m_pCompletion;
clearPending();
}
bool KonqHistoryManager::isSenderOfBroadcast()
{
DCOPClient *dc = callingDcopClient();
return !dc || (dc->senderId() == dc->appId());
}
// loads the entire history
bool KonqHistoryManager::loadHistory()
{
clearPending();
m_history.clear();
m_pCompletion->clear();
TQFile file( m_filename );
if ( !file.open( IO_ReadOnly ) ) {
if ( file.exists() )
kdWarning() << "Can't open " << file.name() << endl;
// try to load the old completion history
bool ret = loadFallback();
emit loadingFinished();
return ret;
}
TQDataStream fileStream( &file );
TQByteArray data; // only used for version == 2
// we construct the stream object now but fill in the data later.
// thanks to QBA's explicit sharing this works :)
TQDataStream crcStream( data, IO_ReadOnly );
if ( !fileStream.atEnd() ) {
TQ_UINT32 version;
fileStream >> version;
TQDataStream *stream = &fileStream;
bool crcChecked = false;
bool crcOk = false;
if ( version == 2 || version == 3) {
TQ_UINT32 crc;
crcChecked = true;
fileStream >> crc >> data;
crcOk = crc32( 0, reinterpret_cast<unsigned char *>( data.data() ), data.size() ) == crc;
stream = &crcStream; // pick up the right stream
}
if ( version == 3 )
{
//Use KURL marshalling for V3 format.
KonqHistoryEntry::marshalURLAsStrings = false;
}
if ( version != 0 && version < 3 ) //Versions 1,2 (but not 0) are also valid
{
//Turn on backwards compatibility mode..
KonqHistoryEntry::marshalURLAsStrings = true;
// it doesn't make sense to save to save maxAge and maxCount in the
// binary file, this would make backups impossible (they would clear
// themselves on startup, because all entries expire).
// [But V1 and V2 formats did it, so we do a dummy read]
TQ_UINT32 dummy;
*stream >> dummy;
*stream >> dummy;
//OK.
version = 3;
}
if ( s_historyVersion != version || ( crcChecked && !crcOk ) ) {
kdWarning() << "The history version doesn't match, aborting loading" << endl;
file.close();
emit loadingFinished();
return false;
}
while ( !stream->atEnd() ) {
KonqHistoryEntry *entry = new KonqHistoryEntry;
Q_CHECK_PTR( entry );
*stream >> *entry;
// kdDebug(1203) << "## loaded entry: " << entry->url << ", Title: " << entry->title << endl;
m_history.append( entry );
TQString urlString2 = entry->url.prettyURL();
addToCompletion( urlString2, entry->typedURL, entry->numberOfTimesVisited );
// and fill our baseclass.
TQString urlString = entry->url.url();
KParts::HistoryProvider::insert( urlString );
// DF: also insert the "pretty" version if different
// This helps getting 'visited' links on websites which don't use fully-escaped urls.
if ( urlString != urlString2 )
KParts::HistoryProvider::insert( urlString2 );
}
kdDebug(1203) << "## loaded: " << m_history.count() << " entries." << endl;
m_history.sort();
adjustSize();
}
//This is important - we need to switch to a consistent marshalling format for
//communicating between different konqueror instances. Since during an upgrade
//some "old" copies may still running, we use the old format for the DCOP transfers.
//This doesn't make that much difference performance-wise for single entries anyway.
KonqHistoryEntry::marshalURLAsStrings = true;
// Theoretically, we should emit update() here, but as we only ever
// load items on startup up to now, this doesn't make much sense. Same
// thing for the above loadFallback().
// emit KParts::HistoryProvider::update( some list );
file.close();
emit loadingFinished();
return true;
}
// saves the entire history
bool KonqHistoryManager::saveHistory()
{
KSaveFile file( m_filename );
if ( file.status() != 0 ) {
kdWarning() << "Can't open " << file.name() << endl;
return false;
}
TQDataStream *fileStream = file.dataStream();
*fileStream << s_historyVersion;
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
//We use KURL for marshalling URLs in entries in the V3
//file format
KonqHistoryEntry::marshalURLAsStrings = false;
TQPtrListIterator<KonqHistoryEntry> it( m_history );
KonqHistoryEntry *entry;
while ( (entry = it.current()) ) {
stream << *entry;
++it;
}
//For DCOP, transfer strings instead - wire compat.
KonqHistoryEntry::marshalURLAsStrings = true;
TQ_UINT32 crc = crc32( 0, reinterpret_cast<unsigned char *>( data.data() ), data.size() );
*fileStream << crc << data;
file.close();
return true;
}
void KonqHistoryManager::adjustSize()
{
KonqHistoryEntry *entry = m_history.getFirst();
while ( m_history.count() > m_maxCount || isExpired( entry ) ) {
removeFromCompletion( entry->url.prettyURL(), entry->typedURL );
TQString urlString = entry->url.url();
KParts::HistoryProvider::remove( urlString );
addToUpdateList( urlString );
emit entryRemoved( m_history.getFirst() );
m_history.removeFirst(); // deletes the entry
entry = m_history.getFirst();
}
}
void KonqHistoryManager::addPending( const KURL& url, const TQString& typedURL,
const TQString& title )
{
addToHistory( true, url, typedURL, title );
}
void KonqHistoryManager::confirmPending( const KURL& url,
const TQString& typedURL,
const TQString& title )
{
addToHistory( false, url, typedURL, title );
}
void KonqHistoryManager::addToHistory( bool pending, const KURL& _url,
const TQString& typedURL,
const TQString& title )
{
kdDebug(1203) << "## addToHistory: " << _url.prettyURL() << " Typed URL: " << typedURL << ", Title: " << title << endl;
if ( filterOut( _url ) ) // we only want remote URLs
return;
// http URLs without a path will get redirected immediately to url + '/'
if ( _url.path().isEmpty() && _url.protocol().startsWith("http") )
return;
KURL url( _url );
bool hasPass = url.hasPass();
url.setPass( TQString::null ); // No password in the history, especially not in the completion!
url.setHost( url.host().lower() ); // All host parts lower case
KonqHistoryEntry entry;
TQString u = url.prettyURL();
entry.url = url;
if ( (u != typedURL) && !hasPass )
entry.typedURL = typedURL;
// we only keep the title if we are confirming an entry. Otherwise,
// we might get bogus titles from the previous url (actually it's just
// konqueror's window caption).
if ( !pending && u != title )
entry.title = title;
entry.firstVisited = TQDateTime::tqcurrentDateTime();
entry.lastVisited = entry.firstVisited;
// always remove from pending if available, otherwise the else branch leaks
// if the map already contains an entry for this key.
TQMapIterator<TQString,KonqHistoryEntry*> it = m_pending.tqfind( u );
if ( it != m_pending.end() ) {
delete it.data();
m_pending.remove( it );
}
if ( !pending ) {
if ( it != m_pending.end() ) {
// we make a pending entry official, so we just have to update
// and not increment the counter. No need to care about
// firstVisited, as this is not taken into account on update.
entry.numberOfTimesVisited = 0;
}
}
else {
// We add a copy of the current history entry of the url to the
// pending list, so that we can restore it if the user canceled.
// If there is no entry for the url yet, we just store the url.
KonqHistoryEntry *oldEntry = findEntry( url );
m_pending.insert( u, oldEntry ?
new KonqHistoryEntry( *oldEntry ) : 0L );
}
// notify all konqueror instances about the entry
emitAddToHistory( entry );
}
// interface of KParts::HistoryManager
// Usually, we only record the history for non-local URLs (i.e. filterOut()
// returns false). But when using the HistoryProvider interface, we record
// exactly those filtered-out urls.
// Moreover, we don't get any pending/confirming entries, just one insert()
void KonqHistoryManager::insert( const TQString& url )
{
KURL u ( url );
if ( !filterOut( u ) || u.protocol() == "about" ) { // remote URL
return;
}
// Local URL -> add to history
KonqHistoryEntry entry;
entry.url = u;
entry.firstVisited = TQDateTime::tqcurrentDateTime();
entry.lastVisited = entry.firstVisited;
emitAddToHistory( entry );
}
void KonqHistoryManager::emitAddToHistory( const KonqHistoryEntry& entry )
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
stream << entry << objId();
// Protection against very long urls (like data:)
if ( data.size() > 4096 )
return;
kapp->dcopClient()->send( "konqueror*", "KonqHistoryManager",
"notifyHistoryEntry(KonqHistoryEntry, TQCString)",
data );
}
void KonqHistoryManager::removePending( const KURL& url )
{
// kdDebug(1203) << "## Removing pending... " << url.prettyURL() << endl;
if ( url.isLocalFile() )
return;
TQMapIterator<TQString,KonqHistoryEntry*> it = m_pending.tqfind( url.prettyURL() );
if ( it != m_pending.end() ) {
KonqHistoryEntry *oldEntry = it.data(); // the old entry, may be 0L
emitRemoveFromHistory( url ); // remove the current pending entry
if ( oldEntry ) // we had an entry before, now use that instead
emitAddToHistory( *oldEntry );
delete oldEntry;
m_pending.remove( it );
}
}
// clears the pending list and makes sure the entries get deleted.
void KonqHistoryManager::clearPending()
{
TQMapIterator<TQString,KonqHistoryEntry*> it = m_pending.begin();
while ( it != m_pending.end() ) {
delete it.data();
++it;
}
m_pending.clear();
}
void KonqHistoryManager::emitRemoveFromHistory( const KURL& url )
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
stream << url << objId();
kapp->dcopClient()->send( "konqueror*", "KonqHistoryManager",
"notifyRemove(KURL, TQCString)", data );
}
void KonqHistoryManager::emitRemoveFromHistory( const KURL::List& urls )
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
stream << urls << objId();
kapp->dcopClient()->send( "konqueror*", "KonqHistoryManager",
"notifyRemove(KURL::List, TQCString)", data );
}
void KonqHistoryManager::emitClear()
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
stream << objId();
kapp->dcopClient()->send( "konqueror*", "KonqHistoryManager",
"notifyClear(TQCString)", data );
}
void KonqHistoryManager::emitSetMaxCount( TQ_UINT32 count )
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
stream << count << objId();
kapp->dcopClient()->send( "konqueror*", "KonqHistoryManager",
"notifyMaxCount(TQ_UINT32, TQCString)", data );
}
void KonqHistoryManager::emitSetMaxAge( TQ_UINT32 days )
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
stream << days << objId();
kapp->dcopClient()->send( "konqueror*", "KonqHistoryManager",
"notifyMaxAge(TQ_UINT32, TQCString)", data );
}
///////////////////////////////////////////////////////////////////
// DCOP called methods
void KonqHistoryManager::notifyHistoryEntry( KonqHistoryEntry e,
TQCString )
{
//kdDebug(1203) << "Got new entry from Broadcast: " << e.url.prettyURL() << endl;
KonqHistoryEntry *entry = findEntry( e.url );
TQString urlString = e.url.url();
if ( !entry ) { // create a new history entry
entry = new KonqHistoryEntry;
entry->url = e.url;
entry->firstVisited = e.firstVisited;
entry->numberOfTimesVisited = 0; // will get set to 1 below
m_history.append( entry );
KParts::HistoryProvider::insert( urlString );
}
if ( !e.typedURL.isEmpty() )
entry->typedURL = e.typedURL;
if ( !e.title.isEmpty() )
entry->title = e.title;
entry->numberOfTimesVisited += e.numberOfTimesVisited;
entry->lastVisited = e.lastVisited;
addToCompletion( entry->url.prettyURL(), entry->typedURL );
// bool pending = (e.numberOfTimesVisited != 0);
adjustSize();
// note, no need to do the updateBookmarkMetadata for every
// history object, only need to for the broadcast sender as
// the history object itself keeps the data consistant.
bool updated = KonqBookmarkManager::self()->updateAccessMetadata( urlString );
if ( isSenderOfBroadcast() ) {
// we are the sender of the broadcast, so we save
saveHistory();
// note, bk save does not notify, and we don't want to!
if (updated)
KonqBookmarkManager::self()->save();
}
addToUpdateList( urlString );
emit entryAdded( entry );
}
void KonqHistoryManager::notifyMaxCount( TQ_UINT32 count, TQCString )
{
m_maxCount = count;
clearPending();
adjustSize();
KConfig *config = KGlobal::config();
KConfigGroupSaver cs( config, "HistorySettings" );
config->writeEntry( "Maximum of History entries", m_maxCount );
if ( isSenderOfBroadcast() ) {
saveHistory();
config->sync();
}
}
void KonqHistoryManager::notifyMaxAge( TQ_UINT32 days, TQCString )
{
m_maxAgeDays = days;
clearPending();
adjustSize();
KConfig *config = KGlobal::config();
KConfigGroupSaver cs( config, "HistorySettings" );
config->writeEntry( "Maximum age of History entries", m_maxAgeDays );
if ( isSenderOfBroadcast() ) {
saveHistory();
config->sync();
}
}
void KonqHistoryManager::notifyClear( TQCString )
{
clearPending();
m_history.clear();
m_pCompletion->clear();
if ( isSenderOfBroadcast() )
saveHistory();
KParts::HistoryProvider::clear(); // also emits the cleared() signal
}
void KonqHistoryManager::notifyRemove( KURL url, TQCString )
{
kdDebug(1203) << "#### Broadcast: remove entry:: " << url.prettyURL() << endl;
KonqHistoryEntry *entry = m_history.findEntry( url );
if ( entry ) { // entry is now the current item
removeFromCompletion( entry->url.prettyURL(), entry->typedURL );
TQString urlString = entry->url.url();
KParts::HistoryProvider::remove( urlString );
addToUpdateList( urlString );
m_history.take(); // does not delete
emit entryRemoved( entry );
delete entry;
if ( isSenderOfBroadcast() )
saveHistory();
}
}
void KonqHistoryManager::notifyRemove( KURL::List urls, TQCString )
{
kdDebug(1203) << "#### Broadcast: removing list!" << endl;
bool doSave = false;
KURL::List::Iterator it = urls.begin();
while ( it != urls.end() ) {
KonqHistoryEntry *entry = m_history.findEntry( *it );
if ( entry ) { // entry is now the current item
removeFromCompletion( entry->url.prettyURL(), entry->typedURL );
TQString urlString = entry->url.url();
KParts::HistoryProvider::remove( urlString );
addToUpdateList( urlString );
m_history.take(); // does not delete
emit entryRemoved( entry );
delete entry;
doSave = true;
}
++it;
}
if (doSave && isSenderOfBroadcast())
saveHistory();
}
// compatibility fallback, try to load the old completion history
bool KonqHistoryManager::loadFallback()
{
TQString file = locateLocal( "config", TQString::tqfromLatin1("konq_history"));
if ( file.isEmpty() )
return false;
KonqHistoryEntry *entry;
KSimpleConfig config( file );
config.setGroup("History");
TQStringList items = config.readListEntry( "CompletionItems" );
TQStringList::Iterator it = items.begin();
while ( it != items.end() ) {
entry = createFallbackEntry( *it );
if ( entry ) {
m_history.append( entry );
addToCompletion( entry->url.prettyURL(), TQString::null, entry->numberOfTimesVisited );
KParts::HistoryProvider::insert( entry->url.url() );
}
++it;
}
m_history.sort();
adjustSize();
saveHistory();
return true;
}
// tries to create a small KonqHistoryEntry out of a string, where the string
// looks like "http://www.bla.com/bla.html:23"
// the attached :23 is the weighting from KCompletion
KonqHistoryEntry * KonqHistoryManager::createFallbackEntry(const TQString& item) const
{
// code taken from KCompletion::addItem(), adjusted to use weight = 1
uint len = item.length();
uint weight = 1;
// find out the weighting of this item (appended to the string as ":num")
int index = item.tqfindRev(':');
if ( index > 0 ) {
bool ok;
weight = item.mid( index + 1 ).toUInt( &ok );
if ( !ok )
weight = 1;
len = index; // only insert until the ':'
}
KonqHistoryEntry *entry = 0L;
KURL u( item.left( len ));
if ( u.isValid() ) {
entry = new KonqHistoryEntry;
// that's the only entries we know about...
entry->url = u;
entry->numberOfTimesVisited = weight;
// to make it not expire immediately...
entry->lastVisited = TQDateTime::tqcurrentDateTime();
}
return entry;
}
KonqHistoryEntry * KonqHistoryManager::findEntry( const KURL& url )
{
// small optimization (dict lookup) for items _not_ in our history
if ( !KParts::HistoryProvider::contains( url.url() ) )
return 0L;
return m_history.findEntry( url );
}
bool KonqHistoryManager::filterOut( const KURL& url )
{
return ( url.isLocalFile() || url.host().isEmpty() );
}
void KonqHistoryManager::slotEmitUpdated()
{
emit KParts::HistoryProvider::updated( m_updateURLs );
m_updateURLs.clear();
}
TQStringList KonqHistoryManager::allURLs() const
{
TQStringList list;
KonqHistoryIterator it ( m_history );
for ( ; it.current(); ++it )
list.append( it.current()->url.url() );
return list;
}
void KonqHistoryManager::addToCompletion( const TQString& url, const TQString& typedURL,
int numberOfTimesVisited )
{
m_pCompletion->addItem( url, numberOfTimesVisited );
// typed urls have a higher priority
m_pCompletion->addItem( typedURL, numberOfTimesVisited +10 );
}
void KonqHistoryManager::removeFromCompletion( const TQString& url, const TQString& typedURL )
{
m_pCompletion->removeItem( url );
m_pCompletion->removeItem( typedURL );
}
//////////////////////////////////////////////////////////////////
KonqHistoryEntry * KonqHistoryList::findEntry( const KURL& url )
{
// we search backwards, probably faster to find an entry
KonqHistoryEntry *entry = last();
while ( entry ) {
if ( entry->url == url )
return entry;
entry = prev();
}
return 0L;
}
// sort by lastVisited date (oldest go first)
int KonqHistoryList::compareItems( TQPtrCollection::Item item1,
TQPtrCollection::Item item2 )
{
KonqHistoryEntry *entry1 = static_cast<KonqHistoryEntry *>( item1 );
KonqHistoryEntry *entry2 = static_cast<KonqHistoryEntry *>( item2 );
if ( entry1->lastVisited > entry2->lastVisited )
return 1;
else if ( entry1->lastVisited < entry2->lastVisited )
return -1;
else
return 0;
}
using namespace KParts; // for IRIX
#include "konq_historymgr.moc"
|