1 #ifndef PDFDOCUMENTREFERENCE_H
2 #define PDFDOCUMENTREFERENCE_H
3 #include <QString>
4 #include <QSharedPointer>
5 #include "pdfcacheoption.h"
6 #include "poppler-qt.h"
7 
8 #include <mutex>
9 
10 // forward-declare
11 struct PDFPageReference;
12 
13 /** Holds a reference to a PDF document.
14  *
15  * What exactly this means depends on the cache option:
16  * If the cache option is hold in memory, this class wild have a
17  * complete in-memory copy of the PDF file.
18  *
19  * If the cache option is reread from disk, the reference is
20  * just the file name.
21  *
22  */
23 class PDFDocumentReference
24 {
25 private:
26   const QString filename_;
27   QByteArray fileContents_;
28   const PDFCacheOption cacheOption_;
29   // protects access to the poppler document
30   mutable std::mutex mutex_;
31   typedef std::lock_guard<std::mutex> Lock;
32   mutable QSharedPointer<const Poppler::Document> popplerDocument_;
33 
34 public:
35   /** Create the document reference.
36    *
37    * If the cache option is keep in memory, this will read the entire file into memory.
38    */
39   PDFDocumentReference( const QString& filename, const PDFCacheOption& cacheOption);
40 
41   /** Returns a Poppler::Page reference to the requested page number
42    * NOTE: Until poppler supports multi-threading, this will
43    * create a new Poppler::Document for each request.
44    */
45   PDFPageReference page(unsigned pageNumber) const;
46 
47   /** Create a Poppler::Document from this reference.
48    * If you did not cache the file to memory, this step will try to
49    * read the file.
50    */
51   QSharedPointer<const Poppler::Document> popplerDocument() const;
52 
53   /** get filename */
54   const QString& filename() const;
55 
56   /** get cache setting */
57   const PDFCacheOption& cacheOption() const;
58 
59   /** Update the document reference.
60    * This only works if rhs has the same filename and cache options,
61    * effectively only updating the file contents buffer.
62    */
63   PDFDocumentReference& operator = (const PDFDocumentReference& rhs);
64 
65   /** Compares the references. Exact behaviour depends on the cache option:
66    * If we are memory-caching, this compares the ByteArrays (i.e. checks for
67    * identical files).
68    *
69    * If we are not caching, it just checks for the same filename.
70    */
71   friend bool operator == (const PDFDocumentReference& lhs, const PDFDocumentReference& rhs);
72 };
73 
74 #endif // PDFDOCUMENTREFERENCE_H
75