1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "passworddlg.hxx"
21 #include "passworddlg.hrc"
24 #include <vcl/msgbox.hxx>
27 using namespace ::com::sun::star
;
30 // -----------------------------------------------------------------------
32 static void lcl_Move( Window
&rWin
, long nOffset
)
34 Point
aTmp( rWin
.GetPosPixel() );
36 rWin
.SetPosPixel( aTmp
);
39 // -----------------------------------------------------------------------
41 PasswordDialog::PasswordDialog(
43 task::PasswordRequestMode nDlgMode
,
45 rtl::OUString
& aDocURL
,
47 bool bIsSimplePasswordRequest
)
49 :ModalDialog( _pParent
, ResId( DLG_UUI_PASSWORD
, *pResMgr
) )
50 ,aFTPassword( this, ResId( FT_PASSWORD
, *pResMgr
))
51 ,aEDPassword( this, ResId( ED_PASSWORD
, *pResMgr
))
52 ,aFTConfirmPassword( this, ResId( FT_CONFIRM_PASSWORD
, *pResMgr
))
53 ,aEDConfirmPassword( this, ResId( ED_CONFIRM_PASSWORD
, *pResMgr
))
54 ,aOKBtn ( this, ResId( BTN_PASSWORD_OK
, *pResMgr
))
55 ,aCancelBtn ( this, ResId( BTN_PASSWORD_CANCEL
, *pResMgr
))
56 ,aHelpBtn ( this, ResId( BTN_PASSWORD_HELP
, *pResMgr
))
57 ,aFixedLine1( this, ResId( FL_FIXED_LINE_1
, *pResMgr
))
59 ,aPasswdMismatch( ResId( STR_PASSWORD_MISMATCH
, *pResMgr
))
60 ,nDialogMode( nDlgMode
)
61 ,pResourceMgr ( pResMgr
)
63 if( nDialogMode
== task::PasswordRequestMode_PASSWORD_REENTER
)
65 const sal_uInt16 nOpenToModifyErrStrId
= bOpenToModify
? STR_ERROR_PASSWORD_TO_MODIFY_WRONG
: STR_ERROR_PASSWORD_TO_OPEN_WRONG
;
66 const sal_uInt16 nErrStrId
= bIsSimplePasswordRequest
? STR_ERROR_SIMPLE_PASSWORD_WRONG
: nOpenToModifyErrStrId
;
67 rtl::OUString
aErrorMsg(ResId(nErrStrId
, *pResourceMgr
).toString());
68 ErrorBox
aErrorBox( GetParent(), WB_OK
, aErrorMsg
);
72 // default settings for enter password or reenter passwd...
73 rtl::OUString
aTitle(ResId(STR_TITLE_ENTER_PASSWORD
, *pResourceMgr
).toString());
74 aFTConfirmPassword
.Hide();
75 aEDConfirmPassword
.Hide();
76 aFTConfirmPassword
.Enable( sal_False
);
77 aEDConfirmPassword
.Enable( sal_False
);
79 // settings for create password
80 if (nDialogMode
== task::PasswordRequestMode_PASSWORD_CREATE
)
82 aTitle
= ResId(STR_TITLE_CREATE_PASSWORD
, *pResourceMgr
).toString();
84 aFTConfirmPassword
.SetText(ResId(STR_CONFIRM_SIMPLE_PASSWORD
, *pResourceMgr
).toString());
86 aFTConfirmPassword
.Show();
87 aEDConfirmPassword
.Show();
88 aFTConfirmPassword
.Enable( sal_True
);
89 aEDConfirmPassword
.Enable( sal_True
);
93 // shrink dialog by size of hidden controls and move rest up accordingly
95 long nDelta
= aFixedLine1
.GetPosPixel().Y() - aFTConfirmPassword
.GetPosPixel().Y();
97 lcl_Move( aFixedLine1
, -nDelta
);
98 lcl_Move( aOKBtn
, -nDelta
);
99 lcl_Move( aCancelBtn
, -nDelta
);
100 lcl_Move( aHelpBtn
, -nDelta
);
102 Size aNewDlgSize
= GetSizePixel();
103 aNewDlgSize
.Height() -= nDelta
;
104 SetSizePixel( aNewDlgSize
);
109 sal_uInt16 nStrId
= bOpenToModify
? STR_ENTER_PASSWORD_TO_MODIFY
: STR_ENTER_PASSWORD_TO_OPEN
;
110 aFTPassword
.SetText(ResId(nStrId
, *pResourceMgr
).toString());
111 aFTPassword
.SetText( aFTPassword
.GetText() + aDocURL
);
112 if (bIsSimplePasswordRequest
)
114 DBG_ASSERT( aDocURL
.isEmpty(), "A simple password request should not have a document URL! Use document password request instead." );
115 aFTPassword
.SetText(ResId(STR_ENTER_SIMPLE_PASSWORD
, *pResourceMgr
).toString());
120 aOKBtn
.SetClickHdl( LINK( this, PasswordDialog
, OKHdl_Impl
) );
124 // move controls down by extra height needed for aFTPassword
125 // (usually only needed if a URL was provided)
128 long nLabelWidth
= aFTPassword
.GetSizePixel().Width();
129 long nLabelHeight
= aFTPassword
.GetSizePixel().Height();
130 long nTextWidth
= aFTPassword
.GetCtrlTextWidth( aFTPassword
.GetText() );
131 long nTextHeight
= aFTPassword
.GetTextHeight();
133 Rectangle
aLabelRect( aFTPassword
.GetPosPixel(), aFTPassword
.GetSizePixel() );
134 Rectangle aRect
= aFTPassword
.GetTextRect( aLabelRect
, aFTPassword
.GetText() );
136 long nNewLabelHeight
= 0;
137 for( nNewLabelHeight
= ( nTextWidth
/ nLabelWidth
+ 1 ) * nTextHeight
;
138 nNewLabelHeight
< aRect
.GetHeight();
139 nNewLabelHeight
+= nTextHeight
) {} ;
141 long nDelta
= nNewLabelHeight
- nLabelHeight
;
143 Size aNewDlgSize
= GetSizePixel();
144 aNewDlgSize
.Height() += nDelta
;
145 SetSizePixel( aNewDlgSize
);
147 Size aNewLabelSize
= aFTPassword
.GetSizePixel();
148 aNewLabelSize
.Height() = nNewLabelHeight
;
149 aFTPassword
.SetPosSizePixel( aFTPassword
.GetPosPixel(), aNewLabelSize
);
151 lcl_Move( aEDPassword
, nDelta
);
152 lcl_Move( aFTConfirmPassword
, nDelta
);
153 lcl_Move( aEDConfirmPassword
, nDelta
);
154 lcl_Move( aFixedLine1
, nDelta
);
155 lcl_Move( aOKBtn
, nDelta
);
156 lcl_Move( aCancelBtn
, nDelta
);
157 lcl_Move( aHelpBtn
, nDelta
);
161 IMPL_LINK_NOARG(PasswordDialog
, OKHdl_Impl
)
163 bool bEDPasswdValid
= aEDPassword
.GetText().Len() >= nMinLen
;
164 bool bPasswdMismatch
= aEDConfirmPassword
.GetText() != aEDPassword
.GetText();
165 bool bValid
= (!aEDConfirmPassword
.IsVisible() && bEDPasswdValid
) ||
166 (aEDConfirmPassword
.IsVisible() && bEDPasswdValid
&& !bPasswdMismatch
);
168 if (aEDConfirmPassword
.IsVisible() && bPasswdMismatch
)
170 ErrorBox
aErrorBox( this, WB_OK
, aPasswdMismatch
);
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */