xref: /aoo41x/main/cui/source/dialogs/passwdomdlg.cxx (revision cdf0e10c)
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_cui.hxx"
30 
31 // include ---------------------------------------------------------------
32 
33 #include "passwdomdlg.hrc"
34 #include "passwdomdlg.hxx"
35 
36 #include "cuires.hrc"
37 #include "dialmgr.hxx"
38 
39 #include <sfx2/tabdlg.hxx>
40 #include <tools/debug.hxx>
41 #include <vcl/fixed.hxx>
42 #include <vcl/edit.hxx>
43 #include <vcl/button.hxx>
44 #include <vcl/morebtn.hxx>
45 #include <vcl/settings.hxx>
46 #include <vcl/msgbox.hxx>
47 
48 
49 //////////////////////////////////////////////////////////////////////
50 
51 class PasswordReenterEdit_Impl : public Edit
52 {
53     String  m_aDefaultTxt;
54 
55     // disallow use of copy c-tor and assignment operator
56     PasswordReenterEdit_Impl( const PasswordReenterEdit_Impl & );
57     PasswordReenterEdit_Impl & operator = ( const PasswordReenterEdit_Impl & );
58 
59 public:
60     PasswordReenterEdit_Impl( Window * pParent, const ResId &rResId );
61     virtual ~PasswordReenterEdit_Impl();
62 
63     // Edit
64     virtual void        Paint( const Rectangle& rRect );
65 };
66 
67 
68 PasswordReenterEdit_Impl::PasswordReenterEdit_Impl( Window * pParent, const ResId &rResId ) :
69     Edit( pParent, rResId )
70 {
71 // currently the spec does not want to display this text anymore...
72 //    m_aDefaultTxt = String( CUI_RES( STR_PASSWD_MUST_BE_CONFIRMED ) );
73 }
74 
75 
76 PasswordReenterEdit_Impl::~PasswordReenterEdit_Impl()
77 {
78 }
79 
80 
81 void PasswordReenterEdit_Impl::Paint( const Rectangle& rRect )
82 {
83     if (GetText().Len() == 0)
84     {
85         Push( /*PUSH_FILLCOLOR | PUSH_TEXTFILLCOLOR |*/ PUSH_TEXTCOLOR );
86 /*
87         Color aFillColor( GetParent()->GetBackground().GetColor() );
88         SetLineColor(); // don't draw a border when painting the Edit field rectangle with the new background color
89         SetFillColor( aFillColor );
90         SetTextFillColor( aFillColor );
91         SetTextColor( GetParent()->GetTextColor() );    // use plain text color even if the Edit field is disabled (it is hard to read the text otherwise)
92 
93         DrawRect( Rectangle( Point(), GetOutputSizePixel() ) );
94 */
95         SetTextColor( Color( COL_GRAY ) );
96         DrawText( Point(), m_aDefaultTxt );
97 
98         Pop();
99     }
100     else
101         Edit::Paint( rRect );
102 }
103 
104 
105 //////////////////////////////////////////////////////////////////////
106 
107 struct PasswordToOpenModifyDialog_Impl
108 {
109     PasswordToOpenModifyDialog *    m_pParent;
110 
111     FixedLine                   m_aFileEncryptionFL;
112     FixedText                   m_aPasswdToOpenFT;
113     Edit                        m_aPasswdToOpenED;
114     FixedText                   m_aReenterPasswdToOpenFT;
115     PasswordReenterEdit_Impl    m_aReenterPasswdToOpenED;
116 //    FixedImage                  m_aPasswdToOpenMatchFI;
117     FixedText                   m_aPasswdNoteFT;
118     FixedLine                   m_aButtonsFL;
119     MoreButton                  m_aMoreFewerOptionsBTN;
120     OKButton                    m_aOk;
121     CancelButton                m_aCancel;
122     FixedLine                   m_aFileSharingOptionsFL;
123     CheckBox                    m_aOpenReadonlyCB;
124     FixedText                   m_aPasswdToModifyFT;
125     Edit                        m_aPasswdToModifyED;
126     FixedText                   m_aReenterPasswdToModifyFT;
127     PasswordReenterEdit_Impl    m_aReenterPasswdToModifyED;
128 //    FixedImage                  m_aPasswdToModifyMatchFI;
129 
130     String                      m_aOneMismatch;
131     String                      m_aTwoMismatch;
132     String                      m_aInvalidStateForOkButton;
133     String                      m_aInvalidStateForOkButton_v2;
134 
135     bool                        m_bIsPasswordToModify;
136 
137 
138 //    DECL_LINK( ModifyHdl, Edit * );
139     DECL_LINK( OkBtnClickHdl, OKButton * );
140 
141     PasswordToOpenModifyDialog_Impl( PasswordToOpenModifyDialog * pParent,
142             sal_uInt16 nMinPasswdLen, sal_uInt16 nMaxPasswdLen, bool bIsPasswordToModify );
143     ~PasswordToOpenModifyDialog_Impl();
144 };
145 
146 
147 PasswordToOpenModifyDialog_Impl::PasswordToOpenModifyDialog_Impl(
148         PasswordToOpenModifyDialog * pParent,
149         sal_uInt16 nMinPasswdLen,
150         sal_uInt16 nMaxPasswdLen,
151         bool bIsPasswordToModify ) :
152     m_pParent( pParent ),
153     m_aFileEncryptionFL         ( pParent, CUI_RES( FL_FILE_ENCRYPTION ) ),
154     m_aPasswdToOpenFT           ( pParent, CUI_RES( FT_PASSWD_TO_OPEN ) ),
155     m_aPasswdToOpenED           ( pParent, CUI_RES( ED_PASSWD_TO_OPEN ) ),
156     m_aReenterPasswdToOpenFT    ( pParent, CUI_RES( FT_REENTER_PASSWD_TO_OPEN ) ),
157     m_aReenterPasswdToOpenED    ( pParent, CUI_RES( ED_REENTER_PASSWD_TO_OPEN ) ),
158 //    m_aPasswdToOpenMatchFI      ( pParent, CUI_RES( FI_PASSWD_TO_OPEN_MATCH ) ),
159     m_aPasswdNoteFT             ( pParent, CUI_RES( FT_PASSWD_NOTE ) ),
160     m_aButtonsFL                ( pParent, CUI_RES( FL_BUTTONS ) ),
161     m_aMoreFewerOptionsBTN      ( pParent, CUI_RES( BTN_MORE_FEWER_OPTIONS ) ),
162     m_aOk                       ( pParent, CUI_RES( BTN_OK ) ),
163     m_aCancel                   ( pParent, CUI_RES( BTN_CANCEL ) ),
164     m_aFileSharingOptionsFL     ( pParent, CUI_RES( FL_FILE_SHARING_OPTIONS ) ),
165     m_aOpenReadonlyCB           ( pParent, CUI_RES( CB_OPEN_READONLY ) ),
166     m_aPasswdToModifyFT         ( pParent, CUI_RES( FT_PASSWD_TO_MODIFY ) ),
167     m_aPasswdToModifyED         ( pParent, CUI_RES( ED_PASSWD_TO_MODIFY ) ),
168     m_aReenterPasswdToModifyFT  ( pParent, CUI_RES( FT_REENTER_PASSWD_TO_MODIFY ) ),
169     m_aReenterPasswdToModifyED  ( pParent, CUI_RES( ED_REENTER_PASSWD_TO_MODIFY ) ),
170 //    m_aPasswdToModifyMatchFI    ( pParent, CUI_RES( FI_PASSWD_TO_MODIFY_MATCH ) )
171     m_aOneMismatch( CUI_RES( STR_ONE_PASSWORD_MISMATCH ) ),
172     m_aTwoMismatch( CUI_RES( STR_TWO_PASSWORDS_MISMATCH ) ),
173     m_aInvalidStateForOkButton( CUI_RES( STR_INVALID_STATE_FOR_OK_BUTTON ) ),
174     m_aInvalidStateForOkButton_v2( CUI_RES( STR_INVALID_STATE_FOR_OK_BUTTON_V2 ) ),
175     m_bIsPasswordToModify( bIsPasswordToModify )
176 {
177 /*
178     const sal_Bool bHighContrast = pParent->GetSettings().GetStyleSettings().GetHighContrastMode();
179     const Image aImage( CUI_RES( bHighContrast ? IMG_PASSWD_MATCH_HC : IMG_PASSWD_MATCH ) );
180     m_aPasswdToOpenMatchFI.SetImage( aImage );
181     m_aPasswdToModifyMatchFI.SetImage( aImage );
182 */
183 
184     m_aMoreFewerOptionsBTN.SetMoreText( String( CUI_RES( STR_MORE_OPTIONS ) ) );
185     m_aMoreFewerOptionsBTN.SetLessText( String( CUI_RES( STR_FEWER_OPTIONS ) ) );
186 
187 #if 0
188     Link aModifyLink = LINK( this, PasswordToOpenModifyDialog_Impl, ModifyHdl );
189     m_aPasswdToOpenED.SetModifyHdl( aModifyLink );
190     m_aReenterPasswdToOpenED.SetModifyHdl( aModifyLink );
191     m_aPasswdToModifyED.SetModifyHdl( aModifyLink );
192     m_aReenterPasswdToModifyED.SetModifyHdl( aModifyLink );
193 #endif
194 
195     m_aOk.SetClickHdl( LINK( this, PasswordToOpenModifyDialog_Impl, OkBtnClickHdl ) );
196 
197 //    m_aOk.Enable( sal_False );
198 
199     if (nMaxPasswdLen)
200     {
201         m_aPasswdToOpenED.SetMaxTextLen( nMaxPasswdLen );
202 	    m_aReenterPasswdToOpenED.SetMaxTextLen( nMaxPasswdLen );
203         m_aPasswdToModifyED.SetMaxTextLen( nMaxPasswdLen );
204         m_aReenterPasswdToModifyED.SetMaxTextLen( nMaxPasswdLen );
205     }
206 
207     (void) nMinPasswdLen;   // currently not supported
208 
209     m_aPasswdToOpenED.GrabFocus();
210 
211 //    ModifyHdl( NULL );
212 
213     m_aMoreFewerOptionsBTN.Enable( bIsPasswordToModify );
214     if (!bIsPasswordToModify)
215         m_aMoreFewerOptionsBTN.Hide( sal_True );
216 }
217 
218 
219 PasswordToOpenModifyDialog_Impl::~PasswordToOpenModifyDialog_Impl()
220 {
221 }
222 
223 #if 0
224 IMPL_LINK( PasswordToOpenModifyDialog_Impl, ModifyHdl, Edit *, EMPTYARG /*pEdit*/ )
225 {
226     // force repaints to get the m_aDefaultTxt displayed again
227     if (m_aReenterPasswdToOpenED.GetText().Len() == 0)
228         m_aReenterPasswdToOpenED.Invalidate();
229     if (m_aReenterPasswdToModifyED.GetText().Len() == 0)
230         m_aReenterPasswdToModifyED.Invalidate();
231 
232     const sal_Int32 nPasswdToOpenLen    = m_aPasswdToOpenED.GetText().Len();
233     const sal_Int32 nPasswdToModifyLen  = m_aPasswdToModifyED.GetText().Len();
234 
235     const bool bBothEmpty       = nPasswdToOpenLen == 0 && nPasswdToModifyLen == 0;
236     const bool bToOpenMatch     = m_aPasswdToOpenED.GetText()   == m_aReenterPasswdToOpenED.GetText();
237     const bool bToModifyMatch   = m_aPasswdToModifyED.GetText() == m_aReenterPasswdToModifyED.GetText();
238 
239     m_aOk.Enable( bToOpenMatch && bToModifyMatch && !bBothEmpty );
240 
241 //    m_aPasswdToOpenMatchFI.Enable( bToOpenMatch && !bBothEmpty );
242 //    m_aPasswdToModifyMatchFI.Enable( bToModifyMatch && !bBothEmpty );
243 
244     return 0;
245 }
246 #endif
247 
248 
249 IMPL_LINK( PasswordToOpenModifyDialog_Impl, OkBtnClickHdl, OKButton *, EMPTYARG /*pBtn*/ )
250 {
251     bool bInvalidState = !m_aOpenReadonlyCB.IsChecked() &&
252             m_aPasswdToOpenED.GetText().Len() == 0 &&
253             m_aPasswdToModifyED.GetText().Len() == 0;
254     if (bInvalidState)
255     {
256         ErrorBox aErrorBox( m_pParent, WB_OK,
257             m_bIsPasswordToModify? m_aInvalidStateForOkButton : m_aInvalidStateForOkButton_v2 );
258         aErrorBox.Execute();
259     }
260     else // check for mismatched passwords...
261     {
262         const bool bToOpenMatch     = m_aPasswdToOpenED.GetText()   == m_aReenterPasswdToOpenED.GetText();
263         const bool bToModifyMatch   = m_aPasswdToModifyED.GetText() == m_aReenterPasswdToModifyED.GetText();
264         const int nMismatch = (bToOpenMatch? 0 : 1) + (bToModifyMatch? 0 : 1);
265         if (nMismatch > 0)
266         {
267             ErrorBox aErrorBox( m_pParent, WB_OK, nMismatch == 1 ? m_aOneMismatch : m_aTwoMismatch );
268             aErrorBox.Execute();
269 
270             Edit &rEdit = !bToOpenMatch? m_aPasswdToOpenED : m_aPasswdToModifyED;
271             PasswordReenterEdit_Impl &rRepeatEdit = !bToOpenMatch? m_aReenterPasswdToOpenED : m_aReenterPasswdToModifyED;
272             String aEmpty;
273             if (nMismatch == 1)
274             {
275                 rEdit.SetText( aEmpty );
276                 rRepeatEdit.SetText( aEmpty );
277             }
278             else if (nMismatch == 2)
279             {
280                 m_aPasswdToOpenED.SetText( aEmpty );
281                 m_aReenterPasswdToOpenED.SetText( aEmpty );
282                 m_aPasswdToModifyED.SetText( aEmpty );
283                 m_aReenterPasswdToModifyED.SetText( aEmpty );
284             }
285             rEdit.GrabFocus();
286         }
287         else
288         {
289             m_pParent->EndDialog( RET_OK );
290         }
291     }
292 
293     return 0;
294 }
295 
296 //////////////////////////////////////////////////////////////////////
297 
298 
299 PasswordToOpenModifyDialog::PasswordToOpenModifyDialog(
300         Window * pParent,
301         sal_uInt16 nMinPasswdLen,
302         sal_uInt16 nMaxPasswdLen,
303         bool bIsPasswordToModify ) :
304     SfxModalDialog( pParent, CUI_RES( RID_DLG_PASSWORD_TO_OPEN_MODIFY ) )
305 {
306     m_pImpl = std::auto_ptr< PasswordToOpenModifyDialog_Impl >(
307             new PasswordToOpenModifyDialog_Impl( this, nMinPasswdLen, nMaxPasswdLen, bIsPasswordToModify ) );
308 
309     FreeResource();
310 }
311 
312 
313 PasswordToOpenModifyDialog::~PasswordToOpenModifyDialog()
314 {
315 }
316 
317 
318 String PasswordToOpenModifyDialog::GetPasswordToOpen() const
319 {
320     const bool bPasswdOk =
321             m_pImpl->m_aPasswdToOpenED.GetText().Len() > 0 &&
322             m_pImpl->m_aPasswdToOpenED.GetText() == m_pImpl->m_aReenterPasswdToOpenED.GetText();
323     return bPasswdOk ? m_pImpl->m_aPasswdToOpenED.GetText() : String();
324 }
325 
326 
327 String PasswordToOpenModifyDialog::GetPasswordToModify() const
328 {
329     const bool bPasswdOk =
330             m_pImpl->m_aPasswdToModifyED.GetText().Len() > 0 &&
331             m_pImpl->m_aPasswdToModifyED.GetText() == m_pImpl->m_aReenterPasswdToModifyED.GetText();
332     return bPasswdOk ? m_pImpl->m_aPasswdToModifyED.GetText() : String();
333 }
334 
335 
336 bool PasswordToOpenModifyDialog::IsRecommendToOpenReadonly() const
337 {
338     return m_pImpl->m_aOpenReadonlyCB.IsChecked();
339 }
340 
341 
342 //////////////////////////////////////////////////////////////////////
343 
344