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