summaryrefslogtreecommitdiff
path: root/desktop/inc/lib/init.hxx
blob: ae7a6707bb462e1ad818996b2fe82cd0d3d2512f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <map>
#include <mutex>

#include <boost/shared_ptr.hpp>

#include <osl/thread.h>
#include <vcl/idle.hxx>
#include <LibreOfficeKit/LibreOfficeKit.h>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include "../../source/inc/desktopdllapi.h"

using namespace css;
using namespace boost;

class LOKInteractionHandler;

namespace desktop {

    class CallbackFlushHandler : public Idle
    {
    public:
        explicit CallbackFlushHandler(LibreOfficeKitCallback pCallback, void* pData)
            : Idle( "lokit timer callback" ),
              m_pCallback(pCallback),
              m_pData(pData),
              m_bPartTilePainting(false)
        {
            SetPriority(SchedulerPriority::POST_PAINT);

            // Add the states that are safe to skip duplicates on,
            // even when not consequent.
            m_states.emplace(LOK_CALLBACK_TEXT_SELECTION_START, "NIL");
            m_states.emplace(LOK_CALLBACK_TEXT_SELECTION_END, "NIL");
            m_states.emplace(LOK_CALLBACK_TEXT_SELECTION, "NIL");
            m_states.emplace(LOK_CALLBACK_GRAPHIC_SELECTION, "NIL");
            m_states.emplace(LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR, "NIL");
            m_states.emplace(LOK_CALLBACK_STATE_CHANGED, "NIL");
            m_states.emplace(LOK_CALLBACK_MOUSE_POINTER, "NIL");
            m_states.emplace(LOK_CALLBACK_CELL_CURSOR, "NIL");
            m_states.emplace(LOK_CALLBACK_CELL_FORMULA, "NIL");
            m_states.emplace(LOK_CALLBACK_CURSOR_VISIBLE, "NIL");
            m_states.emplace(LOK_CALLBACK_SET_PART, "NIL");

            Start();
        }

        virtual ~CallbackFlushHandler()
        {
            Stop();

            // We might have important notification (.uno:save?).
            flush();
        }

        virtual void Invoke() override
        {
            flush();
        }

        static
        void callback(const int type, const char* payload, void* data)
        {
            CallbackFlushHandler* self = reinterpret_cast<CallbackFlushHandler*>(data);
            if (self)
            {
                self->queue(type, payload);
            }
        }

        void queue(const int type, const char* data)
        {
            if (m_bPartTilePainting)
            {
                // We drop notifications when this is set.
                return;
            }

            const std::string payload(data ? data : "(nil)");
            std::unique_lock<std::mutex> lock(m_mutex);

            const auto stateIt = m_states.find(type);
            if (stateIt != m_states.end())
            {
                // If the state didn't change, it's safe to ignore.
                if (stateIt->second == payload)
                {
                    return;
                }

                stateIt->second = payload;
            }

            if (type == LOK_CALLBACK_INVALIDATE_TILES &&
                !m_queue.empty() && m_queue.back().first == type && m_queue.back().second == payload)
            {
                // Supress duplicate invalidation only when they are in sequence.
                return;
            }

            if (type == LOK_CALLBACK_TEXT_SELECTION && payload.empty())
            {
                // Removing text selection invalidates the start and end as well.
                m_states[LOK_CALLBACK_TEXT_SELECTION_START] = "";
                m_states[LOK_CALLBACK_TEXT_SELECTION_END] = "";
            }

            m_queue.emplace_back(type, payload);

            // These are safe to use the latest state and ignore previous
            // ones (if any) since the last overrides previous ones.
            switch (type)
            {
                case LOK_CALLBACK_TEXT_SELECTION_START:
                case LOK_CALLBACK_TEXT_SELECTION_END:
                case LOK_CALLBACK_TEXT_SELECTION:
                case LOK_CALLBACK_GRAPHIC_SELECTION:
                case LOK_CALLBACK_MOUSE_POINTER:
                case LOK_CALLBACK_CELL_CURSOR:
                case LOK_CALLBACK_CELL_FORMULA:
                case LOK_CALLBACK_CURSOR_VISIBLE:
                case LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE:
                    removeAllButLast(type, false);
                break;

                // These come with rects, so drop earlier
                // only when the latter includes former ones.
                case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
                case LOK_CALLBACK_INVALIDATE_TILES:
                    if (payload.empty())
                    {
                        // Invalidating everything means previously
                        // invalidated tiles can be dropped.
                        removeAllButLast(type, false);
                    }
                    else
                    {
                        removeAllButLast(type, true);
                    }

                break;
            }

            lock.unlock();
            if (!IsActive())
            {
                Start();
            }
        }

        void setPartTilePainting(const bool bPartPainting) { m_bPartTilePainting = bPartPainting; }
        bool isPartTilePainting() const { return m_bPartTilePainting; }

    private:
        void flush()
        {
            if (m_pCallback)
            {
                std::unique_lock<std::mutex> lock(m_mutex);
                for (auto& pair : m_queue)
                {
                    m_pCallback(pair.first, pair.second.c_str(), m_pData);
                }

                m_queue.clear();
            }
        }

        void removeAllButLast(const int type, const bool identical)
        {
            int i = m_queue.size() - 1;
            std::string payload;
            for (; i >= 0; --i)
            {
                if (m_queue[i].first == type)
                {
                    payload = m_queue[i].second;
                    //SAL_WARN("idle", "Found [" + std::to_string(type) + "] at " + std::to_string(i) + ": [" + payload + "].");
                    break;
                }
            }

            for (--i; i >= 0; --i)
            {
                if (m_queue[i].first == type &&
                    (!identical || m_queue[i].second == payload))
                {
                    //SAL_WARN("idle", "Removing [" + std::to_string(type) + "] at " + std::to_string(i) + ": " + m_queue[i].second + "].");
                    m_queue.erase(m_queue.begin() + i);
                }
            }
        }

    private:
        std::vector<std::pair<int, std::string>> m_queue;
        std::map<int, std::string> m_states;
        LibreOfficeKitCallback m_pCallback;
        void *m_pData;
        bool m_bPartTilePainting;
        std::mutex m_mutex;
    };

    struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument
    {
        uno::Reference<css::lang::XComponent> mxComponent;
        shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass;
        shared_ptr<CallbackFlushHandler> mpCallbackFlushHandler;

        explicit LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent);
        ~LibLODocument_Impl();
    };

    struct DESKTOP_DLLPUBLIC LibLibreOffice_Impl : public _LibreOfficeKit
    {
        OUString maLastExceptionMsg;
        boost::shared_ptr< LibreOfficeKitClass > m_pOfficeClass;
        oslThread maThread;
        LibreOfficeKitCallback mpCallback;
        void *mpCallbackData;
        int64_t mOptionalFeatures;
        std::map<OString, rtl::Reference<LOKInteractionHandler>> mInteractionMap;

        LibLibreOffice_Impl();
        ~LibLibreOffice_Impl();

        bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
        {
            return (mOptionalFeatures & feature) != 0;
        }
    };
}