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 <QProcess>
#include <QtDebug>
@ -5,26 +10,14 @@
ConvertChecker::ConvertChecker()
{
_pdf = false;
_pdf = true;
_calligra = false;
QProcess pdftoppm(this);
pdftoppm.setProgram("pdftoppm");
pdftoppm.setArguments({"-h"});
pdftoppm.start();
QProcess calligraconverter(this);
calligraconverter.setProgram("calligraconverter");
calligraconverter.setArguments({"-h"});
calligraconverter.start();
if(pdftoppm.waitForFinished(2000))
{
if(pdftoppm.exitStatus() == QProcess::NormalExit && pdftoppm.exitCode() == 0)
{
_pdf = true;
}
}
if(calligraconverter.waitForFinished(2000))
{
if(calligraconverter.exitStatus() == QProcess::NormalExit && calligraconverter.exitCode() == 0)
@ -58,33 +51,12 @@ ConvertChecker* ConvertChecker::instance()
quint32 ConvertChecker::pdfPages(QString filename)
{
quint32 pages = 0;
if(!_pdf || (Mimer::instance()->get_type(filename) != Mimer::PDF))
{ // pdfinfo is a bit slow to return on some non-PDFs
return pages;
}
std::string url("file://");
url.append(filename.toStdString());
GError* error = nullptr;
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;
}