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 <string_view>
17 
18 #include <boost/property_tree/ptree.hpp>
19 #include <boost/variant.hpp>
20 
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>
31 
32 #include <desktop/dllapi.h>
33 
35 
36 namespace desktop {
37 
40  {
42  int m_nPart;
43 
45  : m_nPart(INT_MIN) // -1 is reserved to mean "all parts".
46  {
47  }
48 
49  RectangleAndPart(const tools::Rectangle* pRect, int nPart)
50  : m_aRectangle( pRect ? *pRect : tools::Rectangle(0, 0, SfxLokHelper::MaxTwips, SfxLokHelper::MaxTwips))
51  , m_nPart(nPart)
52  {
53  }
54 
55  OString toString() const
56  {
57  if (m_nPart >= -1)
58  return m_aRectangle.toString() + ", " + OString::number(m_nPart);
59  else
60  return m_aRectangle.toString();
61  }
62 
65  bool isInfinite() const
66  {
67  return m_aRectangle.GetWidth() >= SfxLokHelper::MaxTwips &&
68  m_aRectangle.GetHeight() >= SfxLokHelper::MaxTwips;
69  }
70 
72  bool isEmpty() const
73  {
74  return m_aRectangle.IsEmpty();
75  }
76 
77  static RectangleAndPart Create(const std::string& rPayload);
78  };
79 
81  {
82  public:
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);
88 
91  void disableCallbacks() { ++m_nDisableCallbacks; }
94  void enableCallbacks() { --m_nDisableCallbacks; }
96  bool callbacksDisabled() const { return m_nDisableCallbacks != 0; }
97 
98  void addViewStates(int viewId);
99  void removeViewStates(int viewId);
100 
101  void setViewId( int viewId ) { m_viewId = viewId; }
102 
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;
107 
108  private:
110  {
111  CallbackData(const char* payload)
112  : PayloadString(payload ? payload : "(nil)")
113  {
114  }
115 
116  CallbackData(const char* payload, int viewId)
117  : PayloadString(payload ? payload : "(nil)")
118  , PayloadObject(viewId)
119  {
120  }
121 
122  CallbackData(const tools::Rectangle* pRect, int viewId)
123  : PayloadObject(RectangleAndPart(pRect, viewId))
124  { // PayloadString will be done on demand
125  }
126 
127  const std::string& getPayload() const;
129  void updateRectangleAndPart(const RectangleAndPart& rRectAndPart);
131  const RectangleAndPart& getRectangleAndPart() const;
133  boost::property_tree::ptree& setJson(const std::string& payload);
135  void setJson(const boost::property_tree::ptree& rTree);
137  const boost::property_tree::ptree& getJson() const;
138 
139  int getViewId() const;
140 
141  bool isEmpty() const
142  {
143  return PayloadString.empty() && PayloadObject.which() == 0;
144  }
145  void clear()
146  {
147  PayloadString.clear();
148  PayloadObject = boost::blank();
149  }
150 
152  bool validate() const;
153 
155  bool isCached() const { return PayloadObject.which() != 0; }
156 
157  private:
158  mutable std::string PayloadString;
159 
161  mutable boost::variant<boost::blank, RectangleAndPart, boost::property_tree::ptree, int> PayloadObject;
162  };
163 
164  typedef std::vector<int> queue_type1;
165  typedef std::vector<CallbackData> queue_type2;
166 
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);
174 
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;
184  void *m_pData;
186  std::mutex m_mutex;
187  class TimeoutIdle : public Timer
188  {
189  public:
190  TimeoutIdle( CallbackFlushHandler* handler );
191  virtual void Invoke() override;
192  private:
194  };
196  };
197 
198  struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument
199  {
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;
204 
205  explicit LibLODocument_Impl(const css::uno::Reference<css::lang::XComponent>& xComponent,
206  int nDocumentId);
208  };
209 
210  struct DESKTOP_DLLPUBLIC LibLibreOffice_Impl : public _LibreOfficeKit
211  {
213  std::shared_ptr< LibreOfficeKitClass > m_pOfficeClass;
214  oslThread maThread;
215  LibreOfficeKitCallback mpCallback;
218  std::map<OString, rtl::Reference<LOKInteractionHandler>> mInteractionMap;
219 
222 
223  bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
224  {
225  return (mOptionalFeatures & feature) != 0;
226  }
227  };
228 
232  DESKTOP_DLLPUBLIC OUString extractParameter(OUString& aOptions, std::u16string_view rName);
233 
236  DESKTOP_DLLPUBLIC std::vector<com::sun::star::beans::PropertyValue> jsonToPropertyValuesVector(const char* pJSON);
237 }
238 
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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:177
void disableCallbacks()
Disables callbacks on this handler.
Definition: init.hxx:91
std::map< OString, rtl::Reference< LOKInteractionHandler > > mInteractionMap
Definition: init.hxx:218
static const tools::Long MaxTwips
std::unique_ptr< ContentProperties > pData
#define DESKTOP_DLLPUBLIC
Definition: dllapi.h:19
tools::Rectangle m_aRectangle
Definition: init.hxx:41
InteractionHandler is an interface that provides the user with various dialogs / error messages...
std::vector< CallbackData > queue_type2
Definition: init.hxx:165
void setViewId(int viewId)
Definition: init.hxx:101
bool isInfinite() const
Infinite Rectangle is both sides are equal or longer than SfxLokHelper::MaxTwips. ...
Definition: init.hxx:65
OString toString() const
Definition: init.hxx:55
Definition: app.cxx:164
CallbackData(const char *payload, int viewId)
Definition: init.hxx:116
bool callbacksDisabled() const
Returns true iff callbacks are disabled.
Definition: init.hxx:96
std::shared_ptr< LibreOfficeKitClass > m_pOfficeClass
Definition: init.hxx:213
std::unordered_map< int, std::unordered_map< int, std::string > > m_viewStates
Definition: init.hxx:180
constexpr tools::Long GetWidth() const
css::uno::Reference< css::lang::XComponent > mxComponent
Definition: init.hxx:200
constexpr bool IsEmpty() const
std::map< size_t, std::shared_ptr< CallbackFlushHandler > > mpCallbackFlushHandlers
Definition: init.hxx:202
LibreOfficeKitCallback mpCallback
Definition: init.hxx:215
void enableCallbacks()
Enables callbacks on this handler.
Definition: init.hxx:94
CallbackFlushHandler * mHandler
Definition: init.hxx:193
CallbackData(const tools::Rectangle *pRect, int viewId)
Definition: init.hxx:122
exports com.sun.star.chart2. data
LibreOfficeKitDocument * m_pDocument
Definition: init.hxx:181
static RectangleAndPart Create(const std::string &rPayload)
Definition: init.cxx:517
bool isEmpty() const
Empty Rectangle is when it has zero dimensions.
Definition: init.hxx:72
bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature)
Definition: init.hxx:223
boost::variant< boost::blank, RectangleAndPart, boost::property_tree::ptree, int > PayloadObject
The parsed payload cache. Update validate() when changing this.
Definition: init.hxx:161
std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass
Definition: init.hxx:201
Represents an invalidated rectangle inside a given document part.
Definition: init.hxx:39
DESKTOP_DLLPUBLIC OUString extractParameter(OUString &aOptions, std::u16string_view rName)
Helper function to extract the value from parameters delimited by comma, like: Name1=Value1,Name2=Value2,Name3=Value3.
Definition: init.cxx:985
bool isCached() const
Returns true iff there is cached data.
Definition: init.hxx:155
LibreOfficeKitCallback m_pCallback
Definition: init.hxx:183
std::vector< int > queue_type1
Definition: init.hxx:164
ResultType type
rtl::OString toString() const
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:417
RectangleAndPart(const tools::Rectangle *pRect, int nPart)
Definition: init.hxx:49
std::map< int, std::string > m_states
Definition: init.hxx:179
constexpr tools::Long GetHeight() const