Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / desktop / inc / lib / init.hxx
blob3587ba003e3757bb0480be8ae9df6be33c3318a2
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 /// Returns true iff there is cached data.
120 bool isCached() const { return PayloadObject.which() != 0; }
122 int Type;
123 std::string PayloadString;
125 private:
126 /// The parsed payload cache. Update validate() when changing this.
127 boost::variant<boost::blank, RectangleAndPart, boost::property_tree::ptree> PayloadObject;
130 typedef std::vector<CallbackData> queue_type;
132 private:
133 bool removeAll(const std::function<bool (const queue_type::value_type&)>& rTestFunc);
134 bool processInvalidateTilesEvent(CallbackData& aCallbackData);
135 bool processWindowEvent(CallbackData& aCallbackData);
137 queue_type m_queue;
138 std::map<int, std::string> m_states;
139 std::unordered_map<int, std::unordered_map<int, std::string>> m_viewStates;
140 LibreOfficeKitDocument* m_pDocument;
141 LibreOfficeKitCallback m_pCallback;
142 void *m_pData;
143 bool m_bPartTilePainting;
144 bool m_bEventLatch;
145 std::mutex m_mutex;
148 struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument
150 css::uno::Reference<css::lang::XComponent> mxComponent;
151 std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass;
152 std::map<size_t, std::shared_ptr<CallbackFlushHandler>> mpCallbackFlushHandlers;
154 explicit LibLODocument_Impl(const css::uno::Reference <css::lang::XComponent> &xComponent);
155 ~LibLODocument_Impl();
158 struct DESKTOP_DLLPUBLIC LibLibreOffice_Impl : public _LibreOfficeKit
160 OUString maLastExceptionMsg;
161 std::shared_ptr< LibreOfficeKitClass > m_pOfficeClass;
162 oslThread maThread;
163 LibreOfficeKitCallback mpCallback;
164 void *mpCallbackData;
165 int64_t mOptionalFeatures;
166 std::map<OString, rtl::Reference<LOKInteractionHandler>> mInteractionMap;
168 LibLibreOffice_Impl();
169 ~LibLibreOffice_Impl();
171 bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
173 return (mOptionalFeatures & feature) != 0;
177 /// Helper function to extract the value from parameters delimited by
178 /// comma, like: Name1=Value1,Name2=Value2,Name3=Value3.
179 /// @param rOptions When extracted, the Param=Value is removed from it.
180 DESKTOP_DLLPUBLIC OUString extractParameter(OUString& aOptions, const OUString& rName);
182 /// Helper function to convert JSON to a vector of PropertyValues.
183 /// Public to be unit-test-able.
184 DESKTOP_DLLPUBLIC std::vector<com::sun::star::beans::PropertyValue> jsonToPropertyValuesVector(const char* pJSON);
187 #endif
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */