2020-01-18 11:38:54 +01:00
|
|
|
// Copyright (C) 2018-2020 Jakub Melka
|
2018-11-17 16:48:30 +01:00
|
|
|
//
|
|
|
|
// This file is part of PdfForQt.
|
|
|
|
//
|
|
|
|
// PdfForQt is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// PdfForQt is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef PDFDOCUMENTREADER_H
|
|
|
|
#define PDFDOCUMENTREADER_H
|
|
|
|
|
|
|
|
#include "pdfglobal.h"
|
|
|
|
#include "pdfdocument.h"
|
2019-11-09 17:27:17 +01:00
|
|
|
#include "pdfprogress.h"
|
2018-11-17 16:48:30 +01:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QIODevice>
|
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
/// This class is a reader of PDF document from various devices (file, io device,
|
|
|
|
/// byte buffer). This class doesn't throw exceptions, to check errors, use
|
|
|
|
/// appropriate functions.
|
2018-11-21 19:30:15 +01:00
|
|
|
class PDFFORQTLIBSHARED_EXPORT PDFDocumentReader
|
2018-11-17 16:48:30 +01:00
|
|
|
{
|
|
|
|
Q_DECLARE_TR_FUNCTIONS(pdf::PDFDocumentReader)
|
|
|
|
|
|
|
|
public:
|
2019-11-09 17:27:17 +01:00
|
|
|
explicit PDFDocumentReader(PDFProgress* progress, const std::function<QString(bool*)>& getPasswordCallback);
|
2018-11-17 16:48:30 +01:00
|
|
|
|
2018-11-21 19:30:15 +01:00
|
|
|
constexpr inline PDFDocumentReader(const PDFDocumentReader&) = delete;
|
|
|
|
constexpr inline PDFDocumentReader(PDFDocumentReader&&) = delete;
|
|
|
|
constexpr inline PDFDocumentReader& operator=(const PDFDocumentReader&) = delete;
|
|
|
|
constexpr inline PDFDocumentReader& operator=(PDFDocumentReader&&) = delete;
|
|
|
|
|
2019-08-12 12:02:40 +02:00
|
|
|
enum class Result
|
|
|
|
{
|
|
|
|
OK, ///< Document was successfully loaded
|
|
|
|
Failed, ///< Error occured during document reading
|
|
|
|
Cancelled ///< User cancelled document reading
|
|
|
|
};
|
|
|
|
|
2018-11-17 16:48:30 +01:00
|
|
|
/// Reads a PDF document from the specified file. If file doesn't exist,
|
|
|
|
/// cannot be opened or contain invalid pdf, empty PDF file is returned.
|
|
|
|
/// No exception is thrown.
|
|
|
|
PDFDocument readFromFile(const QString& fileName);
|
|
|
|
|
|
|
|
/// Reads a PDF document from the specified device. If device is not opened
|
|
|
|
/// for reading, then function tries it to open for reading. If it is opened,
|
|
|
|
/// but not for reading, empty PDF document is returned. This also occurs
|
|
|
|
/// when incorrect PDF is read. No exception is thrown.
|
|
|
|
PDFDocument readFromDevice(QIODevice* device);
|
|
|
|
|
|
|
|
/// Reads a PDF document from the specified buffer (byte array). If incorrect
|
|
|
|
/// PDF is read, then empty PDF document is returned. No exception is thrown.
|
|
|
|
PDFDocument readFromBuffer(const QByteArray& buffer);
|
|
|
|
|
2019-08-12 12:02:40 +02:00
|
|
|
/// Returns result code for reading document from the device
|
|
|
|
Result getReadingResult() const { return m_result; }
|
2018-11-17 16:48:30 +01:00
|
|
|
|
2018-11-21 19:30:15 +01:00
|
|
|
/// Returns error message, if document reading was unsuccessfull
|
|
|
|
const QString& getErrorMessage() const { return m_errorMessage; }
|
|
|
|
|
2018-11-17 16:48:30 +01:00
|
|
|
private:
|
|
|
|
static constexpr const int FIND_NOT_FOUND_RESULT = -1;
|
|
|
|
|
|
|
|
/// Resets the internal state and prepares it for new reading cycle
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/// Find a last string in the byte array, scan only \p limit bytes. If string
|
|
|
|
/// is not found, then FIND_NOT_FOUND_RESULT is returned, if it is found, then
|
|
|
|
/// it position from the beginning of byte array is returned.
|
|
|
|
/// \param what String to be found
|
|
|
|
/// \param byteArray Byte array to be scanned from the end
|
|
|
|
/// \param limit Scan up to this value bytes from the end
|
|
|
|
/// \returns Position of string, or FIND_NOT_FOUND_RESULT
|
|
|
|
int findFromEnd(const char* what, const QByteArray& byteArray, int limit);
|
|
|
|
|
2020-01-01 18:23:18 +01:00
|
|
|
void progressStart(size_t stepCount, QString text);
|
2019-11-09 17:27:17 +01:00
|
|
|
void progressStep();
|
|
|
|
void progressFinish();
|
|
|
|
|
2018-11-25 14:48:08 +01:00
|
|
|
/// Mutex for access to variables of this reader from more threads
|
|
|
|
/// (providing thread safety)
|
|
|
|
QMutex m_mutex;
|
|
|
|
|
2019-08-12 12:02:40 +02:00
|
|
|
/// Result of document reading from the device
|
|
|
|
std::atomic<Result> m_result;
|
2018-11-17 16:48:30 +01:00
|
|
|
|
|
|
|
/// In case if error occurs, it is stored here
|
|
|
|
QString m_errorMessage;
|
|
|
|
|
|
|
|
/// Version of the scanned file
|
|
|
|
PDFVersion m_version;
|
2019-08-10 17:24:12 +02:00
|
|
|
|
|
|
|
/// Callback to obtain password from the user
|
|
|
|
std::function<QString(bool*)> m_getPasswordCallback;
|
2019-11-09 17:27:17 +01:00
|
|
|
|
|
|
|
/// Progress indicator
|
|
|
|
PDFProgress* m_progress;
|
2018-11-17 16:48:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace pdf
|
|
|
|
|
|
|
|
#endif // PDFDOCUMENTREADER_H
|