Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/miscdlgs/protectiondlg.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 <protectiondlg.hxx>
21
#include <tabprotection.hxx>
22
#include <svl/PasswordHelper.hxx>
23
#include <vcl/vclenum.hxx>
24
#include <vcl/weld/Builder.hxx>
25
#include <vcl/weld/Dialog.hxx>
26
27
#include <vector>
28
29
namespace {
30
31
// The order must match that of the list box.
32
const std::vector<ScTableProtection::Option> aOptions = {
33
    ScTableProtection::SELECT_LOCKED_CELLS,
34
    ScTableProtection::SELECT_UNLOCKED_CELLS,
35
    ScTableProtection::INSERT_COLUMNS,
36
    ScTableProtection::INSERT_ROWS,
37
    ScTableProtection::DELETE_COLUMNS,
38
    ScTableProtection::DELETE_ROWS,
39
    ScTableProtection::AUTOFILTER,
40
    ScTableProtection::PIVOT_TABLES,
41
};
42
43
}
44
45
ScTableProtectionDlg::ScTableProtectionDlg(weld::Window* pParent)
46
0
    : weld::GenericDialogController(pParent, u"modules/scalc/ui/protectsheetdlg.ui"_ustr, u"ProtectSheetDialog"_ustr)
47
0
    , m_xBtnProtect(m_xBuilder->weld_check_button(u"protect"_ustr))
48
0
    , m_xPasswords(m_xBuilder->weld_container(u"passwords"_ustr))
49
0
    , m_xOptions(m_xBuilder->weld_container(u"options"_ustr))
50
0
    , m_xPassword1Edit(m_xBuilder->weld_entry(u"password1"_ustr))
51
0
    , m_xPassword2Edit(m_xBuilder->weld_entry(u"password2"_ustr))
52
0
    , m_xPasswordStrengthBar(m_xBuilder->weld_level_bar(u"passwordbar"_ustr))
53
0
    , m_xOptionsListBox(m_xBuilder->weld_tree_view(u"checklist"_ustr))
54
0
    , m_xBtnOk(m_xBuilder->weld_button(u"ok"_ustr))
55
0
    , m_xProtected(m_xBuilder->weld_label(u"protected"_ustr))
56
0
    , m_xUnprotected(m_xBuilder->weld_label(u"unprotected"_ustr))
57
0
    , m_xInsertColumns(m_xBuilder->weld_label(u"insert-columns"_ustr))
58
0
    , m_xInsertRows(m_xBuilder->weld_label(u"insert-rows"_ustr))
59
0
    , m_xDeleteColumns(m_xBuilder->weld_label(u"delete-columns"_ustr))
60
0
    , m_xDeleteRows(m_xBuilder->weld_label(u"delete-rows"_ustr))
61
0
    , m_xAutoFilter(m_xBuilder->weld_label(u"useautofilter"_ustr))
62
0
    , m_xPivot(m_xBuilder->weld_label(u"usepivot"_ustr))
63
0
{
64
0
    m_aSelectLockedCells = m_xProtected->get_label();
65
0
    m_aSelectUnlockedCells = m_xUnprotected->get_label();
66
0
    m_aInsertColumns = m_xInsertColumns->get_label();
67
0
    m_aInsertRows = m_xInsertRows->get_label();
68
0
    m_aDeleteColumns = m_xDeleteColumns->get_label();
69
0
    m_aDeleteRows = m_xDeleteRows->get_label();
70
0
    m_aAutoFilter = m_xAutoFilter->get_label();
71
0
    m_aPivot = m_xPivot->get_label();
72
73
0
    m_xOptionsListBox->enable_toggle_buttons();
74
75
0
    Init();
76
0
}
77
78
ScTableProtectionDlg::~ScTableProtectionDlg()
79
0
{
80
0
}
81
82
void ScTableProtectionDlg::SetDialogData(const ScTableProtection& rData)
83
0
{
84
0
    for (size_t i = 0; i < aOptions.size(); ++i)
85
0
        m_xOptionsListBox->set_toggle(i, rData.isOptionEnabled(aOptions[i]) ? TRISTATE_TRUE : TRISTATE_FALSE);
86
0
}
87
88
void ScTableProtectionDlg::WriteData(ScTableProtection& rData) const
89
0
{
90
0
    rData.setProtected(m_xBtnProtect->get_active());
91
92
    // We assume that the two password texts match.
93
0
    rData.setPassword(m_xPassword1Edit->get_text());
94
95
0
    for (size_t i = 0; i < aOptions.size(); ++i)
96
0
        rData.setOption(aOptions[i], m_xOptionsListBox->get_toggle(i) == TRISTATE_TRUE);
97
0
}
98
99
void ScTableProtectionDlg::InsertEntry(const OUString& rTxt)
100
0
{
101
0
    m_xOptionsListBox->append();
102
0
    const int nRow = m_xOptionsListBox->n_children() - 1;
103
0
    m_xOptionsListBox->set_toggle(nRow, TRISTATE_FALSE);
104
0
    m_xOptionsListBox->set_text(nRow, rTxt, 0);
105
0
}
106
107
void ScTableProtectionDlg::Init()
108
0
{
109
0
    m_xBtnProtect->connect_toggled(LINK(this, ScTableProtectionDlg, CheckBoxHdl));
110
111
0
    m_xBtnOk->connect_clicked(LINK(this, ScTableProtectionDlg, OKHdl));
112
113
0
    Link<weld::Entry&,void> aLink = LINK(this, ScTableProtectionDlg, PasswordModifyHdl);
114
0
    m_xPassword1Edit->connect_changed(aLink);
115
0
    m_xPassword2Edit->connect_changed(aLink);
116
117
0
    m_xOptionsListBox->freeze();
118
0
    m_xOptionsListBox->clear();
119
120
0
    InsertEntry(m_aSelectLockedCells);
121
0
    InsertEntry(m_aSelectUnlockedCells);
122
0
    InsertEntry(m_aInsertColumns);
123
0
    InsertEntry(m_aInsertRows);
124
0
    InsertEntry(m_aDeleteColumns);
125
0
    InsertEntry(m_aDeleteRows);
126
0
    InsertEntry(m_aAutoFilter);
127
0
    InsertEntry(m_aPivot);
128
129
0
    m_xOptionsListBox->set_toggle(0, TRISTATE_TRUE);
130
0
    m_xOptionsListBox->set_toggle(1, TRISTATE_TRUE);
131
132
0
    m_xOptionsListBox->thaw();
133
134
    // Set the default state of the dialog.
135
0
    m_xBtnProtect->set_active(true);
136
0
    m_xPassword1Edit->grab_focus();
137
0
}
138
139
void ScTableProtectionDlg::EnableOptionalWidgets(bool bEnable)
140
0
{
141
0
    m_xPasswords->set_sensitive(bEnable);
142
0
    m_xOptions->set_sensitive(bEnable);
143
0
}
144
145
IMPL_LINK(ScTableProtectionDlg, CheckBoxHdl, weld::Toggleable&, rBtn, void)
146
0
{
147
0
    if (&rBtn == m_xBtnProtect.get())
148
0
    {
149
0
        bool bChecked = m_xBtnProtect->get_active();
150
0
        EnableOptionalWidgets(bChecked);
151
0
        m_xBtnOk->set_sensitive(bChecked);
152
0
    }
153
0
}
154
155
IMPL_LINK_NOARG(ScTableProtectionDlg, OKHdl, weld::Button&, void)
156
0
{
157
0
    m_xDialog->response(RET_OK);
158
0
}
159
160
IMPL_LINK(ScTableProtectionDlg, PasswordModifyHdl, weld::Entry&, rEntry, void)
161
0
{
162
0
    OUString aPass1 = m_xPassword1Edit->get_text();
163
0
    if (&rEntry == m_xPassword1Edit.get())
164
0
    {
165
0
        m_xPasswordStrengthBar->set_percentage(
166
0
            SvPasswordHelper::GetPasswordStrengthPercentage(aPass1));
167
0
    }
168
0
    OUString aPass2 = m_xPassword2Edit->get_text();
169
0
    m_xBtnOk->set_sensitive(aPass1 == aPass2);
170
0
}
171
172
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */