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