Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sfx2 / source / dialog / passwd.cxx
blob51f3abd84c0cef1138c7861554afb66ac91ac3b1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/passwd.hxx>
21 #include <sfx2/sfxresid.hxx>
22 #include <sfx2/strings.hrc>
23 #include <rtl/ustrbuf.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
27 IMPL_LINK_NOARG(SfxPasswordDialog, EditModifyHdl, weld::Entry&, void)
29 ModifyHdl();
32 void SfxPasswordDialog::ModifyHdl()
34 bool bEnable = m_xPassword1ED->get_text().getLength() >= mnMinLen;
35 if (m_xPassword2ED->get_visible())
36 bEnable = (bEnable && (m_xPassword2ED->get_text().getLength() >= mnMinLen));
37 m_xOKBtn->set_sensitive(bEnable);
40 IMPL_LINK(SfxPasswordDialog, InsertTextHdl, OUString&, rTest, bool)
42 if (!mbAsciiOnly)
43 return true;
45 const sal_Unicode* pTest = rTest.getStr();
46 sal_Int32 nLen = rTest.getLength();
47 OUStringBuffer aFilter(nLen);
48 bool bReset = false;
49 for (sal_Int32 i = 0; i < nLen; ++i)
51 if( *pTest > 0x007f )
52 bReset = true;
53 else
54 aFilter.append(*pTest);
55 ++pTest;
58 if (bReset)
60 rTest = aFilter.makeStringAndClear();
61 // upgrade from "Normal" to "Warning" if a invalid letter was
62 // discarded
63 m_xOnlyAsciiFT->set_label_type(weld::LabelType::Warning);
66 return true;
69 IMPL_LINK_NOARG(SfxPasswordDialog, OKHdl, weld::Button&, void)
71 bool bConfirmFailed = bool( mnExtras & SfxShowExtras::CONFIRM ) &&
72 ( GetConfirm() != GetPassword() );
73 if( ( mnExtras & SfxShowExtras::CONFIRM2 ) && ( m_xConfirm2ED->get_text() != GetPassword2() ) )
74 bConfirmFailed = true;
75 if ( bConfirmFailed )
77 if (m_xConfirmFailedDialog)
78 m_xConfirmFailedDialog->response(RET_CANCEL);
80 m_xConfirmFailedDialog =
81 std::shared_ptr<weld::MessageDialog>(Application::CreateMessageDialog(m_xDialog.get(),
82 VclMessageType::Warning, VclButtonsType::Ok,
83 SfxResId(STR_ERROR_WRONG_CONFIRM)));
84 m_xConfirmFailedDialog->runAsync(m_xConfirmFailedDialog, [this](sal_uInt32 response){
85 m_xConfirm1ED->set_text(OUString());
86 m_xConfirm1ED->grab_focus();
87 m_xConfirmFailedDialog->response(response);
88 });
90 else
91 m_xDialog->response(RET_OK);
94 // CTOR / DTOR -----------------------------------------------------------
96 SfxPasswordDialog::SfxPasswordDialog(weld::Widget* pParent, const OUString* pGroupText)
97 : GenericDialogController(pParent, "sfx/ui/password.ui", "PasswordDialog")
98 , m_xPassword1Box(m_xBuilder->weld_frame("password1frame"))
99 , m_xUserFT(m_xBuilder->weld_label("userft"))
100 , m_xUserED(m_xBuilder->weld_entry("usered"))
101 , m_xPassword1FT(m_xBuilder->weld_label("pass1ft"))
102 , m_xPassword1ED(m_xBuilder->weld_entry("pass1ed"))
103 , m_xConfirm1FT(m_xBuilder->weld_label("confirm1ft"))
104 , m_xConfirm1ED(m_xBuilder->weld_entry("confirm1ed"))
105 , m_xPassword2Box(m_xBuilder->weld_frame("password2frame"))
106 , m_xPassword2FT(m_xBuilder->weld_label("pass2ft"))
107 , m_xPassword2ED(m_xBuilder->weld_entry("pass2ed"))
108 , m_xConfirm2FT(m_xBuilder->weld_label("confirm2ft"))
109 , m_xConfirm2ED(m_xBuilder->weld_entry("confirm2ed"))
110 , m_xMinLengthFT(m_xBuilder->weld_label("minlenft"))
111 , m_xOnlyAsciiFT(m_xBuilder->weld_label("onlyascii"))
112 , m_xOKBtn(m_xBuilder->weld_button("ok"))
113 , maMinLenPwdStr(SfxResId(STR_PASSWD_MIN_LEN))
114 , maMinLenPwdStr1(SfxResId(STR_PASSWD_MIN_LEN1))
115 , maEmptyPwdStr(SfxResId(STR_PASSWD_EMPTY))
116 , mnMinLen(5)
117 , mnExtras(SfxShowExtras::NONE)
118 , mbAsciiOnly(false)
120 Link<weld::Entry&,void> aLink = LINK(this, SfxPasswordDialog, EditModifyHdl);
121 m_xPassword1ED->connect_changed(aLink);
122 m_xPassword2ED->connect_changed(aLink);
123 Link<OUString&,bool> aLink2 = LINK(this, SfxPasswordDialog, InsertTextHdl);
124 m_xPassword1ED->connect_insert_text(aLink2);
125 m_xPassword2ED->connect_insert_text(aLink2);
126 m_xConfirm1ED->connect_insert_text(aLink2);
127 m_xConfirm2ED->connect_insert_text(aLink2);
128 m_xOKBtn->connect_clicked(LINK(this, SfxPasswordDialog, OKHdl));
130 if (pGroupText)
131 m_xPassword1Box->set_label(*pGroupText);
133 //set the text to the password length
134 SetPasswdText();
137 void SfxPasswordDialog::SetPasswdText( )
139 //set the new string to the minimum password length
140 if (mnMinLen == 0)
141 m_xMinLengthFT->set_label(maEmptyPwdStr);
142 else
144 if( mnMinLen == 1 )
145 m_xMinLengthFT->set_label(maMinLenPwdStr1);
146 else
148 maMainPwdStr = maMinLenPwdStr;
149 maMainPwdStr = maMainPwdStr.replaceAll( "$(MINLEN)", OUString::number(static_cast<sal_Int32>(mnMinLen) ) );
150 m_xMinLengthFT->set_label(maMainPwdStr);
156 void SfxPasswordDialog::SetMinLen( sal_uInt16 nLen )
158 mnMinLen = nLen;
159 SetPasswdText();
160 ModifyHdl();
163 void SfxPasswordDialog::ShowMinLengthText(bool bShow)
165 m_xMinLengthFT->set_visible(bShow);
168 void SfxPasswordDialog::AllowAsciiOnly()
170 mbAsciiOnly = true;
171 m_xOnlyAsciiFT->show();
174 void SfxPasswordDialog::PreRun()
176 m_xUserFT->hide();
177 m_xUserED->hide();
178 m_xConfirm1FT->hide();
179 m_xConfirm1ED->hide();
180 m_xPassword1FT->hide();
181 m_xPassword2Box->hide();
182 m_xPassword2FT->hide();
183 m_xPassword2ED->hide();
184 m_xPassword2FT->hide();
185 m_xConfirm2FT->hide();
186 m_xConfirm2ED->hide();
188 if (mnExtras != SfxShowExtras::NONE)
189 m_xPassword1FT->show();
190 if (mnExtras & SfxShowExtras::USER)
192 m_xUserFT->show();
193 m_xUserED->show();
195 if (mnExtras & SfxShowExtras::CONFIRM)
197 m_xConfirm1FT->show();
198 m_xConfirm1ED->show();
200 if (mnExtras & SfxShowExtras::PASSWORD2)
202 m_xPassword2Box->show();
203 m_xPassword2FT->show();
204 m_xPassword2ED->show();
206 if (mnExtras & SfxShowExtras::CONFIRM2)
208 m_xConfirm2FT->show();
209 m_xConfirm2ED->show();
213 short SfxPasswordDialog::run()
215 PreRun();
217 return GenericDialogController::run();
220 SfxPasswordDialog::~SfxPasswordDialog()
222 if (m_xConfirmFailedDialog)
223 m_xConfirmFailedDialog->response(RET_CANCEL);
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */