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 <sfx2/objsh.hxx>
21 #include <svl/PasswordHelper.hxx>
22 #include <vcl/svapp.hxx>
23 #include <officecfg/Office/Common.hxx>
24 #include <passwdomdlg.hxx>
25 #include <strings.hrc>
26 #include <dialmgr.hxx>
28 IMPL_LINK_NOARG(PasswordToOpenModifyDialog
, OkBtnClickHdl
, weld::Button
&, void)
30 bool bInvalidState
= !m_xOpenReadonlyCB
->get_active() && !m_bAllowEmpty
&&
31 m_xPasswdToOpenED
->get_text().isEmpty() &&
32 m_xPasswdToModifyED
->get_text().isEmpty();
35 m_xErrorBox
.reset(Application::CreateMessageDialog(m_xDialog
.get(),
36 VclMessageType::Warning
, VclButtonsType::Ok
,
37 m_bIsPasswordToModify
? m_aInvalidStateForOkButton
: m_aInvalidStateForOkButton_v2
));
38 m_xErrorBox
->runAsync(m_xErrorBox
, [](sal_Int32
/*nResult*/) {});
40 else // check for mismatched passwords and password policy
42 if (m_oPasswordPolicy
)
44 if (!SvPasswordHelper::PasswordMeetsPolicy(m_xPasswdToOpenED
->get_text(),
47 m_xPasswdToOpenED
->grab_focus();
51 if (m_xOpenReadonlyCB
->get_active()
52 && !SvPasswordHelper::PasswordMeetsPolicy(m_xPasswdToModifyED
->get_text(),
55 m_xPasswdToModifyED
->grab_focus();
60 const bool bToOpenMatch
= m_xPasswdToOpenED
->get_text() == m_xReenterPasswdToOpenED
->get_text();
61 const bool bToModifyMatch
= m_xPasswdToModifyED
->get_text() == m_xReenterPasswdToModifyED
->get_text();
62 const int nMismatch
= (bToOpenMatch
? 0 : 1) + (bToModifyMatch
? 0 : 1);
65 m_xErrorBox
.reset(Application::CreateMessageDialog(m_xDialog
.get(),
66 VclMessageType::Warning
, VclButtonsType::Ok
,
67 nMismatch
== 1 ? m_aOneMismatch
: m_aTwoMismatch
));
68 m_xErrorBox
->runAsync(m_xErrorBox
, [this, bToOpenMatch
, nMismatch
](sal_Int32
/*nResult*/)
70 weld::Entry
* pEdit
= !bToOpenMatch
? m_xPasswdToOpenED
.get() : m_xPasswdToModifyED
.get();
71 weld::Entry
* pRepeatEdit
= !bToOpenMatch
? m_xReenterPasswdToOpenED
.get() : m_xReenterPasswdToModifyED
.get();
74 pEdit
->set_text( u
""_ustr
);
75 pRepeatEdit
->set_text( u
""_ustr
);
77 else if (nMismatch
== 2)
79 m_xPasswdToOpenED
->set_text( u
""_ustr
);
80 m_xReenterPasswdToOpenED
->set_text( u
""_ustr
);
81 m_xPasswdToModifyED
->set_text( u
""_ustr
);
82 m_xReenterPasswdToModifyED
->set_text( u
""_ustr
);
89 m_xDialog
->response(RET_OK
);
94 IMPL_LINK(PasswordToOpenModifyDialog
, ChangeHdl
, weld::Entry
&, rEntry
, void)
96 auto aPasswordText
= rEntry
.get_text();
98 weld::Label
* pIndicator
= nullptr;
99 weld::LevelBar
* pLevelBar
= nullptr;
100 if (&rEntry
== m_xPasswdToOpenED
.get())
102 pIndicator
= m_xPasswdToOpenInd
.get();
103 pLevelBar
= m_xPasswdToOpenBar
.get();
105 else if (&rEntry
== m_xReenterPasswdToOpenED
.get())
107 pIndicator
= m_xReenterPasswdToOpenInd
.get();
109 else if (&rEntry
== m_xPasswdToModifyED
.get())
111 pIndicator
= m_xPasswdToModifyInd
.get();
112 pLevelBar
= m_xPasswdToModifyBar
.get();
114 else if (&rEntry
== m_xReenterPasswdToModifyED
.get())
116 pIndicator
= m_xReenterPasswdToModifyInd
.get();
120 bool bPasswordMeetsPolicy
121 = SvPasswordHelper::PasswordMeetsPolicy(aPasswordText
, m_oPasswordPolicy
);
124 rEntry
.set_message_type(bPasswordMeetsPolicy
? weld::EntryMessageType::Normal
125 : weld::EntryMessageType::Error
);
126 pIndicator
->set_visible(!bPasswordMeetsPolicy
);
129 // if password doesn't meet policy cap the percentage at 70%
131 pLevelBar
->set_percentage(
132 std::min(SvPasswordHelper::GetPasswordStrengthPercentage(aPasswordText
),
133 bPasswordMeetsPolicy
? std::numeric_limits
<double>::max() : 70.0));
137 int nLength
= aPasswordText
.getLength();
138 pIndicator
->set_visible(nLength
>= m_nMaxPasswdLen
);
142 PasswordToOpenModifyDialog::PasswordToOpenModifyDialog(weld::Window
* pParent
, sal_uInt16 nMaxPasswdLen
, bool bIsPasswordToModify
)
143 : SfxDialogController(pParent
, u
"cui/ui/password.ui"_ustr
, u
"PasswordDialog"_ustr
)
144 , m_xPasswdToOpenED(m_xBuilder
->weld_entry(u
"newpassEntry"_ustr
))
145 , m_xPasswdToOpenInd(m_xBuilder
->weld_label(u
"newpassIndicator"_ustr
))
146 , m_xPasswdToOpenBar(m_xBuilder
->weld_level_bar(u
"passlevelbar"_ustr
))
147 , m_xReenterPasswdToOpenED(m_xBuilder
->weld_entry(u
"confirmpassEntry"_ustr
))
148 , m_xReenterPasswdToOpenInd(m_xBuilder
->weld_label(u
"confirmpassIndicator"_ustr
))
149 , m_xOptionsExpander(m_xBuilder
->weld_expander(u
"expander"_ustr
))
150 , m_xOk(m_xBuilder
->weld_button(u
"ok"_ustr
))
151 , m_xOpenReadonlyCB(m_xBuilder
->weld_check_button(u
"readonly"_ustr
))
152 , m_xPasswdToModifyFT(m_xBuilder
->weld_label(u
"label7"_ustr
))
153 , m_xPasswdToModifyED(m_xBuilder
->weld_entry(u
"newpassroEntry"_ustr
))
154 , m_xPasswdToModifyInd(m_xBuilder
->weld_label(u
"newpassroIndicator"_ustr
))
155 , m_xPasswdToModifyBar(m_xBuilder
->weld_level_bar(u
"ropasslevelbar"_ustr
))
156 , m_xReenterPasswdToModifyFT(m_xBuilder
->weld_label(u
"label8"_ustr
))
157 , m_xReenterPasswdToModifyED(m_xBuilder
->weld_entry(u
"confirmropassEntry"_ustr
))
158 , m_xReenterPasswdToModifyInd(m_xBuilder
->weld_label(u
"confirmropassIndicator"_ustr
))
159 , m_aOneMismatch( CuiResId( RID_CUISTR_ONE_PASSWORD_MISMATCH
) )
160 , m_aTwoMismatch( CuiResId( RID_CUISTR_TWO_PASSWORDS_MISMATCH
) )
161 , m_aInvalidStateForOkButton( CuiResId( RID_CUISTR_INVALID_STATE_FOR_OK_BUTTON
) )
162 , m_aInvalidStateForOkButton_v2( CuiResId( RID_CUISTR_INVALID_STATE_FOR_OK_BUTTON_V2
) )
163 , m_oPasswordPolicy(officecfg::Office::Common::Security::Scripting::PasswordPolicy::get())
164 , m_nMaxPasswdLen(nMaxPasswdLen
)
165 , m_bIsPasswordToModify( bIsPasswordToModify
)
166 , m_bAllowEmpty( false )
168 m_xOk
->connect_clicked(LINK(this, PasswordToOpenModifyDialog
, OkBtnClickHdl
));
169 m_xPasswdToOpenED
->connect_changed(LINK(this, PasswordToOpenModifyDialog
, ChangeHdl
));
170 m_xPasswdToModifyED
->connect_changed(LINK(this, PasswordToOpenModifyDialog
, ChangeHdl
));
171 if(m_oPasswordPolicy
|| nMaxPasswdLen
)
173 m_xReenterPasswdToOpenED
->connect_changed(LINK(this, PasswordToOpenModifyDialog
, ChangeHdl
));
174 m_xReenterPasswdToModifyED
->connect_changed(LINK(this, PasswordToOpenModifyDialog
, ChangeHdl
));
176 OUString aIndicatorText
{};
177 OUString aMaxPassLengthIndicator
{ CuiResId(RID_CUISTR_PASSWORD_LEN_INDICATOR
)
179 OUString::number(nMaxPasswdLen
)) };
180 if (m_oPasswordPolicy
&& nMaxPasswdLen
)
183 = officecfg::Office::Common::Security::Scripting::PasswordPolicyErrorMessage::get()
184 + "\n" + aMaxPassLengthIndicator
;
186 else if (m_oPasswordPolicy
)
189 = officecfg::Office::Common::Security::Scripting::PasswordPolicyErrorMessage::get();
191 else if (nMaxPasswdLen
)
193 aIndicatorText
= aMaxPassLengthIndicator
;
196 m_xPasswdToOpenInd
->set_label(aIndicatorText
);
197 m_xReenterPasswdToOpenInd
->set_label(aMaxPassLengthIndicator
);
198 m_xPasswdToModifyInd
->set_label(aIndicatorText
);
199 m_xReenterPasswdToModifyInd
->set_label(aMaxPassLengthIndicator
);
203 m_xPasswdToOpenED
->set_max_length(nMaxPasswdLen
);
204 m_xReenterPasswdToOpenED
->set_max_length(nMaxPasswdLen
);
205 m_xPasswdToModifyED
->set_max_length(nMaxPasswdLen
);
206 m_xReenterPasswdToModifyED
->set_max_length(nMaxPasswdLen
);
210 m_xPasswdToOpenED
->grab_focus();
212 m_xOptionsExpander
->set_sensitive(bIsPasswordToModify
);
213 if (!bIsPasswordToModify
)
214 m_xOptionsExpander
->hide();
215 else if (SfxObjectShell
* pSh
= SfxObjectShell::Current())
217 if (pSh
->IsLoadReadonly())
219 m_xOpenReadonlyCB
->set_active(true);
220 m_xOptionsExpander
->set_expanded(true);
224 m_xOpenReadonlyCB
->connect_toggled(LINK(this, PasswordToOpenModifyDialog
, ReadonlyOnOffHdl
));
225 ReadonlyOnOffHdl(*m_xOpenReadonlyCB
);
228 PasswordToOpenModifyDialog::~PasswordToOpenModifyDialog()
232 m_xErrorBox
->response(RET_CANCEL
);
236 void PasswordToOpenModifyDialog::AllowEmpty()
238 m_bAllowEmpty
= true;
241 OUString
PasswordToOpenModifyDialog::GetPasswordToOpen() const
243 const bool bPasswdOk
=
244 !m_xPasswdToOpenED
->get_text().isEmpty() &&
245 m_xPasswdToOpenED
->get_text() == m_xReenterPasswdToOpenED
->get_text();
246 return bPasswdOk
? m_xPasswdToOpenED
->get_text() : OUString();
250 OUString
PasswordToOpenModifyDialog::GetPasswordToModify() const
252 const bool bPasswdOk
=
253 !m_xPasswdToModifyED
->get_text().isEmpty() &&
254 m_xPasswdToModifyED
->get_text() == m_xReenterPasswdToModifyED
->get_text();
255 return bPasswdOk
? m_xPasswdToModifyED
->get_text() : OUString();
259 bool PasswordToOpenModifyDialog::IsRecommendToOpenReadonly() const
261 return m_xOpenReadonlyCB
->get_active();
264 IMPL_LINK_NOARG(PasswordToOpenModifyDialog
, ReadonlyOnOffHdl
, weld::Toggleable
&, void)
266 bool bEnable
= m_xOpenReadonlyCB
->get_active();
267 m_xPasswdToModifyED
->set_sensitive(bEnable
);
268 m_xPasswdToModifyFT
->set_sensitive(bEnable
);
269 m_xReenterPasswdToModifyED
->set_sensitive(bEnable
);
270 m_xReenterPasswdToModifyFT
->set_sensitive(bEnable
);
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */