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