Commit 49f784ec authored by Albert Astals Cid's avatar Albert Astals Cid Committed by Albert Astals Cid
Browse files

When signing save to a new file, not over the current one

parent 5d152036
......@@ -2701,29 +2701,12 @@ bool Document::canConfigurePrinter() const
return false;
}
void Document::sign(const NewSignatureData &data)
bool Document::sign(const NewSignatureData &data, const QString &newPath)
{
if (d->m_generator->canSign()) {
// we technically need to save the document before signing it,
// so let's ask before clobbering
const int res = KMessageBox::warningContinueCancel(d->m_widget,
i18n("Signing this document will modify the file \"%1\" on disk by embedding a "
"digital signature inside it. Do you wish to proceed?",
currentDocument().fileName()),
i18n("Sign Document"),
KStandardGuiItem::ok(),
KStandardGuiItem::cancel(),
QStringLiteral("SignatureAutoClobber"));
if (res == KMessageBox::Continue) {
// Sign it!
if (!d->m_generator->sign(data, currentDocument().path())) {
KMessageBox::error(d->m_widget, i18nc("%1 is a filename", "Could not sign '%1'. Invalid password or cannot write", currentDocument().fileName()));
}
// no need to - slotAttemptReload() gets called via
// dirty handler anyway:
// swapBackingFile(currentDocument().path(), currentDocument());
}
return d->m_generator->sign(data, newPath);
} else {
return false;
}
}
......
......@@ -999,7 +999,7 @@ public:
*
* @since 21.04
*/
void sign(const NewSignatureData &data);
bool sign(const NewSignatureData &data, const QString &newPath);
/**
* Returns the generator's certificate store (if any)
......
......@@ -788,11 +788,10 @@ void PageView::setupActions(KActionCollection *ac)
kundo->setEnabled(false);
kredo->setEnabled(false);
if (!d->annotator) {
d->annotator = new PageViewAnnotator(this, d->document);
connect(d->annotator, &PageViewAnnotator::toolSelected, d->aMouseNormal, &QAction::trigger);
connect(d->annotator, &PageViewAnnotator::toolSelected, d->mouseAnnotation, &MouseAnnotation::reset);
}
d->annotator = new PageViewAnnotator(this, d->document);
connect(d->annotator, &PageViewAnnotator::toolSelected, d->aMouseNormal, &QAction::trigger);
connect(d->annotator, &PageViewAnnotator::toolSelected, d->mouseAnnotation, &MouseAnnotation::reset);
connect(d->annotator, &PageViewAnnotator::requestOpenFile, this, &PageView::requestOpenFile);
d->annotator->setupActions(ac);
}
......@@ -4785,10 +4784,6 @@ void PageView::slotSignature()
d->messageWindow->display(i18n("Draw a rectangle to insert the signature field"), QString(), PageViewMessage::Info, -1);
if (!d->annotator) {
d->annotator = new PageViewAnnotator(this, d->document);
}
d->annotator->setSignatureMode(true);
// force an update of the cursor
......
......@@ -142,6 +142,7 @@ Q_SIGNALS:
void escPressed();
void fitWindowToPage(const QSize pageViewPortSize, const QSize pageSize);
void triggerSearch(const QString &text);
void requestOpenFile(const QString &filePath);
protected:
bool event(QEvent *event) override;
......
......@@ -17,9 +17,11 @@
#include <QColor>
#include <QEvent>
#include <QFile>
#include <QFileDialog>
#include <QInputDialog>
#include <QList>
#include <QLoggingCategory>
#include <QMimeDatabase>
#include <QPainter>
#include <QSet>
#include <QVariant>
......@@ -399,7 +401,7 @@ public:
return !certNicknameToUse.isEmpty();
}
void sign()
bool sign(const QString &newFilePath)
{
Okular::NewSignatureData data;
data.setCertNickname(certNicknameToUse);
......@@ -407,9 +409,8 @@ public:
data.setPassword(passToUse);
data.setPage(m_page->number());
data.setBoundingRectangle(rect);
m_document->sign(data);
passToUse = QString();
passToUse.clear();
return m_document->sign(data, newFilePath);
}
private:
......@@ -993,7 +994,27 @@ QRect PageViewAnnotator::performRouteMouseOrTabletEvent(const AnnotatorEngine::E
if (signatureMode()) {
auto signEngine = static_cast<PickPointEngineSignature *>(m_engine);
if (signEngine->isAccepted()) {
static_cast<PickPointEngineSignature *>(m_engine)->sign();
QMimeDatabase db;
const QString typeName = m_document->documentInfo().get(Okular::DocumentInfo::MimeType);
const QMimeType mimeType = db.mimeTypeForName(typeName);
const QString mimeTypeFilter = i18nc("File type name and pattern", "%1 (%2)", mimeType.comment(), mimeType.globPatterns().join(QLatin1Char(' ')));
const QUrl currentFileUrl = m_document->currentDocument();
const QFileInfo currentFileInfo(currentFileUrl.fileName());
const QString localFilePathIfAny = currentFileUrl.isLocalFile() ? QFileInfo(currentFileUrl.path()).canonicalPath() + QLatin1Char('/') : QString();
const QString newFileName =
localFilePathIfAny + i18nc("Used when suggesting a new name for a digitally signed file. %1 is the old file name and %2 it's extension", "%1_signed.%2", currentFileInfo.baseName(), currentFileInfo.completeSuffix());
const QString newFilePath = QFileDialog::getSaveFileName(m_pageView, i18n("Save Signed File As"), newFileName, mimeTypeFilter);
if (!newFilePath.isEmpty()) {
const bool success = static_cast<PickPointEngineSignature *>(m_engine)->sign(newFilePath);
if (success) {
emit requestOpenFile(newFilePath);
} else {
KMessageBox::error(m_pageView, i18nc("%1 is a file path", "Could not sign. Invalid certificate password or could not write to '%1'", newFilePath));
}
}
}
m_continuousMode = false;
}
......
......@@ -139,6 +139,7 @@ public Q_SLOTS:
Q_SIGNALS:
void toolSelected();
void requestOpenFile(const QString &filePath);
private:
void reparseBuiltinToolsConfig();
......
......@@ -539,6 +539,11 @@ Part::Part(QWidget *parentWidget, QObject *parent, const QVariantList &args)
connect(m_miniBar, &MiniBar::forwardKeyPressEvent, m_pageView, &PageView::externalKeyPressEvent);
connect(m_pageView.data(), &PageView::escPressed, m_findBar, &FindBar::resetSearch);
connect(m_pageNumberTool, &MiniBar::forwardKeyPressEvent, m_pageView, &PageView::externalKeyPressEvent);
connect(m_pageView.data(), &PageView::requestOpenFile, this, [this](const QString &filePath) {
// We cheat a bit here reusing the urlsDropped signal, but at the end the output is the same, we want to open some files
// urlsDropped should have just had a different name
Q_EMIT urlsDropped({QUrl::fromLocalFile(filePath)});
});
connect(m_reviewsWidget.data(), &Reviews::openAnnotationWindow, m_pageView.data(), &PageView::openAnnotationWindow);
......
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