xref: /aoo41x/main/sfx2/source/dialog/passwd.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_sfx2.hxx"
30 
31 // Include ---------------------------------------------------------------
32 #include <vcl/msgbox.hxx>
33 #ifndef GCC
34 #endif
35 
36 #include <sfx2/passwd.hxx>
37 #include "sfxtypes.hxx"
38 #include "sfx2/sfxresid.hxx"
39 
40 #include "dialog.hrc"
41 #include "passwd.hrc"
42 
43 #include "vcl/sound.hxx"
44 #include "vcl/arrange.hxx"
45 
46 // -----------------------------------------------------------------------
47 
48 IMPL_LINK_INLINE_START( SfxPasswordDialog, EditModifyHdl, Edit *, pEdit )
49 {
50     if( mbAsciiOnly && (pEdit == &maPasswordED || pEdit == &maPassword2ED) )
51     {
52         rtl::OUString aTest( pEdit->GetText() );
53         const sal_Unicode* pTest = aTest.getStr();
54         sal_Int32 nLen = aTest.getLength();
55         rtl::OUStringBuffer aFilter( nLen );
56         bool bReset = false;
57         for( sal_Int32 i = 0; i < nLen; i++ )
58         {
59             if( *pTest > 0x007f )
60                 bReset = true;
61             else
62                 aFilter.append( *pTest );
63             pTest++;
64         }
65         if( bReset )
66         {
67             Sound::Beep( SOUND_ERROR );
68             pEdit->SetSelection( Selection( 0, nLen ) );
69             pEdit->ReplaceSelected( aFilter.makeStringAndClear() );
70         }
71 
72     }
73     bool bEnable = maPasswordED.GetText().Len() >= mnMinLen;
74     if( maPassword2ED.IsVisible() )
75         bEnable = (bEnable && (maPassword2ED.GetText().Len() >= mnMinLen));
76 	maOKBtn.Enable( bEnable );
77 	return 0;
78 }
79 IMPL_LINK_INLINE_END( SfxPasswordDialog, EditModifyHdl, Edit *, EMPTYARG )
80 
81 // -----------------------------------------------------------------------
82 
83 IMPL_LINK( SfxPasswordDialog, OKHdl, OKButton *, EMPTYARG )
84 {
85     bool bConfirmFailed = ( ( mnExtras & SHOWEXTRAS_CONFIRM ) == SHOWEXTRAS_CONFIRM ) &&
86 		                    ( GetConfirm() != GetPassword() );
87     if( ( mnExtras & SHOWEXTRAS_CONFIRM2 ) == SHOWEXTRAS_CONFIRM2 && ( GetConfirm2() != GetPassword2() ) )
88         bConfirmFailed = true;
89 	if ( bConfirmFailed )
90 	{
91 		ErrorBox aBox( this, SfxResId( MSG_ERROR_WRONG_CONFIRM ) );
92 		aBox.Execute();
93 		maConfirmED.SetText( String() );
94 		maConfirmED.GrabFocus();
95 	}
96 	else
97 		EndDialog( RET_OK );
98 	return 0;
99 }
100 
101 // CTOR / DTOR -----------------------------------------------------------
102 
103 SfxPasswordDialog::SfxPasswordDialog( Window* pParent, const String* pGroupText ) :
104 
105 	ModalDialog( pParent, SfxResId ( DLG_PASSWD ) ),
106 
107 	maPasswordBox	( this, SfxResId( GB_PASSWD_PASSWORD ) ),
108 	maUserFT		( this, SfxResId( FT_PASSWD_USER ) ),
109 	maUserED		( this, SfxResId( ED_PASSWD_USER ) ),
110 	maPasswordFT	( this, SfxResId( FT_PASSWD_PASSWORD ) ),
111 	maPasswordED	( this, SfxResId( ED_PASSWD_PASSWORD ) ),
112 	maConfirmFT		( this, SfxResId( FT_PASSWD_CONFIRM ) ),
113 	maConfirmED		( this, SfxResId( ED_PASSWD_CONFIRM ) ),
114 	maPassword2Box	( this, 0 ),
115 	maPassword2FT	( this, SfxResId( FT_PASSWD_PASSWORD2 ) ),
116 	maPassword2ED	( this, SfxResId( ED_PASSWD_PASSWORD2 ) ),
117 	maConfirm2FT	( this, SfxResId( FT_PASSWD_CONFIRM2 ) ),
118 	maConfirm2ED	( this, SfxResId( ED_PASSWD_CONFIRM2 ) ),
119 	maOKBtn			( this, SfxResId( BTN_PASSWD_OK ) ),
120 	maCancelBtn		( this, SfxResId( BTN_PASSWD_CANCEL ) ),
121 	maHelpBtn		( this, SfxResId( BTN_PASSWD_HELP ) ),
122 
123 	mnMinLen		( 1 ),
124 	mnExtras		( 0 ),
125     mbAsciiOnly     ( false )
126 
127 {
128 	maPasswordED.SetAccessibleName(String(SfxResId(TEXT_PASSWD)));
129 	FreeResource();
130 
131 	// setup layout
132     boost::shared_ptr<vcl::RowOrColumn> xLayout =
133         boost::dynamic_pointer_cast<vcl::RowOrColumn>( getLayout() );
134     xLayout->setOuterBorder( 0 );
135 
136     // get edit size, should be used as minimum
137     Size aEditSize( maUserED.GetSizePixel() );
138 
139     // add labelcolumn for the labeled edit fields
140     boost::shared_ptr<vcl::LabelColumn> xEdits( new vcl::LabelColumn( xLayout.get() ) );
141     size_t nChildIndex = xLayout->addChild( xEdits );
142     xLayout->setBorders( nChildIndex, -2, -2, -2, 0 );
143 
144     // add group box
145     xEdits->addWindow( &maPasswordBox );
146 
147     // add user line
148     xEdits->addRow( &maUserFT, &maUserED, -2, aEditSize );
149 
150     // add password line
151     xEdits->addRow( &maPasswordFT, &maPasswordED, -2, aEditSize );
152 
153     // add confirm line
154     xEdits->addRow( &maConfirmFT, &maConfirmED, -2, aEditSize );
155 
156     // add second group box
157     xEdits->addWindow( &maPassword2Box );
158 
159     // add second password line
160     xEdits->addRow( &maPassword2FT, &maPassword2ED, -2, aEditSize );
161 
162     // add second confirm line
163     xEdits->addRow( &maConfirm2FT, &maConfirm2ED, -2, aEditSize );
164 
165     // add a FixedLine
166     FixedLine* pLine = new FixedLine( this, 0 );
167     pLine->Show();
168     addWindow( pLine, true );
169     xLayout->addWindow( pLine );
170 
171     // add button column
172     Size aBtnSize( maCancelBtn.GetSizePixel() );
173     boost::shared_ptr<vcl::RowOrColumn> xButtons( new vcl::RowOrColumn( xLayout.get(), false ) );
174     nChildIndex = xLayout->addChild( xButtons );
175     xLayout->setBorders( nChildIndex, -2, 0, -2, -2 );
176 
177     size_t nBtnIndex = xButtons->addWindow( &maHelpBtn, 0, aBtnSize );
178     xButtons->addChild( new vcl::Spacer( xButtons.get() ) );
179     nBtnIndex = xButtons->addWindow( &maOKBtn, 0, aBtnSize );
180     nBtnIndex = xButtons->addWindow( &maCancelBtn, 0, aBtnSize );
181 
182 	Link aLink = LINK( this, SfxPasswordDialog, EditModifyHdl );
183 	maPasswordED.SetModifyHdl( aLink );
184 	maPassword2ED.SetModifyHdl( aLink );
185 	aLink = LINK( this, SfxPasswordDialog, OKHdl );
186 	maOKBtn.SetClickHdl( aLink );
187 
188 	if ( pGroupText )
189 		  maPasswordBox.SetText( *pGroupText );
190 }
191 
192 // -----------------------------------------------------------------------
193 
194 void SfxPasswordDialog::SetMinLen( sal_uInt16 nLen )
195 {
196 	mnMinLen = nLen;
197 	EditModifyHdl( NULL );
198 }
199 
200 // -----------------------------------------------------------------------
201 
202 void SfxPasswordDialog::SetMaxLen( sal_uInt16 nLen )
203 {
204     maPasswordED.SetMaxTextLen( nLen );
205     maConfirmED.SetMaxTextLen( nLen );
206 	EditModifyHdl( NULL );
207 }
208 
209 // -----------------------------------------------------------------------
210 
211 short SfxPasswordDialog::Execute()
212 {
213     maUserFT.Hide();
214     maUserED.Hide();
215     maConfirmFT.Hide();
216     maConfirmED.Hide();
217     maPasswordFT.Hide();
218     maPassword2Box.Hide();
219     maPassword2FT.Hide();
220     maPassword2ED.Hide();
221     maPassword2FT.Hide();
222     maConfirm2FT.Hide();
223     maConfirm2ED.Hide();
224 
225     if( mnExtras != SHOWEXTRAS_NONE )
226         maPasswordFT.Show();
227     if( (mnExtras & SHOWEXTRAS_USER ) )
228     {
229         maUserFT.Show();
230         maUserED.Show();
231     }
232     if( (mnExtras & SHOWEXTRAS_CONFIRM ) )
233     {
234         maConfirmFT.Show();
235         maConfirmED.Show();
236     }
237     if( (mnExtras & SHOWEXTRAS_PASSWORD2) )
238     {
239         maPassword2Box.Show();
240         maPassword2FT.Show();
241         maPassword2ED.Show();
242     }
243     if( (mnExtras & SHOWEXTRAS_CONFIRM2 ) )
244     {
245         maConfirm2FT.Show();
246         maConfirm2ED.Show();
247     }
248 
249     boost::shared_ptr<vcl::RowOrColumn> xLayout =
250         boost::dynamic_pointer_cast<vcl::RowOrColumn>( getLayout() );
251 	SetSizePixel( xLayout->getOptimalSize( WINDOWSIZE_PREFERRED ) );
252 
253 	return ModalDialog::Execute();
254 }
255 
256 
257