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