1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_XMLSECURITY_INC_CERTIFICATECHOOSER_HXX
21 #define INCLUDED_XMLSECURITY_INC_CERTIFICATECHOOSER_HXX
22 
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <vcl/weld.hxx>
25 
26 namespace com {
27 namespace sun {
28 namespace star {
29 namespace security {
30     class XCertificate; }
31 namespace xml { namespace crypto {
32     class XSecurityEnvironment; }}
33 }}}
34 
35 namespace com::sun::star::xml::crypto { class XXMLSecurityContext; }
36 
37 struct UserData
38 {
39     css::uno::Reference<css::security::XCertificate> xCertificate;
40     css::uno::Reference<css::xml::crypto::XXMLSecurityContext> xSecurityContext;
41     css::uno::Reference<css::xml::crypto::XSecurityEnvironment> xSecurityEnvironment;
42 };
43 
44 enum class UserAction
45 {
46     Sign,
47     SelectSign, // Select signing certificate
48     Encrypt
49 };
50 
51 class CertificateChooser : public weld::GenericDialogController
52 {
53 private:
54     std::vector< css::uno::Reference< css::xml::crypto::XXMLSecurityContext > > mxSecurityContexts;
55     std::vector<std::shared_ptr<UserData>> mvUserData;
56 
57     bool                    mbInitialized;
58     UserAction const        meAction;
59     OUString                msPreferredKey;
60     css::uno::Reference<css::security::XCertificate> mxEncryptToSelf;
61 
62     std::unique_ptr<weld::Label>    m_xFTSign;
63     std::unique_ptr<weld::Label>    m_xFTEncrypt;
64     std::unique_ptr<weld::TreeView> m_xCertLB;
65     std::unique_ptr<weld::Button>   m_xViewBtn;
66     std::unique_ptr<weld::Button>   m_xOKBtn;
67     std::unique_ptr<weld::Label>    m_xFTDescription;
68     std::unique_ptr<weld::Entry>    m_xDescriptionED;
69 
70     DECL_LINK(ViewButtonHdl, weld::Button&, void);
71     DECL_LINK(CertificateHighlightHdl, weld::TreeView&, void);
72     DECL_LINK(CertificateSelectHdl, weld::TreeView&, bool);
73 
74     void ImplShowCertificateDetails();
75     void ImplInitialize();
76 
77     static void HandleOneUsageBit(OUString& string, int& bits, int bit, const char *name);
78 
79 public:
80     CertificateChooser(weld::Window* pParent,
81                        std::vector< css::uno::Reference< css::xml::crypto::XXMLSecurityContext > > const & rxSecurityContexts,
82                        UserAction eAction);
83     virtual ~CertificateChooser() override;
84 
85     short run() override;
86 
87     css::uno::Sequence<css::uno::Reference< css::security::XCertificate > > GetSelectedCertificates();
88     css::uno::Reference< css::xml::crypto::XXMLSecurityContext > GetSelectedSecurityContext() const;
89     /// Gets the description string provided when selecting the certificate.
90     OUString GetDescription() const;
91 
92     /// Returns the usage string of the selected certificate, if any.
93     OUString GetUsageText();
94 
95     static OUString UsageInClearText(int bits);
96 };
97 
98 #endif // INCLUDED_XMLSECURITY_INC_CERTIFICATECHOOSER_HXX
99 
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
101