1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 
27 #include "comphelper/docpasswordrequest.hxx"
28 #include <com/sun/star/task/DocumentMSPasswordRequest2.hpp>
29 #include <com/sun/star/task/DocumentPasswordRequest2.hpp>
30 #include <com/sun/star/task/PasswordRequest.hpp>
31 #include <com/sun/star/task/XInteractionAbort.hpp>
32 #include <com/sun/star/task/XInteractionPassword2.hpp>
33 
34 using ::rtl::OUString;
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::uno::Type;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::RuntimeException;
39 using ::com::sun::star::uno::Sequence;
40 using ::com::sun::star::uno::XInterface;
41 using ::com::sun::star::task::InteractionClassification_QUERY;
42 using ::com::sun::star::task::DocumentMSPasswordRequest2;
43 using ::com::sun::star::task::DocumentPasswordRequest2;
44 using ::com::sun::star::task::PasswordRequest;
45 using ::com::sun::star::task::PasswordRequestMode;
46 using ::com::sun::star::task::XInteractionAbort;
47 using ::com::sun::star::task::XInteractionContinuation;
48 using ::com::sun::star::task::XInteractionPassword2;
49 using ::com::sun::star::task::XInteractionRequest;
50 
51 namespace comphelper {
52 
53 // ============================================================================
54 
55 class AbortContinuation : public ::cppu::WeakImplHelper1< XInteractionAbort >
56 {
57 public:
AbortContinuation()58     inline explicit     AbortContinuation() : mbSelected( false ) {}
59 
isSelected() const60     inline sal_Bool     isSelected() const { return mbSelected; }
reset()61     inline void         reset() { mbSelected = false; }
62 
select()63     virtual void SAL_CALL select() throw( RuntimeException ) { mbSelected = true; }
64 
65 private:
66     sal_Bool            mbSelected;
67 };
68 
69 // ============================================================================
70 
71 class PasswordContinuation : public ::cppu::WeakImplHelper1< XInteractionPassword2 >
72 {
73 public:
PasswordContinuation()74     inline explicit     PasswordContinuation() : mbReadOnly( sal_False ), mbSelected( sal_False ) {}
75 
isSelected() const76     inline sal_Bool     isSelected() const { return mbSelected; }
reset()77     inline void         reset() { mbSelected = sal_False; }
78 
select()79     virtual void SAL_CALL select() throw( RuntimeException ) { mbSelected = sal_True; }
80 
setPassword(const OUString & rPass)81     virtual void SAL_CALL setPassword( const OUString& rPass ) throw( RuntimeException ) { maPassword = rPass; }
getPassword()82     virtual OUString SAL_CALL getPassword() throw( RuntimeException ) { return maPassword; }
83 
setPasswordToModify(const OUString & rPass)84     virtual void SAL_CALL setPasswordToModify( const OUString& rPass ) throw( RuntimeException ) { maModifyPassword = rPass; }
getPasswordToModify()85     virtual OUString SAL_CALL getPasswordToModify() throw( RuntimeException ) { return maModifyPassword; }
86 
setRecommendReadOnly(sal_Bool bReadOnly)87     virtual void SAL_CALL setRecommendReadOnly( sal_Bool bReadOnly ) throw( RuntimeException ) { mbReadOnly = bReadOnly; }
getRecommendReadOnly()88     virtual sal_Bool SAL_CALL getRecommendReadOnly() throw( RuntimeException ) { return mbReadOnly; }
89 
90 private:
91     OUString            maPassword;
92     OUString            maModifyPassword;
93     sal_Bool            mbReadOnly;
94     sal_Bool            mbSelected;
95 };
96 
97 // ============================================================================
98 
SimplePasswordRequest(PasswordRequestMode eMode)99 SimplePasswordRequest::SimplePasswordRequest( PasswordRequestMode eMode )
100 : mpAbort( NULL )
101 , mpPassword( NULL )
102 {
103     PasswordRequest aRequest( OUString(), Reference< XInterface >(),
104         InteractionClassification_QUERY, eMode );
105     maRequest <<= aRequest;
106 
107     maContinuations.realloc( 2 );
108     maContinuations[ 0 ].set( mpAbort = new AbortContinuation );
109     maContinuations[ 1 ].set( mpPassword = new PasswordContinuation );
110 }
111 
~SimplePasswordRequest()112 SimplePasswordRequest::~SimplePasswordRequest()
113 {
114 }
115 
queryInterface(const Type & rType)116 /*uno::*/Any SAL_CALL SimplePasswordRequest::queryInterface( const /*uno::*/Type& rType ) throw (RuntimeException)
117 {
118     return ::cppu::queryInterface ( rType,
119             // OWeakObject interfaces
120             dynamic_cast< XInterface* > ( (XInteractionRequest *) this ),
121             static_cast< XWeak* > ( this ),
122             // my own interfaces
123             static_cast< XInteractionRequest*  > ( this ) );
124 }
125 
acquire()126 void SAL_CALL SimplePasswordRequest::acquire(  ) throw ()
127 {
128     OWeakObject::acquire();
129 }
130 
release()131 void SAL_CALL SimplePasswordRequest::release(  ) throw ()
132 {
133     OWeakObject::release();
134 }
135 
isAbort() const136 sal_Bool SimplePasswordRequest::isAbort() const
137 {
138     return mpAbort->isSelected();
139 }
140 
isPassword() const141 sal_Bool SimplePasswordRequest::isPassword() const
142 {
143     return mpPassword->isSelected();
144 }
145 
getPassword() const146 OUString SimplePasswordRequest::getPassword() const
147 {
148     return mpPassword->getPassword();
149 }
150 
getRequest()151 Any SAL_CALL SimplePasswordRequest::getRequest() throw( RuntimeException )
152 {
153     return maRequest;
154 }
155 
getContinuations()156 Sequence< Reference< XInteractionContinuation > > SAL_CALL SimplePasswordRequest::getContinuations() throw( RuntimeException )
157 {
158     return maContinuations;
159 }
160 
161 // ============================================================================
162 
DocPasswordRequest(DocPasswordRequestType eType,PasswordRequestMode eMode,const OUString & rDocumentName,sal_Bool bPasswordToModify)163 DocPasswordRequest::DocPasswordRequest( DocPasswordRequestType eType,
164         PasswordRequestMode eMode, const OUString& rDocumentName, sal_Bool bPasswordToModify )
165 : mpAbort( NULL )
166 , mpPassword( NULL )
167 {
168     switch( eType )
169     {
170         case DocPasswordRequestType_STANDARD:
171         {
172             DocumentPasswordRequest2 aRequest( OUString(), Reference< XInterface >(),
173                 InteractionClassification_QUERY, eMode, rDocumentName, bPasswordToModify );
174             maRequest <<= aRequest;
175         }
176         break;
177         case DocPasswordRequestType_MS:
178         {
179             DocumentMSPasswordRequest2 aRequest( OUString(), Reference< XInterface >(),
180                 InteractionClassification_QUERY, eMode, rDocumentName, bPasswordToModify );
181             maRequest <<= aRequest;
182         }
183         break;
184         /*  no 'default', so compilers will complain about missing
185             implementation of a new enum value. */
186     }
187 
188     maContinuations.realloc( 2 );
189     maContinuations[ 0 ].set( mpAbort = new AbortContinuation );
190     maContinuations[ 1 ].set( mpPassword = new PasswordContinuation );
191 }
192 
~DocPasswordRequest()193 DocPasswordRequest::~DocPasswordRequest()
194 {
195 }
196 
queryInterface(const Type & rType)197 /*uno::*/Any SAL_CALL DocPasswordRequest::queryInterface( const /*uno::*/Type& rType ) throw (RuntimeException)
198 {
199     return ::cppu::queryInterface ( rType,
200             // OWeakObject interfaces
201             dynamic_cast< XInterface* > ( (XInteractionRequest *) this ),
202             static_cast< XWeak* > ( this ),
203             // my own interfaces
204             static_cast< XInteractionRequest*  > ( this ) );
205 }
206 
acquire()207 void SAL_CALL DocPasswordRequest::acquire(  ) throw ()
208 {
209     OWeakObject::acquire();
210 }
211 
release()212 void SAL_CALL DocPasswordRequest::release(  ) throw ()
213 {
214     OWeakObject::release();
215 }
216 
isAbort() const217 sal_Bool DocPasswordRequest::isAbort() const
218 {
219     return mpAbort->isSelected();
220 }
221 
isPassword() const222 sal_Bool DocPasswordRequest::isPassword() const
223 {
224     return mpPassword->isSelected();
225 }
226 
getPassword() const227 OUString DocPasswordRequest::getPassword() const
228 {
229     return mpPassword->getPassword();
230 }
231 
getPasswordToModify() const232 OUString DocPasswordRequest::getPasswordToModify() const
233 {
234     return mpPassword->getPasswordToModify();
235 }
236 
getRecommendReadOnly() const237 sal_Bool DocPasswordRequest::getRecommendReadOnly() const
238 {
239     return mpPassword->getRecommendReadOnly();
240 }
241 
getRequest()242 Any SAL_CALL DocPasswordRequest::getRequest() throw( RuntimeException )
243 {
244     return maRequest;
245 }
246 
getContinuations()247 Sequence< Reference< XInteractionContinuation > > SAL_CALL DocPasswordRequest::getContinuations() throw( RuntimeException )
248 {
249     return maContinuations;
250 }
251 
252 // ============================================================================
253 
254 } // namespace comphelper
255 
256