1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 
31 #include "comphelper/docpasswordrequest.hxx"
32 #include <com/sun/star/task/DocumentMSPasswordRequest2.hpp>
33 #include <com/sun/star/task/DocumentPasswordRequest2.hpp>
34 #include <com/sun/star/task/PasswordRequest.hpp>
35 #include <com/sun/star/task/XInteractionAbort.hpp>
36 #include <com/sun/star/task/XInteractionPassword2.hpp>
37 
38 using ::rtl::OUString;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::Type;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::RuntimeException;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::uno::XInterface;
45 using ::com::sun::star::task::InteractionClassification_QUERY;
46 using ::com::sun::star::task::DocumentMSPasswordRequest2;
47 using ::com::sun::star::task::DocumentPasswordRequest2;
48 using ::com::sun::star::task::PasswordRequest;
49 using ::com::sun::star::task::PasswordRequestMode;
50 using ::com::sun::star::task::XInteractionAbort;
51 using ::com::sun::star::task::XInteractionContinuation;
52 using ::com::sun::star::task::XInteractionPassword2;
53 using ::com::sun::star::task::XInteractionRequest;
54 
55 namespace comphelper {
56 
57 // ============================================================================
58 
59 class AbortContinuation : public ::cppu::WeakImplHelper1< XInteractionAbort >
60 {
61 public:
62     inline explicit     AbortContinuation() : mbSelected( false ) {}
63 
64     inline sal_Bool     isSelected() const { return mbSelected; }
65     inline void         reset() { mbSelected = false; }
66 
67     virtual void SAL_CALL select() throw( RuntimeException ) { mbSelected = true; }
68 
69 private:
70     sal_Bool            mbSelected;
71 };
72 
73 // ============================================================================
74 
75 class PasswordContinuation : public ::cppu::WeakImplHelper1< XInteractionPassword2 >
76 {
77 public:
78     inline explicit     PasswordContinuation() : mbReadOnly( sal_False ), mbSelected( sal_False ) {}
79 
80     inline sal_Bool     isSelected() const { return mbSelected; }
81     inline void         reset() { mbSelected = sal_False; }
82 
83     virtual void SAL_CALL select() throw( RuntimeException ) { mbSelected = sal_True; }
84 
85     virtual void SAL_CALL setPassword( const OUString& rPass ) throw( RuntimeException ) { maPassword = rPass; }
86     virtual OUString SAL_CALL getPassword() throw( RuntimeException ) { return maPassword; }
87 
88     virtual void SAL_CALL setPasswordToModify( const OUString& rPass ) throw( RuntimeException ) { maModifyPassword = rPass; }
89     virtual OUString SAL_CALL getPasswordToModify() throw( RuntimeException ) { return maModifyPassword; }
90 
91     virtual void SAL_CALL setRecommendReadOnly( sal_Bool bReadOnly ) throw( RuntimeException ) { mbReadOnly = bReadOnly; }
92     virtual sal_Bool SAL_CALL getRecommendReadOnly() throw( RuntimeException ) { return mbReadOnly; }
93 
94 private:
95     OUString            maPassword;
96     OUString            maModifyPassword;
97     sal_Bool            mbReadOnly;
98     sal_Bool            mbSelected;
99 };
100 
101 // ============================================================================
102 
103 SimplePasswordRequest::SimplePasswordRequest( PasswordRequestMode eMode )
104 : mpAbort( NULL )
105 , mpPassword( NULL )
106 {
107     PasswordRequest aRequest( OUString(), Reference< XInterface >(),
108         InteractionClassification_QUERY, eMode );
109     maRequest <<= aRequest;
110 
111     maContinuations.realloc( 2 );
112     maContinuations[ 0 ].set( mpAbort = new AbortContinuation );
113     maContinuations[ 1 ].set( mpPassword = new PasswordContinuation );
114 }
115 
116 SimplePasswordRequest::~SimplePasswordRequest()
117 {
118 }
119 
120 /*uno::*/Any SAL_CALL SimplePasswordRequest::queryInterface( const /*uno::*/Type& rType ) throw (RuntimeException)
121 {
122     return ::cppu::queryInterface ( rType,
123             // OWeakObject interfaces
124             dynamic_cast< XInterface* > ( (XInteractionRequest *) this ),
125             static_cast< XWeak* > ( this ),
126             // my own interfaces
127             static_cast< XInteractionRequest*  > ( this ) );
128 }
129 
130 void SAL_CALL SimplePasswordRequest::acquire(  ) throw ()
131 {
132     OWeakObject::acquire();
133 }
134 
135 void SAL_CALL SimplePasswordRequest::release(  ) throw ()
136 {
137     OWeakObject::release();
138 }
139 
140 sal_Bool SimplePasswordRequest::isAbort() const
141 {
142     return mpAbort->isSelected();
143 }
144 
145 sal_Bool SimplePasswordRequest::isPassword() const
146 {
147     return mpPassword->isSelected();
148 }
149 
150 OUString SimplePasswordRequest::getPassword() const
151 {
152     return mpPassword->getPassword();
153 }
154 
155 Any SAL_CALL SimplePasswordRequest::getRequest() throw( RuntimeException )
156 {
157     return maRequest;
158 }
159 
160 Sequence< Reference< XInteractionContinuation > > SAL_CALL SimplePasswordRequest::getContinuations() throw( RuntimeException )
161 {
162     return maContinuations;
163 }
164 
165 // ============================================================================
166 
167 DocPasswordRequest::DocPasswordRequest( DocPasswordRequestType eType,
168         PasswordRequestMode eMode, const OUString& rDocumentName, sal_Bool bPasswordToModify )
169 : mpAbort( NULL )
170 , mpPassword( NULL )
171 {
172     switch( eType )
173     {
174         case DocPasswordRequestType_STANDARD:
175         {
176             DocumentPasswordRequest2 aRequest( OUString(), Reference< XInterface >(),
177                 InteractionClassification_QUERY, eMode, rDocumentName, bPasswordToModify );
178             maRequest <<= aRequest;
179         }
180         break;
181         case DocPasswordRequestType_MS:
182         {
183             DocumentMSPasswordRequest2 aRequest( OUString(), Reference< XInterface >(),
184                 InteractionClassification_QUERY, eMode, rDocumentName, bPasswordToModify );
185             maRequest <<= aRequest;
186         }
187         break;
188         /*  no 'default', so compilers will complain about missing
189             implementation of a new enum value. */
190     }
191 
192     maContinuations.realloc( 2 );
193     maContinuations[ 0 ].set( mpAbort = new AbortContinuation );
194     maContinuations[ 1 ].set( mpPassword = new PasswordContinuation );
195 }
196 
197 DocPasswordRequest::~DocPasswordRequest()
198 {
199 }
200 
201 /*uno::*/Any SAL_CALL DocPasswordRequest::queryInterface( const /*uno::*/Type& rType ) throw (RuntimeException)
202 {
203     return ::cppu::queryInterface ( rType,
204             // OWeakObject interfaces
205             dynamic_cast< XInterface* > ( (XInteractionRequest *) this ),
206             static_cast< XWeak* > ( this ),
207             // my own interfaces
208             static_cast< XInteractionRequest*  > ( this ) );
209 }
210 
211 void SAL_CALL DocPasswordRequest::acquire(  ) throw ()
212 {
213     OWeakObject::acquire();
214 }
215 
216 void SAL_CALL DocPasswordRequest::release(  ) throw ()
217 {
218     OWeakObject::release();
219 }
220 
221 sal_Bool DocPasswordRequest::isAbort() const
222 {
223     return mpAbort->isSelected();
224 }
225 
226 sal_Bool DocPasswordRequest::isPassword() const
227 {
228     return mpPassword->isSelected();
229 }
230 
231 OUString DocPasswordRequest::getPassword() const
232 {
233     return mpPassword->getPassword();
234 }
235 
236 OUString DocPasswordRequest::getPasswordToModify() const
237 {
238     return mpPassword->getPasswordToModify();
239 }
240 
241 sal_Bool DocPasswordRequest::getRecommendReadOnly() const
242 {
243     return mpPassword->getRecommendReadOnly();
244 }
245 
246 Any SAL_CALL DocPasswordRequest::getRequest() throw( RuntimeException )
247 {
248     return maRequest;
249 }
250 
251 Sequence< Reference< XInteractionContinuation > > SAL_CALL DocPasswordRequest::getContinuations() throw( RuntimeException )
252 {
253     return maContinuations;
254 }
255 
256 // ============================================================================
257 
258 } // namespace comphelper
259 
260