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 <officecfg/Office/Common.hxx>
21 #include <sfx2/passwd.hxx>
22 #include <sfx2/sfxresid.hxx>
23 #include <sfx2/strings.hrc>
24 #include <svl/PasswordHelper.hxx>
25 #include <rtl/ustrbuf.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/weld.hxx>
29 IMPL_LINK_NOARG(SfxPasswordDialog
, EditModifyHdl
, weld::Entry
&, void)
34 void SfxPasswordDialog::ModifyHdl()
36 OUString aPassword1Text
= m_xPassword1ED
->get_text();
37 bool bEnable
= aPassword1Text
.getLength() >= mnMinLen
;
38 if (m_xPassword2ED
->get_visible())
39 bEnable
= (bEnable
&& (m_xPassword2ED
->get_text().getLength() >= mnMinLen
));
40 m_xOKBtn
->set_sensitive(bEnable
);
42 // if there's a confirm entry, the dialog is being used for setting a password
43 if (m_xConfirm1ED
->get_visible())
45 m_xPassword1StrengthBar
->set_percentage(
46 SvPasswordHelper::GetPasswordStrengthPercentage(aPassword1Text
));
47 bool bPasswordMeetsPolicy
= SvPasswordHelper::PasswordMeetsPolicy(
48 aPassword1Text
, moPasswordPolicy
);
49 m_xPassword1ED
->set_message_type(bPasswordMeetsPolicy
? weld::EntryMessageType::Normal
50 : weld::EntryMessageType::Error
);
51 m_xPassword1PolicyLabel
->set_visible(!bPasswordMeetsPolicy
);
54 // if there's a confirm entry, the dialog is being used for setting a password
55 if (m_xConfirm2ED
->get_visible())
57 OUString aPassword2Text
= m_xPassword2ED
->get_text();
59 m_xPassword2StrengthBar
->set_percentage(
60 SvPasswordHelper::GetPasswordStrengthPercentage(m_xPassword2ED
->get_text()));
62 // second password is optional, ignore policy if it is empty
63 bool bPasswordMeetsPolicy
64 = aPassword2Text
.isEmpty()
66 : SvPasswordHelper::PasswordMeetsPolicy(
67 aPassword2Text
, moPasswordPolicy
);
68 m_xPassword2ED
->set_message_type(bPasswordMeetsPolicy
? weld::EntryMessageType::Normal
69 : weld::EntryMessageType::Error
);
70 m_xPassword2PolicyLabel
->set_visible(!bPasswordMeetsPolicy
);
74 IMPL_LINK(SfxPasswordDialog
, InsertTextHdl
, OUString
&, rTest
, bool)
79 const sal_Unicode
* pTest
= rTest
.getStr();
80 sal_Int32 nLen
= rTest
.getLength();
81 OUStringBuffer
aFilter(nLen
);
83 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
88 aFilter
.append(*pTest
);
94 rTest
= aFilter
.makeStringAndClear();
95 // upgrade from "Normal" to "Warning" if a invalid letter was
97 m_xOnlyAsciiFT
->set_label_type(weld::LabelType::Warning
);
103 IMPL_LINK_NOARG(SfxPasswordDialog
, OKHdl
, weld::Button
&, void)
105 if (m_xConfirm1ED
->get_visible()
106 && !SvPasswordHelper::PasswordMeetsPolicy(GetPassword(), moPasswordPolicy
))
108 m_xPassword1ED
->grab_focus();
111 if (m_xConfirm2ED
->get_visible() && !GetPassword2().isEmpty()
112 && !SvPasswordHelper::PasswordMeetsPolicy(GetPassword2(), moPasswordPolicy
))
114 m_xPassword2ED
->grab_focus();
118 bool bConfirmFailed
= bool( mnExtras
& SfxShowExtras::CONFIRM
) &&
119 ( GetConfirm() != GetPassword() );
120 if( ( mnExtras
& SfxShowExtras::CONFIRM2
) && ( m_xConfirm2ED
->get_text() != GetPassword2() ) )
121 bConfirmFailed
= true;
122 if ( bConfirmFailed
)
124 if (m_xConfirmFailedDialog
)
125 m_xConfirmFailedDialog
->response(RET_CANCEL
);
127 m_xConfirmFailedDialog
=
128 std::shared_ptr
<weld::MessageDialog
>(Application::CreateMessageDialog(m_xDialog
.get(),
129 VclMessageType::Warning
, VclButtonsType::Ok
,
130 SfxResId(STR_ERROR_WRONG_CONFIRM
)));
131 m_xConfirmFailedDialog
->runAsync(m_xConfirmFailedDialog
, [this](sal_uInt32 response
){
132 m_xConfirm1ED
->set_text(OUString());
133 m_xConfirm1ED
->grab_focus();
134 m_xConfirmFailedDialog
->response(response
);
138 m_xDialog
->response(RET_OK
);
141 // CTOR / DTOR -----------------------------------------------------------
143 SfxPasswordDialog::SfxPasswordDialog(weld::Widget
* pParent
, const OUString
* pGroupText
)
144 : GenericDialogController(pParent
, u
"sfx/ui/password.ui"_ustr
, u
"PasswordDialog"_ustr
)
145 , m_xPassword1Box(m_xBuilder
->weld_frame(u
"password1frame"_ustr
))
146 , m_xUserFT(m_xBuilder
->weld_label(u
"userft"_ustr
))
147 , m_xUserED(m_xBuilder
->weld_entry(u
"usered"_ustr
))
148 , m_xPassword1FT(m_xBuilder
->weld_label(u
"pass1ft"_ustr
))
149 , m_xPassword1ED(m_xBuilder
->weld_entry(u
"pass1ed"_ustr
))
150 , m_xPassword1StrengthBar(m_xBuilder
->weld_level_bar(u
"pass1bar"_ustr
))
151 , m_xPassword1PolicyLabel(m_xBuilder
->weld_label(u
"pass1policylabel"_ustr
))
152 , m_xConfirm1FT(m_xBuilder
->weld_label(u
"confirm1ft"_ustr
))
153 , m_xConfirm1ED(m_xBuilder
->weld_entry(u
"confirm1ed"_ustr
))
154 , m_xPassword2Box(m_xBuilder
->weld_frame(u
"password2frame"_ustr
))
155 , m_xPassword2FT(m_xBuilder
->weld_label(u
"pass2ft"_ustr
))
156 , m_xPassword2ED(m_xBuilder
->weld_entry(u
"pass2ed"_ustr
))
157 , m_xPassword2StrengthBar(m_xBuilder
->weld_level_bar(u
"pass2bar"_ustr
))
158 , m_xPassword2PolicyLabel(m_xBuilder
->weld_label(u
"pass2policylabel"_ustr
))
159 , m_xConfirm2FT(m_xBuilder
->weld_label(u
"confirm2ft"_ustr
))
160 , m_xConfirm2ED(m_xBuilder
->weld_entry(u
"confirm2ed"_ustr
))
161 , m_xMinLengthFT(m_xBuilder
->weld_label(u
"minlenft"_ustr
))
162 , m_xOnlyAsciiFT(m_xBuilder
->weld_label(u
"onlyascii"_ustr
))
163 , m_xOKBtn(m_xBuilder
->weld_button(u
"ok"_ustr
))
164 , maMinLenPwdStr(SfxResId(STR_PASSWD_MIN_LEN
))
165 , maMinLenPwdStr1(SfxResId(STR_PASSWD_MIN_LEN1
))
166 , maEmptyPwdStr(SfxResId(STR_PASSWD_EMPTY
))
168 , mnExtras(SfxShowExtras::NONE
)
169 , moPasswordPolicy(officecfg::Office::Common:: Security::Scripting::PasswordPolicy::get())
172 Link
<weld::Entry
&,void> aLink
= LINK(this, SfxPasswordDialog
, EditModifyHdl
);
173 m_xPassword1ED
->connect_changed(aLink
);
174 m_xPassword2ED
->connect_changed(aLink
);
175 Link
<OUString
&,bool> aLink2
= LINK(this, SfxPasswordDialog
, InsertTextHdl
);
176 m_xPassword1ED
->connect_insert_text(aLink2
);
177 m_xPassword2ED
->connect_insert_text(aLink2
);
178 m_xConfirm1ED
->connect_insert_text(aLink2
);
179 m_xConfirm2ED
->connect_insert_text(aLink2
);
180 m_xOKBtn
->connect_clicked(LINK(this, SfxPasswordDialog
, OKHdl
));
184 m_xPassword1PolicyLabel
->set_label(
185 officecfg::Office::Common::Security::Scripting::PasswordPolicyErrorMessage::get());
186 m_xPassword2PolicyLabel
->set_label(
187 officecfg::Office::Common::Security::Scripting::PasswordPolicyErrorMessage::get());
191 m_xPassword1Box
->set_label(*pGroupText
);
193 //set the text to the password length
197 void SfxPasswordDialog::SetPasswdText( )
199 //set the new string to the minimum password length
201 m_xMinLengthFT
->set_label(maEmptyPwdStr
);
205 m_xMinLengthFT
->set_label(maMinLenPwdStr1
);
208 maMainPwdStr
= maMinLenPwdStr
;
209 maMainPwdStr
= maMainPwdStr
.replaceAll( "$(MINLEN)", OUString::number(static_cast<sal_Int32
>(mnMinLen
) ) );
210 m_xMinLengthFT
->set_label(maMainPwdStr
);
216 void SfxPasswordDialog::SetMinLen( sal_uInt16 nLen
)
223 void SfxPasswordDialog::ShowMinLengthText(bool bShow
)
225 m_xMinLengthFT
->set_visible(bShow
);
228 void SfxPasswordDialog::AllowAsciiOnly()
231 m_xOnlyAsciiFT
->show();
234 void SfxPasswordDialog::PreRun()
238 m_xConfirm1FT
->hide();
239 m_xConfirm1ED
->hide();
240 m_xPassword1StrengthBar
->hide();
241 m_xPassword1FT
->hide();
242 m_xPassword2Box
->hide();
243 m_xPassword2FT
->hide();
244 m_xPassword2ED
->hide();
245 m_xPassword2FT
->hide();
246 m_xConfirm2FT
->hide();
247 m_xConfirm2ED
->hide();
248 m_xPassword2StrengthBar
->hide();
250 if (mnExtras
!= SfxShowExtras::NONE
)
251 m_xPassword1FT
->show();
252 if (mnExtras
& SfxShowExtras::USER
)
257 if (mnExtras
& SfxShowExtras::CONFIRM
)
259 m_xConfirm1FT
->show();
260 m_xConfirm1ED
->show();
261 m_xPassword1StrengthBar
->show();
263 if (mnExtras
& SfxShowExtras::PASSWORD2
)
265 m_xPassword2Box
->show();
266 m_xPassword2FT
->show();
267 m_xPassword2ED
->show();
269 if (mnExtras
& SfxShowExtras::CONFIRM2
)
271 m_xConfirm2FT
->show();
272 m_xConfirm2ED
->show();
273 m_xPassword2StrengthBar
->show();
277 short SfxPasswordDialog::run()
281 return GenericDialogController::run();
284 SfxPasswordDialog::~SfxPasswordDialog()
286 if (m_xConfirmFailedDialog
)
287 m_xConfirmFailedDialog
->response(RET_CANCEL
);
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */