summaryrefslogtreecommitdiff
path: root/desktop/inc/lib/init.hxx
blob: fdcb54fd264b144a4f45fc9d6828e4484ff6fed9 (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
/* -*- 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)
        {
            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, "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");

            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)
        {
            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() && std::get<0>(m_queue.back()) == type && std::get<1>(m_queue.back()) == payload)
            {
                // Supress duplicate invalidation only when they are in sequence.
                return;
            }

            m_queue.emplace_back(type, payload);

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

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

                m_queue.clear();
            }
        }

    private:
        std::vector<std::tuple<int, std::string>> m_queue;
        std::map<int, std::string> m_states;
        LibreOfficeKitCallback m_pCallback;
        void *m_pData;
        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;
        }
    };
}