document.cpp 56 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2004-2005 by Enrico Ros <eros.kde@email.it>             *
3
 *   Copyright (C) 2004-2005 by Albert Astals Cid <tsdgeos@terra.es>       *
4 5 6 7 8 9 10
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

11 12
// qt/kde/system includes
#include <qdir.h>
13
#include <qfile.h>
14
#include <qfileinfo.h>
15
#include <qimage.h>
16
#include <qtextstream.h>
17
#include <qvaluevector.h>
18
#include <qtimer.h>
19
#include <qmap.h>
Enrico Ros's avatar
Enrico Ros committed
20
#include <kdebug.h>
21
#include <kimageio.h>
22 23 24
#include <klocale.h>
#include <kfinddialog.h>
#include <kmessagebox.h>
25 26
#include <kapplication.h>
#include <kuserprofile.h>
27 28
#include <kmimetype.h>
#include <krun.h>
29
#include <kstandarddirs.h>
30 31
#include <klibloader.h>
#include <ktrader.h>
32 33 34

// local includes
#include "document.h"
35
#include "generator.h"
36
#include "observer.h"
37
#include "page.h"
38
#include "link.h"
39

40
#include "conf/settings.h"
41

42
// structures used internally by KPDFDocument for local variables storage
43 44
class AllocatedPixmap;
class RunningSearch;
45
class KPDFDocumentPrivate
46
{
47
    public:
48 49
        // find descriptors, mapped by ID (we handle multiple searches)
        QMap< int, RunningSearch * > searches;
50

51 52 53 54
        // needed because for remote documents docFileName is a local file and
        // we want the remote url when the document refers to relativeNames
        KURL url;

55
        // cached stuff
56 57
        QString docFileName;
        QString xmlFileName;
58

59 60 61
        // list of the mimetypes 'generator_kimgio' can understand
        QStringList kimgioMimes;

62 63 64
        // viewport stuff
        QValueList< DocumentViewport > viewportHistory;
        QValueList< DocumentViewport >::iterator viewportIterator;
65
        DocumentViewport nextDocumentViewport; // see KPDFLink::Goto for an explanation
66

67
        // observers / requests / allocator stuff
68
        QMap< int, DocumentObserver * > observers;
Enrico Ros's avatar
Enrico Ros committed
69
        QValueList< PixmapRequest * > pixmapRequestsStack;
70
        QValueList< AllocatedPixmap * > allocatedPixmapsFifo;
71
        int allocatedPixmapsTotalMemory;
Enrico Ros's avatar
Enrico Ros committed
72
        bool warnedOutOfMemory;
73

74 75
        // timers (memory checking / info saver)
        QTimer * memCheckTimer;
76
        QTimer * saveBookmarksTimer;
77 78
};

79
struct AllocatedPixmap
80
{
81 82 83 84
    // owner of the page
    int id;
    int page;
    int memory;
85
    // public constructor: initialize data
86
    AllocatedPixmap( int i, int p, int m ) : id( i ), page( p ), memory( m ) {};
87 88
};

89 90
struct RunningSearch
{
91 92
    // store search properties
    int continueOnPage;
93
    RegularAreaRect continueOnMatch;
94 95 96 97 98 99 100 101 102
    QValueList< int > highlightedPages;

    // fields related to previous searches (used for 'continueSearch')
    QString cachedString;
    KPDFDocument::SearchType cachedType;
    bool cachedCaseSensitive;
    bool cachedViewportMove;
    bool cachedNoDialogs;
    QColor cachedColor;
103 104
};

105
#define foreachObserver( cmd ) {\
106 107
    QMap< int, DocumentObserver * >::iterator it=d->observers.begin(), end=d->observers.end();\
    for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
108

109 110 111

/** KPDFDocument **/

112
KPDFDocument::KPDFDocument()
113
    : generator( 0 ), d( new KPDFDocumentPrivate )
114
{
115
    d->allocatedPixmapsTotalMemory = 0;
116 117
    d->memCheckTimer = 0;
    d->saveBookmarksTimer = 0;
Enrico Ros's avatar
Enrico Ros committed
118
    d->warnedOutOfMemory = false;
119
}
Enrico Ros's avatar
Enrico Ros committed
120

121 122
KPDFDocument::~KPDFDocument()
{
123
    // delete generator, pages, and related stuff
124
    closeDocument();
Albert Astals Cid's avatar
Albert Astals Cid committed
125

126
    // delete the private structure
127 128 129
    delete d;
}

130

131
bool KPDFDocument::openDocument( const QString & docFile, const KURL & url )
132 133 134 135
{
    // docFile is always local so we can use QFile on it
    QFile fileReadTest( docFile );
    if ( !fileReadTest.open( IO_ReadOnly ) )
136
    {
137
        d->docFileName = QString::null;
138
        return false;
139
    }
140
    // determine the related "xml document-info" filename
141
    d->url = url;
142 143 144
    d->docFileName = docFile;
    QString fn = docFile.contains('/') ? docFile.section('/', -1, -1) : docFile;
    fn = "kpdf/" + QString::number(fileReadTest.size()) + "." + fn + ".xml";
145
    fileReadTest.close();
146
    d->xmlFileName = locateLocal( "data", fn );
147

148 149
    // create the generator based on the file's mimetype
    KMimeType::Ptr mime = KMimeType::findByPath( docFile );
150 151 152 153 154 155 156 157 158
    if (mime.count()<=0)
	return false;
    
    // 0. load Generator
    // request only valid non-disabled plugins suitable for the mimetype
    QString constraint("([X-KDE-Priority] > 0) and (exist Library)") ;
    KTrader::OfferList offers=KTrader::self()->query(mime->name(),"oKular/Generator",constraint, QString::null);
    
    if (offers.isEmpty())
159
    {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	kdWarning() << "No plugin for '" << mime->name() << "' mimetype." << endl;
	return false;
    }
    
    // best ranked offer search
    int hRank=0;
    for (short i=0;i<offers.count();i++)
	if (offers[hRank]->property("[X-KDE-Priority]").toInt() <  offers[i]->property("[X-KDE-Priority])").toInt())
	    hRank=i;
    
    KLibLoader *loader = KLibLoader::self();
    if (!loader)
    {
	kdWarning() << "Could not start library loader: '" << loader->lastErrorMessage() << "'." << endl;
	return false;
175
    }
176 177 178 179 180 181 182 183 184 185
    KLibrary *lib = loader->globalLibrary( QFile::encodeName( offers[hRank]->library() ) );
    if (!lib) 
    {
	kdWarning() << "Could not load '" << lib->fileName() << "' library." << endl;
	return false;
    }
    
    Generator* (*create_plugin)(KPDFDocument* doc) = ( Generator* (*)(KPDFDocument* doc) ) lib->symbol( "create_plugin" );
    generator=create_plugin(this);
    
186
    if ( !generator )
187
    {
188 189
	kdWarning() << "Sth broke." << endl;
	return false;	
190
    }
191
    // end 
192 193
    // 1. load Document (and set busy cursor while loading)
    QApplication::setOverrideCursor( waitCursor );
194
    bool openOk = generator->loadDocument( docFile, pages_vector );
195
    QApplication::restoreOverrideCursor();
196
    if ( !openOk || pages_vector.size() <= 0 )
197 198 199
    {
        delete generator;
        generator = 0;
200
        return openOk;
201
    }
202

203
    // 2. load Additional Data (our bookmarks and metadata) about the document
204 205
    loadDocumentInfo();

206
    // 3. setup observers inernal lists and data
207
    foreachObserver( notifySetup( pages_vector, true ) );
208

209 210 211
    // 4. set initial page (restoring the page saved in xml if loaded)
    DocumentViewport loadedViewport = (*d->viewportIterator);
    if ( loadedViewport.pageNumber != -1 )
212
    {
213
        (*d->viewportIterator) = DocumentViewport();
214 215 216
        if ( loadedViewport.pageNumber >= (int)pages_vector.size() )
            loadedViewport.pageNumber = pages_vector.size() - 1;
    }
217
    else
218
        loadedViewport.pageNumber = 0;
219
    setViewport( loadedViewport );
220

221
    // start bookmark saver timer
222 223 224 225 226
    if ( !d->saveBookmarksTimer )
    {
        d->saveBookmarksTimer = new QTimer( this );
        connect( d->saveBookmarksTimer, SIGNAL( timeout() ), this, SLOT( saveDocumentInfo() ) );
    }
227 228 229
    d->saveBookmarksTimer->start( 5 * 60 * 1000 );

    // start memory check timer
230 231 232 233 234
    if ( !d->memCheckTimer )
    {
        d->memCheckTimer = new QTimer( this );
        connect( d->memCheckTimer, SIGNAL( timeout() ), this, SLOT( slotTimedMemoryCheck() ) );
    }
235 236
    d->memCheckTimer->start( 2000 );

237 238 239 240 241 242
    if (d->nextDocumentViewport.pageNumber != -1)
    {
        setViewport(d->nextDocumentViewport);
        d->nextDocumentViewport = DocumentViewport();
    }

243 244 245
    return true;
}

246
void KPDFDocument::closeDocument()
247
{
248 249 250
    // save document info if a document is still opened
    if ( generator && pages_vector.size() > 0 )
        saveDocumentInfo();
251

252 253 254 255 256
    // stop timers
    if ( d->memCheckTimer )
        d->memCheckTimer->stop();
    if ( d->saveBookmarksTimer )
        d->saveBookmarksTimer->stop();
257

258 259 260
    // delete contents generator
    delete generator;
    generator = 0;
261
    d->url = KURL();
Enrico Ros's avatar
Enrico Ros committed
262 263 264 265 266 267 268
    // remove requests left in queue
    QValueList< PixmapRequest * >::iterator sIt = d->pixmapRequestsStack.begin();
    QValueList< PixmapRequest * >::iterator sEnd = d->pixmapRequestsStack.end();
    for ( ; sIt != sEnd; ++sIt )
        delete *sIt;
    d->pixmapRequestsStack.clear();

269
    // send an empty list to observers (to free their data)
270
    foreachObserver( notifySetup( QValueVector< KPDFPage * >(), true ) );
271

272
    // delete pages and clear 'pages_vector' container
273 274 275 276
    QValueVector< KPDFPage * >::iterator pIt = pages_vector.begin();
    QValueVector< KPDFPage * >::iterator pEnd = pages_vector.end();
    for ( ; pIt != pEnd; ++pIt )
        delete *pIt;
277 278
    pages_vector.clear();

279
    // clear 'memory allocation' descriptors
280 281 282 283 284
    QValueList< AllocatedPixmap * >::iterator aIt = d->allocatedPixmapsFifo.begin();
    QValueList< AllocatedPixmap * >::iterator aEnd = d->allocatedPixmapsFifo.end();
    for ( ; aIt != aEnd; ++aIt )
        delete *aIt;
    d->allocatedPixmapsFifo.clear();
285

286 287 288 289 290 291 292
    // clear 'running searches' descriptors
    QMap< int, RunningSearch * >::iterator rIt = d->searches.begin();
    QMap< int, RunningSearch * >::iterator rEnd = d->searches.end();
    for ( ; rIt != rEnd; ++rIt )
        delete *rIt;
    d->searches.clear();

293
    // reset internal variables
294

295 296 297
    d->viewportHistory.clear();
    d->viewportHistory.append( DocumentViewport() );
    d->viewportIterator = d->viewportHistory.begin();
298
    d->allocatedPixmapsTotalMemory = 0;
299 300
}

301
void KPDFDocument::addObserver( DocumentObserver * pObserver )
302
{
303
    // keep the pointer to the observer in a map
304
    d->observers[ pObserver->observerId() ] = pObserver;
305 306 307

    // if the observer is added while a document is already opened, tell it
    if ( !pages_vector.isEmpty() )
308
    {
309
        pObserver->notifySetup( pages_vector, true );
310 311
        pObserver->notifyViewportChanged( false /*disables smoothMove*/ );
    }
312 313
}

314
void KPDFDocument::removeObserver( DocumentObserver * pObserver )
315 316
{
    // remove observer from the map. it won't receive notifications anymore
317 318
    if ( d->observers.contains( pObserver->observerId() ) )
    {
319
        // free observer's pixmap data
320 321 322 323
        int observerId = pObserver->observerId();
        QValueVector<KPDFPage*>::iterator it = pages_vector.begin(), end = pages_vector.end();
        for ( ; it != end; ++it )
            (*it)->deletePixmap( observerId );
324

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        // [MEM] free observer's allocation descriptors
        QValueList< AllocatedPixmap * >::iterator aIt = d->allocatedPixmapsFifo.begin();
        QValueList< AllocatedPixmap * >::iterator aEnd = d->allocatedPixmapsFifo.end();
        while ( aIt != aEnd )
        {
            AllocatedPixmap * p = *aIt;
            if ( p->id == observerId )
            {
                aIt = d->allocatedPixmapsFifo.remove( aIt );
                delete p;
            }
            else
                ++aIt;
        }

340
        // delete observer entry from the map
341 342
        d->observers.remove( observerId );
    }
343 344
}

345
void KPDFDocument::reparseConfig()
346
{
347 348
    // reparse generator config and if something changed clear KPDFPages
    if ( generator && generator->reparseConfig() )
349
    {
350
        // invalidate pixmaps
351
        QValueVector<KPDFPage*>::iterator it = pages_vector.begin(), end = pages_vector.end();
352
        for ( ; it != end; ++it )
353
            (*it)->deletePixmapsAndRects();
354 355 356 357 358 359 360 361 362 363

        // [MEM] remove allocation descriptors
        QValueList< AllocatedPixmap * >::iterator aIt = d->allocatedPixmapsFifo.begin();
        QValueList< AllocatedPixmap * >::iterator aEnd = d->allocatedPixmapsFifo.end();
        for ( ; aIt != aEnd; ++aIt )
            delete *aIt;
        d->allocatedPixmapsFifo.clear();
        d->allocatedPixmapsTotalMemory = 0;

        // send reload signals to observers
364
        foreachObserver( notifyContentsCleared( DocumentObserver::Pixmap ) );
365
    }
366 367 368 369 370

    // free memory if in 'low' profile
    if ( Settings::memoryLevel() == Settings::EnumMemoryLevel::Low &&
         !d->allocatedPixmapsFifo.isEmpty() && !pages_vector.isEmpty() )
        cleanupPixmapMemory();
371 372
}

373

374 375 376 377 378
bool KPDFDocument::isOpened() const
{
    return generator;
}

379
const DocumentInfo * KPDFDocument::documentInfo() const
Albert Astals Cid's avatar
Albert Astals Cid committed
380
{
Enrico Ros's avatar
Enrico Ros committed
381
    return generator ? generator->generateDocumentInfo() : NULL;
382 383 384 385
}

const DocumentSynopsis * KPDFDocument::documentSynopsis() const
{
Enrico Ros's avatar
Enrico Ros committed
386
    return generator ? generator->generateDocumentSynopsis() : NULL;
Albert Astals Cid's avatar
Albert Astals Cid committed
387 388
}

389 390 391 392 393
const DocumentFonts * KPDFDocument::documentFonts() const
{
    return generator ? generator->generateDocumentFonts() : NULL;
}

394
const KPDFPage * KPDFDocument::page( uint n ) const
Albert Astals Cid's avatar
Albert Astals Cid committed
395
{
396
    return ( n < pages_vector.count() ) ? pages_vector[n] : 0;
Albert Astals Cid's avatar
Albert Astals Cid committed
397 398
}

399 400
const DocumentViewport & KPDFDocument::viewport() const
{
401
    return (*d->viewportIterator);
402 403
}

404
uint KPDFDocument::currentPage() const
Albert Astals Cid's avatar
Albert Astals Cid committed
405
{
406
    return (*d->viewportIterator).pageNumber;
Albert Astals Cid's avatar
Albert Astals Cid committed
407 408
}

409
uint KPDFDocument::pages() const
Albert Astals Cid's avatar
Albert Astals Cid committed
410
{
411
    return pages_vector.size();
Albert Astals Cid's avatar
Albert Astals Cid committed
412 413
}

Enrico Ros's avatar
Enrico Ros committed
414 415 416 417 418
KURL KPDFDocument::currentDocument() const
{
    return d->url;
}

419
bool KPDFDocument::isAllowed( int flags ) const
420
{
421
    return generator ? generator->isAllowed( flags ) : false;
422 423
}

424 425
bool KPDFDocument::supportsSearching() const
{
426
    return generator ? generator->supportsSearching() : false;
427 428
}

429 430 431 432 433 434 435 436 437 438
bool KPDFDocument::historyAtBegin() const
{
    return d->viewportIterator == d->viewportHistory.begin();
}

bool KPDFDocument::historyAtEnd() const
{
    return d->viewportIterator == --(d->viewportHistory.end());
}

439
QString KPDFDocument::getMetaData( const QString & key, const QString & option ) const
440
{
441
    return generator ? generator->getMetaData( key, option ) : QString();
442
}
443

Enrico Ros's avatar
Enrico Ros committed
444
void KPDFDocument::requestPixmaps( const QValueList< PixmapRequest * > & requests )
445
{
446 447 448 449 450 451 452
    if ( !generator )
    {
        // delete requests..
        QValueList< PixmapRequest * >::const_iterator rIt = requests.begin(), rEnd = requests.end();
        for ( ; rIt != rEnd; ++rIt )
            delete *rIt;
        // ..and return
453
        return;
454
    }
455

Enrico Ros's avatar
Enrico Ros committed
456 457 458 459 460 461
    // 1. [CLEAN STACK] remove previous requests of requesterID
    int requesterID = requests.first()->id;
    QValueList< PixmapRequest * >::iterator sIt = d->pixmapRequestsStack.begin(), sEnd = d->pixmapRequestsStack.end();
    while ( sIt != sEnd )
    {
        if ( (*sIt)->id == requesterID )
462 463 464
        {
            // delete request and remove it from stack
            delete *sIt;
Enrico Ros's avatar
Enrico Ros committed
465
            sIt = d->pixmapRequestsStack.remove( sIt );
466
        }
Enrico Ros's avatar
Enrico Ros committed
467 468 469 470 471 472
        else
            ++sIt;
    }

    // 2. [ADD TO STACK] add requests to stack
    bool threadingDisabled = !Settings::enableThreading();
473 474
    QValueList< PixmapRequest * >::const_iterator rIt = requests.begin(), rEnd = requests.end();
    for ( ; rIt != rEnd; ++rIt )
475
    {
Enrico Ros's avatar
Enrico Ros committed
476
        // set the 'page field' (see PixmapRequest) and check if it is valid
477
        PixmapRequest * request = *rIt;
Enrico Ros's avatar
Enrico Ros committed
478
        if ( !(request->page = pages_vector[ request->pageNumber ]) )
479 480 481
        {
            // skip requests referencing an invalid page (must not happen)
            delete request;
482
            continue;
483
        }
484

Enrico Ros's avatar
Enrico Ros committed
485 486
        if ( !request->async )
            request->priority = 0;
487

Enrico Ros's avatar
Enrico Ros committed
488 489
        if ( request->async && threadingDisabled )
            request->async = false;
490

Enrico Ros's avatar
Enrico Ros committed
491 492 493
        // add request to the 'stack' at the right place
        if ( !request->priority )
            // add priority zero requests to the top of the stack
494
            d->pixmapRequestsStack.append( request );
Enrico Ros's avatar
Enrico Ros committed
495 496 497 498 499 500 501 502 503
        else
        {
            // insert in stack sorted by priority
            sIt = d->pixmapRequestsStack.begin();
            sEnd = d->pixmapRequestsStack.end();
            while ( sIt != sEnd && (*sIt)->priority >= request->priority )
                ++sIt;
            d->pixmapRequestsStack.insert( sIt, request );
        }
504
    }
Enrico Ros's avatar
Enrico Ros committed
505 506 507 508 509 510

    // 3. [START FIRST GENERATION] if generator is ready, start a new generation,
    // or else (if gen is running) it will be started when the new contents will
    //come from generator (in requestDone())
    if ( generator->canGeneratePixmap() )
        sendGeneratorRequest();
511 512
}

513
void KPDFDocument::requestTextPage( uint page )
514
{
515 516 517 518
    KPDFPage * kp = pages_vector[ page ];
    if ( !generator || !kp )
        return;

519 520
    // Memory management for TextPages

Enrico Ros's avatar
Enrico Ros committed
521
    generator->generateSyncTextPage( kp );
522
}
523 524 525 526 527 528 529 530 531 532 533 534 535 536

void KPDFDocument::addPageAnnotation( int page, Annotation * annotation )
{
    // find out the page to attach annotation
    KPDFPage * kp = pages_vector[ page ];
    if ( !generator || !kp )
        return;

    // add annotation to the page
    kp->addAnnotation( annotation );

    // notify observers about the change
    foreachObserver( notifyPageChanged( page, DocumentObserver::Annotations ) );
}
537 538 539 540
/* REFERENCE IMPLEMENTATION: better calling setViewport from other code
void KPDFDocument::setNextPage()
{
    // advance page and set viewport on observers
541 542
    if ( (*d->viewportIterator).pageNumber < (int)pages_vector.count() - 1 )
        setViewport( DocumentViewport( (*d->viewportIterator).pageNumber + 1 ) );
543
}
544

545
void KPDFDocument::setPrevPage()
546
{
547
    // go to previous page and set viewport on observers
548 549
    if ( (*d->viewportIterator).pageNumber > 0 )
        setViewport( DocumentViewport( (*d->viewportIterator).pageNumber - 1 ) );
550 551
}
*/
Enrico Ros's avatar
Enrico Ros committed
552
void KPDFDocument::setViewportPage( int page, int excludeId, bool smoothMove )
553 554
{
    // clamp page in range [0 ... numPages-1]
Enrico Ros's avatar
Enrico Ros committed
555
    if ( page < 0 )
556
        page = 0;
557 558
    else if ( page > (int)pages_vector.count() )
        page = pages_vector.count() - 1;
559 560

    // make a viewport from the page and broadcast it
Enrico Ros's avatar
Enrico Ros committed
561
    setViewport( DocumentViewport( page ), excludeId, smoothMove );
562 563
}

Enrico Ros's avatar
Enrico Ros committed
564
void KPDFDocument::setViewport( const DocumentViewport & viewport, int excludeId, bool smoothMove )
565 566
{
    // if already broadcasted, don't redo it
567
    DocumentViewport & oldViewport = *d->viewportIterator;
568 569 570
    // disabled by enrico on 2005-03-18 (less debug output)
    //if ( viewport == oldViewport )
    //    kdDebug() << "setViewport with the same viewport." << endl;
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585
    // set internal viewport taking care of history
    if ( oldViewport.pageNumber == viewport.pageNumber || oldViewport.pageNumber == -1 )
    {
        // if page is unchanged save the viewport at current position in queue
        oldViewport = viewport;
    }
    else
    {
        // remove elements after viewportIterator in queue
        d->viewportHistory.erase( ++d->viewportIterator, d->viewportHistory.end() );

        // keep the list to a reasonable size by removing head when needed
        if ( d->viewportHistory.count() >= 100 )
            d->viewportHistory.pop_front();
586

587 588 589 590 591
        // add the item at the end of the queue
        d->viewportIterator = d->viewportHistory.append( viewport );
    }

    // notify change to all other (different from id) observers
592
    QMap< int, DocumentObserver * >::iterator it = d->observers.begin(), end = d->observers.end();
593
    for ( ; it != end ; ++ it )
594
        if ( it.key() != excludeId )
Enrico Ros's avatar
Enrico Ros committed
595
            (*it)->notifyViewportChanged( smoothMove );
596

597
    // [MEM] raise position of currently viewed page in allocation queue
598 599
    if ( d->allocatedPixmapsFifo.count() > 1 )
    {
600 601
        const int page = viewport.pageNumber;
        QValueList< AllocatedPixmap * > viewportPixmaps;
602
        QValueList< AllocatedPixmap * >::iterator aIt = d->allocatedPixmapsFifo.begin();
603 604
        QValueList< AllocatedPixmap * >::iterator aEnd = d->allocatedPixmapsFifo.end();
        while ( aIt != aEnd )
605
        {
606
            if ( (*aIt)->page == page )
607
            {
608
                viewportPixmaps.append( *aIt );
609
                aIt = d->allocatedPixmapsFifo.remove( aIt );
610
                continue;
611
            }
612
            ++aIt;
613
        }
614 615 616
        if ( !viewportPixmaps.isEmpty() )
            d->allocatedPixmapsFifo += viewportPixmaps;
    }
617 618
}

619 620 621 622 623 624 625
void KPDFDocument::setPrevViewport()
// restore viewport from the history
{
    if ( d->viewportIterator != d->viewportHistory.begin() )
    {
        // restore previous viewport and notify it to observers
        --d->viewportIterator;
Enrico Ros's avatar
Enrico Ros committed
626
        foreachObserver( notifyViewportChanged( true ) );
627 628 629 630 631 632 633 634 635 636 637 638
    }
}

void KPDFDocument::setNextViewport()
// restore next viewport from the history
{
    QValueList< DocumentViewport >::iterator nextIterator = d->viewportIterator;
    ++nextIterator;
    if ( nextIterator != d->viewportHistory.end() )
    {
        // restore next viewport and notify it to observers
        ++d->viewportIterator;
Enrico Ros's avatar
Enrico Ros committed
639
        foreachObserver( notifyViewportChanged( true ) );
640 641 642
    }
}

643

644 645
bool KPDFDocument::searchText( int searchID, const QString & text, bool fromStart, bool caseSensitive,
                               SearchType type, bool moveViewport, const QColor & color, bool noDialogs )
646
{
647
    // safety checks: don't perform searches on empty or unsearchable docs
648
    if ( !generator || !generator->supportsSearching() || pages_vector.isEmpty() )
649 650
        return false;

651
    // if searchID search not recorded, create new descriptor and init params
652 653 654
    if ( !d->searches.contains( searchID ) )
    {
        RunningSearch * search = new RunningSearch();
655
        search->continueOnPage = -1;
656 657 658 659
        d->searches[ searchID ] = search;
    }
    RunningSearch * s = d->searches[ searchID ];

660 661 662 663 664 665 666 667
    // update search stucture
    bool newText = text != s->cachedString;
    s->cachedString = text;
    s->cachedType = type;
    s->cachedCaseSensitive = caseSensitive;
    s->cachedViewportMove = moveViewport;
    s->cachedNoDialogs = noDialogs;
    s->cachedColor = color;
668

669 670 671 672 673 674 675
    // global data for search
    bool foundAMatch = false;
    QValueList< int > pagesToNotify;

    // remove highlights from pages and queue them for notifying changes
    pagesToNotify += s->highlightedPages;
    QValueList< int >::iterator it = s->highlightedPages.begin(), end = s->highlightedPages.end();
676
    for ( ; it != end; ++it )
677 678
        pages_vector[ *it ]->deleteHighlights( searchID );
    s->highlightedPages.clear();
679

680 681
    // set hourglass cursor
    QApplication::setOverrideCursor( waitCursor );
682

683
    // 1. ALLDOC - proces all document marking pages
684 685
    if ( type == AllDoc )
    {
686
        // search and highlight 'text' (as a solid phrase) on all pages
687 688 689
        QValueVector< KPDFPage * >::iterator it = pages_vector.begin(), end = pages_vector.end();
        for ( ; it != end; ++it )
        {
690
            // get page (from the first to the last)
691
            KPDFPage * page = *it;
692 693 694
            int pageNumber = page->number();

            // request search page if needed
695
            if ( !page->hasSearchPage() )
696
                requestTextPage( pageNumber );
697

698 699
            // loop on a page adding highlights for all found items
            bool addedHighlights = false;
700
            RegularAreaRect * lastMatch = 0;
701 702 703
            while ( 1 )
            {
                if ( lastMatch )
704
                    lastMatch = page->findText( text, NextRes , caseSensitive, lastMatch);
705
                else
706
                    lastMatch = page->findText( text, FromTop, caseSensitive);
707 708 709 710

                if ( !lastMatch )
                    break;

711
                // add highligh rect to the page
712
                page->setHighlight( searchID, lastMatch, color );
713
                addedHighlights = true;
714 715
            }

716 717
            // if added highlights, udpate internals and queue page for notify
            if ( addedHighlights )
718 719
            {
                foundAMatch = true;
720 721 722
                s->highlightedPages.append( pageNumber );
                if ( !pagesToNotify.contains( pageNumber ) )
                    pagesToNotify.append( pageNumber );
723 724
            }
        }
725 726 727 728

        // reset cursor to previous shape
        QApplication::restoreOverrideCursor();

729 730
        // send page lists if found anything new
	//if ( foundAMatch ) ?maybe?
731
        foreachObserver( notifySetup( pages_vector, false ) );
732
    }
733 734
    // 2. NEXTMATCH - find next matching item (or start from top)
    else if ( type == NextMatch )
735 736 737
    {
        // find out from where to start/resume search from
        int viewportPage = (*d->viewportIterator).pageNumber;
738 739
        int currentPage = fromStart ? 0 : ((s->continueOnPage != -1) ? s->continueOnPage : viewportPage);
        KPDFPage * lastPage = fromStart ? 0 : pages_vector[ currentPage ];
740 741

        // continue checking last SearchPage first (if it is the current page)
742
        RegularAreaRect * match = 0;
743
        if ( lastPage && lastPage->number() == s->continueOnPage )
Enrico Ros's avatar
Enrico Ros committed
744
        {
745
            if ( newText )
746
                match = lastPage->findText( text, FromTop , caseSensitive);
747
            else
748
                match = lastPage->findText( text, NextRes, caseSensitive,&s->continueOnMatch );
749
            if ( !match )
750
                currentPage++;
Enrico Ros's avatar
Enrico Ros committed
751 752
        }

753
        // if no match found, loop through the whole doc, starting from currentPage
754
        if ( !match )
755 756 757 758 759 760
        {
            const int pageCount = pages_vector.count();
            for ( int i = 0; i < pageCount; i++ )
            {
                if ( currentPage >= pageCount )
                {
761
                    if ( noDialogs || KMessageBox::questionYesNo(0, i18n("End of document reached.\nContinue from the beginning?")) == KMessageBox::Yes )
762 763 764 765
                        currentPage = 0;
                    else
                        break;
                }
766
                // get page
767
                KPDFPage * page = pages_vector[ currentPage ];
768
                // request search page if needed
769 770
                if ( !page->hasSearchPage() )
                    requestTextPage( page->number() );
771
                // if found a match on the current page, end the loop
772
                if ( (match = page->findText( text, FromTop, caseSensitive )) )
773 774 775 776 777
                    break;
                currentPage++;
            }
        }

778 779 780 781
        // reset cursor to previous shape
        QApplication::restoreOverrideCursor();

        // if a match has been found..
782
        if ( match )
783
        {
784
            // update the RunningSearch structure adding this match..
785
            foundAMatch = true;
786 787 788
            s->continueOnPage = currentPage;
            s->continueOnMatch = *match;
            s->highlightedPages.append( currentPage );
789
            // ..add highlight to the page..
790
            pages_vector[ currentPage ]->setHighlight( searchID, match, color );
791

792
            // ..queue page for notifying changes..
793 794
            if ( !pagesToNotify.contains( currentPage ) )
                pagesToNotify.append( currentPage );
795

796
            // ..move the viewport to show the first of the searched word sequence centered
797 798
            if ( moveViewport )
            {
799
                DocumentViewport searchViewport( currentPage );
Enrico Ros's avatar
Enrico Ros committed
<