1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "passworddlg.hxx"
30 #include "passworddlg.hrc"
33 #include <vcl/msgbox.hxx>
36 using namespace ::com::sun::star
;
39 // -----------------------------------------------------------------------
41 static void lcl_Move( Window
&rWin
, long nOffset
)
43 Point
aTmp( rWin
.GetPosPixel() );
45 rWin
.SetPosPixel( aTmp
);
48 // -----------------------------------------------------------------------
50 PasswordDialog::PasswordDialog(
52 task::PasswordRequestMode nDlgMode
,
54 rtl::OUString
& aDocURL
,
56 bool bIsSimplePasswordRequest
)
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
))
68 ,aPasswdMismatch( ResId( STR_PASSWORD_MISMATCH
, *pResMgr
))
69 ,nDialogMode( nDlgMode
)
70 ,pResourceMgr ( pResMgr
)
72 if( nDialogMode
== task::PasswordRequestMode_PASSWORD_REENTER
)
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 rtl::OUString
aErrorMsg(ResId(nErrStrId
, *pResourceMgr
).toString());
77 ErrorBox
aErrorBox( this, WB_OK
, aErrorMsg
);
81 // default settings for enter password or reenter passwd...
82 rtl::OUString
aTitle(ResId(STR_TITLE_ENTER_PASSWORD
, *pResourceMgr
).toString());
83 aFTConfirmPassword
.Hide();
84 aEDConfirmPassword
.Hide();
85 aFTConfirmPassword
.Enable( sal_False
);
86 aEDConfirmPassword
.Enable( sal_False
);
88 // settings for create password
89 if (nDialogMode
== task::PasswordRequestMode_PASSWORD_CREATE
)
91 aTitle
= ResId(STR_TITLE_CREATE_PASSWORD
, *pResourceMgr
).toString();
93 aFTConfirmPassword
.SetText(ResId(STR_CONFIRM_SIMPLE_PASSWORD
, *pResourceMgr
).toString());
95 aFTConfirmPassword
.Show();
96 aEDConfirmPassword
.Show();
97 aFTConfirmPassword
.Enable( sal_True
);
98 aEDConfirmPassword
.Enable( sal_True
);
102 // shrink dialog by size of hidden controls and move rest up accordingly
104 long nDelta
= aFixedLine1
.GetPosPixel().Y() - aFTConfirmPassword
.GetPosPixel().Y();
106 lcl_Move( aFixedLine1
, -nDelta
);
107 lcl_Move( aOKBtn
, -nDelta
);
108 lcl_Move( aCancelBtn
, -nDelta
);
109 lcl_Move( aHelpBtn
, -nDelta
);
111 Size aNewDlgSize
= GetSizePixel();
112 aNewDlgSize
.Height() -= nDelta
;
113 SetSizePixel( aNewDlgSize
);
118 sal_uInt16 nStrId
= bOpenToModify
? STR_ENTER_PASSWORD_TO_MODIFY
: STR_ENTER_PASSWORD_TO_OPEN
;
119 aFTPassword
.SetText(ResId(nStrId
, *pResourceMgr
).toString());
120 aFTPassword
.SetText( aFTPassword
.GetText() + aDocURL
);
121 if (bIsSimplePasswordRequest
)
123 DBG_ASSERT( aDocURL
.isEmpty(), "A simple password request should not have a document URL! Use document password request instead." );
124 aFTPassword
.SetText(ResId(STR_ENTER_SIMPLE_PASSWORD
, *pResourceMgr
).toString());
129 aOKBtn
.SetClickHdl( LINK( this, PasswordDialog
, OKHdl_Impl
) );
133 // move controls down by extra height needed for aFTPassword
134 // (usually only needed if a URL was provided)
137 long nLabelWidth
= aFTPassword
.GetSizePixel().Width();
138 long nLabelHeight
= aFTPassword
.GetSizePixel().Height();
139 long nTextWidth
= aFTPassword
.GetCtrlTextWidth( aFTPassword
.GetText() );
140 long nTextHeight
= aFTPassword
.GetTextHeight();
142 Rectangle
aLabelRect( aFTPassword
.GetPosPixel(), aFTPassword
.GetSizePixel() );
143 Rectangle aRect
= aFTPassword
.GetTextRect( aLabelRect
, aFTPassword
.GetText() );
145 long nNewLabelHeight
= 0;
146 for( nNewLabelHeight
= ( nTextWidth
/ nLabelWidth
+ 1 ) * nTextHeight
;
147 nNewLabelHeight
< aRect
.GetHeight();
148 nNewLabelHeight
+= nTextHeight
) {} ;
150 long nDelta
= nNewLabelHeight
- nLabelHeight
;
152 Size aNewDlgSize
= GetSizePixel();
153 aNewDlgSize
.Height() += nDelta
;
154 SetSizePixel( aNewDlgSize
);
156 Size aNewLabelSize
= aFTPassword
.GetSizePixel();
157 aNewLabelSize
.Height() = nNewLabelHeight
;
158 aFTPassword
.SetPosSizePixel( aFTPassword
.GetPosPixel(), aNewLabelSize
);
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
);
170 IMPL_LINK_NOARG(PasswordDialog
, OKHdl_Impl
)
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
);
177 if (aEDConfirmPassword
.IsVisible() && bPasswdMismatch
)
179 ErrorBox
aErrorBox( this, WB_OK
, aPasswdMismatch
);
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */