Skip to content

Commit

Permalink
Make sure to remove scanned temporary files after pdf create.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragotin committed May 24, 2020
1 parent d950aea commit 0c917c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
78 changes: 57 additions & 21 deletions src/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QtGlobal>
#include <QScrollBar>
#include <QMenu>
#include <QTemporaryDir>

#include "imagelistdelegate.h"
#include "executor.h"
Expand Down Expand Up @@ -178,22 +179,11 @@ void Dialog::slotListViewSize(QSize s)
void Dialog::slotButtonClicked(QAbstractButton *button)
{
if (!button) return;
const QString path = _settings->value(_SettingsLastFilePath, QDir::homePath()).toString();

if( button == ui->buttonBox->button(QDialogButtonBox::StandardButton::Save)) {
int currIndx = ui->widgetStack->currentIndex();
if ( currIndx == _IndxListView || currIndx == _IndxAbout) {
QStringList files = _model.files();

const QString saveFile = QFileDialog::getSaveFileName(this, tr("Save PDF File"), path, "PDF (*.pdf)");
if (!saveFile.isEmpty()) {
Executor *creator = new Executor;
connect(creator, &Executor::finished, this, &Dialog::pdfCreatorFinished);
creator->setOutputFile(saveFile);
QApplication::setOverrideCursor(Qt::WaitCursor);
updateInfoText(ProcessStatus::CreatingPdf);
creator->buildPdf(files);
}
startPdfCreation();
} else if (currIndx == _IndxConfig) {
const QString colorCmd = ui->leColorScanCmd->text();
if (colorCmd.isEmpty()) {
Expand Down Expand Up @@ -231,7 +221,23 @@ void Dialog::reject()
}
}

void Dialog::pdfCreatorFinished(bool success)
void Dialog::startPdfCreation()
{
const QStringList files = _model.files();
const QString path = _settings->value(_SettingsLastFilePath, QDir::homePath()).toString();

const QString saveFile = QFileDialog::getSaveFileName(this, tr("Save PDF File"), path, "PDF (*.pdf)");
if (!saveFile.isEmpty()) {
Executor *creator = new Executor;
connect(creator, &Executor::finished, this, &Dialog::pdfCreatorFinished);
creator->setOutputFile(saveFile);
startLengthyOperation();
updateInfoText(ProcessStatus::CreatingPdf);
creator->buildPdf(files);
}
}

void Dialog::pdfCreatorFinished(int success)
{
QApplication::restoreOverrideCursor();

Expand All @@ -242,10 +248,25 @@ void Dialog::pdfCreatorFinished(bool success)
resultFile = creator->outputFile();
creator->deleteLater();
}
if (success) {

if (success == 0) {
// cleanup: remove the scanned pages
for (auto file : _scans) {
QFileInfo fi(file);
QDir d = fi.absoluteDir();
const QString fileStr = fi.absoluteFilePath();
QFile::remove(fileStr);
if (d.isEmpty()) {
d.removeRecursively();
}
}
_scans.clear();
_model.clear();
updateInfoText(ProcessStatus::PDFCreated, resultFile);
} else {
updateInfoText(ProcessStatus::PDFCreatedFailed);
}
updateInfoText(ProcessStatus::PDFCreated, resultFile);
endLengthyOperation();
}

void Dialog::slotFromFile()
Expand Down Expand Up @@ -309,6 +330,9 @@ void Dialog::updateInfoText(ProcessStatus stat, const QString& saveFile)
case ProcessStatus::AboutPage:
str = tr("Click the Close button to continue.");
break;
case ProcessStatus::PDFCreatedFailed:
str = tr("The PDF could not be created.");
break;
}

ui->labInfo->setOpenExternalLinks(openExternal);
Expand Down Expand Up @@ -336,6 +360,12 @@ void Dialog::slotFromScanner()
}
_scanner = new Executor;

QTemporaryDir dir;
dir.setAutoRemove(false);
const QString outputFile = dir.filePath("scan.png");

_scanner->setOutputFile(outputFile);

connect(_scanner, &Executor::finished, this, &Dialog::slotScanFinished);
startLengthyOperation();
if (!_scanner->scan(scanCmd)) {
Expand All @@ -355,15 +385,21 @@ void Dialog::slotScanFinished(int exitCode)
delete _scanner;
_scanner = nullptr;
}
if (exitCode == 0) {
// exitCode 0 is success
if (exitCode == 0 && !resultFile.isEmpty()) {
_model.addImageFile(resultFile);
}
endLengthyOperation();

if (_model.rowCount() && exitCode == 0)
updateInfoText(ProcessStatus::ImageScanned, resultFile);
else
if (_model.rowCount()) {
updateInfoText(ProcessStatus::ImageScanned, resultFile);

// remember the scanned file.
_scans.append(resultFile);
}

} else {
updateInfoText(ProcessStatus::ScanFailed);
}
endLengthyOperation();
}

void Dialog::startLengthyOperation()
Expand Down
7 changes: 5 additions & 2 deletions src/dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class Dialog : public QDialog
CreatingPdf,
ConfigPage,
AboutPage,
NotConfigured
NotConfigured,
PDFCreatedFailed
};

public slots:
Expand All @@ -75,7 +76,7 @@ private slots:
void slotFromFile();
void slotFromScanner();
void slotButtonClicked(QAbstractButton *button);
void pdfCreatorFinished(bool success);
void pdfCreatorFinished(int success);
void slotScanFinished(int exitCode);
void slotListViewSize(QSize s);

Expand All @@ -89,6 +90,7 @@ private slots:
private:
void updateInfoText(ProcessStatus stat, const QString& saveFile = QString());
void buildMenu(QToolButton *button);
void startPdfCreation();

const int _IndxListView {0};
const int _IndxConfig {1};
Expand All @@ -106,5 +108,6 @@ private slots:
QScopedPointer<ImageListDelegate> _delegate;
bool _lengthyOpRunning { false };
Executor *_scanner {nullptr};
QStringList _scans;
};
#endif // DIALOG_H
6 changes: 1 addition & 5 deletions src/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Executor::buildPdf(const QStringList& files)
const QString argstr {"-resize 1240x1753 -gravity center -units PixelsPerInch -density 150x150 -background white -extent 1240x1753"};

args.append(files);
args.append(argstr.split(' '));
args.append(parseStringArgs(argstr));
args.append(_outputFile);

_process = new QProcess;
Expand All @@ -136,10 +136,6 @@ bool Executor::scan(const QString& cmd)

QStringList args = parseStringArgs(cmd);
if (args.size() > 0) {
QTemporaryDir dir;
dir.setAutoRemove(false);

_outputFile = dir.filePath("scan.png");
_process = new QProcess;

// if the args contain the %OUTFILE tag, replace that with the temp file
Expand Down

0 comments on commit 0c917c0

Please sign in to comment.