xref: /AOO41X/main/uui/source/passworddlg.cxx (revision 3f5c0e0ebfe00e781666f7f3ffdb58033f795c55)
1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "passworddlg.hxx"
29cdf0e10cSrcweir #include "passworddlg.hrc"
30cdf0e10cSrcweir #include "ids.hrc"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <svtools/filedlg.hxx>
33cdf0e10cSrcweir #include <vcl/msgbox.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // -----------------------------------------------------------------------
40cdf0e10cSrcweir 
41cdf0e10cSrcweir static void lcl_Move( Window &rWin, long nOffset )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     Point aTmp( rWin.GetPosPixel() );
44cdf0e10cSrcweir     aTmp.Y() += nOffset;
45cdf0e10cSrcweir     rWin.SetPosPixel( aTmp );
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // -----------------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir PasswordDialog::PasswordDialog(
51cdf0e10cSrcweir     Window* _pParent,
52cdf0e10cSrcweir     task::PasswordRequestMode nDlgMode,
53cdf0e10cSrcweir     ResMgr * pResMgr,
54cdf0e10cSrcweir     rtl::OUString& aDocURL,
55cdf0e10cSrcweir     bool bOpenToModify,
56cdf0e10cSrcweir     bool bIsSimplePasswordRequest )
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     :ModalDialog( _pParent, ResId( DLG_UUI_PASSWORD, *pResMgr ) )
59cdf0e10cSrcweir     ,aFTPassword( this, ResId( FT_PASSWORD, *pResMgr ))
60cdf0e10cSrcweir     ,aEDPassword( this, ResId( ED_PASSWORD, *pResMgr ))
61cdf0e10cSrcweir     ,aFTConfirmPassword( this, ResId( FT_CONFIRM_PASSWORD, *pResMgr ))
62cdf0e10cSrcweir     ,aEDConfirmPassword( this, ResId( ED_CONFIRM_PASSWORD, *pResMgr ))
63cdf0e10cSrcweir     ,aOKBtn ( this, ResId( BTN_PASSWORD_OK, *pResMgr ))
64cdf0e10cSrcweir     ,aCancelBtn ( this, ResId( BTN_PASSWORD_CANCEL, *pResMgr ))
65cdf0e10cSrcweir     ,aHelpBtn ( this, ResId( BTN_PASSWORD_HELP, *pResMgr ))
66cdf0e10cSrcweir     ,aFixedLine1( this, ResId( FL_FIXED_LINE_1, *pResMgr ))
67cdf0e10cSrcweir     ,nMinLen(1)
68cdf0e10cSrcweir     ,aPasswdMismatch( ResId( STR_PASSWORD_MISMATCH, *pResMgr ))
69cdf0e10cSrcweir     ,nDialogMode( nDlgMode )
70cdf0e10cSrcweir     ,pResourceMgr ( pResMgr )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir     if( nDialogMode == task::PasswordRequestMode_PASSWORD_REENTER )
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir         const sal_uInt16 nOpenToModifyErrStrId = bOpenToModify ? STR_ERROR_PASSWORD_TO_MODIFY_WRONG : STR_ERROR_PASSWORD_TO_OPEN_WRONG;
75cdf0e10cSrcweir         const sal_uInt16 nErrStrId = bIsSimplePasswordRequest ? STR_ERROR_SIMPLE_PASSWORD_WRONG : nOpenToModifyErrStrId;
76cdf0e10cSrcweir         String aErrorMsg( ResId( nErrStrId, *pResourceMgr ));
77*3f5c0e0eSMathias Bauer         ErrorBox aErrorBox( GetParent(), WB_OK, aErrorMsg );
78cdf0e10cSrcweir 		aErrorBox.Execute();
79cdf0e10cSrcweir 	}
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     // default settings for enter password or reenter passwd...
82cdf0e10cSrcweir     String aTitle( ResId( STR_TITLE_ENTER_PASSWORD, *pResourceMgr ) );
83cdf0e10cSrcweir     aFTConfirmPassword.Hide();
84cdf0e10cSrcweir     aEDConfirmPassword.Hide();
85cdf0e10cSrcweir     aFTConfirmPassword.Enable( sal_False );
86cdf0e10cSrcweir     aEDConfirmPassword.Enable( sal_False );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     // settings for create password
89cdf0e10cSrcweir     if (nDialogMode == task::PasswordRequestMode_PASSWORD_CREATE)
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         aTitle = String( ResId( STR_TITLE_CREATE_PASSWORD, *pResourceMgr ) );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         aFTConfirmPassword.SetText( String( ResId( STR_CONFIRM_SIMPLE_PASSWORD, *pResourceMgr ) ) );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         aFTConfirmPassword.Show();
96cdf0e10cSrcweir         aEDConfirmPassword.Show();
97cdf0e10cSrcweir         aFTConfirmPassword.Enable( sal_True );
98cdf0e10cSrcweir         aEDConfirmPassword.Enable( sal_True );
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir     else
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         // shrink dialog by size of hidden controls and move rest up accordingly
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         long nDelta = aFixedLine1.GetPosPixel().Y() - aFTConfirmPassword.GetPosPixel().Y();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         lcl_Move( aFixedLine1, -nDelta );
107cdf0e10cSrcweir         lcl_Move( aOKBtn, -nDelta );
108cdf0e10cSrcweir         lcl_Move( aCancelBtn, -nDelta );
109cdf0e10cSrcweir         lcl_Move( aHelpBtn, -nDelta );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         Size aNewDlgSize = GetSizePixel();
112cdf0e10cSrcweir         aNewDlgSize.Height() -= nDelta;
113cdf0e10cSrcweir         SetSizePixel( aNewDlgSize );
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     SetText( aTitle );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     sal_uInt16 nStrId = bOpenToModify ? STR_ENTER_PASSWORD_TO_MODIFY : STR_ENTER_PASSWORD_TO_OPEN;
119cdf0e10cSrcweir     aFTPassword.SetText( String( ResId( nStrId, *pResourceMgr ) ) );
120cdf0e10cSrcweir     aFTPassword.SetText( aFTPassword.GetText() + aDocURL );
121cdf0e10cSrcweir     if (bIsSimplePasswordRequest)
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         DBG_ASSERT( aDocURL.getLength() == 0, "A simple password request should not have a document URL! Use document password request instead." );
124cdf0e10cSrcweir         aFTPassword.SetText( String( ResId( STR_ENTER_SIMPLE_PASSWORD, *pResourceMgr ) ) );
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	FreeResource();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	aOKBtn.SetClickHdl( LINK( this, PasswordDialog, OKHdl_Impl ) );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     //
133cdf0e10cSrcweir     // move controls down by extra height needed for aFTPassword
134cdf0e10cSrcweir     // (usually only needed if a URL was provided)
135cdf0e10cSrcweir     //
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     long nLabelWidth    = aFTPassword.GetSizePixel().Width();
138cdf0e10cSrcweir     long nLabelHeight   = aFTPassword.GetSizePixel().Height();
139cdf0e10cSrcweir     long nTextWidth     = aFTPassword.GetCtrlTextWidth( aFTPassword.GetText() );
140cdf0e10cSrcweir     long nTextHeight    = aFTPassword.GetTextHeight();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     Rectangle aLabelRect( aFTPassword.GetPosPixel(), aFTPassword.GetSizePixel() );
143cdf0e10cSrcweir     Rectangle aRect = aFTPassword.GetTextRect( aLabelRect, aFTPassword.GetText() );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     long nNewLabelHeight = 0;
146cdf0e10cSrcweir     for( nNewLabelHeight = ( nTextWidth / nLabelWidth + 1 ) * nTextHeight;
147cdf0e10cSrcweir         nNewLabelHeight < aRect.GetHeight();
148cdf0e10cSrcweir 		nNewLabelHeight += nTextHeight ) {} ;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     long nDelta = nNewLabelHeight - nLabelHeight;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     Size aNewDlgSize = GetSizePixel();
153cdf0e10cSrcweir     aNewDlgSize.Height() += nDelta;
154cdf0e10cSrcweir     SetSizePixel( aNewDlgSize );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     Size aNewLabelSize = aFTPassword.GetSizePixel();
157cdf0e10cSrcweir     aNewLabelSize.Height() = nNewLabelHeight;
158cdf0e10cSrcweir     aFTPassword.SetPosSizePixel( aFTPassword.GetPosPixel(), aNewLabelSize );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     lcl_Move( aEDPassword, nDelta );
161cdf0e10cSrcweir     lcl_Move( aFTConfirmPassword, nDelta );
162cdf0e10cSrcweir     lcl_Move( aEDConfirmPassword, nDelta );
163cdf0e10cSrcweir     lcl_Move( aFixedLine1, nDelta );
164cdf0e10cSrcweir     lcl_Move( aOKBtn, nDelta );
165cdf0e10cSrcweir     lcl_Move( aCancelBtn, nDelta );
166cdf0e10cSrcweir     lcl_Move( aHelpBtn, nDelta );
167cdf0e10cSrcweir }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 
170cdf0e10cSrcweir IMPL_LINK( PasswordDialog, OKHdl_Impl, OKButton *, EMPTYARG )
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     bool bEDPasswdValid = aEDPassword.GetText().Len() >= nMinLen;
173cdf0e10cSrcweir     bool bPasswdMismatch = aEDConfirmPassword.GetText() != aEDPassword.GetText();
174cdf0e10cSrcweir     bool bValid = (!aEDConfirmPassword.IsVisible() && bEDPasswdValid) ||
175cdf0e10cSrcweir             (aEDConfirmPassword.IsVisible() && bEDPasswdValid && !bPasswdMismatch);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     if (aEDConfirmPassword.IsVisible() && bPasswdMismatch)
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         ErrorBox aErrorBox( this, WB_OK, aPasswdMismatch );
180cdf0e10cSrcweir         aErrorBox.Execute();
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir     else if (bValid)
183cdf0e10cSrcweir         EndDialog( RET_OK );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     return 1;
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188