Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/dialog/versdlg.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 <sal/config.h>
21
22
#include <com/sun/star/document/XCmisDocument.hpp>
23
#include <com/sun/star/frame/XModel.hpp>
24
#include <com/sun/star/util/RevisionTag.hpp>
25
#include <com/sun/star/beans/NamedValue.hpp>
26
27
#include <officecfg/Office/Common.hxx>
28
#include <unotools/localedatawrapper.hxx>
29
#include <svl/intitem.hxx>
30
#include <svl/stritem.hxx>
31
#include <svl/itemset.hxx>
32
#include <unotools/useroptions.hxx>
33
#include <vcl/svapp.hxx>
34
#include <vcl/settings.hxx>
35
#include <tools/datetime.hxx>
36
37
#include <versdlg.hxx>
38
#include <sfx2/strings.hrc>
39
#include <sfx2/dialoghelper.hxx>
40
#include <sfx2/viewfrm.hxx>
41
#include <sfx2/sfxresid.hxx>
42
#include <sfx2/docfile.hxx>
43
#include <sfx2/objsh.hxx>
44
#include <sfx2/sfxsids.hrc>
45
#include <sfx2/dispatch.hxx>
46
47
#include <sfx2/sfxuno.hxx>
48
#include <memory>
49
#include <vector>
50
51
using namespace com::sun::star;
52
53
struct SfxVersionInfo
54
{
55
    OUString                aName;
56
    OUString                aComment;
57
    OUString                aAuthor;
58
    DateTime                aCreationDate;
59
60
                            SfxVersionInfo();
61
};
62
63
class SfxVersionTableDtor
64
{
65
private:
66
    std::vector<std::unique_ptr<SfxVersionInfo>> aTableList;
67
public:
68
    explicit                SfxVersionTableDtor( const uno::Sequence < util::RevisionTag >& rInfo );
69
    explicit                SfxVersionTableDtor( const uno::Sequence < document::CmisVersion > & rInfo );
70
                            SfxVersionTableDtor(const SfxVersionTableDtor&) = delete;
71
    SfxVersionTableDtor&    operator=(const SfxVersionTableDtor&) = delete;
72
73
    size_t                  size() const
74
0
                            { return aTableList.size(); }
75
76
    SfxVersionInfo*         at( size_t i ) const
77
0
                            { return aTableList[ i ].get(); }
78
};
79
80
SfxVersionTableDtor::SfxVersionTableDtor( const uno::Sequence < util::RevisionTag >& rInfo )
81
0
{
82
0
    for ( const auto& rItem : rInfo )
83
0
    {
84
0
        std::unique_ptr<SfxVersionInfo> pInfo(new SfxVersionInfo);
85
0
        pInfo->aName = rItem.Identifier;
86
0
        pInfo->aComment = rItem.Comment;
87
0
        pInfo->aAuthor = rItem.Author;
88
89
0
        pInfo->aCreationDate = DateTime( rItem.TimeStamp );
90
0
        aTableList.push_back( std::move(pInfo) );
91
0
    }
92
0
}
93
94
SfxVersionTableDtor::SfxVersionTableDtor( const uno::Sequence < document::CmisVersion >& rInfo )
95
0
{
96
0
    for ( const auto& rItem : rInfo )
97
0
    {
98
0
        std::unique_ptr<SfxVersionInfo> pInfo(new SfxVersionInfo);
99
0
        pInfo->aName = rItem.Id;
100
0
        pInfo->aComment = rItem.Comment;
101
0
        pInfo->aAuthor = rItem.Author;
102
103
0
        pInfo->aCreationDate = DateTime( rItem.TimeStamp );
104
0
        aTableList.push_back( std::move(pInfo) );
105
0
    }
106
0
}
107
108
SfxVersionInfo::SfxVersionInfo()
109
0
    : aCreationDate( DateTime::EMPTY )
110
0
{
111
0
}
112
113
namespace
114
{
115
    void setColSizes(weld::TreeView& rVersionBox)
116
0
    {
117
        // recalculate the datetime column width
118
0
        int nWidestTime(rVersionBox.get_pixel_size(getWidestDateTime(Application::GetSettings().GetLocaleDataWrapper(), false)).Width());
119
0
        int nW1 = rVersionBox.get_pixel_size(rVersionBox.get_column_title(1)).Width();
120
121
0
        int nMax = std::max(nWidestTime, nW1) + 12; // max width + a little offset
122
0
        const int nRest = rVersionBox.get_preferred_size().Width() - nMax;
123
124
0
        std::set<OUString> aAuthors;
125
0
        aAuthors.insert(SvtUserOptions().GetFullName());
126
127
0
        for (int i = 0; i < rVersionBox.n_children(); ++i)
128
0
        {
129
0
            aAuthors.insert(weld::fromId<SfxVersionInfo*>(rVersionBox.get_id(i))->aAuthor);
130
0
        }
131
132
0
        int nMaxAuthorWidth = nRest/4;
133
0
        for (auto const& author : aAuthors)
134
0
        {
135
0
            nMaxAuthorWidth = std::max<int>(nMaxAuthorWidth, rVersionBox.get_pixel_size(author).Width());
136
0
            if (nMaxAuthorWidth > nRest/2)
137
0
            {
138
0
                nMaxAuthorWidth = nRest/2;
139
0
                break;
140
0
            }
141
0
        }
142
143
0
        rVersionBox.set_column_fixed_widths({ nMax, nMaxAuthorWidth });
144
0
    }
145
}
146
147
SfxVersionDialog::SfxVersionDialog(weld::Window* pParent, SfxViewFrame* pVwFrame, bool bIsSaveVersionOnClose)
148
0
    : SfxDialogController(pParent, u"sfx/ui/versionsofdialog.ui"_ustr, u"VersionsOfDialog"_ustr)
149
0
    , m_pViewFrame(pVwFrame)
150
0
    , m_bIsSaveVersionOnClose(bIsSaveVersionOnClose)
151
0
    , m_xSaveButton(m_xBuilder->weld_button(u"save"_ustr))
152
0
    , m_xSaveCheckBox(m_xBuilder->weld_check_button(u"always"_ustr))
153
0
    , m_xOpenButton(m_xBuilder->weld_button(u"open"_ustr))
154
0
    , m_xViewButton(m_xBuilder->weld_button(u"show"_ustr))
155
0
    , m_xDeleteButton(m_xBuilder->weld_button(u"delete"_ustr))
156
0
    , m_xCompareButton(m_xBuilder->weld_button(u"compare"_ustr))
157
0
    , m_xCmisButton(m_xBuilder->weld_button(u"cmis"_ustr))
158
0
    , m_xVersionBox(m_xBuilder->weld_tree_view(u"versions"_ustr))
159
0
{
160
0
    m_xVersionBox->set_size_request(m_xVersionBox->get_approximate_digit_width() * 90,
161
0
                                    m_xVersionBox->get_height_rows(15));
162
0
    setColSizes(*m_xVersionBox);
163
164
0
    Link<weld::Button&,void> aClickLink = LINK( this, SfxVersionDialog, ButtonHdl_Impl );
165
0
    m_xViewButton->connect_clicked( aClickLink );
166
0
    m_xSaveButton->connect_clicked( aClickLink );
167
0
    m_xDeleteButton->connect_clicked( aClickLink );
168
0
    m_xCompareButton->connect_clicked( aClickLink );
169
0
    m_xOpenButton->connect_clicked( aClickLink );
170
0
    m_xSaveCheckBox->connect_toggled(LINK(this, SfxVersionDialog, ToggleHdl_Impl));
171
0
    m_xCmisButton->connect_clicked( aClickLink );
172
173
0
    m_xVersionBox->connect_selection_changed(LINK(this, SfxVersionDialog, SelectHdl_Impl));
174
0
    m_xVersionBox->connect_row_activated( LINK( this, SfxVersionDialog, DClickHdl_Impl ) );
175
176
0
    m_xVersionBox->grab_focus();
177
178
    // set dialog title (filename or docinfo title)
179
0
    OUString sText = m_xDialog->get_title() +
180
0
                    " " + m_pViewFrame->GetObjectShell()->GetTitle();
181
0
    m_xDialog->set_title(sText);
182
183
0
    Init_Impl();
184
0
}
185
186
static OUString ConvertWhiteSpaces_Impl( const OUString& rText )
187
0
{
188
    // converted linebreaks and tabs to blanks; it's necessary for the display
189
0
    OUStringBuffer sConverted;
190
0
    const sal_Unicode* pChars = rText.getStr();
191
0
    while ( *pChars )
192
0
    {
193
0
        switch ( *pChars )
194
0
        {
195
0
            case '\n' :
196
0
            case '\t' :
197
0
                sConverted.append(' ');
198
0
                break;
199
200
0
            default:
201
0
                sConverted.append(*pChars);
202
0
        }
203
204
0
        ++pChars;
205
0
    }
206
207
0
    return sConverted.makeStringAndClear();
208
0
}
209
210
void SfxVersionDialog::Init_Impl()
211
0
{
212
0
    SfxObjectShell *pObjShell = m_pViewFrame->GetObjectShell();
213
0
    SfxMedium* pMedium = pObjShell->GetMedium();
214
0
    uno::Sequence < util::RevisionTag > aVersions = pMedium->GetVersionList( true );
215
0
    m_pTable.reset(new SfxVersionTableDtor( aVersions ));
216
0
    m_xVersionBox->freeze();
217
0
    for (size_t n = 0; n < m_pTable->size(); ++n)
218
0
    {
219
0
        SfxVersionInfo *pInfo = m_pTable->at( n );
220
0
        OUString aEntry = formatDateTime(pInfo->aCreationDate, Application::GetSettings().GetLocaleDataWrapper(), false);
221
0
        m_xVersionBox->append(weld::toId(pInfo), aEntry);
222
0
        auto nLastRow = m_xVersionBox->n_children() - 1;
223
0
        m_xVersionBox->set_text(nLastRow, pInfo->aAuthor, 1);
224
0
        m_xVersionBox->set_text(nLastRow, ConvertWhiteSpaces_Impl(pInfo->aComment), 2);
225
0
    }
226
0
    m_xVersionBox->thaw();
227
228
0
    if (auto nCount = m_pTable->size())
229
0
        m_xVersionBox->select(nCount - 1);
230
231
0
    m_xSaveCheckBox->set_active(m_bIsSaveVersionOnClose);
232
233
0
    bool bEnable = !pObjShell->IsReadOnly();
234
0
    m_xSaveButton->set_sensitive( bEnable );
235
0
    m_xSaveCheckBox->set_sensitive( bEnable );
236
237
0
    m_xOpenButton->set_sensitive(false);
238
0
    m_xViewButton->set_sensitive(false);
239
0
    m_xDeleteButton->set_sensitive(false);
240
0
    m_xCompareButton->set_sensitive(false);
241
242
0
    if ( !officecfg::Office::Common::Misc::ExperimentalMode::get() )
243
0
        m_xCmisButton->hide( );
244
0
    uno::Reference<document::XCmisDocument> xCmisDoc(pObjShell->GetModel(), uno::UNO_QUERY);
245
0
    if (xCmisDoc && xCmisDoc->isVersionable())
246
0
        m_xCmisButton->set_sensitive(true);
247
0
    else
248
0
        m_xCmisButton->set_sensitive(false);
249
250
0
    SelectHdl_Impl(*m_xVersionBox);
251
0
}
252
253
SfxVersionDialog::~SfxVersionDialog()
254
0
{
255
0
}
256
257
void SfxVersionDialog::Open_Impl()
258
0
{
259
0
    SfxObjectShell *pObjShell = m_pViewFrame->GetObjectShell();
260
261
0
    auto nPos = m_xVersionBox->get_selected_index();
262
0
    SfxInt16Item aItem( SID_VERSION, nPos + 1);
263
0
    SfxStringItem aTarget( SID_TARGETNAME, u"_blank"_ustr );
264
0
    SfxStringItem aReferer( SID_REFERER, u"private:user"_ustr );
265
0
    SfxStringItem aFile( SID_FILE_NAME, pObjShell->GetMedium()->GetName() );
266
267
0
    uno::Sequence< beans::NamedValue > aEncryptionData;
268
0
    if ( GetEncryptionData_Impl( &pObjShell->GetMedium()->GetItemSet(), aEncryptionData ) )
269
0
    {
270
        // there is a password, it should be used during the opening
271
0
        SfxUnoAnyItem aEncryptionDataItem( SID_ENCRYPTIONDATA, uno::Any( aEncryptionData ) );
272
0
        m_pViewFrame->GetDispatcher()->ExecuteList(
273
0
            SID_OPENDOC, SfxCallMode::ASYNCHRON,
274
0
            { &aFile, &aItem, &aTarget, &aReferer, &aEncryptionDataItem });
275
0
    }
276
0
    else
277
0
    {
278
0
        m_pViewFrame->GetDispatcher()->ExecuteList(
279
0
            SID_OPENDOC, SfxCallMode::ASYNCHRON,
280
0
            { &aFile, &aItem, &aTarget, &aReferer });
281
0
    }
282
283
0
    m_xDialog->response(RET_OK);
284
0
}
285
286
IMPL_LINK_NOARG(SfxVersionDialog, DClickHdl_Impl, weld::TreeView&, bool)
287
0
{
288
0
    Open_Impl();
289
0
    return true;
290
0
}
291
292
IMPL_LINK_NOARG(SfxVersionDialog, SelectHdl_Impl, weld::TreeView&, void)
293
0
{
294
0
    bool bEnable = m_xVersionBox->get_selected_index() != -1;
295
0
    SfxObjectShell* pObjShell = m_pViewFrame->GetObjectShell();
296
0
    m_xDeleteButton->set_sensitive(bEnable && !pObjShell->IsReadOnly());
297
0
    m_xOpenButton->set_sensitive(bEnable);
298
0
    m_xViewButton->set_sensitive(bEnable);
299
300
0
    SfxPoolItemHolder aResult;
301
0
    m_pViewFrame->GetDispatcher()->QueryState(SID_DOCUMENT_MERGE, aResult);
302
0
    SfxItemState eState = m_pViewFrame->GetDispatcher()->QueryState(SID_DOCUMENT_COMPARE, aResult);
303
0
    m_xCompareButton->set_sensitive(bEnable && eState >= SfxItemState::DEFAULT);
304
0
}
305
306
IMPL_LINK(SfxVersionDialog, ButtonHdl_Impl, weld::Button&, rButton, void)
307
0
{
308
0
    SfxObjectShell *pObjShell = m_pViewFrame->GetObjectShell();
309
310
0
    int nEntry = m_xVersionBox->get_selected_index();
311
312
0
    if (&rButton == m_xSaveButton.get())
313
0
    {
314
0
        SfxVersionInfo aInfo;
315
0
        aInfo.aAuthor = SvtUserOptions().GetFullName();
316
0
        SfxViewVersionDialog_Impl aDlg(m_xDialog.get(), aInfo, true);
317
0
        short nRet = aDlg.run();
318
0
        if (nRet == RET_OK)
319
0
        {
320
0
            SfxStringItem aComment( SID_DOCINFO_COMMENTS, aInfo.aComment );
321
0
            pObjShell->SetModified();
322
0
            const SfxPoolItem* aItems[2];
323
0
            aItems[0] = &aComment;
324
0
            aItems[1] = nullptr;
325
0
            m_pViewFrame->GetBindings().ExecuteSynchron( SID_SAVEDOC, aItems );
326
0
            m_xVersionBox->freeze();
327
0
            m_xVersionBox->clear();
328
0
            m_xVersionBox->thaw();
329
0
            Init_Impl();
330
0
        }
331
0
    }
332
0
    else if (&rButton == m_xDeleteButton.get() && nEntry != -1)
333
0
    {
334
0
        SfxVersionInfo* pInfo = weld::fromId<SfxVersionInfo*>(m_xVersionBox->get_id(nEntry));
335
0
        pObjShell->GetMedium()->RemoveVersion_Impl(pInfo->aName);
336
0
        pObjShell->SetModified();
337
0
        m_xVersionBox->freeze();
338
0
        m_xVersionBox->clear();
339
0
        m_xVersionBox->thaw();
340
0
        Init_Impl();
341
0
    }
342
0
    else if (&rButton == m_xOpenButton.get() && nEntry != -1)
343
0
    {
344
0
        Open_Impl();
345
0
    }
346
0
    else if (&rButton == m_xViewButton.get() && nEntry != -1)
347
0
    {
348
0
        SfxVersionInfo* pInfo = weld::fromId<SfxVersionInfo*>(m_xVersionBox->get_id(nEntry));
349
0
        SfxViewVersionDialog_Impl aDlg(m_xDialog.get(), *pInfo, false);
350
0
        aDlg.run();
351
0
    }
352
0
    else if (&rButton == m_xCompareButton.get() && nEntry != -1)
353
0
    {
354
0
        SfxAllItemSet aSet( pObjShell->GetPool() );
355
0
        aSet.Put(SfxInt16Item(SID_VERSION, nEntry + 1));
356
0
        aSet.Put(SfxStringItem(SID_FILE_NAME, pObjShell->GetMedium()->GetName()));
357
358
0
        SfxItemSet& rSet = pObjShell->GetMedium()->GetItemSet();
359
0
        const SfxStringItem* pFilterItem = rSet.GetItem(SID_FILTER_NAME, false);
360
0
        const SfxStringItem* pFilterOptItem = rSet.GetItem(SID_FILE_FILTEROPTIONS, false);
361
0
        if ( pFilterItem )
362
0
            aSet.Put( *pFilterItem );
363
0
        if ( pFilterOptItem )
364
0
            aSet.Put( *pFilterOptItem );
365
366
0
        m_pViewFrame->GetDispatcher()->Execute( SID_DOCUMENT_COMPARE, SfxCallMode::ASYNCHRON, aSet );
367
0
        m_xDialog->response(RET_CLOSE);
368
0
    }
369
0
    else if (&rButton == m_xCmisButton.get())
370
0
    {
371
0
        SfxCmisVersionsDialog aDlg(m_xDialog.get(), m_pViewFrame);
372
0
        aDlg.run();
373
0
    }
374
0
}
375
376
IMPL_LINK(SfxVersionDialog, ToggleHdl_Impl, weld::Toggleable&, rButton, void)
377
0
{
378
0
    if (&rButton == m_xSaveCheckBox.get())
379
0
    {
380
0
        m_bIsSaveVersionOnClose = m_xSaveCheckBox->get_active();
381
0
    }
382
0
}
383
384
SfxViewVersionDialog_Impl::SfxViewVersionDialog_Impl(weld::Window *pParent, SfxVersionInfo& rInfo, bool bEdit)
385
0
    : SfxDialogController(pParent, u"sfx/ui/versioncommentdialog.ui"_ustr, u"VersionCommentDialog"_ustr)
386
0
    , m_rInfo(rInfo)
387
0
    , m_xDateTimeText(m_xBuilder->weld_label(u"timestamp"_ustr))
388
0
    , m_xSavedByText(m_xBuilder->weld_label(u"author"_ustr))
389
0
    , m_xEdit(m_xBuilder->weld_text_view(u"textview"_ustr))
390
0
    , m_xOKButton(m_xBuilder->weld_button(u"ok"_ustr))
391
0
    , m_xCancelButton(m_xBuilder->weld_button(u"cancel"_ustr))
392
0
    , m_xCloseButton(m_xBuilder->weld_button(u"close"_ustr))
393
0
{
394
0
    OUString sAuthor = rInfo.aAuthor.isEmpty() ? SfxResId(STR_NO_NAME_SET) : rInfo.aAuthor;
395
396
0
    const LocaleDataWrapper& rLocaleWrapper( Application::GetSettings().GetLocaleDataWrapper() );
397
0
    m_xDateTimeText->set_label(m_xDateTimeText->get_label() + formatDateTime(rInfo.aCreationDate, rLocaleWrapper, false));
398
0
    m_xSavedByText->set_label(m_xSavedByText->get_label() + sAuthor);
399
0
    m_xEdit->set_text(rInfo.aComment);
400
0
    m_xEdit->set_size_request(40 * m_xEdit->get_approximate_digit_width(),
401
0
                              7 * m_xEdit->get_text_height());
402
0
    m_xOKButton->connect_clicked(LINK(this, SfxViewVersionDialog_Impl, ButtonHdl));
403
404
0
    if (!bEdit)
405
0
    {
406
0
        m_xOKButton->hide();
407
0
        m_xCancelButton->hide();
408
0
        m_xEdit->set_editable(false);
409
0
        m_xDialog->set_title(SfxResId(STR_VIEWVERSIONCOMMENT));
410
0
        m_xCloseButton->grab_focus();
411
0
    }
412
0
    else
413
0
    {
414
0
        m_xDateTimeText->hide();
415
0
        m_xCloseButton->hide();
416
0
        m_xEdit->grab_focus();
417
0
    }
418
0
}
419
420
IMPL_LINK(SfxViewVersionDialog_Impl, ButtonHdl, weld::Button&, rButton, void)
421
0
{
422
0
    assert(&rButton == m_xOKButton.get());
423
0
    (void)rButton;
424
0
    m_rInfo.aComment = m_xEdit->get_text();
425
0
    m_xDialog->response(RET_OK);
426
0
}
427
428
SfxCmisVersionsDialog::SfxCmisVersionsDialog(weld::Window* pParent, SfxViewFrame* pVwFrame)
429
0
    : SfxDialogController(pParent, u"sfx/ui/versionscmis.ui"_ustr, u"VersionsCmisDialog"_ustr)
430
0
    , m_pViewFrame(pVwFrame)
431
0
    , m_xVersionBox(m_xBuilder->weld_tree_view(u"versions"_ustr))
432
0
{
433
0
    m_xVersionBox->set_size_request(m_xVersionBox->get_approximate_digit_width() * 90,
434
0
                                    m_xVersionBox->get_height_rows(15));
435
0
    setColSizes(*m_xVersionBox);
436
437
0
    m_xVersionBox->grab_focus();
438
439
0
    OUString sText = m_xDialog->get_title() +
440
0
                    " " + m_pViewFrame->GetObjectShell()->GetTitle();
441
0
    m_xDialog->set_title(sText);
442
443
0
    LoadVersions();
444
0
}
445
446
SfxCmisVersionsDialog::~SfxCmisVersionsDialog()
447
0
{
448
0
}
449
450
void SfxCmisVersionsDialog::LoadVersions()
451
0
{
452
0
    SfxObjectShell *pObjShell = m_pViewFrame->GetObjectShell();
453
0
    uno::Sequence < document::CmisVersion > aVersions = pObjShell->GetCmisVersions( );
454
0
    m_pTable.reset(new SfxVersionTableDtor( aVersions ));
455
0
    for (size_t n = 0; n < m_pTable->size(); ++n)
456
0
    {
457
0
        SfxVersionInfo *pInfo = m_pTable->at( n );
458
0
        OUString aEntry = formatDateTime(pInfo->aCreationDate, Application::GetSettings().GetLocaleDataWrapper(), false);
459
0
        m_xVersionBox->append(weld::toId(pInfo), aEntry);
460
0
        auto nLastRow = m_xVersionBox->n_children() - 1;
461
0
        m_xVersionBox->set_text(nLastRow, pInfo->aAuthor, 1);
462
0
        m_xVersionBox->set_text(nLastRow, ConvertWhiteSpaces_Impl(pInfo->aComment), 2);
463
0
    }
464
465
0
    if (auto nCount = m_pTable->size())
466
0
        m_xVersionBox->select(nCount - 1);
467
0
}
468
469
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */