From fb6e540547efb6abc7ce57e4a6f91dfbe20f2371 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Mon, 3 Jan 2005 00:28:46 +0000 Subject: [PATCH] Save and read the bookmarks to/from a file when opening/closing a document svn path=/trunk/kdegraphics/kpdf/; revision=375083 --- TODO | 2 +- core/document.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++---- core/document.h | 2 + 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/TODO b/TODO index 61c2e1ab6..670f3b1d3 100644 --- a/TODO +++ b/TODO @@ -7,7 +7,6 @@ Legend: (*) - Some parts of this item are already done In progress: --> Dom for bookmarks and saving/loading document settings. -> Fix threaded generation. It's.. crashy.. Urgent fixes and items to get ready before 3.4 (special high-priority list): @@ -91,6 +90,7 @@ More items (first items will enter 'In progress list' first): -> FIX: single page mode: add a little margin on pageview (top-bottom edges) Done (newest features come first): +-> ADD: Save bookmarks into a file so you they get recovered when opening the same file again (Albert) -> FIX: searchline back to work -> CHG: DocumentInfo is now a DomTree and the properties dialog is dynamically generated (Tobias) -> ADD: Presentation transitions are loaded from the pdf files as well as fullscreen state (Tobias) diff --git a/core/document.cpp b/core/document.cpp index 791352567..a2a2e161d 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #include #include #include +#include // local includes #include "document.h" @@ -50,6 +52,9 @@ class KPDFDocumentPrivate // memory check/free timer QTimer * memCheckTimer; + // bookmark saver timer + QTimer * saveBookmarksTimer; + // observers related (note: won't delete oservers) QMap< int, class ObserverData* > observers; }; @@ -76,6 +81,8 @@ KPDFDocument::KPDFDocument() d->searchPage = -1; d->memCheckTimer = new QTimer( this ); connect( d->memCheckTimer, SIGNAL( timeout() ), this, SLOT( slotCheckMemory() ) ); + d->saveBookmarksTimer = new QTimer( this ); + connect( d->memCheckTimer, SIGNAL( timeout() ), this, SLOT( saveDocumentInfo() ) ); } KPDFDocument::~KPDFDocument() @@ -90,12 +97,12 @@ bool KPDFDocument::openDocument( const QString & docFile ) // docFile is always local so we can use QFile on it QFile fileReadTest( docFile ); if ( !fileReadTest.open( IO_ReadOnly ) ) + { + documentFileName = QString::null; return false; + } fileReadTest.close(); - // reset internal status and frees memory - closeDocument(); - // create the generator based on the file's mimetype KMimeType::Ptr mime = KMimeType::findByPath( docFile ); QString mimeName = mime->name(); @@ -121,12 +128,10 @@ bool KPDFDocument::openDocument( const QString & docFile ) // start memory check timer d->memCheckTimer->start( 1000 ); - // check local directory for an overlay xml - // TODO import overlay layers from XML -// QString fileName = docFile.contains('/') ? docFile.section('/', -1, -1) : docFile; -// fileName = "kpdf/" + QString::number(fileSize) + "." + fileName + ".xml"; -// QString localFN = locateLocal( "data", fileName ); -// kdDebug() << "Using '" << localFN << "' as overlay descriptor." << endl; + // start bookmark saver timer + d->memCheckTimer->start( 5 * 60 * 1000 ); + + loadDocumentInfo(); // filter pages, setup observers and set the first page as current if ( pages_vector.size() > 0 ) @@ -139,6 +144,8 @@ bool KPDFDocument::openDocument( const QString & docFile ) void KPDFDocument::closeDocument() { + saveDocumentInfo(); + // stop memory check timer d->memCheckTimer->stop(); @@ -623,6 +630,54 @@ int KPDFDocument::mFreeMemory() #endif } +void KPDFDocument::loadDocumentInfo() +{ + QFile fileReadTest( documentFileName ); + fileReadTest.open( IO_ReadOnly ); + + QString fileName = documentFileName.contains('/') ? documentFileName.section('/', -1, -1) : documentFileName; + fileName = "kpdf/" + QString::number(fileReadTest.size()) + "." + fileName + ".xml"; + fileReadTest.close(); + QString localFN = locateLocal( "data", fileName ); + kdDebug() << "Using '" << localFN << "' as document info file." << endl; + QFile infoFile( localFN ); + if (infoFile.exists() && infoFile.open( IO_ReadOnly ) ) + { + 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; + + QDomNode bookMarkList = root.firstChild(); + if (bookMarkList.isElement() && bookMarkList.toElement().tagName() != "bookmarkList") return; + + QDomNode n = bookMarkList.firstChild(); + + QDomElement e; + int pageNumber; + bool ok; + while ( !n.isNull() ) + { + if ( n.isElement() ) + { + e = n.toElement(); + if (e.tagName() != "page") return; + + pageNumber = e.text().toInt(&ok); + if (ok) toggleBookmark( pageNumber ); + } + n = n.nextSibling(); + } + } + infoFile.close(); +} + QString KPDFDocument::giveAbsolutePath( const QString & fileName ) { if ( documentFileName.isEmpty() ) @@ -689,6 +744,46 @@ void KPDFDocument::unHilightPages() } +void KPDFDocument::saveDocumentInfo() const +{ + if (documentFileName.isNull()) return; + + QFile fileReadTest( documentFileName ); + fileReadTest.open( IO_ReadOnly ); + + QString fileName = documentFileName.contains('/') ? documentFileName.section('/', -1, -1) : documentFileName; + fileName = "kpdf/" + QString::number(fileReadTest.size()) + "." + fileName + ".xml"; + fileReadTest.close(); + QString localFN = locateLocal( "data", fileName ); + kdDebug() << "Using '" << localFN << "' as document info file for saving." << endl; + QFile infoFile( localFN ); + if (infoFile.open( IO_WriteOnly | IO_Truncate) ) + { + QDomDocument doc( "documentInfo" ); + QDomElement root = doc.createElement( "documentInfo" ); + doc.appendChild( root ); + + 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 ); + } + } + + QString xml = doc.toString(); + QTextStream os( &infoFile ); + os << xml; + } + infoFile.close(); +} + void KPDFDocument::slotCheckMemory() { // perform the memory check for 'free mem dependant' profiles only diff --git a/core/document.h b/core/document.h index 9b9011b14..e33f8d342 100644 --- a/core/document.h +++ b/core/document.h @@ -86,6 +86,7 @@ class KPDFDocument : public QObject // only for a private slot.. int mTotalMemory(); int mFreeMemory(); // more private functions + void loadDocumentInfo(); QString giveAbsolutePath( const QString & fileName ); bool openRelativeFile( const QString & fileName ); void processPageList( bool documentChanged ); @@ -97,6 +98,7 @@ class KPDFDocument : public QObject // only for a private slot.. class KPDFDocumentPrivate * d; private slots: + void saveDocumentInfo() const; void slotCheckMemory(); void slotGeneratedContents( int id, int pageNumber ); };