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