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