Commit 199ec52c authored by Laurent Montel's avatar Laurent Montel 😁

Allow to remove temporary file

parent 4dfa4504
...@@ -59,6 +59,8 @@ void PrintPreviewPageWidgetTest::shouldHaveDefaultValue() ...@@ -59,6 +59,8 @@ void PrintPreviewPageWidgetTest::shouldHaveDefaultValue()
QComboBox *mPageComboBox = w.findChild<QComboBox *>(QStringLiteral("pagecombobox")); QComboBox *mPageComboBox = w.findChild<QComboBox *>(QStringLiteral("pagecombobox"));
QVERIFY(mPageComboBox); QVERIFY(mPageComboBox);
QCOMPARE(mPageComboBox->count(), 0); QCOMPARE(mPageComboBox->count(), 0);
QVERIFY(!w.deleteFile());
} }
QTEST_MAIN(PrintPreviewPageWidgetTest) QTEST_MAIN(PrintPreviewPageWidgetTest)
...@@ -68,9 +68,9 @@ void PrintPreviewDialog::readConfig() ...@@ -68,9 +68,9 @@ void PrintPreviewDialog::readConfig()
} }
} }
void PrintPreviewDialog::loadFile(const QString &path) void PrintPreviewDialog::loadFile(const QString &path, bool deleteFile)
{ {
mPrintPreviewWidget->loadFile(path); mPrintPreviewWidget->loadFile(path, deleteFile);
} }
void PrintPreviewDialog::slotPrint() void PrintPreviewDialog::slotPrint()
......
...@@ -29,7 +29,7 @@ class PrintPreviewDialog : public QDialog ...@@ -29,7 +29,7 @@ class PrintPreviewDialog : public QDialog
public: public:
explicit PrintPreviewDialog(QWidget *parent = Q_NULLPTR); explicit PrintPreviewDialog(QWidget *parent = Q_NULLPTR);
~PrintPreviewDialog(); ~PrintPreviewDialog();
void loadFile(const QString &path); void loadFile(const QString &path, bool deleteFile = false);
private Q_SLOTS: private Q_SLOTS:
void slotPrint(); void slotPrint();
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <QLabel> #include <QLabel>
#include <QComboBox> #include <QComboBox>
#include <QFile>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <KMessageBox> #include <KMessageBox>
#include <KLocalizedString> #include <KLocalizedString>
...@@ -30,6 +31,7 @@ using namespace WebEngineViewer; ...@@ -30,6 +31,7 @@ using namespace WebEngineViewer;
PrintPreviewPageWidget::PrintPreviewPageWidget(QWidget *parent) PrintPreviewPageWidget::PrintPreviewPageWidget(QWidget *parent)
: QWidget(parent), : QWidget(parent),
mDeleteFile(false),
mDoc(Q_NULLPTR) mDoc(Q_NULLPTR)
{ {
QVBoxLayout *layout = new QVBoxLayout; QVBoxLayout *layout = new QVBoxLayout;
...@@ -58,15 +60,24 @@ PrintPreviewPageWidget::PrintPreviewPageWidget(QWidget *parent) ...@@ -58,15 +60,24 @@ PrintPreviewPageWidget::PrintPreviewPageWidget(QWidget *parent)
PrintPreviewPageWidget::~PrintPreviewPageWidget() PrintPreviewPageWidget::~PrintPreviewPageWidget()
{ {
if (mDeleteFile && !mFilePath.isEmpty()) {
QFile file(mFilePath);
bool removeFile = file.remove();
if (!removeFile) {
qWarning() << "Impossible to remove file " << mFilePath;
}
}
delete mDoc; delete mDoc;
mDoc = Q_NULLPTR; mDoc = Q_NULLPTR;
} }
void PrintPreviewPageWidget::loadFile(const QString &path) void PrintPreviewPageWidget::loadFile(const QString &path, bool deleteFile)
{ {
if (path.isEmpty()) { if (path.isEmpty()) {
return; return;
} }
mDeleteFile = deleteFile;
mFilePath = path;
mDoc = Poppler::Document::load(path); mDoc = Poppler::Document::load(path);
if (!mDoc) { if (!mDoc) {
KMessageBox::error(this, i18n("Unable to open file \"%1\"", path), i18n("Open file error")); KMessageBox::error(this, i18n("Unable to open file \"%1\"", path), i18n("Open file error"));
...@@ -89,6 +100,11 @@ void PrintPreviewPageWidget::fillComboBox() ...@@ -89,6 +100,11 @@ void PrintPreviewPageWidget::fillComboBox()
} }
} }
bool PrintPreviewPageWidget::deleteFile() const
{
return mDeleteFile;
}
void PrintPreviewPageWidget::showPage(int index) void PrintPreviewPageWidget::showPage(int index)
{ {
Poppler::Page *popplerPage = mDoc->page(index); Poppler::Page *popplerPage = mDoc->page(index);
......
...@@ -35,14 +35,18 @@ public: ...@@ -35,14 +35,18 @@ public:
explicit PrintPreviewPageWidget(QWidget *parent = Q_NULLPTR); explicit PrintPreviewPageWidget(QWidget *parent = Q_NULLPTR);
~PrintPreviewPageWidget(); ~PrintPreviewPageWidget();
void loadFile(const QString &path); void loadFile(const QString &path, bool deleteFile = false);
void print(const QList<int> &page); void print(const QList<int> &page);
bool deleteFile() const;
private Q_SLOTS: private Q_SLOTS:
void showPage(int index); void showPage(int index);
private: private:
void fillComboBox(); void fillComboBox();
QString mFilePath;
bool mDeleteFile;
PrintPreviewPageViewer *mPrintPreviewPage; PrintPreviewPageViewer *mPrintPreviewPage;
Poppler::Document *mDoc; Poppler::Document *mDoc;
QComboBox *mPageComboBox; QComboBox *mPageComboBox;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment