LibreOffice Module desktop (master) 1
init.hxx
Go to the documentation of this file.
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
10#pragma once
11
12#include <map>
13#include <unordered_map>
14#include <memory>
15#include <mutex>
16#include <set>
17#include <string_view>
18
19#include <boost/property_tree/ptree.hpp>
20#include <boost/variant.hpp>
21#include <boost/container/flat_map.hpp>
22
23#include <osl/thread.h>
24#include <rtl/ref.hxx>
25#include <rtl/strbuf.hxx>
26#include <vcl/idle.hxx>
27#include <LibreOfficeKit/LibreOfficeKit.h>
28#include <LibreOfficeKit/LibreOfficeKitEnums.h>
29#include <com/sun/star/beans/PropertyValue.hpp>
30#include <com/sun/star/lang/XComponent.hpp>
31#include <tools/gen.hxx>
32#include <sfx2/lokcallback.hxx>
33#include <sfx2/lokhelper.hxx>
34
35#include <desktop/dllapi.h>
36
38
39namespace desktop {
40
43 {
47
48 // This is the "EMPTY" rectangle, which somewhat confusingly actually means
49 // to drop all rectangles (see LOK_CALLBACK_INVALIDATE_TILES documentation),
50 // and so it is actually an infinite rectangle and not an empty one.
52
54 : m_nPart(INT_MIN) // -1 is reserved to mean "all parts".
55 , m_nMode(0)
56 {
57 }
58
59 RectangleAndPart(const tools::Rectangle* pRect, int nPart, int nMode)
61 , m_nPart(nPart)
62 , m_nMode(nMode)
63 {
64 }
65
66 OString toString() const
67 {
68 if (m_nPart >= -1)
69 return (isInfinite() ? "EMPTY" : m_aRectangle.toString())
70 + ", " + OString::number(m_nPart) + ", " + OString::number(m_nMode);
71 else
72 return (isInfinite() ? "EMPTY" : m_aRectangle.toString());
73 }
74
77 bool isInfinite() const
78 {
81 }
82
84 bool isEmpty() const
85 {
86 return m_aRectangle.IsEmpty();
87 }
88
89 static RectangleAndPart Create(const OString& rPayload);
93 };
94
97 {
98 public:
99 explicit CallbackFlushHandler(LibreOfficeKitDocument* pDocument, LibreOfficeKitCallback pCallback, void* pData);
100 virtual ~CallbackFlushHandler() override;
101 virtual void Invoke() override;
102 // TODO This should be dropped and the binary libreOfficeKitViewCallback() variants should be called?
103 void queue(const int type, const OString& data);
104
107 void disableCallbacks() { ++m_nDisableCallbacks; }
110 void enableCallbacks() { --m_nDisableCallbacks; }
112 bool callbacksDisabled() const { return m_nDisableCallbacks != 0; }
113
114 void addViewStates(int viewId);
115 void removeViewStates(int viewId);
116
117 void setViewId( int viewId ) { m_viewId = viewId; }
118
119 // SfxLockCallbackInterface
120 virtual void libreOfficeKitViewCallback(int nType, const OString& pPayload) override;
121 virtual void libreOfficeKitViewCallbackWithViewId(int nType, const OString& pPayload, int nViewId) override;
122 virtual void libreOfficeKitViewInvalidateTilesCallback(const tools::Rectangle* pRect, int nPart, int nMode) override;
123 virtual void libreOfficeKitViewUpdatedCallback(int nType) override;
124 virtual void libreOfficeKitViewUpdatedCallbackPerViewId(int nType, int nViewId, int nSourceViewId) override;
125 virtual void libreOfficeKitViewAddPendingInvalidateTiles() override;
126 virtual void dumpState(rtl::OStringBuffer &rState) override;
127
128 private:
130 {
131 CallbackData(OString payload)
132 : PayloadString(payload)
133 {
134 }
135
136 CallbackData(OString payload, int viewId)
137 : PayloadString(payload)
138 , PayloadObject(viewId)
139 {
140 }
141
142 CallbackData(const tools::Rectangle* pRect, int viewId)
143 : PayloadObject(RectangleAndPart(pRect, viewId, 0))
144 { // PayloadString will be done on demand
145 }
146
147 CallbackData(const tools::Rectangle* pRect, int part, int mode)
148 : PayloadObject(RectangleAndPart(pRect, part, mode))
149 { // PayloadString will be done on demand
150 }
151
152 const OString& getPayload() const;
154 void updateRectangleAndPart(const RectangleAndPart& rRectAndPart);
156 const RectangleAndPart& getRectangleAndPart() const;
158 boost::property_tree::ptree& setJson(const std::string& payload);
160 void setJson(const boost::property_tree::ptree& rTree);
162 const boost::property_tree::ptree& getJson() const;
163
164 int getViewId() const;
165
166 bool isEmpty() const
167 {
168 return PayloadString.isEmpty() && PayloadObject.which() == 0;
169 }
170 void clear()
171 {
172 PayloadString.clear();
173 PayloadObject = boost::blank();
174 }
175
177 bool validate() const;
178
180 bool isCached() const { return PayloadObject.which() != 0; }
181
182 private:
183 mutable OString PayloadString;
184
186 mutable boost::variant<boost::blank, RectangleAndPart, boost::property_tree::ptree, int> PayloadObject;
187 };
188
189 typedef std::vector<int> queue_type1;
190 typedef std::vector<CallbackData> queue_type2;
191
192 void startTimer();
193 bool removeAll(int type);
194 bool removeAll(int type, const std::function<bool (const CallbackData&)>& rTestFunc);
195 bool processInvalidateTilesEvent(int type, CallbackData& aCallbackData);
196 bool processWindowEvent(int type, CallbackData& aCallbackData);
197 queue_type2::iterator toQueue2(queue_type1::iterator);
198 queue_type2::reverse_iterator toQueue2(queue_type1::reverse_iterator);
199 void queue(const int type, CallbackData& data);
200 void enqueueUpdatedTypes();
201 void enqueueUpdatedType( int type, const SfxViewShell* sourceViewShell, int viewId );
202
207 std::map<int, OString> m_states;
208 std::unordered_map<OString, OString> m_lastStateChange;
209 std::unordered_map<int, std::unordered_map<int, OString>> m_viewStates;
210
211 // For some types only the last message matters (see isUpdatedType()) or only the last message
212 // per each viewId value matters (see isUpdatedTypePerViewId()), so instead of using push model
213 // where we'd get flooded by repeated messages (which might be costly to generate and process),
214 // the preferred way is that libreOfficeKitViewUpdatedCallback()
215 // or libreOfficeKitViewUpdatedCallbackPerViewId() get called to notify about such a message being
216 // needed, and we'll set a flag here to fetch the actual message before flushing.
217 void setUpdatedType( int nType, bool value );
218 void setUpdatedTypePerViewId( int nType, int nViewId, int nSourceViewId, bool value );
219 void resetUpdatedType( int nType);
220 void resetUpdatedTypePerViewId( int nType, int nViewId );
221 std::vector<bool> m_updatedTypes; // index is type, value is if set
223 {
224 bool set = false; // value is if set
226 };
227 // Flat_map is used in preference to unordered_map because the map is accessed very often.
228 boost::container::flat_map<int, std::vector<PerViewIdData>> m_updatedTypesPerViewId; // key is view, index is type
229
230 LibreOfficeKitDocument* m_pDocument;
231 int m_viewId = -1; // view id of the associated SfxViewShell
232 LibreOfficeKitCallback m_pCallback;
233 void *m_pData;
235 std::recursive_mutex m_mutex;
236 class TimeoutIdle : public Timer
237 {
238 public:
240 virtual void Invoke() override;
241 private:
243 };
245 };
246
247 struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument
248 {
249 css::uno::Reference<css::lang::XComponent> mxComponent;
250 std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass;
251 std::map<size_t, std::shared_ptr<CallbackFlushHandler>> mpCallbackFlushHandlers;
252 const int mnDocumentId;
253 std::set<OUString> maFontsMissing;
254
255 explicit LibLODocument_Impl(css::uno::Reference<css::lang::XComponent> xComponent,
256 int nDocumentId);
258 };
259
260 struct DESKTOP_DLLPUBLIC LibLibreOffice_Impl : public _LibreOfficeKit
261 {
263 std::shared_ptr< LibreOfficeKitClass > m_pOfficeClass;
264 oslThread maThread;
265 LibreOfficeKitCallback mpCallback;
268 std::map<OString, rtl::Reference<LOKInteractionHandler>> mInteractionMap;
269
272
273 bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
274 {
275 return (mOptionalFeatures & feature) != 0;
276 }
277
278 void dumpState(rtl::OStringBuffer &aState);
279 };
280
284 DESKTOP_DLLPUBLIC OUString extractParameter(OUString& aOptions, std::u16string_view rName);
285
288 DESKTOP_DLLPUBLIC std::vector<com::sun::star::beans::PropertyValue> jsonToPropertyValuesVector(const char* pJSON);
289}
290
291/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
InteractionHandler is an interface that provides the user with various dialogs / error messages.
static const tools::Long MaxTwips
CallbackFlushHandler * mHandler
Definition: init.hxx:242
One instance of this per view, handles flushing callbacks.
Definition: init.hxx:97
LibreOfficeKitDocument * m_pDocument
Definition: init.hxx:230
queue_type2::reverse_iterator toQueue2(queue_type1::reverse_iterator)
std::vector< int > queue_type1
Definition: init.hxx:189
std::unordered_map< OString, OString > m_lastStateChange
Definition: init.hxx:208
std::unordered_map< int, std::unordered_map< int, OString > > m_viewStates
Definition: init.hxx:209
std::map< int, OString > m_states
Definition: init.hxx:207
std::vector< bool > m_updatedTypes
Definition: init.hxx:221
bool callbacksDisabled() const
Returns true iff callbacks are disabled.
Definition: init.hxx:112
queue_type2::iterator toQueue2(queue_type1::iterator)
std::vector< CallbackData > queue_type2
Definition: init.hxx:190
boost::container::flat_map< int, std::vector< PerViewIdData > > m_updatedTypesPerViewId
Definition: init.hxx:228
void disableCallbacks()
Disables callbacks on this handler.
Definition: init.hxx:107
LibreOfficeKitCallback m_pCallback
Definition: init.hxx:232
std::recursive_mutex m_mutex
Definition: init.hxx:235
queue_type1 m_queue1
we frequently want to scan the queue, and mostly when we do so, we only care about the element type s...
Definition: init.hxx:205
void setViewId(int viewId)
Definition: init.hxx:117
void enableCallbacks()
Enables callbacks on this handler.
Definition: init.hxx:110
constexpr tools::Long GetWidth() const
constexpr tools::Long GetHeight() const
constexpr bool IsEmpty() const
rtl::OString toString() const
#define DESKTOP_DLLPUBLIC
Definition: dllapi.h:19
std::unique_ptr< sal_Int32[]> pData
void set(css::uno::UnoInterfaceReference const &value)
Definition: app.cxx:167
DESKTOP_DLLPUBLIC OUString extractParameter(OUString &aOptions, std::u16string_view rName)
Helper function to extract the value from parameters delimited by comma, like: Name1=Value1,...
Definition: init.cxx:1046
DESKTOP_DLLPUBLIC std::vector< com::sun::star::beans::PropertyValue > jsonToPropertyValuesVector(const char *pJSON)
Helper function to convert JSON to a vector of PropertyValues.
Definition: init.cxx:403
long Long
void dumpState(rtl::OStringBuffer &rState)
ConversionMode mode
CallbackData(OString payload, int viewId)
Definition: init.hxx:136
CallbackData(const tools::Rectangle *pRect, int viewId)
Definition: init.hxx:142
bool isCached() const
Returns true iff there is cached data.
Definition: init.hxx:180
CallbackData(const tools::Rectangle *pRect, int part, int mode)
Definition: init.hxx:147
boost::variant< boost::blank, RectangleAndPart, boost::property_tree::ptree, int > PayloadObject
The parsed payload cache. Update validate() when changing this.
Definition: init.hxx:186
std::map< size_t, std::shared_ptr< CallbackFlushHandler > > mpCallbackFlushHandlers
Definition: init.hxx:251
std::set< OUString > maFontsMissing
Definition: init.hxx:253
std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass
Definition: init.hxx:250
css::uno::Reference< css::lang::XComponent > mxComponent
Definition: init.hxx:249
std::map< OString, rtl::Reference< LOKInteractionHandler > > mInteractionMap
Definition: init.hxx:268
LibreOfficeKitCallback mpCallback
Definition: init.hxx:265
std::shared_ptr< LibreOfficeKitClass > m_pOfficeClass
Definition: init.hxx:263
bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
Definition: init.hxx:273
Represents an invalidated rectangle inside a given document part.
Definition: init.hxx:43
tools::Rectangle m_aRectangle
Definition: init.hxx:44
static constexpr tools::Rectangle emptyAllRectangle
Definition: init.hxx:51
static tools::Rectangle SanitizedRectangle(tools::Long nLeft, tools::Long nTop, tools::Long nWidth, tools::Long nHeight)
Makes sure a rectangle is valid (apparently some code does not like negative coordinates for example)...
Definition: init.cxx:587
RectangleAndPart(const tools::Rectangle *pRect, int nPart, int nMode)
Definition: init.hxx:59
static RectangleAndPart Create(const OString &rPayload)
Definition: init.cxx:516
OString toString() const
Definition: init.hxx:66
bool isInfinite() const
Infinite Rectangle is both sides are equal or longer than SfxLokHelper::MaxTwips.
Definition: init.hxx:77
bool isEmpty() const
Empty Rectangle is when it has zero dimensions.
Definition: init.hxx:84
ResultType type