xref: /aoo41x/main/uui/source/passworddlg.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 #include "passworddlg.hxx"
29 #include "passworddlg.hrc"
30 #include "ids.hrc"
31 
32 #include <svtools/filedlg.hxx>
33 #include <vcl/msgbox.hxx>
34 
35 
36 using namespace ::com::sun::star;
37 
38 
39 // -----------------------------------------------------------------------
40 
41 static void lcl_Move( Window &rWin, long nOffset )
42 {
43     Point aTmp( rWin.GetPosPixel() );
44     aTmp.Y() += nOffset;
45     rWin.SetPosPixel( aTmp );
46 }
47 
48 // -----------------------------------------------------------------------
49 
50 PasswordDialog::PasswordDialog(
51     Window* _pParent,
52     task::PasswordRequestMode nDlgMode,
53     ResMgr * pResMgr,
54     rtl::OUString& aDocURL,
55     bool bOpenToModify,
56     bool bIsSimplePasswordRequest )
57 
58     :ModalDialog( _pParent, ResId( DLG_UUI_PASSWORD, *pResMgr ) )
59     ,aFTPassword( this, ResId( FT_PASSWORD, *pResMgr ))
60     ,aEDPassword( this, ResId( ED_PASSWORD, *pResMgr ))
61     ,aFTConfirmPassword( this, ResId( FT_CONFIRM_PASSWORD, *pResMgr ))
62     ,aEDConfirmPassword( this, ResId( ED_CONFIRM_PASSWORD, *pResMgr ))
63     ,aOKBtn ( this, ResId( BTN_PASSWORD_OK, *pResMgr ))
64     ,aCancelBtn ( this, ResId( BTN_PASSWORD_CANCEL, *pResMgr ))
65     ,aHelpBtn ( this, ResId( BTN_PASSWORD_HELP, *pResMgr ))
66     ,aFixedLine1( this, ResId( FL_FIXED_LINE_1, *pResMgr ))
67     ,nMinLen(1)
68     ,aPasswdMismatch( ResId( STR_PASSWORD_MISMATCH, *pResMgr ))
69     ,nDialogMode( nDlgMode )
70     ,pResourceMgr ( pResMgr )
71 {
72     if( nDialogMode == task::PasswordRequestMode_PASSWORD_REENTER )
73 	{
74         const sal_uInt16 nOpenToModifyErrStrId = bOpenToModify ? STR_ERROR_PASSWORD_TO_MODIFY_WRONG : STR_ERROR_PASSWORD_TO_OPEN_WRONG;
75         const sal_uInt16 nErrStrId = bIsSimplePasswordRequest ? STR_ERROR_SIMPLE_PASSWORD_WRONG : nOpenToModifyErrStrId;
76         String aErrorMsg( ResId( nErrStrId, *pResourceMgr ));
77         ErrorBox aErrorBox( this, WB_OK, aErrorMsg );
78 		aErrorBox.Execute();
79 	}
80 
81     // default settings for enter password or reenter passwd...
82     String aTitle( ResId( STR_TITLE_ENTER_PASSWORD, *pResourceMgr ) );
83     aFTConfirmPassword.Hide();
84     aEDConfirmPassword.Hide();
85     aFTConfirmPassword.Enable( sal_False );
86     aEDConfirmPassword.Enable( sal_False );
87 
88     // settings for create password
89     if (nDialogMode == task::PasswordRequestMode_PASSWORD_CREATE)
90     {
91         aTitle = String( ResId( STR_TITLE_CREATE_PASSWORD, *pResourceMgr ) );
92 
93         aFTConfirmPassword.SetText( String( ResId( STR_CONFIRM_SIMPLE_PASSWORD, *pResourceMgr ) ) );
94 
95         aFTConfirmPassword.Show();
96         aEDConfirmPassword.Show();
97         aFTConfirmPassword.Enable( sal_True );
98         aEDConfirmPassword.Enable( sal_True );
99     }
100     else
101     {
102         // shrink dialog by size of hidden controls and move rest up accordingly
103 
104         long nDelta = aFixedLine1.GetPosPixel().Y() - aFTConfirmPassword.GetPosPixel().Y();
105 
106         lcl_Move( aFixedLine1, -nDelta );
107         lcl_Move( aOKBtn, -nDelta );
108         lcl_Move( aCancelBtn, -nDelta );
109         lcl_Move( aHelpBtn, -nDelta );
110 
111         Size aNewDlgSize = GetSizePixel();
112         aNewDlgSize.Height() -= nDelta;
113         SetSizePixel( aNewDlgSize );
114     }
115 
116     SetText( aTitle );
117 
118     sal_uInt16 nStrId = bOpenToModify ? STR_ENTER_PASSWORD_TO_MODIFY : STR_ENTER_PASSWORD_TO_OPEN;
119     aFTPassword.SetText( String( ResId( nStrId, *pResourceMgr ) ) );
120     aFTPassword.SetText( aFTPassword.GetText() + aDocURL );
121     if (bIsSimplePasswordRequest)
122     {
123         DBG_ASSERT( aDocURL.getLength() == 0, "A simple password request should not have a document URL! Use document password request instead." );
124         aFTPassword.SetText( String( ResId( STR_ENTER_SIMPLE_PASSWORD, *pResourceMgr ) ) );
125     }
126 
127 	FreeResource();
128 
129 	aOKBtn.SetClickHdl( LINK( this, PasswordDialog, OKHdl_Impl ) );
130 
131 
132     //
133     // move controls down by extra height needed for aFTPassword
134     // (usually only needed if a URL was provided)
135     //
136 
137     long nLabelWidth    = aFTPassword.GetSizePixel().Width();
138     long nLabelHeight   = aFTPassword.GetSizePixel().Height();
139     long nTextWidth     = aFTPassword.GetCtrlTextWidth( aFTPassword.GetText() );
140     long nTextHeight    = aFTPassword.GetTextHeight();
141 
142     Rectangle aLabelRect( aFTPassword.GetPosPixel(), aFTPassword.GetSizePixel() );
143     Rectangle aRect = aFTPassword.GetTextRect( aLabelRect, aFTPassword.GetText() );
144 
145     long nNewLabelHeight = 0;
146     for( nNewLabelHeight = ( nTextWidth / nLabelWidth + 1 ) * nTextHeight;
147         nNewLabelHeight < aRect.GetHeight();
148 		nNewLabelHeight += nTextHeight ) {} ;
149 
150     long nDelta = nNewLabelHeight - nLabelHeight;
151 
152     Size aNewDlgSize = GetSizePixel();
153     aNewDlgSize.Height() += nDelta;
154     SetSizePixel( aNewDlgSize );
155 
156     Size aNewLabelSize = aFTPassword.GetSizePixel();
157     aNewLabelSize.Height() = nNewLabelHeight;
158     aFTPassword.SetPosSizePixel( aFTPassword.GetPosPixel(), aNewLabelSize );
159 
160     lcl_Move( aEDPassword, nDelta );
161     lcl_Move( aFTConfirmPassword, nDelta );
162     lcl_Move( aEDConfirmPassword, nDelta );
163     lcl_Move( aFixedLine1, nDelta );
164     lcl_Move( aOKBtn, nDelta );
165     lcl_Move( aCancelBtn, nDelta );
166     lcl_Move( aHelpBtn, nDelta );
167 }
168 
169 
170 IMPL_LINK( PasswordDialog, OKHdl_Impl, OKButton *, EMPTYARG )
171 {
172     bool bEDPasswdValid = aEDPassword.GetText().Len() >= nMinLen;
173     bool bPasswdMismatch = aEDConfirmPassword.GetText() != aEDPassword.GetText();
174     bool bValid = (!aEDConfirmPassword.IsVisible() && bEDPasswdValid) ||
175             (aEDConfirmPassword.IsVisible() && bEDPasswdValid && !bPasswdMismatch);
176 
177     if (aEDConfirmPassword.IsVisible() && bPasswdMismatch)
178     {
179         ErrorBox aErrorBox( this, WB_OK, aPasswdMismatch );
180         aErrorBox.Execute();
181     }
182     else if (bValid)
183         EndDialog( RET_OK );
184 
185     return 1;
186 }
187 
188