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 <comphelper/string.hxx>
21 #include <vcl/msgbox.hxx>
22 #include "logindlg.hxx"
24 #include "logindlg.hrc"
26 #include <tools/resid.hxx>
27 #include <osl/file.hxx>
31 #define _MAX_PATH PATH_MAX
34 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
35 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
36 #include <comphelper/processfactory.hxx>
38 using namespace com::sun::star
;
40 static void lcl_Move( Window
&rWin
, long nOffset
)
42 Point
aTmp( rWin
.GetPosPixel() );
44 rWin
.SetPosPixel( aTmp
);
48 void LoginDialog::HideControls_Impl( sal_uInt16 nFlags
)
50 bool bPathHide
= sal_False
;
51 bool bErrorHide
= sal_False
;
52 bool bAccountHide
= sal_False
;
53 bool bUseSysCredsHide
= sal_False
;
55 if ( ( nFlags
& LF_NO_PATH
) == LF_NO_PATH
)
62 else if ( ( nFlags
& LF_PATH_READONLY
) == LF_PATH_READONLY
)
64 aPathED
.Enable( sal_False
);
65 aPathBtn
.Enable( sal_False
);
68 if ( ( nFlags
& LF_NO_USERNAME
) == LF_NO_USERNAME
)
73 else if ( ( nFlags
& LF_USERNAME_READONLY
) == LF_USERNAME_READONLY
)
75 aNameED
.Enable( sal_False
);
78 if ( ( nFlags
& LF_NO_PASSWORD
) == LF_NO_PASSWORD
)
84 if ( ( nFlags
& LF_NO_SAVEPASSWORD
) == LF_NO_SAVEPASSWORD
)
85 aSavePasswdBtn
.Hide();
87 if ( ( nFlags
& LF_NO_ERRORTEXT
) == LF_NO_ERRORTEXT
)
92 bErrorHide
= sal_True
;
95 if ( ( nFlags
& LF_NO_ACCOUNT
) == LF_NO_ACCOUNT
)
99 bAccountHide
= sal_True
;
102 if ( ( nFlags
& LF_NO_USESYSCREDS
) == LF_NO_USESYSCREDS
)
104 aUseSysCredsCB
.Hide();
105 bUseSysCredsHide
= sal_True
;
110 long nOffset
= aRequestInfo
.GetPosPixel().Y() -
111 aErrorFT
.GetPosPixel().Y();
112 lcl_Move( aRequestInfo
, nOffset
);
113 lcl_Move( aLogin2FL
, nOffset
);
114 lcl_Move( aPathFT
, nOffset
);
115 lcl_Move( aPathED
, nOffset
);
116 lcl_Move( aPathBtn
, nOffset
);
117 lcl_Move( aNameFT
, nOffset
);
118 lcl_Move( aNameED
, nOffset
);
119 lcl_Move( aPasswordFT
, nOffset
);
120 lcl_Move( aPasswordED
, nOffset
);
121 lcl_Move( aAccountFT
, nOffset
);
122 lcl_Move( aAccountED
, nOffset
);
123 lcl_Move( aSavePasswdBtn
, nOffset
);
124 lcl_Move( aUseSysCredsCB
, nOffset
);
125 lcl_Move( aButtonsFL
, nOffset
);
126 lcl_Move( aOKBtn
, nOffset
);
127 lcl_Move( aCancelBtn
, nOffset
);
128 lcl_Move( aHelpBtn
, nOffset
);
130 Size aNewSiz
= GetSizePixel();
131 aNewSiz
.Height() -= nOffset
;
132 SetSizePixel( aNewSiz
);
137 long nOffset
= aNameED
.GetPosPixel().Y() -
138 aPathED
.GetPosPixel().Y();
139 lcl_Move( aNameFT
, nOffset
);
140 lcl_Move( aNameED
, nOffset
);
141 lcl_Move( aPasswordFT
, nOffset
);
142 lcl_Move( aPasswordED
, nOffset
);
143 lcl_Move( aAccountFT
, nOffset
);
144 lcl_Move( aAccountED
, nOffset
);
145 lcl_Move( aSavePasswdBtn
, nOffset
);
146 lcl_Move( aUseSysCredsCB
, nOffset
);
147 lcl_Move( aButtonsFL
, nOffset
);
148 lcl_Move( aOKBtn
, nOffset
);
149 lcl_Move( aCancelBtn
, nOffset
);
150 lcl_Move( aHelpBtn
, nOffset
);
152 Size aNewSz
= GetSizePixel();
153 aNewSz
.Height() -= nOffset
;
154 SetSizePixel( aNewSz
);
159 long nOffset
= aAccountED
.GetPosPixel().Y() -
160 aPasswordED
.GetPosPixel().Y();
161 lcl_Move( aSavePasswdBtn
, nOffset
);
162 lcl_Move( aUseSysCredsCB
, nOffset
);
163 lcl_Move( aButtonsFL
, nOffset
);
164 lcl_Move( aOKBtn
, nOffset
);
165 lcl_Move( aCancelBtn
, nOffset
);
166 lcl_Move( aHelpBtn
, nOffset
);
168 Size aNewSz
= GetSizePixel();
169 aNewSz
.Height() -= nOffset
;
170 SetSizePixel( aNewSz
);
173 if ( bUseSysCredsHide
)
175 long nOffset
= aUseSysCredsCB
.GetPosPixel().Y() -
176 aSavePasswdBtn
.GetPosPixel().Y();
177 lcl_Move( aButtonsFL
, nOffset
);
178 lcl_Move( aOKBtn
, nOffset
);
179 lcl_Move( aCancelBtn
, nOffset
);
180 lcl_Move( aHelpBtn
, nOffset
);
182 Size aNewSz
= GetSizePixel();
183 aNewSz
.Height() -= nOffset
;
184 SetSizePixel( aNewSz
);
188 void LoginDialog::EnableUseSysCredsControls_Impl( sal_Bool bUseSysCredsEnabled
)
190 aErrorInfo
.Enable( !bUseSysCredsEnabled
);
191 aErrorFT
.Enable( !bUseSysCredsEnabled
);
192 aRequestInfo
.Enable( !bUseSysCredsEnabled
);
193 aPathFT
.Enable( !bUseSysCredsEnabled
);
194 aPathED
.Enable( !bUseSysCredsEnabled
);
195 aPathBtn
.Enable( !bUseSysCredsEnabled
);
196 aNameFT
.Enable( !bUseSysCredsEnabled
);
197 aNameED
.Enable( !bUseSysCredsEnabled
);
198 aPasswordFT
.Enable( !bUseSysCredsEnabled
);
199 aPasswordED
.Enable( !bUseSysCredsEnabled
);
200 aAccountFT
.Enable( !bUseSysCredsEnabled
);
201 aAccountED
.Enable( !bUseSysCredsEnabled
);
204 IMPL_LINK_NOARG(LoginDialog
, OKHdl_Impl
)
207 aNameED
.SetText(comphelper::string::strip(aNameED
.GetText(), ' '));
208 aPasswordED
.SetText(comphelper::string::strip(aPasswordED
.GetText(), ' '));
213 IMPL_LINK_NOARG(LoginDialog
, PathHdl_Impl
)
217 uno::Reference
<ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
219 OUString
aPath( aPathED
.GetText() );
220 osl::FileBase::getFileURLFromSystemPath( aPath
, aPath
);
221 xFolderPicker
->setDisplayDirectory( aPath
);
223 if (xFolderPicker
->execute() == ui::dialogs::ExecutableDialogResults::OK
)
225 osl::FileBase::getSystemPathFromFileURL( xFolderPicker
->getDirectory(), aPath
);
226 aPathED
.SetText( aPath
);
229 catch (uno::Exception
& e
)
231 SAL_WARN("uui", "LoginDialog::PathHdl_Impl: caught UNO exception: " << e
.Message
);
237 IMPL_LINK_NOARG(LoginDialog
, UseSysCredsHdl_Impl
)
239 EnableUseSysCredsControls_Impl( aUseSysCredsCB
.IsChecked() );
243 LoginDialog::LoginDialog
247 const OUString
& rServer
,
248 const OUString
& rRealm
,
252 ModalDialog( pParent
, ResId( DLG_UUI_LOGIN
, *pResMgr
) ),
254 aErrorFT ( this, ResId( FT_LOGIN_ERROR
, *pResMgr
) ),
255 aErrorInfo ( this, ResId( FT_INFO_LOGIN_ERROR
, *pResMgr
) ),
256 aLogin1FL ( this, ResId( FL_LOGIN_1
, *pResMgr
) ),
257 aRequestInfo ( this, ResId( FT_INFO_LOGIN_REQUEST
, *pResMgr
) ),
258 aLogin2FL ( this, ResId( FL_LOGIN_2
, *pResMgr
) ),
259 aPathFT ( this, ResId( FT_LOGIN_PATH
, *pResMgr
) ),
260 aPathED ( this, ResId( ED_LOGIN_PATH
, *pResMgr
) ),
261 aPathBtn ( this, ResId( BTN_LOGIN_PATH
, *pResMgr
) ),
262 aNameFT ( this, ResId( FT_LOGIN_USERNAME
, *pResMgr
) ),
263 aNameED ( this, ResId( ED_LOGIN_USERNAME
, *pResMgr
) ),
264 aPasswordFT ( this, ResId( FT_LOGIN_PASSWORD
, *pResMgr
) ),
265 aPasswordED ( this, ResId( ED_LOGIN_PASSWORD
, *pResMgr
) ),
266 aAccountFT ( this, ResId( FT_LOGIN_ACCOUNT
, *pResMgr
) ),
267 aAccountED ( this, ResId( ED_LOGIN_ACCOUNT
, *pResMgr
) ),
268 aSavePasswdBtn ( this, ResId( CB_LOGIN_SAVEPASSWORD
, *pResMgr
) ),
269 aUseSysCredsCB ( this, ResId( CB_LOGIN_USESYSCREDS
, *pResMgr
) ),
270 aButtonsFL ( this, ResId( FL_BUTTONS
, *pResMgr
) ),
271 aOKBtn ( this, ResId( BTN_LOGIN_OK
, *pResMgr
) ),
272 aCancelBtn ( this, ResId( BTN_LOGIN_CANCEL
, *pResMgr
) ),
273 aHelpBtn ( this, ResId( BTN_LOGIN_HELP
, *pResMgr
) )
277 if ((nFlags
& LF_NO_ACCOUNT
) != 0 && !rRealm
.isEmpty())
279 aRequest
= OUString(ResId(STR_LOGIN_REALM
, *pResMgr
));
280 aRequest
= aRequest
.replaceAll("%2", rRealm
);
283 aRequest
= aRequestInfo
.GetText();
285 if ( !( ( nFlags
& LF_NO_USESYSCREDS
) == LF_NO_USESYSCREDS
) )
286 EnableUseSysCredsControls_Impl( aUseSysCredsCB
.IsChecked() );
288 aRequest
= aRequest
.replaceAll("%1", rServer
);
289 aRequestInfo
.SetText(aRequest
);
293 aPathED
.SetMaxTextLen( _MAX_PATH
);
294 aNameED
.SetMaxTextLen( _MAX_PATH
);
296 aOKBtn
.SetClickHdl( LINK( this, LoginDialog
, OKHdl_Impl
) );
297 aPathBtn
.SetClickHdl( LINK( this, LoginDialog
, PathHdl_Impl
) );
298 aUseSysCredsCB
.SetClickHdl( LINK( this, LoginDialog
, UseSysCredsHdl_Impl
) );
300 HideControls_Impl( nFlags
);
303 LoginDialog::~LoginDialog()
307 void LoginDialog::SetUseSystemCredentials( sal_Bool bUse
)
309 if ( aUseSysCredsCB
.IsVisible() )
311 aUseSysCredsCB
.Check( bUse
);
312 EnableUseSysCredsControls_Impl( bUse
);
316 void LoginDialog::ClearPassword()
318 aPasswordED
.SetText( OUString() );
320 if ( aNameED
.GetText().isEmpty() )
323 aPasswordED
.GrabFocus();
326 void LoginDialog::ClearAccount()
328 aAccountED
.SetText( OUString() );
329 aAccountED
.GrabFocus();
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */