Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/dialog/securitypage.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 <sfx2/htmlmode.hxx>
21
22
#include <sfx2/sfxresid.hxx>
23
24
#include <sfx2/sfxsids.hrc>
25
#include <sfx2/objsh.hxx>
26
#include <sfx2/viewsh.hxx>
27
#include <sfx2/dispatch.hxx>
28
#include <sfx2/passwd.hxx>
29
30
#include <vcl/svapp.hxx>
31
#include <vcl/vclenum.hxx>
32
#include <vcl/weld/Builder.hxx>
33
#include <vcl/weld/CheckButton.hxx>
34
#include <vcl/weld/MessageDialog.hxx>
35
#include <svl/eitem.hxx>
36
#include <svl/poolitem.hxx>
37
#include <svl/intitem.hxx>
38
#include <svl/PasswordHelper.hxx>
39
#include <tools/debug.hxx>
40
#include <comphelper/docpasswordhelper.hxx>
41
42
#include <sfx2/strings.hrc>
43
44
#include <com/sun/star/frame/XModel3.hpp>
45
46
#include "securitypage.hxx"
47
48
using namespace ::com::sun::star;
49
50
namespace
51
{
52
    enum RedliningMode  { RL_NONE, RL_WRITER, RL_CALC };
53
54
    bool QueryState( TypedWhichId<SfxBoolItem> _nSlot, bool& _rValue )
55
0
    {
56
0
        bool bRet = false;
57
0
        SfxViewShell* pViewSh = SfxViewShell::Current();
58
0
        if (pViewSh)
59
0
        {
60
0
            SfxPoolItemHolder aResult;
61
0
            const SfxItemState nState(pViewSh->GetDispatcher()->QueryState(_nSlot, aResult));
62
0
            bRet = SfxItemState::DEFAULT <= nState;
63
0
            if (bRet)
64
0
                _rValue = static_cast<const SfxBoolItem*>(aResult.getItem())->GetValue();
65
0
        }
66
0
        return bRet;
67
0
    }
68
69
70
    bool QueryRecordChangesProtectionState( RedliningMode _eMode, bool& _rValue )
71
0
    {
72
0
        bool bRet = false;
73
0
        if (_eMode != RL_NONE)
74
0
        {
75
0
            TypedWhichId<SfxBoolItem> nSlot = _eMode == RL_WRITER ? FN_REDLINE_PROTECT : SID_CHG_PROTECT;
76
0
            bRet = QueryState( nSlot, _rValue );
77
0
        }
78
0
        return bRet;
79
0
    }
80
81
82
    bool QueryRecordChangesState( RedliningMode _eMode, bool& _rValue )
83
0
    {
84
0
        bool bRet = false;
85
0
        if (_eMode != RL_NONE)
86
0
        {
87
0
            TypedWhichId<SfxBoolItem> nSlot = _eMode == RL_WRITER ? FN_REDLINE_ON : FID_CHG_RECORD;
88
0
            bRet = QueryState( nSlot, _rValue );
89
0
        }
90
0
        return bRet;
91
0
    }
92
}
93
94
95
static bool lcl_GetPassword(
96
    weld::Window *pParent,
97
    bool bProtect,
98
    /*out*/OUString &rPassword )
99
0
{
100
0
    bool bRes = false;
101
0
    SfxPasswordDialog aPasswdDlg(pParent);
102
0
    aPasswdDlg.SetMinLen(1);
103
0
    if (bProtect)
104
0
        aPasswdDlg.ShowExtras( SfxShowExtras::CONFIRM );
105
0
    if (RET_OK == aPasswdDlg.run() && !aPasswdDlg.GetPassword().isEmpty())
106
0
    {
107
0
        rPassword = aPasswdDlg.GetPassword();
108
0
        bRes = true;
109
0
    }
110
0
    return bRes;
111
0
}
112
113
114
static bool lcl_IsPasswordCorrect(weld::Window *pParent, std::u16string_view rPassword)
115
0
{
116
0
    SfxObjectShell* pCurDocShell = SfxObjectShell::Current();
117
0
    if (!pCurDocShell)
118
0
        return false;
119
120
0
    bool bRes = false;
121
0
    uno::Sequence< sal_Int8 >   aPasswordHash;
122
0
    pCurDocShell->GetProtectionHash( aPasswordHash );
123
124
    // check if supplied password was correct
125
0
    if (aPasswordHash.getLength() == 1 && aPasswordHash[0] == 1)
126
0
    {
127
        // dummy RedlinePassword from OOXML import: get real password info
128
        // from the grab-bag to verify the password
129
0
        const css::uno::Sequence< css::beans::PropertyValue > aDocumentProtection =
130
0
                                                pCurDocShell->GetDocumentProtectionFromGrabBag();
131
0
        bRes =
132
            // password is ok, if there is no DocumentProtection in the GrabBag,
133
            // i.e. the dummy RedlinePassword imported from an OpenDocument file
134
0
            !aDocumentProtection.hasElements() ||
135
            // verify password with the password info imported from OOXML
136
0
            ::comphelper::DocPasswordHelper::IsModifyPasswordCorrect( rPassword,
137
0
                    ::comphelper::DocPasswordHelper::ConvertPasswordInfo ( aDocumentProtection ) );
138
0
    }
139
0
    else
140
0
    {
141
0
        uno::Sequence< sal_Int8 > aNewPasswd( aPasswordHash );
142
0
        SvPasswordHelper::GetHashPassword( aNewPasswd, rPassword );
143
0
        bRes = SvPasswordHelper::CompareHashPassword( aPasswordHash, rPassword );
144
0
    }
145
146
0
    if ( !bRes )
147
0
    {
148
0
        std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(pParent,
149
0
                                                      VclMessageType::Info, VclButtonsType::Ok,
150
0
                                                      SfxResId(RID_SVXSTR_INCORRECT_PASSWORD)));
151
0
        xInfoBox->run();
152
0
    }
153
154
0
    return bRes;
155
0
}
156
157
struct SfxSecurityPage_Impl
158
{
159
    SfxSecurityPage &   m_rMyTabPage;
160
161
    RedliningMode       m_eRedlingMode;             // for record changes
162
163
    bool                m_bOrigPasswordIsConfirmed;
164
    bool                m_bNewPasswordIsValid;
165
    OUString            m_aNewPassword;
166
167
    OUString            m_aEndRedliningWarning;
168
    bool                m_bEndRedliningWarningDone;
169
170
    std::unique_ptr<weld::CheckButton> m_xOpenReadonlyCB;
171
    std::unique_ptr<weld::CheckButton> m_xRecordChangesCB;         // for record changes
172
    std::unique_ptr<weld::Button> m_xProtectPB;               // for record changes
173
    std::unique_ptr<weld::Button> m_xUnProtectPB;             // for record changes
174
175
    DECL_LINK(RecordChangesCBToggleHdl, weld::Toggleable&, void);
176
    DECL_LINK(ChangeProtectionPBHdl, weld::Button&, void);
177
178
    SfxSecurityPage_Impl( SfxSecurityPage &rDlg );
179
180
    bool    FillItemSet_Impl();
181
    void    Reset_Impl();
182
};
183
184
SfxSecurityPage_Impl::SfxSecurityPage_Impl(SfxSecurityPage &rTabPage)
185
0
    : m_rMyTabPage(rTabPage)
186
0
    , m_eRedlingMode(RL_NONE)
187
0
    , m_bOrigPasswordIsConfirmed(false)
188
0
    , m_bNewPasswordIsValid(false)
189
0
    , m_aEndRedliningWarning(SfxResId(RID_SVXSTR_END_REDLINING_WARNING))
190
0
    , m_bEndRedliningWarningDone(false)
191
0
    , m_xOpenReadonlyCB(rTabPage.GetBuilder().weld_check_button(u"readonly"_ustr))
192
0
    , m_xRecordChangesCB(rTabPage.GetBuilder().weld_check_button(u"recordchanges"_ustr))
193
0
    , m_xProtectPB(rTabPage.GetBuilder().weld_button(u"protect"_ustr))
194
0
    , m_xUnProtectPB(rTabPage.GetBuilder().weld_button(u"unprotect"_ustr))
195
0
{
196
0
    m_xProtectPB->show();
197
0
    m_xUnProtectPB->hide();
198
199
0
    m_xRecordChangesCB->connect_toggled(LINK(this, SfxSecurityPage_Impl, RecordChangesCBToggleHdl));
200
0
    m_xProtectPB->connect_clicked(LINK(this, SfxSecurityPage_Impl, ChangeProtectionPBHdl));
201
0
    m_xUnProtectPB->connect_clicked(LINK(this, SfxSecurityPage_Impl, ChangeProtectionPBHdl));
202
0
}
203
204
bool SfxSecurityPage_Impl::FillItemSet_Impl()
205
0
{
206
0
    bool bModified = false;
207
208
0
    SfxObjectShell* pCurDocShell = SfxObjectShell::Current();
209
0
    if (pCurDocShell && !pCurDocShell->IsReadOnly())
210
0
    {
211
0
        if (m_eRedlingMode != RL_NONE )
212
0
        {
213
0
            const bool bDoRecordChanges = m_xRecordChangesCB->get_active();
214
0
            const bool bDoChangeProtection  = m_xUnProtectPB->get_visible();
215
216
            // sanity checks
217
0
            DBG_ASSERT( bDoRecordChanges || !bDoChangeProtection, "no change recording should imply no change protection" );
218
0
            DBG_ASSERT( bDoChangeProtection || !bDoRecordChanges, "no change protection should imply no change recording" );
219
0
            DBG_ASSERT( !bDoChangeProtection || !m_aNewPassword.isEmpty(), "change protection should imply password length is > 0" );
220
0
            DBG_ASSERT( bDoChangeProtection || m_aNewPassword.isEmpty(), "no change protection should imply password length is 0" );
221
222
            // change recording
223
0
            if (bDoRecordChanges != pCurDocShell->IsChangeRecording())
224
0
            {
225
0
                pCurDocShell->SetChangeRecording( bDoRecordChanges );
226
0
                bModified = true;
227
0
            }
228
229
            // change record protection
230
0
            if (m_bNewPasswordIsValid &&
231
0
                bDoChangeProtection != pCurDocShell->HasChangeRecordProtection())
232
0
            {
233
0
                DBG_ASSERT( !bDoChangeProtection || bDoRecordChanges,
234
0
                        "change protection requires record changes to be active!" );
235
0
                pCurDocShell->SetProtectionPassword( m_aNewPassword );
236
0
                bModified = true;
237
0
            }
238
0
        }
239
240
        // open read-only?
241
0
        const bool bDoOpenReadonly = m_xOpenReadonlyCB->get_active();
242
0
        if (bDoOpenReadonly != pCurDocShell->IsSecurityOptOpenReadOnly())
243
0
        {
244
0
            pCurDocShell->SetSecurityOptOpenReadOnly( bDoOpenReadonly );
245
0
            bModified = true;
246
0
        }
247
0
    }
248
249
0
    return bModified;
250
0
}
251
252
253
void SfxSecurityPage_Impl::Reset_Impl()
254
0
{
255
0
    SfxObjectShell* pCurDocShell = SfxObjectShell::Current();
256
257
0
    if (!pCurDocShell)
258
0
    {
259
        // no doc -> hide document settings
260
0
        m_xOpenReadonlyCB->set_sensitive(false);
261
0
        m_xRecordChangesCB->set_sensitive(false);
262
0
        m_xProtectPB->show();
263
0
        m_xProtectPB->set_sensitive(false);
264
0
        m_xUnProtectPB->hide();
265
0
        m_xUnProtectPB->set_sensitive(false);
266
0
    }
267
0
    else
268
0
    {
269
0
        bool bIsHTMLDoc = false;
270
0
        bool bProtect = true, bUnProtect = false;
271
0
        SfxViewShell* pViewSh = SfxViewShell::Current();
272
0
        if (pViewSh)
273
0
        {
274
0
            SfxPoolItemHolder aResult;
275
276
0
            if (SfxItemState::DEFAULT <= pViewSh->GetDispatcher()->QueryState(SID_HTML_MODE, aResult))
277
0
            {
278
0
                const SfxUInt16Item* pItem(static_cast<const SfxUInt16Item*>(aResult.getItem()));
279
0
                const sal_uInt16 nMode(pItem->GetValue());
280
0
                bIsHTMLDoc = ( ( nMode & HTMLMODE_ON ) != 0 );
281
0
            }
282
0
        }
283
284
0
        bool bIsReadonly = pCurDocShell->IsReadOnly();
285
0
        if (!bIsHTMLDoc)
286
0
        {
287
0
            m_xOpenReadonlyCB->set_active(pCurDocShell->IsSecurityOptOpenReadOnly());
288
0
            m_xOpenReadonlyCB->set_sensitive(!bIsReadonly);
289
0
        }
290
0
        else
291
0
            m_xOpenReadonlyCB->set_sensitive(false);
292
293
0
        bool bRecordChanges;
294
0
        if (QueryRecordChangesState( RL_WRITER, bRecordChanges ) && !bIsHTMLDoc)
295
0
            m_eRedlingMode = RL_WRITER;
296
0
        else if (QueryRecordChangesState( RL_CALC, bRecordChanges ))
297
0
            m_eRedlingMode = RL_CALC;
298
0
        else
299
0
            m_eRedlingMode = RL_NONE;
300
301
0
        if (m_eRedlingMode != RL_NONE)
302
0
        {
303
0
            bool bProtection(false);
304
0
            QueryRecordChangesProtectionState( m_eRedlingMode, bProtection );
305
306
0
            m_xProtectPB->set_sensitive(!bIsReadonly);
307
0
            m_xUnProtectPB->set_sensitive(!bIsReadonly);
308
            // set the right text
309
0
            if (bProtection)
310
0
            {
311
0
                bProtect = false;
312
0
                bUnProtect = true;
313
0
            }
314
315
0
            m_xRecordChangesCB->set_active(bRecordChanges);
316
0
            m_xRecordChangesCB->set_sensitive(/*!bProtection && */!bIsReadonly);
317
318
0
            m_bOrigPasswordIsConfirmed = true;   // default case if no password is set
319
0
            uno::Sequence< sal_Int8 > aPasswordHash;
320
            // check if password is available
321
0
            if (pCurDocShell->GetProtectionHash( aPasswordHash ) &&
322
0
                aPasswordHash.hasElements())
323
0
                m_bOrigPasswordIsConfirmed = false;  // password found, needs to be confirmed later on
324
0
        }
325
0
        else
326
0
        {
327
            // A Calc document that is shared will have 'm_eRedlingMode == RL_NONE'
328
            // In shared documents change recording and protection must be disabled,
329
            // similar to documents that do not support change recording at all.
330
0
            m_xRecordChangesCB->set_active(false);
331
0
            m_xRecordChangesCB->set_sensitive(false);
332
0
            m_xProtectPB->set_sensitive(false);
333
0
            m_xUnProtectPB->set_sensitive(false);
334
0
        }
335
336
0
        m_xProtectPB->set_visible(bProtect);
337
0
        m_xUnProtectPB->set_visible(bUnProtect);
338
0
    }
339
0
}
340
341
IMPL_LINK_NOARG(SfxSecurityPage_Impl, RecordChangesCBToggleHdl, weld::Toggleable&, void)
342
0
{
343
    // when change recording gets disabled protection must be disabled as well
344
0
    if (m_xRecordChangesCB->get_active())    // the new check state is already present, thus the '!'
345
0
        return;
346
347
0
    bool bAlreadyDone = false;
348
0
    if (!m_bEndRedliningWarningDone)
349
0
    {
350
0
        std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(m_rMyTabPage.GetFrameWeld(),
351
0
                                                   VclMessageType::Warning, VclButtonsType::YesNo,
352
0
                                                   m_aEndRedliningWarning));
353
0
        xWarn->set_default_response(RET_NO);
354
0
        if (xWarn->run() != RET_YES)
355
0
            bAlreadyDone = true;
356
0
        else
357
0
            m_bEndRedliningWarningDone = true;
358
0
    }
359
360
0
    const bool bNeedPassword = !m_bOrigPasswordIsConfirmed
361
0
            && m_xUnProtectPB->get_visible(); // tdf#128230 Require password if the Unprotect button is visible
362
0
    if (!bAlreadyDone && bNeedPassword)
363
0
    {
364
0
        OUString aPasswordText;
365
366
        // dialog canceled or no password provided
367
0
        if (!lcl_GetPassword( m_rMyTabPage.GetFrameWeld(), false, aPasswordText ))
368
0
            bAlreadyDone = true;
369
370
        // ask for password and if dialog is canceled or no password provided return
371
0
        if (lcl_IsPasswordCorrect(m_rMyTabPage.GetFrameWeld(), aPasswordText))
372
0
            m_bOrigPasswordIsConfirmed = true;
373
0
        else
374
0
            bAlreadyDone = true;
375
0
    }
376
377
0
    if (bAlreadyDone)
378
0
        m_xRecordChangesCB->set_active(true);     // restore original state
379
0
    else
380
0
    {
381
        // remember required values to change protection and change recording in
382
        // FillItemSet_Impl later on if password was correct.
383
0
        m_bNewPasswordIsValid = true;
384
0
        m_aNewPassword.clear();
385
0
        m_xProtectPB->show();
386
0
        m_xUnProtectPB->hide();
387
0
    }
388
0
}
389
390
IMPL_LINK_NOARG(SfxSecurityPage_Impl, ChangeProtectionPBHdl, weld::Button&, void)
391
0
{
392
0
    if (m_eRedlingMode == RL_NONE)
393
0
        return;
394
395
    // the push button text is always the opposite of the current state. Thus:
396
0
    const bool bCurrentProtection = m_xUnProtectPB->get_visible();
397
398
    // ask user for password (if still necessary)
399
0
    OUString aPasswordText;
400
0
    bool bNewProtection = !bCurrentProtection;
401
0
    const bool bNeedPassword = bNewProtection || !m_bOrigPasswordIsConfirmed;
402
0
    if (bNeedPassword)
403
0
    {
404
        // ask for password and if dialog is canceled or no password provided return
405
0
        if (!lcl_GetPassword(m_rMyTabPage.GetFrameWeld(), bNewProtection, aPasswordText))
406
0
            return;
407
408
        // provided password still needs to be checked?
409
0
        if (!bNewProtection && !m_bOrigPasswordIsConfirmed)
410
0
        {
411
0
            if (lcl_IsPasswordCorrect(m_rMyTabPage.GetFrameWeld(), aPasswordText))
412
0
                m_bOrigPasswordIsConfirmed = true;
413
0
            else
414
0
                return;
415
0
        }
416
0
    }
417
0
    DBG_ASSERT( m_bOrigPasswordIsConfirmed, "ooops... this should not have happened!" );
418
419
    // remember required values to change protection and change recording in
420
    // FillItemSet_Impl later on if password was correct.
421
0
    m_bNewPasswordIsValid = true;
422
0
    m_aNewPassword = bNewProtection? aPasswordText : OUString();
423
424
0
    m_xRecordChangesCB->set_active(bNewProtection);
425
426
0
    m_xUnProtectPB->set_visible(bNewProtection);
427
0
    m_xProtectPB->set_visible(!bNewProtection);
428
0
}
429
430
std::unique_ptr<SfxTabPage> SfxSecurityPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet * rItemSet)
431
0
{
432
0
    return std::make_unique<SfxSecurityPage>(pPage, pController, *rItemSet);
433
0
}
434
435
SfxSecurityPage::SfxSecurityPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rItemSet)
436
0
    : SfxTabPage(pPage, pController, u"sfx/ui/securityinfopage.ui"_ustr, u"SecurityInfoPage"_ustr, &rItemSet)
437
0
{
438
0
    m_pImpl.reset(new SfxSecurityPage_Impl( *this ));
439
0
}
440
441
bool SfxSecurityPage::FillItemSet( SfxItemSet * /*rItemSet*/ )
442
0
{
443
0
    bool bModified = false;
444
0
    DBG_ASSERT(m_pImpl, "implementation pointer is 0. Still in c-tor?");
445
0
    if (m_pImpl != nullptr)
446
0
        bModified = m_pImpl->FillItemSet_Impl();
447
0
    return bModified;
448
0
}
449
450
void SfxSecurityPage::Reset( const SfxItemSet * /*rItemSet*/ )
451
0
{
452
0
    DBG_ASSERT(m_pImpl, "implementation pointer is 0. Still in c-tor?");
453
0
    if (m_pImpl != nullptr)
454
0
        m_pImpl->Reset_Impl();
455
0
}
456
457
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */