2004-09-08 12:41:14 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2004 by Enrico Ros <eros.kde@email.it> *
|
2004-09-23 21:33:53 +00:00
|
|
|
* Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es> *
|
2004-09-08 12:41:14 +00:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2004-10-06 00:05:49 +00:00
|
|
|
// qt/kde/system includes
|
|
|
|
#include <qdir.h>
|
2004-09-08 12:41:14 +00:00
|
|
|
#include <qfile.h>
|
2004-10-06 00:05:49 +00:00
|
|
|
#include <qfileinfo.h>
|
2005-01-03 00:28:46 +00:00
|
|
|
#include <qtextstream.h>
|
2004-09-08 12:41:14 +00:00
|
|
|
#include <qvaluevector.h>
|
2004-12-24 10:24:10 +00:00
|
|
|
#include <qtimer.h>
|
2004-09-15 20:45:00 +00:00
|
|
|
#include <qmap.h>
|
2004-09-12 23:19:18 +00:00
|
|
|
#include <kdebug.h>
|
2004-09-16 21:04:49 +00:00
|
|
|
#include <klocale.h>
|
|
|
|
#include <kfinddialog.h>
|
|
|
|
#include <kmessagebox.h>
|
2004-10-06 00:05:49 +00:00
|
|
|
#include <kapplication.h>
|
|
|
|
#include <kuserprofile.h>
|
2004-12-10 16:04:45 +00:00
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <krun.h>
|
2005-01-03 00:28:46 +00:00
|
|
|
#include <kstandarddirs.h>
|
2004-09-08 12:41:14 +00:00
|
|
|
|
|
|
|
// local includes
|
|
|
|
#include "document.h"
|
2005-01-02 14:55:14 +00:00
|
|
|
#include "observer.h"
|
2004-09-08 12:41:14 +00:00
|
|
|
#include "page.h"
|
2004-12-10 16:04:45 +00:00
|
|
|
#include "link.h"
|
2005-01-02 14:37:49 +00:00
|
|
|
#include "generator_pdf/generator_pdf.h" // PDF generator
|
2005-01-02 14:55:14 +00:00
|
|
|
#include "conf/settings.h"
|
2004-09-09 13:25:40 +00:00
|
|
|
|
2004-12-21 12:38:52 +00:00
|
|
|
// structures used internally by KPDFDocument for local variables storage
|
2004-09-09 13:25:40 +00:00
|
|
|
class KPDFDocumentPrivate
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
public:
|
|
|
|
// find related
|
|
|
|
QString searchText;
|
|
|
|
bool searchCase;
|
|
|
|
int searchPage;
|
|
|
|
// filtering related
|
|
|
|
QString filterText;
|
|
|
|
bool filterCase;
|
|
|
|
|
|
|
|
// cached stuff
|
2005-01-09 23:37:07 +00:00
|
|
|
DocumentViewport viewport;
|
2005-01-13 11:03:48 +00:00
|
|
|
QString docFileName;
|
|
|
|
QString xmlFileName;
|
2004-12-10 16:04:45 +00:00
|
|
|
|
2005-01-13 11:03:48 +00:00
|
|
|
// observers / requests stuff
|
|
|
|
QMap< int, class ObserverData* > observers;
|
|
|
|
//QValueList< PixmapRequest * > asyncRequestsQueue;
|
2004-12-24 10:24:10 +00:00
|
|
|
|
2005-01-13 11:03:48 +00:00
|
|
|
// timers (memory checking / info saver)
|
|
|
|
QTimer * memCheckTimer;
|
2005-01-03 00:28:46 +00:00
|
|
|
QTimer * saveBookmarksTimer;
|
2004-12-21 12:38:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ObserverData
|
|
|
|
{
|
|
|
|
// public data fields
|
2005-01-09 23:37:07 +00:00
|
|
|
DocumentObserver * instance;
|
2004-12-21 12:38:52 +00:00
|
|
|
QMap< int, int > pageMemory;
|
|
|
|
int totalMemory;
|
|
|
|
// public constructor: initialize data
|
2005-01-09 23:37:07 +00:00
|
|
|
ObserverData( DocumentObserver * obs ) : instance( obs ), totalMemory( 0 ) {};
|
2004-09-08 12:41:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define foreachObserver( cmd ) {\
|
2004-12-21 12:38:52 +00:00
|
|
|
QMap< int, ObserverData * >::iterator it = d->observers.begin(), end = d->observers.end();\
|
2005-01-09 23:37:07 +00:00
|
|
|
for ( ; it != end ; ++ it ) { (*it)->instance-> cmd ; } }
|
2004-12-21 12:38:52 +00:00
|
|
|
|
2004-09-08 12:41:14 +00:00
|
|
|
|
|
|
|
KPDFDocument::KPDFDocument()
|
2004-12-10 16:04:45 +00:00
|
|
|
: generator( 0 ), d( new KPDFDocumentPrivate )
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-09-17 17:58:42 +00:00
|
|
|
d->searchPage = -1;
|
2004-12-24 10:24:10 +00:00
|
|
|
d->memCheckTimer = new QTimer( this );
|
|
|
|
connect( d->memCheckTimer, SIGNAL( timeout() ), this, SLOT( slotCheckMemory() ) );
|
2005-01-03 00:28:46 +00:00
|
|
|
d->saveBookmarksTimer = new QTimer( this );
|
2005-01-03 00:45:47 +00:00
|
|
|
connect( d->saveBookmarksTimer, SIGNAL( timeout() ), this, SLOT( saveDocumentInfo() ) );
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
2004-09-12 23:19:18 +00:00
|
|
|
|
2004-09-08 12:41:14 +00:00
|
|
|
KPDFDocument::~KPDFDocument()
|
|
|
|
{
|
2004-09-17 17:58:42 +00:00
|
|
|
closeDocument();
|
2004-09-08 12:41:14 +00:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2004-10-27 14:07:24 +00:00
|
|
|
|
2004-09-17 17:58:42 +00:00
|
|
|
bool KPDFDocument::openDocument( const QString & docFile )
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
|
|
|
// docFile is always local so we can use QFile on it
|
|
|
|
QFile fileReadTest( docFile );
|
|
|
|
if ( !fileReadTest.open( IO_ReadOnly ) )
|
2005-01-03 00:28:46 +00:00
|
|
|
{
|
2005-01-13 11:03:48 +00:00
|
|
|
d->docFileName = QString::null;
|
2004-09-08 12:41:14 +00:00
|
|
|
return false;
|
2005-01-03 00:28:46 +00:00
|
|
|
}
|
2005-01-13 11:03:48 +00:00
|
|
|
// determine the related "xml document-info" filename
|
|
|
|
d->docFileName = docFile;
|
|
|
|
QString fn = docFile.contains('/') ? docFile.section('/', -1, -1) : docFile;
|
|
|
|
fn = "kpdf/" + QString::number(fileReadTest.size()) + "." + fn + ".xml";
|
2004-09-08 12:41:14 +00:00
|
|
|
fileReadTest.close();
|
2005-01-13 11:03:48 +00:00
|
|
|
d->xmlFileName = locateLocal( "data", fn );
|
2004-09-08 12:41:14 +00:00
|
|
|
|
2004-12-21 12:38:52 +00:00
|
|
|
// create the generator based on the file's mimetype
|
|
|
|
KMimeType::Ptr mime = KMimeType::findByPath( docFile );
|
|
|
|
QString mimeName = mime->name();
|
|
|
|
if ( mimeName == "application/pdf" )
|
|
|
|
generator = new PDFGenerator();
|
2004-12-22 18:21:36 +00:00
|
|
|
else if ( mimeName == "application/postscript" )
|
|
|
|
kdError() << "PS generator not available" << endl;
|
2004-12-21 12:38:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
kdWarning() << "Unknown mimetype '" << mimeName << "'." << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-22 18:21:36 +00:00
|
|
|
// get notification of completed jobs
|
|
|
|
connect( generator, SIGNAL( contentsChanged( int, int ) ),
|
|
|
|
this, SLOT( slotGeneratedContents( int, int ) ) );
|
2004-12-21 12:38:52 +00:00
|
|
|
|
2005-01-10 13:43:44 +00:00
|
|
|
// 1. load Document (and set busy cursor while loading)
|
|
|
|
QApplication::setOverrideCursor( waitCursor );
|
2004-12-10 16:04:45 +00:00
|
|
|
bool openOk = generator->loadDocument( docFile, pages_vector );
|
2005-01-10 13:43:44 +00:00
|
|
|
QApplication::restoreOverrideCursor();
|
2005-01-03 15:51:05 +00:00
|
|
|
if ( !openOk || pages_vector.size() <= 0 )
|
|
|
|
return openOk;
|
2005-01-03 00:45:47 +00:00
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// 2. load Additional Data (our bookmarks and metadata) about the document
|
2005-01-03 00:45:47 +00:00
|
|
|
loadDocumentInfo();
|
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// 3. setup observers inernal lists and data
|
|
|
|
processPageList( true );
|
|
|
|
|
|
|
|
// 4. set initial page (restoring previous page saved in xml)
|
2005-01-09 23:37:07 +00:00
|
|
|
DocumentViewport loadedViewport = d->viewport;
|
|
|
|
if ( loadedViewport.pageNumber < 0 )
|
|
|
|
loadedViewport.pageNumber = 0;
|
|
|
|
else
|
|
|
|
d->viewport = DocumentViewport();
|
|
|
|
setViewport( loadedViewport );
|
2005-01-03 15:51:05 +00:00
|
|
|
|
2005-01-03 00:45:47 +00:00
|
|
|
// start bookmark saver timer
|
|
|
|
d->saveBookmarksTimer->start( 5 * 60 * 1000 );
|
|
|
|
|
|
|
|
// start memory check timer
|
|
|
|
d->memCheckTimer->start( 2000 );
|
|
|
|
|
2004-09-08 12:41:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-17 17:58:42 +00:00
|
|
|
void KPDFDocument::closeDocument()
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2005-01-03 15:51:05 +00:00
|
|
|
// save document info if a document is still opened
|
|
|
|
if ( generator && pages_vector.size() > 0 )
|
|
|
|
saveDocumentInfo();
|
2005-01-03 00:28:46 +00:00
|
|
|
|
2004-12-24 10:24:10 +00:00
|
|
|
// stop memory check timer
|
|
|
|
d->memCheckTimer->stop();
|
|
|
|
|
2005-01-03 00:24:59 +00:00
|
|
|
// delete contents generator
|
|
|
|
delete generator;
|
|
|
|
generator = 0;
|
|
|
|
|
|
|
|
// send an empty list to observers (to free their data)
|
2005-01-09 23:37:07 +00:00
|
|
|
foreachObserver( notifySetup( QValueVector< KPDFPage * >(), true ) );
|
2005-01-03 00:24:59 +00:00
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
// delete pages and clear 'pages_vector' container
|
|
|
|
for ( uint i = 0; i < pages_vector.count() ; i++ )
|
|
|
|
delete pages_vector[i];
|
|
|
|
pages_vector.clear();
|
|
|
|
|
2004-12-22 18:21:36 +00:00
|
|
|
// clear memory management data
|
|
|
|
QMap< int, ObserverData * >::iterator oIt = d->observers.begin(), oEnd = d->observers.end();
|
|
|
|
for ( ; oIt != oEnd ; ++oIt )
|
|
|
|
{
|
|
|
|
ObserverData * observerData = *oIt;
|
|
|
|
observerData->pageMemory.clear();
|
|
|
|
observerData->totalMemory = 0;
|
|
|
|
}
|
|
|
|
|
2004-09-17 17:58:42 +00:00
|
|
|
// reset internal variables
|
2005-01-09 23:37:07 +00:00
|
|
|
d->viewport = DocumentViewport();
|
2004-09-17 17:58:42 +00:00
|
|
|
d->searchPage = -1;
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-09 13:25:40 +00:00
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
void KPDFDocument::addObserver( DocumentObserver * pObserver )
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-12-17 17:14:46 +00:00
|
|
|
// keep the pointer to the observer in a map
|
2004-12-21 12:38:52 +00:00
|
|
|
d->observers[ pObserver->observerId() ] = new ObserverData( pObserver );
|
2004-12-17 17:14:46 +00:00
|
|
|
|
|
|
|
// if the observer is added while a document is already opened, tell it
|
|
|
|
if ( !pages_vector.isEmpty() )
|
2005-01-09 23:37:07 +00:00
|
|
|
pObserver->notifySetup( pages_vector, true );
|
2004-12-17 17:14:46 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
void KPDFDocument::removeObserver( DocumentObserver * pObserver )
|
2004-12-17 17:14:46 +00:00
|
|
|
{
|
|
|
|
// remove observer from the map. it won't receive notifications anymore
|
2004-12-21 12:38:52 +00:00
|
|
|
if ( d->observers.contains( pObserver->observerId() ) )
|
|
|
|
{
|
2005-01-03 15:51:05 +00:00
|
|
|
// free observer's pixmap data
|
2004-12-21 12:38:52 +00:00
|
|
|
int observerId = pObserver->observerId();
|
|
|
|
QValueVector<KPDFPage*>::iterator it = pages_vector.begin(), end = pages_vector.end();
|
|
|
|
for ( ; it != end; ++it )
|
|
|
|
(*it)->deletePixmap( observerId );
|
2004-12-24 10:24:10 +00:00
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// delete observer storage info
|
2004-12-21 12:38:52 +00:00
|
|
|
delete d->observers[ observerId ];
|
|
|
|
d->observers.remove( observerId );
|
|
|
|
}
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2004-11-09 17:20:19 +00:00
|
|
|
void KPDFDocument::reparseConfig()
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
// reparse generator config and if something changed clear KPDFPages
|
|
|
|
if ( generator && generator->reparseConfig() )
|
2004-11-09 17:20:19 +00:00
|
|
|
{
|
2004-11-12 10:58:33 +00:00
|
|
|
// invalidate pixmaps and send reload signals to observers
|
2004-12-10 16:04:45 +00:00
|
|
|
QValueVector<KPDFPage*>::iterator it = pages_vector.begin(), end = pages_vector.end();
|
2004-11-12 10:58:33 +00:00
|
|
|
for ( ; it != end; ++it )
|
2004-12-10 16:04:45 +00:00
|
|
|
(*it)->deletePixmapsAndRects();
|
2005-01-09 23:37:07 +00:00
|
|
|
foreachObserver( notifyContentsCleared( DocumentObserver::Pixmap) );
|
2004-11-09 17:20:19 +00:00
|
|
|
}
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2004-11-09 17:20:19 +00:00
|
|
|
|
2004-12-11 17:25:03 +00:00
|
|
|
const DocumentInfo * KPDFDocument::documentInfo() const
|
2004-12-05 22:47:32 +00:00
|
|
|
{
|
2004-12-11 17:25:03 +00:00
|
|
|
return generator ? generator->documentInfo() : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DocumentSynopsis * KPDFDocument::documentSynopsis() const
|
|
|
|
{
|
|
|
|
return generator ? generator->documentSynopsis() : NULL;
|
2004-12-05 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
const KPDFPage * KPDFDocument::page( uint n ) const
|
2004-12-05 22:47:32 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
return ( n < pages_vector.count() ) ? pages_vector[n] : 0;
|
2004-12-05 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
const DocumentViewport & KPDFDocument::viewport() const
|
|
|
|
{
|
|
|
|
return d->viewport;
|
|
|
|
}
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
uint KPDFDocument::currentPage() const
|
2004-12-05 22:47:32 +00:00
|
|
|
{
|
2005-01-09 23:37:07 +00:00
|
|
|
return d->viewport.pageNumber;
|
2004-12-05 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
uint KPDFDocument::pages() const
|
2004-12-05 22:47:32 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
return pages_vector.size();
|
2004-12-05 22:47:32 +00:00
|
|
|
}
|
|
|
|
|
2004-11-09 17:20:19 +00:00
|
|
|
bool KPDFDocument::okToPrint() const
|
2004-09-09 13:25:40 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
return generator ? generator->allowed( Generator::Print ) : false;
|
2004-09-09 13:25:40 +00:00
|
|
|
}
|
|
|
|
|
2005-01-07 13:07:29 +00:00
|
|
|
QString KPDFDocument::getMetaData( const QString & key, const QString & option ) const
|
2005-01-01 15:44:44 +00:00
|
|
|
{
|
2005-01-07 13:07:29 +00:00
|
|
|
return generator ? generator->getMetaData( key, option ) : QString();
|
2005-01-01 15:44:44 +00:00
|
|
|
}
|
2004-11-09 17:20:19 +00:00
|
|
|
|
2004-12-28 18:50:11 +00:00
|
|
|
void KPDFDocument::requestPixmaps( const QValueList< PixmapRequest * > & requests, bool async )
|
2004-09-15 20:45:00 +00:00
|
|
|
{
|
2004-12-22 18:21:36 +00:00
|
|
|
if ( !generator )
|
2004-09-15 20:45:00 +00:00
|
|
|
return;
|
|
|
|
|
2004-12-22 18:21:36 +00:00
|
|
|
QValueList< PixmapRequest * >::const_iterator rIt = requests.begin(), rEnd = requests.end();
|
|
|
|
for ( ; rIt != rEnd; ++rIt )
|
2004-12-21 12:38:52 +00:00
|
|
|
{
|
2004-12-22 18:21:36 +00:00
|
|
|
// set the 'page field' (see PixmapRequest) and check if request is valid
|
|
|
|
PixmapRequest * request = *rIt;
|
|
|
|
request->page = pages_vector[ request->pageNumber ];
|
|
|
|
if ( !request->page || request->page->width() < 1 || request->page->height() < 1 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// 1. Update statistics (pageMemory / totalMemory) adding this pixmap
|
|
|
|
ObserverData * obs = d->observers[ request->id ];
|
2004-12-24 10:24:10 +00:00
|
|
|
int pageNumber = request->pageNumber;
|
2004-12-22 18:21:36 +00:00
|
|
|
if ( obs->pageMemory.contains( pageNumber ) )
|
|
|
|
obs->totalMemory -= obs->pageMemory[ pageNumber ];
|
|
|
|
int pixmapMemory = 4 * request->width * request->height / 1024;
|
|
|
|
obs->pageMemory[ pageNumber ] = pixmapMemory;
|
|
|
|
obs->totalMemory += pixmapMemory;
|
|
|
|
|
|
|
|
// 2. Perform pre-cleaning if needed
|
|
|
|
mCleanupMemory( request->id );
|
|
|
|
|
|
|
|
// 3. Enqueue to Generator (that takes ownership of request)
|
2004-12-28 18:50:11 +00:00
|
|
|
generator->requestPixmap( request, Settings::disableThreading() ? false : async );
|
2004-12-21 12:38:52 +00:00
|
|
|
}
|
2004-09-15 20:45:00 +00:00
|
|
|
}
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
void KPDFDocument::requestTextPage( uint page )
|
2004-10-09 08:10:56 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
KPDFPage * kp = pages_vector[ page ];
|
|
|
|
if ( !generator || !kp )
|
|
|
|
return;
|
|
|
|
|
2004-12-21 12:38:52 +00:00
|
|
|
// Memory management for TextPages
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
generator->requestTextPage( kp );
|
2004-10-09 08:10:56 +00:00
|
|
|
}
|
2005-01-09 23:37:07 +00:00
|
|
|
/* REFERENCE IMPLEMENTATION: better calling setViewport from other code
|
|
|
|
void KPDFDocument::setNextPage()
|
|
|
|
{
|
|
|
|
// advance page and set viewport on observers
|
|
|
|
if ( d->viewport.pageNumber < (int)pages_vector.count() - 1 )
|
|
|
|
setViewport( DocumentViewport( d->viewport.pageNumber + 1 ) );
|
|
|
|
}
|
2004-10-09 08:10:56 +00:00
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
void KPDFDocument::setPrevPage()
|
2004-09-09 13:25:40 +00:00
|
|
|
{
|
2005-01-09 23:37:07 +00:00
|
|
|
// go to previous page and set viewport on observers
|
|
|
|
if ( d->viewport.pageNumber > 0 )
|
|
|
|
setViewport( DocumentViewport( d->viewport.pageNumber - 1 ) );
|
|
|
|
}
|
|
|
|
*/
|
2005-01-10 13:43:44 +00:00
|
|
|
void KPDFDocument::setViewportPage( int page, int id )
|
2005-01-09 23:37:07 +00:00
|
|
|
{
|
|
|
|
// clamp page in range [0 ... numPages-1]
|
2004-10-10 13:21:30 +00:00
|
|
|
if ( page < 0 )
|
2004-10-09 08:10:56 +00:00
|
|
|
page = 0;
|
2004-12-10 16:04:45 +00:00
|
|
|
else if ( page > (int)pages_vector.count() )
|
|
|
|
page = pages_vector.count() - 1;
|
2005-01-09 23:37:07 +00:00
|
|
|
|
|
|
|
// make a viewport from the page and broadcast it
|
2005-01-10 13:43:44 +00:00
|
|
|
setViewport( DocumentViewport( page ), id );
|
2005-01-09 23:37:07 +00:00
|
|
|
}
|
|
|
|
|
2005-01-10 13:43:44 +00:00
|
|
|
void KPDFDocument::setViewport( const DocumentViewport & viewport, int id )
|
2005-01-09 23:37:07 +00:00
|
|
|
{
|
|
|
|
// if already broadcasted, don't redo it
|
|
|
|
if ( viewport == d->viewport )
|
|
|
|
kdDebug() << "setViewport with the same viewport." << endl;
|
|
|
|
|
2005-01-10 13:43:44 +00:00
|
|
|
// set internal viewport
|
2005-01-09 23:37:07 +00:00
|
|
|
d->viewport = viewport;
|
2005-01-10 13:43:44 +00:00
|
|
|
|
|
|
|
// notify change to all other (different from id) viewports
|
|
|
|
QMap< int, ObserverData * >::iterator it = d->observers.begin(), end = d->observers.end();
|
|
|
|
for ( ; it != end ; ++ it )
|
|
|
|
if ( it.key() != id )
|
|
|
|
(*it)->instance->notifyViewportChanged();
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 23:19:04 +00:00
|
|
|
bool KPDFDocument::findText( const QString & string, bool keepCase, bool findAhead )
|
2004-09-16 21:04:49 +00:00
|
|
|
{
|
2004-09-17 17:58:42 +00:00
|
|
|
// turn selection drawing off on filtered pages
|
|
|
|
if ( !d->filterText.isEmpty() )
|
|
|
|
unHilightPages();
|
|
|
|
|
|
|
|
// save params for the 'find next' case
|
|
|
|
if ( !string.isEmpty() )
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-09-17 17:58:42 +00:00
|
|
|
d->searchText = string;
|
|
|
|
d->searchCase = keepCase;
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-16 21:04:49 +00:00
|
|
|
// continue checking last SearchPage first (if it is the current page)
|
2005-01-09 23:37:07 +00:00
|
|
|
int currentPage = d->viewport.pageNumber;
|
2004-12-10 16:04:45 +00:00
|
|
|
int pageCount = pages_vector.count();
|
2004-09-17 17:58:42 +00:00
|
|
|
KPDFPage * foundPage = 0,
|
2004-12-10 16:04:45 +00:00
|
|
|
* lastPage = (d->searchPage > -1) ? pages_vector[ d->searchPage ] : 0;
|
2004-09-17 17:58:42 +00:00
|
|
|
if ( lastPage && d->searchPage == currentPage )
|
2005-01-14 23:19:04 +00:00
|
|
|
if ( lastPage->hasText( d->searchText, d->searchCase, findAhead ) )
|
2004-09-17 17:58:42 +00:00
|
|
|
foundPage = lastPage;
|
2004-09-16 21:04:49 +00:00
|
|
|
else
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-11-06 00:56:55 +00:00
|
|
|
lastPage->clearAttribute( KPDFPage::Highlight );
|
2004-09-16 21:04:49 +00:00
|
|
|
currentPage++;
|
|
|
|
pageCount--;
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2004-09-16 21:04:49 +00:00
|
|
|
if ( !foundPage )
|
|
|
|
// loop through the whole document
|
|
|
|
for ( int i = 0; i < pageCount; i++ )
|
2004-09-13 08:51:36 +00:00
|
|
|
{
|
2004-09-16 21:04:49 +00:00
|
|
|
if ( currentPage >= pageCount )
|
|
|
|
{
|
2005-01-14 23:19:04 +00:00
|
|
|
if ( !findAhead && KMessageBox::questionYesNo(0, i18n("End of document reached.\nContinue from the beginning?")) == KMessageBox::Yes )
|
2004-09-16 21:04:49 +00:00
|
|
|
currentPage = 0;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2004-12-10 16:04:45 +00:00
|
|
|
KPDFPage * page = pages_vector[ currentPage ];
|
2004-09-16 21:04:49 +00:00
|
|
|
if ( !page->hasSearchPage() )
|
2004-10-09 08:10:56 +00:00
|
|
|
requestTextPage( page->number() );
|
2004-09-17 17:58:42 +00:00
|
|
|
if ( page->hasText( d->searchText, d->searchCase, true ) )
|
2004-09-16 21:04:49 +00:00
|
|
|
{
|
|
|
|
foundPage = page;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
currentPage++;
|
2004-09-13 08:51:36 +00:00
|
|
|
}
|
|
|
|
|
2004-09-16 21:04:49 +00:00
|
|
|
if ( foundPage )
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-09-17 17:58:42 +00:00
|
|
|
int pageNumber = foundPage->number();
|
|
|
|
d->searchPage = pageNumber;
|
2004-11-06 00:56:55 +00:00
|
|
|
foundPage->setAttribute( KPDFPage::Highlight );
|
2005-01-10 13:43:44 +00:00
|
|
|
// move the viewport to show the searched word centered
|
|
|
|
DocumentViewport searchViewport( pageNumber );
|
|
|
|
const QPoint & center = foundPage->getLastSearchCenter();
|
|
|
|
searchViewport.reCenter.enabled = true;
|
|
|
|
searchViewport.reCenter.normalizedCenterX = (double)center.x() / foundPage->width();
|
|
|
|
searchViewport.reCenter.normalizedCenterY = (double)center.y() / foundPage->height();
|
|
|
|
setViewport( searchViewport );
|
|
|
|
// notify all observers about hilighting chages
|
2005-01-09 23:37:07 +00:00
|
|
|
foreachObserver( notifyPageChanged( pageNumber, DocumentObserver::Highlights ) );
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
2005-01-14 23:19:04 +00:00
|
|
|
else if ( !findAhead )
|
2004-09-17 17:58:42 +00:00
|
|
|
KMessageBox::information( 0, i18n("No matches found for '%1'.").arg(d->searchText) );
|
2005-01-14 23:19:04 +00:00
|
|
|
return foundPage;
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2004-11-09 17:20:19 +00:00
|
|
|
void KPDFDocument::findTextAll( const QString & pattern, bool keepCase )
|
|
|
|
{
|
2004-12-13 18:21:37 +00:00
|
|
|
// if pattern is null, clear 'hilighted' attribute in all pages
|
|
|
|
if ( pattern.isEmpty() )
|
|
|
|
unHilightPages();
|
|
|
|
|
2005-01-10 13:43:44 +00:00
|
|
|
// cache search pattern
|
2004-11-09 17:20:19 +00:00
|
|
|
d->filterText = pattern;
|
|
|
|
d->filterCase = keepCase;
|
2005-01-10 13:43:44 +00:00
|
|
|
// set the busy cursor globally on the application
|
|
|
|
QApplication::setOverrideCursor( waitCursor );
|
|
|
|
// perform a linear search/mark
|
2004-11-09 17:20:19 +00:00
|
|
|
processPageList( false );
|
2005-01-10 13:43:44 +00:00
|
|
|
// reset cursor to previous shape
|
|
|
|
QApplication::restoreOverrideCursor();
|
2004-11-09 17:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KPDFDocument::toggleBookmark( int n )
|
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
KPDFPage * page = ( n < (int)pages_vector.count() ) ? pages_vector[ n ] : 0;
|
2004-11-09 17:20:19 +00:00
|
|
|
if ( page )
|
|
|
|
{
|
|
|
|
page->toggleAttribute( KPDFPage::Bookmark );
|
2005-01-09 23:37:07 +00:00
|
|
|
foreachObserver( notifyPageChanged( n, DocumentObserver::Bookmark ) );
|
2004-11-09 17:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KPDFDocument::processLink( const KPDFLink * link )
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2004-10-06 00:05:49 +00:00
|
|
|
if ( !link )
|
|
|
|
return;
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
switch( link->linkType() )
|
2004-10-06 00:05:49 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
case KPDFLink::Goto: {
|
|
|
|
const KPDFLinkGoto * go = static_cast< const KPDFLinkGoto * >( link );
|
2005-01-09 23:37:07 +00:00
|
|
|
DocumentViewport destVp = go->destViewport();
|
2004-12-10 16:04:45 +00:00
|
|
|
|
|
|
|
// first open filename if link is pointing outside this document
|
|
|
|
if ( go->isExternal() && !openRelativeFile( go->fileName() ) )
|
2004-10-06 00:05:49 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
kdWarning() << "Link: Error opening '" << go->fileName() << "'." << endl;
|
2004-10-06 00:05:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
// note: if external file is opened, 'link' doesn't exist anymore!
|
2005-01-09 23:37:07 +00:00
|
|
|
setViewport( destVp );
|
2004-12-10 16:04:45 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case KPDFLink::Execute: {
|
|
|
|
const KPDFLinkExecute * exe = static_cast< const KPDFLinkExecute * >( link );
|
|
|
|
QString fileName = exe->fileName();
|
|
|
|
if ( fileName.endsWith( ".pdf" ) || fileName.endsWith( ".PDF" ) )
|
2004-10-09 08:10:56 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
openRelativeFile( fileName );
|
|
|
|
return;
|
2004-10-09 08:10:56 +00:00
|
|
|
}
|
2004-12-10 16:04:45 +00:00
|
|
|
|
|
|
|
// Albert: the only pdf i have that has that kind of link don't define
|
|
|
|
// an application and use the fileName as the file to open
|
|
|
|
fileName = giveAbsolutePath( fileName );
|
|
|
|
KMimeType::Ptr mime = KMimeType::findByPath( fileName );
|
|
|
|
// Check executables
|
|
|
|
if ( KRun::isExecutableFile( fileName, mime->name() ) )
|
2004-10-09 08:10:56 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
// Don't have any pdf that uses this code path, just a guess on how it should work
|
|
|
|
if ( !exe->parameters().isEmpty() )
|
|
|
|
{
|
|
|
|
fileName = giveAbsolutePath( exe->parameters() );
|
|
|
|
mime = KMimeType::findByPath( fileName );
|
|
|
|
if ( KRun::isExecutableFile( fileName, mime->name() ) )
|
|
|
|
{
|
|
|
|
// this case is a link pointing to an executable with a parameter
|
|
|
|
// that also is an executable, possibly a hand-crafted pdf
|
|
|
|
KMessageBox::information( 0, i18n("The pdf file is trying to execute an external application and for your safety kpdf does not allow that.") );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this case is a link pointing to an executable with no parameters
|
|
|
|
// core developers find unacceptable executing it even after asking the user
|
|
|
|
KMessageBox::information( 0, i18n("The pdf file is trying to execute an external application and for your safety kpdf does not allow that.") );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KService::Ptr ptr = KServiceTypeProfile::preferredService( mime->name(), "Application" );
|
|
|
|
if ( ptr )
|
|
|
|
{
|
|
|
|
KURL::List lst;
|
|
|
|
lst.append( fileName );
|
|
|
|
KRun::run( *ptr, lst );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
KMessageBox::information( 0, i18n( "No application found for opening file of mimetype %1." ).arg( mime->name() ) );
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case KPDFLink::Action: {
|
|
|
|
const KPDFLinkAction * action = static_cast< const KPDFLinkAction * >( link );
|
|
|
|
switch( action->actionType() )
|
|
|
|
{
|
|
|
|
case KPDFLinkAction::PageFirst:
|
2005-01-09 23:37:07 +00:00
|
|
|
setViewportPage( 0 );
|
2004-12-10 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case KPDFLinkAction::PagePrev:
|
2005-01-09 23:37:07 +00:00
|
|
|
if ( d->viewport.pageNumber > 0 )
|
|
|
|
setViewportPage( d->viewport.pageNumber - 1 );
|
2004-12-10 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case KPDFLinkAction::PageNext:
|
2005-01-09 23:37:07 +00:00
|
|
|
if ( d->viewport.pageNumber < (int)pages_vector.count() - 1 )
|
|
|
|
setViewportPage( d->viewport.pageNumber + 1 );
|
2004-12-10 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case KPDFLinkAction::PageLast:
|
2005-01-09 23:37:07 +00:00
|
|
|
setViewportPage( pages_vector.count() - 1 );
|
2004-12-10 16:04:45 +00:00
|
|
|
break;
|
|
|
|
case KPDFLinkAction::HistoryBack:
|
|
|
|
{} //TODO
|
|
|
|
break;
|
|
|
|
case KPDFLinkAction::HistoryForward:
|
|
|
|
{} //TODO
|
|
|
|
break;
|
|
|
|
case KPDFLinkAction::Quit:
|
|
|
|
kapp->quit();
|
|
|
|
break;
|
2005-01-01 21:50:34 +00:00
|
|
|
case KPDFLinkAction::Find:
|
|
|
|
emit linkFind();
|
|
|
|
break;
|
|
|
|
case KPDFLinkAction::GoToPage:
|
|
|
|
emit linkGoToPage();
|
|
|
|
break;
|
2004-12-10 16:04:45 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case KPDFLink::Browse: {
|
|
|
|
const KPDFLinkBrowse * browse = static_cast< const KPDFLinkBrowse * >( link );
|
2004-12-17 17:14:46 +00:00
|
|
|
// get service for web browsing TODO: check for "mailto:" links
|
2004-12-10 16:04:45 +00:00
|
|
|
KService::Ptr ptr = KServiceTypeProfile::preferredService("text/html", "Application");
|
|
|
|
KURL::List lst;
|
|
|
|
// append 'url' parameter to the service and run it
|
|
|
|
lst.append( browse->url() );
|
|
|
|
KRun::run( *ptr, lst );
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case KPDFLink::Movie:
|
2005-01-10 13:43:44 +00:00
|
|
|
//const KPDFLinkMovie * browse = static_cast< const KPDFLinkMovie * >( link );
|
2004-12-10 16:04:45 +00:00
|
|
|
// TODO this
|
|
|
|
break;
|
2004-10-06 00:05:49 +00:00
|
|
|
}
|
2004-09-09 13:25:40 +00:00
|
|
|
}
|
2004-11-09 17:20:19 +00:00
|
|
|
|
2004-12-10 16:04:45 +00:00
|
|
|
bool KPDFDocument::print( KPrinter &printer )
|
2004-11-09 17:20:19 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
return generator ? generator->print( printer ) : false;
|
2004-11-09 17:20:19 +00:00
|
|
|
}
|
2004-12-10 16:04:45 +00:00
|
|
|
|
2004-09-08 12:41:14 +00:00
|
|
|
|
2004-12-22 18:21:36 +00:00
|
|
|
void KPDFDocument::mCleanupMemory( int observerId )
|
|
|
|
{
|
|
|
|
// get observer data for given id
|
|
|
|
ObserverData * obs = d->observers[ observerId ];
|
|
|
|
|
|
|
|
// choose memory parameters based on configuration profile
|
2004-12-24 10:24:10 +00:00
|
|
|
int clipValue = 0;
|
2004-12-22 18:21:36 +00:00
|
|
|
int memoryToFree = 0;
|
|
|
|
switch ( Settings::memoryLevel() )
|
|
|
|
{
|
|
|
|
case Settings::EnumMemoryLevel::Low:
|
|
|
|
memoryToFree = obs->totalMemory;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Settings::EnumMemoryLevel::Normal:
|
2004-12-24 10:24:10 +00:00
|
|
|
clipValue = obs->totalMemory - mFreeMemory() / 3;
|
|
|
|
if ( observerId == THUMBNAILS_ID )
|
|
|
|
memoryToFree = obs->totalMemory - mTotalMemory() / 20;
|
|
|
|
else
|
|
|
|
memoryToFree = obs->totalMemory - mTotalMemory() / 5;
|
2004-12-22 18:21:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Settings::EnumMemoryLevel::Aggressive:
|
2004-12-24 10:24:10 +00:00
|
|
|
clipValue = obs->totalMemory - mFreeMemory() / 2;
|
2004-12-22 18:21:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-12-24 10:24:10 +00:00
|
|
|
if ( clipValue > memoryToFree )
|
|
|
|
memoryToFree = clipValue;
|
|
|
|
|
2005-01-07 13:13:58 +00:00
|
|
|
if ( memoryToFree <= 0 )
|
|
|
|
return;
|
|
|
|
|
2004-12-22 18:21:36 +00:00
|
|
|
// free memory. remove older data until we free enough memory
|
|
|
|
int freed = 0;
|
2005-01-07 13:13:58 +00:00
|
|
|
QMap< int, int >::iterator it = obs->pageMemory.begin(), end = obs->pageMemory.end();
|
|
|
|
while ( (it != end) && (memoryToFree > 0) )
|
2004-12-22 18:21:36 +00:00
|
|
|
{
|
2005-01-07 13:13:58 +00:00
|
|
|
int pageNumber = it.key();
|
2005-01-09 23:37:07 +00:00
|
|
|
if ( obs->instance->canUnloadPixmap( pageNumber ) )
|
2004-12-22 18:21:36 +00:00
|
|
|
{
|
2005-01-07 13:13:58 +00:00
|
|
|
// copy iterator to avoid invalidation on map->remove( it )
|
|
|
|
QMap< int, int >::iterator i( it );
|
|
|
|
++it;
|
|
|
|
// update mem stats
|
|
|
|
memoryToFree -= i.data();
|
|
|
|
obs->totalMemory -= i.data();
|
|
|
|
obs->pageMemory.remove( i );
|
|
|
|
// delete pixmap
|
|
|
|
pages_vector[ pageNumber ]->deletePixmap( observerId );
|
|
|
|
freed++;
|
|
|
|
} else
|
2004-12-22 18:21:36 +00:00
|
|
|
++it;
|
|
|
|
}
|
2004-12-28 16:06:11 +00:00
|
|
|
//kdDebug() << "Id:" << observerId << " [" << obs->totalMemory << "kB] Removed " << freed << " pages. " << obs->pageMemory.count() << " pages kept in memory." << endl;
|
2004-12-22 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
2004-12-21 12:38:52 +00:00
|
|
|
int KPDFDocument::mTotalMemory()
|
|
|
|
{
|
2004-12-22 18:21:36 +00:00
|
|
|
static int cachedValue = 0;
|
|
|
|
if ( cachedValue )
|
|
|
|
return cachedValue;
|
|
|
|
|
2004-12-21 12:38:52 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
// if /proc/meminfo doesn't exist, return 128MB
|
|
|
|
QFile memFile( "/proc/meminfo" );
|
|
|
|
if ( !memFile.open( IO_ReadOnly ) )
|
2004-12-22 18:21:36 +00:00
|
|
|
return (cachedValue = 131072);
|
2004-12-21 12:38:52 +00:00
|
|
|
|
|
|
|
// read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
|
|
|
|
// and 'Cached' fields. consider swapped memory as used memory.
|
|
|
|
QTextStream readStream( &memFile );
|
|
|
|
while ( !readStream.atEnd() )
|
|
|
|
{
|
|
|
|
QString entry = readStream.readLine();
|
|
|
|
if ( entry.startsWith( "MemTotal:" ) )
|
2004-12-22 18:21:36 +00:00
|
|
|
return (cachedValue = entry.section( ' ', -2, -2 ).toInt());
|
2004-12-21 12:38:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-12-22 18:21:36 +00:00
|
|
|
return (cachedValue = 131072);
|
2004-12-21 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int KPDFDocument::mFreeMemory()
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2004-12-24 10:24:10 +00:00
|
|
|
// if /proc/meminfo doesn't exist, return MEMORY FULL
|
2004-12-21 12:38:52 +00:00
|
|
|
QFile memFile( "/proc/meminfo" );
|
|
|
|
if ( !memFile.open( IO_ReadOnly ) )
|
2004-12-24 10:24:10 +00:00
|
|
|
return 0;
|
2004-12-21 12:38:52 +00:00
|
|
|
|
|
|
|
// read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
|
|
|
|
// and 'Cached' fields. consider swapped memory as used memory.
|
|
|
|
int memoryFree = 0;
|
|
|
|
QString entry;
|
|
|
|
QTextStream readStream( &memFile );
|
|
|
|
while ( !readStream.atEnd() )
|
|
|
|
{
|
|
|
|
entry = readStream.readLine();
|
|
|
|
if ( entry.startsWith( "MemFree:" ) ||
|
|
|
|
entry.startsWith( "Buffers:" ) ||
|
|
|
|
entry.startsWith( "Cached:" ) ||
|
|
|
|
entry.startsWith( "SwapFree:" ) )
|
|
|
|
memoryFree += entry.section( ' ', -2, -2 ).toInt();
|
|
|
|
if ( entry.startsWith( "SwapTotal:" ) )
|
|
|
|
memoryFree -= entry.section( ' ', -2, -2 ).toInt();
|
|
|
|
}
|
|
|
|
memFile.close();
|
|
|
|
return memoryFree;
|
|
|
|
#else
|
2004-12-24 10:24:10 +00:00
|
|
|
// tell the memory is full.. will act as in LOW profile
|
|
|
|
return 0;
|
2004-12-21 12:38:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-01-03 00:28:46 +00:00
|
|
|
void KPDFDocument::loadDocumentInfo()
|
2005-01-03 15:51:05 +00:00
|
|
|
// note: load data and stores it internally (document or pages). observers
|
|
|
|
// are still uninitialized at this point so don't access them
|
2005-01-03 00:28:46 +00:00
|
|
|
{
|
2005-01-13 11:03:48 +00:00
|
|
|
//kdDebug() << "Using '" << d->xmlFileName << "' as document info file." << endl;
|
|
|
|
QFile infoFile( d->xmlFileName );
|
2005-01-03 00:28:46 +00:00
|
|
|
if (infoFile.exists() && infoFile.open( IO_ReadOnly ) )
|
|
|
|
{
|
2005-01-03 15:51:05 +00:00
|
|
|
// Load DOM from XML file
|
2005-01-03 00:28:46 +00:00
|
|
|
QDomDocument doc( "documentInfo" );
|
|
|
|
if ( !doc.setContent( &infoFile ) )
|
|
|
|
{
|
|
|
|
kdDebug() << "Could not set content" << endl;
|
|
|
|
infoFile.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDomElement root = doc.documentElement();
|
|
|
|
if (root.tagName() != "documentInfo") return;
|
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// Parse the DOM tree
|
|
|
|
QDomNode topLevelNode = root.firstChild();
|
|
|
|
while ( topLevelNode.isElement() )
|
2005-01-03 00:28:46 +00:00
|
|
|
{
|
2005-01-03 15:51:05 +00:00
|
|
|
QString catName = topLevelNode.toElement().tagName();
|
2005-01-03 00:28:46 +00:00
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// Get bookmarks list from DOM
|
|
|
|
if ( catName == "bookmarkList" )
|
|
|
|
{
|
|
|
|
QDomNode n = topLevelNode.firstChild();
|
|
|
|
QDomElement e;
|
|
|
|
int pageNumber;
|
|
|
|
bool ok;
|
|
|
|
while ( n.isElement() )
|
|
|
|
{
|
|
|
|
e = n.toElement();
|
|
|
|
if (e.tagName() == "page")
|
|
|
|
{
|
|
|
|
pageNumber = e.text().toInt(&ok);
|
|
|
|
if ( ok && pageNumber >= 0 && pageNumber < (int)pages_vector.count() )
|
|
|
|
pages_vector[ pageNumber ]->setAttribute( KPDFPage::Bookmark );
|
|
|
|
}
|
|
|
|
n = n.nextSibling();
|
|
|
|
}
|
2005-01-03 00:28:46 +00:00
|
|
|
}
|
2005-01-03 15:51:05 +00:00
|
|
|
// Get 'general info' from the DOM
|
|
|
|
else if ( catName == "generalInfo" )
|
|
|
|
{
|
|
|
|
QDomNode infoNode = topLevelNode.firstChild();
|
|
|
|
while ( infoNode.isElement() )
|
|
|
|
{
|
|
|
|
QDomElement infoElement = infoNode.toElement();
|
|
|
|
if ( infoElement.tagName() == "activePage" )
|
|
|
|
{
|
2005-01-09 23:37:07 +00:00
|
|
|
if ( infoElement.hasAttribute( "viewport" ) )
|
|
|
|
d->viewport = DocumentViewport( infoElement.attribute( "viewport" ) );
|
2005-01-03 15:51:05 +00:00
|
|
|
}
|
|
|
|
infoNode = infoNode.nextSibling();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
topLevelNode = topLevelNode.nextSibling();
|
|
|
|
}
|
2005-01-03 00:28:46 +00:00
|
|
|
}
|
|
|
|
infoFile.close();
|
|
|
|
}
|
|
|
|
|
2004-10-17 18:40:02 +00:00
|
|
|
QString KPDFDocument::giveAbsolutePath( const QString & fileName )
|
|
|
|
{
|
2005-01-13 11:03:48 +00:00
|
|
|
if ( d->docFileName.isEmpty() )
|
2004-12-10 16:04:45 +00:00
|
|
|
return QString::null;
|
2004-10-17 18:40:02 +00:00
|
|
|
|
|
|
|
// convert the pdf fileName to absolute using current pdf path
|
2005-01-13 11:03:48 +00:00
|
|
|
QFileInfo currentInfo( d->docFileName );
|
2004-12-10 16:04:45 +00:00
|
|
|
return currentInfo.dir().absFilePath( fileName );
|
2004-10-17 18:40:02 +00:00
|
|
|
}
|
|
|
|
|
2004-10-06 00:05:49 +00:00
|
|
|
bool KPDFDocument::openRelativeFile( const QString & fileName )
|
|
|
|
{
|
2004-10-17 18:40:02 +00:00
|
|
|
QString absFileName = giveAbsolutePath( fileName );
|
|
|
|
if ( absFileName.isNull() )
|
2004-10-06 00:05:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
kdDebug() << "openDocument: '" << absFileName << "'" << endl;
|
|
|
|
|
|
|
|
// open the absolute filename
|
|
|
|
return openDocument( absFileName );
|
|
|
|
}
|
|
|
|
|
2004-09-17 17:58:42 +00:00
|
|
|
void KPDFDocument::processPageList( bool documentChanged )
|
2004-09-09 13:25:40 +00:00
|
|
|
{
|
2004-09-17 17:58:42 +00:00
|
|
|
if ( d->filterText.length() < 3 )
|
|
|
|
unHilightPages();
|
|
|
|
else
|
2004-09-16 21:27:34 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
uint pageCount = pages_vector.count();
|
2004-09-15 20:45:00 +00:00
|
|
|
for ( uint i = 0; i < pageCount ; i++ )
|
2004-09-16 21:27:34 +00:00
|
|
|
{
|
2004-12-10 16:04:45 +00:00
|
|
|
KPDFPage * page = pages_vector[ i ];
|
2004-11-06 00:56:55 +00:00
|
|
|
page->clearAttribute( KPDFPage::Highlight );
|
|
|
|
if ( d->filterText.length() > 2 )
|
2004-09-16 21:27:34 +00:00
|
|
|
{
|
|
|
|
if ( !page->hasSearchPage() )
|
2004-12-10 16:04:45 +00:00
|
|
|
requestTextPage( i );
|
2004-11-06 00:56:55 +00:00
|
|
|
if ( page->hasText( d->filterText, d->filterCase, true ) )
|
|
|
|
page->setAttribute( KPDFPage::Highlight );
|
2004-09-16 21:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-08 12:41:14 +00:00
|
|
|
|
|
|
|
// send the list to observers
|
2005-01-09 23:37:07 +00:00
|
|
|
foreachObserver( notifySetup( pages_vector, documentChanged ) );
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 23:19:04 +00:00
|
|
|
void KPDFDocument::unHilightPages(bool filteredOnly)
|
2004-09-08 12:41:14 +00:00
|
|
|
{
|
2005-01-14 23:19:04 +00:00
|
|
|
if ( filteredOnly && d->filterText.isEmpty() )
|
2004-09-08 12:41:14 +00:00
|
|
|
return;
|
|
|
|
|
2004-11-06 00:56:55 +00:00
|
|
|
d->filterText = QString::null;
|
2004-12-10 16:04:45 +00:00
|
|
|
QValueVector<KPDFPage*>::iterator it = pages_vector.begin(), end = pages_vector.end();
|
2004-09-17 17:58:42 +00:00
|
|
|
for ( ; it != end; ++it )
|
|
|
|
{
|
|
|
|
KPDFPage * page = *it;
|
2004-11-06 00:56:55 +00:00
|
|
|
if ( page->attributes() & KPDFPage::Highlight )
|
2004-09-17 17:58:42 +00:00
|
|
|
{
|
2004-11-06 00:56:55 +00:00
|
|
|
page->clearAttribute( KPDFPage::Highlight );
|
2005-01-09 23:37:07 +00:00
|
|
|
foreachObserver( notifyPageChanged( page->number(), DocumentObserver::Highlights ) );
|
2004-09-17 17:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-08 12:41:14 +00:00
|
|
|
}
|
2004-12-22 18:21:36 +00:00
|
|
|
|
2004-12-24 10:24:10 +00:00
|
|
|
|
2005-01-03 00:28:46 +00:00
|
|
|
void KPDFDocument::saveDocumentInfo() const
|
|
|
|
{
|
2005-01-13 11:03:48 +00:00
|
|
|
if ( d->docFileName.isNull() )
|
|
|
|
return;
|
2005-01-03 00:28:46 +00:00
|
|
|
|
2005-01-13 11:03:48 +00:00
|
|
|
//kdDebug() << "Using '" << d->xmlFileName << "' as document info file for saving." << endl;
|
|
|
|
QFile infoFile( d->xmlFileName );
|
2005-01-03 00:28:46 +00:00
|
|
|
if (infoFile.open( IO_WriteOnly | IO_Truncate) )
|
|
|
|
{
|
2005-01-03 15:51:05 +00:00
|
|
|
// Create DOM
|
2005-01-03 00:28:46 +00:00
|
|
|
QDomDocument doc( "documentInfo" );
|
|
|
|
QDomElement root = doc.createElement( "documentInfo" );
|
|
|
|
doc.appendChild( root );
|
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// Add bookmark list to DOM
|
2005-01-03 00:28:46 +00:00
|
|
|
QDomElement bookmarkList = doc.createElement( "bookmarkList" );
|
|
|
|
root.appendChild( bookmarkList );
|
|
|
|
|
|
|
|
for ( uint i = 0; i < pages_vector.count() ; i++ )
|
|
|
|
{
|
|
|
|
if (pages_vector[i]->attributes() & KPDFPage::Bookmark)
|
|
|
|
{
|
|
|
|
QDomElement page = doc.createElement( "page" );
|
|
|
|
page.appendChild( doc.createTextNode( QString::number(i) ) );
|
|
|
|
|
|
|
|
bookmarkList.appendChild( page );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
// Add general info to DOM
|
|
|
|
QDomElement generalInfo = doc.createElement( "generalInfo" );
|
|
|
|
root.appendChild( generalInfo );
|
|
|
|
|
|
|
|
QDomElement activePage = doc.createElement( "activePage" );
|
2005-01-09 23:37:07 +00:00
|
|
|
activePage.setAttribute( "viewport", d->viewport.toString() );
|
2005-01-03 15:51:05 +00:00
|
|
|
generalInfo.appendChild( activePage );
|
|
|
|
|
|
|
|
// Save DOM to XML file
|
2005-01-03 00:28:46 +00:00
|
|
|
QString xml = doc.toString();
|
|
|
|
QTextStream os( &infoFile );
|
|
|
|
os << xml;
|
|
|
|
}
|
|
|
|
infoFile.close();
|
|
|
|
}
|
|
|
|
|
2004-12-24 10:24:10 +00:00
|
|
|
void KPDFDocument::slotCheckMemory()
|
|
|
|
{
|
|
|
|
// perform the memory check for 'free mem dependant' profiles only
|
|
|
|
if ( Settings::memoryLevel() == Settings::EnumMemoryLevel::Low )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// for each observer going over 1MB of memory, invoke the manager
|
|
|
|
QMap< int, ObserverData * >::iterator it = d->observers.begin(), end = d->observers.end();
|
|
|
|
for ( ; it != end ; ++ it )
|
|
|
|
if ( (*it)->totalMemory > 1024 )
|
|
|
|
mCleanupMemory( it.key() /*observerId*/ );
|
|
|
|
}
|
|
|
|
|
2004-12-22 18:21:36 +00:00
|
|
|
void KPDFDocument::slotGeneratedContents( int id, int pageNumber )
|
|
|
|
{
|
2004-12-24 10:24:10 +00:00
|
|
|
// notify an observer that its pixmap changed
|
2004-12-22 18:21:36 +00:00
|
|
|
if ( d->observers.contains( id ) )
|
2005-01-09 23:37:07 +00:00
|
|
|
d->observers[ id ]->instance->notifyPageChanged( pageNumber, DocumentObserver::Pixmap );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** DocumentViewport **/
|
|
|
|
|
|
|
|
DocumentViewport::DocumentViewport( int n )
|
|
|
|
: pageNumber( n )
|
|
|
|
{
|
|
|
|
// default settings
|
|
|
|
reCenter.enabled = false;
|
|
|
|
reCenter.normalizedCenterX = 0.5;
|
|
|
|
reCenter.normalizedCenterY = 0.0;
|
|
|
|
autoFit.enabled = false;
|
|
|
|
autoFit.width = false;
|
|
|
|
autoFit.height = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentViewport::DocumentViewport( const QString & xmlDesc )
|
|
|
|
: pageNumber( -1 )
|
|
|
|
{
|
|
|
|
// default settings (maybe overridden below)
|
|
|
|
reCenter.enabled = false;
|
|
|
|
reCenter.normalizedCenterX = 0.5;
|
|
|
|
reCenter.normalizedCenterY = 0.0;
|
|
|
|
autoFit.enabled = false;
|
|
|
|
autoFit.width = false;
|
|
|
|
autoFit.height = false;
|
|
|
|
|
|
|
|
// check for string presence
|
|
|
|
if ( xmlDesc.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// decode the string
|
|
|
|
bool ok;
|
|
|
|
int field = 0;
|
|
|
|
QString token = xmlDesc.section( ';', field, field );
|
|
|
|
while ( !token.isEmpty() )
|
|
|
|
{
|
|
|
|
// decode the current token
|
|
|
|
if ( field == 0 )
|
|
|
|
{
|
|
|
|
pageNumber = token.toInt( &ok );
|
|
|
|
if ( !ok )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if ( token.startsWith( "C1" ) )
|
|
|
|
{
|
|
|
|
reCenter.enabled = true;
|
|
|
|
reCenter.normalizedCenterX = token.section( ':', 1, 1 ).toDouble();
|
|
|
|
reCenter.normalizedCenterY = token.section( ':', 2, 2 ).toDouble();
|
|
|
|
}
|
|
|
|
else if ( token.startsWith( "AF1" ) )
|
|
|
|
{
|
|
|
|
autoFit.enabled = true;
|
|
|
|
autoFit.width = token.section( ':', 1, 1 ) == "T";
|
|
|
|
autoFit.height = token.section( ':', 2, 2 ) == "T";
|
|
|
|
}
|
|
|
|
// proceed tokenizing string
|
|
|
|
field++;
|
|
|
|
token = xmlDesc.section( ';', field, field );
|
|
|
|
}
|
2004-12-22 18:21:36 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
QString DocumentViewport::toString() const
|
|
|
|
{
|
|
|
|
// start string with page number
|
|
|
|
QString s = QString::number( pageNumber );
|
|
|
|
// if has center coordinates, save them on string
|
|
|
|
if ( reCenter.enabled )
|
|
|
|
s += QString( ";C1:" ) + QString::number( reCenter.normalizedCenterX ) +
|
|
|
|
':' + QString::number( reCenter.normalizedCenterY );
|
|
|
|
// if has autofit enabled, save its state on string
|
|
|
|
if ( autoFit.enabled )
|
|
|
|
s += QString( ";AF1:" ) + (autoFit.width ? "T" : "F") +
|
|
|
|
':' + (autoFit.height ? "T" : "F");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentViewport::operator==( const DocumentViewport & vp ) const
|
|
|
|
{
|
|
|
|
bool equal = ( pageNumber == vp.pageNumber ) &&
|
|
|
|
( reCenter.enabled == vp.reCenter.enabled ) &&
|
|
|
|
( autoFit.enabled == vp.autoFit.enabled );
|
|
|
|
if ( !equal )
|
|
|
|
return false;
|
|
|
|
if ( reCenter.enabled &&
|
|
|
|
(( reCenter.normalizedCenterX != vp.reCenter.normalizedCenterX ) ||
|
|
|
|
( reCenter.normalizedCenterY != vp.reCenter.normalizedCenterY )) )
|
|
|
|
return false;
|
|
|
|
if ( autoFit.enabled &&
|
|
|
|
(( autoFit.width != vp.autoFit.width ) ||
|
|
|
|
( autoFit.height != vp.autoFit.height )) )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-02 22:37:52 +00:00
|
|
|
/** DocumentInfo **/
|
|
|
|
|
|
|
|
DocumentInfo::DocumentInfo()
|
|
|
|
: QDomDocument( "DocumentInformation" )
|
|
|
|
{
|
|
|
|
QDomElement docElement = createElement( "DocumentInfo" );
|
|
|
|
appendChild( docElement );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentInfo::set( const QString &key, const QString &value,
|
|
|
|
const QString &title )
|
|
|
|
{
|
|
|
|
QDomElement docElement = documentElement();
|
|
|
|
QDomElement element;
|
|
|
|
|
|
|
|
// check whether key already exists
|
|
|
|
QDomNodeList list = docElement.elementsByTagName( key );
|
|
|
|
if ( list.count() > 0 )
|
|
|
|
element = list.item( 0 ).toElement();
|
|
|
|
else
|
|
|
|
element = createElement( key );
|
|
|
|
|
|
|
|
element.setAttribute( "value", value );
|
|
|
|
element.setAttribute( "title", title );
|
|
|
|
|
|
|
|
if ( list.count() == 0 )
|
|
|
|
docElement.appendChild( element );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DocumentInfo::get( const QString &key ) const
|
|
|
|
{
|
|
|
|
QDomElement docElement = documentElement();
|
|
|
|
QDomElement element;
|
|
|
|
|
|
|
|
// check whether key already exists
|
|
|
|
QDomNodeList list = docElement.elementsByTagName( key );
|
|
|
|
if ( list.count() > 0 )
|
|
|
|
return list.item( 0 ).toElement().attribute( "value" );
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
|
2005-01-03 15:51:05 +00:00
|
|
|
/** DocumentSynopsis **/
|
|
|
|
|
|
|
|
DocumentSynopsis::DocumentSynopsis()
|
|
|
|
: QDomDocument( "DocumentSynopsis" )
|
|
|
|
{
|
|
|
|
// void implementation, only subclassed for naming
|
|
|
|
}
|
|
|
|
|
2004-12-22 18:21:36 +00:00
|
|
|
#include "document.moc"
|
2005-01-02 14:19:33 +00:00
|
|
|
#include "generator.moc"
|