LibreOffice Module desktop (master)  1
init.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 #include <config_features.h>
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 
16 #ifdef IOS
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <unicode/udata.h>
20 #include <unicode/ucnv.h>
21 #include <premac.h>
22 #import <Foundation/Foundation.h>
23 #import <CoreGraphics/CoreGraphics.h>
24 #include <postmac.h>
25 #endif
26 
27 #ifdef ANDROID
28 #include <osl/detail/android-bootstrap.h>
29 #endif
30 
31 #include <algorithm>
32 #include <memory>
33 #include <iostream>
34 #include <string_view>
35 
36 #include <boost/property_tree/json_parser.hpp>
37 #include <boost/algorithm/string.hpp>
38 
39 #include <LibreOfficeKit/LibreOfficeKit.h>
40 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
41 
42 #include <sal/log.hxx>
43 #include <vcl/errinf.hxx>
44 #include <vcl/lok.hxx>
45 #include <o3tl/any.hxx>
46 #include <o3tl/unit_conversion.hxx>
47 #include <osl/file.hxx>
48 #include <osl/process.h>
49 #include <osl/thread.h>
50 #include <rtl/bootstrap.hxx>
51 #include <rtl/strbuf.hxx>
52 #include <rtl/uri.hxx>
53 #include <svl/zforlist.hxx>
54 #include <cppuhelper/bootstrap.hxx>
55 #include <comphelper/base64.hxx>
57 #include <comphelper/lok.hxx>
59 #include <comphelper/string.hxx>
65 
66 #include <com/sun/star/document/MacroExecMode.hpp>
67 #include <com/sun/star/beans/XPropertySet.hpp>
68 #include <com/sun/star/container/XNameAccess.hpp>
69 #include <com/sun/star/frame/Desktop.hpp>
70 #include <com/sun/star/frame/DispatchResultEvent.hpp>
71 #include <com/sun/star/frame/DispatchResultState.hpp>
72 #include <com/sun/star/frame/XDispatchProvider.hpp>
73 #include <com/sun/star/frame/XDispatchResultListener.hpp>
74 #include <com/sun/star/frame/XSynchronousDispatch.hpp>
75 #include <com/sun/star/frame/XStorable.hpp>
76 #include <com/sun/star/lang/Locale.hpp>
77 #include <com/sun/star/lang/XComponent.hpp>
78 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
79 #include <com/sun/star/reflection/theCoreReflection.hpp>
80 #include <com/sun/star/reflection/XIdlClass.hpp>
81 #include <com/sun/star/reflection/XIdlReflection.hpp>
82 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
83 #include <com/sun/star/util/URLTransformer.hpp>
84 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
85 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
86 #include <com/sun/star/datatransfer/XTransferable2.hpp>
87 #include <com/sun/star/text/TextContentAnchorType.hpp>
88 #include <com/sun/star/document/XRedlinesSupplier.hpp>
89 #include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
90 
91 #include <com/sun/star/xml/crypto/SEInitializer.hpp>
92 #include <com/sun/star/xml/crypto/XSEInitializer.hpp>
93 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
94 #include <com/sun/star/xml/crypto/XCertificateCreator.hpp>
95 #include <com/sun/star/security/XCertificate.hpp>
96 
97 #include <com/sun/star/linguistic2/LinguServiceManager.hpp>
98 #include <com/sun/star/linguistic2/XSpellChecker.hpp>
99 #include <com/sun/star/i18n/LocaleCalendar2.hpp>
100 #include <com/sun/star/i18n/ScriptType.hpp>
101 #include <com/sun/star/lang/DisposedException.hpp>
102 
103 #include <editeng/flstitem.hxx>
104 #ifdef IOS
105 #include <sfx2/app.hxx>
106 #endif
107 #include <sfx2/objsh.hxx>
108 #include <sfx2/docfilt.hxx>
109 #include <sfx2/docfile.hxx>
110 #include <sfx2/viewsh.hxx>
111 #include <sfx2/viewfrm.hxx>
112 #include <sfx2/msgpool.hxx>
113 #include <sfx2/dispatch.hxx>
114 #include <sfx2/lokcharthelper.hxx>
115 #include <sfx2/DocumentSigner.hxx>
118 #include <svl/numformat.hxx>
119 #include <svx/dialmgr.hxx>
120 #include <svx/strings.hrc>
121 #include <svx/svdview.hxx>
122 #include <svx/svxids.hrc>
123 #include <svx/ucsubset.hxx>
124 #include <vcl/vclevent.hxx>
125 #include <vcl/GestureEvent.hxx>
126 #include <vcl/svapp.hxx>
127 #include <unotools/resmgr.hxx>
128 #include <tools/fract.hxx>
129 #include <tools/json_writer.hxx>
130 #include <svtools/ctrltool.hxx>
131 #include <svtools/langtab.hxx>
132 #include <vcl/fontcharmap.hxx>
133 #include <vcl/graphicfilter.hxx>
134 #ifdef IOS
135 #include <vcl/sysdata.hxx>
136 #endif
137 #include <vcl/virdev.hxx>
138 #include <vcl/ImageTree.hxx>
139 #include <vcl/ITiledRenderable.hxx>
140 #include <vcl/dialoghelper.hxx>
141 #include <unicode/uchar.h>
143 #include <unotools/configmgr.hxx>
144 #include <unotools/confignode.hxx>
147 #include <unotools/pathoptions.hxx>
148 #include <unotools/tempfile.hxx>
149 #include <unotools/streamwrap.hxx>
150 #include <osl/module.hxx>
151 #include <comphelper/sequence.hxx>
152 #include <sfx2/sfxbasemodel.hxx>
153 #include <svl/undo.hxx>
154 #include <unotools/datetime.hxx>
155 #include <i18nlangtag/mslangid.hxx>
157 #include <vcl/abstdlg.hxx>
158 #include <tools/diagnose_ex.h>
159 #include <vcl/uitest/uiobject.hxx>
160 #include <vcl/jsdialog/executor.hxx>
161 
162 // Needed for getUndoManager()
163 #include <com/sun/star/document/XUndoManager.hpp>
164 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
165 #include <editeng/sizeitem.hxx>
166 #include <svx/rulritem.hxx>
167 #include <svx/pageitem.hxx>
168 
169 #include <app.hxx>
170 
171 #include "../app/cmdlineargs.hxx"
172 // We also need to hackily be able to start the main libreoffice thread:
173 #include "../app/sofficemain.h"
174 #include "../app/officeipcthread.hxx"
175 #include <lib/init.hxx>
176 
177 #include "lokinteractionhandler.hxx"
178 #include "lokclipboard.hxx"
179 #include <officecfg/Office/Impress.hxx>
180 
181 using namespace css;
182 using namespace vcl;
183 using namespace desktop;
184 using namespace utl;
185 
186 static LibLibreOffice_Impl *gImpl = nullptr;
187 static std::weak_ptr< LibreOfficeKitClass > gOfficeClass;
188 static std::weak_ptr< LibreOfficeKitDocumentClass > gDocumentClass;
189 
190 static void SetLastExceptionMsg(const OUString& s = OUString())
191 {
192  SAL_WARN_IF(!s.isEmpty(), "lok", "lok exception '" + s + "'");
193  if (gImpl)
194  gImpl->maLastExceptionMsg = s;
195 }
196 
197 namespace {
198 
199 struct ExtensionMap
200 {
201  const char *extn;
202  const char *filterName;
203 };
204 
205 class TraceEventDumper : public AutoTimer
206 {
207  static const int dumpTimeoutMS = 5000;
208 
209 public:
210  TraceEventDumper() : AutoTimer( "Trace Event dumper" )
211  {
212  SetTimeout(dumpTimeoutMS);
213  Start();
214  }
215 
216  virtual void Invoke() override
217  {
218  flushRecordings();
219  }
220 
221  static void flushRecordings()
222  {
223  const css::uno::Sequence<OUString> aEvents =
225  OStringBuffer aOutput;
226  for (const auto &s : aEvents)
227  {
228  aOutput.append(OUStringToOString(s, RTL_TEXTENCODING_UTF8));
229  aOutput.append("\n");
230  }
231  if (aOutput.getLength() > 0)
232  {
233  OString aChunk = aOutput.makeStringAndClear();
234  if (gImpl && gImpl->mpCallback)
235  gImpl->mpCallback(LOK_CALLBACK_PROFILE_FRAME, aChunk.getStr(), gImpl->mpCallbackData);
236  }
237  }
238 };
239 
240 } // unnamed namespace
241 
242 static TraceEventDumper *traceEventDumper = nullptr;
243 
244 const ExtensionMap aWriterExtensionMap[] =
245 {
246  { "doc", "MS Word 97" },
247  { "docm", "MS Word 2007 XML VBA" },
248  { "docx", "MS Word 2007 XML" },
249  { "fodt", "OpenDocument Text Flat XML" },
250  { "html", "HTML (StarWriter)" },
251  { "odt", "writer8" },
252  { "ott", "writer8_template" },
253  { "pdf", "writer_pdf_Export" },
254  { "epub", "EPUB" },
255  { "rtf", "Rich Text Format" },
256  { "txt", "Text" },
257  { "xhtml", "XHTML Writer File" },
258  { "png", "writer_png_Export" },
259  { "xml", "writer_indexing_export" },
260  { nullptr, nullptr }
261 };
262 
263 const ExtensionMap aCalcExtensionMap[] =
264 {
265  { "csv", "Text - txt - csv (StarCalc)" },
266  { "fods", "OpenDocument Spreadsheet Flat XML" },
267  { "html", "HTML (StarCalc)" },
268  { "ods", "calc8" },
269  { "ots", "calc8_template" },
270  { "pdf", "calc_pdf_Export" },
271  { "xhtml", "XHTML Calc File" },
272  { "xls", "MS Excel 97" },
273  { "xlsm", "Calc MS Excel 2007 VBA XML" },
274  { "xlsx", "Calc MS Excel 2007 XML" },
275  { "png", "calc_png_Export" },
276  { nullptr, nullptr }
277 };
278 
279 const ExtensionMap aImpressExtensionMap[] =
280 {
281  { "fodp", "OpenDocument Presentation Flat XML" },
282  { "html", "impress_html_Export" },
283  { "odg", "impress8_draw" },
284  { "odp", "impress8" },
285  { "otp", "impress8_template" },
286  { "pdf", "impress_pdf_Export" },
287  { "potm", "Impress MS PowerPoint 2007 XML Template" },
288  { "pot", "MS PowerPoint 97 Vorlage" },
289  { "pptm", "Impress MS PowerPoint 2007 XML VBA" },
290  { "pptx", "Impress MS PowerPoint 2007 XML" },
291  { "pps", "MS PowerPoint 97 Autoplay" },
292  { "ppt", "MS PowerPoint 97" },
293  { "svg", "impress_svg_Export" },
294  { "xhtml", "XHTML Impress File" },
295  { "png", "impress_png_Export"},
296  { nullptr, nullptr }
297 };
298 
299 const ExtensionMap aDrawExtensionMap[] =
300 {
301  { "fodg", "draw_ODG_FlatXML" },
302  { "html", "draw_html_Export" },
303  { "odg", "draw8" },
304  { "pdf", "draw_pdf_Export" },
305  { "svg", "draw_svg_Export" },
306  { "xhtml", "XHTML Draw File" },
307  { "png", "draw_png_Export"},
308  { nullptr, nullptr }
309 };
310 
311 static OUString getUString(const char* pString)
312 {
313  if (pString == nullptr)
314  return OUString();
315 
316  OString sString(pString, strlen(pString));
317  return OStringToOUString(sString, RTL_TEXTENCODING_UTF8);
318 }
319 
320 // Tolerate embedded \0s etc.
321 static char *convertOString(const OString &rStr)
322 {
323  char* pMemory = static_cast<char*>(malloc(rStr.getLength() + 1));
324  assert(pMemory); // don't tolerate failed allocations.
325  memcpy(pMemory, rStr.getStr(), rStr.getLength() + 1);
326  return pMemory;
327 }
328 
329 static char *convertOUString(std::u16string_view aStr)
330 {
331  return convertOString(OUStringToOString(aStr, RTL_TEXTENCODING_UTF8));
332 }
333 
335 static OUString getAbsoluteURL(const char* pURL)
336 {
337  OUString aURL(getUString(pURL));
338  if (aURL.isEmpty())
339  return aURL;
340 
341  // convert relative paths to absolute ones
342  OUString aWorkingDir;
343  osl_getProcessWorkingDir(&aWorkingDir.pData);
344  if (!aWorkingDir.endsWith("/"))
345  aWorkingDir += "/";
346 
347  try
348  {
349  return rtl::Uri::convertRelToAbs(aWorkingDir, aURL);
350  }
351  catch (const rtl::MalformedUriException &)
352  {
353  }
354 
355  return OUString();
356 }
357 
358 static uno::Any jsonToUnoAny(const boost::property_tree::ptree& aTree)
359 {
360  uno::Any aAny;
361  uno::Any aValue;
362  sal_Int32 nFields;
363  uno::Reference< reflection::XIdlField > aField;
364  boost::property_tree::ptree aNodeNull, aNodeValue, aNodeField;
365  const std::string& rType = aTree.get<std::string>("type", "");
366  const std::string& rValue = aTree.get<std::string>("value", "");
367  uno::Sequence< uno::Reference< reflection::XIdlField > > aFields;
368  uno::Reference< reflection:: XIdlClass > xIdlClass =
369  css::reflection::theCoreReflection::get(comphelper::getProcessComponentContext())->forName(OUString::fromUtf8(rType.c_str()));
370  if (xIdlClass.is())
371  {
372  uno::TypeClass aTypeClass = xIdlClass->getTypeClass();
373  xIdlClass->createObject(aAny);
374  aFields = xIdlClass->getFields();
375  nFields = aFields.getLength();
376  aNodeValue = aTree.get_child("value", aNodeNull);
377  if (nFields > 0 && aNodeValue != aNodeNull)
378  {
379  for (sal_Int32 itField = 0; itField < nFields; ++itField)
380  {
381  aField = aFields[itField];
382  aNodeField = aNodeValue.get_child(aField->getName().toUtf8().getStr(), aNodeNull);
383  if (aNodeField != aNodeNull)
384  {
385  aValue = jsonToUnoAny(aNodeField);
386  aField->set(aAny, aValue);
387  }
388  }
389  }
390  else if (!rValue.empty())
391  {
392  if (aTypeClass == uno::TypeClass_VOID)
393  aAny.clear();
394  else if (aTypeClass == uno::TypeClass_BYTE)
395  aAny <<= static_cast<sal_Int8>(OString(rValue.c_str()).toInt32());
396  else if (aTypeClass == uno::TypeClass_BOOLEAN)
397  aAny <<= OString(rValue.c_str()).toBoolean();
398  else if (aTypeClass == uno::TypeClass_SHORT)
399  aAny <<= static_cast<sal_Int16>(OString(rValue.c_str()).toInt32());
400  else if (aTypeClass == uno::TypeClass_UNSIGNED_SHORT)
401  aAny <<= static_cast<sal_uInt16>(OString(rValue.c_str()).toUInt32());
402  else if (aTypeClass == uno::TypeClass_LONG)
403  aAny <<= OString(rValue.c_str()).toInt32();
404  else if (aTypeClass == uno::TypeClass_UNSIGNED_LONG)
405  aAny <<= static_cast<sal_uInt32>(OString(rValue.c_str()).toInt32());
406  else if (aTypeClass == uno::TypeClass_FLOAT)
407  aAny <<= OString(rValue.c_str()).toFloat();
408  else if (aTypeClass == uno::TypeClass_DOUBLE)
409  aAny <<= OString(rValue.c_str()).toDouble();
410  else if (aTypeClass == uno::TypeClass_STRING)
411  aAny <<= OUString::fromUtf8(rValue.c_str());
412  }
413  }
414  return aAny;
415 }
416 
417 std::vector<beans::PropertyValue> desktop::jsonToPropertyValuesVector(const char* pJSON)
418 {
419  std::vector<beans::PropertyValue> aArguments;
420  if (pJSON && pJSON[0] != '\0')
421  {
422  boost::property_tree::ptree aTree, aNodeNull, aNodeValue;
423  std::stringstream aStream(pJSON);
424  boost::property_tree::read_json(aStream, aTree);
425 
426  for (const auto& rPair : aTree)
427  {
428  const std::string& rType = rPair.second.get<std::string>("type", "");
429  const std::string& rValue = rPair.second.get<std::string>("value", "");
430 
431  beans::PropertyValue aValue;
432  aValue.Name = OUString::fromUtf8(rPair.first.c_str());
433  if (rType == "string")
434  aValue.Value <<= OUString::fromUtf8(rValue.c_str());
435  else if (rType == "boolean")
436  aValue.Value <<= OString(rValue.c_str()).toBoolean();
437  else if (rType == "float")
438  aValue.Value <<= OString(rValue.c_str()).toFloat();
439  else if (rType == "long")
440  aValue.Value <<= OString(rValue.c_str()).toInt32();
441  else if (rType == "short")
442  aValue.Value <<= sal_Int16(OString(rValue.c_str()).toInt32());
443  else if (rType == "unsigned short")
444  aValue.Value <<= sal_uInt16(OString(rValue.c_str()).toUInt32());
445  else if (rType == "int64")
446  aValue.Value <<= OString(rValue.c_str()).toInt64();
447  else if (rType == "int32")
448  aValue.Value <<= OString(rValue.c_str()).toInt32();
449  else if (rType == "int16")
450  aValue.Value <<= sal_Int16(OString(rValue.c_str()).toInt32());
451  else if (rType == "uint64")
452  aValue.Value <<= OString(rValue.c_str()).toUInt64();
453  else if (rType == "uint32")
454  aValue.Value <<= OString(rValue.c_str()).toUInt32();
455  else if (rType == "uint16")
456  aValue.Value <<= sal_uInt16(OString(rValue.c_str()).toUInt32());
457  else if (rType == "[]byte")
458  {
459  aNodeValue = rPair.second.get_child("value", aNodeNull);
460  if (aNodeValue != aNodeNull && aNodeValue.size() == 0)
461  {
462  uno::Sequence< sal_Int8 > aSeqByte(reinterpret_cast<const sal_Int8*>(rValue.c_str()), rValue.size());
463  aValue.Value <<= aSeqByte;
464  }
465  }
466  else if (rType == "[]any")
467  {
468  aNodeValue = rPair.second.get_child("value", aNodeNull);
469  if (aNodeValue != aNodeNull && !aNodeValue.empty())
470  {
471  sal_Int32 itSeq = 0;
472  uno::Sequence< uno::Any > aSeq(aNodeValue.size());
473  for (const auto& rSeqPair : aNodeValue)
474  aSeq[itSeq++] = jsonToUnoAny(rSeqPair.second);
475  aValue.Value <<= aSeq;
476  }
477  }
478  else
479  SAL_WARN("desktop.lib", "jsonToPropertyValuesVector: unhandled type '"<<rType<<"'");
480  aArguments.push_back(aValue);
481  }
482  }
483  return aArguments;
484 }
485 
486 static void unoAnyToJson(tools::JsonWriter& rJson, const char * pNodeName, const uno::Any& anyItem)
487 {
488  auto aNode = rJson.startNode(pNodeName);
489  OUString aType = anyItem.getValueTypeName();
490  rJson.put("type", aType.toUtf8().getStr());
491 
492  if (aType == "string")
493  rJson.put("value", anyItem.get<OUString>().toUtf8().getStr());
494  else if (aType == "unsigned long")
495  rJson.put("value", OString::number(anyItem.get<sal_uInt32>()).getStr());
496  else if (aType == "long")
497  rJson.put("value", OString::number(anyItem.get<sal_Int32>()).getStr());
498  else if (aType == "[]any")
499  {
500  uno::Sequence<uno::Any> aSeq;
501  if (anyItem >>= aSeq)
502  {
503  auto valueNode = rJson.startNode("value");
504 
505  for (auto i = 0; i < aSeq.getLength(); ++i)
506  {
507  unoAnyToJson(rJson, OString::number(i).getStr(), aSeq[i]);
508  }
509  }
510  }
511 }
512 
513 namespace desktop {
514 
515 RectangleAndPart RectangleAndPart::Create(const std::string& rPayload)
516 {
517  RectangleAndPart aRet;
518  if (rPayload.compare(0, 5, "EMPTY") == 0) // payload starts with "EMPTY"
519  {
522  aRet.m_nPart = std::stol(rPayload.substr(6));
523 
524  return aRet;
525  }
526 
527  // Read '<left>, <top>, <width>, <height>[, <part>]'. C++ streams are simpler but slower.
528  const char* pos = rPayload.c_str();
529  const char* end = rPayload.c_str() + rPayload.size();
530  tools::Long nLeft = rtl_str_toInt64_WithLength(pos, 10, end - pos);
531  while( *pos != ',' )
532  ++pos;
533  ++pos;
534  assert(pos < end);
535  tools::Long nTop = rtl_str_toInt64_WithLength(pos, 10, end - pos);
536  while( *pos != ',' )
537  ++pos;
538  ++pos;
539  assert(pos < end);
540  tools::Long nWidth = rtl_str_toInt64_WithLength(pos, 10, end - pos);
541  while( *pos != ',' )
542  ++pos;
543  ++pos;
544  assert(pos < end);
545  tools::Long nHeight = rtl_str_toInt64_WithLength(pos, 10, end - pos);
546  tools::Long nPart = INT_MIN;
548  {
549  while( *pos != ',' )
550  ++pos;
551  ++pos;
552  assert(pos < end);
553  nPart = rtl_str_toInt64_WithLength(pos, 10, end - pos);
554  }
555 
556  if (nWidth > 0 && nHeight > 0)
557  {
558  // The top-left corner starts at (0, 0).
559  // Anything negative is invalid.
560  if (nLeft < 0)
561  {
562  nWidth += nLeft;
563  nLeft = 0;
564  }
565 
566  if (nTop < 0)
567  {
568  nHeight += nTop;
569  nTop = 0;
570  }
571 
572  if (nWidth > 0 && nHeight > 0)
573  {
574  aRet.m_aRectangle = tools::Rectangle(nLeft, nTop, nLeft + nWidth, nTop + nHeight);
575  }
576  }
577  // else leave empty rect.
578 
579  aRet.m_nPart = nPart;
580  return aRet;
581 }
582 
583 RectangleAndPart& CallbackFlushHandler::CallbackData::setRectangleAndPart(const std::string& payload)
584 {
585  setRectangleAndPart(RectangleAndPart::Create(payload));
586 
587  // Return reference to the cached object.
588  return boost::get<RectangleAndPart>(PayloadObject);
589 }
590 
591 void CallbackFlushHandler::CallbackData::setRectangleAndPart(const RectangleAndPart& rRectAndPart)
592 {
593  PayloadString = rRectAndPart.toString().getStr();
594  PayloadObject = rRectAndPart;
595 }
596 
597 const RectangleAndPart& CallbackFlushHandler::CallbackData::getRectangleAndPart() const
598 {
599  assert(PayloadObject.which() == 1);
600  return boost::get<RectangleAndPart>(PayloadObject);
601 }
602 
603 boost::property_tree::ptree& CallbackFlushHandler::CallbackData::setJson(const std::string& payload)
604 {
605  boost::property_tree::ptree aTree;
606  std::stringstream aStream(payload);
607  boost::property_tree::read_json(aStream, aTree);
608 
609  // Let boost normalize the payload so it always matches the cache.
610  setJson(aTree);
611 
612  // Return reference to the cached object.
613  return boost::get<boost::property_tree::ptree>(PayloadObject);
614 }
615 
616 void CallbackFlushHandler::CallbackData::setJson(const boost::property_tree::ptree& rTree)
617 {
618  std::stringstream aJSONStream;
619  constexpr bool bPretty = false; // Don't waste time and bloat logs.
620  boost::property_tree::write_json(aJSONStream, rTree, bPretty);
621  PayloadString = boost::trim_copy(aJSONStream.str());
622 
623  PayloadObject = rTree;
624 }
625 
626 const boost::property_tree::ptree& CallbackFlushHandler::CallbackData::getJson() const
627 {
628  assert(PayloadObject.which() == 2);
629  return boost::get<boost::property_tree::ptree>(PayloadObject);
630 }
631 
632 bool CallbackFlushHandler::CallbackData::validate() const
633 {
634  switch (PayloadObject.which())
635  {
636  // Not cached.
637  case 0:
638  return true;
639 
640  // RectangleAndPart.
641  case 1:
642  return getRectangleAndPart().toString().getStr() == PayloadString;
643 
644  // Json.
645  case 2:
646  {
647  std::stringstream aJSONStream;
648  boost::property_tree::write_json(aJSONStream, getJson(), false);
649  const std::string aExpected = boost::trim_copy(aJSONStream.str());
650  return aExpected == PayloadString;
651  }
652 
653  default:
654  assert(!"Unknown variant type; please add an entry to validate.");
655  }
656 
657  return false;
658 }
659 
660 }
661 
662 namespace {
663 
664 bool lcl_isViewCallbackType(const int type)
665 {
666  switch (type)
667  {
668  case LOK_CALLBACK_CELL_VIEW_CURSOR:
669  case LOK_CALLBACK_GRAPHIC_VIEW_SELECTION:
670  case LOK_CALLBACK_INVALIDATE_VIEW_CURSOR:
671  case LOK_CALLBACK_TEXT_VIEW_SELECTION:
672  case LOK_CALLBACK_VIEW_CURSOR_VISIBLE:
673  return true;
674 
675  default:
676  return false;
677  }
678 }
679 
680 int lcl_getViewId(const std::string& payload)
681 {
682  // this is a cheap way how to get the viewId from a JSON message; proper
683  // parsing is terribly expensive, and we just need the viewId here
684  size_t viewIdPos = payload.find("viewId");
685  if (viewIdPos == std::string::npos)
686  return 0;
687 
688  size_t numberPos = payload.find(":", viewIdPos + 6);
689  if (numberPos == std::string::npos)
690  return 0;
691 
692  for (++numberPos; numberPos < payload.length(); ++numberPos)
693  {
694  if (payload[numberPos] == ',' || payload[numberPos] == '}' || (payload[numberPos] >= '0' && payload[numberPos] <= '9'))
695  break;
696  }
697 
698  if (numberPos < payload.length() && payload[numberPos] >= '0' && payload[numberPos] <= '9')
699  return strtol(payload.substr(numberPos).c_str(), nullptr, 10);
700 
701  return 0;
702 }
703 
704 int lcl_getViewId(const desktop::CallbackFlushHandler::CallbackData& rCallbackData)
705 {
706  if (rCallbackData.isCached())
707  return rCallbackData.getJson().get<int>("viewId");
708  return lcl_getViewId(rCallbackData.PayloadString);
709 }
710 
711 std::string extractCertificate(const std::string & certificate)
712 {
713  const std::string header("-----BEGIN CERTIFICATE-----");
714  const std::string footer("-----END CERTIFICATE-----");
715 
716  std::string result;
717 
718  size_t pos1 = certificate.find(header);
719  if (pos1 == std::string::npos)
720  return result;
721 
722  size_t pos2 = certificate.find(footer, pos1 + 1);
723  if (pos2 == std::string::npos)
724  return result;
725 
726  pos1 = pos1 + header.length();
727  pos2 = pos2 - pos1;
728 
729  return certificate.substr(pos1, pos2);
730 }
731 
732 std::string extractPrivateKey(const std::string & privateKey)
733 {
734  const std::string header("-----BEGIN PRIVATE KEY-----");
735  const std::string footer("-----END PRIVATE KEY-----");
736 
737  std::string result;
738 
739  size_t pos1 = privateKey.find(header);
740  if (pos1 == std::string::npos)
741  return result;
742 
743  size_t pos2 = privateKey.find(footer, pos1 + 1);
744  if (pos2 == std::string::npos)
745  return result;
746 
747  pos1 = pos1 + header.length();
748  pos2 = pos2 - pos1;
749 
750  return privateKey.substr(pos1, pos2);
751 }
752 
753 OUString lcl_getCurrentDocumentMimeType(const LibLODocument_Impl* pDocument)
754 {
755  SfxBaseModel* pBaseModel = dynamic_cast<SfxBaseModel*>(pDocument->mxComponent.get());
756  if (!pBaseModel)
757  return "";
758 
759  SfxObjectShell* pObjectShell = pBaseModel->GetObjectShell();
760  if (!pObjectShell)
761  return "";
762 
763  SfxMedium* pMedium = pObjectShell->GetMedium();
764  if (!pMedium)
765  return "";
766 
767  auto pFilter = pMedium->GetFilter();
768  if (!pFilter)
769  return "";
770 
771  return pFilter->GetMimeType();
772 }
773 
774 // Gets an undo manager to enter and exit undo context. Needed by ToggleOrientation
775 css::uno::Reference< css::document::XUndoManager > getUndoManager( const css::uno::Reference< css::frame::XFrame >& rxFrame )
776 {
777  const css::uno::Reference< css::frame::XController >& xController = rxFrame->getController();
778  if ( xController.is() )
779  {
780  const css::uno::Reference< css::frame::XModel >& xModel = xController->getModel();
781  if ( xModel.is() )
782  {
783  const css::uno::Reference< css::document::XUndoManagerSupplier > xSuppUndo( xModel, css::uno::UNO_QUERY_THROW );
784  return css::uno::Reference< css::document::XUndoManager >( xSuppUndo->getUndoManager(), css::uno::UNO_SET_THROW );
785  }
786  }
787 
788  return css::uno::Reference< css::document::XUndoManager > ();
789 }
790 
791 // Adjusts page margins for Writer doc. Needed by ToggleOrientation
792 void ExecuteMarginLRChange(
793  const tools::Long nPageLeftMargin,
794  const tools::Long nPageRightMargin,
795  SvxLongLRSpaceItem* pPageLRMarginItem)
796 {
797  pPageLRMarginItem->SetLeft( nPageLeftMargin );
798  pPageLRMarginItem->SetRight( nPageRightMargin );
799  SfxViewShell::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_LRSPACE,
800  SfxCallMode::RECORD, { pPageLRMarginItem });
801 }
802 
803 // Adjusts page margins for Writer doc. Needed by ToggleOrientation
804 void ExecuteMarginULChange(
805  const tools::Long nPageTopMargin,
806  const tools::Long nPageBottomMargin,
807  SvxLongULSpaceItem* pPageULMarginItem)
808 {
809  pPageULMarginItem->SetUpper( nPageTopMargin );
810  pPageULMarginItem->SetLower( nPageBottomMargin );
811  SfxViewShell::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_ULSPACE,
812  SfxCallMode::RECORD, { pPageULMarginItem });
813 }
814 
815 // Main function which toggles page orientation of the Writer doc. Needed by ToggleOrientation
816 void ExecuteOrientationChange()
817 {
818  std::unique_ptr<SvxPageItem> pPageItem(new SvxPageItem(SID_ATTR_PAGE));
819 
820  // 1mm in twips rounded
821  // This should be in sync with MINBODY in sw/source/uibase/sidebar/PageMarginControl.hxx
823 
824  css::uno::Reference< css::document::XUndoManager > mxUndoManager(
825  getUndoManager( SfxViewFrame::Current()->GetFrame().GetFrameInterface() ) );
826 
827  if ( mxUndoManager.is() )
828  mxUndoManager->enterUndoContext( "" );
829 
830 
831  const SfxPoolItem* pItem;
832 
833 
834  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_SIZE, pItem);
835  std::unique_ptr<SvxSizeItem> pPageSizeItem(&pItem->Clone()->StaticWhichCast(SID_ATTR_PAGE_SIZE));
836 
837  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_LRSPACE, pItem);
838  std::unique_ptr<SvxLongLRSpaceItem> pPageLRMarginItem(&pItem->Clone()->StaticWhichCast(SID_ATTR_PAGE_LRSPACE));
839 
840  SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PAGE_ULSPACE, pItem);
841  std::unique_ptr<SvxLongULSpaceItem> pPageULMarginItem(&pItem->Clone()->StaticWhichCast(SID_ATTR_PAGE_ULSPACE));
842 
843  {
844  bool bIsLandscape = false;
845  if ( pPageSizeItem->GetSize().Width() > pPageSizeItem->GetSize().Height())
846  bIsLandscape = true;
847 
848  // toggle page orientation
849  pPageItem->SetLandscape(!bIsLandscape);
850 
851 
852  // swap the width and height of the page size
853  const tools::Long nRotatedWidth = pPageSizeItem->GetSize().Height();
854  const tools::Long nRotatedHeight = pPageSizeItem->GetSize().Width();
855  pPageSizeItem->SetSize(Size(nRotatedWidth, nRotatedHeight));
856 
857 
858  // apply changed attributes
859  if (SfxViewShell::Current())
860  {
861  SfxViewShell::Current()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_SIZE,
862  SfxCallMode::RECORD, { pPageSizeItem.get(), pPageItem.get() });
863  }
864  }
865 
866 
867  // check, if margin values still fit to the changed page size.
868  // if not, adjust margin values
869  {
870  const tools::Long nML = pPageLRMarginItem->GetLeft();
871  const tools::Long nMR = pPageLRMarginItem->GetRight();
872  const tools::Long nTmpPW = nML + nMR + MINBODY;
873 
874  const tools::Long nPW = pPageSizeItem->GetSize().Width();
875 
876  if ( nTmpPW > nPW )
877  {
878  if ( nML <= nMR )
879  {
880  ExecuteMarginLRChange( pPageLRMarginItem->GetLeft(), nMR - (nTmpPW - nPW ), pPageLRMarginItem.get() );
881  }
882  else
883  {
884  ExecuteMarginLRChange( nML - (nTmpPW - nPW ), pPageLRMarginItem->GetRight(), pPageLRMarginItem.get() );
885  }
886  }
887 
888  const tools::Long nMT = pPageULMarginItem->GetUpper();
889  const tools::Long nMB = pPageULMarginItem->GetLower();
890  const tools::Long nTmpPH = nMT + nMB + MINBODY;
891 
892  const tools::Long nPH = pPageSizeItem->GetSize().Height();
893 
894  if ( nTmpPH > nPH )
895  {
896  if ( nMT <= nMB )
897  {
898  ExecuteMarginULChange( pPageULMarginItem->GetUpper(), nMB - ( nTmpPH - nPH ), pPageULMarginItem.get() );
899  }
900  else
901  {
902  ExecuteMarginULChange( nMT - ( nTmpPH - nPH ), pPageULMarginItem->GetLower(), pPageULMarginItem.get() );
903  }
904  }
905  }
906 
907  if ( mxUndoManager.is() )
908  mxUndoManager->leaveUndoContext();
909 }
910 
911 void setupSidebar(std::u16string_view sidebarDeckId = u"")
912 {
913  SfxViewShell* pViewShell = SfxViewShell::Current();
914  SfxViewFrame* pViewFrame = pViewShell ? pViewShell->GetViewFrame() : nullptr;
915  if (pViewFrame)
916  {
917  if (!pViewFrame->GetChildWindow(SID_SIDEBAR))
918  pViewFrame->SetChildWindow(SID_SIDEBAR, false /* create it */, true /* focus */);
919 
920  pViewFrame->ShowChildWindow(SID_SIDEBAR, true);
921 
922  // Force synchronous population of panels
923  SfxChildWindow *pChild = pViewFrame->GetChildWindow(SID_SIDEBAR);
924  if (!pChild)
925  return;
926 
927  auto pDockingWin = dynamic_cast<sfx2::sidebar::SidebarDockingWindow *>(pChild->GetWindow());
928  if (!pDockingWin)
929  return;
930 
931  OUString currentDeckId = pDockingWin->GetSidebarController()->GetCurrentDeckId();
932 
933  // check if it is the chart deck id, if it is, don't switch to default deck
934  bool switchToDefault = true;
935 
936  if (currentDeckId == "ChartDeck")
937  switchToDefault = false;
938 
939  if (!sidebarDeckId.empty())
940  {
941  pDockingWin->GetSidebarController()->SwitchToDeck(sidebarDeckId);
942  }
943  else
944  {
945  if (switchToDefault)
946  pDockingWin->GetSidebarController()->SwitchToDefaultDeck();
947  }
948 
949  pDockingWin->SyncUpdate();
950  }
951  else
952  SetLastExceptionMsg("No view shell or sidebar");
953 }
954 
955 void hideSidebar()
956 {
957  SfxViewShell* pViewShell = SfxViewShell::Current();
958  SfxViewFrame* pViewFrame = pViewShell? pViewShell->GetViewFrame(): nullptr;
959  if (pViewFrame)
960  pViewFrame->SetChildWindow(SID_SIDEBAR, false , false );
961  else
962  SetLastExceptionMsg("No view shell or sidebar");
963 }
964 
965 } // end anonymous namespace
966 
967 // Could be anonymous in principle, but for the unit testing purposes, we
968 // declare it in init.hxx.
969 OUString desktop::extractParameter(OUString& rOptions, std::u16string_view rName)
970 {
971  OUString aValue;
972 
973  OUString aNameEquals(OUString::Concat(rName) + "=");
974  OUString aCommaNameEquals(OUString::Concat(",") + rName + "=");
975 
976  int nIndex = -1;
977  if (rOptions.startsWith(aNameEquals))
978  {
979  size_t nLen = aNameEquals.getLength();
980  int nComma = rOptions.indexOf(",", nLen);
981  if (nComma >= 0)
982  {
983  aValue = rOptions.copy(nLen, nComma - nLen);
984  rOptions = rOptions.copy(nComma + 1);
985  }
986  else
987  {
988  aValue = rOptions.copy(nLen);
989  rOptions.clear();
990  }
991  }
992  else if ((nIndex = rOptions.indexOf(aCommaNameEquals)) >= 0)
993  {
994  size_t nLen = aCommaNameEquals.getLength();
995  int nComma = rOptions.indexOf(",", nIndex + nLen);
996  if (nComma >= 0)
997  {
998  aValue = rOptions.copy(nIndex + nLen, nComma - nIndex - nLen);
999  rOptions = OUString::Concat(rOptions.subView(0, nIndex)) + rOptions.subView(nComma);
1000  }
1001  else
1002  {
1003  aValue = rOptions.copy(nIndex + nLen);
1004  rOptions = rOptions.copy(0, nIndex);
1005  }
1006  }
1007 
1008  return aValue;
1009 }
1010 
1011 extern "C"
1012 {
1013 
1014 static void doc_destroy(LibreOfficeKitDocument* pThis);
1015 static int doc_saveAs(LibreOfficeKitDocument* pThis, const char* pUrl, const char* pFormat, const char* pFilterOptions);
1016 static int doc_getDocumentType(LibreOfficeKitDocument* pThis);
1017 static int doc_getParts(LibreOfficeKitDocument* pThis);
1018 static char* doc_getPartPageRectangles(LibreOfficeKitDocument* pThis);
1019 static int doc_getPart(LibreOfficeKitDocument* pThis);
1020 static void doc_setPart(LibreOfficeKitDocument* pThis, int nPart);
1021 static void doc_selectPart(LibreOfficeKitDocument* pThis, int nPart, int nSelect);
1022 static void doc_moveSelectedParts(LibreOfficeKitDocument* pThis, int nPosition, bool bDuplicate);
1023 static char* doc_getPartName(LibreOfficeKitDocument* pThis, int nPart);
1024 static void doc_setPartMode(LibreOfficeKitDocument* pThis, int nPartMode);
1025 static void doc_paintTile(LibreOfficeKitDocument* pThis,
1026  unsigned char* pBuffer,
1027  const int nCanvasWidth, const int nCanvasHeight,
1028  const int nTilePosX, const int nTilePosY,
1029  const int nTileWidth, const int nTileHeight);
1030 #ifdef IOS
1031 static void doc_paintTileToCGContext(LibreOfficeKitDocument* pThis,
1032  void* rCGContext,
1033  const int nCanvasWidth, const int nCanvasHeight,
1034  const int nTilePosX, const int nTilePosY,
1035  const int nTileWidth, const int nTileHeight);
1036 #endif
1037 static void doc_paintPartTile(LibreOfficeKitDocument* pThis,
1038  unsigned char* pBuffer,
1039  const int nPart,
1040  const int nCanvasWidth, const int nCanvasHeight,
1041  const int nTilePosX, const int nTilePosY,
1042  const int nTileWidth, const int nTileHeight);
1043 static int doc_getTileMode(LibreOfficeKitDocument* pThis);
1044 static void doc_getDocumentSize(LibreOfficeKitDocument* pThis,
1045  long* pWidth,
1046  long* pHeight);
1047 static void doc_initializeForRendering(LibreOfficeKitDocument* pThis,
1048  const char* pArguments);
1049 
1050 static void doc_registerCallback(LibreOfficeKitDocument* pThis,
1051  LibreOfficeKitCallback pCallback,
1052  void* pData);
1053 static void doc_postKeyEvent(LibreOfficeKitDocument* pThis,
1054  int nType,
1055  int nCharCode,
1056  int nKeyCode);
1057 static void doc_setFreemiumDenyList(LibreOfficeKitDocument* pThis,
1058  const char* freemiumDenyList);
1059 
1060 static void doc_setFreemiumView(LibreOfficeKitDocument* pThis,
1061  int nViewId,
1062  bool isFreemium);
1063 
1064 static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis,
1065  unsigned nWindowId,
1066  int nType,
1067  const char* pText);
1068 static void doc_removeTextContext(LibreOfficeKitDocument* pThis,
1069  unsigned nLOKWindowId,
1070  int nCharBefore,
1071  int nCharAfter);
1072 static void doc_sendDialogEvent(LibreOfficeKitDocument* pThis,
1073  unsigned long long int nLOKWindowId,
1074  const char* pArguments);
1075 static void doc_postWindowKeyEvent(LibreOfficeKitDocument* pThis,
1076  unsigned nLOKWindowId,
1077  int nType,
1078  int nCharCode,
1079  int nKeyCode);
1080 static void doc_postMouseEvent (LibreOfficeKitDocument* pThis,
1081  int nType,
1082  int nX,
1083  int nY,
1084  int nCount,
1085  int nButtons,
1086  int nModifier);
1087 static void doc_postWindowMouseEvent (LibreOfficeKitDocument* pThis,
1088  unsigned nLOKWindowId,
1089  int nType,
1090  int nX,
1091  int nY,
1092  int nCount,
1093  int nButtons,
1094  int nModifier);
1095 static void doc_postWindowGestureEvent(LibreOfficeKitDocument* pThis,
1096  unsigned nLOKWindowId,
1097  const char* pType,
1098  int nX,
1099  int nY,
1100  int nOffset);
1101 static void doc_postUnoCommand(LibreOfficeKitDocument* pThis,
1102  const char* pCommand,
1103  const char* pArguments,
1104  bool bNotifyWhenFinished);
1105 static void doc_setWindowTextSelection(LibreOfficeKitDocument* pThis,
1106  unsigned nLOKWindowId,
1107  bool swap,
1108  int nX,
1109  int nY);
1110 static void doc_setTextSelection (LibreOfficeKitDocument* pThis,
1111  int nType,
1112  int nX,
1113  int nY);
1114 static char* doc_getTextSelection(LibreOfficeKitDocument* pThis,
1115  const char* pMimeType,
1116  char** pUsedMimeType);
1117 static int doc_getSelectionType(LibreOfficeKitDocument* pThis);
1118 static int doc_getClipboard (LibreOfficeKitDocument* pThis,
1119  const char **pMimeTypes,
1120  size_t *pOutCount,
1121  char ***pOutMimeTypes,
1122  size_t **pOutSizes,
1123  char ***pOutStreams);
1124 static int doc_setClipboard (LibreOfficeKitDocument* pThis,
1125  const size_t nInCount,
1126  const char **pInMimeTypes,
1127  const size_t *pInSizes,
1128  const char **pInStreams);
1129 static bool doc_paste(LibreOfficeKitDocument* pThis,
1130  const char* pMimeType,
1131  const char* pData,
1132  size_t nSize);
1133 static void doc_setGraphicSelection (LibreOfficeKitDocument* pThis,
1134  int nType,
1135  int nX,
1136  int nY);
1137 static void doc_resetSelection (LibreOfficeKitDocument* pThis);
1138 static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCommand);
1139 static void doc_setClientZoom(LibreOfficeKitDocument* pThis,
1140  int nTilePixelWidth,
1141  int nTilePixelHeight,
1142  int nTileTwipWidth,
1143  int nTileTwipHeight);
1144 static void doc_setClientVisibleArea(LibreOfficeKitDocument* pThis, int nX, int nY, int nWidth, int nHeight);
1145 static void doc_setOutlineState(LibreOfficeKitDocument* pThis, bool bColumn, int nLevel, int nIndex, bool bHidden);
1146 static int doc_createView(LibreOfficeKitDocument* pThis);
1147 static int doc_createViewWithOptions(LibreOfficeKitDocument* pThis, const char* pOptions);
1148 static void doc_destroyView(LibreOfficeKitDocument* pThis, int nId);
1149 static void doc_setView(LibreOfficeKitDocument* pThis, int nId);
1150 static int doc_getView(LibreOfficeKitDocument* pThis);
1151 static int doc_getViewsCount(LibreOfficeKitDocument* pThis);
1152 static bool doc_getViewIds(LibreOfficeKitDocument* pThis, int* pArray, size_t nSize);
1153 static void doc_setViewLanguage(LibreOfficeKitDocument* pThis, int nId, const char* language);
1154 static unsigned char* doc_renderFontOrientation(LibreOfficeKitDocument* pThis,
1155  const char *pFontName,
1156  const char *pChar,
1157  int* pFontWidth,
1158  int* pFontHeight,
1159  int pOrientation);
1160 static unsigned char* doc_renderFont(LibreOfficeKitDocument* pThis,
1161  const char *pFontName,
1162  const char *pChar,
1163  int* pFontWidth,
1164  int* pFontHeight);
1165 static char* doc_getPartHash(LibreOfficeKitDocument* pThis, int nPart);
1166 
1167 static void doc_paintWindow(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId, unsigned char* pBuffer,
1168  const int nX, const int nY,
1169  const int nWidth, const int nHeight);
1170 
1171 static void doc_paintWindowDPI(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId, unsigned char* pBuffer,
1172  const int nX, const int nY,
1173  const int nWidth, const int nHeight,
1174  const double fDPIScale);
1175 
1176 static void doc_paintWindowForView(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId, unsigned char* pBuffer,
1177  const int nX, const int nY,
1178  const int nWidth, const int nHeight,
1179  const double fDPIScale, int viewId);
1180 
1181 static void doc_postWindow(LibreOfficeKitDocument* pThis, unsigned
1182  nLOKWindowId, int nAction, const char* pData);
1183 
1184 static char* doc_getPartInfo(LibreOfficeKitDocument* pThis, int nPart);
1185 
1186 static bool doc_insertCertificate(LibreOfficeKitDocument* pThis,
1187  const unsigned char* pCertificateBinary,
1188  const int nCertificateBinarySize,
1189  const unsigned char* pPrivateKeyBinary,
1190  const int nPrivateKeyBinarySize);
1191 
1192 static bool doc_addCertificate(LibreOfficeKitDocument* pThis,
1193  const unsigned char* pCertificateBinary,
1194  const int nCertificateBinarySize);
1195 
1196 static int doc_getSignatureState(LibreOfficeKitDocument* pThis);
1197 
1198 static size_t doc_renderShapeSelection(LibreOfficeKitDocument* pThis, char** pOutput);
1199 
1200 static void doc_resizeWindow(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId,
1201  const int nWidth, const int nHeight);
1202 
1203 static void doc_completeFunction(LibreOfficeKitDocument* pThis, const char*);
1204 
1205 
1206 static void doc_sendFormFieldEvent(LibreOfficeKitDocument* pThis,
1207  const char* pArguments);
1208 
1209 static bool doc_renderSearchResult(LibreOfficeKitDocument* pThis,
1210  const char* pSearchResult, unsigned char** pBitmapBuffer,
1211  int* pWidth, int* pHeight, size_t* pByteSize);
1212 
1213 } // extern "C"
1214 
1215 namespace {
1216 ITiledRenderable* getTiledRenderable(LibreOfficeKitDocument* pThis)
1217 {
1218  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
1219  return dynamic_cast<ITiledRenderable*>(pDocument->mxComponent.get());
1220 }
1221 
1222 #ifndef IOS
1223 
1224 /*
1225  * Unfortunately clipboard creation using UNO is insanely baroque.
1226  * we also need to ensure that this works for the first view which
1227  * has no clear 'createView' called for it (unfortunately).
1228  */
1229 rtl::Reference<LOKClipboard> forceSetClipboardForCurrentView(LibreOfficeKitDocument *pThis)
1230 {
1231  ITiledRenderable* pDoc = getTiledRenderable(pThis);
1233 
1234  SAL_INFO("lok", "Set to clipboard for view " << xClip.get());
1235  // FIXME: using a hammer here - should not be necessary if all tests used createView.
1236  pDoc->setClipboard(uno::Reference<datatransfer::clipboard::XClipboard>(xClip->getXI(), UNO_QUERY));
1237 
1238  return xClip;
1239 }
1240 
1241 #endif
1242 
1243 const vcl::Font* FindFont(const OUString& rFontName)
1244 {
1246  const SvxFontListItem* pFonts
1247  = static_cast<const SvxFontListItem*>(pDocSh->GetItem(SID_ATTR_CHAR_FONTLIST));
1248  const FontList* pList = pFonts ? pFonts->GetFontList() : nullptr;
1249  if (pList && !rFontName.isEmpty())
1250  if (sal_Handle hMetric = pList->GetFirstFontMetric(rFontName))
1251  return &FontList::GetFontMetric(hMetric);
1252  return nullptr;
1253 }
1254 
1255 vcl::Font FindFont_FallbackToDefault(const OUString& rFontName)
1256 {
1257  if (auto pFound = FindFont(rFontName))
1258  return *pFound;
1259 
1260  return OutputDevice::GetDefaultFont(DefaultFontType::SANS_UNICODE, LANGUAGE_NONE,
1261  GetDefaultFontFlags::NONE);
1262 }
1263 } // anonymous namespace
1264 
1265 LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent, int nDocumentId)
1266  : mxComponent(xComponent)
1267  , mnDocumentId(nDocumentId)
1268 {
1269  assert(nDocumentId != -1 && "Cannot set mnDocumentId to -1");
1270 
1272  if (!m_pDocumentClass)
1273  {
1274  m_pDocumentClass = std::make_shared<LibreOfficeKitDocumentClass>();
1275 
1276  m_pDocumentClass->nSize = sizeof(LibreOfficeKitDocumentClass);
1277 
1278  m_pDocumentClass->destroy = doc_destroy;
1279  m_pDocumentClass->saveAs = doc_saveAs;
1280  m_pDocumentClass->getDocumentType = doc_getDocumentType;
1281  m_pDocumentClass->getParts = doc_getParts;
1282  m_pDocumentClass->getPartPageRectangles = doc_getPartPageRectangles;
1283  m_pDocumentClass->getPart = doc_getPart;
1284  m_pDocumentClass->setPart = doc_setPart;
1285  m_pDocumentClass->selectPart = doc_selectPart;
1286  m_pDocumentClass->moveSelectedParts = doc_moveSelectedParts;
1287  m_pDocumentClass->getPartName = doc_getPartName;
1288  m_pDocumentClass->setPartMode = doc_setPartMode;
1289  m_pDocumentClass->paintTile = doc_paintTile;
1290 #ifdef IOS
1291  m_pDocumentClass->paintTileToCGContext = doc_paintTileToCGContext;
1292 #endif
1293  m_pDocumentClass->paintPartTile = doc_paintPartTile;
1294  m_pDocumentClass->getTileMode = doc_getTileMode;
1295  m_pDocumentClass->getDocumentSize = doc_getDocumentSize;
1296  m_pDocumentClass->initializeForRendering = doc_initializeForRendering;
1297  m_pDocumentClass->registerCallback = doc_registerCallback;
1298  m_pDocumentClass->postKeyEvent = doc_postKeyEvent;
1299  m_pDocumentClass->postWindowExtTextInputEvent = doc_postWindowExtTextInputEvent;
1300  m_pDocumentClass->removeTextContext = doc_removeTextContext;
1301  m_pDocumentClass->postWindowKeyEvent = doc_postWindowKeyEvent;
1302  m_pDocumentClass->postMouseEvent = doc_postMouseEvent;
1303  m_pDocumentClass->postWindowMouseEvent = doc_postWindowMouseEvent;
1304  m_pDocumentClass->sendDialogEvent = doc_sendDialogEvent;
1305  m_pDocumentClass->postUnoCommand = doc_postUnoCommand;
1306  m_pDocumentClass->setTextSelection = doc_setTextSelection;
1307  m_pDocumentClass->setWindowTextSelection = doc_setWindowTextSelection;
1308  m_pDocumentClass->getTextSelection = doc_getTextSelection;
1309  m_pDocumentClass->getSelectionType = doc_getSelectionType;
1310  m_pDocumentClass->getClipboard = doc_getClipboard;
1311  m_pDocumentClass->setClipboard = doc_setClipboard;
1312  m_pDocumentClass->paste = doc_paste;
1313  m_pDocumentClass->setGraphicSelection = doc_setGraphicSelection;
1314  m_pDocumentClass->resetSelection = doc_resetSelection;
1315  m_pDocumentClass->getCommandValues = doc_getCommandValues;
1316  m_pDocumentClass->setClientZoom = doc_setClientZoom;
1317  m_pDocumentClass->setClientVisibleArea = doc_setClientVisibleArea;
1318  m_pDocumentClass->setOutlineState = doc_setOutlineState;
1319 
1320  m_pDocumentClass->createView = doc_createView;
1321  m_pDocumentClass->destroyView = doc_destroyView;
1322  m_pDocumentClass->setView = doc_setView;
1323  m_pDocumentClass->getView = doc_getView;
1324  m_pDocumentClass->getViewsCount = doc_getViewsCount;
1325  m_pDocumentClass->getViewIds = doc_getViewIds;
1326 
1327  m_pDocumentClass->renderFont = doc_renderFont;
1328  m_pDocumentClass->renderFontOrientation = doc_renderFontOrientation;
1329  m_pDocumentClass->getPartHash = doc_getPartHash;
1330 
1331  m_pDocumentClass->paintWindow = doc_paintWindow;
1332  m_pDocumentClass->paintWindowDPI = doc_paintWindowDPI;
1333  m_pDocumentClass->paintWindowForView = doc_paintWindowForView;
1334  m_pDocumentClass->postWindow = doc_postWindow;
1335  m_pDocumentClass->resizeWindow = doc_resizeWindow;
1336 
1337  m_pDocumentClass->setViewLanguage = doc_setViewLanguage;
1338 
1339  m_pDocumentClass->getPartInfo = doc_getPartInfo;
1340 
1341  m_pDocumentClass->insertCertificate = doc_insertCertificate;
1342  m_pDocumentClass->addCertificate = doc_addCertificate;
1343  m_pDocumentClass->getSignatureState = doc_getSignatureState;
1344 
1345  m_pDocumentClass->renderShapeSelection = doc_renderShapeSelection;
1346  m_pDocumentClass->postWindowGestureEvent = doc_postWindowGestureEvent;
1347 
1348  m_pDocumentClass->createViewWithOptions = doc_createViewWithOptions;
1349  m_pDocumentClass->completeFunction = doc_completeFunction;
1350 
1351  m_pDocumentClass->sendFormFieldEvent = doc_sendFormFieldEvent;
1352  m_pDocumentClass->renderSearchResult = doc_renderSearchResult;
1353 
1354  m_pDocumentClass->setFreemiumDenyList = doc_setFreemiumDenyList;
1355  m_pDocumentClass->setFreemiumView = doc_setFreemiumView;
1356 
1358  }
1359  pClass = m_pDocumentClass.get();
1360 
1361 #ifndef IOS
1362  forceSetClipboardForCurrentView(this);
1363 #endif
1364 }
1365 
1367 {
1368  try
1369  {
1370  mxComponent->dispose();
1371  }
1372  catch (const css::lang::DisposedException&)
1373  {
1374  TOOLS_WARN_EXCEPTION("lok", "failed to dispose document");
1375  }
1376 }
1377 
1378 static OUString getGenerator()
1379 {
1380  OUString sGenerator(
1381  Translate::ExpandVariables("%PRODUCTNAME %PRODUCTVERSION%PRODUCTEXTENSION (%1)"));
1382  OUString os("$_OS");
1383  ::rtl::Bootstrap::expandMacros(os);
1384  return sGenerator.replaceFirst("%1", os);
1385 }
1386 
1387 extern "C" {
1388 
1389 CallbackFlushHandler::CallbackFlushHandler(LibreOfficeKitDocument* pDocument, LibreOfficeKitCallback pCallback, void* pData)
1390  : Idle( "lokit timer callback" ),
1391  m_pDocument(pDocument),
1392  m_pCallback(pCallback),
1393  m_pData(pData),
1394  m_nDisableCallbacks(0)
1395 {
1396  SetPriority(TaskPriority::POST_PAINT);
1397 
1398  // Add the states that are safe to skip duplicates on, even when
1399  // not consequent (i.e. do no emit them if unchanged from last).
1400  m_states.emplace(LOK_CALLBACK_TEXT_SELECTION, "NIL");
1401  m_states.emplace(LOK_CALLBACK_GRAPHIC_SELECTION, "NIL");
1402  m_states.emplace(LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR, "NIL");
1403  m_states.emplace(LOK_CALLBACK_STATE_CHANGED, "NIL");
1404  m_states.emplace(LOK_CALLBACK_MOUSE_POINTER, "NIL");
1405  m_states.emplace(LOK_CALLBACK_CELL_CURSOR, "NIL");
1406  m_states.emplace(LOK_CALLBACK_CELL_FORMULA, "NIL");
1407  m_states.emplace(LOK_CALLBACK_CELL_ADDRESS, "NIL");
1408  m_states.emplace(LOK_CALLBACK_CURSOR_VISIBLE, "NIL");
1409  m_states.emplace(LOK_CALLBACK_SET_PART, "NIL");
1410  m_states.emplace(LOK_CALLBACK_TABLE_SELECTED, "NIL");
1411 
1412  Start();
1413 }
1414 
1416 {
1417  Stop();
1418 }
1419 
1420 void CallbackFlushHandler::callback(const int type, const char* payload, void* data)
1421 {
1422  CallbackFlushHandler* self = static_cast<CallbackFlushHandler*>(data);
1423  if (self)
1424  {
1425  self->queue(type, payload);
1426  }
1427 }
1428 
1429 CallbackFlushHandler::queue_type2::reverse_iterator CallbackFlushHandler::toQueue2(CallbackFlushHandler::queue_type1::reverse_iterator pos)
1430 {
1431  int delta = std::distance(m_queue1.rbegin(), pos);
1432  return m_queue2.rbegin() + delta;
1433 }
1434 
1435 void CallbackFlushHandler::queue(const int type, const char* data)
1436 {
1437  comphelper::ProfileZone aZone("CallbackFlushHandler::queue");
1438 
1439  CallbackData aCallbackData(data ? data : "(nil)");
1440  const std::string& payload = aCallbackData.PayloadString;
1441  SAL_INFO("lok", "Queue: [" << type << "]: [" << payload << "] on " << m_queue1.size() << " entries.");
1442 
1443  bool bIsChartActive = false;
1444  if (type == LOK_CALLBACK_GRAPHIC_SELECTION)
1445  {
1446  LokChartHelper aChartHelper(SfxViewShell::Current());
1447  bIsChartActive = aChartHelper.GetWindow() != nullptr;
1448  }
1449 
1450  if (callbacksDisabled() && !bIsChartActive)
1451  {
1452  // We drop notifications when this is set, except for important ones.
1453  // When we issue a complex command (such as .uno:InsertAnnotation)
1454  // there will be multiple notifications. On the first invalidation
1455  // we will start painting, but other events will get fired
1456  // while the complex command in question executes.
1457  // We don't want to suppress everything here on the wrong assumption
1458  // that no new events are fired during painting.
1459  if (type != LOK_CALLBACK_STATE_CHANGED &&
1460  type != LOK_CALLBACK_INVALIDATE_TILES &&
1461  type != LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR &&
1462  type != LOK_CALLBACK_CURSOR_VISIBLE &&
1463  type != LOK_CALLBACK_VIEW_CURSOR_VISIBLE &&
1464  type != LOK_CALLBACK_TEXT_SELECTION &&
1465  type != LOK_CALLBACK_TEXT_SELECTION_START &&
1466  type != LOK_CALLBACK_TEXT_SELECTION_END &&
1467  type != LOK_CALLBACK_REFERENCE_MARKS)
1468  {
1469  SAL_INFO("lok", "Skipping while painting [" << type << "]: [" << payload << "].");
1470  return;
1471  }
1472 
1473  // In Writer we drop all notifications during painting.
1474  if (doc_getDocumentType(m_pDocument) == LOK_DOCTYPE_TEXT)
1475  return;
1476  }
1477 
1478  // Suppress invalid payloads.
1479  if (type == LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR &&
1480  payload.find(", 0, 0, ") != std::string::npos &&
1481  payload.find("\"hyperlink\":\"\"") == std::string::npos &&
1482  payload.find("\"hyperlink\": {}") == std::string::npos)
1483  {
1484  // The cursor position is often the relative coordinates of the widget
1485  // issuing it, instead of the absolute one that we expect.
1486  // This is temporary however, and, once the control is created and initialized
1487  // correctly, it eventually emits the correct absolute coordinates.
1488  SAL_INFO("lok", "Skipping invalid event [" << type << "]: [" << payload << "].");
1489  return;
1490  }
1491 
1492  std::unique_lock<std::mutex> lock(m_mutex);
1493 
1494  // drop duplicate callbacks for the listed types
1495  switch (type)
1496  {
1497  case LOK_CALLBACK_TEXT_SELECTION_START:
1498  case LOK_CALLBACK_TEXT_SELECTION_END:
1499  case LOK_CALLBACK_TEXT_SELECTION:
1500  case LOK_CALLBACK_GRAPHIC_SELECTION:
1501  case LOK_CALLBACK_GRAPHIC_VIEW_SELECTION:
1502  case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
1503  case LOK_CALLBACK_INVALIDATE_VIEW_CURSOR:
1504  case LOK_CALLBACK_STATE_CHANGED:
1505  case LOK_CALLBACK_MOUSE_POINTER:
1506  case LOK_CALLBACK_CELL_CURSOR:
1507  case LOK_CALLBACK_CELL_VIEW_CURSOR:
1508  case LOK_CALLBACK_CELL_FORMULA:
1509  case LOK_CALLBACK_CELL_ADDRESS:
1510  case LOK_CALLBACK_CELL_SELECTION_AREA:
1511  case LOK_CALLBACK_CURSOR_VISIBLE:
1512  case LOK_CALLBACK_VIEW_CURSOR_VISIBLE:
1513  case LOK_CALLBACK_SET_PART:
1514  case LOK_CALLBACK_TEXT_VIEW_SELECTION:
1515  case LOK_CALLBACK_INVALIDATE_HEADER:
1516  case LOK_CALLBACK_WINDOW:
1517  case LOK_CALLBACK_CALC_FUNCTION_LIST:
1518  case LOK_CALLBACK_INVALIDATE_SHEET_GEOMETRY:
1519  {
1520  const auto& pos = std::find_if(m_queue1.rbegin(), m_queue1.rend(),
1521  [type] (int elemType) { return (elemType == type); });
1522  auto pos2 = toQueue2(pos);
1523  if (pos != m_queue1.rend() && pos2->PayloadString == payload)
1524  {
1525  SAL_INFO("lok", "Skipping queue duplicate [" << type << + "]: [" << payload << "].");
1526  return;
1527  }
1528  }
1529  break;
1530  }
1531 
1532  if (type == LOK_CALLBACK_TEXT_SELECTION && payload.empty())
1533  {
1534  const auto& posStart = std::find_if(m_queue1.rbegin(), m_queue1.rend(),
1535  [] (int elemType) { return (elemType == LOK_CALLBACK_TEXT_SELECTION_START); });
1536  auto posStart2 = toQueue2(posStart);
1537  if (posStart != m_queue1.rend())
1538  posStart2->PayloadString.clear();
1539 
1540  const auto& posEnd = std::find_if(m_queue1.rbegin(), m_queue1.rend(),
1541  [] (int elemType) { return (elemType == LOK_CALLBACK_TEXT_SELECTION_END); });
1542  auto posEnd2 = toQueue2(posEnd);
1543  if (posEnd != m_queue1.rend())
1544  posEnd2->PayloadString.clear();
1545  }
1546 
1547  // When payload is empty discards any previous state.
1548  if (payload.empty())
1549  {
1550  switch (type)
1551  {
1552  case LOK_CALLBACK_TEXT_SELECTION_START:
1553  case LOK_CALLBACK_TEXT_SELECTION_END:
1554  case LOK_CALLBACK_TEXT_SELECTION:
1555  case LOK_CALLBACK_GRAPHIC_SELECTION:
1556  case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
1557  case LOK_CALLBACK_INVALIDATE_TILES:
1558  if (removeAll(
1559  [type](int elemType, const CallbackData&) { return (elemType == type); }))
1560  SAL_INFO("lok", "Removed dups of [" << type << "]: [" << payload << "].");
1561  break;
1562  }
1563  }
1564  else
1565  {
1566  switch (type)
1567  {
1568  // These are safe to use the latest state and ignore previous
1569  // ones (if any) since the last overrides previous ones.
1570  case LOK_CALLBACK_TEXT_SELECTION_START:
1571  case LOK_CALLBACK_TEXT_SELECTION_END:
1572  case LOK_CALLBACK_TEXT_SELECTION:
1573  case LOK_CALLBACK_MOUSE_POINTER:
1574  case LOK_CALLBACK_CELL_CURSOR:
1575  case LOK_CALLBACK_CELL_FORMULA:
1576  case LOK_CALLBACK_CELL_ADDRESS:
1577  case LOK_CALLBACK_CURSOR_VISIBLE:
1578  case LOK_CALLBACK_SET_PART:
1579  case LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE:
1580  case LOK_CALLBACK_RULER_UPDATE:
1581  {
1582  if (removeAll(
1583  [type](int elemType, const CallbackData&) { return (elemType == type); }))
1584  SAL_INFO("lok", "Removed dups of [" << type << "]: [" << payload << "].");
1585  }
1586  break;
1587 
1588  // These are safe to use the latest state and ignore previous
1589  // ones (if any) since the last overrides previous ones,
1590  // but only if the view is the same.
1591  case LOK_CALLBACK_CELL_VIEW_CURSOR:
1592  case LOK_CALLBACK_GRAPHIC_VIEW_SELECTION:
1593  case LOK_CALLBACK_INVALIDATE_VIEW_CURSOR:
1594  case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
1595  case LOK_CALLBACK_TEXT_VIEW_SELECTION:
1596  case LOK_CALLBACK_VIEW_CURSOR_VISIBLE:
1597  case LOK_CALLBACK_CALC_FUNCTION_LIST:
1598  case LOK_CALLBACK_FORM_FIELD_BUTTON:
1599  {
1600  // deleting the duplicate of visible cursor message can cause hyperlink popup not to show up on second/or more click on the same place.
1601  // If the hyperlink is not empty we can bypass that to show the popup
1602  const bool hyperLinkException = type == LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR &&
1603  payload.find("\"hyperlink\":\"\"") == std::string::npos &&
1604  payload.find("\"hyperlink\": {}") == std::string::npos;
1605  const int nViewId = lcl_getViewId(payload);
1606  removeAll(
1607  [type, nViewId, hyperLinkException] (int elemType, const CallbackData& elemData) {
1608  return (elemType == type && nViewId == lcl_getViewId(elemData) && !hyperLinkException);
1609  }
1610  );
1611  }
1612  break;
1613 
1614  case LOK_CALLBACK_INVALIDATE_TILES:
1615  if (processInvalidateTilesEvent(type, aCallbackData))
1616  return;
1617  break;
1618 
1619  // State changes with same name override previous ones with a different value.
1620  // Ex. ".uno:PageStatus=Slide 20 of 83" overwrites any previous PageStatus.
1621  case LOK_CALLBACK_STATE_CHANGED:
1622  {
1623  // Compare the state name=value and overwrite earlier entries with same name.
1624  const auto pos = payload.find('=');
1625  if (pos != std::string::npos)
1626  {
1627  const std::string name = payload.substr(0, pos + 1);
1628  // This is needed because otherwise it creates some problems when
1629  // a save occurs while a cell is still edited in Calc.
1630  if (name != ".uno:ModifiedStatus=")
1631  {
1632  removeAll(
1633  [type, &name] (int elemType, const CallbackData& elemData) {
1634  return (elemType == type) && (elemData.PayloadString.compare(0, name.size(), name) == 0);
1635  }
1636  );
1637  }
1638  }
1639  }
1640  break;
1641 
1642  case LOK_CALLBACK_WINDOW:
1643  if (processWindowEvent(type, aCallbackData))
1644  return;
1645  break;
1646 
1647  case LOK_CALLBACK_GRAPHIC_SELECTION:
1648  {
1649  // remove only selection ranges and 'EMPTY' messages
1650  // always send 'INPLACE' and 'INPLACE EXIT' messages
1651  removeAll([type, payload] (int elemType, const CallbackData& elemData)
1652  { return (elemType == type && elemData.PayloadString[0] != 'I'); });
1653  }
1654  break;
1655  }
1656  }
1657 
1658  // Validate that the cached data and the payload string are identical.
1659  assert(aCallbackData.validate() && "Cached callback payload object and string mismatch!");
1660  m_queue1.emplace_back(type);
1661  m_queue2.emplace_back(aCallbackData);
1662  SAL_INFO("lok", "Queued #" << (m_queue1.size() - 1) <<
1663  " [" << type << "]: [" << payload << "] to have " << m_queue1.size() << " entries.");
1664 
1665 #ifdef DBG_UTIL
1666  {
1667  // Dump the queue state and validate cached data.
1668  int i = 1;
1669  std::ostringstream oss;
1670  if (m_queue1.empty())
1671  oss << "Empty";
1672  else
1673  oss << m_queue1.size() << " items\n";
1674  auto it1 = m_queue1.begin();
1675  auto it2 = m_queue2.begin();
1676  for (; it1 != m_queue1.end(); ++it1, ++it2)
1677  oss << i++ << ": [" << *it1 << "] [" << it2->PayloadString << "].\n";
1678  SAL_INFO("lok", "Current Queue: " << oss.str());
1679  assert(
1680  std::all_of(
1681  m_queue2.begin(), m_queue2.end(),
1682  [](const CallbackData& c) { return c.validate(); }));
1683  }
1684 #endif
1685 
1686  lock.unlock();
1687  if (!IsActive())
1688  {
1689  Start();
1690  }
1691 }
1692 
1694 {
1695  const std::string& payload = aCallbackData.PayloadString;
1696 
1697  RectangleAndPart& rcNew = aCallbackData.setRectangleAndPart(payload);
1698  if (rcNew.isEmpty())
1699  {
1700  SAL_INFO("lok", "Skipping invalid event [" << type << "]: [" << payload << "].");
1701  return true;
1702  }
1703 
1704  // If we have to invalidate all tiles, we can skip any new tile invalidation.
1705  // Find the last INVALIDATE_TILES entry, if any to see if it's invalidate-all.
1706  const auto& pos
1707  = std::find_if(m_queue1.rbegin(), m_queue1.rend(), [](int elemType) {
1708  return (elemType == LOK_CALLBACK_INVALIDATE_TILES);
1709  });
1710  if (pos != m_queue1.rend())
1711  {
1712  auto pos2 = toQueue2(pos);
1713  const RectangleAndPart& rcOld = pos2->getRectangleAndPart();
1714  if (rcOld.isInfinite() && (rcOld.m_nPart == -1 || rcOld.m_nPart == rcNew.m_nPart))
1715  {
1716  SAL_INFO("lok", "Skipping queue [" << type << "]: [" << payload
1717  << "] since all tiles need to be invalidated.");
1718  return true;
1719  }
1720 
1721  if (rcOld.m_nPart == -1 || rcOld.m_nPart == rcNew.m_nPart)
1722  {
1723  // If fully overlapping.
1724  if (rcOld.m_aRectangle.IsInside(rcNew.m_aRectangle))
1725  {
1726  SAL_INFO("lok", "Skipping queue [" << type << "]: [" << payload
1727  << "] since overlaps existing all-parts.");
1728  return true;
1729  }
1730  }
1731  }
1732 
1733  if (rcNew.isInfinite())
1734  {
1735  SAL_INFO("lok", "Have Empty [" << type << "]: [" << payload
1736  << "] so removing all with part " << rcNew.m_nPart << ".");
1737  removeAll([&rcNew](int elemType, const CallbackData& elemData) {
1738  if (elemType == LOK_CALLBACK_INVALIDATE_TILES)
1739  {
1740  // Remove exiting if new is all-encompassing, or if of the same part.
1742  return (rcNew.m_nPart == -1 || rcOld.m_nPart == rcNew.m_nPart);
1743  }
1744 
1745  // Keep others.
1746  return false;
1747  });
1748  }
1749  else
1750  {
1751  const auto rcOrig = rcNew;
1752 
1753  SAL_INFO("lok", "Have [" << type << "]: [" << payload << "] so merging overlapping.");
1754  removeAll([&rcNew](int elemType, const CallbackData& elemData) {
1755  if (elemType == LOK_CALLBACK_INVALIDATE_TILES)
1756  {
1757  const RectangleAndPart& rcOld = elemData.getRectangleAndPart();
1758  if (rcNew.m_nPart != -1 && rcOld.m_nPart != -1 && rcOld.m_nPart != rcNew.m_nPart)
1759  {
1760  SAL_INFO("lok", "Nothing to merge between new: "
1761  << rcNew.toString() << ", and old: " << rcOld.toString());
1762  return false;
1763  }
1764 
1765  if (rcNew.m_nPart == -1)
1766  {
1767  // Don't merge unless fully overlapped.
1768  SAL_INFO("lok", "New " << rcNew.toString() << " has " << rcOld.toString()
1769  << "?");
1770  if (rcNew.m_aRectangle.IsInside(rcOld.m_aRectangle))
1771  {
1772  SAL_INFO("lok", "New " << rcNew.toString() << " engulfs old "
1773  << rcOld.toString() << ".");
1774  return true;
1775  }
1776  }
1777  else if (rcOld.m_nPart == -1)
1778  {
1779  // Don't merge unless fully overlapped.
1780  SAL_INFO("lok", "Old " << rcOld.toString() << " has " << rcNew.toString()
1781  << "?");
1782  if (rcOld.m_aRectangle.IsInside(rcNew.m_aRectangle))
1783  {
1784  SAL_INFO("lok", "New " << rcNew.toString() << " engulfs old "
1785  << rcOld.toString() << ".");
1786  return true;
1787  }
1788  }
1789  else
1790  {
1791  const tools::Rectangle rcOverlap
1792  = rcNew.m_aRectangle.GetIntersection(rcOld.m_aRectangle);
1793  const bool bOverlap = !rcOverlap.IsEmpty();
1794  SAL_INFO("lok", "Merging " << rcNew.toString() << " & " << rcOld.toString()
1795  << " => " << rcOverlap.toString()
1796  << " Overlap: " << bOverlap);
1797  if (bOverlap)
1798  {
1799  rcNew.m_aRectangle.Union(rcOld.m_aRectangle);
1800  SAL_INFO("lok", "Merged: " << rcNew.toString());
1801  return true;
1802  }
1803  }
1804  }
1805 
1806  // Keep others.
1807  return false;
1808  });
1809 
1810  if (rcNew.m_aRectangle != rcOrig.m_aRectangle)
1811  {
1812  SAL_INFO("lok", "Replacing: " << rcOrig.toString() << " by " << rcNew.toString());
1813  if (rcNew.m_aRectangle.GetWidth() < rcOrig.m_aRectangle.GetWidth()
1814  || rcNew.m_aRectangle.GetHeight() < rcOrig.m_aRectangle.GetHeight())
1815  {
1816  SAL_WARN("lok", "Error: merged rect smaller.");
1817  }
1818  }
1819  }
1820 
1821  aCallbackData.setRectangleAndPart(rcNew);
1822  // Queue this one.
1823  return false;
1824 }
1825 
1827 {
1828  const std::string& payload = aCallbackData.PayloadString;
1829 
1830  boost::property_tree::ptree& aTree = aCallbackData.setJson(payload);
1831  const unsigned nLOKWindowId = aTree.get<unsigned>("id", 0);
1832  const std::string aAction = aTree.get<std::string>("action", "");
1833  if (aAction == "invalidate")
1834  {
1835  std::string aRectStr = aTree.get<std::string>("rectangle", "");
1836  // no 'rectangle' field => invalidate all of the window =>
1837  // remove all previous window part invalidations
1838  if (aRectStr.empty())
1839  {
1840  removeAll([&nLOKWindowId](int elemType, const CallbackData& elemData) {
1841  if (elemType == LOK_CALLBACK_WINDOW)
1842  {
1843  const boost::property_tree::ptree& aOldTree = elemData.getJson();
1844  if (nLOKWindowId == aOldTree.get<unsigned>("id", 0)
1845  && aOldTree.get<std::string>("action", "") == "invalidate")
1846  {
1847  return true;
1848  }
1849  }
1850  return false;
1851  });
1852  }
1853  else
1854  {
1855  // if we have to invalidate all of the window, ignore
1856  // any part invalidation message
1857  bool invAllExist = false;
1858  auto it1 = m_queue1.rbegin();
1859  auto it2 = m_queue2.rbegin();
1860  for (;it1 != m_queue1.rend(); ++it1, ++it2)
1861  {
1862  if (*it1 != LOK_CALLBACK_WINDOW)
1863  continue;
1864  const boost::property_tree::ptree& aOldTree = it2->getJson();
1865  if (nLOKWindowId == aOldTree.get<unsigned>("id", 0)
1866  && aOldTree.get<std::string>("action", "") == "invalidate"
1867  && aOldTree.get<std::string>("rectangle", "").empty())
1868  {
1869  invAllExist = true;
1870  break;
1871  }
1872  }
1873 
1874  // we found a invalidate-all window callback
1875  if (invAllExist)
1876  {
1877  SAL_INFO("lok.dialog", "Skipping queue ["
1878  << type << "]: [" << payload
1879  << "] since whole window needs to be invalidated.");
1880  return true;
1881  }
1882 
1883  std::istringstream aRectStream(aRectStr);
1884  tools::Long nLeft, nTop, nWidth, nHeight;
1885  char nComma;
1886  aRectStream >> nLeft >> nComma >> nTop >> nComma >> nWidth >> nComma >> nHeight;
1887  tools::Rectangle aNewRect(nLeft, nTop, nLeft + nWidth, nTop + nHeight);
1888  bool currentIsRedundant = false;
1889  removeAll([&aNewRect, &nLOKWindowId,
1890  &currentIsRedundant](int elemType, const CallbackData& elemData) {
1891  if (elemType != LOK_CALLBACK_WINDOW)
1892  return false;
1893 
1894  const boost::property_tree::ptree& aOldTree = elemData.getJson();
1895  if (aOldTree.get<std::string>("action", "") == "invalidate")
1896  {
1897  // Not possible that we encounter an empty rectangle here; we already handled this case above.
1898  std::istringstream aOldRectStream(aOldTree.get<std::string>("rectangle", ""));
1899  tools::Long nOldLeft, nOldTop, nOldWidth, nOldHeight;
1900  char nOldComma;
1901  aOldRectStream >> nOldLeft >> nOldComma >> nOldTop >> nOldComma >> nOldWidth
1902  >> nOldComma >> nOldHeight;
1903  const tools::Rectangle aOldRect = tools::Rectangle(
1904  nOldLeft, nOldTop, nOldLeft + nOldWidth, nOldTop + nOldHeight);
1905 
1906  if (nLOKWindowId == aOldTree.get<unsigned>("id", 0))
1907  {
1908  if (aNewRect == aOldRect)
1909  {
1910  SAL_INFO("lok.dialog", "Duplicate rect [" << aNewRect.toString()
1911  << "]. Skipping new.");
1912  // We have a rectangle in the queue already that makes the current Callback useless.
1913  currentIsRedundant = true;
1914  return false;
1915  }
1916  // new one engulfs the old one?
1917  else if (aNewRect.IsInside(aOldRect))
1918  {
1919  SAL_INFO("lok.dialog",
1920  "New rect [" << aNewRect.toString() << "] engulfs old ["
1921  << aOldRect.toString() << "]. Replacing old.");
1922  return true;
1923  }
1924  // old one engulfs the new one?
1925  else if (aOldRect.IsInside(aNewRect))
1926  {
1927  SAL_INFO("lok.dialog",
1928  "Old rect [" << aOldRect.toString() << "] engulfs new ["
1929  << aNewRect.toString() << "]. Skipping new.");
1930  // We have a rectangle in the queue already that makes the current Callback useless.
1931  currentIsRedundant = true;
1932  return false;
1933  }
1934  else
1935  {
1936  // Overlapping rects.
1937  const tools::Rectangle aPreMergeRect = aNewRect;
1938  aNewRect.Union(aOldRect);
1939  SAL_INFO("lok.dialog", "Merging rects ["
1940  << aPreMergeRect.toString() << "] & ["
1941  << aOldRect.toString() << "] = ["
1942  << aNewRect.toString()
1943  << "]. Replacing old.");
1944  return true;
1945  }
1946  }
1947  }
1948 
1949  // keep rest
1950  return false;
1951  });
1952 
1953  // Do not enqueue if redundant.
1954  if (currentIsRedundant)
1955  return true;
1956 
1957  aTree.put("rectangle", aNewRect.toString().getStr());
1958  aCallbackData.setJson(aTree);
1959  assert(aCallbackData.validate() && "Validation after setJson failed!");
1960  }
1961  }
1962  else if (aAction == "created")
1963  {
1964  // Remove all previous actions on same dialog, if we are creating it anew.
1965  removeAll([&nLOKWindowId](int elemType, const CallbackData& elemData) {
1966  if (elemType == LOK_CALLBACK_WINDOW)
1967  {
1968  const boost::property_tree::ptree& aOldTree = elemData.getJson();
1969  if (nLOKWindowId == aOldTree.get<unsigned>("id", 0))
1970  return true;
1971  }
1972  return false;
1973  });
1974 
1975  VclPtr<Window> pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
1976  if (!pWindow)
1977  {
1978  gImpl->maLastExceptionMsg = "Document doesn't support dialog rendering, or window not found.";
1979  return false;
1980  }
1981 
1982 #ifndef IOS
1983  auto xClip = forceSetClipboardForCurrentView(m_pDocument);
1984 
1985  uno::Reference<datatransfer::clipboard::XClipboard> xClipboard(xClip);
1986  pWindow->SetClipboard(xClipboard);
1987 #endif
1988  }
1989  else if (aAction == "size_changed")
1990  {
1991  // A size change is practically re-creation of the window.
1992  // But at a minimum it's a full invalidation.
1993  removeAll([&nLOKWindowId](int elemType, const CallbackData& elemData) {
1994  if (elemType == LOK_CALLBACK_WINDOW)
1995  {
1996  const boost::property_tree::ptree& aOldTree = elemData.getJson();
1997  if (nLOKWindowId == aOldTree.get<unsigned>("id", 0))
1998  {
1999  const std::string aOldAction = aOldTree.get<std::string>("action", "");
2000  if (aOldAction == "invalidate")
2001  return true;
2002  }
2003  }
2004  return false;
2005  });
2006  }
2007 
2008  // Queue this one.
2009  return false;
2010 }
2011 
2013 {
2014  comphelper::ProfileZone aZone("CallbackFlushHandler::Invoke");
2015 
2016  if (!m_pCallback)
2017  return;
2018 
2019  std::scoped_lock<std::mutex> lock(m_mutex);
2020 
2021  SAL_INFO("lok", "Flushing " << m_queue1.size() << " elements.");
2022  auto it1 = m_queue1.begin();
2023  auto it2 = m_queue2.begin();
2024  for (; it1 != m_queue1.end(); ++it1, ++it2)
2025  {
2026  const int type = *it1;
2027  const auto& payload = it2->PayloadString;
2028  const int viewId = lcl_isViewCallbackType(type) ? lcl_getViewId(*it2) : -1;
2029 
2030  if (viewId == -1)
2031  {
2032  const auto stateIt = m_states.find(type);
2033  if (stateIt != m_states.end())
2034  {
2035  // If the state didn't change, it's safe to ignore.
2036  if (stateIt->second == payload)
2037  {
2038  SAL_INFO("lok", "Skipping duplicate [" << type << "]: [" << payload << "].");
2039  continue;
2040  }
2041 
2042  stateIt->second = payload;
2043  }
2044  }
2045  else
2046  {
2047  const auto statesIt = m_viewStates.find(viewId);
2048  if (statesIt != m_viewStates.end())
2049  {
2050  auto& states = statesIt->second;
2051  const auto stateIt = states.find(type);
2052  if (stateIt != states.end())
2053  {
2054  // If the state didn't change, it's safe to ignore.
2055  if (stateIt->second == payload)
2056  {
2057  SAL_INFO("lok", "Skipping view duplicate [" << type << ',' << viewId << "]: [" << payload << "].");
2058  continue;
2059  }
2060 
2061  SAL_INFO("lok", "Replacing an element in view states [" << type << ',' << viewId << "]: [" << payload << "].");
2062  stateIt->second = payload;
2063  }
2064  else
2065  {
2066  SAL_INFO("lok", "Inserted a new element in view states: [" << type << ',' << viewId << "]: [" << payload << "]");
2067  states.emplace(type, payload);
2068 
2069  }
2070  }
2071  }
2072 
2073  m_pCallback(type, payload.c_str(), m_pData);
2074  }
2075 
2076  m_queue1.clear();
2077  m_queue2.clear();
2078 }
2079 
2080 bool CallbackFlushHandler::removeAll(const std::function<bool (int, const CallbackData&)>& rTestFunc)
2081 {
2082  bool bErased = false;
2083  auto it1 = m_queue1.begin();
2084  auto it2 = m_queue2.begin();
2085  while (it1 != m_queue1.end())
2086  {
2087  if (rTestFunc(*it1, *it2))
2088  {
2089  it1 = m_queue1.erase(it1);
2090  it2 = m_queue2.erase(it2);
2091  bErased = true;
2092  }
2093  else
2094  {
2095  ++it1;
2096  ++it2;
2097  }
2098  }
2099  return bErased;
2100 }
2101 
2103 {
2104  const auto& result = m_viewStates.emplace(viewId, decltype(m_viewStates)::mapped_type());
2105  if (!result.second && result.first != m_viewStates.end())
2106  {
2107  result.first->second.clear();
2108  }
2109 }
2110 
2112 {
2113  m_viewStates.erase(viewId);
2114 }
2115 
2116 
2117 static void doc_destroy(LibreOfficeKitDocument *pThis)
2118 {
2119  comphelper::ProfileZone aZone("doc_destroy");
2120 
2121  SolarMutexGuard aGuard;
2122 
2124 
2125  LibLODocument_Impl *pDocument = static_cast<LibLODocument_Impl*>(pThis);
2126  delete pDocument;
2127 }
2128 
2129 static void lo_destroy (LibreOfficeKit* pThis);
2130 static int lo_initialize (LibreOfficeKit* pThis, const char* pInstallPath, const char* pUserProfilePath);
2131 static LibreOfficeKitDocument* lo_documentLoad (LibreOfficeKit* pThis, const char* pURL);
2132 static char * lo_getError (LibreOfficeKit* pThis);
2133 static void lo_freeError (char* pFree);
2134 static LibreOfficeKitDocument* lo_documentLoadWithOptions (LibreOfficeKit* pThis,
2135  const char* pURL,
2136  const char* pOptions);
2137 static void lo_registerCallback (LibreOfficeKit* pThis,
2138  LibreOfficeKitCallback pCallback,
2139  void* pData);
2140 static char* lo_getFilterTypes(LibreOfficeKit* pThis);
2141 static void lo_setOptionalFeatures(LibreOfficeKit* pThis, unsigned long long features);
2142 static void lo_setDocumentPassword(LibreOfficeKit* pThis,
2143  const char* pURL,
2144  const char* pPassword);
2145 static char* lo_getVersionInfo(LibreOfficeKit* pThis);
2146 static int lo_runMacro (LibreOfficeKit* pThis, const char* pURL);
2147 
2148 static bool lo_signDocument(LibreOfficeKit* pThis,
2149  const char* pUrl,
2150  const unsigned char* pCertificateBinary,
2151  const int nCertificateBinarySize,
2152  const unsigned char* pPrivateKeyBinary,
2153  const int nPrivateKeyBinarySize);
2154 
2155 static void lo_runLoop(LibreOfficeKit* pThis,
2156  LibreOfficeKitPollCallback pPollCallback,
2157  LibreOfficeKitWakeCallback pWakeCallback,
2158  void* pData);
2159 
2160 static void lo_sendDialogEvent(LibreOfficeKit* pThis,
2161  unsigned long long int nLOKWindowId,
2162  const char* pArguments);
2163 
2164 static void lo_setOption(LibreOfficeKit* pThis, const char* pOption, const char* pValue);
2165 
2167  : m_pOfficeClass( gOfficeClass.lock() )
2168  , maThread(nullptr)
2169  , mpCallback(nullptr)
2170  , mpCallbackData(nullptr)
2171  , mOptionalFeatures(0)
2172 {
2173  if(!m_pOfficeClass) {
2174  m_pOfficeClass = std::make_shared<LibreOfficeKitClass>();
2175  m_pOfficeClass->nSize = sizeof(LibreOfficeKitClass);
2176 
2177  m_pOfficeClass->destroy = lo_destroy;
2178  m_pOfficeClass->documentLoad = lo_documentLoad;
2179  m_pOfficeClass->getError = lo_getError;
2180  m_pOfficeClass->freeError = lo_freeError;
2181  m_pOfficeClass->documentLoadWithOptions = lo_documentLoadWithOptions;
2182  m_pOfficeClass->registerCallback = lo_registerCallback;
2183  m_pOfficeClass->getFilterTypes = lo_getFilterTypes;
2184  m_pOfficeClass->setOptionalFeatures = lo_setOptionalFeatures;
2185  m_pOfficeClass->setDocumentPassword = lo_setDocumentPassword;
2186  m_pOfficeClass->getVersionInfo = lo_getVersionInfo;
2187  m_pOfficeClass->runMacro = lo_runMacro;
2188  m_pOfficeClass->signDocument = lo_signDocument;
2189  m_pOfficeClass->runLoop = lo_runLoop;
2190  m_pOfficeClass->sendDialogEvent = lo_sendDialogEvent;
2191  m_pOfficeClass->setOption = lo_setOption;
2192 
2194  }
2195 
2196  pClass = m_pOfficeClass.get();
2197 }
2198 
2200 {
2201 }
2202 
2203 namespace
2204 {
2205 
2206 #ifdef IOS
2207 void paintTileToCGContext(ITiledRenderable* pDocument,
2208  void* rCGContext, const Size nCanvasSize,
2209  const int nTilePosX, const int nTilePosY,
2210  const int nTileWidth, const int nTileHeight)
2211 {
2213  aData.rCGContext = reinterpret_cast<CGContextRef>(rCGContext);
2214 
2215  ScopedVclPtrInstance<VirtualDevice> pDevice(aData, Size(1, 1), DeviceFormat::DEFAULT);
2216  pDevice->SetBackground(Wallpaper(COL_TRANSPARENT));
2217  pDevice->SetOutputSizePixel(nCanvasSize);
2218  pDocument->paintTile(*pDevice, nCanvasSize.Width(), nCanvasSize.Height(),
2219  nTilePosX, nTilePosY, nTileWidth, nTileHeight);
2220 }
2221 
2222 void paintTileIOS(LibreOfficeKitDocument* pThis,
2223  unsigned char* pBuffer,
2224  const int nCanvasWidth, const int nCanvasHeight, const double fDPIScale,
2225  const int nTilePosX, const int nTilePosY,
2226  const int nTileWidth, const int nTileHeight)
2227 {
2228  CGContextRef pCGContext = CGBitmapContextCreate(pBuffer, nCanvasWidth, nCanvasHeight, 8,
2229  nCanvasWidth * 4, CGColorSpaceCreateDeviceRGB(),
2230  kCGImageAlphaPremultipliedFirst | kCGImageByteOrder32Little);
2231 
2232  CGContextTranslateCTM(pCGContext, 0, nCanvasHeight);
2233  CGContextScaleCTM(pCGContext, fDPIScale, -fDPIScale);
2234 
2235  doc_paintTileToCGContext(pThis, (void*) pCGContext, nCanvasWidth, nCanvasHeight, nTilePosX, nTilePosY, nTileWidth, nTileHeight);
2236 
2237  CGContextRelease(pCGContext);
2238 }
2239 #endif
2240 
2241 void setLanguageAndLocale(OUString const & aLangISO)
2242 {
2243  SvtSysLocaleOptions aLocalOptions;
2244  aLocalOptions.SetLocaleConfigString(aLangISO);
2245  aLocalOptions.SetUILocaleConfigString(aLangISO);
2246  aLocalOptions.Commit();
2247 }
2248 
2249 void setFormatSpecificFilterData(std::u16string_view sFormat, comphelper::SequenceAsHashMap & rFilterDataMap)
2250 {
2251  if (sFormat == u"pdf")
2252  {
2253  // always export bookmarks, which is needed for annotations
2254  rFilterDataMap["ExportBookmarks"] <<= true;
2255  }
2256 }
2257 
2258 } // anonymous namespace
2259 
2260 // Wonder global state ...
2261 static uno::Reference<css::uno::XComponentContext> xContext;
2262 static uno::Reference<css::lang::XMultiServiceFactory> xSFactory;
2263 static uno::Reference<css::lang::XMultiComponentFactory> xFactory;
2264 
2265 static LibreOfficeKitDocument* lo_documentLoad(LibreOfficeKit* pThis, const char* pURL)
2266 {
2267  return lo_documentLoadWithOptions(pThis, pURL, nullptr);
2268 }
2269 
2270 static LibreOfficeKitDocument* lo_documentLoadWithOptions(LibreOfficeKit* pThis, const char* pURL, const char* pOptions)
2271 {
2272  comphelper::ProfileZone aZone("lo_documentLoadWithOptions");
2273 
2274  SolarMutexGuard aGuard;
2275 
2276  static int nDocumentIdCounter = 0;
2277 
2278  LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
2279  pLib->maLastExceptionMsg.clear();
2280 
2281  const OUString aURL(getAbsoluteURL(pURL));
2282  if (aURL.isEmpty())
2283  {
2284  pLib->maLastExceptionMsg = "Filename to load was not provided.";
2285  SAL_INFO("lok", "URL for load is empty");
2286  return nullptr;
2287  }
2288 
2289  pLib->maLastExceptionMsg.clear();
2290 
2291  if (!xContext.is())
2292  {
2293  pLib->maLastExceptionMsg = "ComponentContext is not available";
2294  SAL_INFO("lok", "ComponentContext is not available");
2295  return nullptr;
2296  }
2297 
2298  uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(xContext);
2299 
2300  if (!xComponentLoader.is())
2301  {
2302  pLib->maLastExceptionMsg = "ComponentLoader is not available";
2303  SAL_INFO("lok", "ComponentLoader is not available");
2304  return nullptr;
2305  }
2306 
2307  try
2308  {
2309  // 'Language=...' is an option that LOK consumes by itself, and does
2310  // not pass it as a parameter to the filter
2311  OUString aOptions = getUString(pOptions);
2312  const OUString aLanguage = extractParameter(aOptions, u"Language");
2313  bool isValidLangTag = LanguageTag::isValidBcp47(aLanguage, nullptr);
2314 
2315  if (!aLanguage.isEmpty() && isValidLangTag)
2316  {
2318  // Set the LOK language tag, used for dialog tunneling.
2321 
2322  SAL_INFO("lok", "Set document language to " << aLanguage);
2323  // use with care - it sets it for the entire core, not just the
2324  // document
2325  setLanguageAndLocale(aLanguage);
2326  // Need to reset the static initialized values
2328  }
2329 
2330  const OUString aDeviceFormFactor = extractParameter(aOptions, u"DeviceFormFactor");
2331  SfxLokHelper::setDeviceFormFactor(aDeviceFormFactor);
2332 
2333  const OUString aBatch = extractParameter(aOptions, u"Batch");
2334  if (!aBatch.isEmpty())
2335  {
2336  Application::SetDialogCancelMode(DialogCancelMode::LOKSilent);
2337  }
2338 
2339  uno::Sequence<css::beans::PropertyValue> aFilterOptions(3);
2340  aFilterOptions[0] = css::beans::PropertyValue( "FilterOptions",
2341  0,
2342  uno::makeAny(aOptions),
2343  beans::PropertyState_DIRECT_VALUE);
2344 
2345  rtl::Reference<LOKInteractionHandler> const pInteraction(
2346  new LOKInteractionHandler("load", pLib));
2347  auto const pair(pLib->mInteractionMap.insert(std::make_pair(aURL.toUtf8(), pInteraction)));
2348  comphelper::ScopeGuard const g([&] () {
2349  if (pair.second)
2350  {
2351  pLib->mInteractionMap.erase(aURL.toUtf8());
2352  }
2353  });
2354  uno::Reference<task::XInteractionHandler2> const xInteraction(pInteraction);
2355  aFilterOptions[1].Name = "InteractionHandler";
2356  aFilterOptions[1].Value <<= xInteraction;
2357 
2358  int nMacroSecurityLevel = 1;
2359  const OUString aMacroSecurityLevel = extractParameter(aOptions, u"MacroSecurityLevel");
2360  if (!aMacroSecurityLevel.isEmpty())
2361  {
2362  double nNumber;
2363  sal_uInt32 nFormat = 1;
2365  if (aFormatter.IsNumberFormat(aMacroSecurityLevel, nFormat, nNumber))
2366  nMacroSecurityLevel = static_cast<int>(nNumber);
2367  }
2368  SvtSecurityOptions::SetMacroSecurityLevel(nMacroSecurityLevel);
2369 
2370 #if defined(ANDROID) && HAVE_FEATURE_ANDROID_LOK
2371  sal_Int16 nMacroExecMode = document::MacroExecMode::USE_CONFIG;
2372 #else
2373  const OUString aEnableMacrosExecution = extractParameter(aOptions, u"EnableMacrosExecution");
2374  sal_Int16 nMacroExecMode = aEnableMacrosExecution == "true" ? document::MacroExecMode::USE_CONFIG :
2375  document::MacroExecMode::NEVER_EXECUTE;
2376 #endif
2377  aFilterOptions[2].Name = "MacroExecutionMode";
2378  aFilterOptions[2].Value <<= nMacroExecMode;
2379 
2380  /* TODO
2381  sal_Int16 nUpdateDoc = document::UpdateDocMode::ACCORDING_TO_CONFIG;
2382  aFilterOptions[3].Name = "UpdateDocMode";
2383  aFilterOptions[3].Value <<= nUpdateDoc;
2384  */
2385 
2386  const int nThisDocumentId = nDocumentIdCounter++;
2388  uno::Reference<lang::XComponent> xComponent = xComponentLoader->loadComponentFromURL(
2389  aURL, "_blank", 0,
2390  aFilterOptions);
2391 
2392  assert(!xComponent.is() || pair.second); // concurrent loading of same URL ought to fail
2393 
2394  if (!xComponent.is())
2395  {
2396  pLib->maLastExceptionMsg = "loadComponentFromURL returned an empty reference";
2397  SAL_INFO("lok", "Document can't be loaded - " << pLib->maLastExceptionMsg);
2398  return nullptr;
2399  }
2400 
2401  LibLODocument_Impl* pDocument = new LibLODocument_Impl(xComponent, nThisDocumentId);
2402 
2403  // After loading the document, its initial view is the "current" view.
2404  if (pLib->mpCallback)
2405  {
2406  int nState = doc_getSignatureState(pDocument);
2407  pLib->mpCallback(LOK_CALLBACK_SIGNATURE_STATUS, OString::number(nState).getStr(), pLib->mpCallbackData);
2408  }
2409  return pDocument;
2410  }
2411  catch (const uno::Exception& exception)
2412  {
2413  pLib->maLastExceptionMsg = exception.Message;
2414  TOOLS_INFO_EXCEPTION("lok", "Document can't be loaded");
2415  }
2416 
2417  return nullptr;
2418 }
2419 
2420 static int lo_runMacro(LibreOfficeKit* pThis, const char *pURL)
2421 {
2422  comphelper::ProfileZone aZone("lo_runMacro");
2423 
2424  SolarMutexGuard aGuard;
2425 
2426  LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
2427  pLib->maLastExceptionMsg.clear();
2428 
2429  OUString sURL( pURL, strlen(pURL), RTL_TEXTENCODING_UTF8 );
2430  if (sURL.isEmpty())
2431  {
2432  pLib->maLastExceptionMsg = "Macro to run was not provided.";
2433  SAL_INFO("lok", "Macro URL is empty");
2434  return false;
2435  }
2436 
2437  if (!sURL.startsWith("macro://"))
2438  {
2439  pLib->maLastExceptionMsg = "This doesn't look like macro URL";
2440  SAL_INFO("lok", "Macro URL is invalid");
2441  return false;
2442  }
2443 
2444  pLib->maLastExceptionMsg.clear();
2445 
2446  if (!xContext.is())
2447  {
2448  pLib->maLastExceptionMsg = "ComponentContext is not available";
2449  SAL_INFO("lok", "ComponentContext is not available");
2450  return false;
2451  }
2452 
2453  util::URL aURL;
2454  aURL.Complete = sURL;
2455 
2456  uno::Reference < util::XURLTransformer > xParser( util::URLTransformer::create( xContext ) );
2457 
2458  if( xParser.is() )
2459  xParser->parseStrict( aURL );
2460 
2461  uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(xContext);
2462 
2463  if (!xComponentLoader.is())
2464  {
2465  pLib->maLastExceptionMsg = "ComponentLoader is not available";
2466  SAL_INFO("lok", "ComponentLoader is not available");
2467  return false;
2468  }
2469 
2470  xFactory = xContext->getServiceManager();
2471 
2472  if (xFactory.is())
2473  {
2474  uno::Reference<frame::XDispatchProvider> xDP;
2475  xSFactory.set(xFactory, uno::UNO_QUERY_THROW);
2476  xDP.set( xSFactory->createInstance("com.sun.star.comp.sfx2.SfxMacroLoader"), uno::UNO_QUERY );
2477  uno::Reference<frame::XDispatch> xD = xDP->queryDispatch( aURL, OUString(), 0);
2478 
2479  if (!xD.is())
2480  {
2481  pLib->maLastExceptionMsg = "Macro loader is not available";
2482  SAL_INFO("lok", "Macro loader is not available");
2483  return false;
2484  }
2485 
2486  uno::Reference < frame::XSynchronousDispatch > xSyncDisp( xD, uno::UNO_QUERY_THROW );
2487  uno::Sequence<css::beans::PropertyValue> aEmpty;
2488  css::beans::PropertyValue aErr;
2489  uno::Any aRet = xSyncDisp->dispatchWithReturnValue( aURL, aEmpty );
2490  aRet >>= aErr;
2491 
2492  if (aErr.Name == "ErrorCode")
2493  {
2494  sal_uInt32 nErrCode = 0; // ERRCODE_NONE
2495  aErr.Value >>= nErrCode;
2496 
2497  pLib->maLastExceptionMsg = "An error occurred running macro (error code: " + OUString::number( nErrCode ) + ")";
2498  SAL_INFO("lok", "Macro execution terminated with error code " << nErrCode);
2499 
2500  return false;
2501  }
2502 
2503  return true;
2504  }
2505 
2506  return false;
2507 }
2508 
2509 static bool lo_signDocument(LibreOfficeKit* /*pThis*/,
2510  const char* pURL,
2511  const unsigned char* pCertificateBinary,
2512  const int nCertificateBinarySize,
2513  const unsigned char* pPrivateKeyBinary,
2514  const int nPrivateKeyBinarySize)
2515 {
2516  comphelper::ProfileZone aZone("lo_signDocument");
2517 
2518  OUString aURL(getAbsoluteURL(pURL));
2519  if (aURL.isEmpty())
2520  return false;
2521 
2522  if (!xContext.is())
2523  return false;
2524 
2525  uno::Sequence<sal_Int8> aCertificateSequence;
2526 
2527  std::string aCertificateString(reinterpret_cast<const char*>(pCertificateBinary), nCertificateBinarySize);
2528  std::string aCertificateBase64String = extractCertificate(aCertificateString);
2529  if (!aCertificateBase64String.empty())
2530  {
2531  OUString aBase64OUString = OUString::createFromAscii(aCertificateBase64String.c_str());
2532  comphelper::Base64::decode(aCertificateSequence, aBase64OUString);
2533  }
2534  else
2535  {
2536  aCertificateSequence.realloc(nCertificateBinarySize);
2537  std::copy(pCertificateBinary, pCertificateBinary + nCertificateBinarySize, aCertificateSequence.begin());
2538  }
2539 
2540  uno::Sequence<sal_Int8> aPrivateKeySequence;
2541  std::string aPrivateKeyString(reinterpret_cast<const char*>(pPrivateKeyBinary), nPrivateKeyBinarySize);
2542  std::string aPrivateKeyBase64String = extractPrivateKey(aPrivateKeyString);
2543  if (!aPrivateKeyBase64String.empty())
2544  {
2545  OUString aBase64OUString = OUString::createFromAscii(aPrivateKeyBase64String.c_str());
2546  comphelper::Base64::decode(aPrivateKeySequence, aBase64OUString);
2547  }
2548  else
2549  {
2550  aPrivateKeySequence.realloc(nPrivateKeyBinarySize);
2551  std::copy(pPrivateKeyBinary, pPrivateKeyBinary + nPrivateKeyBinarySize, aPrivateKeySequence.begin());
2552  }
2553 
2554  uno::Reference<xml::crypto::XSEInitializer> xSEInitializer = xml::crypto::SEInitializer::create(xContext);
2555  uno::Reference<xml::crypto::XXMLSecurityContext> xSecurityContext = xSEInitializer->createSecurityContext(OUString());
2556  if (!xSecurityContext.is())
2557  return false;
2558 
2559  uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment = xSecurityContext->getSecurityEnvironment();
2560  uno::Reference<xml::crypto::XCertificateCreator> xCertificateCreator(xSecurityEnvironment, uno::UNO_QUERY);
2561 
2562  if (!xCertificateCreator.is())
2563  return false;
2564 
2565  uno::Reference<security::XCertificate> xCertificate = xCertificateCreator->createDERCertificateWithPrivateKey(aCertificateSequence, aPrivateKeySequence);
2566 
2567  if (!xCertificate.is())
2568  return false;
2569 
2570  sfx2::DocumentSigner aDocumentSigner(aURL);
2571  if (!aDocumentSigner.signDocument(xCertificate))
2572  return false;
2573 
2574  return true;
2575 }
2576 
2577 static void lo_registerCallback (LibreOfficeKit* pThis,
2578  LibreOfficeKitCallback pCallback,
2579  void* pData)
2580 {
2581  SolarMutexGuard aGuard;
2582 
2583  Application* pApp = GetpApp();
2584  assert(pApp);
2585 
2586  LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
2587  pLib->maLastExceptionMsg.clear();
2588 
2589  pApp->m_pCallback = pLib->mpCallback = pCallback;
2590  pApp->m_pCallbackData = pLib->mpCallbackData = pData;
2591 }
2592 
2593 static int doc_saveAs(LibreOfficeKitDocument* pThis, const char* sUrl, const char* pFormat, const char* pFilterOptions)
2594 {
2595  comphelper::ProfileZone aZone("doc_saveAs");
2596 
2597  SolarMutexGuard aGuard;
2599 
2600  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
2601 
2602  OUString sFormat = getUString(pFormat);
2603  OUString aURL(getAbsoluteURL(sUrl));
2604 
2605  uno::Reference<frame::XStorable> xStorable(pDocument->mxComponent, uno::UNO_QUERY_THROW);
2606 
2607  if (aURL.isEmpty())
2608  {
2609  SetLastExceptionMsg("Filename to save to was not provided.");
2610  SAL_INFO("lok", "URL for save is empty");
2611  return false;
2612  }
2613 
2614  try
2615  {
2616  const ExtensionMap* pMap;
2617 
2618  switch (doc_getDocumentType(pThis))
2619  {
2620  case LOK_DOCTYPE_SPREADSHEET:
2621  pMap = aCalcExtensionMap;
2622  break;
2623  case LOK_DOCTYPE_PRESENTATION:
2624  pMap = aImpressExtensionMap;
2625  break;
2626  case LOK_DOCTYPE_DRAWING:
2627  pMap = aDrawExtensionMap;
2628  break;
2629  case LOK_DOCTYPE_TEXT:
2630  pMap = aWriterExtensionMap;
2631  break;
2632  case LOK_DOCTYPE_OTHER:
2633  default:
2634  SAL_INFO("lok", "Can't save document - unsupported document type.");
2635  return false;
2636  }
2637 
2638  if (pFormat == nullptr)
2639  {
2640  // sniff from the extension
2641  sal_Int32 idx = aURL.lastIndexOf(".");
2642  if( idx > 0 )
2643  {
2644  sFormat = aURL.copy( idx + 1 );
2645  }
2646  else
2647  {
2648  SetLastExceptionMsg("input filename without a suffix");
2649  return false;
2650  }
2651  }
2652 
2653  OUString aFilterName;
2654  for (sal_Int32 i = 0; pMap[i].extn; ++i)
2655  {
2656  if (sFormat.equalsIgnoreAsciiCaseAscii(pMap[i].extn))
2657  {
2658  aFilterName = getUString(pMap[i].filterName);
2659  break;
2660  }
2661  }
2662  if (aFilterName.isEmpty())
2663  {
2664  SetLastExceptionMsg("no output filter found for provided suffix");
2665  return false;
2666  }
2667 
2668  OUString aFilterOptions = getUString(pFilterOptions);
2669 
2670  // Check if watermark for pdf is passed by filteroptions...
2671  // It is not a real filter option so it must be filtered out.
2672  OUString watermarkText, sFullSheetPreview;
2673  int aIndex = -1;
2674  if ((aIndex = aFilterOptions.indexOf(",Watermark=")) >= 0)
2675  {
2676  int bIndex = aFilterOptions.indexOf("WATERMARKEND");
2677  watermarkText = aFilterOptions.subView(aIndex+11, bIndex-(aIndex+11));
2678  aFilterOptions = OUString::Concat(aFilterOptions.subView(0, aIndex)) + aFilterOptions.subView(bIndex+12);
2679  }
2680 
2681  if ((aIndex = aFilterOptions.indexOf(",FullSheetPreview=")) >= 0)
2682  {
2683  int bIndex = aFilterOptions.indexOf("FULLSHEETPREVEND");
2684  sFullSheetPreview = aFilterOptions.subView(aIndex+18, bIndex-(aIndex+18));
2685  aFilterOptions = OUString::Concat(aFilterOptions.subView(0, aIndex)) + aFilterOptions.subView(bIndex+16);
2686  }
2687 
2688  bool bFullSheetPreview = sFullSheetPreview == "true";
2689 
2690  // 'TakeOwnership' == this is a 'real' SaveAs (that is, the document
2691  // gets a new name). When this is not provided, the meaning of
2692  // saveAs() is more like save-a-copy, which allows saving to any
2693  // random format like PDF or PNG.
2694  // It is not a real filter option, so we have to filter it out.
2695  const uno::Sequence<OUString> aOptionSeq = comphelper::string::convertCommaSeparated(aFilterOptions);
2696  std::vector<OUString> aFilteredOptionVec;
2697  bool bTakeOwnership = false;
2698  MediaDescriptor aSaveMediaDescriptor;
2699  for (const auto& rOption : aOptionSeq)
2700  {
2701  if (rOption == "TakeOwnership")
2702  bTakeOwnership = true;
2703  else if (rOption == "NoFileSync")
2704  aSaveMediaDescriptor["NoFileSync"] <<= true;
2705  else
2706  aFilteredOptionVec.push_back(rOption);
2707  }
2708 
2709  aSaveMediaDescriptor["Overwrite"] <<= true;
2710  aSaveMediaDescriptor["FilterName"] <<= aFilterName;
2711 
2712  auto aFilteredOptionSeq = comphelper::containerToSequence<OUString>(aFilteredOptionVec);
2713  aFilterOptions = comphelper::string::convertCommaSeparated(aFilteredOptionSeq);
2714  aSaveMediaDescriptor[MediaDescriptor::PROP_FILTEROPTIONS()] <<= aFilterOptions;
2715 
2716  comphelper::SequenceAsHashMap aFilterDataMap;
2717 
2718  setFormatSpecificFilterData(sFormat, aFilterDataMap);
2719 
2720  if (!watermarkText.isEmpty())
2721  aFilterDataMap["TiledWatermark"] <<= watermarkText;
2722 
2723  if (bFullSheetPreview)
2724  aFilterDataMap["SinglePageSheets"] <<= true;
2725 
2726  if (!aFilterDataMap.empty())
2727  {
2728  aSaveMediaDescriptor["FilterData"] <<= aFilterDataMap.getAsConstPropertyValueList();
2729  }
2730 
2731  // add interaction handler too
2732  if (gImpl)
2733  {
2734  // gImpl does not have to exist when running from a unit test
2735  rtl::Reference<LOKInteractionHandler> const pInteraction(
2736  new LOKInteractionHandler("saveas", gImpl, pDocument));
2737  uno::Reference<task::XInteractionHandler2> const xInteraction(pInteraction);
2738 
2739  aSaveMediaDescriptor[MediaDescriptor::PROP_INTERACTIONHANDLER()] <<= xInteraction;
2740  }
2741 
2742 
2743  if (bTakeOwnership)
2744  xStorable->storeAsURL(aURL, aSaveMediaDescriptor.getAsConstPropertyValueList());
2745  else
2746  xStorable->storeToURL(aURL, aSaveMediaDescriptor.getAsConstPropertyValueList());
2747 
2748  return true;
2749  }
2750  catch (const uno::Exception& exception)
2751  {
2752  SetLastExceptionMsg("exception: " + exception.Message);
2753  }
2754  return false;
2755 }
2756 
2763 static void doc_iniUnoCommands ()
2764 {
2765  SolarMutexGuard aGuard;
2767 
2768  OUString sUnoCommands[] =
2769  {
2770  OUString(".uno:AlignLeft"),
2771  OUString(".uno:AlignHorizontalCenter"),
2772  OUString(".uno:AlignRight"),
2773  OUString(".uno:BackColor"),
2774  OUString(".uno:BackgroundColor"),
2775  OUString(".uno:TableCellBackgroundColor"),
2776  OUString(".uno:Bold"),
2777  OUString(".uno:CenterPara"),
2778  OUString(".uno:CharBackColor"),
2779  OUString(".uno:CharBackgroundExt"),
2780  OUString(".uno:CharFontName"),
2781  OUString(".uno:Color"),
2782  OUString(".uno:ControlCodes"),
2783  OUString(".uno:DecrementIndent"),
2784  OUString(".uno:DefaultBullet"),
2785  OUString(".uno:DefaultNumbering"),
2786  OUString(".uno:FontColor"),
2787  OUString(".uno:FontHeight"),
2788  OUString(".uno:IncrementIndent"),
2789  OUString(".uno:Italic"),
2790  OUString(".uno:JustifyPara"),
2791  OUString(".uno:JumpToMark"),
2792  OUString(".uno:OutlineFont"),
2793  OUString(".uno:LeftPara"),
2794  OUString(".uno:LanguageStatus"),
2795  OUString(".uno:RightPara"),
2796  OUString(".uno:Shadowed"),
2797  OUString(".uno:SubScript"),
2798  OUString(".uno:SuperScript"),
2799  OUString(".uno:Strikeout"),
2800  OUString(".uno:StyleApply"),
2801  OUString(".uno:Underline"),
2802  OUString(".uno:ModifiedStatus"),
2803  OUString(".uno:Undo"),
2804  OUString(".uno:Redo"),
2805  OUString(".uno:InsertPage"),
2806  OUString(".uno:DeletePage"),
2807  OUString(".uno:DuplicatePage"),
2808  OUString(".uno:Cut"),
2809  OUString(".uno:Copy"),
2810  OUString(".uno:Paste"),
2811  OUString(".uno:SelectAll"),
2812  OUString(".uno:InsertAnnotation"),
2813  OUString(".uno:DeleteAnnotation"),
2814  OUString(".uno:ReplyComment"),
2815  OUString(".uno:ResolveComment"),
2816  OUString(".uno:ResolveCommentThread"),
2817  OUString(".uno:InsertRowsBefore"),
2818  OUString(".uno:InsertRowsAfter"),
2819  OUString(".uno:InsertColumnsBefore"),
2820  OUString(".uno:InsertColumnsAfter"),
2821  OUString(".uno:MergeCells"),
2822  OUString(".uno:DeleteRows"),
2823  OUString(".uno:DeleteColumns"),
2824  OUString(".uno:DeleteTable"),
2825  OUString(".uno:SelectTable"),
2826  OUString(".uno:EntireRow"),
2827  OUString(".uno:EntireColumn"),
2828  OUString(".uno:EntireCell"),
2829  OUString(".uno:AssignLayout"),
2830  OUString(".uno:StatusDocPos"),
2831  OUString(".uno:RowColSelCount"),
2832  OUString(".uno:StatusPageStyle"),
2833  OUString(".uno:InsertMode"),
2834  OUString(".uno:SpellOnline"),
2835  OUString(".uno:StatusSelectionMode"),
2836  OUString(".uno:StateTableCell"),
2837  OUString(".uno:StatusBarFunc"),
2838  OUString(".uno:StatePageNumber"),
2839  OUString(".uno:StateWordCount"),
2840  OUString(".uno:SelectionMode"),
2841  OUString(".uno:PageStatus"),
2842  OUString(".uno:LayoutStatus"),
2843  OUString(".uno:Scale"),
2844  OUString(".uno:Context"),
2845  OUString(".uno:WrapText"),
2846  OUString(".uno:ToggleMergeCells"),
2847  OUString(".uno:NumberFormatCurrency"),
2848  OUString(".uno:NumberFormatPercent"),
2849  OUString(".uno:NumberFormatDecimal"),
2850  OUString(".uno:NumberFormatDate"),
2851  OUString(".uno:FrameLineColor"),
2852  OUString(".uno:SortAscending"),
2853  OUString(".uno:SortDescending"),
2854  OUString(".uno:TrackChanges"),
2855  OUString(".uno:ShowTrackedChanges"),
2856  OUString(".uno:NextTrackedChange"),
2857  OUString(".uno:PreviousTrackedChange"),
2858  OUString(".uno:AcceptAllTrackedChanges"),
2859  OUString(".uno:RejectAllTrackedChanges"),
2860  OUString(".uno:TableDialog"),
2861  OUString(".uno:FormatCellDialog"),
2862  OUString(".uno:FontDialog"),
2863  OUString(".uno:ParagraphDialog"),
2864  OUString(".uno:OutlineBullet"),
2865  OUString(".uno:InsertIndexesEntry"),
2866  OUString(".uno:DocumentRepair"),
2867  OUString(".uno:TransformDialog"),
2868  OUString(".uno:InsertPageHeader"),
2869  OUString(".uno:InsertPageFooter"),
2870  OUString(".uno:OnlineAutoFormat"),
2871  OUString(".uno:InsertObjectChart"),
2872  OUString(".uno:InsertSection"),
2873  OUString(".uno:InsertAnnotation"),
2874  OUString(".uno:InsertPagebreak"),
2875  OUString(".uno:InsertColumnBreak"),
2876  OUString(".uno:HyperlinkDialog"),
2877  OUString(".uno:InsertSymbol"),
2878  OUString(".uno:EditRegion"),
2879  OUString(".uno:ThesaurusDialog"),
2880  OUString(".uno:FormatArea"),
2881  OUString(".uno:FormatLine"),
2882  OUString(".uno:FormatColumns"),
2883  OUString(".uno:Watermark"),
2884  OUString(".uno:ResetAttributes"),
2885  OUString(".uno:Orientation"),
2886  OUString(".uno:ObjectAlignLeft"),
2887  OUString(".uno:ObjectAlignRight"),
2888  OUString(".uno:AlignCenter"),
2889  OUString(".uno:TransformPosX"),
2890  OUString(".uno:TransformPosY"),
2891  OUString(".uno:TransformWidth"),
2892  OUString(".uno:TransformHeight"),
2893  OUString(".uno:ObjectBackOne"),
2894  OUString(".uno:SendToBack"),
2895  OUString(".uno:ObjectForwardOne"),
2896  OUString(".uno:BringToFront"),
2897  OUString(".uno:WrapRight"),
2898  OUString(".uno:WrapThrough"),
2899  OUString(".uno:WrapLeft"),
2900  OUString(".uno:WrapIdeal"),
2901  OUString(".uno:WrapOn"),
2902  OUString(".uno:WrapOff"),
2903  OUString(".uno:UpdateCurIndex"),
2904  OUString(".uno:InsertCaptionDialog"),
2905  OUString(".uno:FormatGroup"),
2906  OUString(".uno:SplitTable"),
2907  OUString(".uno:MergeCells"),
2908  OUString(".uno:DeleteNote"),
2909  OUString(".uno:AcceptChanges"),
2910  OUString(".uno:FormatPaintbrush"),
2911  OUString(".uno:SetDefault"),
2912  OUString(".uno:ParaLeftToRight"),
2913  OUString(".uno:ParaRightToLeft"),
2914  OUString(".uno:ParaspaceIncrease"),
2915  OUString(".uno:ParaspaceDecrease"),
2916  OUString(".uno:AcceptTrackedChange"),
2917  OUString(".uno:RejectTrackedChange"),
2918  OUString(".uno:ShowResolvedAnnotations"),
2919  OUString(".uno:InsertBreak"),
2920  OUString(".uno:InsertEndnote"),
2921  OUString(".uno:InsertFootnote"),
2922  OUString(".uno:InsertReferenceField"),
2923  OUString(".uno:InsertBookmark"),
2924  OUString(".uno:InsertAuthoritiesEntry"),
2925  OUString(".uno:InsertMultiIndex"),
2926  OUString(".uno:InsertField"),
2927  OUString(".uno:InsertPageNumberField"),
2928  OUString(".uno:InsertPageCountField"),
2929  OUString(".uno:InsertDateField"),
2930  OUString(".uno:InsertTitleField"),
2931  OUString(".uno:InsertFieldCtrl"),
2932  OUString(".uno:CharmapControl"),
2933  OUString(".uno:EnterGroup"),
2934  OUString(".uno:LeaveGroup"),
2935  OUString(".uno:AlignUp"),
2936  OUString(".uno:AlignMiddle"),
2937  OUString(".uno:AlignDown"),
2938  OUString(".uno:TraceChangeMode"),
2939  OUString(".uno:Combine"),
2940  OUString(".uno:Merge"),
2941  OUString(".uno:Dismantle"),
2942  OUString(".uno:Substract"),
2943  OUString(".uno:DistributeSelection"),
2944  OUString(".uno:Intersect"),
2945  OUString(".uno:BorderInner"),
2946  OUString(".uno:BorderOuter"),
2947  OUString(".uno:FreezePanes"),
2948  OUString(".uno:FreezePanesColumn"),
2949  OUString(".uno:FreezePanesRow"),
2950  OUString(".uno:Sidebar"),
2951  OUString(".uno:SheetRightToLeft"),
2952  OUString(".uno:RunMacro"),
2953  OUString(".uno:SpacePara1"),
2954  OUString(".uno:SpacePara15"),
2955  OUString(".uno:SpacePara2")
2956  };
2957 
2958  util::URL aCommandURL;
2959  SfxViewShell* pViewShell = SfxViewShell::Current();
2960  SfxViewFrame* pViewFrame = pViewShell? pViewShell->GetViewFrame(): nullptr;
2961 
2962  // check if Frame-Controller were created.
2963  if (!pViewFrame)
2964  {
2965  SAL_WARN("lok", "iniUnoCommands: No Frame-Controller created.");
2966  return;
2967  }
2968 
2969  if (!xContext.is())
2971  if (!xContext.is())
2972  {
2973  SAL_WARN("lok", "iniUnoCommands: Component context is not available");
2974  return;
2975  }
2976 
2977  SfxSlotPool& rSlotPool = SfxSlotPool::GetSlotPool(pViewFrame);
2978  uno::Reference<util::XURLTransformer> xParser(util::URLTransformer::create(xContext));
2979 
2980  for (const auto & sUnoCommand : sUnoCommands)
2981  {
2982  aCommandURL.Complete = sUnoCommand;
2983  xParser->parseStrict(aCommandURL);
2984 
2985  // when null, this command is not supported by the given component
2986  // (like eg. Calc does not have ".uno:DefaultBullet" etc.)
2987  if (const SfxSlot* pSlot = rSlotPool.GetUnoSlot(aCommandURL.Path))
2988  {
2989  // Initialize slot to dispatch .uno: Command.
2990  pViewFrame->GetBindings().GetDispatch(pSlot, aCommandURL, false);
2991  }
2992  }
2993 }
2994 
2995 static int doc_getDocumentType (LibreOfficeKitDocument* pThis)
2996 {
2997  comphelper::ProfileZone aZone("doc_getDocumentType");
2998 
2999  SolarMutexGuard aGuard;
3001 
3002  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
3003 
3004  try
3005  {
3006  uno::Reference<lang::XServiceInfo> xDocument(pDocument->mxComponent, uno::UNO_QUERY_THROW);
3007 
3008  if (xDocument->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
3009  {
3010  return LOK_DOCTYPE_SPREADSHEET;
3011  }
3012  else if (xDocument->supportsService("com.sun.star.presentation.PresentationDocument"))
3013  {
3014  return LOK_DOCTYPE_PRESENTATION;
3015  }
3016  else if (xDocument->supportsService("com.sun.star.drawing.DrawingDocument"))
3017  {
3018  return LOK_DOCTYPE_DRAWING;
3019  }
3020  else if (xDocument->supportsService("com.sun.star.text.TextDocument") || xDocument->supportsService("com.sun.star.text.WebDocument"))
3021  {
3022  return LOK_DOCTYPE_TEXT;
3023  }
3024  else
3025  {
3026  SetLastExceptionMsg("unknown document type");
3027  }
3028  }
3029  catch (const uno::Exception& exception)
3030  {
3031  SetLastExceptionMsg("exception: " + exception.Message);
3032  }
3033  return LOK_DOCTYPE_OTHER;
3034 }
3035 
3036 static int doc_getParts (LibreOfficeKitDocument* pThis)
3037 {
3038  comphelper::ProfileZone aZone("doc_getParts");
3039 
3040  SolarMutexGuard aGuard;
3041 
3042  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3043  if (!pDoc)
3044  {
3045  SetLastExceptionMsg("Document doesn't support tiled rendering");
3046  return 0;
3047  }
3048 
3049  return pDoc->getParts();
3050 }
3051 
3052 static int doc_getPart (LibreOfficeKitDocument* pThis)
3053 {
3054  comphelper::ProfileZone aZone("doc_getPart");
3055 
3056  SolarMutexGuard aGuard;
3058 
3059  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3060  if (!pDoc)
3061  {
3062  SetLastExceptionMsg("Document doesn't support tiled rendering");
3063  return 0;
3064  }
3065 
3066  return pDoc->getPart();
3067 }
3068 
3069 static void doc_setPartImpl(LibreOfficeKitDocument* pThis, int nPart, bool bAllowChangeFocus = true)
3070 {
3071  comphelper::ProfileZone aZone("doc_setPart");
3072 
3073  SolarMutexGuard aGuard;
3075 
3076  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3077  if (!pDoc)
3078  {
3079  SetLastExceptionMsg("Document doesn't support tiled rendering");
3080  return;
3081  }
3082 
3083  pDoc->setPart( nPart, bAllowChangeFocus );
3084 }
3085 
3086 static void doc_setPart(LibreOfficeKitDocument* pThis, int nPart)
3087 {
3088  doc_setPartImpl(pThis, nPart, true);
3089 }
3090 
3091 static char* doc_getPartInfo(LibreOfficeKitDocument* pThis, int nPart)
3092 {
3093  comphelper::ProfileZone aZone("doc_getPartInfo");
3094 
3095  SolarMutexGuard aGuard;
3096  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3097  if (!pDoc)
3098  {
3099  SetLastExceptionMsg("Document doesn't support tiled rendering");
3100  return nullptr;
3101  }
3102 
3103  return convertOUString(pDoc->getPartInfo(nPart));
3104 }
3105 
3106 static void doc_selectPart(LibreOfficeKitDocument* pThis, int nPart, int nSelect)
3107 {
3108  SolarMutexGuard aGuard;
3109  if (gImpl)
3110  gImpl->maLastExceptionMsg.clear();
3111 
3112  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3113  if (!pDoc)
3114  {
3115  gImpl->maLastExceptionMsg = "Document doesn't support tiled rendering";
3116  return;
3117  }
3118 
3119  pDoc->selectPart( nPart, nSelect );
3120 }
3121 
3122 static void doc_moveSelectedParts(LibreOfficeKitDocument* pThis, int nPosition, bool bDuplicate)
3123 {
3124  SolarMutexGuard aGuard;
3125  if (gImpl)
3126  gImpl->maLastExceptionMsg.clear();
3127 
3128  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3129  if (!pDoc)
3130  {
3131  gImpl->maLastExceptionMsg = "Document doesn't support tiled rendering";
3132  return;
3133  }
3134 
3135  pDoc->moveSelectedParts(nPosition, bDuplicate);
3136 }
3137 
3138 static char* doc_getPartPageRectangles(LibreOfficeKitDocument* pThis)
3139 {
3140  comphelper::ProfileZone aZone("doc_getPartPageRectangles");
3141 
3142  SolarMutexGuard aGuard;
3144 
3145  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3146  if (!pDoc)
3147  {
3148  SetLastExceptionMsg("Document doesn't support tiled rendering");
3149  return nullptr;
3150  }
3151 
3152  return convertOUString(pDoc->getPartPageRectangles());
3153 }
3154 
3155 static char* doc_getPartName(LibreOfficeKitDocument* pThis, int nPart)
3156 {
3157  comphelper::ProfileZone aZone("doc_getPartName");
3158 
3159  SolarMutexGuard aGuard;
3161 
3162  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3163  if (!pDoc)
3164  {
3165  SetLastExceptionMsg("Document doesn't support tiled rendering");
3166  return nullptr;
3167  }
3168 
3169  return convertOUString(pDoc->getPartName(nPart));
3170 }
3171 
3172 static char* doc_getPartHash(LibreOfficeKitDocument* pThis, int nPart)
3173 {
3174  comphelper::ProfileZone aZone("doc_getPartHash");
3175 
3176  SolarMutexGuard aGuard;
3178 
3179  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3180  if (!pDoc)
3181  {
3182  SetLastExceptionMsg("Document doesn't support tiled rendering");
3183  return nullptr;
3184  }
3185 
3186  return convertOUString(pDoc->getPartHash(nPart));
3187 }
3188 
3189 static void doc_setPartMode(LibreOfficeKitDocument* pThis,
3190  int nPartMode)
3191 {
3192  comphelper::ProfileZone aZone("doc_setPartMode");
3193 
3194  SolarMutexGuard aGuard;
3196 
3197  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3198  if (!pDoc)
3199  {
3200  SetLastExceptionMsg("Document doesn't support tiled rendering");
3201  return;
3202  }
3203 
3204 
3205  int nCurrentPart = pDoc->getPart();
3206 
3207  pDoc->setPartMode(nPartMode);
3208 
3209  // We need to make sure the internal state is updated, just changing the mode
3210  // might not update the relevant shells (i.e. impress will keep rendering the
3211  // previous mode unless we do this).
3212  // TODO: we might want to do this within the relevant components rather than
3213  // here, but that's also dependent on how we implement embedded object
3214  // rendering I guess?
3215  // TODO: we could be clever and e.g. set to 0 when we change to/from
3216  // embedded object mode, and not when changing between slide/notes/combined
3217  // modes?
3218  if ( nCurrentPart < pDoc->getParts() )
3219  {
3220  pDoc->setPart( nCurrentPart );
3221  }
3222  else
3223  {
3224  pDoc->setPart( 0 );
3225  }
3226 }
3227 
3228 static void doc_paintTile(LibreOfficeKitDocument* pThis,
3229  unsigned char* pBuffer,
3230  const int nCanvasWidth, const int nCanvasHeight,
3231  const int nTilePosX, const int nTilePosY,
3232  const int nTileWidth, const int nTileHeight)
3233 {
3234  comphelper::ProfileZone aZone("doc_paintTile");
3235 
3236  SolarMutexGuard aGuard;
3238 
3239  SAL_INFO( "lok.tiledrendering", "paintTile: painting [" << nTileWidth << "x" << nTileHeight <<
3240  "]@(" << nTilePosX << ", " << nTilePosY << ") to [" <<
3241  nCanvasWidth << "x" << nCanvasHeight << "]px" );
3242 
3243  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3244  if (!pDoc)
3245  {
3246  SetLastExceptionMsg("Document doesn't support tiled rendering");
3247  return;
3248  }
3249 
3250 #if defined(UNX) && !defined(MACOSX)
3251 
3252  // Painting of zoomed or HiDPI spreadsheets is special, we actually draw everything at 100%,
3253  // and only set cairo's (or CoreGraphic's, in the iOS case) scale factor accordingly, so that
3254  // everything is painted bigger or smaller. This is different to what Calc's internal scaling
3255  // would do - because that one is trying to fit the lines between cells to integer multiples of
3256  // pixels.
3258 
3259 #if defined(IOS)
3260  double fDPIScaleX = 1.0;
3261  paintTileIOS(pThis, pBuffer, nCanvasWidth, nCanvasHeight, fDPIScaleX, nTilePosX, nTilePosY, nTileWidth, nTileHeight);
3262 #else
3263  ScopedVclPtrInstance< VirtualDevice > pDevice(DeviceFormat::DEFAULT);
3264 
3265  // Set background to transparent by default.
3266  pDevice->SetBackground(Wallpaper(COL_TRANSPARENT));
3267 
3268  pDevice->SetOutputSizePixelScaleOffsetAndBuffer(
3269  Size(nCanvasWidth, nCanvasHeight), Fraction(1.0), Point(),
3270  pBuffer);
3271 
3272  pDoc->paintTile(*pDevice, nCanvasWidth, nCanvasHeight,
3273  nTilePosX, nTilePosY, nTileWidth, nTileHeight);
3274 
3275  static bool bDebug = getenv("LOK_DEBUG_TILES") != nullptr;
3276  if (bDebug)
3277  {
3278  // Draw a small red rectangle in the top left corner so that it's easy to see where a new tile begins.
3279  tools::Rectangle aRect(0, 0, 5, 5);
3280  aRect = pDevice->PixelToLogic(aRect);
3281  pDevice->Push(PushFlags::FILLCOLOR | PushFlags::LINECOLOR);
3282  pDevice->SetFillColor(COL_LIGHTRED);
3283  pDevice->SetLineColor();
3284  pDevice->DrawRect(aRect);
3285  pDevice->Pop();
3286  }
3287 #endif
3288 
3289 #else
3290  (void) pBuffer;
3291 #endif
3292 }
3293 
3294 #ifdef IOS
3295 
3296 // This function is separate only to be used by LibreOfficeLight. If that app can be retired, this
3297 // function's code can be inlined.
3298 static void doc_paintTileToCGContext(LibreOfficeKitDocument* pThis,
3299  void* rCGContext,
3300  const int nCanvasWidth, const int nCanvasHeight,
3301  const int nTilePosX, const int nTilePosY,
3302  const int nTileWidth, const int nTileHeight)
3303 {
3304  SolarMutexGuard aGuard;
3306 
3307  SAL_INFO( "lok.tiledrendering", "paintTileToCGContext: painting [" << nTileWidth << "x" << nTileHeight <<
3308  "]@(" << nTilePosX << ", " << nTilePosY << ") to [" <<
3309  nCanvasWidth << "x" << nCanvasHeight << "]px" );
3310 
3311  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3312  if (!pDoc)
3313  {
3314  SetLastExceptionMsg("Document doesn't support tiled rendering");
3315  return;
3316  }
3317 
3318  Size aCanvasSize(nCanvasWidth, nCanvasHeight);
3319  paintTileToCGContext(pDoc, rCGContext, aCanvasSize, nTilePosX, nTilePosY, nTileWidth, nTileHeight);
3320 }
3321 
3322 #endif
3323 
3324 static void doc_paintPartTile(LibreOfficeKitDocument* pThis,
3325  unsigned char* pBuffer,
3326  const int nPart,
3327  const int nCanvasWidth, const int nCanvasHeight,
3328  const int nTilePosX, const int nTilePosY,
3329  const int nTileWidth, const int nTileHeight)
3330 {
3331  comphelper::ProfileZone aZone("doc_paintPartTile");
3332 
3333  SolarMutexGuard aGuard;
3335 
3336  SAL_INFO( "lok.tiledrendering", "paintPartTile: painting @ " << nPart << " ["
3337  << nTileWidth << "x" << nTileHeight << "]@("
3338  << nTilePosX << ", " << nTilePosY << ") to ["
3339  << nCanvasWidth << "x" << nCanvasHeight << "]px" );
3340 
3341  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
3342  int nOrigViewId = doc_getView(pThis);
3343 
3344  if (nOrigViewId < 0)
3345  {
3346  // tile painting always needs a SfxViewShell::Current(), but actually
3347  // it does not really matter which one - all of them should paint the
3348  // same thing. It's important to get a view for the correct document,
3349  // though.
3350  // doc_getViewsCount() returns the count of views for the document in the current view.
3351  int viewCount = doc_getViewsCount(pThis);
3352  if (viewCount == 0)
3353  return;
3354 
3355  std::vector<int> viewIds(viewCount);
3356  doc_getViewIds(pThis, viewIds.data(), viewCount);
3357 
3358  nOrigViewId = viewIds[0];
3359  doc_setView(pThis, nOrigViewId);
3360  }
3361 
3362  // Disable callbacks while we are painting.
3363  if (nOrigViewId >= 0)
3364  {
3365  const auto handlerIt = pDocument->mpCallbackFlushHandlers.find(nOrigViewId);
3366  if (handlerIt != pDocument->mpCallbackFlushHandlers.end())
3367  handlerIt->second->disableCallbacks();
3368  }
3369 
3370  try
3371  {
3372  // Text documents have a single coordinate system; don't change part.
3373  int nOrigPart = 0;
3374  const bool isText = (doc_getDocumentType(pThis) == LOK_DOCTYPE_TEXT);
3375  int nViewId = nOrigViewId;
3376  if (!isText)
3377  {
3378  // Check if just switching to another view is enough, that has
3379  // less side-effects.
3380  if (nPart != doc_getPart(pThis))
3381  {
3382  SfxViewShell* pViewShell = SfxViewShell::GetFirst();
3383  while (pViewShell)
3384  {
3385  if (pViewShell->getPart() == nPart)
3386  {
3387  nViewId = static_cast<sal_Int32>(pViewShell->GetViewShellId());
3388  doc_setView(pThis, nViewId);
3389  break;
3390  }
3391  pViewShell = SfxViewShell::GetNext(*pViewShell);
3392  }
3393  }
3394 
3395  nOrigPart = doc_getPart(pThis);
3396  if (nPart != nOrigPart)
3397  {
3398  doc_setPartImpl(pThis, nPart, false);
3399  }
3400  }
3401 
3402  doc_paintTile(pThis, pBuffer, nCanvasWidth, nCanvasHeight, nTilePosX, nTilePosY, nTileWidth, nTileHeight);
3403 
3404  if (!isText && nPart != nOrigPart)
3405  {
3406  doc_setPartImpl(pThis, nOrigPart, false);
3407  }
3408  if (!isText && nViewId != nOrigViewId)
3409  {
3410  doc_setView(pThis, nOrigViewId);
3411  }
3412  }
3413  catch (const std::exception&)
3414  {
3415  // Nothing to do but restore the PartTilePainting flag.
3416  }
3417 
3418  if (nOrigViewId >= 0)
3419  {
3420  const auto handlerIt = pDocument->mpCallbackFlushHandlers.find(nOrigViewId);
3421  if (handlerIt != pDocument->mpCallbackFlushHandlers.end())
3422  handlerIt->second->enableCallbacks();
3423  }
3424 }
3425 
3426 static int doc_getTileMode(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/)
3427 {
3429  return LOK_TILEMODE_BGRA;
3430 }
3431 
3432 static void doc_getDocumentSize(LibreOfficeKitDocument* pThis,
3433  long* pWidth,
3434  long* pHeight)
3435 {
3436  comphelper::ProfileZone aZone("doc_getDocumentSize");
3437 
3438  SolarMutexGuard aGuard;
3440 
3441  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3442  if (pDoc)
3443  {
3444  Size aDocumentSize = pDoc->getDocumentSize();
3445  *pWidth = aDocumentSize.Width();
3446  *pHeight = aDocumentSize.Height();
3447  }
3448  else
3449  {
3450  SetLastExceptionMsg("Document doesn't support tiled rendering");
3451  }
3452 }
3453 
3454 static void doc_initializeForRendering(LibreOfficeKitDocument* pThis,
3455  const char* pArguments)
3456 {
3457  comphelper::ProfileZone aZone("doc_initializeForRendering");
3458 
3459  SolarMutexGuard aGuard;
3461 
3462  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3463  if (pDoc)
3464  {
3468  }
3469 }
3470 
3471 static void doc_registerCallback(LibreOfficeKitDocument* pThis,
3472  LibreOfficeKitCallback pCallback,
3473  void* pData)
3474 {
3475  SolarMutexGuard aGuard;
3477 
3478  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
3479 
3480  const int nView = SfxLokHelper::getView();
3481  if (nView < 0)
3482  return;
3483 
3484  const size_t nId = nView;
3485  if (pCallback != nullptr)
3486  {
3487  for (auto& pair : pDocument->mpCallbackFlushHandlers)
3488  {
3489  if (pair.first == nId)
3490  continue;
3491 
3492  pair.second->addViewStates(nView);
3493  }
3494  }
3495  else
3496  {
3497  for (auto& pair : pDocument->mpCallbackFlushHandlers)
3498  {
3499  if (pair.first == nId)
3500  continue;
3501 
3502  pair.second->removeViewStates(nView);
3503  }
3504  }
3505 
3506  pDocument->mpCallbackFlushHandlers[nView] = std::make_shared<CallbackFlushHandler>(pThis, pCallback, pData);
3507 
3508  if (pCallback != nullptr)
3509  {
3510  for (const auto& pair : pDocument->mpCallbackFlushHandlers)
3511  {
3512  if (pair.first == nId)
3513  continue;
3514 
3515  pDocument->mpCallbackFlushHandlers[nView]->addViewStates(pair.first);
3516  }
3517 
3518  if (SfxViewShell* pViewShell = SfxViewShell::Current())
3519  {
3521  CallbackFlushHandler::callback, pDocument->mpCallbackFlushHandlers[nView].get());
3522  }
3523  }
3524  else
3525  {
3526  if (SfxViewShell* pViewShell = SfxViewShell::Current())
3527  {
3528  pViewShell->registerLibreOfficeKitViewCallback(nullptr, nullptr);
3529  }
3530  }
3531 }
3532 
3534 static char* getPostIts(LibreOfficeKitDocument* pThis)
3535 {
3537  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3538  if (!pDoc)
3539  {
3540  SetLastExceptionMsg("Document doesn't support tiled rendering");
3541  return nullptr;
3542  }
3543  tools::JsonWriter aJsonWriter;
3544  pDoc->getPostIts(aJsonWriter);
3545  return aJsonWriter.extractData();
3546 }
3547 
3549 static char* getPostItsPos(LibreOfficeKitDocument* pThis)
3550 {
3552  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3553  if (!pDoc)
3554  {
3555  SetLastExceptionMsg("Document doesn't support tiled rendering");
3556  return nullptr;
3557  }
3558  tools::JsonWriter aJsonWriter;
3559  pDoc->getPostItsPos(aJsonWriter);
3560  return aJsonWriter.extractData();
3561 }
3562 
3563 static char* getRulerState(LibreOfficeKitDocument* pThis)
3564 {
3566  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3567  if (!pDoc)
3568  {
3569  SetLastExceptionMsg("Document doesn't support tiled rendering");
3570  return nullptr;
3571  }
3572  tools::JsonWriter aJsonWriter;
3573  pDoc->getRulerState(aJsonWriter);
3574  return aJsonWriter.extractData();
3575 }
3576 
3577 static void doc_postKeyEvent(LibreOfficeKitDocument* pThis, int nType, int nCharCode, int nKeyCode)
3578 {
3579  comphelper::ProfileZone aZone("doc_postKeyEvent");
3580 
3581  SolarMutexGuard aGuard;
3583 
3584  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3585  if (!pDoc)
3586  {
3587  SetLastExceptionMsg("Document doesn't support tiled rendering");
3588  return;
3589  }
3590 
3591  try
3592  {
3593  pDoc->postKeyEvent(nType, nCharCode, nKeyCode);
3594  }
3595  catch (const uno::Exception& exception)
3596  {
3597  SetLastExceptionMsg(exception.Message);
3598  SAL_INFO("lok", "Failed to postKeyEvent " << exception.Message);
3599  }
3600 }
3601 
3602 static void doc_setFreemiumDenyList(LibreOfficeKitDocument* /*pThis*/, const char* freemiumDenyList)
3603 {
3605 }
3606 
3607 static void doc_setFreemiumView(LibreOfficeKitDocument* /*pThis*/, int nViewId, bool isFreemium)
3608 {
3609  SolarMutexGuard aGuard;
3610  SfxLokHelper::setFreemiumView(nViewId, isFreemium);
3611 }
3612 
3613 static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis, unsigned nWindowId, int nType, const char* pText)
3614 {
3615  comphelper::ProfileZone aZone("doc_postWindowExtTextInputEvent");
3616 
3617  SolarMutexGuard aGuard;
3618  VclPtr<vcl::Window> pWindow;
3619  if (nWindowId == 0)
3620  {
3621  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3622  if (!pDoc)
3623  {
3624  SetLastExceptionMsg("Document doesn't support tiled rendering");
3625  return;
3626  }
3627  pWindow = pDoc->getDocWindow();
3628  }
3629  else
3630  {
3631  pWindow = vcl::Window::FindLOKWindow(nWindowId);
3632  }
3633 
3634  if (!pWindow)
3635  {
3636  SetLastExceptionMsg("No window found for window id: " + OUString::number(nWindowId));
3637  return;
3638  }
3639 
3640  SfxLokHelper::postExtTextEventAsync(pWindow, nType, OUString::fromUtf8(std::string_view(pText, strlen(pText))));
3641 }
3642 
3643 static void doc_removeTextContext(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId, int nCharBefore, int nCharAfter)
3644 {
3645  SolarMutexGuard aGuard;
3646  VclPtr<vcl::Window> pWindow;
3647  if (nLOKWindowId == 0)
3648  {
3649  ITiledRenderable* pDoc = getTiledRenderable(pThis);
3650  if (!pDoc)
3651  {
3652  gImpl->maLastExceptionMsg = "Document doesn't support tiled rendering";
3653  return;
3654  }
3655  pWindow = pDoc->getDocWindow();
3656  }
3657  else
3658  {
3659  pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
3660  }
3661 
3662  if (!pWindow)
3663  {
3664  gImpl->maLastExceptionMsg = "No window found for window id: " + OUString::number(nLOKWindowId);
3665  return;
3666  }
3667 
3668  // Annoyingly - backspace and delete are handled in the apps via an accelerator
3669  // which are PostMessage'd by SfxViewShell::ExecKey_Impl so to stay in the same
3670  // order we do this synchronously here, unless we're in a dialog.
3671  if (nCharBefore > 0)
3672  {
3673  // backspace
3674  if (nLOKWindowId == 0)
3675  {
3676  KeyEvent aEvt(8, 1283);
3677  for (int i = 0; i < nCharBefore; ++i)
3678  pWindow->KeyInput(aEvt);
3679  }
3680  else
3681  SfxLokHelper::postKeyEventAsync(pWindow, LOK_KEYEVENT_KEYINPUT, 8, 1283, nCharBefore - 1);
3682  }
3683 
3684  if (nCharAfter > 0)
3685  {
3686  // delete (forward)
3687  if (nLOKWindowId == 0)
3688  {
3689  KeyEvent aEvt(46, 1286);
3690  for (int i = 0; i < nCharAfter; ++i)
3691  pWindow->KeyInput(aEvt);
3692  }
3693  else
3694  SfxLokHelper::postKeyEventAsync(pWindow, LOK_KEYEVENT_KEYINPUT, 46, 1286, nCharAfter - 1);
3695  }
3696 }
3697 
3698 static void doc_postWindowKeyEvent(LibreOfficeKitDocument* /*pThis*/, unsigned nLOKWindowId, int nType, int nCharCode, int nKeyCode)
3699 {
3700  comphelper::ProfileZone aZone("doc_postWindowKeyEvent");
3701 
3702  SolarMutexGuard aGuard;
3704 
3705  VclPtr<Window> pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
3706  if (!pWindow)
3707  {
3708  SetLastExceptionMsg("Document doesn't support dialog rendering, or window not found.");
3709  return;
3710  }
3711 
3712  KeyEvent aEvent(nCharCode, nKeyCode, 0);
3713 
3714  switch (nType)
3715  {
3716  case LOK_KEYEVENT_KEYINPUT:
3717  Application::PostKeyEvent(VclEventId::WindowKeyInput, pWindow, &aEvent);
3718  break;
3719  case LOK_KEYEVENT_KEYUP:
3720  Application::PostKeyEvent(VclEventId::WindowKeyUp, pWindow, &aEvent);
3721  break;
3722  default:
3723  assert(false);
3724  break;
3725  }
3726 }
3727 
3728 static size_t doc_renderShapeSelection(LibreOfficeKitDocument* pThis, char** pOutput)
3729 {
3730  comphelper::ProfileZone aZone("doc_renderShapeSelection");
3731 
3732  SolarMutexGuard aGuard;
3734 
3735  LokChartHelper aChartHelper(SfxViewShell::Current());
3736 
3737  if (aChartHelper.GetWindow())
3738  return 0;
3739 
3740  try
3741  {
3742  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
3743 
3744  uno::Reference<frame::XStorable> xStorable(pDocument->mxComponent, uno::UNO_QUERY_THROW);
3745 
3746  SvMemoryStream aOutStream;
3747  uno::Reference<io::XOutputStream> xOut = new utl::OOutputStreamWrapper(aOutStream);
3748 
3749  utl::MediaDescriptor aMediaDescriptor;
3750  switch (doc_getDocumentType(pThis))
3751  {
3752  case LOK_DOCTYPE_PRESENTATION:
3753  aMediaDescriptor["FilterName"] <<= OUString("impress_svg_Export");
3754  break;
3755  case LOK_DOCTYPE_DRAWING:
3756  aMediaDescriptor["FilterName"] <<= OUString("draw_svg_Export");
3757  break;
3758  case LOK_DOCTYPE_TEXT:
3759  aMediaDescriptor["FilterName"] <<= OUString("writer_svg_Export");
3760  break;
3761  case LOK_DOCTYPE_SPREADSHEET:
3762  aMediaDescriptor["FilterName"] <<= OUString("calc_svg_Export");
3763  break;
3764  default:
3765  SAL_WARN("lok", "Failed to render shape selection: Document type is not supported");
3766  }
3767  aMediaDescriptor["SelectionOnly"] <<= true;
3768  aMediaDescriptor["OutputStream"] <<= xOut;
3769 
3770  xStorable->storeToURL("private:stream", aMediaDescriptor.getAsConstPropertyValueList());
3771 
3772  if (pOutput)
3773  {
3774  const size_t nOutputSize = aOutStream.GetEndOfData();
3775  *pOutput = static_cast<char*>(malloc(nOutputSize));
3776  if (*pOutput)
3777  {
3778  std::memcpy(*pOutput, aOutStream.GetData(), nOutputSize);
3779  return nOutputSize;
3780  }
3781  }
3782  }
3783  catch (const uno::Exception& exception)
3784  {
3785  css::uno::Any exAny( cppu::getCaughtException() );
3786  SetLastExceptionMsg(exception.Message);
3787  SAL_WARN("lok", "Failed to render shape selection: " << exceptionToString(exAny));
3788  }
3789 
3790  return 0;
3791 }
3792 
3793 namespace {
3794 
3802 class DispatchResultListener : public cppu::WeakImplHelper<css::frame::XDispatchResultListener>
3803 {
3804  OString maCommand;
3805  std::shared_ptr<CallbackFlushHandler> mpCallback;
3806 
3807 public:
3808  DispatchResultListener(const char* pCommand, std::shared_ptr<CallbackFlushHandler> const & pCallback)
3809  : maCommand(pCommand)
3810  , mpCallback(pCallback)
3811  {
3812  assert(mpCallback);
3813  }
3814 
3815  virtual void SAL_CALL dispatchFinished(const css::frame::DispatchResultEvent& rEvent) override
3816  {
3817  tools::JsonWriter aJson;
3818  aJson.put("commandName", maCommand);
3819 
3820  if (rEvent.State != frame::DispatchResultState::DONTKNOW)
3821  {
3822  bool bSuccess = (rEvent.State == frame::DispatchResultState::SUCCESS);
3823  aJson.put("success", bSuccess);
3824  }
3825 
3826  unoAnyToJson(aJson, "result", rEvent.Result);
3827  mpCallback->queue(LOK_CALLBACK_UNO_COMMAND_RESULT, aJson.extractData());
3828  }
3829 
3830  virtual void SAL_CALL disposing(const css::lang::EventObject&) override {}
3831 };
3832 
3833 } // anonymous namespace
3834 
3835 
3836 static void lcl_sendDialogEvent(unsigned long long int nWindowId, const char* pArguments)
3837 {
3838  SolarMutexGuard aGuard;
3839 
3841 
3842  if (aMap.find("id") == aMap.end())
3843  return;
3844 
3845  sal_uInt64 nCurrentShellId = reinterpret_cast<sal_uInt64>(SfxViewShell::Current());
3846 
3847  try
3848  {
3849  OString sControlId = OUStringToOString(aMap["id"], RTL_TEXTENCODING_ASCII_US);
3850 
3851  // dialogs send own id but notebookbar and sidebar controls are remembered by SfxViewShell id
3852  bool bFoundWeldedControl = jsdialog::ExecuteAction(nWindowId, sControlId, aMap);
3853  if (!bFoundWeldedControl)
3854  bFoundWeldedControl = jsdialog::ExecuteAction(nCurrentShellId, sControlId, aMap);
3855 
3856  if (bFoundWeldedControl)
3857  return;
3858 
3859  // force resend - used in mobile-wizard
3860  jsdialog::SendFullUpdate(nCurrentShellId, "Panel");
3861 
3862  } catch(...) {}
3863 }
3864 
3865 
3866 static void doc_sendDialogEvent(LibreOfficeKitDocument* /*pThis*/, unsigned long long int nWindowId, const char* pArguments)
3867 {
3868  lcl_sendDialogEvent(nWindowId, pArguments);
3869 }
3870 
3871 static void lo_sendDialogEvent(LibreOfficeKit* /*pThis*/, unsigned long long int nWindowId, const char* pArguments)
3872 {
3873  lcl_sendDialogEvent(nWindowId, pArguments);
3874 }
3875 
3876 static void lo_setOption(LibreOfficeKit* /*pThis*/, const char *pOption, const char* pValue)
3877 {
3878  static char* pCurrentSalLogOverride = nullptr;
3879 
3880  if (strcmp(pOption, "traceeventrecording") == 0)
3881  {
3882  if (strcmp(pValue, "start") == 0)
3883  {
3884  comphelper::TraceEvent::setBufferSizeAndCallback(100, TraceEventDumper::flushRecordings);
3886  if (traceEventDumper == nullptr)
3887  traceEventDumper = new TraceEventDumper();
3888  }
3889  else if (strcmp(pValue, "stop") == 0)
3891  }
3892  else if (strcmp(pOption, "sallogoverride") == 0)
3893  {
3894  if (pCurrentSalLogOverride != nullptr)
3895  free(pCurrentSalLogOverride);
3896  if (pValue == nullptr)
3897  pCurrentSalLogOverride = nullptr;
3898  else
3899  pCurrentSalLogOverride = strdup(pValue);
3900 
3901  if (pCurrentSalLogOverride == nullptr || pCurrentSalLogOverride[0] == '\0')
3902  sal_detail_set_log_selector(nullptr);
3903  else
3904  sal_detail_set_log_selector(pCurrentSalLogOverride);
3905  }
3906 }
3907 
3908 static void doc_postUnoCommand(LibreOfficeKitDocument* pThis, const char* pCommand, const char* pArguments, bool bNotifyWhenFinished)
3909 {
3910  comphelper::ProfileZone aZone("doc_postUnoCommand");
3911 
3912  SolarMutexGuard aGuard;
3914 
3916  OUString aCommand(pCommand, strlen(pCommand), RTL_TEXTENCODING_UTF8);
3917  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
3918 
3919  std::vector<beans::PropertyValue> aPropertyValuesVector(jsonToPropertyValuesVector(pArguments));
3920 
3921  if (!vcl::lok::isUnipoll())
3922  {
3923  beans::PropertyValue aSynchronMode;
3924  aSynchronMode.Name = "SynchronMode";
3925  aSynchronMode.Value <<= false;
3926  aPropertyValuesVector.push_back(aSynchronMode);
3927  }
3928 
3929  int nView = SfxLokHelper::getView();
3930  if (nView < 0)
3931  return;
3932 
3933  if (gImpl && aCommand == ".uno:ToggleOrientation")
3934  {
3935  ExecuteOrientationChange();
3936  return;
3937  }
3938 
3939  // handle potential interaction
3940  if (gImpl && aCommand == ".uno:Save")
3941  {
3942  // Check if saving a PDF file
3943  OUString aMimeType = lcl_getCurrentDocumentMimeType(pDocument);
3944  if (pDocSh->IsModified() && aMimeType == "application/pdf")
3945  {
3946  // If we have a PDF file (for saving annotations for example), we need
3947  // to run save-as to the same file as the opened document. Plain save
3948  // doesn't work as the PDF is not a "native" format.
3949  uno::Reference<frame::XStorable> xStorable(pDocument->mxComponent, uno::UNO_QUERY_THROW);
3950  OUString aURL = xStorable->getLocation();
3951  OString aURLUtf8 = OUStringToOString(aURL, RTL_TEXTENCODING_UTF8);
3952  bool bResult = doc_saveAs(pThis, aURLUtf8.getStr(), "pdf", nullptr);
3953 
3954  // Send the result of save
3955  tools::JsonWriter aJson;
3956  aJson.put("commandName", pCommand);
3957  aJson.put("success", bResult);
3958  pDocument->mpCallbackFlushHandlers[nView]->queue(LOK_CALLBACK_UNO_COMMAND_RESULT, aJson.extractData());
3959  return;
3960  }
3961 
3962 
3963  rtl::Reference<LOKInteractionHandler> const pInteraction(
3964  new LOKInteractionHandler("save", gImpl, pDocument));
3965  uno::Reference<task::XInteractionHandler2> const xInteraction(pInteraction);
3966 
3967  beans::PropertyValue aValue;
3968  aValue.Name = "InteractionHandler";
3969  aValue.Value <<= xInteraction;
3970  aPropertyValuesVector.push_back(aValue);
3971 
3972  bool bDontSaveIfUnmodified = false;
3973  aPropertyValuesVector.erase(std::remove_if(aPropertyValuesVector.begin(),
3974  aPropertyValuesVector.end(),
3975  [&bDontSaveIfUnmodified](const beans::PropertyValue& aItem){
3976  if (aItem.Name == "DontSaveIfUnmodified")
3977  {
3978  bDontSaveIfUnmodified = aItem.Value.get<bool>();
3979  return true;
3980  }
3981  return false;
3982  }), aPropertyValuesVector.end());
3983 
3984  // skip saving and tell the result via UNO_COMMAND_RESULT
3985  if (bDontSaveIfUnmodified && !pDocSh->IsModified())
3986  {
3987  tools::JsonWriter aJson;
3988  aJson.put("commandName", pCommand);
3989  aJson.put("success", false);
3990  // Add the reason for not saving
3991  {
3992  auto resultNode = aJson.startNode("result");
3993  aJson.put("type", "string");
3994  aJson.put("value", "unmodified");
3995  }
3996  pDocument->mpCallbackFlushHandlers[nView]->queue(LOK_CALLBACK_UNO_COMMAND_RESULT, aJson.extractData());
3997  return;
3998  }
3999  }
4000  else if (gImpl && aCommand == ".uno:TransformDialog")
4001  {
4002  bool bNeedConversion = false;
4003  SfxViewShell* pViewShell = SfxViewShell::Current();
4004  LokChartHelper aChartHelper(pViewShell);
4005 
4006  if (aChartHelper.GetWindow() )
4007  {
4008  bNeedConversion = true;
4009  }
4010  else if (const SdrView* pView = pViewShell->GetDrawView())
4011  {
4012  if (OutputDevice* pOutputDevice = pView->GetFirstOutputDevice())
4013  {
4014  bNeedConversion = (pOutputDevice->GetMapMode().GetMapUnit() == MapUnit::Map100thMM);
4015  }
4016  }
4017 
4018  if (bNeedConversion)
4019  {
4020  sal_Int32 value;
4021  for (beans::PropertyValue& rPropValue: aPropertyValuesVector)
4022  {
4023  if (rPropValue.Name == "TransformPosX"
4024  || rPropValue.Name == "TransformPosY"
4025  || rPropValue.Name == "TransformWidth"
4026  || rPropValue.Name == "TransformHeight"
4027  || rPropValue.Name == "TransformRotationX"
4028  || rPropValue.Name == "TransformRotationY")
4029  {
4030  rPropValue.Value >>= value;
4031  value = OutputDevice::LogicToLogic(value, MapUnit::MapTwip, MapUnit::Map100thMM);
4032  rPropValue.Value <<= value;
4033  }
4034  }
4035  }
4036 
4037  if (aChartHelper.GetWindow() && aPropertyValuesVector.size() > 0)
4038  {
4039  if (aPropertyValuesVector[0].Name != "Action")
4040  {
4041  tools::Rectangle aChartBB = aChartHelper.GetChartBoundingBox();
4042 
4043  int nLeft = o3tl::convert(aChartBB.Left(), o3tl::Length::twip, o3tl::Length::mm100);
4044  int nTop = o3tl::convert(aChartBB.Top(), o3tl::Length::twip, o3tl::Length::mm100);
4045 
4046  for (beans::PropertyValue& rPropValue: aPropertyValuesVector)
4047  {
4048  if (rPropValue.Name == "TransformPosX" || rPropValue.Name == "TransformRotationX")
4049  {
4050  auto const value = *o3tl::doAccess<sal_Int32>(rPropValue.Value);
4051  rPropValue.Value <<= value - nLeft;
4052  }
4053  else if (rPropValue.Name == "TransformPosY" || rPropValue.Name == "TransformRotationY")
4054  {
4055  auto const value = *o3tl::doAccess<sal_Int32>(rPropValue.Value);
4056  rPropValue.Value <<= value - nTop;
4057  }
4058  }
4059  }
4060  util::URL aCommandURL;
4061  aCommandURL.Path = "LOKTransform";
4062  css::uno::Reference<css::frame::XDispatch>& aChartDispatcher = aChartHelper.GetXDispatcher();
4063  aChartDispatcher->dispatch(aCommandURL, comphelper::containerToSequence(aPropertyValuesVector));
4064  return;
4065  }
4066  }
4067  else if (gImpl && aCommand == ".uno:LOKSidebarWriterPage")
4068  {
4069  setupSidebar(u"WriterPageDeck");
4070  return;
4071  }
4072  else if (gImpl && aCommand == ".uno:SidebarShow")
4073  {
4074  setupSidebar();
4075  return;
4076  }
4077  else if (gImpl && aCommand == ".uno:SidebarHide")
4078  {
4079  hideSidebar();
4080  return;
4081  }
4082 
4083  bool bResult = false;
4084  LokChartHelper aChartHelper(SfxViewShell::Current());
4085 
4086  if (aChartHelper.GetWindow() && aCommand != ".uno:Save" )
4087  {
4088  util::URL aCommandURL;
4089  aCommandURL.Path = aCommand.copy(5);
4090  css::uno::Reference<css::frame::XDispatch>& aChartDispatcher = aChartHelper.GetXDispatcher();
4091  aChartDispatcher->dispatch(aCommandURL, comphelper::containerToSequence(aPropertyValuesVector));
4092  return;
4093  }
4094  else if (bNotifyWhenFinished && pDocument->mpCallbackFlushHandlers.count(nView))
4095  {
4096  bResult = comphelper::dispatchCommand(aCommand, comphelper::containerToSequence(aPropertyValuesVector),
4097  new DispatchResultListener(pCommand, pDocument->mpCallbackFlushHandlers[nView]));
4098  }
4099  else
4100  bResult = comphelper::dispatchCommand(aCommand, comphelper::containerToSequence(aPropertyValuesVector));
4101 
4102  if (!bResult)
4103  {
4104  SetLastExceptionMsg("Failed to dispatch " + aCommand);
4105  }
4106 }
4107 
4108 static void doc_postMouseEvent(LibreOfficeKitDocument* pThis, int nType, int nX, int nY, int nCount, int nButtons, int nModifier)
4109 {
4110  comphelper::ProfileZone aZone("doc_postMouseEvent");
4111 
4112  SolarMutexGuard aGuard;
4114 
4115  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4116  if (!pDoc)
4117  {
4118  SetLastExceptionMsg("Document doesn't support tiled rendering");
4119  return;
4120  }
4121  try
4122  {
4123  pDoc->postMouseEvent(nType, nX, nY, nCount, nButtons, nModifier);
4124  }
4125  catch (const uno::Exception& exception)
4126  {
4127  SetLastExceptionMsg(exception.Message);
4128  SAL_INFO("lok", "Failed to postMouseEvent " << exception.Message);
4129  }
4130 }
4131 
4132 static void doc_postWindowMouseEvent(LibreOfficeKitDocument* /*pThis*/, unsigned nLOKWindowId, int nType, int nX, int nY, int nCount, int nButtons, int nModifier)
4133 {
4134  comphelper::ProfileZone aZone("doc_postWindowMouseEvent");
4135 
4136  SolarMutexGuard aGuard;
4138 
4139  VclPtr<Window> pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
4140  if (!pWindow)
4141  {
4142  SetLastExceptionMsg("Document doesn't support dialog rendering, or window not found.");
4143  return;
4144  }
4145 
4146  const Point aPos(nX, nY);
4147 
4148  MouseEvent aEvent(aPos, nCount, MouseEventModifiers::SIMPLECLICK, nButtons, nModifier);
4149 
4150  vcl::EnableDialogInput(pWindow);
4151 
4152  switch (nType)
4153  {
4154  case LOK_MOUSEEVENT_MOUSEBUTTONDOWN:
4155  Application::PostMouseEvent(VclEventId::WindowMouseButtonDown, pWindow, &aEvent);
4156  break;
4157  case LOK_MOUSEEVENT_MOUSEBUTTONUP:
4158  Application::PostMouseEvent(VclEventId::WindowMouseButtonUp, pWindow, &aEvent);
4159  break;
4160  case LOK_MOUSEEVENT_MOUSEMOVE:
4161  Application::PostMouseEvent(VclEventId::WindowMouseMove, pWindow, &aEvent);
4162  break;
4163  default:
4164  assert(false);
4165  break;
4166  }
4167 }
4168 
4169 static void doc_postWindowGestureEvent(LibreOfficeKitDocument* /*pThis*/, unsigned nLOKWindowId, const char* pType, int nX, int nY, int nOffset)
4170 {
4171  comphelper::ProfileZone aZone("doc_postWindowGestureEvent");
4172 
4173  SolarMutexGuard aGuard;
4175 
4176  VclPtr<Window> pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
4177  if (!pWindow)
4178  {
4179  SetLastExceptionMsg("Document doesn't support dialog rendering, or window not found.");
4180  return;
4181  }
4182 
4183  OString aType(pType);
4184  GestureEventType eEventType = GestureEventType::PanningUpdate;
4185 
4186  if (aType == "panBegin")
4187  eEventType = GestureEventType::PanningBegin;
4188  else if (aType == "panEnd")
4189  eEventType = GestureEventType::PanningEnd;
4190 
4192  sal_Int32(nX),
4193  sal_Int32(nY),
4194  eEventType,
4195  sal_Int32(nOffset),
4196  PanningOrientation::Vertical,
4197  };
4198 
4199  vcl::EnableDialogInput(pWindow);
4200 
4201  Application::PostGestureEvent(VclEventId::WindowGestureEvent, pWindow, &aEvent);
4202 }
4203 
4204 static void doc_setTextSelection(LibreOfficeKitDocument* pThis, int nType, int nX, int nY)
4205 {
4206  comphelper::ProfileZone aZone("doc_setTextSelection");
4207 
4208  SolarMutexGuard aGuard;
4210 
4211  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4212  if (!pDoc)
4213  {
4214  SetLastExceptionMsg("Document doesn't support tiled rendering");
4215  return;
4216  }
4217 
4218  pDoc->setTextSelection(nType, nX, nY);
4219 }
4220 
4221 static void doc_setWindowTextSelection(LibreOfficeKitDocument* /*pThis*/, unsigned nLOKWindowId, bool swap, int nX, int nY)
4222 {
4223  comphelper::ProfileZone aZone("doc_setWindowTextSelection");
4224 
4225  SolarMutexGuard aGuard;
4227 
4228  VclPtr<Window> pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
4229  if (!pWindow)
4230  {
4231  SetLastExceptionMsg("Document doesn't support dialog rendering, or window not found.");
4232  return;
4233  }
4234 
4235 
4236  Size aOffset(pWindow->GetOutOffXPixel(), pWindow->GetOutOffYPixel());
4237  Point aCursorPos(nX, nY);
4238  aCursorPos.Move(aOffset);
4239  sal_uInt16 nModifier = swap ? KEY_MOD1 + KEY_MOD2 : KEY_SHIFT;
4240 
4241  MouseEvent aCursorEvent(aCursorPos, 1, MouseEventModifiers::SIMPLECLICK, 0, nModifier);
4242  Application::PostMouseEvent(VclEventId::WindowMouseButtonDown, pWindow, &aCursorEvent);
4243  Application::PostMouseEvent(VclEventId::WindowMouseButtonUp, pWindow, &aCursorEvent);
4244 }
4245 
4246 static bool getFromTransferrable(
4247  const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
4248  const OString &aInMimeType, OString &aRet);
4249 
4250 static bool encodeImageAsHTML(
4251  const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
4252  const OString &aMimeType, OString &aRet)
4253 {
4254  if (!getFromTransferrable(xTransferable, aMimeType, aRet))
4255  return false;
4256 
4257  // Encode in base64.
4258  auto aSeq = Sequence<sal_Int8>(reinterpret_cast<const sal_Int8*>(aRet.getStr()),
4259  aRet.getLength());
4260  OUStringBuffer aBase64Data;
4261  comphelper::Base64::encode(aBase64Data, aSeq);
4262 
4263  // Embed in HTML.
4264  aRet = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
4265  "<html><head>"
4266  "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><meta "
4267  "name=\"generator\" content=\""
4268  + getGenerator().toUtf8()
4269  + "\"/>"
4270  "</head><body><img src=\"data:" + aMimeType + ";base64,"
4271  + aBase64Data.makeStringAndClear().toUtf8() + "\"/></body></html>";
4272 
4273  return true;
4274 }
4275 
4276 static bool encodeTextAsHTML(
4277  const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
4278  const OString &aMimeType, OString &aRet)
4279 {
4280  if (!getFromTransferrable(xTransferable, aMimeType, aRet))
4281  return false;
4282 
4283  // Embed in HTML - FIXME: needs some escaping.
4284  aRet = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
4285  "<html><head>"
4286  "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><meta "
4287  "name=\"generator\" content=\""
4288  + getGenerator().toUtf8()
4289  + "\"/></head><body><pre>" + aRet + "</pre></body></html>";
4290 
4291  return true;
4292 }
4293 
4295  const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
4296  const OString &aInMimeType, OString &aRet)
4297 {
4298  OString aMimeType(aInMimeType);
4299 
4300  // Take care of UTF-8 text here.
4301  bool bConvert = false;
4302  sal_Int32 nIndex = 0;
4303  if (aMimeType.getToken(0, ';', nIndex) == "text/plain")
4304  {
4305  if (aMimeType.getToken(0, ';', nIndex) == "charset=utf-8")
4306  {
4307  aMimeType = "text/plain;charset=utf-16";
4308  bConvert = true;
4309  }
4310  }
4311 
4312  datatransfer::DataFlavor aFlavor;
4313  aFlavor.MimeType = OUString::fromUtf8(aMimeType.getStr());
4314  if (aMimeType == "text/plain;charset=utf-16")
4315  aFlavor.DataType = cppu::UnoType<OUString>::get();
4316  else
4317  aFlavor.DataType = cppu::UnoType< uno::Sequence<sal_Int8> >::get();
4318 
4319  if (!xTransferable->isDataFlavorSupported(aFlavor))
4320  {
4321  // Try harder for HTML it is our copy/paste meta-file format
4322  if (aInMimeType == "text/html")
4323  {
4324  // Desperate measures - convert text to HTML instead.
4325  if (encodeTextAsHTML(xTransferable, "text/plain;charset=utf-8", aRet))
4326  return true;
4327  // If html is not supported, might be a graphic-selection,
4328  if (encodeImageAsHTML(xTransferable, "image/png", aRet))
4329  return true;
4330  }
4331 
4332  SetLastExceptionMsg("Flavor " + aFlavor.MimeType + " is not supported");
4333  return false;
4334  }
4335 
4336  uno::Any aAny;
4337  try
4338  {
4339  aAny = xTransferable->getTransferData(aFlavor);
4340  }
4341  catch (const css::datatransfer::UnsupportedFlavorException& e)
4342  {
4343  SetLastExceptionMsg("Unsupported flavor " + aFlavor.MimeType + " exception " + e.Message);
4344  return false;
4345  }
4346  catch (const css::uno::Exception& e)
4347  {
4348  SetLastExceptionMsg("Exception getting " + aFlavor.MimeType + " exception " + e.Message);
4349  return false;
4350  }
4351 
4352  if (aFlavor.DataType == cppu::UnoType<OUString>::get())
4353  {
4354  OUString aString;
4355  aAny >>= aString;
4356  if (bConvert)
4357  aRet = OUStringToOString(aString, RTL_TEXTENCODING_UTF8);
4358  else
4359  aRet = OString(reinterpret_cast<const char *>(aString.getStr()), aString.getLength() * sizeof(sal_Unicode));
4360  }
4361  else
4362  {
4363  uno::Sequence<sal_Int8> aSequence;
4364  aAny >>= aSequence;
4365  aRet = OString(reinterpret_cast<const char*>(aSequence.getConstArray()), aSequence.getLength());
4366  }
4367 
4368  return true;
4369 }
4370 
4371 static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMimeType, char** pUsedMimeType)
4372 {
4373  comphelper::ProfileZone aZone("doc_getTextSelection");
4374 
4375  SolarMutexGuard aGuard;
4377 
4378  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4379  if (!pDoc)
4380  {
4381  SetLastExceptionMsg("Document doesn't support tiled rendering");
4382  return nullptr;
4383  }
4384 
4385  css::uno::Reference<css::datatransfer::XTransferable> xTransferable = pDoc->getSelection();
4386  if (!xTransferable)
4387  {
4388  SetLastExceptionMsg("No selection available");
4389  return nullptr;
4390  }
4391 
4392  const char *pType = pMimeType;
4393  if (!pType || pType[0] == '\0')
4394  pType = "text/plain;charset=utf-8";
4395 
4396  OString aRet;
4397  bool bSuccess = getFromTransferrable(xTransferable, OString(pType), aRet);
4398  if (!bSuccess)
4399  return nullptr;
4400 
4401  if (pUsedMimeType) // legacy
4402  {
4403  if (pMimeType)
4404  *pUsedMimeType = strdup(pMimeType);
4405  else
4406  *pUsedMimeType = nullptr;
4407  }
4408 
4409  return convertOString(aRet);
4410 }
4411 
4412 static int doc_getSelectionType(LibreOfficeKitDocument* pThis)
4413 {
4414  comphelper::ProfileZone aZone("doc_getSelectionType");
4415 
4416  SolarMutexGuard aGuard;
4418 
4419  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4420  if (!pDoc)
4421  {
4422  SetLastExceptionMsg("Document doesn't support tiled rendering");
4423  return LOK_SELTYPE_NONE;
4424  }
4425 
4426  css::uno::Reference<css::datatransfer::XTransferable2> xTransferable(pDoc->getSelection(), css::uno::UNO_QUERY);
4427  if (!xTransferable)
4428  {
4429  SetLastExceptionMsg("No selection available");
4430  return LOK_SELTYPE_NONE;
4431  }
4432 
4433  if (xTransferable->isComplex())
4434  return LOK_SELTYPE_COMPLEX;
4435 
4436  OString aRet;
4437  bool bSuccess = getFromTransferrable(xTransferable, "text/plain;charset=utf-8", aRet);
4438  if (!bSuccess)
4439  return LOK_SELTYPE_NONE;
4440 
4441  if (aRet.getLength() > 10000)
4442  return LOK_SELTYPE_COMPLEX;
4443 
4444  return aRet.getLength() ? LOK_SELTYPE_TEXT : LOK_SELTYPE_NONE;
4445 }
4446 
4447 static int doc_getClipboard(LibreOfficeKitDocument* pThis,
4448  const char **pMimeTypes,
4449  size_t *pOutCount,
4450  char ***pOutMimeTypes,
4451  size_t **pOutSizes,
4452  char ***pOutStreams)
4453 {
4454  comphelper::ProfileZone aZone("doc_getClipboard");
4455 
4456  SolarMutexGuard aGuard;
4458 
4459  assert (pOutCount);
4460  assert (pOutMimeTypes);
4461  assert (pOutSizes);
4462  assert (pOutStreams);
4463 
4464  *pOutCount = 0;
4465  *pOutMimeTypes = nullptr;
4466  *pOutSizes = nullptr;
4467  *pOutStreams = nullptr;
4468 
4469  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4470  if (!pDoc)
4471  {
4472  SetLastExceptionMsg("Document doesn't support tiled rendering");
4473  return 0;
4474  }
4475 
4477 
4478  css::uno::Reference<css::datatransfer::XTransferable> xTransferable = xClip->getContents();
4479  SAL_INFO("lok", "Got from clip: " << xClip.get() << " transferrable: " << xTransferable);
4480  if (!xTransferable)
4481  {
4482  SetLastExceptionMsg("No clipboard content available");
4483  return 0;
4484  }
4485 
4486  std::vector<OString> aMimeTypes;
4487  if (!pMimeTypes) // everything
4488  {
4489  const uno::Sequence< css::datatransfer::DataFlavor > flavors = xTransferable->getTransferDataFlavors();
4490  if (!flavors.getLength())
4491  {
4492  SetLastExceptionMsg("Flavourless selection");
4493  return 0;
4494  }
4495  for (const auto &it : flavors)
4496  aMimeTypes.push_back(OUStringToOString(it.MimeType, RTL_TEXTENCODING_UTF8));
4497  }
4498  else
4499  {
4500  for (size_t i = 0; pMimeTypes[i]; ++i)
4501  aMimeTypes.push_back(OString(pMimeTypes[i]));
4502  }
4503 
4504  *pOutCount = aMimeTypes.size();
4505  *pOutSizes = static_cast<size_t *>(malloc(*pOutCount * sizeof(size_t)));
4506  *pOutMimeTypes = static_cast<char **>(malloc(*pOutCount * sizeof(char *)));
4507  *pOutStreams = static_cast<char **>(malloc(*pOutCount * sizeof(char *)));
4508  for (size_t i = 0; i < aMimeTypes.size(); ++i)
4509  {
4510  if (aMimeTypes[i] == "text/plain;charset=utf-16")
4511  (*pOutMimeTypes)[i] = strdup("text/plain;charset=utf-8");
4512  else
4513  (*pOutMimeTypes)[i] = strdup(aMimeTypes[i].getStr());
4514 
4515  OString aRet;
4516  bool bSuccess = getFromTransferrable(xTransferable, (*pOutMimeTypes)[i], aRet);
4517  if (!bSuccess || aRet.getLength() < 1)
4518  {
4519  (*pOutSizes)[i] = 0;
4520  (*pOutStreams)[i] = nullptr;
4521  }
4522  else
4523  {
4524  (*pOutSizes)[i] = aRet.getLength();
4525  (*pOutStreams)[i] = convertOString(aRet);
4526  }
4527  }
4528 
4529  return 1;
4530 }
4531 
4532 static int doc_setClipboard(LibreOfficeKitDocument* pThis,
4533  const size_t nInCount,
4534  const char **pInMimeTypes,
4535  const size_t *pInSizes,
4536  const char **pInStreams)
4537 {
4538 #ifdef IOS
4539  (void) pThis;
4540  (void) nInCount;
4541  (void) pInMimeTypes;
4542  (void) pInSizes;
4543  (void) pInStreams;
4544 #else
4545  comphelper::ProfileZone aZone("doc_setClipboard");
4546 
4547  SolarMutexGuard aGuard;
4549 
4550  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4551  if (!pDoc)
4552  {
4553  SetLastExceptionMsg("Document doesn't support tiled rendering");
4554  return false;
4555  }
4556 
4557  uno::Reference<datatransfer::XTransferable> xTransferable(new LOKTransferable(nInCount, pInMimeTypes, pInSizes, pInStreams));
4558 
4559  auto xClip = forceSetClipboardForCurrentView(pThis);
4560  xClip->setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
4561 
4562  SAL_INFO("lok", "Set clip: " << xClip.get() << " to: " << xTransferable);
4563 
4564  if (!pDoc->isMimeTypeSupported())
4565  {
4566  SetLastExceptionMsg("Document doesn't support this mime type");
4567  return false;
4568  }
4569 #endif
4570  return true;
4571 }
4572 
4573 static bool doc_paste(LibreOfficeKitDocument* pThis, const char* pMimeType, const char* pData, size_t nSize)
4574 {
4575  comphelper::ProfileZone aZone("doc_paste");
4576 
4577  SolarMutexGuard aGuard;
4578 
4579  const char *pInMimeTypes[1];
4580  const char *pInStreams[1];
4581  size_t pInSizes[1];
4582  pInMimeTypes[0] = pMimeType;
4583  pInSizes[0] = nSize;
4584  pInStreams[0] = pData;
4585 
4586  if (!doc_setClipboard(pThis, 1, pInMimeTypes, pInSizes, pInStreams))
4587  return false;
4588 
4589  uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence(
4590  {
4591  {"AnchorType", uno::makeAny(static_cast<sal_uInt16>(css::text::TextContentAnchorType_AS_CHARACTER))},
4592  {"IgnoreComments", uno::makeAny(true)},
4593  }));
4594  if (!comphelper::dispatchCommand(".uno:Paste", aPropertyValues))
4595  {
4596  SetLastExceptionMsg("Failed to dispatch the .uno: command");
4597  return false;
4598  }
4599 
4600  return true;
4601 }
4602 
4603 static void doc_setGraphicSelection(LibreOfficeKitDocument* pThis, int nType, int nX, int nY)
4604 {
4605  comphelper::ProfileZone aZone("doc_setGraphicSelection");
4606 
4607  SolarMutexGuard aGuard;
4609 
4610  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4611  if (!pDoc)
4612  {
4613  SetLastExceptionMsg("Document doesn't support tiled rendering");
4614  return;
4615  }
4616 
4617  pDoc->setGraphicSelection(nType, nX, nY);
4618 }
4619 
4620 static void doc_resetSelection(LibreOfficeKitDocument* pThis)
4621 {
4622  comphelper::ProfileZone aZone("doc_resetSelection");
4623 
4624  SolarMutexGuard aGuard;
4626 
4627  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4628  if (!pDoc)
4629  {
4630  SetLastExceptionMsg("Document doesn't support tiled rendering");
4631  return;
4632  }
4633 
4634  pDoc->resetSelection();
4635 }
4636 
4637 static char* getLanguages(const char* pCommand)
4638 {
4639  css::uno::Sequence< css::lang::Locale > aLocales;
4640 
4641  if (xContext.is())
4642  {
4643  css::uno::Reference<css::linguistic2::XLinguServiceManager2> xLangSrv = css::linguistic2::LinguServiceManager::create(xContext);
4644  if (xLangSrv.is())
4645  {
4646  css::uno::Reference<css::linguistic2::XSpellChecker> xSpell = xLangSrv->getSpellChecker();
4647  if (xSpell.is())
4648  aLocales = xSpell->getLocales();
4649  }
4650  }
4651 
4652  boost::property_tree::ptree aTree;
4653  aTree.put("commandName", pCommand);
4654  boost::property_tree::ptree aValues;
4655  boost::property_tree::ptree aChild;
4656  OUString sLanguage;
4657  for ( css::lang::Locale const & locale : std::as_const(aLocales) )
4658  {
4659  const LanguageTag aLanguageTag( locale );
4660  sLanguage = SvtLanguageTable::GetLanguageString(aLanguageTag.getLanguageType());
4661  if (sLanguage.startsWith("{") && sLanguage.endsWith("}"))
4662  continue;
4663 
4664  sLanguage += ";" + aLanguageTag.getBcp47(false);
4665  aChild.put("", sLanguage.toUtf8());
4666  aValues.push_back(std::make_pair("", aChild));
4667  }
4668  aTree.add_child("commandValues", aValues);
4669  std::stringstream aStream;
4670  boost::property_tree::write_json(aStream, aTree);
4671  char* pJson = static_cast<char*>(malloc(aStream.str().size() + 1));
4672  assert(pJson); // Don't handle OOM conditions
4673  strcpy(pJson, aStream.str().c_str());
4674  pJson[aStream.str().size()] = '\0';
4675  return pJson;
4676 }
4677 
4678 static char* getFonts (const char* pCommand)
4679 {
4681  const SvxFontListItem* pFonts = static_cast<const SvxFontListItem*>(
4682  pDocSh->GetItem(SID_ATTR_CHAR_FONTLIST));
4683  const FontList* pList = pFonts ? pFonts->GetFontList() : nullptr;
4684 
4685  boost::property_tree::ptree aTree;
4686  aTree.put("commandName", pCommand);
4687  boost::property_tree::ptree aValues;
4688  if ( pList )
4689  {
4690  sal_uInt16 nFontCount = pList->GetFontNameCount();
4691  for (sal_uInt16 i = 0; i < nFontCount; ++i)
4692  {
4693  boost::property_tree::ptree aChildren;
4694  const FontMetric& rFontMetric = pList->GetFontName(i);
4695  const int* pAry = pList->GetSizeAry(rFontMetric);
4696  sal_uInt16 nSizeCount = 0;
4697  while (pAry[nSizeCount])
4698  {
4699  boost::property_tree::ptree aChild;
4700  aChild.put("", static_cast<float>(pAry[nSizeCount]) / 10);
4701  aChildren.push_back(std::make_pair("", aChild));
4702  nSizeCount++;
4703  }
4704  aValues.add_child(rFontMetric.GetFamilyName().toUtf8().getStr(), aChildren);
4705  }
4706  }
4707  aTree.add_child("commandValues", aValues);
4708  std::stringstream aStream;
4709  boost::property_tree::write_json(aStream, aTree);
4710  char* pJson = static_cast<char*>(malloc(aStream.str().size() + 1));
4711  assert(pJson); // Don't handle OOM conditions
4712  strcpy(pJson, aStream.str().c_str());
4713  pJson[aStream.str().size()] = '\0';
4714  return pJson;
4715 }
4716 
4717 static char* getFontSubset (std::string_view aFontName)
4718 {
4719  OUString aFoundFont(::rtl::Uri::decode(OStringToOUString(aFontName, RTL_TEXTENCODING_UTF8), rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8));
4720 
4721  boost::property_tree::ptree aTree;
4722  aTree.put("commandName", ".uno:FontSubset");
4723  boost::property_tree::ptree aValues;
4724 
4725  if (const vcl::Font* pFont = FindFont(aFoundFont))
4726  {
4727  FontCharMapRef xFontCharMap (new FontCharMap());
4728  auto aDevice(VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT));
4729 
4730  aDevice->SetFont(*pFont);
4731  aDevice->GetFontCharMap(xFontCharMap);
4732  SubsetMap aSubMap(xFontCharMap);
4733 
4734  for (auto const& subset : aSubMap.GetSubsetMap())
4735  {
4736  boost::property_tree::ptree aChild;
4737  aChild.put("", static_cast<int>(ublock_getCode(subset.GetRangeMin())));
4738  aValues.push_back(std::make_pair("", aChild));
4739  }
4740  }
4741 
4742  aTree.add_child("commandValues", aValues);
4743  std::stringstream aStream;
4744  boost::property_tree::write_json(aStream, aTree);
4745  char* pJson = static_cast<char*>(malloc(aStream.str().size() + 1));
4746  assert(pJson); // Don't handle OOM conditions
4747  strcpy(pJson, aStream.str().c_str());
4748  pJson[aStream.str().size()] = '\0';
4749  return pJson;
4750 }
4751 
4752 static char* getStyles(LibreOfficeKitDocument* pThis, const char* pCommand)
4753 {
4754  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
4755 
4756  boost::property_tree::ptree aTree;
4757  aTree.put("commandName", pCommand);
4758  uno::Reference<css::style::XStyleFamiliesSupplier> xStyleFamiliesSupplier(pDocument->mxComponent, uno::UNO_QUERY);
4759  const uno::Reference<container::XNameAccess> xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies();
4760  const uno::Sequence<OUString> aStyleFamilies = xStyleFamilies->getElementNames();
4761 
4762  static const std::vector<OUString> aWriterStyles =
4763  {
4764  "Text body",
4765  "Quotations",
4766  "Title",
4767  "Subtitle",
4768  "Heading 1",
4769  "Heading 2",
4770  "Heading 3"
4771  };
4772 
4773  // We need to keep a list of the default style names
4774  // in order to filter these out later when processing
4775  // the full list of styles.
4776  std::set<OUString> aDefaultStyleNames;
4777 
4778  boost::property_tree::ptree aValues;
4779  for (OUString const & sStyleFam : aStyleFamilies)
4780  {
4781  boost::property_tree::ptree aChildren;
4782  uno::Reference<container::XNameAccess> xStyleFamily(xStyleFamilies->getByName(sStyleFam), uno::UNO_QUERY);
4783 
4784  // Writer provides a huge number of styles, we have a list of 7 "default" styles which
4785  // should be shown in the normal dropdown, which we should add to the start of the list
4786  // to simplify their selection.
4787  if (sStyleFam == "ParagraphStyles"
4788  && doc_getDocumentType(pThis) == LOK_DOCTYPE_TEXT)
4789  {
4790  for (const OUString& rStyle: aWriterStyles)
4791  {
4792  aDefaultStyleNames.insert( rStyle );
4793 
4794  boost::property_tree::ptree aChild;
4795  aChild.put("", rStyle.toUtf8());
4796  aChildren.push_back(std::make_pair("", aChild));
4797  }
4798  }
4799 
4800  const uno::Sequence<OUString> aStyles = xStyleFamily->getElementNames();
4801  for (const OUString& rStyle: aStyles )
4802  {
4803  // Filter out the default styles - they are already at the top
4804  // of the list
4805  if (aDefaultStyleNames.find(rStyle) == aDefaultStyleNames.end() ||
4806  (sStyleFam != "ParagraphStyles" || doc_getDocumentType(pThis) != LOK_DOCTYPE_TEXT) )
4807  {
4808  boost::property_tree::ptree aChild;
4809  aChild.put("", rStyle.toUtf8());
4810  aChildren.push_back(std::make_pair("", aChild));
4811  }
4812  }
4813  aValues.add_child(sStyleFam.toUtf8().getStr(), aChildren);
4814  }
4815 
4816  // Header & Footer Styles
4817  {
4818  boost::property_tree::ptree aChild;
4819  boost::property_tree::ptree aChildren;
4820  static const OUStringLiteral sPageStyles(u"PageStyles");
4821  uno::Reference<beans::XPropertySet> xProperty;
4822  uno::Reference<container::XNameContainer> xContainer;
4823 
4824  if (xStyleFamilies->hasByName(sPageStyles) && (xStyleFamilies->getByName(sPageStyles) >>= xContainer))
4825  {
4826  const uno::Sequence<OUString> aSeqNames = xContainer->getElementNames();
4827  for (OUString const & sName : aSeqNames)
4828  {
4829  bool bIsPhysical;
4830  xProperty.set(xContainer->getByName(sName), uno::UNO_QUERY);
4831  if (xProperty.is() && (xProperty->getPropertyValue("IsPhysical") >>= bIsPhysical) && bIsPhysical)
4832  {
4833  OUString displayName;
4834  xProperty->getPropertyValue("DisplayName") >>= displayName;
4835  aChild.put("", displayName.toUtf8());
4836  aChildren.push_back(std::make_pair("", aChild));
4837  }
4838  }
4839  aValues.add_child("HeaderFooter", aChildren);
4840  }
4841  }
4842 
4843  {
4844  boost::property_tree::ptree aCommandList;
4845 
4846  {
4847  boost::property_tree::ptree aChild;
4848 
4849  OUString sClearFormat = SvxResId(RID_SVXSTR_CLEARFORM);
4850 
4851  boost::property_tree::ptree aName;
4852  aName.put("", sClearFormat.toUtf8());
4853  aChild.push_back(std::make_pair("text", aName));
4854 
4855  boost::property_tree::ptree aCommand;
4856  aCommand.put("", ".uno:ResetAttributes");
4857  aChild.push_back(std::make_pair("id", aCommand));
4858 
4859  aCommandList.push_back(std::make_pair("", aChild));
4860  }
4861 
4862  aValues.add_child("Commands", aCommandList);
4863  }
4864 
4865  aTree.add_child("commandValues", aValues);
4866  std::stringstream aStream;
4867  boost::property_tree::write_json(aStream, aTree);
4868  char* pJson = static_cast<char*>(malloc(aStream.str().size() + 1));
4869  assert(pJson); // Don't handle OOM conditions
4870  strcpy(pJson, aStream.str().c_str());
4871  pJson[aStream.str().size()] = '\0';
4872  return pJson;
4873 }
4874 
4875 namespace {
4876 
4877 enum class UndoOrRedo
4878 {
4879  UNDO,
4880  REDO
4881 };
4882 
4883 }
4884 
4886 static char* getUndoOrRedo(LibreOfficeKitDocument* pThis, UndoOrRedo eCommand)
4887 {
4888  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
4889 
4890  auto pBaseModel = dynamic_cast<SfxBaseModel*>(pDocument->mxComponent.get());
4891  if (!pBaseModel)
4892  return nullptr;
4893 
4894  SfxObjectShell* pObjectShell = pBaseModel->GetObjectShell();
4895  if (!pObjectShell)
4896  return nullptr;
4897 
4898  SfxUndoManager* pUndoManager = pObjectShell->GetUndoManager();
4899  if (!pUndoManager)
4900  return nullptr;
4901 
4902  OUString aString;
4903  if (eCommand == UndoOrRedo::UNDO)
4904  aString = pUndoManager->GetUndoActionsInfo();
4905  else
4906  aString = pUndoManager->GetRedoActionsInfo();
4907  char* pJson = strdup(aString.toUtf8().getStr());
4908  return pJson;
4909 }
4910 
4912 static char* getTrackedChanges(LibreOfficeKitDocument* pThis)
4913 {
4914  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
4915 
4916  uno::Reference<document::XRedlinesSupplier> xRedlinesSupplier(pDocument->mxComponent, uno::UNO_QUERY);
4917  tools::JsonWriter aJson;
4918  // We want positions of the track changes also which is not possible from
4919  // UNO. Enable positioning information for text documents only for now, so
4920  // construct the tracked changes JSON from inside the sw/, not here using UNO
4921  if (doc_getDocumentType(pThis) != LOK_DOCTYPE_TEXT && xRedlinesSupplier.is())
4922  {
4923  auto redlinesNode = aJson.startArray("redlines");
4924  uno::Reference<container::XEnumeration> xRedlines = xRedlinesSupplier->getRedlines()->createEnumeration();
4925  for (size_t nIndex = 0; xRedlines->hasMoreElements(); ++nIndex)
4926  {
4927  uno::Reference<beans::XPropertySet> xRedline(xRedlines->nextElement(), uno::UNO_QUERY);
4928  auto redlineNode = aJson.startStruct();
4929  aJson.put("index", static_cast<sal_Int32>(nIndex));
4930 
4931  OUString sAuthor;
4932  xRedline->getPropertyValue("RedlineAuthor") >>= sAuthor;
4933  aJson.put("author", sAuthor);
4934 
4935  OUString sType;
4936  xRedline->getPropertyValue("RedlineType") >>= sType;
4937  aJson.put("type", sType);
4938 
4939  OUString sComment;
4940  xRedline->getPropertyValue("RedlineComment") >>= sComment;
4941  aJson.put("comment", sComment);
4942 
4943  OUString sDescription;
4944  xRedline->getPropertyValue("RedlineDescription") >>= sDescription;
4945  aJson.put("description", sDescription);
4946 
4947  util::DateTime aDateTime;
4948  xRedline->getPropertyValue("RedlineDateTime") >>= aDateTime;
4949  OUString sDateTime = utl::toISO8601(aDateTime);
4950  aJson.put("dateTime", sDateTime);
4951  }
4952  }
4953  else
4954  {
4955  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4956  if (!pDoc)
4957  {
4958  SetLastExceptionMsg("Document doesn't support tiled rendering");
4959  return nullptr;
4960  }
4961  pDoc->getTrackedChanges(aJson);
4962  }
4963 
4964  return aJson.extractData();
4965 }
4966 
4967 
4969 static char* getTrackedChangeAuthors(LibreOfficeKitDocument* pThis)
4970 {
4971  ITiledRenderable* pDoc = getTiledRenderable(pThis);
4972  if (!pDoc)
4973  {
4974  SetLastExceptionMsg("Document doesn't support tiled rendering");
4975  return nullptr;
4976  }
4977  tools::JsonWriter aJsonWriter;
4978  pDoc->getTrackedChangeAuthors(aJsonWriter);
4979  return aJsonWriter.extractData();
4980 }
4981 
4982 static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCommand)
4983 {
4984  comphelper::ProfileZone aZone("doc_getCommandValues");
4985 
4986  SolarMutexGuard aGuard;
4988 
4989  OString aCommand(pCommand);
4990  static constexpr OStringLiteral aViewRowColumnHeaders(".uno:ViewRowColumnHeaders");
4991  static constexpr OStringLiteral aSheetGeometryData(".uno:SheetGeometryData");
4992  static constexpr OStringLiteral aCellCursor(".uno:CellCursor");
4993  static constexpr OStringLiteral aFontSubset(".uno:FontSubset&name=");
4994 
4995  if (!strcmp(pCommand, ".uno:LanguageStatus"))
4996  {
4997  return getLanguages(pCommand);
4998  }
4999  else if (!strcmp(pCommand, ".uno:CharFontName"))
5000  {
5001  return getFonts(pCommand);
5002  }
5003  else if (!strcmp(pCommand, ".uno:StyleApply"))
5004  {
5005  return getStyles(pThis, pCommand);
5006  }
5007  else if (aCommand == ".uno:Undo")
5008  {
5009  return getUndoOrRedo(pThis, UndoOrRedo::UNDO);
5010  }
5011  else if (aCommand == ".uno:Redo")
5012  {
5013  return getUndoOrRedo(pThis, UndoOrRedo::REDO);
5014  }
5015  else if (aCommand == ".uno:AcceptTrackedChanges")
5016  {
5017  return getTrackedChanges(pThis);
5018  }
5019  else if (aCommand == ".uno:TrackedChangeAuthors")
5020  {
5021  return getTrackedChangeAuthors(pThis);
5022  }
5023  else if (aCommand == ".uno:ViewAnnotations")
5024  {
5025  return getPostIts(pThis);
5026  }
5027  else if (aCommand == ".uno:ViewAnnotationsPosition")
5028  {
5029  return getPostItsPos(pThis);
5030  }
5031  else if (aCommand == ".uno:RulerState")
5032  {
5033  return getRulerState(pThis);
5034  }
5035  else if (aCommand.startsWith(aViewRowColumnHeaders))
5036  {
5037  ITiledRenderable* pDoc = getTiledRenderable(pThis);
5038  if (!pDoc)
5039  {
5040  SetLastExceptionMsg("Document doesn't support tiled rendering");
5041  return nullptr;
5042  }
5043 
5044  tools::Rectangle aRectangle;
5045  if (aCommand.getLength() > aViewRowColumnHeaders.getLength())
5046  {
5047  // Command has parameters.
5048  int nX = 0;
5049  int nY = 0;
5050  int nWidth = 0;
5051  int nHeight = 0;
5052  OString aArguments = aCommand.copy(aViewRowColumnHeaders.getLength() + 1);
5053  sal_Int32 nParamIndex = 0;
5054  do
5055  {
5056  OString aParamToken = aArguments.getToken(0, '&', nParamIndex);
5057  sal_Int32 nIndex = 0;
5058  OString aKey;
5059  OString aValue;
5060  do
5061  {
5062  OString aToken = aParamToken.getToken(0, '=', nIndex);
5063  if (!aKey.getLength())
5064  aKey = aToken;
5065  else
5066  aValue = aToken;
5067  }
5068  while (nIndex >= 0);
5069  if (aKey == "x")
5070  nX = aValue.toInt32();
5071  else if (aKey == "y")
5072  nY = aValue.toInt32();
5073  else if (aKey == "width")
5074  nWidth = aValue.toInt32();
5075  else if (aKey == "height")
5076  nHeight = aValue.toInt32();
5077  }
5078  while (nParamIndex >= 0);
5079 
5080  aRectangle = tools::Rectangle(nX, nY, nX + nWidth, nY + nHeight);
5081  }
5082 
5083  tools::JsonWriter aJsonWriter;
5084  pDoc->getRowColumnHeaders(aRectangle, aJsonWriter);
5085  return aJsonWriter.extractData();
5086  }
5087  else if (aCommand.startsWith(aSheetGeometryData))
5088  {
5089  ITiledRenderable* pDoc = getTiledRenderable(pThis);
5090  if (!pDoc)
5091  {
5092  SetLastExceptionMsg("Document doesn't support tiled rendering");
5093  return nullptr;
5094  }
5095 
5096  bool bColumns = true;
5097  bool bRows = true;
5098  bool bSizes = true;
5099  bool bHidden = true;
5100  bool bFiltered = true;
5101  bool bGroups = true;
5102  if (aCommand.getLength() > aSheetGeometryData.getLength())
5103  {
5104  bColumns = bRows = bSizes = bHidden = bFiltered = bGroups = false;
5105 
5106  OString aArguments = aCommand.copy(aSheetGeometryData.getLength() + 1);
5107  sal_Int32 nParamIndex = 0;
5108  do
5109  {
5110  OString aParamToken = aArguments.getToken(0, '&', nParamIndex);
5111  sal_Int32 nIndex = 0;
5112  OString aKey;
5113  OString aValue;
5114  do
5115  {
5116  OString aToken = aParamToken.getToken(0, '=', nIndex);
5117  if (!aKey.getLength())
5118  aKey = aToken;
5119  else
5120  aValue = aToken;
5121 
5122  } while (nIndex >= 0);
5123 
5124  bool bEnableFlag = aValue.isEmpty() ||
5125  aValue.equalsIgnoreAsciiCase("true") || aValue.toInt32() > 0;
5126  if (!bEnableFlag)
5127  continue;
5128 
5129  if (aKey == "columns")
5130  bColumns = true;
5131  else if (aKey == "rows")
5132  bRows = true;
5133  else if (aKey == "sizes")
5134  bSizes = true;
5135  else if (aKey == "hidden")
5136  bHidden = true;
5137  else if (aKey == "filtered")
5138  bFiltered = true;
5139  else if (aKey == "groups")
5140  bGroups = true;
5141 
5142  } while (nParamIndex >= 0);
5143  }
5144 
5145  OString aGeomDataStr
5146  = pDoc->getSheetGeometryData(bColumns, bRows, bSizes, bHidden, bFiltered, bGroups);
5147 
5148  if (aGeomDataStr.isEmpty())
5149  return nullptr;
5150 
5151  return convertOString(aGeomDataStr);
5152  }
5153  else if (aCommand.startsWith(aCellCursor))
5154  {
5155  ITiledRenderable* pDoc = getTiledRenderable(pThis);
5156  if (!pDoc)
5157  {
5158  SetLastExceptionMsg("Document doesn't support tiled rendering");
5159  return nullptr;
5160  }
5161  // Ignore command's deprecated parameters.
5162  tools::JsonWriter aJsonWriter;
5163  pDoc->getCellCursor(aJsonWriter);
5164  return aJsonWriter.extractData();
5165  }
5166  else if (aCommand.startsWith(aFontSubset))
5167  {
5168  return getFontSubset(std::string_view(pCommand + aFontSubset.getLength()));
5169  }
5170  else
5171  {
5172  SetLastExceptionMsg("Unknown command, no values returned");
5173  return nullptr;
5174  }
5175 }
5176 
5177 static void doc_setClientZoom(LibreOfficeKitDocument* pThis, int nTilePixelWidth, int nTilePixelHeight,
5178  int nTileTwipWidth, int nTileTwipHeight)
5179 {
5180  comphelper::ProfileZone aZone("doc_setClientZoom");
5181 
5182  SolarMutexGuard aGuard;
5184 
5185  ITiledRenderable* pDoc = getTiledRenderable(pThis);
5186  if (!pDoc)
5187  {
5188  SetLastExceptionMsg("Document doesn't support tiled rendering");
5189  return;
5190  }
5191 
5192  pDoc->setClientZoom(nTilePixelWidth, nTilePixelHeight, nTileTwipWidth, nTileTwipHeight);
5193 }
5194 
5195 static void doc_setClientVisibleArea(LibreOfficeKitDocument* pThis, int nX, int nY, int nWidth, int nHeight)
5196 {
5197  comphelper::ProfileZone aZone("doc_setClientVisibleArea");
5198 
5199  SolarMutexGuard aGuard;
5201 
5202  ITiledRenderable* pDoc = getTiledRenderable(pThis);
5203  if (!pDoc)
5204  {
5205  SetLastExceptionMsg("Document doesn't support tiled rendering");
5206  return;
5207  }
5208 
5209  tools::Rectangle aRectangle(Point(nX, nY), Size(nWidth, nHeight));
5210  pDoc->setClientVisibleArea(aRectangle);
5211 }
5212 
5213 static void doc_setOutlineState(LibreOfficeKitDocument* pThis, bool bColumn, int nLevel, int nIndex, bool bHidden)
5214 {
5215  comphelper::ProfileZone aZone("doc_setOutlineState");
5216 
5217  SolarMutexGuard aGuard;
5219 
5220  ITiledRenderable* pDoc = getTiledRenderable(pThis);
5221  if (!pDoc)
5222  {
5223  SetLastExceptionMsg("Document doesn't support tiled rendering");
5224  return;
5225  }
5226 
5227  pDoc->setOutlineState(bColumn, nLevel, nIndex, bHidden);
5228 }
5229 
5230 static int doc_createViewWithOptions(LibreOfficeKitDocument* pThis,
5231  const char* pOptions)
5232 {
5233  comphelper::ProfileZone aZone("doc_createView");
5234 
5235  SolarMutexGuard aGuard;
5237 
5238  OUString aOptions = getUString(pOptions);
5239  const OUString aLanguage = extractParameter(aOptions, u"Language");
5240 
5241  if (!aLanguage.isEmpty())
5242  {
5243  // Set the LOK language tag, used for dialog tunneling.
5246  }
5247 
5248  const OUString aDeviceFormFactor = extractParameter(aOptions, u"DeviceFormFactor");
5249  SfxLokHelper::setDeviceFormFactor(aDeviceFormFactor);
5250 
5251  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
5252  const int nId = SfxLokHelper::createView(pDocument->mnDocumentId);
5253 
5255 
5256 #ifdef IOS
5257  (void) pThis;
5258 #else
5259  forceSetClipboardForCurrentView(pThis);
5260 #endif
5261 
5262  return nId;
5263 }
5264 
5265 static int doc_createView(LibreOfficeKitDocument* pThis)
5266 {
5267  return doc_createViewWithOptions(pThis, nullptr); // No options.
5268 }
5269 
5270 static void doc_destroyView(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* pThis, int nId)
5271 {
5272  comphelper::ProfileZone aZone("doc_destroyView");
5273 
5274  SolarMutexGuard aGuard;
5276 
5278 
5280 
5281  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
5283 }
5284 
5285 static void doc_setView(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/, int nId)
5286 {
5287  comphelper::ProfileZone aZone("doc_setView");
5288 
5289  SolarMutexGuard aGuard;
5291 
5292  SfxLokHelper::setView(nId);
5293 }
5294 
5295 static int doc_getView(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/)
5296 {
5297  comphelper::ProfileZone aZone("doc_getView");
5298 
5299  SolarMutexGuard aGuard;
5301 
5302  return SfxLokHelper::getView();
5303 }
5304 
5305 static int doc_getViewsCount(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* pThis)
5306 {
5307  comphelper::ProfileZone aZone("doc_getViewsCount");
5308 
5309  SolarMutexGuard aGuard;
5311 
5312  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
5313  return SfxLokHelper::getViewsCount(pDocument->mnDocumentId);
5314 }
5315 
5316 static bool doc_getViewIds(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* pThis, int* pArray, size_t nSize)
5317 {
5318  comphelper::ProfileZone aZone("doc_getViewsIds");
5319 
5320  SolarMutexGuard aGuard;
5322 
5323  LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
5324  return SfxLokHelper::getViewIds(pDocument->mnDocumentId, pArray, nSize);
5325 }
5326 
5327 static void doc_setViewLanguage(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/, int nId, const char* language)
5328 {
5329  comphelper::ProfileZone aZone("doc_setViewLanguage");
5330 
5331  SolarMutexGuard aGuard;
5333 
5334  OUString sLanguage = OStringToOUString(language, RTL_TEXTENCODING_UTF8);
5335  SfxLokHelper::setViewLanguage(nId, sLanguage);
5336  SfxLokHelper::setViewLocale(nId, sLanguage);
5337 }
5338 
5339 
5340 
5341 unsigned char* doc_renderFont(LibreOfficeKitDocument* pThis,
5342  const char* pFontName,
5343  const char* pChar,
5344  int* pFontWidth,
5345  int* pFontHeight)
5346 {
5347  return doc_renderFontOrientation(pThis, pFontName, pChar, pFontWidth, pFontHeight, 0);
5348 }
5349 
5350 unsigned char* doc_renderFontOrientation(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/,
5351  const char* pFontName,
5352  const char* pChar,
5353  int* pFontWidth,
5354  int* pFontHeight,
5355  int pOrientation)
5356 {
5357  comphelper::ProfileZone aZone("doc_renderFont");
5358 
5359  SolarMutexGuard aGuard;
5361 
5362  const int nDefaultFontSize = 25;
5363 
5364  auto aFont = FindFont_FallbackToDefault(OStringToOUString(pFontName, RTL_TEXTENCODING_UTF8));
5365 
5366  OUString aText(OStringToOUString(pChar, RTL_TEXTENCODING_UTF8));
5367  if (aText.isEmpty())
5368  aText = aFont.GetFamilyName();
5369 
5370  auto aDevice(VclPtr<VirtualDevice>::Create(DeviceFormat::DEFAULT));
5371  ::tools::Rectangle aRect;
5372  aFont.SetFontSize(Size(0, nDefaultFontSize));
5373  aFont.S