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