1 /* poppler-form.h: qt interface to poppler
2  * Copyright (C) 2007-2008, Pino Toscano <pino@kde.org>
3  * Copyright (C) 2008, 2011, 2016, 2017, 2019-2021, Albert Astals Cid <aacid@kde.org>
4  * Copyright (C) 2012, Adam Reichold <adamreichold@myopera.com>
5  * Copyright (C) 2016, Hanno Meyer-Thurow <h.mth@web.de>
6  * Copyright (C) 2017, Hans-Ulrich Jüttner <huj@froreich-bioscientia.de>
7  * Copyright (C) 2017, Tobias C. Berner <tcberner@freebsd.org>
8  * Copyright (C) 2018, Andre Heinecke <aheinecke@intevation.de>
9  * Copyright (C) 2018, Chinmoy Ranjan Pradhan <chinmoyrp65@protonmail.com>
10  * Copyright (C) 2018, Oliver Sander <oliver.sander@tu-dresden.de>
11  * Copyright (C) 2019 João Netto <joaonetto901@gmail.com>
12  * Copyright (C) 2019, Adrian Johnson <ajohnson@redneon.com>
13  * Copyright (C) 2020, Thorsten Behrens <Thorsten.Behrens@CIB.de>
14  * Copyright (C) 2020, Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by Technische Universität Dresden
15  * Copyright (C) 2021, Theofilos Intzoglou <int.teo@gmail.com>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2, or (at your option)
20  * any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
30  */
31 
32 #ifndef _POPPLER_QT5_FORM_H_
33 #define _POPPLER_QT5_FORM_H_
34 
35 #include <functional>
36 #include <memory>
37 #include <ctime>
38 #include <QtCore/QDateTime>
39 #include <QtCore/QVector>
40 #include <QtCore/QList>
41 #include <QtCore/QRectF>
42 #include <QtCore/QStringList>
43 #include <QtCore/QSharedPointer>
44 #include "poppler-export.h"
45 #include "poppler-annotation.h"
46 
47 class Object;
48 class Page;
49 class FormWidget;
50 class FormWidgetButton;
51 class FormWidgetText;
52 class FormWidgetChoice;
53 class FormWidgetSignature;
54 
55 namespace Poppler {
56 
57 class DocumentData;
58 class Link;
59 
60 class FormFieldData;
61 class FormFieldIconData;
62 
63 /**
64      The class containing the appearance information
65 
66      \since 0.79
67  */
68 
69 class POPPLER_QT5_EXPORT FormFieldIcon
70 {
71 
72     friend class FormFieldIconData;
73 
74 public:
75     explicit FormFieldIcon(FormFieldIconData *data);
76     FormFieldIcon(const FormFieldIcon &ffIcon);
77     ~FormFieldIcon();
78 
79     FormFieldIcon &operator=(const FormFieldIcon &ffIcon);
80 
81 private:
82     FormFieldIconData *d_ptr;
83 };
84 /**
85   The base class representing a form field.
86 
87   \since 0.6
88  */
89 class POPPLER_QT5_EXPORT FormField
90 {
91 
92     friend class FormFieldData;
93 
94 public:
95     /**
96        The different types of form field.
97     */
98     enum FormType
99     {
100         FormButton, ///< A button field. See \ref Poppler::FormFieldButton::ButtonType "ButtonType"
101         FormText, ///< A text field. See \ref Poppler::FormFieldText::TextType "TextType"
102         FormChoice, ///< A single choice field. See \ref Poppler::FormFieldChoice::ChoiceType "ChoiceType"
103         FormSignature ///< A signature field.
104     };
105 
106     virtual ~FormField();
107 
108     /**
109       The type of the field.
110      */
111     virtual FormType type() const = 0;
112 
113     /**
114        \return The size of the field, in normalized coordinates, i.e.
115        [0..1] with regard to the dimensions (cropbox) of the page
116     */
117     QRectF rect() const;
118 
119     /**
120       The ID of the field.
121      */
122     int id() const;
123 
124     /**
125       The internal name (T) of the field.
126      */
127     QString name() const;
128 
129     /**
130       Sets the internal name (T) of the field.
131       \since 0.51
132      */
133     void setName(const QString &name) const;
134 
135     /**
136       The internal fully qualified name of the field.
137       \since 0.18
138      */
139     QString fullyQualifiedName() const;
140 
141     /**
142       The name of the field to be used in user interface (eg messages to
143       the user).
144      */
145     QString uiName() const;
146 
147     /**
148       Whether this form field is read-only.
149      */
150     bool isReadOnly() const;
151 
152     /**
153       Set whether this form field is read-only.
154       \since 0.64
155      */
156     void setReadOnly(bool value);
157 
158     /**
159       Whether this form field is visible.
160      */
161     bool isVisible() const;
162 
163     /**
164       Set whether this form field is visible.
165       \since 0.64
166      */
167     void setVisible(bool value);
168 
169     /**
170       Whether this field is printable.
171       \since 0.79
172      */
173     bool isPrintable() const;
174 
175     /**
176       Set whether this field is printable.
177       \since 0.79
178      */
179     void setPrintable(bool value);
180 
181     /**
182       The activation action of this form field.
183 
184       \note It may be null.
185      */
186     Link *activationAction() const;
187 
188     /**
189      * Describes the flags from the form 'AA' dictionary.
190      *
191      * \since 0.53
192      */
193     enum AdditionalActionType
194     {
195         FieldModified, ///< A JavaScript action to be performed when the user modifies the field
196         FormatField, ///< A JavaScript action to be performed before the field is formatted to display its value
197         ValidateField, ///< A JavaScript action to be performed when the field value changes
198         CalculateField, ///< A JavaScript action to be performed when the field needs to be recalculated
199     };
200     /**
201      * Returns a given form additional action
202      *
203      * \since 0.53
204      */
205     Link *additionalAction(AdditionalActionType type) const;
206 
207     /**
208      * Returns a given widget annotation additional action
209      *
210      * \since 0.65
211      */
212     Link *additionalAction(Annotation::AdditionalActionType type) const;
213 
214 protected:
215     /// \cond PRIVATE
216     explicit FormField(std::unique_ptr<FormFieldData> dd);
217 
218     std::unique_ptr<FormFieldData> m_formData;
219     /// \endcond
220 
221 private:
222     Q_DISABLE_COPY(FormField)
223 };
224 
225 /**
226   A form field that represents a "button".
227 
228   \since 0.8
229  */
230 class POPPLER_QT5_EXPORT FormFieldButton : public FormField
231 {
232 public:
233     /**
234      * The types of button field.
235      */
236     enum ButtonType
237     {
238         Push, ///< A simple push button.
239         CheckBox, ///< A check box.
240         Radio ///< A radio button.
241     };
242 
243     /// \cond PRIVATE
244     FormFieldButton(DocumentData *doc, ::Page *p, ::FormWidgetButton *w);
245     /// \endcond
246     ~FormFieldButton() override;
247 
248     FormType type() const override;
249 
250     /**
251       The particular type of the button field.
252      */
253     ButtonType buttonType() const;
254 
255     /**
256      * The caption to be used for the button.
257      */
258     QString caption() const;
259 
260     /**
261      * Gets the icon used by the button
262      *
263      * \since 0.79
264      */
265     FormFieldIcon icon() const;
266 
267     /**
268      * Sets a new icon for the button, it has to be a icon
269      * returned by FormFieldButton::icon.
270      *
271      * \since 0.79
272      */
273     void setIcon(const FormFieldIcon &icon);
274 
275     /**
276       The state of the button.
277      */
278     bool state() const;
279 
280     /**
281       Sets the state of the button to the new \p state .
282      */
283     void setState(bool state);
284 
285     /**
286       The list with the IDs of siblings (ie, buttons belonging to the same
287       group as the current one.
288 
289       Valid only for \ref Radio buttons, an empty list otherwise.
290      */
291     QList<int> siblings() const;
292 
293 private:
294     Q_DISABLE_COPY(FormFieldButton)
295 };
296 
297 /**
298   A form field that represents a text input.
299 
300   \since 0.6
301  */
302 class POPPLER_QT5_EXPORT FormFieldText : public FormField
303 {
304 public:
305     /**
306        The particular type of this text field.
307     */
308     enum TextType
309     {
310         Normal, ///< A simple singleline text field.
311         Multiline, ///< A multiline text field.
312         FileSelect ///< An input field to select the path of a file on disk.
313     };
314 
315     /// \cond PRIVATE
316     FormFieldText(DocumentData *doc, ::Page *p, ::FormWidgetText *w);
317     /// \endcond
318     ~FormFieldText() override;
319 
320     FormType type() const override;
321 
322     /**
323       The text type of the text field.
324      */
325     TextType textType() const;
326 
327     /**
328       The text associated with the text field.
329      */
330     QString text() const;
331 
332     /**
333       Sets the text associated with the text field to the specified
334       \p text.
335      */
336     void setText(const QString &text);
337 
338     /**
339       Sets the text inside the Appearance Stream to the specified
340       \p text
341       \since 0.80
342      */
343     void setAppearanceText(const QString &text);
344 
345     /**
346       Whether this text field is a password input, eg its text \b must be
347       replaced with asterisks.
348 
349       Always false for \ref FileSelect text fields.
350      */
351     bool isPassword() const;
352 
353     /**
354       Whether this text field should allow rich text.
355      */
356     bool isRichText() const;
357 
358     /**
359       The maximum length for the text of this field, or -1 if not set.
360      */
361     int maximumLength() const;
362 
363     /**
364       The horizontal alignment for the text of this text field.
365      */
366     Qt::Alignment textAlignment() const;
367 
368     /**
369       Whether the text inserted manually in the field (where possible)
370       can be spell-checked.
371      */
372     bool canBeSpellChecked() const;
373 
374     /**
375       The font size of the text in the form field
376      */
377     double getFontSize() const;
378 
379     /**
380       Set the font size of the text in the form field (currently only as integer)
381      */
382     void setFontSize(int fontSize);
383 
384 private:
385     Q_DISABLE_COPY(FormFieldText)
386 };
387 
388 /**
389   A form field that represents a choice field.
390 
391   \since 0.6
392  */
393 class POPPLER_QT5_EXPORT FormFieldChoice : public FormField
394 {
395 public:
396     /**
397        The particular type of this choice field.
398     */
399     enum ChoiceType
400     {
401         ComboBox, ///< A simple singleline text field.
402         ListBox ///< A multiline text field.
403     };
404 
405     /// \cond PRIVATE
406     FormFieldChoice(DocumentData *doc, ::Page *p, ::FormWidgetChoice *w);
407     /// \endcond
408     ~FormFieldChoice() override;
409 
410     FormType type() const override;
411 
412     /**
413       The choice type of the choice field.
414      */
415     ChoiceType choiceType() const;
416 
417     /**
418       The possible choices of the choice field.
419      */
420     QStringList choices() const;
421 
422     /**
423       The possible choices of the choice field.
424       The first value of the pair is the display name of the choice,
425       The second value is the export value (i.e. for use in javascript, etc) of the choice
426       @since 0.87
427      */
428     QVector<QPair<QString, QString>> choicesWithExportValues() const;
429 
430     /**
431       Whether this FormFieldChoice::ComboBox is editable, i.e. the user
432       can type in a custom value.
433 
434       Always false for the other types of choices.
435      */
436     bool isEditable() const;
437 
438     /**
439       Whether more than one choice of this FormFieldChoice::ListBox
440       can be selected at the same time.
441 
442       Always false for the other types of choices.
443      */
444     bool multiSelect() const;
445 
446     /**
447       The currently selected choices.
448      */
449     QList<int> currentChoices() const;
450 
451     /**
452       Sets the selected choices to \p choice.
453      */
454     void setCurrentChoices(const QList<int> &choice);
455 
456     /**
457       The text entered into an editable combo box choice field. Otherwise a null string.
458 
459       \since 0.22
460     */
461     QString editChoice() const;
462 
463     /**
464       Sets the text entered into an editable combo box choice field. Otherwise does nothing.
465 
466       \since 0.22
467     */
468     void setEditChoice(const QString &text);
469 
470     /**
471       The horizontal alignment for the text of this text field.
472      */
473     Qt::Alignment textAlignment() const;
474 
475     /**
476       Whether the text inserted manually in the field (where possible)
477       can be spell-checked.
478 
479       Returns false if the field is not an editable text field.
480      */
481     bool canBeSpellChecked() const;
482 
483 private:
484     Q_DISABLE_COPY(FormFieldChoice)
485 };
486 
487 /**
488   A helper class to store x509 certificate information.
489 
490   \since 0.73
491  */
492 class CertificateInfoPrivate;
493 class POPPLER_QT5_EXPORT CertificateInfo
494 {
495 public:
496     /**
497       The algorithm of public key.
498      */
499     enum PublicKeyType
500     {
501         RsaKey,
502         DsaKey,
503         EcKey,
504         OtherKey
505     };
506 
507     /**
508       Certificate key usage extensions.
509      */
510     enum KeyUsageExtension
511     {
512         KuDigitalSignature = 0x80,
513         KuNonRepudiation = 0x40,
514         KuKeyEncipherment = 0x20,
515         KuDataEncipherment = 0x10,
516         KuKeyAgreement = 0x08,
517         KuKeyCertSign = 0x04,
518         KuClrSign = 0x02,
519         KuEncipherOnly = 0x01,
520         KuNone = 0x00
521     };
522     Q_DECLARE_FLAGS(KeyUsageExtensions, KeyUsageExtension)
523 
524     /**
525       Predefined keys for elements in an entity's distinguished name.
526      */
527     enum EntityInfoKey
528     {
529         CommonName,
530         DistinguishedName,
531         EmailAddress,
532         Organization,
533     };
534 
535     CertificateInfo();
536     explicit CertificateInfo(CertificateInfoPrivate *priv);
537     ~CertificateInfo();
538 
539     /**
540       Returns true if certificate has no contents; otherwise returns false
541      */
542     bool isNull() const;
543 
544     /**
545       The certificate version string.
546      */
547     int version() const;
548 
549     /**
550       The certificate serial number.
551      */
552     QByteArray serialNumber() const;
553 
554     /**
555       Information about the issuer.
556      */
557     QString issuerInfo(EntityInfoKey key) const;
558 
559     /**
560       Information about the subject
561      */
562     QString subjectInfo(EntityInfoKey key) const;
563 
564     /**
565       The certificate internal database nickname
566 
567       \since 21.01
568      */
569     QString nickName() const;
570 
571     /**
572       The date-time when certificate becomes valid.
573      */
574     QDateTime validityStart() const;
575 
576     /**
577       The date-time when certificate expires.
578      */
579     QDateTime validityEnd() const;
580 
581     /**
582       The uses allowed for the certificate.
583      */
584     KeyUsageExtensions keyUsageExtensions() const;
585 
586     /**
587       The public key value.
588      */
589     QByteArray publicKey() const;
590 
591     /**
592       The public key type.
593      */
594     PublicKeyType publicKeyType() const;
595 
596     /**
597       The strength of public key in bits.
598      */
599     int publicKeyStrength() const;
600 
601     /**
602       Returns true if certificate is self-signed otherwise returns false.
603      */
604     bool isSelfSigned() const;
605 
606     /**
607       The DER encoded certificate.
608      */
609     QByteArray certificateData() const;
610 
611     /**
612       Checks if the given password is the correct one for this certificate
613 
614       \since 21.01
615      */
616     bool checkPassword(const QString &password) const;
617 
618     CertificateInfo(const CertificateInfo &other);
619     CertificateInfo &operator=(const CertificateInfo &other);
620 
621 private:
622     Q_DECLARE_PRIVATE(CertificateInfo)
623 
624     QSharedPointer<CertificateInfoPrivate> d_ptr;
625 };
626 Q_DECLARE_OPERATORS_FOR_FLAGS(CertificateInfo::KeyUsageExtensions)
627 
628 /**
629   A signature validation info helper class.
630 
631   \since 0.51
632  */
633 class SignatureValidationInfoPrivate;
634 class POPPLER_QT5_EXPORT SignatureValidationInfo
635 {
636 public:
637     /**
638        The verification result of the signature.
639     */
640     enum SignatureStatus
641     {
642         SignatureValid, ///< The signature is cryptographically valid.
643         SignatureInvalid, ///< The signature is cryptographically invalid.
644         SignatureDigestMismatch, ///< The document content was changed after the signature was applied.
645         SignatureDecodingError, ///< The signature CMS/PKCS7 structure is malformed.
646         SignatureGenericError, ///< The signature could not be verified.
647         SignatureNotFound, ///< The requested signature is not present in the document.
648         SignatureNotVerified ///< The signature is not yet verified.
649     };
650 
651     /**
652        The verification result of the certificate.
653     */
654     enum CertificateStatus
655     {
656         CertificateTrusted, ///< The certificate is considered trusted.
657         CertificateUntrustedIssuer, ///< The issuer of this certificate has been marked as untrusted by the user.
658         CertificateUnknownIssuer, ///< The certificate trust chain has not finished in a trusted root certificate.
659         CertificateRevoked, ///< The certificate was revoked by the issuing certificate authority.
660         CertificateExpired, ///< The signing time is outside the validity bounds of this certificate.
661         CertificateGenericError, ///< The certificate could not be verified.
662         CertificateNotVerified ///< The certificate is not yet verified.
663     };
664 
665     /**
666         The hash algorithm of the signature
667         \since 0.58
668      */
669     enum HashAlgorithm
670     {
671         HashAlgorithmUnknown,
672         HashAlgorithmMd2,
673         HashAlgorithmMd5,
674         HashAlgorithmSha1,
675         HashAlgorithmSha256,
676         HashAlgorithmSha384,
677         HashAlgorithmSha512,
678         HashAlgorithmSha224
679     };
680 
681     /// \cond PRIVATE
682     explicit SignatureValidationInfo(SignatureValidationInfoPrivate *priv);
683     /// \endcond
684     ~SignatureValidationInfo();
685 
686     /**
687       The signature status of the signature.
688      */
689     SignatureStatus signatureStatus() const;
690 
691     /**
692       The certificate status of the signature.
693      */
694     CertificateStatus certificateStatus() const;
695 
696     /**
697       The signer name associated with the signature.
698      */
699     QString signerName() const;
700 
701     /**
702       The signer subject distinguished name associated with the signature.
703       \since 0.58
704      */
705     QString signerSubjectDN() const;
706 
707     /**
708       Get signing location.
709       \since 0.68
710     */
711     QString location() const;
712 
713     /**
714       Get signing reason.
715       \since 0.68
716     */
717     QString reason() const;
718 
719     /**
720       The hash algorithm used for the signature.
721       \since 0.58
722      */
723     HashAlgorithm hashAlgorithm() const;
724 
725     /**
726       The signing time associated with the signature.
727      */
728     time_t signingTime() const;
729 
730     /**
731       Get the signature binary data.
732       \since 0.58
733      */
734     QByteArray signature() const;
735 
736     /**
737       Get the bounds of the ranges of the document which are signed.
738       \since 0.58
739      */
740     QList<qint64> signedRangeBounds() const;
741 
742     /**
743       Checks whether the signature authenticates the total document
744       except for the signature itself.
745       \since 0.58
746      */
747     bool signsTotalDocument() const;
748 
749     /**
750       The signer certificate info.
751       \since 0.73
752     */
753     CertificateInfo certificateInfo() const;
754 
755     SignatureValidationInfo(const SignatureValidationInfo &other);
756     SignatureValidationInfo &operator=(const SignatureValidationInfo &other);
757 
758 private:
759     Q_DECLARE_PRIVATE(SignatureValidationInfo)
760 
761     QSharedPointer<SignatureValidationInfoPrivate> d_ptr;
762 };
763 
764 /**
765   A form field that represents a signature.
766 
767   \since 0.51
768  */
769 class POPPLER_QT5_EXPORT FormFieldSignature : public FormField
770 {
771 public:
772     /**
773         The types of signature fields.
774         \since 0.58
775     */
776     enum SignatureType
777     {
778         AdbePkcs7sha1,
779         AdbePkcs7detached,
780         EtsiCAdESdetached,
781         UnknownSignatureType ///< \since 0.90
782     };
783 
784     /**
785        The validation options of this signature.
786     */
787     enum ValidateOptions
788     {
789         ValidateVerifyCertificate = 1, ///< Validate the certificate.
790         ValidateForceRevalidation = 2, ///< Force revalidation of the certificate.
791         ValidateWithoutOCSPRevocationCheck = 4, ///< Do not contact OCSP servers to check for certificate revocation status \since 21.10
792         ValidateUseAIACertFetch = 8 ///< Use the AIA extension for certificate fetching \since 21.10
793     };
794 
795     /// \cond PRIVATE
796     FormFieldSignature(DocumentData *doc, ::Page *p, ::FormWidgetSignature *w);
797     /// \endcond
798     ~FormFieldSignature() override;
799 
800     FormType type() const override;
801 
802     /**
803         The signature type
804         \since 0.58
805     */
806     SignatureType signatureType() const;
807 
808     /**
809       Validate the signature with now as validation time.
810 
811       Reset signature validatation info of scoped instance.
812      */
813     SignatureValidationInfo validate(ValidateOptions opt) const;
814 
815     /**
816       Validate the signature with @p validationTime as validation time.
817 
818       Reset signature validatation info of scoped instance.
819 
820       \since 0.58
821      */
822     SignatureValidationInfo validate(int opt, const QDateTime &validationTime) const;
823 
824 private:
825     Q_DISABLE_COPY(FormFieldSignature)
826 };
827 
828 /**
829   Returns is poppler was compiled with NSS support
830 
831   \since 21.01
832 */
833 bool POPPLER_QT5_EXPORT hasNSSSupport();
834 
835 /**
836   Return vector of suitable signing certificates
837 
838   \since 21.01
839 */
840 QVector<CertificateInfo> POPPLER_QT5_EXPORT getAvailableSigningCertificates();
841 
842 /**
843   Gets the current NSS CertDB directory
844 
845   \since 21.01
846 */
847 QString POPPLER_QT5_EXPORT getNSSDir();
848 
849 /**
850   Set a custom NSS CertDB directory. Needs to be called before doing any other signature operation
851 
852   \since 21.01
853 */
854 void POPPLER_QT5_EXPORT setNSSDir(const QString &pathURL);
855 
856 /**
857   Sets the callback for NSS password requests
858 
859   \since 21.01
860 */
861 void POPPLER_QT5_EXPORT setNSSPasswordCallback(const std::function<char *(const char *)> &f);
862 }
863 
864 #endif
865