LOK: always update the payload string via the cache
[LibreOffice.git] / desktop / inc / lib / init.hxx
blob2ba5595d8b46fb1a7a2e00faa38027f71e5e93da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef INCLUDED_DESKTOP_INC_LIB_INIT_HXX
11 #define INCLUDED_DESKTOP_INC_LIB_INIT_HXX
13 #include <map>
14 #include <unordered_map>
15 #include <memory>
16 #include <mutex>
18 #include <boost/variant.hpp>
20 #include <osl/thread.h>
21 #include <rtl/ref.hxx>
22 #include <vcl/idle.hxx>
23 #include <LibreOfficeKit/LibreOfficeKit.h>
24 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include <tools/gen.hxx>
28 #include <sfx2/lokhelper.hxx>
30 #include <desktop/dllapi.h>
32 class LOKInteractionHandler;
34 namespace desktop {
36 /// Represents an invalidated rectangle inside a given document part.
37 struct RectangleAndPart
39 tools::Rectangle m_aRectangle;
40 int m_nPart;
42 RectangleAndPart()
43 : m_nPart(INT_MIN) // -1 is reserved to mean "all parts".
47 OString toString() const
49 std::stringstream ss;
50 ss << m_aRectangle.toString();
51 if (m_nPart >= -1)
52 ss << ", " << m_nPart;
53 return ss.str().c_str();
56 /// Infinite Rectangle is both sides are
57 /// equal or longer than SfxLokHelper::MaxTwips.
58 bool isInfinite() const
60 return m_aRectangle.GetWidth() >= SfxLokHelper::MaxTwips &&
61 m_aRectangle.GetHeight() >= SfxLokHelper::MaxTwips;
64 /// Empty Rectangle is when it has zero dimensions.
65 bool isEmpty() const
67 return m_aRectangle.IsEmpty();
70 static RectangleAndPart Create(const std::string& rPayload);
73 class DESKTOP_DLLPUBLIC CallbackFlushHandler : public Idle
75 public:
76 explicit CallbackFlushHandler(LibreOfficeKitDocument* pDocument, LibreOfficeKitCallback pCallback, void* pData);
77 virtual ~CallbackFlushHandler() override;
78 virtual void Invoke() override;
79 static void callback(const int type, const char* payload, void* data);
80 void queue(const int type, const char* data);
82 /// When enabled events are queued but callback not invoked.
83 void setEventLatch(const bool bEventLatch)
85 m_bEventLatch = bEventLatch;
88 bool isEventLatchOn() const { return m_bEventLatch; }
89 void setPartTilePainting(const bool bPartPainting) { m_bPartTilePainting = bPartPainting; }
90 bool isPartTilePainting() const { return m_bPartTilePainting; }
92 void addViewStates(int viewId);
93 void removeViewStates(int viewId);
95 struct CallbackData
97 CallbackData(int type, const std::string& payload)
98 : Type(type)
99 , PayloadString(payload)
103 /// Parse and set the RectangleAndPart object and return it. Clobbers PayloadString.
104 RectangleAndPart& setRectangleAndPart(const std::string& payload);
105 /// Set a RectangleAndPart object and update PayloadString.
106 void setRectangleAndPart(const RectangleAndPart& rRectAndPart);
107 /// Return the parsed RectangleAndPart instance.
108 const RectangleAndPart& getRectangleAndPart() const;
109 /// Parse and set the JSON object and return it. Clobbers PayloadString.
110 boost::property_tree::ptree& setJson(const std::string& payload);
111 /// Set a Json object and update PayloadString.
112 void setJson(const boost::property_tree::ptree& rTree);
113 /// Return the parsed JSON instance.
114 const boost::property_tree::ptree& getJson() const;
116 /// Validate that the payload and parsed object match.
117 bool validate() const;
119 int Type;
120 std::string PayloadString;
122 private:
123 /// The parsed payload cache. Update validate() when changing this.
124 boost::variant<boost::blank, RectangleAndPart, boost::property_tree::ptree> PayloadObject;
127 typedef std::vector<CallbackData> queue_type;
129 private:
130 void removeAll(const std::function<bool (const queue_type::value_type&)>& rTestFunc);
132 queue_type m_queue;
133 std::map<int, std::string> m_states;
134 std::unordered_map<int, std::unordered_map<int, std::string>> m_viewStates;
135 LibreOfficeKitDocument* m_pDocument;
136 LibreOfficeKitCallback m_pCallback;
137 void *m_pData;
138 bool m_bPartTilePainting;
139 bool m_bEventLatch;
140 std::mutex m_mutex;
143 struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument
145 css::uno::Reference<css::lang::XComponent> mxComponent;
146 std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass;
147 std::map<size_t, std::shared_ptr<CallbackFlushHandler>> mpCallbackFlushHandlers;
149 explicit LibLODocument_Impl(const css::uno::Reference <css::lang::XComponent> &xComponent);
150 ~LibLODocument_Impl();
153 struct DESKTOP_DLLPUBLIC LibLibreOffice_Impl : public _LibreOfficeKit
155 OUString maLastExceptionMsg;
156 std::shared_ptr< LibreOfficeKitClass > m_pOfficeClass;
157 oslThread maThread;
158 LibreOfficeKitCallback mpCallback;
159 void *mpCallbackData;
160 int64_t mOptionalFeatures;
161 std::map<OString, rtl::Reference<LOKInteractionHandler>> mInteractionMap;
163 LibLibreOffice_Impl();
164 ~LibLibreOffice_Impl();
166 bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
168 return (mOptionalFeatures & feature) != 0;
172 /// Helper function to extract the value from parameters delimited by
173 /// comma, like: Name1=Value1,Name2=Value2,Name3=Value3.
174 /// @param rOptions When extracted, the Param=Value is removed from it.
175 DESKTOP_DLLPUBLIC OUString extractParameter(OUString& aOptions, const OUString& rName);
177 /// Helper function to convert JSON to a vector of PropertyValues.
178 /// Public to be unit-test-able.
179 DESKTOP_DLLPUBLIC std::vector<com::sun::star::beans::PropertyValue> jsonToPropertyValuesVector(const char* pJSON);
182 #endif
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */