xref: /AOO41X/main/uui/source/passworddlg.cxx (revision 859212d11457d0fe6c8750969b32b9412d9ba923)
1*859212d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*859212d1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*859212d1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*859212d1SAndrew Rist  * distributed with this work for additional information
6*859212d1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*859212d1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*859212d1SAndrew Rist  * "License"); you may not use this file except in compliance
9*859212d1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*859212d1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*859212d1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*859212d1SAndrew Rist  * software distributed under the License is distributed on an
15*859212d1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*859212d1SAndrew Rist  * KIND, either express or implied.  See the License for the
17*859212d1SAndrew Rist  * specific language governing permissions and limitations
18*859212d1SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*859212d1SAndrew Rist  *************************************************************/
21*859212d1SAndrew Rist 
22*859212d1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "passworddlg.hxx"
25cdf0e10cSrcweir #include "passworddlg.hrc"
26cdf0e10cSrcweir #include "ids.hrc"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <svtools/filedlg.hxx>
29cdf0e10cSrcweir #include <vcl/msgbox.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir // -----------------------------------------------------------------------
36cdf0e10cSrcweir 
lcl_Move(Window & rWin,long nOffset)37cdf0e10cSrcweir static void lcl_Move( Window &rWin, long nOffset )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir     Point aTmp( rWin.GetPosPixel() );
40cdf0e10cSrcweir     aTmp.Y() += nOffset;
41cdf0e10cSrcweir     rWin.SetPosPixel( aTmp );
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // -----------------------------------------------------------------------
45cdf0e10cSrcweir 
PasswordDialog(Window * _pParent,task::PasswordRequestMode nDlgMode,ResMgr * pResMgr,rtl::OUString & aDocURL,bool bOpenToModify,bool bIsSimplePasswordRequest)46cdf0e10cSrcweir PasswordDialog::PasswordDialog(
47cdf0e10cSrcweir     Window* _pParent,
48cdf0e10cSrcweir     task::PasswordRequestMode nDlgMode,
49cdf0e10cSrcweir     ResMgr * pResMgr,
50cdf0e10cSrcweir     rtl::OUString& aDocURL,
51cdf0e10cSrcweir     bool bOpenToModify,
52cdf0e10cSrcweir     bool bIsSimplePasswordRequest )
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     :ModalDialog( _pParent, ResId( DLG_UUI_PASSWORD, *pResMgr ) )
55cdf0e10cSrcweir     ,aFTPassword( this, ResId( FT_PASSWORD, *pResMgr ))
56cdf0e10cSrcweir     ,aEDPassword( this, ResId( ED_PASSWORD, *pResMgr ))
57cdf0e10cSrcweir     ,aFTConfirmPassword( this, ResId( FT_CONFIRM_PASSWORD, *pResMgr ))
58cdf0e10cSrcweir     ,aEDConfirmPassword( this, ResId( ED_CONFIRM_PASSWORD, *pResMgr ))
59cdf0e10cSrcweir     ,aOKBtn ( this, ResId( BTN_PASSWORD_OK, *pResMgr ))
60cdf0e10cSrcweir     ,aCancelBtn ( this, ResId( BTN_PASSWORD_CANCEL, *pResMgr ))
61cdf0e10cSrcweir     ,aHelpBtn ( this, ResId( BTN_PASSWORD_HELP, *pResMgr ))
62cdf0e10cSrcweir     ,aFixedLine1( this, ResId( FL_FIXED_LINE_1, *pResMgr ))
63cdf0e10cSrcweir     ,nMinLen(1)
64cdf0e10cSrcweir     ,aPasswdMismatch( ResId( STR_PASSWORD_MISMATCH, *pResMgr ))
65cdf0e10cSrcweir     ,nDialogMode( nDlgMode )
66cdf0e10cSrcweir     ,pResourceMgr ( pResMgr )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     if( nDialogMode == task::PasswordRequestMode_PASSWORD_REENTER )
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir         const sal_uInt16 nOpenToModifyErrStrId = bOpenToModify ? STR_ERROR_PASSWORD_TO_MODIFY_WRONG : STR_ERROR_PASSWORD_TO_OPEN_WRONG;
71cdf0e10cSrcweir         const sal_uInt16 nErrStrId = bIsSimplePasswordRequest ? STR_ERROR_SIMPLE_PASSWORD_WRONG : nOpenToModifyErrStrId;
72cdf0e10cSrcweir         String aErrorMsg( ResId( nErrStrId, *pResourceMgr ));
733f5c0e0eSMathias Bauer         ErrorBox aErrorBox( GetParent(), WB_OK, aErrorMsg );
74cdf0e10cSrcweir 		aErrorBox.Execute();
75cdf0e10cSrcweir 	}
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     // default settings for enter password or reenter passwd...
78cdf0e10cSrcweir     String aTitle( ResId( STR_TITLE_ENTER_PASSWORD, *pResourceMgr ) );
79cdf0e10cSrcweir     aFTConfirmPassword.Hide();
80cdf0e10cSrcweir     aEDConfirmPassword.Hide();
81cdf0e10cSrcweir     aFTConfirmPassword.Enable( sal_False );
82cdf0e10cSrcweir     aEDConfirmPassword.Enable( sal_False );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     // settings for create password
85cdf0e10cSrcweir     if (nDialogMode == task::PasswordRequestMode_PASSWORD_CREATE)
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         aTitle = String( ResId( STR_TITLE_CREATE_PASSWORD, *pResourceMgr ) );
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         aFTConfirmPassword.SetText( String( ResId( STR_CONFIRM_SIMPLE_PASSWORD, *pResourceMgr ) ) );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         aFTConfirmPassword.Show();
92cdf0e10cSrcweir         aEDConfirmPassword.Show();
93cdf0e10cSrcweir         aFTConfirmPassword.Enable( sal_True );
94cdf0e10cSrcweir         aEDConfirmPassword.Enable( sal_True );
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir     else
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         // shrink dialog by size of hidden controls and move rest up accordingly
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         long nDelta = aFixedLine1.GetPosPixel().Y() - aFTConfirmPassword.GetPosPixel().Y();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         lcl_Move( aFixedLine1, -nDelta );
103cdf0e10cSrcweir         lcl_Move( aOKBtn, -nDelta );
104cdf0e10cSrcweir         lcl_Move( aCancelBtn, -nDelta );
105cdf0e10cSrcweir         lcl_Move( aHelpBtn, -nDelta );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         Size aNewDlgSize = GetSizePixel();
108cdf0e10cSrcweir         aNewDlgSize.Height() -= nDelta;
109cdf0e10cSrcweir         SetSizePixel( aNewDlgSize );
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     SetText( aTitle );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     sal_uInt16 nStrId = bOpenToModify ? STR_ENTER_PASSWORD_TO_MODIFY : STR_ENTER_PASSWORD_TO_OPEN;
115cdf0e10cSrcweir     aFTPassword.SetText( String( ResId( nStrId, *pResourceMgr ) ) );
116cdf0e10cSrcweir     aFTPassword.SetText( aFTPassword.GetText() + aDocURL );
117cdf0e10cSrcweir     if (bIsSimplePasswordRequest)
118cdf0e10cSrcweir     {
119cdf0e10cSrcweir         DBG_ASSERT( aDocURL.getLength() == 0, "A simple password request should not have a document URL! Use document password request instead." );
120cdf0e10cSrcweir         aFTPassword.SetText( String( ResId( STR_ENTER_SIMPLE_PASSWORD, *pResourceMgr ) ) );
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	FreeResource();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	aOKBtn.SetClickHdl( LINK( this, PasswordDialog, OKHdl_Impl ) );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     //
129cdf0e10cSrcweir     // move controls down by extra height needed for aFTPassword
130cdf0e10cSrcweir     // (usually only needed if a URL was provided)
131cdf0e10cSrcweir     //
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     long nLabelWidth    = aFTPassword.GetSizePixel().Width();
134cdf0e10cSrcweir     long nLabelHeight   = aFTPassword.GetSizePixel().Height();
135cdf0e10cSrcweir     long nTextWidth     = aFTPassword.GetCtrlTextWidth( aFTPassword.GetText() );
136cdf0e10cSrcweir     long nTextHeight    = aFTPassword.GetTextHeight();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     Rectangle aLabelRect( aFTPassword.GetPosPixel(), aFTPassword.GetSizePixel() );
139cdf0e10cSrcweir     Rectangle aRect = aFTPassword.GetTextRect( aLabelRect, aFTPassword.GetText() );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     long nNewLabelHeight = 0;
142cdf0e10cSrcweir     for( nNewLabelHeight = ( nTextWidth / nLabelWidth + 1 ) * nTextHeight;
143cdf0e10cSrcweir         nNewLabelHeight < aRect.GetHeight();
144cdf0e10cSrcweir 		nNewLabelHeight += nTextHeight ) {} ;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     long nDelta = nNewLabelHeight - nLabelHeight;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     Size aNewDlgSize = GetSizePixel();
149cdf0e10cSrcweir     aNewDlgSize.Height() += nDelta;
150cdf0e10cSrcweir     SetSizePixel( aNewDlgSize );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     Size aNewLabelSize = aFTPassword.GetSizePixel();
153cdf0e10cSrcweir     aNewLabelSize.Height() = nNewLabelHeight;
154cdf0e10cSrcweir     aFTPassword.SetPosSizePixel( aFTPassword.GetPosPixel(), aNewLabelSize );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     lcl_Move( aEDPassword, nDelta );
157cdf0e10cSrcweir     lcl_Move( aFTConfirmPassword, nDelta );
158cdf0e10cSrcweir     lcl_Move( aEDConfirmPassword, nDelta );
159cdf0e10cSrcweir     lcl_Move( aFixedLine1, nDelta );
160cdf0e10cSrcweir     lcl_Move( aOKBtn, nDelta );
161cdf0e10cSrcweir     lcl_Move( aCancelBtn, nDelta );
162cdf0e10cSrcweir     lcl_Move( aHelpBtn, nDelta );
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 
IMPL_LINK(PasswordDialog,OKHdl_Impl,OKButton *,EMPTYARG)166cdf0e10cSrcweir IMPL_LINK( PasswordDialog, OKHdl_Impl, OKButton *, EMPTYARG )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     bool bEDPasswdValid = aEDPassword.GetText().Len() >= nMinLen;
169cdf0e10cSrcweir     bool bPasswdMismatch = aEDConfirmPassword.GetText() != aEDPassword.GetText();
170cdf0e10cSrcweir     bool bValid = (!aEDConfirmPassword.IsVisible() && bEDPasswdValid) ||
171cdf0e10cSrcweir             (aEDConfirmPassword.IsVisible() && bEDPasswdValid && !bPasswdMismatch);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     if (aEDConfirmPassword.IsVisible() && bPasswdMismatch)
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         ErrorBox aErrorBox( this, WB_OK, aPasswdMismatch );
176cdf0e10cSrcweir         aErrorBox.Execute();
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir     else if (bValid)
179cdf0e10cSrcweir         EndDialog( RET_OK );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     return 1;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184