Prototype for future ConvertChecker

This commit is contained in:
Anton Thomasson 2021-11-16 19:55:13 +01:00
parent 7a5e409997
commit 8cad5d9ae9

View file

@ -1,3 +1,8 @@
#include <poppler/glib/poppler.h>
#include <poppler/glib/poppler-document.h>
#include <dlfcn.h>
#include "convertchecker.h" #include "convertchecker.h"
#include <QProcess> #include <QProcess>
#include <QtDebug> #include <QtDebug>
@ -5,26 +10,14 @@
ConvertChecker::ConvertChecker() ConvertChecker::ConvertChecker()
{ {
_pdf = false; _pdf = true;
_calligra = false; _calligra = false;
QProcess pdftoppm(this);
pdftoppm.setProgram("pdftoppm");
pdftoppm.setArguments({"-h"});
pdftoppm.start();
QProcess calligraconverter(this); QProcess calligraconverter(this);
calligraconverter.setProgram("calligraconverter"); calligraconverter.setProgram("calligraconverter");
calligraconverter.setArguments({"-h"}); calligraconverter.setArguments({"-h"});
calligraconverter.start(); calligraconverter.start();
if(pdftoppm.waitForFinished(2000))
{
if(pdftoppm.exitStatus() == QProcess::NormalExit && pdftoppm.exitCode() == 0)
{
_pdf = true;
}
}
if(calligraconverter.waitForFinished(2000)) if(calligraconverter.waitForFinished(2000))
{ {
if(calligraconverter.exitStatus() == QProcess::NormalExit && calligraconverter.exitCode() == 0) if(calligraconverter.exitStatus() == QProcess::NormalExit && calligraconverter.exitCode() == 0)
@ -58,33 +51,12 @@ ConvertChecker* ConvertChecker::instance()
quint32 ConvertChecker::pdfPages(QString filename) quint32 ConvertChecker::pdfPages(QString filename)
{ {
quint32 pages = 0; std::string url("file://");
if(!_pdf || (Mimer::instance()->get_type(filename) != Mimer::PDF)) url.append(filename.toStdString());
{ // pdfinfo is a bit slow to return on some non-PDFs GError* error = nullptr;
return pages; PopplerDocument* doc = poppler_document_new_from_file(url.c_str(), nullptr, &error);
} quint32 pages = poppler_document_get_n_pages(doc);
g_object_unref(doc);
QProcess* pdfinfo = new QProcess(this);
pdfinfo->setProgram("pdfinfo");
pdfinfo->setArguments({filename});
pdfinfo->start();
if(!pdfinfo->waitForStarted(1000) || !pdfinfo->waitForFinished(1000))
{
pdfinfo->deleteLater();
return pages;
}
QByteArray pdfInfoOutput = pdfinfo->readAllStandardOutput();
pdfinfo->deleteLater();
qDebug() << pdfInfoOutput;
QList<QByteArray> pdfInfoOutputLines = pdfInfoOutput.split('\n');
for(QList<QByteArray>::iterator it = pdfInfoOutputLines.begin(); it != pdfInfoOutputLines.end(); it++)
{
if(it->startsWith("Pages"))
{
QList<QByteArray> pagesTokens = it->split(' ');
pages = pagesTokens.last().toInt();
}
}
return pages; return pages;
} }