diff --git a/core/signatureutils.cpp b/core/signatureutils.cpp index 627f3888e..454a8493f 100644 --- a/core/signatureutils.cpp +++ b/core/signatureutils.cpp @@ -175,3 +175,20 @@ QList CertificateStore::signingCertificates(bool *userCancell *userCancelled = false; return QList(); } + +QList CertificateStore::signingCertificatesForNow(bool *userCancelled, bool *nonDateValidCerts) const +{ + const QDateTime now = QDateTime::currentDateTime(); + QList certs = signingCertificates(userCancelled); + auto it = certs.begin(); + *nonDateValidCerts = false; + while (it != certs.end()) { + if ((*it)->validityStart() > now || now > (*it)->validityEnd()) { + it = certs.erase(it); + *nonDateValidCerts = true; + } else { + ++it; + } + } + return certs; +} diff --git a/core/signatureutils.h b/core/signatureutils.h index dbce450fc..c17c5133d 100644 --- a/core/signatureutils.h +++ b/core/signatureutils.h @@ -266,6 +266,15 @@ public: */ virtual QList signingCertificates(bool *userCancelled) const; + /** + * Returns list of valid, usable signing certificates for current date and time. + * + * This can ask the user for a password, userCancelled will be true if the user decided not to enter it. + * + * nonDateValidCerts is true if the user has signing certificates but their validity start date is in the future or past their validity end date. + */ + QList signingCertificatesForNow(bool *userCancelled, bool *nonDateValidCerts) const; + protected: CertificateStore(); diff --git a/part/pageview.cpp b/part/pageview.cpp index cab395171..1e3659120 100644 --- a/part/pageview.cpp +++ b/part/pageview.cpp @@ -4880,6 +4880,20 @@ void PageView::slotSetMouseTableSelect() d->annotator->detachAnnotation(); } +void PageView::showNoSigningCertificatesDialog(bool nonDateValidCerts) +{ + if (nonDateValidCerts) { + KMessageBox::information(this, i18n("All your signing certificates are either not valid yet or are past their validity date.")); + } else { + KMessageBox::information(this, + i18n("There are no available signing certificates.
For more information, please see the section about Adding Digital Signatures in the manual.", + QStringLiteral("help:/okular/signatures.html#adding_digital_signatures")), + QString(), + QString(), + KMessageBox::Notify | KMessageBox::AllowLink); + } +} + void PageView::slotSignature() { if (!d->document->isHistoryClean()) { @@ -4887,6 +4901,18 @@ void PageView::slotSignature() return; } + const Okular::CertificateStore *certStore = d->document->certificateStore(); + bool userCancelled, nonDateValidCerts; + const QList &certs = certStore->signingCertificatesForNow(&userCancelled, &nonDateValidCerts); + if (userCancelled) { + return; + } + + if (certs.isEmpty()) { + showNoSigningCertificatesDialog(nonDateValidCerts); + return; + } + d->messageWindow->display(i18n("Draw a rectangle to insert the signature field"), QString(), PageViewMessage::Info, -1); d->annotator->setSignatureMode(true); diff --git a/part/pageview.h b/part/pageview.h index 5b4f6305b..0297ea581 100644 --- a/part/pageview.h +++ b/part/pageview.h @@ -115,6 +115,8 @@ public: void highlightSignatureFormWidget(const Okular::FormFieldSignature *form); + void showNoSigningCertificatesDialog(bool nonDateValidCerts); + public Q_SLOTS: void copyTextSelection() const; void selectAll(); diff --git a/part/pageviewannotator.cpp b/part/pageviewannotator.cpp index 34a403688..393683b2d 100644 --- a/part/pageviewannotator.cpp +++ b/part/pageviewannotator.cpp @@ -355,39 +355,27 @@ public: } const Okular::CertificateStore *certStore = m_document->certificateStore(); - bool userCancelled; - const QList &certs = certStore->signingCertificates(&userCancelled); + bool userCancelled, nonDateValidCerts; + const QList &certs = certStore->signingCertificatesForNow(&userCancelled, &nonDateValidCerts); if (userCancelled) { m_aborted = true; return {}; } - QStringList items; - QHash nickToCert; - const QDateTime now = QDateTime::currentDateTime(); - for (auto cert : certs) { - if (cert->validityStart() <= now && now <= cert->validityEnd()) { - items.append(cert->nickName()); - nickToCert[cert->nickName()] = cert; - } - } - - if (items.isEmpty()) { + if (certs.isEmpty()) { m_creationCompleted = false; clicked = false; - if (certs.isEmpty()) { - KMessageBox::information(m_pageView, - i18n("There are no available signing certificates.
For more information, please see the section about Adding Digital Signatures in the manual.", - QStringLiteral("help:/okular/signatures.html#adding_digital_signatures")), - QString(), - QString(), - KMessageBox::Notify | KMessageBox::AllowLink); - } else { - KMessageBox::information(m_pageView, i18n("All your signing certificates are either not valid yet or are past their validity date.")); - } + m_pageView->showNoSigningCertificatesDialog(nonDateValidCerts); return {}; } + QStringList items; + QHash nickToCert; + for (auto cert : certs) { + items.append(cert->nickName()); + nickToCert[cert->nickName()] = cert; + } + bool resok = false; certNicknameToUse = QInputDialog::getItem(m_pageView, i18n("Select certificate to sign with"), i18n("Certificates:"), items, 0, false, &resok);