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