xref: /AOO41X/main/sfx2/source/dialog/passwd.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // Include ---------------------------------------------------------------
32*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
33*cdf0e10cSrcweir #ifndef GCC
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <sfx2/passwd.hxx>
37*cdf0e10cSrcweir #include "sfxtypes.hxx"
38*cdf0e10cSrcweir #include "sfx2/sfxresid.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "dialog.hrc"
41*cdf0e10cSrcweir #include "passwd.hrc"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "vcl/sound.hxx"
44*cdf0e10cSrcweir #include "vcl/arrange.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir // -----------------------------------------------------------------------
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir IMPL_LINK_INLINE_START( SfxPasswordDialog, EditModifyHdl, Edit *, pEdit )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir     if( mbAsciiOnly && (pEdit == &maPasswordED || pEdit == &maPassword2ED) )
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir         rtl::OUString aTest( pEdit->GetText() );
53*cdf0e10cSrcweir         const sal_Unicode* pTest = aTest.getStr();
54*cdf0e10cSrcweir         sal_Int32 nLen = aTest.getLength();
55*cdf0e10cSrcweir         rtl::OUStringBuffer aFilter( nLen );
56*cdf0e10cSrcweir         bool bReset = false;
57*cdf0e10cSrcweir         for( sal_Int32 i = 0; i < nLen; i++ )
58*cdf0e10cSrcweir         {
59*cdf0e10cSrcweir             if( *pTest > 0x007f )
60*cdf0e10cSrcweir                 bReset = true;
61*cdf0e10cSrcweir             else
62*cdf0e10cSrcweir                 aFilter.append( *pTest );
63*cdf0e10cSrcweir             pTest++;
64*cdf0e10cSrcweir         }
65*cdf0e10cSrcweir         if( bReset )
66*cdf0e10cSrcweir         {
67*cdf0e10cSrcweir             Sound::Beep( SOUND_ERROR );
68*cdf0e10cSrcweir             pEdit->SetSelection( Selection( 0, nLen ) );
69*cdf0e10cSrcweir             pEdit->ReplaceSelected( aFilter.makeStringAndClear() );
70*cdf0e10cSrcweir         }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     }
73*cdf0e10cSrcweir     bool bEnable = maPasswordED.GetText().Len() >= mnMinLen;
74*cdf0e10cSrcweir     if( maPassword2ED.IsVisible() )
75*cdf0e10cSrcweir         bEnable = (bEnable && (maPassword2ED.GetText().Len() >= mnMinLen));
76*cdf0e10cSrcweir 	maOKBtn.Enable( bEnable );
77*cdf0e10cSrcweir 	return 0;
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir IMPL_LINK_INLINE_END( SfxPasswordDialog, EditModifyHdl, Edit *, EMPTYARG )
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir // -----------------------------------------------------------------------
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir IMPL_LINK( SfxPasswordDialog, OKHdl, OKButton *, EMPTYARG )
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     bool bConfirmFailed = ( ( mnExtras & SHOWEXTRAS_CONFIRM ) == SHOWEXTRAS_CONFIRM ) &&
86*cdf0e10cSrcweir 		                    ( GetConfirm() != GetPassword() );
87*cdf0e10cSrcweir     if( ( mnExtras & SHOWEXTRAS_CONFIRM2 ) == SHOWEXTRAS_CONFIRM2 && ( GetConfirm2() != GetPassword2() ) )
88*cdf0e10cSrcweir         bConfirmFailed = true;
89*cdf0e10cSrcweir 	if ( bConfirmFailed )
90*cdf0e10cSrcweir 	{
91*cdf0e10cSrcweir 		ErrorBox aBox( this, SfxResId( MSG_ERROR_WRONG_CONFIRM ) );
92*cdf0e10cSrcweir 		aBox.Execute();
93*cdf0e10cSrcweir 		maConfirmED.SetText( String() );
94*cdf0e10cSrcweir 		maConfirmED.GrabFocus();
95*cdf0e10cSrcweir 	}
96*cdf0e10cSrcweir 	else
97*cdf0e10cSrcweir 		EndDialog( RET_OK );
98*cdf0e10cSrcweir 	return 0;
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir // CTOR / DTOR -----------------------------------------------------------
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir SfxPasswordDialog::SfxPasswordDialog( Window* pParent, const String* pGroupText ) :
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	ModalDialog( pParent, SfxResId ( DLG_PASSWD ) ),
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	maPasswordBox	( this, SfxResId( GB_PASSWD_PASSWORD ) ),
108*cdf0e10cSrcweir 	maUserFT		( this, SfxResId( FT_PASSWD_USER ) ),
109*cdf0e10cSrcweir 	maUserED		( this, SfxResId( ED_PASSWD_USER ) ),
110*cdf0e10cSrcweir 	maPasswordFT	( this, SfxResId( FT_PASSWD_PASSWORD ) ),
111*cdf0e10cSrcweir 	maPasswordED	( this, SfxResId( ED_PASSWD_PASSWORD ) ),
112*cdf0e10cSrcweir 	maConfirmFT		( this, SfxResId( FT_PASSWD_CONFIRM ) ),
113*cdf0e10cSrcweir 	maConfirmED		( this, SfxResId( ED_PASSWD_CONFIRM ) ),
114*cdf0e10cSrcweir 	maPassword2Box	( this, 0 ),
115*cdf0e10cSrcweir 	maPassword2FT	( this, SfxResId( FT_PASSWD_PASSWORD2 ) ),
116*cdf0e10cSrcweir 	maPassword2ED	( this, SfxResId( ED_PASSWD_PASSWORD2 ) ),
117*cdf0e10cSrcweir 	maConfirm2FT	( this, SfxResId( FT_PASSWD_CONFIRM2 ) ),
118*cdf0e10cSrcweir 	maConfirm2ED	( this, SfxResId( ED_PASSWD_CONFIRM2 ) ),
119*cdf0e10cSrcweir 	maOKBtn			( this, SfxResId( BTN_PASSWD_OK ) ),
120*cdf0e10cSrcweir 	maCancelBtn		( this, SfxResId( BTN_PASSWD_CANCEL ) ),
121*cdf0e10cSrcweir 	maHelpBtn		( this, SfxResId( BTN_PASSWD_HELP ) ),
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	mnMinLen		( 1 ),
124*cdf0e10cSrcweir 	mnExtras		( 0 ),
125*cdf0e10cSrcweir     mbAsciiOnly     ( false )
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir 	maPasswordED.SetAccessibleName(String(SfxResId(TEXT_PASSWD)));
129*cdf0e10cSrcweir 	FreeResource();
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	// setup layout
132*cdf0e10cSrcweir     boost::shared_ptr<vcl::RowOrColumn> xLayout =
133*cdf0e10cSrcweir         boost::dynamic_pointer_cast<vcl::RowOrColumn>( getLayout() );
134*cdf0e10cSrcweir     xLayout->setOuterBorder( 0 );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     // get edit size, should be used as minimum
137*cdf0e10cSrcweir     Size aEditSize( maUserED.GetSizePixel() );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     // add labelcolumn for the labeled edit fields
140*cdf0e10cSrcweir     boost::shared_ptr<vcl::LabelColumn> xEdits( new vcl::LabelColumn( xLayout.get() ) );
141*cdf0e10cSrcweir     size_t nChildIndex = xLayout->addChild( xEdits );
142*cdf0e10cSrcweir     xLayout->setBorders( nChildIndex, -2, -2, -2, 0 );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     // add group box
145*cdf0e10cSrcweir     xEdits->addWindow( &maPasswordBox );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     // add user line
148*cdf0e10cSrcweir     xEdits->addRow( &maUserFT, &maUserED, -2, aEditSize );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     // add password line
151*cdf0e10cSrcweir     xEdits->addRow( &maPasswordFT, &maPasswordED, -2, aEditSize );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     // add confirm line
154*cdf0e10cSrcweir     xEdits->addRow( &maConfirmFT, &maConfirmED, -2, aEditSize );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     // add second group box
157*cdf0e10cSrcweir     xEdits->addWindow( &maPassword2Box );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     // add second password line
160*cdf0e10cSrcweir     xEdits->addRow( &maPassword2FT, &maPassword2ED, -2, aEditSize );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     // add second confirm line
163*cdf0e10cSrcweir     xEdits->addRow( &maConfirm2FT, &maConfirm2ED, -2, aEditSize );
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     // add a FixedLine
166*cdf0e10cSrcweir     FixedLine* pLine = new FixedLine( this, 0 );
167*cdf0e10cSrcweir     pLine->Show();
168*cdf0e10cSrcweir     addWindow( pLine, true );
169*cdf0e10cSrcweir     xLayout->addWindow( pLine );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     // add button column
172*cdf0e10cSrcweir     Size aBtnSize( maCancelBtn.GetSizePixel() );
173*cdf0e10cSrcweir     boost::shared_ptr<vcl::RowOrColumn> xButtons( new vcl::RowOrColumn( xLayout.get(), false ) );
174*cdf0e10cSrcweir     nChildIndex = xLayout->addChild( xButtons );
175*cdf0e10cSrcweir     xLayout->setBorders( nChildIndex, -2, 0, -2, -2 );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     size_t nBtnIndex = xButtons->addWindow( &maHelpBtn, 0, aBtnSize );
178*cdf0e10cSrcweir     xButtons->addChild( new vcl::Spacer( xButtons.get() ) );
179*cdf0e10cSrcweir     nBtnIndex = xButtons->addWindow( &maOKBtn, 0, aBtnSize );
180*cdf0e10cSrcweir     nBtnIndex = xButtons->addWindow( &maCancelBtn, 0, aBtnSize );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	Link aLink = LINK( this, SfxPasswordDialog, EditModifyHdl );
183*cdf0e10cSrcweir 	maPasswordED.SetModifyHdl( aLink );
184*cdf0e10cSrcweir 	maPassword2ED.SetModifyHdl( aLink );
185*cdf0e10cSrcweir 	aLink = LINK( this, SfxPasswordDialog, OKHdl );
186*cdf0e10cSrcweir 	maOKBtn.SetClickHdl( aLink );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	if ( pGroupText )
189*cdf0e10cSrcweir 		  maPasswordBox.SetText( *pGroupText );
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir // -----------------------------------------------------------------------
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir void SfxPasswordDialog::SetMinLen( sal_uInt16 nLen )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir 	mnMinLen = nLen;
197*cdf0e10cSrcweir 	EditModifyHdl( NULL );
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir // -----------------------------------------------------------------------
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir void SfxPasswordDialog::SetMaxLen( sal_uInt16 nLen )
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir     maPasswordED.SetMaxTextLen( nLen );
205*cdf0e10cSrcweir     maConfirmED.SetMaxTextLen( nLen );
206*cdf0e10cSrcweir 	EditModifyHdl( NULL );
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir // -----------------------------------------------------------------------
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir short SfxPasswordDialog::Execute()
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     maUserFT.Hide();
214*cdf0e10cSrcweir     maUserED.Hide();
215*cdf0e10cSrcweir     maConfirmFT.Hide();
216*cdf0e10cSrcweir     maConfirmED.Hide();
217*cdf0e10cSrcweir     maPasswordFT.Hide();
218*cdf0e10cSrcweir     maPassword2Box.Hide();
219*cdf0e10cSrcweir     maPassword2FT.Hide();
220*cdf0e10cSrcweir     maPassword2ED.Hide();
221*cdf0e10cSrcweir     maPassword2FT.Hide();
222*cdf0e10cSrcweir     maConfirm2FT.Hide();
223*cdf0e10cSrcweir     maConfirm2ED.Hide();
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     if( mnExtras != SHOWEXTRAS_NONE )
226*cdf0e10cSrcweir         maPasswordFT.Show();
227*cdf0e10cSrcweir     if( (mnExtras & SHOWEXTRAS_USER ) )
228*cdf0e10cSrcweir     {
229*cdf0e10cSrcweir         maUserFT.Show();
230*cdf0e10cSrcweir         maUserED.Show();
231*cdf0e10cSrcweir     }
232*cdf0e10cSrcweir     if( (mnExtras & SHOWEXTRAS_CONFIRM ) )
233*cdf0e10cSrcweir     {
234*cdf0e10cSrcweir         maConfirmFT.Show();
235*cdf0e10cSrcweir         maConfirmED.Show();
236*cdf0e10cSrcweir     }
237*cdf0e10cSrcweir     if( (mnExtras & SHOWEXTRAS_PASSWORD2) )
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         maPassword2Box.Show();
240*cdf0e10cSrcweir         maPassword2FT.Show();
241*cdf0e10cSrcweir         maPassword2ED.Show();
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir     if( (mnExtras & SHOWEXTRAS_CONFIRM2 ) )
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir         maConfirm2FT.Show();
246*cdf0e10cSrcweir         maConfirm2ED.Show();
247*cdf0e10cSrcweir     }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir     boost::shared_ptr<vcl::RowOrColumn> xLayout =
250*cdf0e10cSrcweir         boost::dynamic_pointer_cast<vcl::RowOrColumn>( getLayout() );
251*cdf0e10cSrcweir 	SetSizePixel( xLayout->getOptimalSize( WINDOWSIZE_PREFERRED ) );
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 	return ModalDialog::Execute();
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 
257