LibreOffice Module cui (master)  1
passwdomdlg.cxx
Go to the documentation of this file.
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 <vcl/svapp.hxx>
21 #include <passwdomdlg.hxx>
22 #include <strings.hrc>
23 #include <dialmgr.hxx>
24 
26 {
27  bool bInvalidState = !m_xOpenReadonlyCB->get_active() &&
28  m_xPasswdToOpenED->get_text().isEmpty() &&
29  m_xPasswdToModifyED->get_text().isEmpty();
30  if (bInvalidState)
31  {
32  std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
33  VclMessageType::Warning, VclButtonsType::Ok,
34  m_bIsPasswordToModify? m_aInvalidStateForOkButton : m_aInvalidStateForOkButton_v2));
35  xErrorBox->run();
36  }
37  else // check for mismatched passwords...
38  {
39  const bool bToOpenMatch = m_xPasswdToOpenED->get_text() == m_xReenterPasswdToOpenED->get_text();
40  const bool bToModifyMatch = m_xPasswdToModifyED->get_text() == m_xReenterPasswdToModifyED->get_text();
41  const int nMismatch = (bToOpenMatch? 0 : 1) + (bToModifyMatch? 0 : 1);
42  if (nMismatch > 0)
43  {
44  std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(m_xDialog.get(),
45  VclMessageType::Warning, VclButtonsType::Ok,
46  nMismatch == 1 ? m_aOneMismatch : m_aTwoMismatch));
47  xErrorBox->run();
48 
49  weld::Entry* pEdit = !bToOpenMatch ? m_xPasswdToOpenED.get() : m_xPasswdToModifyED.get();
50  weld::Entry* pRepeatEdit = !bToOpenMatch? m_xReenterPasswdToOpenED.get() : m_xReenterPasswdToModifyED.get();
51  if (nMismatch == 1)
52  {
53  pEdit->set_text( "" );
54  pRepeatEdit->set_text( "" );
55  }
56  else if (nMismatch == 2)
57  {
58  m_xPasswdToOpenED->set_text( "" );
59  m_xReenterPasswdToOpenED->set_text( "" );
60  m_xPasswdToModifyED->set_text( "" );
61  m_xReenterPasswdToModifyED->set_text( "" );
62  }
63  pEdit->grab_focus();
64  }
65  else
66  {
67  m_xDialog->response(RET_OK);
68  }
69  }
70 }
71 
73 {
74  weld::Label* pIndicator = nullptr;
75  int nLength = rEntry.get_text().getLength();
76  if (&rEntry == m_xPasswdToOpenED.get())
77  pIndicator = m_xPasswdToOpenInd.get();
78  else if (&rEntry == m_xReenterPasswdToOpenED.get())
79  pIndicator = m_xReenterPasswdToOpenInd.get();
80  else if (&rEntry == m_xPasswdToModifyED.get())
81  pIndicator = m_xPasswdToModifyInd.get();
82  else if (&rEntry == m_xReenterPasswdToModifyED.get())
83  pIndicator = m_xReenterPasswdToModifyInd.get();
84  assert(pIndicator);
85  pIndicator->set_visible(nLength >= m_nMaxPasswdLen);
86 }
87 
88 PasswordToOpenModifyDialog::PasswordToOpenModifyDialog(weld::Window * pParent, sal_uInt16 nMaxPasswdLen, bool bIsPasswordToModify)
89  : SfxDialogController(pParent, "cui/ui/password.ui", "PasswordDialog")
90  , m_xPasswdToOpenED(m_xBuilder->weld_entry("newpassEntry"))
91  , m_xPasswdToOpenInd(m_xBuilder->weld_label("newpassIndicator"))
92  , m_xReenterPasswdToOpenED(m_xBuilder->weld_entry("confirmpassEntry"))
93  , m_xReenterPasswdToOpenInd(m_xBuilder->weld_label("confirmpassIndicator"))
94  , m_xOptionsExpander(m_xBuilder->weld_expander("expander"))
95  , m_xOk(m_xBuilder->weld_button("ok"))
96  , m_xOpenReadonlyCB(m_xBuilder->weld_check_button("readonly"))
97  , m_xPasswdToModifyFT(m_xBuilder->weld_label("label7"))
98  , m_xPasswdToModifyED(m_xBuilder->weld_entry("newpassroEntry"))
99  , m_xPasswdToModifyInd(m_xBuilder->weld_label("newpassroIndicator"))
100  , m_xReenterPasswdToModifyFT(m_xBuilder->weld_label("label8"))
101  , m_xReenterPasswdToModifyED(m_xBuilder->weld_entry("confirmropassEntry"))
102  , m_xReenterPasswdToModifyInd(m_xBuilder->weld_label("confirmropassIndicator"))
103  , m_aOneMismatch( CuiResId( RID_SVXSTR_ONE_PASSWORD_MISMATCH ) )
104  , m_aTwoMismatch( CuiResId( RID_SVXSTR_TWO_PASSWORDS_MISMATCH ) )
105  , m_aInvalidStateForOkButton( CuiResId( RID_SVXSTR_INVALID_STATE_FOR_OK_BUTTON ) )
106  , m_aInvalidStateForOkButton_v2( CuiResId( RID_SVXSTR_INVALID_STATE_FOR_OK_BUTTON_V2 ) )
107  , m_nMaxPasswdLen(nMaxPasswdLen)
108  , m_bIsPasswordToModify( bIsPasswordToModify )
109 {
110  m_xOk->connect_clicked(LINK(this, PasswordToOpenModifyDialog, OkBtnClickHdl));
111 
112  if (nMaxPasswdLen)
113  {
114  OUString aIndicatorTemplate(CuiResId(RID_SVXSTR_PASSWORD_LEN_INDICATOR).replaceFirst("%1", OUString::number(nMaxPasswdLen)));
115  m_xPasswdToOpenED->set_max_length( nMaxPasswdLen );
116  m_xPasswdToOpenED->connect_changed(LINK(this, PasswordToOpenModifyDialog, ChangeHdl));
117  m_xPasswdToOpenInd->set_label(aIndicatorTemplate);
118  m_xReenterPasswdToOpenED->set_max_length( nMaxPasswdLen );
119  m_xReenterPasswdToOpenED->connect_changed(LINK(this, PasswordToOpenModifyDialog, ChangeHdl));
120  m_xReenterPasswdToOpenInd->set_label(aIndicatorTemplate);
121  m_xPasswdToModifyED->set_max_length( nMaxPasswdLen );
122  m_xPasswdToModifyED->connect_changed(LINK(this, PasswordToOpenModifyDialog, ChangeHdl));
123  m_xPasswdToModifyInd->set_label(aIndicatorTemplate);
124  m_xReenterPasswdToModifyED->set_max_length( nMaxPasswdLen );
125  m_xReenterPasswdToModifyED->connect_changed(LINK(this, PasswordToOpenModifyDialog, ChangeHdl));
126  m_xReenterPasswdToModifyInd->set_label(aIndicatorTemplate);
127  }
128 
129  m_xPasswdToOpenED->grab_focus();
130 
131  m_xOptionsExpander->set_sensitive(bIsPasswordToModify);
132  if (!bIsPasswordToModify)
133  m_xOptionsExpander->hide();
134 
135  m_xOpenReadonlyCB->connect_clicked(LINK(this, PasswordToOpenModifyDialog, ReadonlyOnOffHdl));
136  ReadonlyOnOffHdl(*m_xOpenReadonlyCB);
137 }
138 
140 {
141  const bool bPasswdOk =
142  !m_xPasswdToOpenED->get_text().isEmpty() &&
143  m_xPasswdToOpenED->get_text() == m_xReenterPasswdToOpenED->get_text();
144  return bPasswdOk ? m_xPasswdToOpenED->get_text() : OUString();
145 }
146 
147 
149 {
150  const bool bPasswdOk =
151  !m_xPasswdToModifyED->get_text().isEmpty() &&
152  m_xPasswdToModifyED->get_text() == m_xReenterPasswdToModifyED->get_text();
153  return bPasswdOk ? m_xPasswdToModifyED->get_text() : OUString();
154 }
155 
156 
158 {
159  return m_xOpenReadonlyCB->get_active();
160 }
161 
163 {
164  bool bEnable = m_xOpenReadonlyCB->get_active();
165  m_xPasswdToModifyED->set_sensitive(bEnable);
166  m_xPasswdToModifyFT->set_sensitive(bEnable);
167  m_xReenterPasswdToModifyED->set_sensitive(bEnable);
168  m_xReenterPasswdToModifyFT->set_sensitive(bEnable);
169 }
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Entry > m_xPasswdToOpenED
Definition: passwdomdlg.hxx:26
std::unique_ptr< weld::CheckButton > m_xOpenReadonlyCB
Definition: passwdomdlg.hxx:32
std::unique_ptr< weld::Entry > m_xReenterPasswdToModifyED
Definition: passwdomdlg.hxx:37
std::unique_ptr< weld::Expander > m_xOptionsExpander
Definition: passwdomdlg.hxx:30
std::unique_ptr< weld::Label > m_xReenterPasswdToOpenInd
Definition: passwdomdlg.hxx:29
virtual void set_text(const OUString &rText)=0
std::unique_ptr< weld::Button > m_xOk
Definition: passwdomdlg.hxx:31
IMPL_LINK(PasswordToOpenModifyDialog, ChangeHdl, weld::Entry &, rEntry, void)
Definition: passwdomdlg.cxx:72
IMPL_LINK_NOARG(PasswordToOpenModifyDialog, OkBtnClickHdl, weld::Button &, void)
Definition: passwdomdlg.cxx:25
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool IsRecommendToOpenReadonly() const
std::unique_ptr< weld::Label > m_xPasswdToOpenInd
Definition: passwdomdlg.hxx:27
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
OUString GetPasswordToOpen() const
std::unique_ptr< weld::Entry > m_xPasswdToModifyED
Definition: passwdomdlg.hxx:34
PasswordToOpenModifyDialog(const PasswordToOpenModifyDialog &)=delete
std::unique_ptr< weld::Entry > m_xReenterPasswdToOpenED
Definition: passwdomdlg.hxx:28
std::unique_ptr< weld::Label > m_xPasswdToModifyInd
Definition: passwdomdlg.hxx:35
OUString GetPasswordToModify() const
virtual void grab_focus()=0
RET_OK
Reference< XExecutableDialog > m_xDialog
sal_Int32 nLength
std::unique_ptr< weld::Label > m_xReenterPasswdToModifyInd
Definition: passwdomdlg.hxx:38
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
virtual void set_visible(bool visible)