1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sfx2/passwd.hxx>
21 #include <sfxtypes.hxx>
22 #include <sfx2/sfxresid.hxx>
23 #include <sfx2/strings.hrc>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
26 
IMPL_LINK_NOARG(SfxPasswordDialog,EditModifyHdl,weld::Entry &,void)27 IMPL_LINK_NOARG(SfxPasswordDialog, EditModifyHdl, weld::Entry&, void)
28 {
29     ModifyHdl();
30 }
31 
ModifyHdl()32 void SfxPasswordDialog::ModifyHdl()
33 {
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);
38 }
39 
IMPL_LINK(SfxPasswordDialog,InsertTextHdl,OUString &,rTest,bool)40 IMPL_LINK(SfxPasswordDialog, InsertTextHdl, OUString&, rTest, bool)
41 {
42     if (!mbAsciiOnly)
43         return true;
44 
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)
50     {
51         if( *pTest > 0x007f )
52             bReset = true;
53         else
54             aFilter.append(*pTest);
55         ++pTest;
56     }
57 
58     if (bReset)
59         rTest = aFilter.makeStringAndClear();
60 
61     return true;
62 }
63 
IMPL_LINK_NOARG(SfxPasswordDialog,OKHdl,weld::Button &,void)64 IMPL_LINK_NOARG(SfxPasswordDialog, OKHdl, weld::Button&, void)
65 {
66     bool bConfirmFailed = bool( mnExtras & SfxShowExtras::CONFIRM ) &&
67                           ( GetConfirm() != GetPassword() );
68     if( ( mnExtras & SfxShowExtras::CONFIRM2 ) && ( m_xConfirm2ED->get_text() != GetPassword2() ) )
69         bConfirmFailed = true;
70     if ( bConfirmFailed )
71     {
72         std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
73                                                                  VclMessageType::Warning, VclButtonsType::Ok,
74                                                                  SfxResId(STR_ERROR_WRONG_CONFIRM)));
75         xBox->run();
76         m_xConfirm1ED->set_text(OUString());
77         m_xConfirm1ED->grab_focus();
78     }
79     else
80         m_xDialog->response(RET_OK);
81 }
82 
83 // CTOR / DTOR -----------------------------------------------------------
84 
SfxPasswordDialog(weld::Widget * pParent,const OUString * pGroupText)85 SfxPasswordDialog::SfxPasswordDialog(weld::Widget* pParent, const OUString* pGroupText)
86     : GenericDialogController(pParent, "sfx/ui/password.ui", "PasswordDialog")
87     , m_xPassword1Box(m_xBuilder->weld_frame("password1frame"))
88     , m_xUserFT(m_xBuilder->weld_label("userft"))
89     , m_xUserED(m_xBuilder->weld_entry("usered"))
90     , m_xPassword1FT(m_xBuilder->weld_label("pass1ft"))
91     , m_xPassword1ED(m_xBuilder->weld_entry("pass1ed"))
92     , m_xConfirm1FT(m_xBuilder->weld_label("confirm1ft"))
93     , m_xConfirm1ED(m_xBuilder->weld_entry("confirm1ed"))
94     , m_xPassword2Box(m_xBuilder->weld_frame("password2frame"))
95     , m_xPassword2FT(m_xBuilder->weld_label("pass2ft"))
96     , m_xPassword2ED(m_xBuilder->weld_entry("pass2ed"))
97     , m_xConfirm2FT(m_xBuilder->weld_label("confirm2ft"))
98     , m_xConfirm2ED(m_xBuilder->weld_entry("confirm2ed"))
99     , m_xMinLengthFT(m_xBuilder->weld_label("minlenft"))
100     , m_xOKBtn(m_xBuilder->weld_button("ok"))
101     , maMinLenPwdStr(SfxResId(STR_PASSWD_MIN_LEN))
102     , maMinLenPwdStr1(SfxResId(STR_PASSWD_MIN_LEN1))
103     , maEmptyPwdStr(SfxResId(STR_PASSWD_EMPTY))
104     , mnMinLen(5)
105     , mnExtras(SfxShowExtras::NONE)
106     , mbAsciiOnly(false)
107 {
108     Link<weld::Entry&,void> aLink = LINK(this, SfxPasswordDialog, EditModifyHdl);
109     m_xPassword1ED->connect_changed(aLink);
110     m_xPassword2ED->connect_changed(aLink);
111     Link<OUString&,bool> aLink2 = LINK(this, SfxPasswordDialog, InsertTextHdl);
112     m_xPassword1ED->connect_insert_text(aLink2);
113     m_xPassword2ED->connect_insert_text(aLink2);
114     m_xOKBtn->connect_clicked(LINK(this, SfxPasswordDialog, OKHdl));
115 
116     if (pGroupText)
117         m_xPassword1Box->set_label(*pGroupText);
118 
119     //set the text to the password length
120     SetPasswdText();
121 }
122 
SetPasswdText()123 void SfxPasswordDialog::SetPasswdText( )
124 {
125     //set the new string to the minimum password length
126     if (mnMinLen == 0)
127         m_xMinLengthFT->set_label(maEmptyPwdStr);
128     else
129     {
130         if( mnMinLen == 1 )
131             m_xMinLengthFT->set_label(maMinLenPwdStr1);
132         else
133         {
134             maMainPwdStr = maMinLenPwdStr;
135             maMainPwdStr = maMainPwdStr.replaceAll( "$(MINLEN)", OUString::number(static_cast<sal_Int32>(mnMinLen) ) );
136             m_xMinLengthFT->set_label(maMainPwdStr);
137         }
138     }
139 }
140 
141 
SetMinLen(sal_uInt16 nLen)142 void SfxPasswordDialog::SetMinLen( sal_uInt16 nLen )
143 {
144     mnMinLen = nLen;
145     SetPasswdText();
146     ModifyHdl();
147 }
148 
ShowMinLengthText(bool bShow)149 void SfxPasswordDialog::ShowMinLengthText(bool bShow)
150 {
151     m_xMinLengthFT->set_visible(bShow);
152 }
153 
run()154 short SfxPasswordDialog::run()
155 {
156     m_xUserFT->hide();
157     m_xUserED->hide();
158     m_xConfirm1FT->hide();
159     m_xConfirm1ED->hide();
160     m_xPassword1FT->hide();
161     m_xPassword2Box->hide();
162     m_xPassword2FT->hide();
163     m_xPassword2ED->hide();
164     m_xPassword2FT->hide();
165     m_xConfirm2FT->hide();
166     m_xConfirm2ED->hide();
167 
168     if (mnExtras != SfxShowExtras::NONE)
169         m_xPassword1FT->show();
170     if (mnExtras & SfxShowExtras::USER)
171     {
172         m_xUserFT->show();
173         m_xUserED->show();
174     }
175     if (mnExtras & SfxShowExtras::CONFIRM)
176     {
177         m_xConfirm1FT->show();
178         m_xConfirm1ED->show();
179     }
180     if (mnExtras & SfxShowExtras::PASSWORD2)
181     {
182         m_xPassword2Box->show();
183         m_xPassword2FT->show();
184         m_xPassword2ED->show();
185     }
186     if (mnExtras & SfxShowExtras::CONFIRM2)
187     {
188         m_xConfirm2FT->show();
189         m_xConfirm2ED->show();
190     }
191 
192     return GenericDialogController::run();
193 }
194 
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
196