1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
13 #include <unordered_map>
16 #include <string_view>
18 #include <boost/property_tree/ptree.hpp>
19 #include <boost/variant.hpp>
21 #include <osl/thread.h>
22 #include <rtl/ref.hxx>
23 #include <vcl/idle.hxx>
24 #include <LibreOfficeKit/LibreOfficeKit.h>
25 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <tools/gen.hxx>
29 #include <sfx2/lokcallback.hxx>
30 #include <sfx2/lokhelper.hxx>
32 #include <desktop/dllapi.h>
34 class LOKInteractionHandler
;
38 /// Represents an invalidated rectangle inside a given document part.
39 struct RectangleAndPart
41 tools::Rectangle m_aRectangle
;
45 : m_nPart(INT_MIN
) // -1 is reserved to mean "all parts".
49 RectangleAndPart(const tools::Rectangle
* pRect
, int nPart
)
50 : m_aRectangle( pRect
? *pRect
: tools::Rectangle(0, 0, SfxLokHelper::MaxTwips
, SfxLokHelper::MaxTwips
))
55 OString
toString() const
58 return m_aRectangle
.toString() + ", " + OString::number(m_nPart
);
60 return m_aRectangle
.toString();
63 /// Infinite Rectangle is both sides are
64 /// equal or longer than SfxLokHelper::MaxTwips.
65 bool isInfinite() const
67 return m_aRectangle
.GetWidth() >= SfxLokHelper::MaxTwips
&&
68 m_aRectangle
.GetHeight() >= SfxLokHelper::MaxTwips
;
71 /// Empty Rectangle is when it has zero dimensions.
74 return m_aRectangle
.IsEmpty();
77 static RectangleAndPart
Create(const std::string
& rPayload
);
80 class DESKTOP_DLLPUBLIC CallbackFlushHandler final
: public Idle
, public SfxLokCallbackInterface
83 explicit CallbackFlushHandler(LibreOfficeKitDocument
* pDocument
, LibreOfficeKitCallback pCallback
, void* pData
);
84 virtual ~CallbackFlushHandler() override
;
85 virtual void Invoke() override
;
86 // TODO This should be dropped and the binary libreOfficeKitViewCallback() variants should be called?
87 void queue(const int type
, const char* data
);
89 /// Disables callbacks on this handler. Must match with identical count
90 /// of enableCallbacks. Used during painting and changing views.
91 void disableCallbacks() { ++m_nDisableCallbacks
; }
92 /// Enables callbacks on this handler. Must match with identical count
93 /// of disableCallbacks. Used during painting and changing views.
94 void enableCallbacks() { --m_nDisableCallbacks
; }
95 /// Returns true iff callbacks are disabled.
96 bool callbacksDisabled() const { return m_nDisableCallbacks
!= 0; }
98 void addViewStates(int viewId
);
99 void removeViewStates(int viewId
);
101 void setViewId( int viewId
) { m_viewId
= viewId
; }
103 // SfxLockCallbackInterface
104 virtual void libreOfficeKitViewCallback(int nType
, const char* pPayload
) override
;
105 virtual void libreOfficeKitViewCallback(int nType
, const char* pPayload
, int nViewId
) override
;
106 virtual void libreOfficeKitViewInvalidateTilesCallback(const tools::Rectangle
* pRect
, int nPart
) override
;
111 CallbackData(const char* payload
)
112 : PayloadString(payload
? payload
: "(nil)")
116 CallbackData(const char* payload
, int viewId
)
117 : PayloadString(payload
? payload
: "(nil)")
118 , PayloadObject(viewId
)
122 CallbackData(const tools::Rectangle
* pRect
, int viewId
)
123 : PayloadObject(RectangleAndPart(pRect
, viewId
))
124 { // PayloadString will be done on demand
127 const std::string
& getPayload() const;
128 /// Update a RectangleAndPart object and update PayloadString if necessary.
129 void updateRectangleAndPart(const RectangleAndPart
& rRectAndPart
);
130 /// Return the parsed RectangleAndPart instance.
131 const RectangleAndPart
& getRectangleAndPart() const;
132 /// Parse and set the JSON object and return it. Clobbers PayloadString.
133 boost::property_tree::ptree
& setJson(const std::string
& payload
);
134 /// Set a Json object and update PayloadString.
135 void setJson(const boost::property_tree::ptree
& rTree
);
136 /// Return the parsed JSON instance.
137 const boost::property_tree::ptree
& getJson() const;
139 int getViewId() const;
143 return PayloadString
.empty() && PayloadObject
.which() == 0;
147 PayloadString
.clear();
148 PayloadObject
= boost::blank();
151 /// Validate that the payload and parsed object match.
152 bool validate() const;
154 /// Returns true iff there is cached data.
155 bool isCached() const { return PayloadObject
.which() != 0; }
158 mutable std::string PayloadString
;
160 /// The parsed payload cache. Update validate() when changing this.
161 mutable boost::variant
<boost::blank
, RectangleAndPart
, boost::property_tree::ptree
, int> PayloadObject
;
164 typedef std::vector
<int> queue_type1
;
165 typedef std::vector
<CallbackData
> queue_type2
;
167 bool removeAll(int type
);
168 bool removeAll(int type
, const std::function
<bool (const CallbackData
&)>& rTestFunc
);
169 bool processInvalidateTilesEvent(int type
, CallbackData
& aCallbackData
);
170 bool processWindowEvent(int type
, CallbackData
& aCallbackData
);
171 queue_type2::iterator
toQueue2(queue_type1::iterator
);
172 queue_type2::reverse_iterator
toQueue2(queue_type1::reverse_iterator
);
173 void queue(const int type
, CallbackData
& data
);
175 /** we frequently want to scan the queue, and mostly when we do so, we only care about the element type
176 so we split the queue in 2 to make the scanning cache friendly. */
177 queue_type1 m_queue1
;
178 queue_type2 m_queue2
;
179 std::map
<int, std::string
> m_states
;
180 std::unordered_map
<int, std::unordered_map
<int, std::string
>> m_viewStates
;
181 LibreOfficeKitDocument
* m_pDocument
;
182 int m_viewId
= -1; // view id of the associated SfxViewShell
183 LibreOfficeKitCallback m_pCallback
;
185 int m_nDisableCallbacks
;
187 class TimeoutIdle
: public Timer
190 TimeoutIdle( CallbackFlushHandler
* handler
);
191 virtual void Invoke() override
;
193 CallbackFlushHandler
* mHandler
;
195 TimeoutIdle m_TimeoutIdle
;
198 struct DESKTOP_DLLPUBLIC LibLODocument_Impl
: public _LibreOfficeKitDocument
200 css::uno::Reference
<css::lang::XComponent
> mxComponent
;
201 std::shared_ptr
< LibreOfficeKitDocumentClass
> m_pDocumentClass
;
202 std::map
<size_t, std::shared_ptr
<CallbackFlushHandler
>> mpCallbackFlushHandlers
;
203 const int mnDocumentId
;
205 explicit LibLODocument_Impl(const css::uno::Reference
<css::lang::XComponent
>& xComponent
,
207 ~LibLODocument_Impl();
210 struct DESKTOP_DLLPUBLIC LibLibreOffice_Impl
: public _LibreOfficeKit
212 OUString maLastExceptionMsg
;
213 std::shared_ptr
< LibreOfficeKitClass
> m_pOfficeClass
;
215 LibreOfficeKitCallback mpCallback
;
216 void *mpCallbackData
;
217 int64_t mOptionalFeatures
;
218 std::map
<OString
, rtl::Reference
<LOKInteractionHandler
>> mInteractionMap
;
220 LibLibreOffice_Impl();
221 ~LibLibreOffice_Impl();
223 bool hasOptionalFeature(LibreOfficeKitOptionalFeatures
const feature
)
225 return (mOptionalFeatures
& feature
) != 0;
229 /// Helper function to extract the value from parameters delimited by
230 /// comma, like: Name1=Value1,Name2=Value2,Name3=Value3.
231 /// @param rOptions When extracted, the Param=Value is removed from it.
232 DESKTOP_DLLPUBLIC OUString
extractParameter(OUString
& aOptions
, std::u16string_view rName
);
234 /// Helper function to convert JSON to a vector of PropertyValues.
235 /// Public to be unit-test-able.
236 DESKTOP_DLLPUBLIC
std::vector
<com::sun::star::beans::PropertyValue
> jsonToPropertyValuesVector(const char* pJSON
);
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */