From 11a23b94ca87b894908144b1b7d1cd84275f2bdd Mon Sep 17 00:00:00 2001
From: Jakub Kucharski <jakubkucharski97@gmail.com>
Date: Mon, 6 Jun 2016 22:06:23 +0200
Subject: [PATCH 6/7] qt5: Added document property setters & getters

---
 qt5/src/poppler-document.cc | 236 +++++++++++++++++++++++++++++++++++---------
 qt5/src/poppler-private.cc  |   9 ++
 qt5/src/poppler-private.h   |   3 +
 qt5/src/poppler-qt5.h       | 120 +++++++++++++++++++++-
 4 files changed, 323 insertions(+), 45 deletions(-)

diff --git a/qt5/src/poppler-document.cc b/qt5/src/poppler-document.cc
index 537dca3..e739514 100644
--- a/qt5/src/poppler-document.cc
+++ b/qt5/src/poppler-document.cc
@@ -9,6 +9,7 @@
  * Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
  * Copyright (C) 2014 Adam Reichold <adamreichold@myopera.com>
  * Copyright (C) 2015 William Bader <williambader@hotmail.com>
+ * Copyright (C) 2016 Jakub Kucharski <jakubkucharski97@gmail.com>
  *
  * 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
@@ -264,37 +265,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> xref(m_doc->doc->getXRef()->copy());
-	if (!xref)
-		return QString();
-	xref->getDocInfo(&info);
-	if ( !info.isDict() )
+	QScopedPointer<GooString> 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<GooString> 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<GooString> 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<GooString> 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<GooString> 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<GooString> 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<GooString> 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
@@ -323,35 +442,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> 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
@@ -633,7 +781,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/qt5/src/poppler-private.cc b/qt5/src/poppler-private.cc
index 86855fd..55d8972 100644
--- a/qt5/src/poppler-private.cc
+++ b/qt5/src/poppler-private.cc
@@ -4,6 +4,7 @@
  * Copyright (C) 2008, 2010, 2011, 2014 by Pino Toscano <pino@kde.org>
  * Copyright (C) 2013 by Thomas Freitag <Thomas.Freitag@alfa.de>
  * Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
+ * Copyright (C) 2016 Jakub Kucharski <jakubkucharski97@gmail.com>
  * Inspired on code by
  * Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>
  * Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>
@@ -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/qt5/src/poppler-private.h b/qt5/src/poppler-private.h
index df6290b..1a83bb6 100644
--- a/qt5/src/poppler-private.h
+++ b/qt5/src/poppler-private.h
@@ -9,6 +9,7 @@
  * Copyright (C) 2013 Anthony Granger <grangeranthony@gmail.com>
  * Copyright (C) 2014 Bogdan Cristea <cristeab@gmail.com>
  * Copyright (C) 2014 Aki Koskinen <freedesktop@akikoskinen.info>
+ * Copyright (C) 2016 Jakub Kucharski <jakubkucharski97@gmail.com>
  * Inspired on code by
  * Copyright (C) 2004 by Albert Astals Cid <tsdgeos@terra.es>
  * Copyright (C) 2004 by Enrico Ros <eros.kde@email.it>
@@ -63,6 +64,8 @@ namespace Poppler {
 
     GooString *QStringToGooString(const QString &s);
 
+    GooString *QDateTimeToUnicodeGooString(const QDateTime &dt);
+
     void qt5ErrorFunction(int pos, char *msg, va_list args);
 
     class LinkDestinationData
diff --git a/qt5/src/poppler-qt5.h b/qt5/src/poppler-qt5.h
index b159477..9eda848 100644
--- a/qt5/src/poppler-qt5.h
+++ b/qt5/src/poppler-qt5.h
@@ -15,6 +15,7 @@
  * Copyright (C) 2012, 2014, 2015 Adam Reichold <adamreichold@myopera.com>
  * Copyright (C) 2012, 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
  * Copyright (C) 2013 Anthony Granger <grangeranthony@gmail.com>
+ * Copyright (C) 2016 Jakub Kucharski <jakubkucharski97@gmail.com>
  *
  * 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
@@ -1072,6 +1073,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:
@@ -1090,6 +1122,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;
@@ -1659,7 +1777,7 @@ height = dummy.height();
     /**
        Conversion from PDF date string format to QDateTime
     */
-    POPPLER_QT5_EXPORT QDateTime convertDate( char *dateString );
+    POPPLER_QT5_EXPORT QDateTime convertDate( const char *dateString );
 
     /**
        Whether the color management functions are available.
-- 
2.8.3