qt5/src/poppler-form.cc |   88 +++++++++++++++++++++++++++++++++++++++++++++++
 qt5/src/poppler-form.h  |   55 +++++++++++++++++++++++++++++
 qt6/src/poppler-form.cc |   89 ++++++++++++++++++++++++++++++++++++++++++++++++
 qt6/src/poppler-form.h  |   54 ++++++++++++++++++++++++++++-
 4 files changed, 284 insertions(+), 2 deletions(-)

New commits:
commit 83630a0ecf3db671c820b80f584e45ab9927c3fe
Author: Sune Vuorela <s...@vuorela.dk>
Date:   Wed May 24 23:04:24 2023 +0000

    Qt frontend code for selecting cryptosign backend

diff --git a/qt5/src/poppler-form.cc b/qt5/src/poppler-form.cc
index 05852bb4..770afd57 100644
--- a/qt5/src/poppler-form.cc
+++ b/qt5/src/poppler-form.cc
@@ -1147,6 +1147,94 @@ QVector<CertificateInfo> 
getAvailableSigningCertificates()
     return vReturnCerts;
 }
 
+static std::optional<CryptoSignBackend> 
convertToFrontend(std::optional<CryptoSign::Backend::Type> type)
+{
+    if (!type) {
+        return std::nullopt;
+    }
+    switch (type.value()) {
+    case CryptoSign::Backend::Type::NSS3:
+        return CryptoSignBackend::NSS;
+    case CryptoSign::Backend::Type::GPGME:
+        return CryptoSignBackend::GPG;
+    }
+    return std::nullopt;
+}
+
+static std::optional<CryptoSign::Backend::Type> 
convertToBackend(std::optional<CryptoSignBackend> backend)
+{
+    if (!backend) {
+        return std::nullopt;
+    }
+    switch (backend.value()) {
+    case CryptoSignBackend::NSS:
+        return CryptoSign::Backend::Type::NSS3;
+    case CryptoSignBackend::GPG:
+        return CryptoSign::Backend::Type::GPGME;
+    }
+    return std::nullopt;
+}
+
+QVector<CryptoSignBackend> availableCryptoSignBackends()
+{
+    QVector<CryptoSignBackend> backends;
+    for (auto &backend : CryptoSign::Factory::getAvailable()) {
+        auto converted = convertToFrontend(backend);
+        if (converted) {
+            backends.push_back(converted.value());
+        }
+    }
+    return backends;
+}
+
+std::optional<CryptoSignBackend> activeCryptoSignBackend()
+{
+    return convertToFrontend(CryptoSign::Factory::getActive());
+}
+
+bool setActiveCryptoSignBackend(CryptoSignBackend backend)
+{
+    auto available = availableCryptoSignBackends();
+    if (!available.contains(backend)) {
+        return false;
+    }
+    auto converted = convertToBackend(backend);
+    if (!converted) {
+        return false;
+    }
+    CryptoSign::Factory::setPreferredBackend(converted.value());
+    return activeCryptoSignBackend() == backend;
+}
+
+static bool hasNSSBackendFeature(CryptoSignBackendFeature feature)
+{
+    switch (feature) {
+    case CryptoSignBackendFeature::BackendAsksPassphrase:
+        return false;
+    }
+    return false;
+}
+
+static bool hasGPGBackendFeature(CryptoSignBackendFeature feature)
+{
+    switch (feature) {
+    case CryptoSignBackendFeature::BackendAsksPassphrase:
+        return true;
+    }
+    return false;
+}
+
+bool hasCryptoSignBackendFeature(CryptoSignBackend backend, 
CryptoSignBackendFeature feature)
+{
+    switch (backend) {
+    case CryptoSignBackend::NSS:
+        return hasNSSBackendFeature(feature);
+    case CryptoSignBackend::GPG:
+        return hasGPGBackendFeature(feature);
+    }
+    return false;
+}
+
 QString POPPLER_QT5_EXPORT getNSSDir()
 {
 #ifdef ENABLE_NSS3
diff --git a/qt5/src/poppler-form.h b/qt5/src/poppler-form.h
index 0e64f2ee..55b07093 100644
--- a/qt5/src/poppler-form.h
+++ b/qt5/src/poppler-form.h
@@ -35,6 +35,7 @@
 #include <functional>
 #include <memory>
 #include <ctime>
+#include <optional>
 #include <QtCore/QDateTime>
 #include <QtCore/QVector>
 #include <QtCore/QList>
@@ -846,12 +847,64 @@ private:
     Q_DISABLE_COPY(FormFieldSignature)
 };
 
+/**
+ * Possible compiled in backends for signature handling
+ *
+ * \since 23.06
+ */
+enum class CryptoSignBackend
+{
+    NSS,
+    GPG
+};
+
+/**
+ * The available compiled-in backends
+ *
+ * \since 23.06
+ */
+QVector<CryptoSignBackend> POPPLER_QT5_EXPORT availableCryptoSignBackends();
+
+/**
+ * Returns current active backend or nullopt if none is active
+ *
+ * \note there will always be an active backend if there is available backends
+ *
+ * \since 23.06
+ */
+std::optional<CryptoSignBackend> POPPLER_QT5_EXPORT activeCryptoSignBackend();
+
+/**
+ * Sets active backend
+ *
+ * \return true on success
+ *
+ * \since 23.06
+ */
+bool POPPLER_QT5_EXPORT setActiveCryptoSignBackend(CryptoSignBackend backend);
+
+enum class CryptoSignBackendFeature
+{
+    /// If the backend itself out of band requests passwords
+    /// or if the host applicaion somehow must do it
+    BackendAsksPassphrase
+};
+
+/**
+ * Queries if a backend supports or not supports a given feature.
+ *
+ * \since 23.06
+ */
+bool POPPLER_QT5_EXPORT hasCryptoSignBackendFeature(CryptoSignBackend, 
CryptoSignBackendFeature);
+
 /**
   Returns is poppler was compiled with NSS support
 
+  \deprecated Use availableBackends instead
+
   \since 21.01
 */
-bool POPPLER_QT5_EXPORT hasNSSSupport();
+bool POPPLER_QT5_DEPRECATED POPPLER_QT5_EXPORT hasNSSSupport();
 
 /**
   Return vector of suitable signing certificates
diff --git a/qt6/src/poppler-form.cc b/qt6/src/poppler-form.cc
index 83dc79d7..01655a81 100644
--- a/qt6/src/poppler-form.cc
+++ b/qt6/src/poppler-form.cc
@@ -1147,6 +1147,95 @@ QVector<CertificateInfo> 
getAvailableSigningCertificates()
     return vReturnCerts;
 }
 
+static std::optional<CryptoSignBackend> 
convertToFrontend(std::optional<CryptoSign::Backend::Type> type)
+{
+    if (!type) {
+        return std::nullopt;
+    }
+    switch (type.value()) {
+    case CryptoSign::Backend::Type::NSS3:
+        return CryptoSignBackend::NSS;
+    case CryptoSign::Backend::Type::GPGME:
+        return CryptoSignBackend::GPG;
+    }
+    return std::nullopt;
+}
+
+static std::optional<CryptoSign::Backend::Type> 
convertToBackend(std::optional<CryptoSignBackend> backend)
+{
+    if (!backend) {
+        return std::nullopt;
+    }
+
+    switch (backend.value()) {
+    case CryptoSignBackend::NSS:
+        return CryptoSign::Backend::Type::NSS3;
+    case CryptoSignBackend::GPG:
+        return CryptoSign::Backend::Type::GPGME;
+    }
+    return std::nullopt;
+}
+
+QVector<CryptoSignBackend> availableCryptoSignBackends()
+{
+    QVector<CryptoSignBackend> backends;
+    for (auto &backend : CryptoSign::Factory::getAvailable()) {
+        auto converted = convertToFrontend(backend);
+        if (converted) {
+            backends.push_back(converted.value());
+        }
+    }
+    return backends;
+}
+
+std::optional<CryptoSignBackend> activeCryptoSignBackend()
+{
+    return convertToFrontend(CryptoSign::Factory::getActive());
+}
+
+bool setActiveCryptoSignBackend(CryptoSignBackend backend)
+{
+    auto available = availableCryptoSignBackends();
+    if (!available.contains(backend)) {
+        return false;
+    }
+    auto converted = convertToBackend(backend);
+    if (!converted) {
+        return false;
+    }
+    CryptoSign::Factory::setPreferredBackend(converted.value());
+    return activeCryptoSignBackend() == backend;
+}
+
+static bool hasNSSBackendFeature(CryptoSignBackendFeature feature)
+{
+    switch (feature) {
+    case CryptoSignBackendFeature::BackendAsksPassphrase:
+        return false;
+    }
+    return false;
+}
+
+static bool hasGPGBackendFeature(CryptoSignBackendFeature feature)
+{
+    switch (feature) {
+    case CryptoSignBackendFeature::BackendAsksPassphrase:
+        return true;
+    }
+    return false;
+}
+
+bool hasCryptoSignBackendFeature(CryptoSignBackend backend, 
CryptoSignBackendFeature feature)
+{
+    switch (backend) {
+    case CryptoSignBackend::NSS:
+        return hasNSSBackendFeature(feature);
+    case CryptoSignBackend::GPG:
+        return hasGPGBackendFeature(feature);
+    }
+    return false;
+}
+
 QString POPPLER_QT6_EXPORT getNSSDir()
 {
 #ifdef ENABLE_NSS3
diff --git a/qt6/src/poppler-form.h b/qt6/src/poppler-form.h
index beba9fca..b64a6e02 100644
--- a/qt6/src/poppler-form.h
+++ b/qt6/src/poppler-form.h
@@ -35,6 +35,7 @@
 #include <functional>
 #include <memory>
 #include <ctime>
+#include <optional>
 #include <QtCore/QDateTime>
 #include <QtCore/QVector>
 #include <QtCore/QList>
@@ -794,13 +795,64 @@ public:
 private:
     Q_DISABLE_COPY(FormFieldSignature)
 };
+/**
+ * Possible compiled in backends for signature handling
+ *
+ * \since 23.06
+ */
+enum class CryptoSignBackend
+{
+    NSS,
+    GPG
+};
+
+/**
+ * The available compiled-in backends
+ *
+ * \since 23.06
+ */
+QVector<CryptoSignBackend> POPPLER_QT6_EXPORT availableCryptoSignBackends();
+
+/**
+ * Returns current active backend or nullopt if none is active
+ *
+ * \note there will always be an active backend if there is available backends
+ *
+ * \since 23.06
+ */
+std::optional<CryptoSignBackend> POPPLER_QT6_EXPORT activeCryptoSignBackend();
+
+/**
+ * Sets active backend
+ *
+ * \return true on success
+ *
+ * \since 23.06
+ */
+bool POPPLER_QT6_EXPORT setActiveCryptoSignBackend(CryptoSignBackend backend);
+
+enum class CryptoSignBackendFeature
+{
+    /// If the backend itself out of band requests passwords
+    /// or if the host applicaion somehow must do it
+    BackendAsksPassphrase
+};
+
+/**
+ * Queries if a backend supports or not supports a given feature.
+ *
+ * \since 23.06
+ */
+bool POPPLER_QT6_EXPORT hasCryptoSignBackendFeature(CryptoSignBackend, 
CryptoSignBackendFeature);
 
 /**
   Returns is poppler was compiled with NSS support
 
+  \deprecated Use availableBackends instead
+
   \since 21.01
 */
-bool POPPLER_QT6_EXPORT hasNSSSupport();
+bool POPPLER_QT6_DEPRECATED POPPLER_QT6_EXPORT hasNSSSupport();
 
 /**
   Return vector of suitable signing certificates

Reply via email to