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