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 
21 #include <comphelper/docpasswordrequest.hxx>
22 #include <com/sun/star/task/DocumentMSPasswordRequest2.hpp>
23 #include <com/sun/star/task/DocumentPasswordRequest2.hpp>
24 #include <com/sun/star/task/PasswordRequest.hpp>
25 #include <com/sun/star/task/XInteractionAbort.hpp>
26 #include <com/sun/star/task/XInteractionPassword2.hpp>
27 #include <cppuhelper/implbase.hxx>
28 
29 using ::com::sun::star::uno::Any;
30 using ::com::sun::star::uno::Reference;
31 using ::com::sun::star::uno::Sequence;
32 using ::com::sun::star::uno::XInterface;
33 using ::com::sun::star::task::InteractionClassification_QUERY;
34 using ::com::sun::star::task::DocumentMSPasswordRequest2;
35 using ::com::sun::star::task::DocumentPasswordRequest2;
36 using ::com::sun::star::task::PasswordRequest;
37 using ::com::sun::star::task::PasswordRequestMode;
38 using ::com::sun::star::task::XInteractionAbort;
39 using ::com::sun::star::task::XInteractionContinuation;
40 using ::com::sun::star::task::XInteractionPassword2;
41 
42 namespace comphelper {
43 
44 namespace {
45 
46 class AbortContinuation : public ::cppu::WeakImplHelper< XInteractionAbort >
47 {
48 public:
select()49     virtual void SAL_CALL select() override {}
50 };
51 
52 }
53 
54 class PasswordContinuation : public ::cppu::WeakImplHelper< XInteractionPassword2 >
55 {
56 public:
PasswordContinuation()57     explicit     PasswordContinuation() : mbReadOnly( false ), mbSelected( false ) {}
58 
isSelected() const59     bool     isSelected() const { return mbSelected; }
60 
select()61     virtual void SAL_CALL select() override { mbSelected = true; }
62 
setPassword(const OUString & rPass)63     virtual void SAL_CALL setPassword( const OUString& rPass ) override { maPassword = rPass; }
getPassword()64     virtual OUString SAL_CALL getPassword() override { return maPassword; }
65 
setPasswordToModify(const OUString & rPass)66     virtual void SAL_CALL setPasswordToModify( const OUString& rPass ) override { maModifyPassword = rPass; }
getPasswordToModify()67     virtual OUString SAL_CALL getPasswordToModify() override { return maModifyPassword; }
68 
setRecommendReadOnly(sal_Bool bReadOnly)69     virtual void SAL_CALL setRecommendReadOnly( sal_Bool bReadOnly ) override { mbReadOnly = bReadOnly; }
getRecommendReadOnly()70     virtual sal_Bool SAL_CALL getRecommendReadOnly() override { return mbReadOnly; }
71 
72 private:
73     OUString            maPassword;
74     OUString            maModifyPassword;
75     bool            mbReadOnly;
76     bool            mbSelected;
77 };
78 
79 
SimplePasswordRequest()80 SimplePasswordRequest::SimplePasswordRequest()
81 {
82     PasswordRequest aRequest( OUString(), Reference< XInterface >(),
83         InteractionClassification_QUERY, css::task::PasswordRequestMode_PASSWORD_CREATE );
84     maRequest <<= aRequest;
85 
86     mxAbort = new AbortContinuation;
87     mxPassword = new PasswordContinuation;
88 }
89 
~SimplePasswordRequest()90 SimplePasswordRequest::~SimplePasswordRequest()
91 {
92 }
93 
isPassword() const94 bool SimplePasswordRequest::isPassword() const
95 {
96     return mxPassword->isSelected();
97 }
98 
getPassword() const99 OUString SimplePasswordRequest::getPassword() const
100 {
101     return mxPassword->getPassword();
102 }
103 
getRequest()104 Any SAL_CALL SimplePasswordRequest::getRequest()
105 {
106     return maRequest;
107 }
108 
getContinuations()109 Sequence< Reference< XInteractionContinuation > > SAL_CALL SimplePasswordRequest::getContinuations()
110 {
111     return { mxAbort, mxPassword };
112 }
113 
114 
DocPasswordRequest(DocPasswordRequestType eType,PasswordRequestMode eMode,const OUString & rDocumentUrl,bool bPasswordToModify)115 DocPasswordRequest::DocPasswordRequest( DocPasswordRequestType eType,
116         PasswordRequestMode eMode, const OUString& rDocumentUrl, bool bPasswordToModify )
117 {
118     switch( eType )
119     {
120         case DocPasswordRequestType::Standard:
121         {
122             DocumentPasswordRequest2 aRequest( OUString(), Reference< XInterface >(),
123                 InteractionClassification_QUERY, eMode, rDocumentUrl, bPasswordToModify );
124             maRequest <<= aRequest;
125         }
126         break;
127         case DocPasswordRequestType::MS:
128         {
129             DocumentMSPasswordRequest2 aRequest( OUString(), Reference< XInterface >(),
130                 InteractionClassification_QUERY, eMode, rDocumentUrl, bPasswordToModify );
131             maRequest <<= aRequest;
132         }
133         break;
134         /*  no 'default', so compilers will complain about missing
135             implementation of a new enum value. */
136     }
137 
138     mxAbort = new AbortContinuation;
139     mxPassword = new PasswordContinuation;
140 }
141 
~DocPasswordRequest()142 DocPasswordRequest::~DocPasswordRequest()
143 {
144 }
145 
isPassword() const146 bool DocPasswordRequest::isPassword() const
147 {
148     return mxPassword->isSelected();
149 }
150 
getPassword() const151 OUString DocPasswordRequest::getPassword() const
152 {
153     return mxPassword->getPassword();
154 }
155 
getPasswordToModify() const156 OUString DocPasswordRequest::getPasswordToModify() const
157 {
158     return mxPassword->getPasswordToModify();
159 }
160 
getRecommendReadOnly() const161 bool DocPasswordRequest::getRecommendReadOnly() const
162 {
163     return mxPassword->getRecommendReadOnly();
164 }
165 
getRequest()166 Any SAL_CALL DocPasswordRequest::getRequest()
167 {
168     return maRequest;
169 }
170 
getContinuations()171 Sequence< Reference< XInteractionContinuation > > SAL_CALL DocPasswordRequest::getContinuations()
172 {
173     return { mxAbort, mxPassword };
174 }
175 
176 
177 } // namespace comphelper
178 
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
180