From b911dfb78bb644b3d132ca29fb514d30c504c261 Mon Sep 17 00:00:00 2001 From: Jakub Kucharski Date: Mon, 6 Jun 2016 21:50:39 +0200 Subject: [PATCH 5/7] qt4: Added document property setters & getters --- qt4/src/poppler-document.cc | 236 +++++++++++++++++++++++++++++++++++--------- qt4/src/poppler-private.cc | 9 ++ qt4/src/poppler-private.h | 3 + qt4/src/poppler-qt4.h | 120 +++++++++++++++++++++- 4 files changed, 323 insertions(+), 45 deletions(-) diff --git a/qt4/src/poppler-document.cc b/qt4/src/poppler-document.cc index 94f997d..05bcd87 100644 --- a/qt4/src/poppler-document.cc +++ b/qt4/src/poppler-document.cc @@ -9,6 +9,7 @@ * Copyright (C) 2012 Fabio D'Urso * Copyright (C) 2014 Adam Reichold * Copyright (C) 2015 William Bader + * Copyright (C) 2016 Jakub Kucharski * * 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 @@ -278,37 +279,155 @@ namespace Poppler { return result; } - /* borrowed from kpdf */ QString Document::info( const QString & type ) const { - // [Albert] Code adapted from pdfinfo.cc on xpdf - Object info; - if ( m_doc->locked ) + if (m_doc->locked) { return QString(); + } - QScopedPointer xref(m_doc->doc->getXRef()->copy()); - if (!xref) - return QString(); - xref->getDocInfo(&info); - if ( !info.isDict() ) + QScopedPointer goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData())); + return UnicodeParsedString(goo.data()); + } + + bool Document::setInfo( const QString & key, const QString & val ) + { + if (m_doc->locked) { + return false; + } + + GooString *goo = QStringToUnicodeGooString(val); + m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), goo); + return true; + } + + QString Document::title() const + { + if (m_doc->locked) { return QString(); + } - QString result; - Object obj; - GooString *s1; - Dict *infoDict = info.getDict(); + QScopedPointer goo(m_doc->doc->getDocInfoTitle()); + return UnicodeParsedString(goo.data()); + } - if ( infoDict->lookup( type.toLatin1().data(), &obj )->isString() ) - { - s1 = obj.getString(); - result = UnicodeParsedString(s1); - obj.free(); - info.free(); - return result; + bool Document::setTitle( const QString & val ) + { + if (m_doc->locked) { + return false; } - obj.free(); - info.free(); - return QString(); + + m_doc->doc->setDocInfoTitle(QStringToUnicodeGooString(val)); + return true; + } + + QString Document::author() const + { + if (m_doc->locked) { + return QString(); + } + + QScopedPointer goo(m_doc->doc->getDocInfoAuthor()); + return UnicodeParsedString(goo.data()); + } + + bool Document::setAuthor( const QString & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoAuthor(QStringToUnicodeGooString(val)); + return true; + } + + QString Document::subject() const + { + if (m_doc->locked) { + return QString(); + } + + QScopedPointer goo(m_doc->doc->getDocInfoSubject()); + return UnicodeParsedString(goo.data()); + } + + bool Document::setSubject( const QString & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoSubject(QStringToUnicodeGooString(val)); + return true; + } + + QString Document::keywords() const + { + if (m_doc->locked) { + return QString(); + } + + QScopedPointer goo(m_doc->doc->getDocInfoKeywords()); + return UnicodeParsedString(goo.data()); + } + + bool Document::setKeywords( const QString & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoKeywords(QStringToUnicodeGooString(val)); + return true; + } + + QString Document::creator() const + { + if (m_doc->locked) { + return QString(); + } + + QScopedPointer goo(m_doc->doc->getDocInfoCreator()); + return UnicodeParsedString(goo.data()); + } + + bool Document::setCreator( const QString & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoCreator(QStringToUnicodeGooString(val)); + return true; + } + + QString Document::producer() const + { + if (m_doc->locked) { + return QString(); + } + + QScopedPointer goo(m_doc->doc->getDocInfoProducer()); + return UnicodeParsedString(goo.data()); + } + + bool Document::setProducer( const QString & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoProducer(QStringToUnicodeGooString(val)); + return true; + } + + bool Document::removeInfo() + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->removeDocInfo(); + return true; } QStringList Document::infoKeys() const @@ -337,35 +456,64 @@ namespace Poppler { return keys; } - /* borrowed from kpdf */ QDateTime Document::date( const QString & type ) const { - // [Albert] Code adapted from pdfinfo.cc on xpdf - if ( m_doc->locked ) + if (m_doc->locked) { return QDateTime(); + } - Object info; - QScopedPointer xref(m_doc->doc->getXRef()->copy()); - if (!xref) - return QDateTime(); - xref->getDocInfo(&info); - if ( !info.isDict() ) { - info.free(); + QString str = UnicodeParsedString(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData())); + return Poppler::convertDate(str.toLatin1().constData()); + } + + bool Document::setDate( const QString & key, const QDateTime & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoStringEntry(key.toLatin1().constData(), QDateTimeToUnicodeGooString(val)); + return true; + } + + QDateTime Document::creationDate() const + { + if (m_doc->locked) { return QDateTime(); } - Object obj; - Dict *infoDict = info.getDict(); - QDateTime result; + QString str = UnicodeParsedString(m_doc->doc->getDocInfoCreatDate()); + return Poppler::convertDate(str.toLatin1().constData()); + } - if ( infoDict->lookup( type.toLatin1().data(), &obj )->isString() ) - { - char *aux = obj.getString()->getCString(); - result = Poppler::convertDate(aux); + bool Document::setCreationDate( const QDateTime & val ) + { + if (m_doc->locked) { + return false; } - obj.free(); - info.free(); - return result; + + m_doc->doc->setDocInfoCreatDate(QDateTimeToUnicodeGooString(val)); + return true; + } + + QDateTime Document::modDate() const + { + if (m_doc->locked) { + return QDateTime(); + } + + QString str = UnicodeParsedString(m_doc->doc->getDocInfoModDate()); + return Poppler::convertDate(str.toLatin1().constData()); + } + + bool Document::setModDate( const QDateTime & val ) + { + if (m_doc->locked) { + return false; + } + + m_doc->doc->setDocInfoModDate(QDateTimeToUnicodeGooString(val)); + return true; } bool Document::isEncrypted() const @@ -652,7 +800,7 @@ namespace Poppler { return Document::NoForm; // make gcc happy } - QDateTime convertDate( char *dateString ) + QDateTime convertDate( const char *dateString ) { int year, mon, day, hour, min, sec, tzHours, tzMins; char tz; diff --git a/qt4/src/poppler-private.cc b/qt4/src/poppler-private.cc index cbf21b1..d64dca6 100644 --- a/qt4/src/poppler-private.cc +++ b/qt4/src/poppler-private.cc @@ -4,6 +4,7 @@ * Copyright (C) 2008, 2010, 2011 by Pino Toscano * Copyright (C) 2013 by Thomas Freitag * Copyright (C) 2013 Adrian Johnson + * Copyright (C) 2016 Jakub Kucharski * Inspired on code by * Copyright (C) 2004 by Albert Astals Cid * Copyright (C) 2004 by Enrico Ros @@ -153,6 +154,14 @@ namespace Debug { return ret; } + GooString *QDateTimeToUnicodeGooString(const QDateTime &dt) { + if (!dt.isValid()) { + return NULL; + } + + return QStringToUnicodeGooString(dt.toUTC().toString("yyyyMMddhhmmss+00'00'")); + } + void linkActionToTocItem( ::LinkAction * a, DocumentData * doc, QDomElement * e ) { if ( !a || !e ) diff --git a/qt4/src/poppler-private.h b/qt4/src/poppler-private.h index 0d11e83..015f18e 100644 --- a/qt4/src/poppler-private.h +++ b/qt4/src/poppler-private.h @@ -7,6 +7,7 @@ * Copyright (C) 2011 Hib Eris * Copyright (C) 2012, 2013 Thomas Freitag * Copyright (C) 2013 Julien Nabet + * Copyright (C) 2016 Jakub Kucharski * Inspired on code by * Copyright (C) 2004 by Albert Astals Cid * Copyright (C) 2004 by Enrico Ros @@ -61,6 +62,8 @@ namespace Poppler { GooString *QStringToGooString(const QString &s); + GooString *QDateTimeToUnicodeGooString(const QDateTime &dt); + void qt4ErrorFunction(int pos, char *msg, va_list args); class LinkDestinationData diff --git a/qt4/src/poppler-qt4.h b/qt4/src/poppler-qt4.h index c0340a4..de863af 100644 --- a/qt4/src/poppler-qt4.h +++ b/qt4/src/poppler-qt4.h @@ -14,6 +14,7 @@ * Copyright (C) 2012, Tobias Koenig * Copyright (C) 2012, 2014, 2015 Adam Reichold * Copyright (C) 2012, 2013 Thomas Freitag + * Copyright (C) 2016 Jakub Kucharski * * 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 @@ -1082,6 +1083,37 @@ QDateTime modified = m_doc->date("ModDate"); QDateTime date( const QString & data ) const; /** + Set the Info dict date entry specified by \param key to \param val + + \returns true on success, false on failure + */ + bool setDate( const QString & key, const QDateTime & val ); + + /** + The date of the creation of the document + */ + QDateTime creationDate() const; + + /** + Set the creation date of the document to \param val + + \returns true on success, false on failure + */ + bool setCreationDate( const QDateTime & val ); + + /** + The date of the last change in the document + */ + QDateTime modDate() const; + + /** + Set the modification date of the document to \param val + + \returns true on success, false on failure + */ + bool setModDate( const QDateTime & val ); + + /** Get specified information associated with the document You would use this method with something like: @@ -1100,6 +1132,92 @@ QString subject = m_doc->info("Subject"); QString info( const QString & data ) const; /** + Set the value of the document's Info dictionary entry specified by \param key to \param val + + \returns true on success, false on failure + */ + bool setInfo( const QString & key, const QString & val ); + + /** + The title of the document + */ + QString title() const; + + /** + Set the title of the document to \param val + + \returns true on success, false on failure + */ + bool setTitle( const QString & val ); + + /** + The author of the document + */ + QString author() const; + + /** + Set the author of the document to \param val + + \returns true on success, false on failure + */ + bool setAuthor( const QString & val ); + + /** + The subject of the document + */ + QString subject() const; + + /** + Set the subject of the document to \param val + + \returns true on success, false on failure + */ + bool setSubject( const QString & val ); + + /** + The keywords of the document + */ + QString keywords() const; + + /** + Set the keywords of the document to \param val + + \returns true on success, false on failure + */ + bool setKeywords( const QString & val ); + + /** + The creator of the document + */ + QString creator() const; + + /** + Set the creator of the document to \param val + + \returns true on success, false on failure + */ + bool setCreator( const QString & val ); + + /** + The producer of the document + */ + QString producer() const; + + /** + Set the producer of the document to \param val + + \returns true on success, false on failure + */ + bool setProducer( const QString & val ); + + /** + Remove the document's Info dictionary + + \returns true on success, false on failure + */ + bool removeInfo(); + + /** Obtain a list of the available string information keys. */ QStringList infoKeys() const; @@ -1698,7 +1816,7 @@ height = dummy.height(); /** Conversion from PDF date string format to QDateTime */ - POPPLER_QT4_EXPORT QDateTime convertDate( char *dateString ); + POPPLER_QT4_EXPORT QDateTime convertDate( const char *dateString ); /** Whether the color management functions are available. -- 2.8.3