Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / dialog / passwd.cxx
blob681d3683bb0a5d278eb3e60e5cb450d26c3e3d0d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 // Include ---------------------------------------------------------------
31 #include <vcl/msgbox.hxx>
33 #include <sfx2/passwd.hxx>
34 #include "sfxtypes.hxx"
35 #include "sfx2/sfxresid.hxx"
37 #include "dialog.hrc"
38 #include "passwd.hrc"
40 #include "vcl/sound.hxx"
41 #include "vcl/arrange.hxx"
43 // -----------------------------------------------------------------------
45 IMPL_LINK_INLINE_START( SfxPasswordDialog, EditModifyHdl, Edit *, pEdit )
47 if( mbAsciiOnly && (pEdit == &maPasswordED || pEdit == &maPassword2ED) )
49 rtl::OUString aTest( pEdit->GetText() );
50 const sal_Unicode* pTest = aTest.getStr();
51 sal_Int32 nLen = aTest.getLength();
52 rtl::OUStringBuffer aFilter( nLen );
53 bool bReset = false;
54 for( sal_Int32 i = 0; i < nLen; i++ )
56 if( *pTest > 0x007f )
57 bReset = true;
58 else
59 aFilter.append( *pTest );
60 pTest++;
62 if( bReset )
64 Sound::Beep( SOUND_ERROR );
65 pEdit->SetSelection( Selection( 0, nLen ) );
66 pEdit->ReplaceSelected( aFilter.makeStringAndClear() );
70 bool bEnable = maPasswordED.GetText().Len() >= mnMinLen;
71 if( maPassword2ED.IsVisible() )
72 bEnable = (bEnable && (maPassword2ED.GetText().Len() >= mnMinLen));
73 maOKBtn.Enable( bEnable );
74 return 0;
76 IMPL_LINK_INLINE_END(SfxPasswordDialog, EditModifyHdl, Edit *, pEdit)
78 // -----------------------------------------------------------------------
80 IMPL_LINK_NOARG(SfxPasswordDialog, OKHdl)
82 bool bConfirmFailed = ( ( mnExtras & SHOWEXTRAS_CONFIRM ) == SHOWEXTRAS_CONFIRM ) &&
83 ( GetConfirm() != GetPassword() );
84 if( ( mnExtras & SHOWEXTRAS_CONFIRM2 ) == SHOWEXTRAS_CONFIRM2 && ( GetConfirm2() != GetPassword2() ) )
85 bConfirmFailed = true;
86 if ( bConfirmFailed )
88 ErrorBox aBox( this, SfxResId( MSG_ERROR_WRONG_CONFIRM ) );
89 aBox.Execute();
90 maConfirmED.SetText( String() );
91 maConfirmED.GrabFocus();
93 else
94 EndDialog( RET_OK );
95 return 0;
98 // CTOR / DTOR -----------------------------------------------------------
100 SfxPasswordDialog::SfxPasswordDialog( Window* pParent, const String* pGroupText ) :
102 ModalDialog( pParent, SfxResId ( DLG_PASSWD ) ),
104 maPasswordBox ( this, SfxResId( GB_PASSWD_PASSWORD ) ),
105 maUserFT ( this, SfxResId( FT_PASSWD_USER ) ),
106 maUserED ( this, SfxResId( ED_PASSWD_USER ) ),
107 maPasswordFT ( this, SfxResId( FT_PASSWD_PASSWORD ) ),
108 maPasswordED ( this, SfxResId( ED_PASSWD_PASSWORD ) ),
109 maConfirmFT ( this, SfxResId( FT_PASSWD_CONFIRM ) ),
110 maConfirmED ( this, SfxResId( ED_PASSWD_CONFIRM ) ),
112 maMinLengthFT ( this, SfxResId( FT_PASSWD_MINLEN ) ),
113 maPassword2Box ( this, 0 ),
114 maPassword2FT ( this, SfxResId( FT_PASSWD_PASSWORD2 ) ),
115 maPassword2ED ( this, SfxResId( ED_PASSWD_PASSWORD2 ) ),
116 maConfirm2FT ( this, SfxResId( FT_PASSWD_CONFIRM2 ) ),
117 maConfirm2ED ( this, SfxResId( ED_PASSWD_CONFIRM2 ) ),
118 maOKBtn ( this, SfxResId( BTN_PASSWD_OK ) ),
119 maCancelBtn ( this, SfxResId( BTN_PASSWD_CANCEL ) ),
120 maHelpBtn ( this, SfxResId( BTN_PASSWD_HELP ) ),
122 maMinLenPwdStr ( SfxResId( STR_PASSWD_MIN_LEN ) ),
123 maEmptyPwdStr ( SfxResId( STR_PASSWD_EMPTY ) ),
124 maMainPwdStr ( ),
125 mnMinLen ( 5 ),
126 mnExtras ( 0 ),
127 mbAsciiOnly ( false )
130 maPasswordED.SetAccessibleName(String(SfxResId(TEXT_PASSWD)));
131 FreeResource();
133 // setup layout
134 boost::shared_ptr<vcl::RowOrColumn> xLayout =
135 boost::dynamic_pointer_cast<vcl::RowOrColumn>( getLayout() );
136 xLayout->setOuterBorder( 0 );
138 // get edit size, should be used as minimum
139 Size aEditSize( maUserED.GetSizePixel() );
141 // add labelcolumn for the labeled edit fields
142 boost::shared_ptr<vcl::LabelColumn> xEdits( new vcl::LabelColumn( xLayout.get() ) );
143 size_t nChildIndex = xLayout->addChild( xEdits );
144 xLayout->setBorders( nChildIndex, -2, -2, -2, 0 );
146 // add group box
147 xEdits->addWindow( &maPasswordBox );
149 // add user line
150 xEdits->addRow( &maUserFT, &maUserED, -2, aEditSize );
152 // add password line
153 xEdits->addRow( &maPasswordFT, &maPasswordED, -2, aEditSize );
155 // add confirm line
156 xEdits->addRow( &maConfirmFT, &maConfirmED, -2, aEditSize );
158 // add second group box
159 xEdits->addWindow( &maPassword2Box );
161 // add second password line
162 xEdits->addRow( &maPassword2FT, &maPassword2ED, -2, aEditSize );
164 // add second confirm line
165 xEdits->addRow( &maConfirm2FT, &maConfirm2ED, -2, aEditSize );
167 // add password length warning line
168 xEdits->addWindow( &maMinLengthFT );
170 // add a FixedLine
171 FixedLine* pLine = new FixedLine( this, 0 );
172 pLine->Show();
173 addWindow( pLine, true );
174 xLayout->addWindow( pLine );
176 // add button column
177 Size aBtnSize( maCancelBtn.GetSizePixel() );
178 boost::shared_ptr<vcl::RowOrColumn> xButtons( new vcl::RowOrColumn( xLayout.get(), false ) );
179 nChildIndex = xLayout->addChild( xButtons );
180 xLayout->setBorders( nChildIndex, -2, 0, -2, -2 );
182 xButtons->addWindow( &maHelpBtn, 0, aBtnSize );
183 xButtons->addChild( new vcl::Spacer( xButtons.get() ) );
184 xButtons->addWindow( &maOKBtn, 0, aBtnSize );
185 xButtons->addWindow( &maCancelBtn, 0, aBtnSize );
187 Link aLink = LINK( this, SfxPasswordDialog, EditModifyHdl );
188 maPasswordED.SetModifyHdl( aLink );
189 maPassword2ED.SetModifyHdl( aLink );
190 aLink = LINK( this, SfxPasswordDialog, OKHdl );
191 maOKBtn.SetClickHdl( aLink );
193 if ( pGroupText )
194 maPasswordBox.SetText( *pGroupText );
196 //set the text to the pasword length
197 SetPasswdText();
200 // -----------------------------------------------------------------------
202 void SfxPasswordDialog::SetPasswdText( )
204 //set the new string to the minimum password length
205 if( mnMinLen == 0 )
206 maMinLengthFT.SetText( maEmptyPwdStr );
207 else
209 maMainPwdStr = maMinLenPwdStr;
210 maMainPwdStr.SearchAndReplace( String::CreateFromAscii( "$(MINLEN)" ), String::CreateFromInt32((sal_Int32) mnMinLen ), 0);
211 maMinLengthFT.SetText( maMainPwdStr );
212 maMinLengthFT.Show();
216 // -----------------------------------------------------------------------
218 void SfxPasswordDialog::SetMinLen( sal_uInt16 nLen )
220 mnMinLen = nLen;
221 SetPasswdText();
222 EditModifyHdl( NULL );
225 // -----------------------------------------------------------------------
227 short SfxPasswordDialog::Execute()
229 maUserFT.Hide();
230 maUserED.Hide();
231 maConfirmFT.Hide();
232 maConfirmED.Hide();
233 maPasswordFT.Hide();
234 maPassword2Box.Hide();
235 maPassword2FT.Hide();
236 maPassword2ED.Hide();
237 maPassword2FT.Hide();
238 maConfirm2FT.Hide();
239 maConfirm2ED.Hide();
241 if( mnExtras != SHOWEXTRAS_NONE )
242 maPasswordFT.Show();
243 if( (mnExtras & SHOWEXTRAS_USER ) )
245 //TODO: Inevitably this layout logic will be wrong post merge until we can see the dialog to test it.
246 Size a3Size = LogicToPixel( Size( 3, 3 ), MAP_APPFONT );
247 Size a6Size = LogicToPixel( Size( 6, 6 ), MAP_APPFONT );
248 long nMinHeight = maHelpBtn.GetPosPixel().Y() +
249 maHelpBtn.GetSizePixel().Height() + a6Size.Height();
250 sal_uInt16 nRowHided = 1;
252 if ( SHOWEXTRAS_NONE == mnExtras )
254 maUserFT.Hide();
255 maUserED.Hide();
256 maConfirmFT.Hide();
257 maConfirmED.Hide();
258 maPasswordFT.Hide();
260 Point aPos = maUserFT.GetPosPixel();
261 long nEnd = maUserED.GetPosPixel().X() + maUserED.GetSizePixel().Width();
262 maPasswordED.SetPosPixel( aPos );
263 Size aSize = maPasswordED.GetSizePixel();
264 aSize.Width() = nEnd - aPos.X();
265 maPasswordED.SetSizePixel( aSize );
267 nRowHided = 2;
269 else if ( SHOWEXTRAS_USER == mnExtras )
271 maConfirmFT.Hide();
272 maConfirmED.Hide();
274 else if ( SHOWEXTRAS_CONFIRM == mnExtras )
276 maUserFT.Hide();
277 maUserED.Hide();
279 Point aPwdPos1 = maPasswordFT.GetPosPixel();
280 Point aPwdPos2 = maPasswordED.GetPosPixel();
282 Point aPos = maUserFT.GetPosPixel();
283 maPasswordFT.SetPosPixel( aPos );
284 aPos = maUserED.GetPosPixel();
285 maPasswordED.SetPosPixel( aPos );
287 aPos = maConfirmFT.GetPosPixel();
288 maConfirmFT.SetPosPixel( aPwdPos1 );
289 maConfirmED.SetPosPixel( aPwdPos2 );
290 maMinLengthFT.SetPosPixel(aPos);
293 Size aBoxSize = maPasswordBox.GetSizePixel();
294 aBoxSize.Height() -= ( nRowHided * maUserED.GetSizePixel().Height() );
295 aBoxSize.Height() -= ( nRowHided * a3Size.Height() );
296 maPasswordBox.SetSizePixel( aBoxSize );
298 long nDlgHeight = maPasswordBox.GetPosPixel().Y() + aBoxSize.Height() + a6Size.Height();
299 if ( nDlgHeight < nMinHeight )
300 nDlgHeight = nMinHeight;
301 Size aDlgSize = GetOutputSizePixel();
302 aDlgSize.Height() = nDlgHeight;
303 SetOutputSizePixel( aDlgSize );
305 maUserFT.Show();
306 maUserED.Show();
308 if( (mnExtras & SHOWEXTRAS_CONFIRM ) )
310 maConfirmFT.Show();
311 maConfirmED.Show();
313 if( (mnExtras & SHOWEXTRAS_PASSWORD2) )
315 maPassword2Box.Show();
316 maPassword2FT.Show();
317 maPassword2ED.Show();
319 if( (mnExtras & SHOWEXTRAS_CONFIRM2 ) )
321 maConfirm2FT.Show();
322 maConfirm2ED.Show();
325 boost::shared_ptr<vcl::RowOrColumn> xLayout =
326 boost::dynamic_pointer_cast<vcl::RowOrColumn>( getLayout() );
327 SetSizePixel( xLayout->getOptimalSize( WINDOWSIZE_PREFERRED ) );
329 return ModalDialog::Execute();
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */