Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/framework/undomanagerhelper.hxx
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
#pragma once
21
22
#include <config_options.h>
23
#include <framework/fwkdllapi.h>
24
#include <rtl/ustring.hxx>
25
26
#include <memory>
27
28
namespace com::sun::star::document { class XUndoAction; }
29
namespace com::sun::star::document { class XUndoManager; }
30
namespace com::sun::star::document { class XUndoManagerListener; }
31
namespace com::sun::star::util { class XModifyListener; }
32
namespace com::sun::star::uno { template <class interface_type> class Reference; }
33
namespace com::sun::star::uno { template <typename> class Sequence; }
34
namespace framework { class IMutex; }
35
36
class SfxUndoManager;
37
38
39
namespace framework
40
{
41
42
43
    //= IMutexGuard
44
45
    class SAL_NO_VTABLE IMutexGuard
46
    {
47
    public:
48
        /** clears the lock. If the guard does not currently hold the lock, nothing happens.
49
        */
50
        virtual void clear() = 0;
51
52
        /** returns the mutex guarded by the instance.
53
54
            Even if the guard currently has not a lock on the mutex, this method must succeed.
55
        */
56
        virtual IMutex& getGuardedMutex() = 0;
57
58
    protected:
59
7.73k
        ~IMutexGuard() {}
60
    };
61
62
63
    //= IUndoManagerImplementation
64
65
    class SAL_NO_VTABLE IUndoManagerImplementation
66
    {
67
    public:
68
        /** returns the SfxUndoManager interface to the actual Undo stack
69
70
            @throws css::lang::DisposedException
71
                when the instance is already disposed, and no SfxUndoManager can be provided
72
73
            @throws css::lang::NotInitializedException
74
                when the instance is not initialized, yet, and no SfxUndoManager can be provided
75
        */
76
        virtual SfxUndoManager&        getImplUndoManager() = 0;
77
78
        /** provides access to a UNO interface for the XUndoManager implementation. Used when throwing exceptions.
79
        */
80
        virtual css::uno::Reference< css::document::XUndoManager >
81
                                        getThis() = 0;
82
83
    protected:
84
2.71k
        ~IUndoManagerImplementation() {}
85
    };
86
87
88
    //= UndoManagerHelper
89
90
    class UndoManagerHelper_Impl;
91
    /** helper class for implementing an XUndoManager
92
93
        Several of the methods of the class take an IMutexGuard instance. It is assumed that this guard has a lock on
94
        its mutex at the moment the method is entered. The lock will be released before any notifications to the
95
        registered XUndoManagerListeners happen.
96
97
        The following locking strategy is used for this mutex:
98
        <ul><li>Any notifications to the registered XUndoManagerListeners are after the guard has been cleared. i.e.
99
                without the mutex being locked.</p>
100
            <li>Any calls into the <code>SfxUndoManager</code> implementation is made without the mutex being locked.
101
                Note that this implies that the <code>SfxUndoManager</code> implementation must be thread-safe in itself
102
                (which is true for the default implementation, SfxUndoManager).</li>
103
            <li>An exception to the previous item are the <member>SfxUndoManager::Undo</member> and
104
                <member>SfxUndoManager::Redo</member> methods: They're called with the given external mutex being
105
                locked.</li>
106
        </ul>
107
108
        The reason for the exception for SfxUndoManager::Undo and SfxUndoManager::Redo is that those are expected to
109
        modify the actual document which the UndoManager works for. And as long as our documents are not thread-safe,
110
        and as long as we do not re-fit <strong>all</strong> existing SfxUndoImplementations to <em>not</em> expect
111
        the dreaded SolarMutex being locked when they're called, the above behavior is a compromise between "how it should
112
        be" and "how it can realistically be".
113
    */
114
    class UNLESS_MERGELIBS_MORE(FWK_DLLPUBLIC) UndoManagerHelper
115
    {
116
    public:
117
        UndoManagerHelper( IUndoManagerImplementation& i_undoManagerImpl );
118
        ~UndoManagerHelper();
119
120
        // life time control
121
        void disposing();
122
123
        // XUndoManager equivalents
124
        void            enterUndoContext( const OUString& i_title, IMutexGuard& i_instanceLock );
125
        void            enterHiddenUndoContext( IMutexGuard& i_instanceLock );
126
        void            leaveUndoContext( IMutexGuard& i_instanceLock );
127
        void            addUndoAction( const css::uno::Reference< css::document::XUndoAction >& i_action, IMutexGuard& i_instanceLock );
128
        void            undo( IMutexGuard& i_instanceLock );
129
        void            redo( IMutexGuard& i_instanceLock );
130
        bool            isUndoPossible() const;
131
        bool            isRedoPossible() const;
132
        OUString getCurrentUndoActionTitle() const;
133
        OUString getCurrentRedoActionTitle() const;
134
        css::uno::Sequence< OUString >
135
                        getAllUndoActionTitles() const;
136
        css::uno::Sequence< OUString >
137
                        getAllRedoActionTitles() const;
138
        void            clear( IMutexGuard& i_instanceLock );
139
        void            clearRedo( IMutexGuard& i_instanceLock );
140
        void            reset( IMutexGuard& i_instanceLock );
141
        void            addUndoManagerListener( const css::uno::Reference< css::document::XUndoManagerListener >& i_listener );
142
        void            removeUndoManagerListener( const css::uno::Reference< css::document::XUndoManagerListener >& i_listener );
143
144
        // XLockable, base of XUndoManager, equivalents
145
        void            lock();
146
        void            unlock();
147
        bool            isLocked();
148
149
        // XModifyBroadcaster equivalents
150
        void            addModifyListener( const css::uno::Reference< css::util::XModifyListener >& i_listener );
151
        void            removeModifyListener( const css::uno::Reference< css::util::XModifyListener >& i_listener );
152
153
    private:
154
        std::unique_ptr< UndoManagerHelper_Impl >   m_xImpl;
155
    };
156
157
158
} // namespace framework
159
160
161
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */