Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/uui/source/passworddlg.cxx
Line
Count
Source
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 "passworddlg.hxx"
21
#include <strings.hrc>
22
23
#include <comphelper/string.hxx>
24
#include <unotools/resmgr.hxx>
25
#include <unotools/configmgr.hxx>
26
#include <tools/urlobj.hxx>
27
#include <tools/debug.hxx>
28
#include <vcl/svapp.hxx>
29
#include <vcl/vclenum.hxx>
30
#include <vcl/weld/Builder.hxx>
31
#include <vcl/weld/Dialog.hxx>
32
#include <vcl/weld/MessageDialog.hxx>
33
#include <vcl/weld/weld.hxx>
34
#include <bitmaps.hlst>
35
36
using namespace ::com::sun::star;
37
38
PasswordDialog::PasswordDialog(weld::Window* pParent,
39
    task::PasswordRequestMode nDialogMode, const std::locale& rResLocale,
40
    const OUString& aDocURL, bool bIsPasswordToModify, bool bIsSimplePasswordRequest)
41
0
    : GenericDialogController(pParent, u"uui/ui/password.ui"_ustr, u"PasswordDialog"_ustr)
42
0
    , m_xFTPassword(m_xBuilder->weld_label(u"newpassFT"_ustr))
43
0
    , m_xEDPassword(m_xBuilder->weld_entry(u"newpassEntry"_ustr))
44
0
    , m_xFTConfirmPassword(m_xBuilder->weld_label(u"confirmpassFT"_ustr))
45
0
    , m_xEDConfirmPassword(m_xBuilder->weld_entry(u"confirmpassEntry"_ustr))
46
0
    , m_xOKBtn(m_xBuilder->weld_button(u"ok"_ustr))
47
0
    , m_xBtn1(m_xBuilder->weld_toggle_button(u"togglebt1"_ustr))
48
0
    , m_xBtn2(m_xBuilder->weld_toggle_button(u"togglebt2"_ustr))
49
0
    , nMinLen(1)
50
0
    , aPasswdMismatch(Translate::get(STR_PASSWORD_MISMATCH, rResLocale))
51
0
{
52
    // Disable the OK button until something has been entered in the password field
53
0
    m_xOKBtn->set_sensitive(false);
54
    // tdf#115964 we can be launched before the parent has resized to its final size
55
0
    m_xDialog->set_centered_on_parent(true);
56
57
0
    Link<weld::Toggleable&, void> aToggleLink = LINK(this, PasswordDialog, ShowHdl);
58
0
    m_xBtn1->connect_toggled(aToggleLink);
59
0
    m_xBtn2->connect_toggled(aToggleLink);
60
61
    // default settings for enter password or reenter passwd...
62
0
    m_xFTConfirmPassword->hide();
63
0
    m_xEDConfirmPassword->hide();
64
0
    m_xBtn2->hide();
65
0
    m_xFTConfirmPassword->set_sensitive(false);
66
0
    m_xEDConfirmPassword->set_sensitive(false);
67
0
    m_xBtn2->set_sensitive(false);
68
69
    // settings for create password
70
0
    if (nDialogMode == task::PasswordRequestMode_PASSWORD_CREATE)
71
0
    {
72
0
        m_xDialog->set_title(Translate::get(STR_TITLE_CREATE_PASSWORD, rResLocale));
73
0
        m_xFTConfirmPassword->set_label(Translate::get(STR_CONFIRM_SIMPLE_PASSWORD, rResLocale));
74
75
0
        m_xFTConfirmPassword->show();
76
0
        m_xEDConfirmPassword->show();
77
0
        m_xFTConfirmPassword->set_sensitive(true);
78
0
        m_xEDConfirmPassword->set_sensitive(true);
79
0
        m_xBtn2->show();
80
0
    }
81
82
0
    auto xOpenLabel = m_xBuilder->weld_label(u"openPasswordLabel"_ustr);
83
0
    auto xEditLabel = m_xBuilder->weld_label(u"editPasswordLabel"_ustr);
84
85
0
    xOpenLabel->set_visible(!bIsPasswordToModify);
86
0
    xEditLabel->set_visible(bIsPasswordToModify);
87
88
0
    OUString aMessage(Translate::get(STR_ENTER_PASSWORD_TO_OPEN, rResLocale));
89
90
0
    INetURLObject url(aDocURL);
91
92
    // Append filename and product name to the title for password manager compatibility
93
0
    OUString sFileName = url.getName(INetURLObject::LAST_SEGMENT, true,
94
0
                                     INetURLObject::DecodeMechanism::Unambiguous);
95
0
    if (!sFileName.isEmpty())
96
0
    {
97
0
        const OUString sFinalTitle = Translate::get(STR_TITLE_FULL_FORMAT, rResLocale)
98
0
            .replaceAll(u"%TITLE", m_xDialog->get_title())
99
0
            .replaceAll(u"%FILENAME", sFileName)
100
0
            .replaceAll(u"%PRODUCTNAME", utl::ConfigManager::getProductName());
101
102
0
        m_xDialog->set_title(sFinalTitle);
103
0
    }
104
105
0
    OUString sTooltipTextToShow = url.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous);
106
107
0
    if (!url.HasError() && !sTooltipTextToShow.isEmpty())
108
0
    {
109
0
        OUStringBuffer sFileNameBuffer(sFileName);
110
0
        OUString sFileNameShort = comphelper::string::truncateToLength(sFileNameBuffer, 50).makeStringAndClear();
111
112
0
        m_xFTPassword->set_label(aMessage.replaceAll("%s", sFileNameShort));
113
0
        m_xFTPassword->set_tooltip_text(sTooltipTextToShow);
114
0
    }
115
0
    else
116
0
    {
117
0
        m_xFTPassword->set_label(aMessage.replaceAll("%s", aDocURL));
118
0
        m_xFTPassword->set_tooltip_text(aDocURL);
119
0
    }
120
0
    m_xEDPassword->grab_focus();
121
122
0
    if (bIsSimplePasswordRequest)
123
0
    {
124
0
        DBG_ASSERT( aDocURL.isEmpty(), "A simple password request should not have a document URL! Use document password request instead." );
125
0
        m_xFTPassword->set_label(Translate::get(STR_ENTER_SIMPLE_PASSWORD, rResLocale));
126
0
        m_xFTPassword->set_tooltip_text({});
127
0
    }
128
129
    // This signal handler enables/disables the OK button based on whether the password entry is empty
130
0
    m_xEDPassword->connect_changed(LINK(this, PasswordDialog, EnableOKBtn_Impl));
131
132
0
    EnableOKBtn_Impl(*m_xEDPassword);
133
134
0
    m_xOKBtn->connect_clicked(LINK(this, PasswordDialog, OKHdl_Impl));
135
0
}
136
137
IMPL_LINK_NOARG(PasswordDialog, EnableOKBtn_Impl, weld::Entry&, void)
138
0
{
139
0
    bool bFirstPasswordHasText = !m_xEDPassword->get_text().isEmpty();
140
0
    m_xOKBtn->set_sensitive(bFirstPasswordHasText);
141
0
}
142
143
IMPL_LINK_NOARG(PasswordDialog, OKHdl_Impl, weld::Button&, void)
144
0
{
145
0
    bool bEDPasswdValid = m_xEDPassword->get_text().getLength() >= nMinLen;
146
0
    bool bPasswdMismatch = m_xEDConfirmPassword->get_visible() && (m_xEDConfirmPassword->get_text() != m_xEDPassword->get_text());
147
148
0
    bool bValid = (!m_xEDConfirmPassword->get_visible() && bEDPasswdValid) ||
149
0
            (m_xEDConfirmPassword->get_visible() && bEDPasswdValid && !bPasswdMismatch);
150
151
0
    if (m_xEDConfirmPassword->get_visible() && bPasswdMismatch)
152
0
    {
153
0
        std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
154
0
                                                  VclMessageType::Warning, VclButtonsType::Ok,
155
0
                                                  aPasswdMismatch));
156
0
        xBox->run();
157
158
0
        m_xEDPassword->set_text({});
159
0
        m_xEDConfirmPassword->set_text({});
160
0
        m_xEDPassword->grab_focus();
161
0
    }
162
0
    else if (bValid)
163
0
        m_xDialog->response(RET_OK);
164
0
}
165
166
IMPL_LINK(PasswordDialog, ShowHdl, weld::Toggleable& ,rToggleable, void)
167
0
{
168
0
    bool bChecked = rToggleable.get_active();
169
0
    if (&rToggleable == m_xBtn1.get())
170
0
    {
171
0
        if (bChecked)
172
0
        {
173
0
            m_xBtn1->set_from_icon_name(RID_SVXBMP_SHOWPASS);
174
0
            m_xEDPassword->set_visibility(true);
175
0
            m_xEDPassword->grab_focus();
176
0
        }
177
0
        else
178
0
        {
179
0
            m_xBtn1->set_from_icon_name(RID_SVXBMP_HIDEPASS);
180
0
            m_xEDPassword->set_visibility(false);
181
0
            m_xEDPassword->grab_focus();
182
0
        }
183
0
    }
184
0
    else if (&rToggleable == m_xBtn2.get())
185
0
    {
186
0
        if (bChecked)
187
0
        {
188
0
            m_xBtn2->set_from_icon_name(RID_SVXBMP_SHOWPASS);
189
0
            m_xEDConfirmPassword->set_visibility(true);
190
0
            m_xEDConfirmPassword->grab_focus();
191
0
        }
192
0
        else
193
0
        {
194
0
            m_xBtn2->set_from_icon_name(RID_SVXBMP_HIDEPASS);
195
0
            m_xEDConfirmPassword->set_visibility(false);
196
0
            m_xEDConfirmPassword->grab_focus();
197
0
        }
198
0
    }
199
0
}
200
201
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
202