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