[Libreoffice-commits] core.git: 27 commits - desktop/qa desktop/source editeng/source include/LibreOfficeKit include/vcl offapi/com sc/source sfx2/source sw/source vcl/source

Michael Meeks (via logerrit) logerrit at kemper.freedesktop.org
Fri Aug 2 15:45:26 UTC 2019


 desktop/qa/data/objects.odt                         |binary
 desktop/qa/desktop_lib/test_desktop_lib.cxx         |   61 +++
 desktop/source/lib/init.cxx                         |  331 ++++++++++++++++++--
 desktop/source/lib/lokclipboard.cxx                 |  225 +++++++++++--
 desktop/source/lib/lokclipboard.hxx                 |   83 ++++-
 editeng/source/editeng/editeng.cxx                  |    3 
 include/LibreOfficeKit/LibreOfficeKit.h             |   19 +
 include/LibreOfficeKit/LibreOfficeKit.hxx           |   50 +++
 include/LibreOfficeKit/LibreOfficeKitEnums.h        |    9 
 include/vcl/transfer.hxx                            |    1 
 offapi/com/sun/star/datatransfer/XTransferable2.idl |    5 
 sc/source/ui/app/seltrans.cxx                       |   12 
 sc/source/ui/app/transobj.cxx                       |   83 +++--
 sc/source/ui/inc/seltrans.hxx                       |    1 
 sc/source/ui/inc/transobj.hxx                       |    1 
 sc/source/ui/view/viewfun5.cxx                      |    3 
 sfx2/source/view/lokhelper.cxx                      |   17 -
 sw/source/filter/html/htmlplug.cxx                  |    6 
 sw/source/filter/html/wrthtml.cxx                   |   24 -
 sw/source/filter/html/wrthtml.hxx                   |    6 
 sw/source/uibase/dochdl/swdtflvr.cxx                |   42 ++
 sw/source/uibase/inc/swdtflvr.hxx                   |    1 
 vcl/source/treelist/transfer.cxx                    |    6 
 23 files changed, 863 insertions(+), 126 deletions(-)

New commits:
commit 343bc8260d428f2fc3b61c82af25eda4adfc04e4
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 27 16:56:46 2019 -0400
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    Use implicit OString constructor and cleanup casts.
    
    Change-Id: I686c32d3666bceb23dd86a4220428fa319ffdf45

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 8a7934b9f0e2..8ccb6b01335f 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2649,13 +2649,13 @@ void DesktopLOKTest::testComplexSelection()
     static const OString aText("hello world");
 
     // Certainly not complex.
-    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_NONE, pDocument->pClass->getSelectionType(pDocument));
+    CPPUNIT_ASSERT_EQUAL(static_cast<int>(LOK_SELTYPE_NONE), pDocument->pClass->getSelectionType(pDocument));
 
     // Paste text.
     CPPUNIT_ASSERT(pDocument->pClass->paste(pDocument, "text/plain;charset=utf-8", aText.getStr(), aText.getLength()));
 
     // No selection.
-    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_NONE, pDocument->pClass->getSelectionType(pDocument));
+    CPPUNIT_ASSERT_EQUAL(static_cast<int>(LOK_SELTYPE_NONE), pDocument->pClass->getSelectionType(pDocument));
 
     // Paste an image.
     OUString aFileURL;
@@ -2689,7 +2689,7 @@ void DesktopLOKTest::testComplexSelection()
     free(pText);
 
     // We expect this to be complex.
-    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_COMPLEX, pDocument->pClass->getSelectionType(pDocument));
+    CPPUNIT_ASSERT_EQUAL(static_cast<int>(LOK_SELTYPE_COMPLEX), pDocument->pClass->getSelectionType(pDocument));
 }
 
 namespace {
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 03c0d86267d1..7193b1ec4571 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -986,6 +986,15 @@ LibLODocument_Impl::~LibLODocument_Impl()
     }
 }
 
+static OUString getGenerator()
+{
+    OUString sGenerator(
+        Translate::ExpandVariables("%PRODUCTNAME %PRODUCTVERSION%PRODUCTEXTENSION (%1)"));
+    OUString os("$_OS");
+    ::rtl::Bootstrap::expandMacros(os);
+    return sGenerator.replaceFirst("%1", os);
+}
+
 extern "C" {
 
 CallbackFlushHandler::CallbackFlushHandler(LibreOfficeKitDocument* pDocument, LibreOfficeKitCallback pCallback, void* pData)
@@ -3427,15 +3436,6 @@ static void doc_setTextSelection(LibreOfficeKitDocument* pThis, int nType, int n
     pDoc->setTextSelection(nType, nX, nY);
 }
 
-static OUString getGenerator()
-{
-    OUString sGenerator(
-        Translate::ExpandVariables("%PRODUCTNAME %PRODUCTVERSION%PRODUCTEXTENSION (%1)"));
-    OUString os("$_OS");
-    ::rtl::Bootstrap::expandMacros(os);
-    return sGenerator.replaceFirst("%1", os);
-}
-
 static bool getFromTransferrable(
     const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
     const OString &aInMimeType, OString &aRet);
@@ -3633,7 +3633,7 @@ static int doc_getSelectionType(LibreOfficeKitDocument* pThis)
         return LOK_SELTYPE_COMPLEX;
 
     OString aRet;
-    bool bSuccess = getFromTransferrable(xTransferable, OString("text/plain;charset=utf-8"), aRet);
+    bool bSuccess = getFromTransferrable(xTransferable, "text/plain;charset=utf-8", aRet);
     if (!bSuccess)
         return LOK_SELTYPE_NONE;
 
commit 666427b7a51eb16829d96563b53bcef2de541da1
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Thu Jul 25 19:02:47 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    clipboard: repair unit test so that it runs in the lok mode.
    
    SwTransferable::AddSupportedFormats needs the kit to be active.
    
    Change-Id: Id84210eb2e526785f35dfae609d861c310885314

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 6d6416a46e16..8a7934b9f0e2 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2641,6 +2641,9 @@ void DesktopLOKTest::testShowHideDialog()
 
 void DesktopLOKTest::testComplexSelection()
 {
+    // needed for SwTransferable to cope with the selection.
+    comphelper::LibreOfficeKit::setActive();
+
     // Start with a blank text file and add contents.
     LibLODocument_Impl* pDocument = loadDoc("blank_text.odt");
     static const OString aText("hello world");
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 9036abedba15..03c0d86267d1 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -878,7 +878,6 @@ ITiledRenderable* getTiledRenderable(LibreOfficeKitDocument* pThis)
  * we also need to ensure that this works for the first view which
  * has no clear 'createView' called for it (unfortunately).
  */
-
 rtl::Reference<LOKClipboard> forceSetClipboardForCurrentView(LibreOfficeKitDocument *pThis)
 {
     ITiledRenderable* pDoc = getTiledRenderable(pThis);
commit 6e33d3046186b10f4ef49f3de8ee87adb100bab9
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Mon Jul 22 19:00:23 2019 -0400
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    clipboard: log before erasing
    
    Change-Id: I6d59cdd31c62bbc7db81ccb8985745f68d19cd28

diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index 9ed05bc2c383..8b7577f3bf63 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -51,8 +51,8 @@ void LOKClipboardFactory::releaseClipboardForView(int nViewId)
         auto it = gClipboards.get()->find(nViewId);
         if (it != gClipboards.get()->end())
         {
+            SAL_INFO("lok", "Releasing clip: " << it->second.get() << " for destroyed " << nViewId);
             gClipboards.get()->erase(it);
-            SAL_INFO("lok", "Released clip: " << it->second.get() << " for destroyed " << nViewId);
         }
     }
 }
commit 8d82bb6d4a6e9a0a7150bd0a2117970c99a48f63
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Tue Jul 23 09:49:11 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    clipboard: check for null.
    
    Change-Id: I07060035a9744642c98d4a0946695408dc0f6b42

diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index 2c155c121d91..9ed05bc2c383 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -37,7 +37,6 @@ rtl::Reference<LOKClipboard> LOKClipboardFactory::getClipboardForCurView()
     return xClip;
 }
 
-/// FIXME: should really copy and stash its content for a bit.
 void LOKClipboardFactory::releaseClipboardForView(int nViewId)
 {
     osl::MutexGuard aGuard(gMutex);
@@ -47,7 +46,7 @@ void LOKClipboardFactory::releaseClipboardForView(int nViewId)
         gClipboards.get()->clear();
         SAL_INFO("lok", "Released all clipboards on doc destroy\n");
     }
-    else
+    else if (gClipboards.get())
     {
         auto it = gClipboards.get()->find(nViewId);
         if (it != gClipboards.get()->end())
commit c4bebdc659bd922831db28cbb59c62291d327b2f
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Mon Jul 22 09:48:46 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    lok: share more of the clipboard mime-type handling code.
    
    Change-Id: I98fc9fa0fd1b5afa00c33b642d2d504eab0b62ba

diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index c85ac029406c..2c155c121d91 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -189,20 +189,7 @@ LOKTransferable::LOKTransferable(const size_t nInCount, const char** pInMimeType
     m_aFlavors = css::uno::Sequence<css::datatransfer::DataFlavor>(nInCount);
     for (size_t i = 0; i < nInCount; ++i)
     {
-        OUString aMimeType = OUString::fromUtf8(pInMimeTypes[i]);
-
-        // cf. sot/source/base/exchange.cxx for these two exceptional types.
-        if (aMimeType.startsWith("text/plain"))
-        {
-            aMimeType = "text/plain;charset=utf-16";
-            m_aFlavors[i].DataType = cppu::UnoType<OUString>::get();
-        }
-        else if (aMimeType == "application/x-libreoffice-tsvc")
-            m_aFlavors[i].DataType = cppu::UnoType<OUString>::get();
-        else
-            m_aFlavors[i].DataType = cppu::UnoType<uno::Sequence<sal_Int8>>::get();
-        m_aFlavors[i].MimeType = aMimeType;
-        m_aFlavors[i].HumanPresentableName = aMimeType;
+        initFlavourFromMime(m_aFlavors[i], OUString::fromUtf8(pInMimeTypes[i]));
 
         uno::Any aContent;
         if (m_aFlavors[i].DataType == cppu::UnoType<OUString>::get())
commit ed9bdfaf472d4a208d855897f3cc12497a5176a9
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 20 11:22:09 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    clipboard: encourage paste to show up by having some dummy content.
    
    Having an empty system clipboard is a hyper-unusual state, but
    this is quite common for new lok instances. If we populate some dummy
    state we can hide that.
    
    Allows context-menu paste into new documents.
    
    Change-Id: Ia180dd370c180d2d666cc57330db6c1385578a36

diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index 18b728c2343a..c85ac029406c 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -69,6 +69,9 @@ LOKClipboard::LOKClipboard()
     : cppu::WeakComponentImplHelper<css::datatransfer::clipboard::XSystemClipboard,
                                     css::lang::XServiceInfo>(m_aMutex)
 {
+    // Encourage 'paste' menu items to always show up.
+    uno::Reference<datatransfer::XTransferable> xTransferable(new LOKTransferable());
+    setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
 }
 
 Sequence<OUString> LOKClipboard::getSupportedServiceNames_static()
@@ -150,6 +153,18 @@ LOKTransferable::LOKTransferable(const OUString& sMimeType,
         aContent <<= aSequence;
     m_aContent.push_back(aContent);
 }
+
+/// Use to ensure we have some dummy content on the clipboard to allow a 1st 'paste'
+LOKTransferable::LOKTransferable()
+{
+    m_aContent.reserve(1);
+    m_aFlavors = css::uno::Sequence<css::datatransfer::DataFlavor>(1);
+    initFlavourFromMime(m_aFlavors[0], "text/plain");
+    uno::Any aContent;
+    aContent <<= OUString();
+    m_aContent.push_back(aContent);
+}
+
 // cf. sot/source/base/exchange.cxx for these two exceptional types.
 void LOKTransferable::initFlavourFromMime(css::datatransfer::DataFlavor& rFlavor,
                                           OUString aMimeType)
diff --git a/desktop/source/lib/lokclipboard.hxx b/desktop/source/lib/lokclipboard.hxx
index 90e41176fee4..6c0509265db4 100644
--- a/desktop/source/lib/lokclipboard.hxx
+++ b/desktop/source/lib/lokclipboard.hxx
@@ -77,6 +77,7 @@ class LOKTransferable : public cppu::WeakImplHelper<css::datatransfer::XTransfer
     static void initFlavourFromMime(css::datatransfer::DataFlavor& rFlavor, OUString sMimeType);
 
 public:
+    LOKTransferable();
     LOKTransferable(const size_t nInCount, const char** pInMimeTypes, const size_t* pInSizes,
                     const char** pInStreams);
     LOKTransferable(const OUString& sMimeType, const css::uno::Sequence<sal_Int8>& aSequence);
diff --git a/vcl/source/treelist/transfer.cxx b/vcl/source/treelist/transfer.cxx
index 656018abbf79..a0727a2f464a 100644
--- a/vcl/source/treelist/transfer.cxx
+++ b/vcl/source/treelist/transfer.cxx
@@ -392,7 +392,7 @@ sal_Bool SAL_CALL TransferableHelper::isComplex()
 {
     // By default everything is complex, until proven otherwise
     // in the respective document type transferable handler.
-    return sal_True;
+    return true;
 }
 
 Sequence< DataFlavor > SAL_CALL TransferableHelper::getTransferDataFlavors()
commit 21072445cebfe74dc01165dc6a120bf0fb951767
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Fri Jun 28 18:36:15 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:50 2019 -0400

    Try harder to generate HTML from text.
    
    Change-Id: I5b4778cceefc652274a72b779cd6c060a50bf06c

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 96ba15ace3fd..9036abedba15 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -3439,6 +3439,54 @@ static OUString getGenerator()
 
 static bool getFromTransferrable(
     const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
+    const OString &aInMimeType, OString &aRet);
+
+static bool encodeImageAsHTML(
+    const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
+    const OString &aMimeType, OString &aRet)
+{
+    if (!getFromTransferrable(xTransferable, aMimeType, aRet))
+        return false;
+
+    // Encode in base64.
+    auto aSeq = Sequence<sal_Int8>(reinterpret_cast<const sal_Int8*>(aRet.getStr()),
+                                   aRet.getLength());
+    OUStringBuffer aBase64Data;
+    comphelper::Base64::encode(aBase64Data, aSeq);
+
+    // Embed in HTML.
+    aRet = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
+        "<html><head>"
+        "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><meta "
+        "name=\"generator\" content=\""
+        + getGenerator().toUtf8()
+        + "\"/>"
+        "</head><body><img src=\"data:" + aMimeType + ";base64,"
+        + aBase64Data.makeStringAndClear().toUtf8() + "\"/></body></html>";
+
+    return true;
+}
+
+static bool encodeTextAsHTML(
+    const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
+    const OString &aMimeType, OString &aRet)
+{
+    if (!getFromTransferrable(xTransferable, aMimeType, aRet))
+        return false;
+
+    // Embed in HTML - FIXME: needs some escaping.
+    aRet = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
+        "<html><head>"
+        "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><meta "
+        "name=\"generator\" content=\""
+        + getGenerator().toUtf8()
+        + "\"/></head><body><pre>" + aRet + "</pre></body></html>";
+
+    return true;
+}
+
+static bool getFromTransferrable(
+    const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
     const OString &aInMimeType, OString &aRet)
 {
     OString aMimeType(aInMimeType);
@@ -3464,28 +3512,15 @@ static bool getFromTransferrable(
 
     if (!xTransferable->isDataFlavorSupported(aFlavor))
     {
-        // If html is not supported, might be a graphic-selection, which supports png.
-        if (aInMimeType == "text/html" && getFromTransferrable(xTransferable, "image/png", aRet))
+        // Try harder for HTML it is our copy/paste meta-file format
+        if (aInMimeType == "text/html")
         {
-            // Encode in base64.
-            auto aSeq = Sequence<sal_Int8>(reinterpret_cast<const sal_Int8*>(aRet.getStr()),
-                                           aRet.getLength());
-            OUStringBuffer aBase64Data;
-            comphelper::Base64::encode(aBase64Data, aSeq);
-
-            // Embed in HTML.
-            static const OString aHeader
-                = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
-                  "<html><head>"
-                  "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><meta "
-                  "name=\"generator\" content=\""
-                  + getGenerator().toUtf8()
-                  + "\"/>"
-                    "</head><body><img src=\"data:image/png;charset=utf-8;base64,";
-
-            aRet = aHeader + aBase64Data.makeStringAndClear().toUtf8() + "\"/></body></html>";
-
-            return true;
+            // Desperate measures - convert text to HTML instead.
+            if (encodeTextAsHTML(xTransferable, "text/plain;charset=utf-8", aRet))
+                return true;
+            // If html is not supported, might be a graphic-selection,
+            if (encodeImageAsHTML(xTransferable, "image/png", aRet))
+                return true;
         }
 
         SetLastExceptionMsg("Flavor " + aFlavor.MimeType + " is not supported");
@@ -3603,7 +3638,7 @@ static int doc_getSelectionType(LibreOfficeKitDocument* pThis)
     if (!bSuccess)
         return LOK_SELTYPE_NONE;
 
-    if (aRet.getLength() > 1000) // About 2 paragraphs.
+    if (aRet.getLength() > 10000)
         return LOK_SELTYPE_COMPLEX;
 
     return aRet.getLength() ? LOK_SELTYPE_TEXT : LOK_SELTYPE_NONE;
commit 93400d9b37063721d4d925a9c37020633801248c
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 20 11:15:05 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:49 2019 -0400

    lok: minimal implementation of isComplex for calc for now.
    
    Change-Id: Ic4141d30a0ff48a9ec5ecc8e6119d9d779c4a0fc

diff --git a/sc/source/ui/app/seltrans.cxx b/sc/source/ui/app/seltrans.cxx
index 79a0437f6061..3a1c2128d560 100644
--- a/sc/source/ui/app/seltrans.cxx
+++ b/sc/source/ui/app/seltrans.cxx
@@ -413,4 +413,16 @@ void ScSelectionTransferObj::ObjectReleased()
     TransferableHelper::ObjectReleased();
 }
 
+sal_Bool SAL_CALL ScSelectionTransferObj::isComplex()
+{
+    switch (eMode)
+    {
+    case SC_SELTRANS_CELL:
+    case SC_SELTRANS_CELLS:
+        return false;
+    default:
+        return true;
+    }
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/app/transobj.cxx b/sc/source/ui/app/transobj.cxx
index c78e8ae9e859..dfa3d47cf8e9 100644
--- a/sc/source/ui/app/transobj.cxx
+++ b/sc/source/ui/app/transobj.cxx
@@ -234,6 +234,42 @@ void ScTransferObj::AddSupportedFormats()
     }
 }
 
+static ScRange lcl_reduceBlock(ScDocumentUniquePtr &pDoc, ScRange aReducedBlock, bool bIncludeVisual = false)
+{
+    if ((aReducedBlock.aEnd.Col() == MAXCOL || aReducedBlock.aEnd.Row() == MAXROW) &&
+        aReducedBlock.aStart.Tab() == aReducedBlock.aEnd.Tab())
+    {
+        // Shrink the block here so we don't waste time creating huge
+        // output when whole columns or rows are selected.
+
+        SCCOL nPrintAreaEndCol = 0;
+        SCROW nPrintAreaEndRow = 0;
+        if (bIncludeVisual)
+            pDoc->GetPrintArea( aReducedBlock.aStart.Tab(), nPrintAreaEndCol, nPrintAreaEndRow, true );
+
+        // Shrink the area to allow pasting to external applications.
+        // Shrink to real data area for HTML, RTF and RICHTEXT, but include
+        // all objects and top-left area for BITMAP and PNG.
+        SCCOL nStartCol = aReducedBlock.aStart.Col();
+        SCROW nStartRow = aReducedBlock.aStart.Row();
+        SCCOL nEndCol = aReducedBlock.aEnd.Col();
+        SCROW nEndRow = aReducedBlock.aEnd.Row();
+        bool bShrunk = false;
+        pDoc->ShrinkToUsedDataArea( bShrunk, aReducedBlock.aStart.Tab(), nStartCol, nStartRow, nEndCol, nEndRow,
+                                      false, bIncludeVisual /*bStickyTopRow*/, bIncludeVisual /*bStickyLeftCol*/,
+                                      bIncludeVisual /*bConsiderCellNotes*/, bIncludeVisual /*bConsiderCellDrawObjects*/);
+
+        if ( nPrintAreaEndRow > nEndRow )
+            nEndRow = nPrintAreaEndRow;
+
+        if ( nPrintAreaEndCol > nEndCol )
+            nEndCol = nPrintAreaEndCol;
+
+        aReducedBlock = ScRange(nStartCol, nStartRow, aReducedBlock.aStart.Tab(), nEndCol, nEndRow, aReducedBlock.aEnd.Tab());
+    }
+    return aReducedBlock;
+}
+
 bool ScTransferObj::GetData( const datatransfer::DataFlavor& rFlavor, const OUString& /*rDestDoc*/ )
 {
     SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor );
@@ -250,39 +286,11 @@ bool ScTransferObj::GetData( const datatransfer::DataFlavor& rFlavor, const OUSt
             || nFormat == SotClipboardFormatId::BITMAP
             || nFormat == SotClipboardFormatId::PNG;
 
-        if (bReduceBlockFormat && (m_aBlock.aEnd.Col() == MAXCOL || m_aBlock.aEnd.Row() == MAXROW) &&
-                m_aBlock.aStart.Tab() == m_aBlock.aEnd.Tab())
-        {
-            // Shrink the block here so we don't waste time creating huge
-            // output when whole columns or rows are selected.
-
-            SCCOL nPrintAreaEndCol = 0;
-            SCROW nPrintAreaEndRow = 0;
-            const bool bIncludeVisual = (nFormat == SotClipboardFormatId::BITMAP ||
-                    nFormat == SotClipboardFormatId::PNG);
-            if (bIncludeVisual)
-                m_pDoc->GetPrintArea( m_aBlock.aStart.Tab(), nPrintAreaEndCol, nPrintAreaEndRow, true );
-
-            // Shrink the area to allow pasting to external applications.
-            // Shrink to real data area for HTML, RTF and RICHTEXT, but include
-            // all objects and top-left area for BITMAP and PNG.
-            SCCOL nStartCol = aReducedBlock.aStart.Col();
-            SCROW nStartRow = aReducedBlock.aStart.Row();
-            SCCOL nEndCol = aReducedBlock.aEnd.Col();
-            SCROW nEndRow = aReducedBlock.aEnd.Row();
-            bool bShrunk = false;
-            m_pDoc->ShrinkToUsedDataArea( bShrunk, aReducedBlock.aStart.Tab(), nStartCol, nStartRow, nEndCol, nEndRow,
-                    false, bIncludeVisual /*bStickyTopRow*/, bIncludeVisual /*bStickyLeftCol*/,
-                    bIncludeVisual /*bConsiderCellNotes*/, bIncludeVisual /*bConsiderCellDrawObjects*/);
-
-            if ( nPrintAreaEndRow > nEndRow )
-                nEndRow = nPrintAreaEndRow;
-
-            if ( nPrintAreaEndCol > nEndCol )
-                nEndCol = nPrintAreaEndCol;
-
-            aReducedBlock = ScRange(nStartCol, nStartRow, aReducedBlock.aStart.Tab(), nEndCol, nEndRow, aReducedBlock.aEnd.Tab());
-        }
+        const bool bIncludeVisual = (nFormat == SotClipboardFormatId::BITMAP ||
+                                     nFormat == SotClipboardFormatId::PNG);
+
+        if (bReduceBlockFormat)
+            aReducedBlock = lcl_reduceBlock(m_pDoc, m_aBlock, bIncludeVisual);
 
         if ( nFormat == SotClipboardFormatId::LINKSRCDESCRIPTOR || nFormat == SotClipboardFormatId::OBJECTDESCRIPTOR )
         {
@@ -535,6 +543,15 @@ bool ScTransferObj::WriteObject( tools::SvRef<SotStorageStream>& rxOStm, void* p
     return bRet;
 }
 
+sal_Bool SAL_CALL ScTransferObj::isComplex()
+{
+    ScRange aReduced = lcl_reduceBlock(m_pDoc, m_aBlock);
+    size_t nCells = (aReduced.aEnd.Col() - aReduced.aStart.Col() + 1) *
+                    (aReduced.aEnd.Row() - aReduced.aStart.Row() + 1) *
+                    (aReduced.aEnd.Tab() - aReduced.aStart.Tab() + 1);
+    return nCells > 1000;
+}
+
 void ScTransferObj::DragFinished( sal_Int8 nDropAction )
 {
     if ( nDropAction == DND_ACTION_MOVE && !m_bDragWasInternal && !(m_nDragSourceFlags & ScDragSrc::Navigator) )
diff --git a/sc/source/ui/inc/seltrans.hxx b/sc/source/ui/inc/seltrans.hxx
index af73f0adf896..83adb2c11eb2 100644
--- a/sc/source/ui/inc/seltrans.hxx
+++ b/sc/source/ui/inc/seltrans.hxx
@@ -67,6 +67,7 @@ public:
     virtual void        AddSupportedFormats() override;
     virtual bool GetData( const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc ) override;
     virtual void        ObjectReleased() override;
+    virtual sal_Bool SAL_CALL isComplex() override;
 };
 
 #endif
diff --git a/sc/source/ui/inc/transobj.hxx b/sc/source/ui/inc/transobj.hxx
index 5b7f405fb55e..1ebfd1a4e892 100644
--- a/sc/source/ui/inc/transobj.hxx
+++ b/sc/source/ui/inc/transobj.hxx
@@ -76,6 +76,7 @@ public:
     virtual bool        WriteObject( tools::SvRef<SotStorageStream>& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId,
                                         const css::datatransfer::DataFlavor& rFlavor ) override;
     virtual void        DragFinished( sal_Int8 nDropAction ) override;
+    virtual sal_Bool SAL_CALL isComplex() override;
 
     ScDocument*         GetDocument() const     { return m_pDoc.get(); }        // owned by ScTransferObj
     const ScRange&      GetRange() const        { return m_aBlock; }
diff --git a/sw/source/uibase/dochdl/swdtflvr.cxx b/sw/source/uibase/dochdl/swdtflvr.cxx
index 798b3dd23e3a..f4b8bf904532 100644
--- a/sw/source/uibase/dochdl/swdtflvr.cxx
+++ b/sw/source/uibase/dochdl/swdtflvr.cxx
@@ -419,7 +419,7 @@ namespace
     }
 }
 
-sal_Bool SwTransferable::isComplex()
+sal_Bool SAL_CALL SwTransferable::isComplex()
 {
     // Copy into a new Doc so we don't mess with the existing one.
     //FIXME: We *should* be able to avoid this and improve the performance.
diff --git a/sw/source/uibase/inc/swdtflvr.hxx b/sw/source/uibase/inc/swdtflvr.hxx
index 0b15b8f15acf..c19bc8efc075 100644
--- a/sw/source/uibase/inc/swdtflvr.hxx
+++ b/sw/source/uibase/inc/swdtflvr.hxx
@@ -151,7 +151,7 @@ protected:
                                         const css::datatransfer::DataFlavor& rFlavor ) override;
     virtual void        DragFinished( sal_Int8 nDropAction ) override;
     virtual void        ObjectReleased() override;
-    virtual sal_Bool isComplex() override;
+    virtual sal_Bool SAL_CALL isComplex() override;
 
     using TransferableHelper::StartDrag;
 
commit 05683f3ade1ed623847df5204338f440981239c7
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Fri Jun 28 12:30:40 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:49 2019 -0400

    lok: vital to quote nested JSON inserted into a json property.
    
    Somehow this managed to work fine in most browsers, but when
    the Kit/Poco tried to parse JSON to extract viewID it could explode.
    
    Change-Id: I39d2ecc9ee95b7e6f67a23c8b15f9a1d01769ddc

diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index a7828287205d..55b157ef6cb2 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -143,11 +143,26 @@ void SfxLokHelper::setViewLanguage(int nId, const OUString& rBcp47LanguageTag)
     }
 }
 
+static OString lcl_escapeQuotes(const OString &rStr)
+{
+    if (rStr.getLength() < 1)
+        return rStr;
+    // FIXME: need an optimized 'escape' method for O[U]String.
+    OStringBuffer aBuf(rStr.getLength() + 8);
+    for (sal_Int32 i = 0; i < rStr.getLength(); ++i)
+    {
+        if (rStr[i] == '"' || rStr[i] == '\\')
+            aBuf.append('\\');
+        aBuf.append(rStr[i]);
+    }
+    return aBuf.makeStringAndClear();
+}
+
 void SfxLokHelper::notifyOtherView(SfxViewShell* pThisView, SfxViewShell const* pOtherView, int nType, const OString& rKey, const OString& rPayload)
 {
     OString aPayload = OString("{ \"viewId\": \"") + OString::number(SfxLokHelper::getView(pThisView)) +
                        "\", \"part\": \"" + OString::number(pThisView->getPart()) +
-                       "\", \"" + rKey + "\": \"" + rPayload + "\" }";
+                       "\", \"" + rKey + "\": \"" + lcl_escapeQuotes(rPayload) + "\" }";
 
     pOtherView->libreOfficeKitViewCallback(nType, aPayload.getStr());
 }
commit bd304791005f6006487941272dfac525509434ea
Author:     Szymon Kłos <szymon.klos at collabora.com>
AuthorDate: Sat Jul 20 11:07:56 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:49 2019 -0400

    Set clipboard for a window when created
    
    This allows to paste in dialogs input using tunneled context menu.
    
    Change-Id: Ie6cc6f1a6cd453734c8f6084b0bd50d1d7ab6c09

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index af4f531e89ba..96ba15ace3fd 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -1529,6 +1529,18 @@ bool CallbackFlushHandler::processWindowEvent(CallbackData& aCallbackData)
             }
             return false;
         });
+
+        VclPtr<Window> pWindow = vcl::Window::FindLOKWindow(nLOKWindowId);
+        if (!pWindow)
+        {
+            gImpl->maLastExceptionMsg = "Document doesn't support dialog rendering, or window not found.";
+            return false;
+        }
+
+        auto xClip = forceSetClipboardForCurrentView(m_pDocument);
+
+        uno::Reference<datatransfer::clipboard::XClipboard> xClipboard(xClip.get());
+        pWindow->SetClipboard(xClipboard);
     }
     else if (aAction == "size_changed")
     {
commit b7b091ed71557213e9ce9123dfed727393e86b4b
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Sat Jul 20 11:07:01 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:49 2019 -0400

    LOK: Copy Graphics as embedded PNG in HTML
    
    Change-Id: Id8c14d7304d30bfcd956b126dfe291ef044f62bf

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 6b0e8863bbd9..af4f531e89ba 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -47,6 +47,7 @@
 #include <rtl/strbuf.hxx>
 #include <rtl/uri.hxx>
 #include <cppuhelper/bootstrap.hxx>
+#include <comphelper/base64.hxx>
 #include <comphelper/dispatchcommand.hxx>
 #include <comphelper/lok.hxx>
 #include <comphelper/processfactory.hxx>
@@ -3415,6 +3416,15 @@ static void doc_setTextSelection(LibreOfficeKitDocument* pThis, int nType, int n
     pDoc->setTextSelection(nType, nX, nY);
 }
 
+static OUString getGenerator()
+{
+    OUString sGenerator(
+        Translate::ExpandVariables("%PRODUCTNAME %PRODUCTVERSION%PRODUCTEXTENSION (%1)"));
+    OUString os("$_OS");
+    ::rtl::Bootstrap::expandMacros(os);
+    return sGenerator.replaceFirst("%1", os);
+}
+
 static bool getFromTransferrable(
     const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
     const OString &aInMimeType, OString &aRet)
@@ -3442,6 +3452,30 @@ static bool getFromTransferrable(
 
     if (!xTransferable->isDataFlavorSupported(aFlavor))
     {
+        // If html is not supported, might be a graphic-selection, which supports png.
+        if (aInMimeType == "text/html" && getFromTransferrable(xTransferable, "image/png", aRet))
+        {
+            // Encode in base64.
+            auto aSeq = Sequence<sal_Int8>(reinterpret_cast<const sal_Int8*>(aRet.getStr()),
+                                           aRet.getLength());
+            OUStringBuffer aBase64Data;
+            comphelper::Base64::encode(aBase64Data, aSeq);
+
+            // Embed in HTML.
+            static const OString aHeader
+                = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
+                  "<html><head>"
+                  "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><meta "
+                  "name=\"generator\" content=\""
+                  + getGenerator().toUtf8()
+                  + "\"/>"
+                    "</head><body><img src=\"data:image/png;charset=utf-8;base64,";
+
+            aRet = aHeader + aBase64Data.makeStringAndClear().toUtf8() + "\"/></body></html>";
+
+            return true;
+        }
+
         SetLastExceptionMsg("Flavor " + aFlavor.MimeType + " is not supported");
         return false;
     }
@@ -3478,7 +3512,7 @@ static bool getFromTransferrable(
         aRet = OString(reinterpret_cast<sal_Char*>(aSequence.getArray()), aSequence.getLength());
     }
 
-    return true;;
+    return true;
 }
 
 // Tolerate embedded \0s etc.
diff --git a/vcl/source/treelist/transfer.cxx b/vcl/source/treelist/transfer.cxx
index 67e71396e8dc..656018abbf79 100644
--- a/vcl/source/treelist/transfer.cxx
+++ b/vcl/source/treelist/transfer.cxx
@@ -390,7 +390,8 @@ Any SAL_CALL TransferableHelper::getTransferData2( const DataFlavor& rFlavor, co
 
 sal_Bool SAL_CALL TransferableHelper::isComplex()
 {
-    //FIXME: get from each document.
+    // By default everything is complex, until proven otherwise
+    // in the respective document type transferable handler.
     return sal_True;
 }
 
commit ab8a6d596f2850b600bc3bc2f08f008b96fc65cd
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 20 11:05:49 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:49 2019 -0400

    lok: cleanup clipboards associated with a view on destruction.
    
    Also defer destruction potentially indefinitely for bad users who
    quit without destroying views, documents, shutting down nicely etc.
    
    Change-Id: Ieea6ad00b2983d372b745179bfe3b884c3c64eb0

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index bb938443861f..6b0e8863bbd9 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -1650,6 +1650,8 @@ static void doc_destroy(LibreOfficeKitDocument *pThis)
 
     SolarMutexGuard aGuard;
 
+    LOKClipboardFactory::releaseClipboardForView(-1);
+
     LibLODocument_Impl *pDocument = static_cast<LibLODocument_Impl*>(pThis);
     delete pDocument;
 }
@@ -4369,6 +4371,8 @@ static void doc_destroyView(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis
     SolarMutexGuard aGuard;
     SetLastExceptionMsg();
 
+    LOKClipboardFactory::releaseClipboardForView(nId);
+
     SfxLokHelper::destroyView(nId);
 }
 
diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index ef52a4596c43..18b728c2343a 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -8,14 +8,16 @@
  */
 
 #include "lokclipboard.hxx"
+#include <vcl/lazydelete.hxx>
 #include <sfx2/lokhelper.hxx>
 #include <cppuhelper/supportsservice.hxx>
 
 using namespace css;
 using namespace css::uno;
 
-osl::Mutex LOKClipboardFactory::gMutex;
-std::unordered_map<int, rtl::Reference<LOKClipboard>> LOKClipboardFactory::gClipboards;
+/* static */ osl::Mutex LOKClipboardFactory::gMutex;
+static vcl::DeleteOnDeinit<std::unordered_map<int, rtl::Reference<LOKClipboard>>>
+gClipboards(new std::unordered_map<int, rtl::Reference<LOKClipboard>>);
 
 rtl::Reference<LOKClipboard> LOKClipboardFactory::getClipboardForCurView()
 {
@@ -23,18 +25,39 @@ rtl::Reference<LOKClipboard> LOKClipboardFactory::getClipboardForCurView()
 
     osl::MutexGuard aGuard(gMutex);
 
-    auto it = gClipboards.find(nViewId);
-    if (it != gClipboards.end())
+    auto it = gClipboards.get()->find(nViewId);
+    if (it != gClipboards.get()->end())
     {
         SAL_INFO("lok", "Got clip: " << it->second.get() << " from " << nViewId);
         return it->second;
     }
     rtl::Reference<LOKClipboard> xClip(new LOKClipboard());
-    gClipboards[nViewId] = xClip;
+    (*gClipboards.get())[nViewId] = xClip;
     SAL_INFO("lok", "Created clip: " << xClip.get() << " for viewId " << nViewId);
     return xClip;
 }
 
+/// FIXME: should really copy and stash its content for a bit.
+void LOKClipboardFactory::releaseClipboardForView(int nViewId)
+{
+    osl::MutexGuard aGuard(gMutex);
+
+    if (nViewId < 0) // clear all
+    {
+        gClipboards.get()->clear();
+        SAL_INFO("lok", "Released all clipboards on doc destroy\n");
+    }
+    else
+    {
+        auto it = gClipboards.get()->find(nViewId);
+        if (it != gClipboards.get()->end())
+        {
+            gClipboards.get()->erase(it);
+            SAL_INFO("lok", "Released clip: " << it->second.get() << " for destroyed " << nViewId);
+        }
+    }
+}
+
 uno::Reference<uno::XInterface>
     SAL_CALL LOKClipboardFactory::createInstanceWithArguments(const Sequence<Any>& /* rArgs */)
 {
@@ -178,7 +201,7 @@ LOKTransferable::LOKTransferable(const size_t nInCount, const char** pInMimeType
 
 uno::Any SAL_CALL LOKTransferable::getTransferData(const datatransfer::DataFlavor& rFlavor)
 {
-    assert(m_aContent.size() == (size_t)m_aFlavors.getLength());
+    assert(m_aContent.size() == static_cast<size_t>(m_aFlavors.getLength()));
     for (size_t i = 0; i < m_aContent.size(); ++i)
     {
         if (m_aFlavors[i].MimeType == rFlavor.MimeType)
diff --git a/desktop/source/lib/lokclipboard.hxx b/desktop/source/lib/lokclipboard.hxx
index e6f7876d4836..90e41176fee4 100644
--- a/desktop/source/lib/lokclipboard.hxx
+++ b/desktop/source/lib/lokclipboard.hxx
@@ -92,7 +92,6 @@ public:
 class LOKClipboardFactory : public ::cppu::WeakComponentImplHelper<css::lang::XSingleServiceFactory>
 {
     static osl::Mutex gMutex;
-    static std::unordered_map<int, rtl::Reference<LOKClipboard>> gClipboards;
 
 public:
     LOKClipboardFactory()
@@ -106,7 +105,12 @@ public:
     }
     css::uno::Reference<css::uno::XInterface> SAL_CALL
     createInstanceWithArguments(const css::uno::Sequence<css::uno::Any>& /* rArgs */) override;
+
+    /// Fetch clipboard from the gobal pool.
     static rtl::Reference<LOKClipboard> getClipboardForCurView();
+
+    /// Release a clipboard before its document dies, nViewId of -1 clears all.
+    static void releaseClipboardForView(int nViewId);
 };
 
 #endif
commit c3816a0c85839404d3042ce46635a11ada8a41e3
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Tue Jun 25 23:45:37 2019 -0400
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:49 2019 -0400

    LOK: Improved selection complexity detection
    
    Only Graphics and OLE now unconditionally
    trigger 'complex' case, and for all others,
    we actually tally the number of text characters.
    
    Currently anything above 512KB is flagged as 'complex'.
    
    Change-Id: I19fbef72f2eb725648b2a18c1ee41b1612d2bac0

diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
index bd6609c321a4..517a38a50527 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -356,7 +356,7 @@ public:
      *
      * @return an element of the LibreOfficeKitSelectionType enum.
      */
-    int getSelectionType(LibreOfficeKitDocument* /*pThis*/)
+    int getSelectionType()
     {
         return mpDoc->pClass->getSelectionType(mpDoc);
     }
diff --git a/sw/source/uibase/dochdl/swdtflvr.cxx b/sw/source/uibase/dochdl/swdtflvr.cxx
index 223ed806bc32..798b3dd23e3a 100644
--- a/sw/source/uibase/dochdl/swdtflvr.cxx
+++ b/sw/source/uibase/dochdl/swdtflvr.cxx
@@ -421,15 +421,6 @@ namespace
 
 sal_Bool SwTransferable::isComplex()
 {
-    const SelectionType nSelectionType = m_pWrtShell->GetSelectionType();
-
-    // Anything other than text is complex by definition.
-    if (nSelectionType != SelectionType::Text)
-        return true;
-
-    if (m_pWrtShell->IsFrameSelected() || m_pWrtShell->IsObjSelected())
-        return true;
-
     // Copy into a new Doc so we don't mess with the existing one.
     //FIXME: We *should* be able to avoid this and improve the performance.
     m_pClpDocFac.reset(new SwDocFac);
@@ -439,21 +430,29 @@ sal_Bool SwTransferable::isComplex()
         .LockExpFields(); // never update fields - leave text as it is
     lclOverWriteDoc(*m_pWrtShell, *pTmpDoc);
 
-    bool isComplex = false;
+    sal_Int32 nTextLength = 0;
+    const SwNode* pEndOfContent = &m_pWrtShell->GetDoc()->GetNodes().GetEndOfContent();
     SwNodes& aNodes = pTmpDoc->GetNodes();
     for( sal_uLong nIndex = 0; nIndex < aNodes.Count(); ++nIndex)
     {
         SwNode& rNd = *aNodes[nIndex];
-        if (rNd.IsContentNode() && !rNd.IsTextNode())
-        {
-            isComplex = true;
+        if (&rNd == pEndOfContent)
             break;
+
+        if (rNd.IsOLENode() || rNd.IsGrfNode())
+            return true; // Complex
+
+        SwTextNode* pTextNode = rNd.GetTextNode();
+        if (pTextNode)
+        {
+            nTextLength += pTextNode->GetText().getLength();
+            if (nTextLength >= 1024 * 512)
+                return true; // Complex
         }
-        else if (&rNd == &m_pWrtShell->GetDoc()->GetNodes().GetEndOfContent())
-            break;
     }
 
-    return isComplex;
+    // Simple
+    return false;
 }
 
 bool SwTransferable::GetData( const DataFlavor& rFlavor, const OUString& rDestDoc )
commit ff90a487276fa98d1868fdeda4d14dceae5f75b5
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Sat Jul 20 11:01:38 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    LOK: Enable embedding images in text/html format output
    
    Change-Id: Ibd8bed796678ee26de2ceb6e434dce24da4eab05

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 9f1e66a0851e..6d6416a46e16 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2682,7 +2682,7 @@ void DesktopLOKTest::testComplexSelection()
     pText = pDocument->pClass->getTextSelection(pDocument, "text/html", nullptr);
     CPPUNIT_ASSERT(pText != nullptr);
     CPPUNIT_ASSERT(std::string(pText).find(aText.getStr()) != std::string::npos); // Must have the text.
-    // CPPUNIT_ASSERT(std::string(pText).find("<img") != std::string::npos); // Must have the image as well.
+    CPPUNIT_ASSERT(std::string(pText).find("<img") != std::string::npos); // Must have the image as well.
     free(pText);
 
     // We expect this to be complex.
diff --git a/sw/source/filter/html/wrthtml.cxx b/sw/source/filter/html/wrthtml.cxx
index 54afa80ba7c9..5c1a6ab52a42 100644
--- a/sw/source/filter/html/wrthtml.cxx
+++ b/sw/source/filter/html/wrthtml.cxx
@@ -91,7 +91,7 @@ using namespace css;
 static sal_Char sIndentTabs[MAX_INDENT_LEVEL+2] =
     "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
 
-SwHTMLWriter::SwHTMLWriter( const OUString& rBaseURL )
+SwHTMLWriter::SwHTMLWriter( const OUString& rBaseURL, const OUString& rFilterOptions )
     : m_pNumRuleInfo(new SwHTMLNumRuleInfo)
     , m_nHTMLMode(0)
     , m_eCSS1Unit(FieldUnit::NONE)
@@ -163,6 +163,8 @@ SwHTMLWriter::SwHTMLWriter( const OUString& rBaseURL )
         mpTempBaseURL->EnableKillingFile();
         SetBaseURL(mpTempBaseURL->GetURL());
     }
+
+    SetupFilterOptions(rFilterOptions);
 }
 
 SwHTMLWriter::~SwHTMLWriter()
@@ -185,21 +187,26 @@ void SwHTMLWriter::SetupFilterOptions(SfxMedium& rMedium)
         return;
 
 
-    OUString sFilterOptions = static_cast<const SfxStringItem*>(pItem)->GetValue();
-    if (sFilterOptions == "SkipImages")
+    const OUString sFilterOptions = static_cast<const SfxStringItem*>(pItem)->GetValue();
+    SetupFilterOptions(sFilterOptions);
+}
+
+void SwHTMLWriter::SetupFilterOptions(const OUString& rFilterOptions)
+{
+    if (rFilterOptions == "SkipImages")
     {
         mbSkipImages = true;
     }
-    else if (sFilterOptions == "SkipHeaderFooter")
+    else if (rFilterOptions == "SkipHeaderFooter")
     {
         mbSkipHeaderFooter = true;
     }
-    else if (sFilterOptions == "EmbedImages" )
+    else if (rFilterOptions == "EmbedImages")
     {
         mbEmbedImages = true;
     }
 
-    uno::Sequence<OUString> aOptionSeq = comphelper::string::convertCommaSeparated(sFilterOptions);
+    uno::Sequence<OUString> aOptionSeq = comphelper::string::convertCommaSeparated(rFilterOptions);
     const OUString aXhtmlNsKey("xhtmlns=");
     for (const auto& rOption : aOptionSeq)
     {
@@ -823,13 +830,6 @@ void SwHTMLWriter::Out_SwDoc( SwPaM* pPam )
                 OutHTML_Section( *this, *rNd.GetSectionNode() );
                 m_nBkmkTabPos = m_bWriteAll ? FindPos_Bkmk( *m_pCurrentPam->GetPoint() ) : -1;
             }
-            else if( rNd.IsGrfNode() )
-            {
-                SwGrfNode* pGrfNd = rNd.GetGrfNode();
-                assert(pGrfNd && !"FIXME: Implement Graphic copy as HTML.");
-                // if (pGrfNd)
-                //     OutHTML_SwGrfNode( *this, *pGrfNd );
-            }
             else if( &rNd == &m_pDoc->GetNodes().GetEndOfContent() )
                 break;
 
@@ -1555,9 +1555,9 @@ HTMLSaveData::~HTMLSaveData()
     }
 }
 
-void GetHTMLWriter( const OUString&, const OUString& rBaseURL, WriterRef& xRet )
+void GetHTMLWriter( const OUString& rFilterOptions, const OUString& rBaseURL, WriterRef& xRet )
 {
-    xRet = new SwHTMLWriter( rBaseURL );
+    xRet = new SwHTMLWriter( rBaseURL, rFilterOptions );
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/filter/html/wrthtml.hxx b/sw/source/filter/html/wrthtml.hxx
index 47893227f98f..98c9005fe095 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -269,6 +269,8 @@ class SW_DLLPUBLIC SwHTMLWriter : public Writer
     void AddLinkTarget( const OUString& rURL );
     void CollectLinkTargets();
 
+    void SetupFilterOptions(const OUString& rFilterOptions);
+
 protected:
     ErrCode WriteStream() override;
     void SetupFilterOptions(SfxMedium& rMedium) override;
@@ -403,7 +405,9 @@ public:
     /// Tracks which text portion attributes are currently open: a which id -> open count map.
     std::map<sal_uInt16, int> maStartedAttributes;
 
-    explicit SwHTMLWriter( const OUString& rBaseURL );
+    /// Construct an instance of SwHTMLWriter and optionally give it
+    /// the filter options directly, which can also be set via SetupFilterOptions().
+    explicit SwHTMLWriter( const OUString& rBaseURL, const OUString& rFilterOptions = "" );
     virtual ~SwHTMLWriter() override;
 
     void Out_SwDoc( SwPaM* );       // write the marked range
diff --git a/sw/source/uibase/dochdl/swdtflvr.cxx b/sw/source/uibase/dochdl/swdtflvr.cxx
index 847ec3481590..223ed806bc32 100644
--- a/sw/source/uibase/dochdl/swdtflvr.cxx
+++ b/sw/source/uibase/dochdl/swdtflvr.cxx
@@ -432,7 +432,7 @@ sal_Bool SwTransferable::isComplex()
 
     // Copy into a new Doc so we don't mess with the existing one.
     //FIXME: We *should* be able to avoid this and improve the performance.
-    m_pClpDocFac = new SwDocFac;
+    m_pClpDocFac.reset(new SwDocFac);
     SwDoc* const pTmpDoc = lcl_GetDoc(*m_pClpDocFac);
 
     pTmpDoc->getIDocumentFieldsAccess()
@@ -773,8 +773,12 @@ bool SwTransferable::WriteObject( tools::SvRef<SotStorageStream>& xStream,
         break;
 
     case SWTRANSFER_OBJECTTYPE_HTML:
-        GetHTMLWriter(OUString(), OUString(), xWrt);
+    {
+        // LOK is interested in getting images embedded for copy/paste support.
+        const OUString aFilterOptions("EmbedImages");
+        GetHTMLWriter( comphelper::LibreOfficeKit::isActive() ? aFilterOptions : OUString(), OUString(), xWrt );
         break;
+    }
 
     case SWTRANSFER_OBJECTTYPE_RTF:
     case SWTRANSFER_OBJECTTYPE_RICHTEXT:
commit 7fe30d1cb00c576469d6cbe5606268a9cdf35bd3
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Tue Jun 25 12:36:52 2019 -0400
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    LOK: detect Graphics in isComplex for Writer
    
    Change-Id: I814d71a21fa5352a9a1051f5477cb452a0dc5c2f

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 8eaaacecc475..9f1e66a0851e 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2686,7 +2686,7 @@ void DesktopLOKTest::testComplexSelection()
     free(pText);
 
     // We expect this to be complex.
-    // CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_COMPLEX, pDocument->pClass->getSelectionType(pDocument)); // Fails!
+    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_COMPLEX, pDocument->pClass->getSelectionType(pDocument));
 }
 
 namespace {
diff --git a/sw/source/uibase/dochdl/swdtflvr.cxx b/sw/source/uibase/dochdl/swdtflvr.cxx
index 79a2f34d7d83..847ec3481590 100644
--- a/sw/source/uibase/dochdl/swdtflvr.cxx
+++ b/sw/source/uibase/dochdl/swdtflvr.cxx
@@ -430,6 +430,8 @@ sal_Bool SwTransferable::isComplex()
     if (m_pWrtShell->IsFrameSelected() || m_pWrtShell->IsObjSelected())
         return true;
 
+    // Copy into a new Doc so we don't mess with the existing one.
+    //FIXME: We *should* be able to avoid this and improve the performance.
     m_pClpDocFac = new SwDocFac;
     SwDoc* const pTmpDoc = lcl_GetDoc(*m_pClpDocFac);
 
@@ -437,36 +439,18 @@ sal_Bool SwTransferable::isComplex()
         .LockExpFields(); // never update fields - leave text as it is
     lclOverWriteDoc(*m_pWrtShell, *pTmpDoc);
 
-    // in CORE a new one was created (OLE-objects copied!)
-    m_aDocShellRef = pTmpDoc->GetTmpDocShell();
-    if (m_aDocShellRef.Is())
-        SwTransferable::InitOle(m_aDocShellRef);
-    pTmpDoc->SetTmpDocShell(nullptr);
-
-    SwPaM aOrigPam(pTmpDoc->GetNodes().GetEndOfContent());
-    aOrigPam.Move(fnMoveBackward, GoInDoc);
-    aOrigPam.SetMark();
-    aOrigPam.Move(fnMoveForward, GoInDoc);
-
-    SwPaM aPam(*aOrigPam.End(), *aOrigPam.Start());
-
     bool isComplex = false;
-    while (aPam.GetPoint()->nNode.GetIndex() < aPam.GetMark()->nNode.GetIndex()
-        || (aPam.GetPoint()->nNode.GetIndex() == aPam.GetMark()->nNode.GetIndex()
-            && aPam.GetPoint()->nContent.GetIndex() <= aPam.GetMark()->nContent.GetIndex()))
+    SwNodes& aNodes = pTmpDoc->GetNodes();
+    for( sal_uLong nIndex = 0; nIndex < aNodes.Count(); ++nIndex)
     {
-        SwNode& rNd = aPam.GetNode();
-
+        SwNode& rNd = *aNodes[nIndex];
         if (rNd.IsContentNode() && !rNd.IsTextNode())
         {
-            //FIXME: this doesn't detect GrfNode, which we need to detect complex selections.
             isComplex = true;
             break;
         }
         else if (&rNd == &m_pWrtShell->GetDoc()->GetNodes().GetEndOfContent())
             break;
-
-        ++aPam.GetPoint()->nNode;
     }
 
     return isComplex;
commit 169a87563a3940299811d874b4df0ad13591771c
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Mon Jun 24 22:07:08 2019 -0400
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    LOK: Implement getSelectionType
    
    Detects all complex scenarios, except for Graphics.
    
    DesktopLOKTest::testComplexSelection() has commented
    out check that currently fails due to the missing
    detection of Graphics nodes.
    
    Change-Id: Ifce17192d26daba218d2c3d38577cccec0699e99

diff --git a/desktop/qa/data/objects.odt b/desktop/qa/data/objects.odt
new file mode 100644
index 000000000000..45c2b39cc1a4
Binary files /dev/null and b/desktop/qa/data/objects.odt differ
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index d482f23e5eff..8eaaacecc475 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2643,12 +2643,17 @@ void DesktopLOKTest::testComplexSelection()
 {
     // Start with a blank text file and add contents.
     LibLODocument_Impl* pDocument = loadDoc("blank_text.odt");
-    // LibLODocument_Impl* pDocument = loadDoc("sheet_with_image.ods");
     static const OString aText("hello world");
 
+    // Certainly not complex.
+    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_NONE, pDocument->pClass->getSelectionType(pDocument));
+
     // Paste text.
     CPPUNIT_ASSERT(pDocument->pClass->paste(pDocument, "text/plain;charset=utf-8", aText.getStr(), aText.getLength()));
 
+    // No selection.
+    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_NONE, pDocument->pClass->getSelectionType(pDocument));
+
     // Paste an image.
     OUString aFileURL;
     createFileURL("paste.jpg", aFileURL);
@@ -2660,10 +2665,6 @@ void DesktopLOKTest::testComplexSelection()
     pDocument->pClass->postUnoCommand(pDocument, ".uno:SelectAll", nullptr, false);
     Scheduler::ProcessEventsToIdle();
 
-    // We expect this to be complex.
-    const int type = pDocument->pClass->getSelectionType(pDocument);
-    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_COMPLEX, type);
-
     // Export as plain text, we should get only the text part "hello".
     char* pText = pDocument->pClass->getTextSelection(pDocument, "text/plain;charset=utf-8", nullptr);
     CPPUNIT_ASSERT(pText != nullptr);
@@ -2683,6 +2684,9 @@ void DesktopLOKTest::testComplexSelection()
     CPPUNIT_ASSERT(std::string(pText).find(aText.getStr()) != std::string::npos); // Must have the text.
     // CPPUNIT_ASSERT(std::string(pText).find("<img") != std::string::npos); // Must have the image as well.
     free(pText);
+
+    // We expect this to be complex.
+    // CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_COMPLEX, pDocument->pClass->getSelectionType(pDocument)); // Fails!
 }
 
 namespace {
diff --git a/sw/source/uibase/dochdl/swdtflvr.cxx b/sw/source/uibase/dochdl/swdtflvr.cxx
index 351e7782c273..79a2f34d7d83 100644
--- a/sw/source/uibase/dochdl/swdtflvr.cxx
+++ b/sw/source/uibase/dochdl/swdtflvr.cxx
@@ -419,6 +419,59 @@ namespace
     }
 }
 
+sal_Bool SwTransferable::isComplex()
+{
+    const SelectionType nSelectionType = m_pWrtShell->GetSelectionType();
+
+    // Anything other than text is complex by definition.
+    if (nSelectionType != SelectionType::Text)
+        return true;
+
+    if (m_pWrtShell->IsFrameSelected() || m_pWrtShell->IsObjSelected())
+        return true;
+
+    m_pClpDocFac = new SwDocFac;
+    SwDoc* const pTmpDoc = lcl_GetDoc(*m_pClpDocFac);
+
+    pTmpDoc->getIDocumentFieldsAccess()
+        .LockExpFields(); // never update fields - leave text as it is
+    lclOverWriteDoc(*m_pWrtShell, *pTmpDoc);
+
+    // in CORE a new one was created (OLE-objects copied!)
+    m_aDocShellRef = pTmpDoc->GetTmpDocShell();
+    if (m_aDocShellRef.Is())
+        SwTransferable::InitOle(m_aDocShellRef);
+    pTmpDoc->SetTmpDocShell(nullptr);
+
+    SwPaM aOrigPam(pTmpDoc->GetNodes().GetEndOfContent());
+    aOrigPam.Move(fnMoveBackward, GoInDoc);
+    aOrigPam.SetMark();
+    aOrigPam.Move(fnMoveForward, GoInDoc);
+
+    SwPaM aPam(*aOrigPam.End(), *aOrigPam.Start());
+
+    bool isComplex = false;
+    while (aPam.GetPoint()->nNode.GetIndex() < aPam.GetMark()->nNode.GetIndex()
+        || (aPam.GetPoint()->nNode.GetIndex() == aPam.GetMark()->nNode.GetIndex()
+            && aPam.GetPoint()->nContent.GetIndex() <= aPam.GetMark()->nContent.GetIndex()))
+    {
+        SwNode& rNd = aPam.GetNode();
+
+        if (rNd.IsContentNode() && !rNd.IsTextNode())
+        {
+            //FIXME: this doesn't detect GrfNode, which we need to detect complex selections.
+            isComplex = true;
+            break;
+        }
+        else if (&rNd == &m_pWrtShell->GetDoc()->GetNodes().GetEndOfContent())
+            break;
+
+        ++aPam.GetPoint()->nNode;
+    }
+
+    return isComplex;
+}
+
 bool SwTransferable::GetData( const DataFlavor& rFlavor, const OUString& rDestDoc )
 {
     SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor );
diff --git a/sw/source/uibase/inc/swdtflvr.hxx b/sw/source/uibase/inc/swdtflvr.hxx
index 1b3cf2536964..0b15b8f15acf 100644
--- a/sw/source/uibase/inc/swdtflvr.hxx
+++ b/sw/source/uibase/inc/swdtflvr.hxx
@@ -151,6 +151,7 @@ protected:
                                         const css::datatransfer::DataFlavor& rFlavor ) override;
     virtual void        DragFinished( sal_Int8 nDropAction ) override;
     virtual void        ObjectReleased() override;
+    virtual sal_Bool isComplex() override;
 
     using TransferableHelper::StartDrag;
 
commit 164188c3e2f0be1d20765accd8d59f5e4df77c3d
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 20 10:55:35 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    clipboard: get view setting correct before setClipboard.
    
    Change-Id: If8e9b057b819074f035c598569e3bf6d3d2fff00

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 07158ce052b9..bb938443861f 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -3589,12 +3589,12 @@ static int doc_getClipboard(LibreOfficeKitDocument* pThis,
     }
 
     rtl::Reference<LOKClipboard> xClip(LOKClipboardFactory::getClipboardForCurView());
-    SAL_INFO("lok", "Got clipboard for view " << xClip.get());
 
     css::uno::Reference<css::datatransfer::XTransferable> xTransferable = xClip->getContents();
+    SAL_INFO("lok", "Got from clip: " << xClip.get() << " transferrable: " << xTransferable);
     if (!xTransferable)
     {
-        SetLastExceptionMsg("No selection available");
+        SetLastExceptionMsg("No clipboard content available");
         return 0;
     }
 
@@ -3667,6 +3667,8 @@ static int doc_setClipboard(LibreOfficeKitDocument* pThis,
     auto xClip = forceSetClipboardForCurrentView(pThis);
     xClip->setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
 
+    SAL_INFO("lok", "Set clip: " << xClip.get() << " to: " << xTransferable);
+
     if (!pDoc->isMimeTypeSupported())
     {
         SetLastExceptionMsg("Document doesn't support this mime type");
diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index a959258269a1..ef52a4596c43 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -25,10 +25,13 @@ rtl::Reference<LOKClipboard> LOKClipboardFactory::getClipboardForCurView()
 
     auto it = gClipboards.find(nViewId);
     if (it != gClipboards.end())
+    {
+        SAL_INFO("lok", "Got clip: " << it->second.get() << " from " << nViewId);
         return it->second;
+    }
     rtl::Reference<LOKClipboard> xClip(new LOKClipboard());
     gClipboards[nViewId] = xClip;
-    SAL_INFO("lok", "Created clipboard for view " << nViewId << " as " << xClip.get());
+    SAL_INFO("lok", "Created clip: " << xClip.get() << " for viewId " << nViewId);
     return xClip;
 }
 
@@ -81,6 +84,7 @@ void LOKClipboard::setContents(
     std::vector<Reference<datatransfer::clipboard::XClipboardListener>> aListeners(m_aListeners);
     datatransfer::clipboard::ClipboardEvent aEv;
     aEv.Contents = m_xTransferable;
+    SAL_INFO("lok", "Clip: " << this << " set contents to " << m_xTransferable);
 
     aGuard.clear();
 
commit 153700dbb5b8fd3bcadb46e668224507a6b7fb45
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 20 10:52:51 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    lok: clipboard: per view clipboard creation.
    
    A bit brutal, but the mess around clipboard instantiation is awful.
    
    Change-Id: I62d6af8bf6813e6bab81123417ea8bfb28394e29

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 6874c92f4cf7..d482f23e5eff 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -588,7 +588,7 @@ void DesktopLOKTest::testPasteWriter()
     free(pText);
 
     // textt/plain should be rejected.
-    CPPUNIT_ASSERT(!pDocument->pClass->paste(pDocument, "text/plain;charset=utf-8", aText.getStr(), aText.getLength()));
+    CPPUNIT_ASSERT(!pDocument->pClass->paste(pDocument, "textt/plain;charset=utf-8", aText.getStr(), aText.getLength()));
     // Writer is expected to support text/html.
     CPPUNIT_ASSERT(pDocument->pClass->paste(pDocument, "text/html", aText.getStr(), aText.getLength()));
 
@@ -2772,7 +2772,7 @@ void DesktopLOKTest::testABI()
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(49), offsetof(struct _LibreOfficeKitDocumentClass, selectPart));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(50), offsetof(struct _LibreOfficeKitDocumentClass, moveSelectedParts));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(51), offsetof(struct _LibreOfficeKitDocumentClass, resizeWindow));
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(52), offsetof(struct _LibreOfficeKitDocumentClass, getSelection));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(52), offsetof(struct _LibreOfficeKitDocumentClass, getClipboard));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(53), offsetof(struct _LibreOfficeKitDocumentClass, setClipboard));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(54), offsetof(struct _LibreOfficeKitDocumentClass, getSelectionType));
 
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 2eb862c07564..07158ce052b9 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -3650,9 +3650,6 @@ static int doc_setClipboard(LibreOfficeKitDocument* pThis,
                             const size_t  *pInSizes,
                             const char   **pInStreams)
 {
-    SolarMutexGuard aGuard;
-    if (gImpl)
-
     comphelper::ProfileZone aZone("doc_paste");
 
     SolarMutexGuard aGuard;
diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index 74e5131f435d..a959258269a1 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -9,6 +9,7 @@
 
 #include "lokclipboard.hxx"
 #include <sfx2/lokhelper.hxx>
+#include <cppuhelper/supportsservice.hxx>
 
 using namespace css;
 using namespace css::uno;
@@ -105,17 +106,38 @@ void LOKClipboard::removeClipboardListener(
     m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), listener),
                        m_aListeners.end());
 }
-
-LOKTransferable::LOKTransferable(const char* pMimeType, const char* pData, std::size_t nSize)
-    : m_aMimeType(OUString::fromUtf8(pMimeType))
-    , m_aSequence(reinterpret_cast<const sal_Int8*>(pData), nSize)
+LOKTransferable::LOKTransferable(const OUString& sMimeType,
+                                 const css::uno::Sequence<sal_Int8>& aSequence)
 {
-}
+    m_aContent.reserve(1);
+    m_aFlavors = css::uno::Sequence<css::datatransfer::DataFlavor>(1);
+    initFlavourFromMime(m_aFlavors[0], sMimeType);
 
-LOKTransferable::LOKTransferable(OUString sMimeType, const css::uno::Sequence<sal_Int8>& aSequence)
-    : m_aMimeType(std::move(sMimeType))
-    , m_aSequence(aSequence)
+    uno::Any aContent;
+    if (m_aFlavors[0].DataType == cppu::UnoType<OUString>::get())
+    {
+        auto pText = reinterpret_cast<const sal_Char*>(aSequence.getConstArray());
+        aContent <<= OUString(pText, aSequence.getLength(), RTL_TEXTENCODING_UTF8);
+    }
+    else
+        aContent <<= aSequence;
+    m_aContent.push_back(aContent);
+}
+// cf. sot/source/base/exchange.cxx for these two exceptional types.
+void LOKTransferable::initFlavourFromMime(css::datatransfer::DataFlavor& rFlavor,
+                                          OUString aMimeType)
 {
+    if (aMimeType.startsWith("text/plain"))
+    {
+        aMimeType = "text/plain;charset=utf-16";
+        rFlavor.DataType = cppu::UnoType<OUString>::get();
+    }
+    else if (aMimeType == "application/x-libreoffice-tsvc")
+        rFlavor.DataType = cppu::UnoType<OUString>::get();
+    else
+        rFlavor.DataType = cppu::UnoType<uno::Sequence<sal_Int8>>::get();
+    rFlavor.MimeType = aMimeType;
+    rFlavor.HumanPresentableName = aMimeType;
 }
 
 LOKTransferable::LOKTransferable(const size_t nInCount, const char** pInMimeTypes,
@@ -152,7 +174,7 @@ LOKTransferable::LOKTransferable(const size_t nInCount, const char** pInMimeType
 
 uno::Any SAL_CALL LOKTransferable::getTransferData(const datatransfer::DataFlavor& rFlavor)
 {
-    assert(m_aContent.size() == m_aFlavors.getLength());
+    assert(m_aContent.size() == (size_t)m_aFlavors.getLength());
     for (size_t i = 0; i < m_aContent.size(); ++i)
     {
         if (m_aFlavors[i].MimeType == rFlavor.MimeType)
diff --git a/desktop/source/lib/lokclipboard.hxx b/desktop/source/lib/lokclipboard.hxx
index d917a377eb11..e6f7876d4836 100644
--- a/desktop/source/lib/lokclipboard.hxx
+++ b/desktop/source/lib/lokclipboard.hxx
@@ -29,6 +29,7 @@ class LOKClipboard
                                            css::lang::XServiceInfo>
 {
     osl::Mutex m_aMutex;
+    css::uno::Reference<css::datatransfer::XTransferable> m_xTransferable;
     css::uno::Reference<css::datatransfer::clipboard::XClipboardOwner> m_aOwner;
     std::vector<css::uno::Reference<css::datatransfer::clipboard::XClipboardListener>> m_aListeners;
 
@@ -73,9 +74,12 @@ class LOKTransferable : public cppu::WeakImplHelper<css::datatransfer::XTransfer
     css::uno::Sequence<css::datatransfer::DataFlavor> m_aFlavors;
     std::vector<css::uno::Any> m_aContent;
 
+    static void initFlavourFromMime(css::datatransfer::DataFlavor& rFlavor, OUString sMimeType);
+
 public:
     LOKTransferable(const size_t nInCount, const char** pInMimeTypes, const size_t* pInSizes,
                     const char** pInStreams);
+    LOKTransferable(const OUString& sMimeType, const css::uno::Sequence<sal_Int8>& aSequence);
 
     css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor& rFlavor) override;
 
commit f59e986d0dc04a2d62cc1f236acedfcf727449ce
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jul 20 10:43:39 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    lok: clipboard: per view clipboard creation.
    
    A bit brutal, but the mess around clipboard instantiation is awful.
    
    Change-Id: I62d6af8bf6813e6bab81123417ea8bfb28394e29

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 4ebb21cdfe0a..6874c92f4cf7 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -2772,9 +2772,13 @@ void DesktopLOKTest::testABI()
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(49), offsetof(struct _LibreOfficeKitDocumentClass, selectPart));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(50), offsetof(struct _LibreOfficeKitDocumentClass, moveSelectedParts));
     CPPUNIT_ASSERT_EQUAL(documentClassOffset(51), offsetof(struct _LibreOfficeKitDocumentClass, resizeWindow));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(52), offsetof(struct _LibreOfficeKitDocumentClass, getSelection));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(53), offsetof(struct _LibreOfficeKitDocumentClass, setClipboard));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(54), offsetof(struct _LibreOfficeKitDocumentClass, getSelectionType));
+
     // Extending is fine, update this, and add new assert for the offsetof the
     // new method
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(52), sizeof(struct _LibreOfficeKitDocumentClass));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(55), sizeof(struct _LibreOfficeKitDocumentClass));
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest);
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index eb9c7854bc13..2eb862c07564 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -790,7 +790,7 @@ static char* doc_getTextSelection(LibreOfficeKitDocument* pThis,
                                   const char* pMimeType,
                                   char** pUsedMimeType);
 static int doc_getSelectionType(LibreOfficeKitDocument* pThis);
-static int doc_getSelection (LibreOfficeKitDocument* pThis,
+static int doc_getClipboard (LibreOfficeKitDocument* pThis,
                              const char **pMimeTypes,
                              size_t      *pOutCount,
                              char      ***pOutMimeTypes,
@@ -863,6 +863,34 @@ static size_t doc_renderShapeSelection(LibreOfficeKitDocument* pThis, char** pOu
 static void doc_resizeWindow(LibreOfficeKitDocument* pThis, unsigned nLOKWindowId,
                              const int nWidth, const int nHeight);
 
+} // extern "C"
+
+namespace {
+ITiledRenderable* getTiledRenderable(LibreOfficeKitDocument* pThis)
+{
+    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
+    return dynamic_cast<ITiledRenderable*>(pDocument->mxComponent.get());
+}
+
+/*
+ * Unfortunately clipboard creation using UNO is insanely baroque.
+ * we also need to ensure that this works for the first view which
+ * has no clear 'createView' called for it (unfortunately).
+ */
+
+rtl::Reference<LOKClipboard> forceSetClipboardForCurrentView(LibreOfficeKitDocument *pThis)
+{
+    ITiledRenderable* pDoc = getTiledRenderable(pThis);
+    rtl::Reference<LOKClipboard> xClip(LOKClipboardFactory::getClipboardForCurView());
+
+    SAL_INFO("lok", "Set to clipboard for view " << xClip.get());
+    // FIXME: using a hammer here - should not be necessary if all tests used createView.
+    pDoc->setClipboard(uno::Reference<datatransfer::clipboard::XClipboard>(xClip->getXI(), UNO_QUERY));
+
+    return xClip;
+}
+} // anonymous namespace
+
 LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent)
     : mxComponent(xComponent)
 {
@@ -901,7 +929,7 @@ LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XCompone
         m_pDocumentClass->setTextSelection = doc_setTextSelection;
         m_pDocumentClass->getTextSelection = doc_getTextSelection;
         m_pDocumentClass->getSelectionType = doc_getSelectionType;
-        m_pDocumentClass->getSelection = doc_getSelection;
+        m_pDocumentClass->getClipboard = doc_getClipboard;
         m_pDocumentClass->setClipboard = doc_setClipboard;
         m_pDocumentClass->paste = doc_paste;
         m_pDocumentClass->setGraphicSelection = doc_setGraphicSelection;
@@ -942,6 +970,8 @@ LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XCompone
         gDocumentClass = m_pDocumentClass;
     }
     pClass = m_pDocumentClass.get();
+
+    forceSetClipboardForCurrentView(this);
 }
 
 LibLODocument_Impl::~LibLODocument_Impl()
@@ -956,6 +986,8 @@ LibLODocument_Impl::~LibLODocument_Impl()
     }
 }
 
+extern "C" {
+
 CallbackFlushHandler::CallbackFlushHandler(LibreOfficeKitDocument* pDocument, LibreOfficeKitCallback pCallback, void* pData)
     : Idle( "lokit timer callback" ),
       m_pDocument(pDocument),
@@ -1691,12 +1723,6 @@ LibLibreOffice_Impl::~LibLibreOffice_Impl()
 namespace
 {
 
-ITiledRenderable* getTiledRenderable(LibreOfficeKitDocument* pThis)
-{
-    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
-    return dynamic_cast<ITiledRenderable*>(pDocument->mxComponent.get());
-}
-
 #ifdef IOS
 void paintTileToCGContext(ITiledRenderable* pDocument,
                           void* rCGContext, const Size nCanvasSize,
@@ -3535,7 +3561,7 @@ static int doc_getSelectionType(LibreOfficeKitDocument* pThis)
     return aRet.getLength() ? LOK_SELTYPE_TEXT : LOK_SELTYPE_NONE;
 }
 
-static int doc_getSelection(LibreOfficeKitDocument* pThis,
+static int doc_getClipboard(LibreOfficeKitDocument* pThis,
                             const char **pMimeTypes,
                             size_t      *pOutCount,
                             char      ***pOutMimeTypes,
@@ -3562,7 +3588,10 @@ static int doc_getSelection(LibreOfficeKitDocument* pThis,
         return 0;
     }
 
-    css::uno::Reference<css::datatransfer::XTransferable> xTransferable = pDoc->getSelection();
+    rtl::Reference<LOKClipboard> xClip(LOKClipboardFactory::getClipboardForCurView());
+    SAL_INFO("lok", "Got clipboard for view " << xClip.get());
+
+    css::uno::Reference<css::datatransfer::XTransferable> xTransferable = xClip->getContents();
     if (!xTransferable)
     {
         SetLastExceptionMsg("No selection available");
@@ -3637,9 +3666,9 @@ static int doc_setClipboard(LibreOfficeKitDocument* pThis,
     }
 
     uno::Reference<datatransfer::XTransferable> xTransferable(new LOKTransferable(nInCount, pInMimeTypes, pInSizes, pInStreams));
-    uno::Reference<datatransfer::clipboard::XClipboard> xClipboard(new LOKClipboard);
-    xClipboard->setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
-    pDoc->setClipboard(xClipboard);
+
+    auto xClip = forceSetClipboardForCurrentView(pThis);
+    xClip->setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
 
     if (!pDoc->isMimeTypeSupported())
     {
@@ -4305,7 +4334,7 @@ static void doc_setOutlineState(LibreOfficeKitDocument* pThis, bool bColumn, int
     pDoc->setOutlineState(bColumn, nLevel, nIndex, bHidden);
 }
 
-static int doc_createViewWithOptions(SAL_UNUSED_PARAMETER LibreOfficeKitDocument* /*pThis*/,
+static int doc_createViewWithOptions(LibreOfficeKitDocument* pThis,
                                      const char* pOptions)
 {
     comphelper::ProfileZone aZone("doc_createView");
@@ -4322,7 +4351,11 @@ static int doc_createViewWithOptions(SAL_UNUSED_PARAMETER LibreOfficeKitDocument
         comphelper::LibreOfficeKit::setLanguageTag(LanguageTag(aLanguage));
     }
 
-    return SfxLokHelper::createView();
+    int nId = SfxLokHelper::createView();
+
+    forceSetClipboardForCurrentView(pThis);
+
+    return nId;
 }
 
 static int doc_createView(LibreOfficeKitDocument* pThis)
@@ -5498,14 +5531,10 @@ static void lo_destroy(LibreOfficeKit* pThis)
     SAL_INFO("lok", "LO Destroy Done");
 }
 
-}
-
 #ifdef IOS
 
 // Used by the unmaintained LibreOfficeLight app. Once that has been retired, get rid of this, too.
 
-extern "C"
-{
 __attribute__((visibility("default")))
 void temporaryHackToInvokeCallbackHandlers(LibreOfficeKitDocument* pThis)
 {
@@ -5519,7 +5548,9 @@ void temporaryHackToInvokeCallbackHandlers(LibreOfficeKitDocument* pThis)
         pDocument->mpCallbackFlushHandlers[nOrigViewId]->Invoke();
     }
 }
-}
+
 #endif
 
+} // extern "C"
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
index 015690bd9bc4..74e5131f435d 100644
--- a/desktop/source/lib/lokclipboard.cxx
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -8,23 +8,103 @@
  */
 
 #include "lokclipboard.hxx"
-#include <comphelper/sequence.hxx>
+#include <sfx2/lokhelper.hxx>
 
-using namespace com::sun::star;
+using namespace css;
+using namespace css::uno;
 
-uno::Reference<datatransfer::XTransferable> SAL_CALL LOKClipboard::getContents()
+osl::Mutex LOKClipboardFactory::gMutex;
+std::unordered_map<int, rtl::Reference<LOKClipboard>> LOKClipboardFactory::gClipboards;
+
+rtl::Reference<LOKClipboard> LOKClipboardFactory::getClipboardForCurView()
+{
+    int nViewId = SfxLokHelper::getView(); // currently active.
+
+    osl::MutexGuard aGuard(gMutex);
+
+    auto it = gClipboards.find(nViewId);
+    if (it != gClipboards.end())
+        return it->second;
+    rtl::Reference<LOKClipboard> xClip(new LOKClipboard());
+    gClipboards[nViewId] = xClip;
+    SAL_INFO("lok", "Created clipboard for view " << nViewId << " as " << xClip.get());
+    return xClip;
+}
+
+uno::Reference<uno::XInterface>
+    SAL_CALL LOKClipboardFactory::createInstanceWithArguments(const Sequence<Any>& /* rArgs */)
+{
+    return uno::Reference<uno::XInterface>(
+        static_cast<cppu::OWeakObject*>(getClipboardForCurView().get()));
+}
+
+LOKClipboard::LOKClipboard()
+    : cppu::WeakComponentImplHelper<css::datatransfer::clipboard::XSystemClipboard,
+                                    css::lang::XServiceInfo>(m_aMutex)
+{
+}
+
+Sequence<OUString> LOKClipboard::getSupportedServiceNames_static()
+{
+    Sequence<OUString> aRet{ "com.sun.star.datatransfer.clipboard.SystemClipboard" };
+    return aRet;
+}
+
+OUString LOKClipboard::getImplementationName()
+{
+    return OUString("com.sun.star.datatransfer.LOKClipboard");
+}
+
+Sequence<OUString> LOKClipboard::getSupportedServiceNames()
+{
+    return getSupportedServiceNames_static();
+}
+
+sal_Bool LOKClipboard::supportsService(const OUString& ServiceName)
+{
+    return cppu::supportsService(this, ServiceName);
+}
+
+Reference<css::datatransfer::XTransferable> LOKClipboard::getContents() { return m_xTransferable; }
+
+void LOKClipboard::setContents(
+    const Reference<css::datatransfer::XTransferable>& xTrans,
+    const Reference<css::datatransfer::clipboard::XClipboardOwner>& xClipboardOwner)
 {
-    return m_xTransferable;
+    osl::ClearableMutexGuard aGuard(m_aMutex);
+    Reference<datatransfer::clipboard::XClipboardOwner> xOldOwner(m_aOwner);
+    Reference<datatransfer::XTransferable> xOldContents(m_xTransferable);
+    m_xTransferable = xTrans;
+    m_aOwner = xClipboardOwner;
+
+    std::vector<Reference<datatransfer::clipboard::XClipboardListener>> aListeners(m_aListeners);
+    datatransfer::clipboard::ClipboardEvent aEv;
+    aEv.Contents = m_xTransferable;
+
+    aGuard.clear();
+
+    if (xOldOwner.is() && xOldOwner != xClipboardOwner)
+        xOldOwner->lostOwnership(this, xOldContents);
+    for (auto const& listener : aListeners)
+    {
+        listener->changedContents(aEv);
+    }
 }
 
-void SAL_CALL LOKClipboard::setContents(
-    const uno::Reference<datatransfer::XTransferable>& xTransferable,
-    const uno::Reference<datatransfer::clipboard::XClipboardOwner>& /*xClipboardOwner*/)
+void LOKClipboard::addClipboardListener(
+    const Reference<datatransfer::clipboard::XClipboardListener>& listener)
 {
-    m_xTransferable = xTransferable;
+    osl::ClearableMutexGuard aGuard(m_aMutex);
+    m_aListeners.push_back(listener);
 }
 
-OUString SAL_CALL LOKClipboard::getName() { return OUString(); }
+void LOKClipboard::removeClipboardListener(
+    const Reference<datatransfer::clipboard::XClipboardListener>& listener)
+{
+    osl::ClearableMutexGuard aGuard(m_aMutex);
+    m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), listener),
+                       m_aListeners.end());
+}
 
 LOKTransferable::LOKTransferable(const char* pMimeType, const char* pData, std::size_t nSize)
     : m_aMimeType(OUString::fromUtf8(pMimeType))
diff --git a/desktop/source/lib/lokclipboard.hxx b/desktop/source/lib/lokclipboard.hxx
index 5da8b3e2b63e..d917a377eb11 100644
--- a/desktop/source/lib/lokclipboard.hxx
+++ b/desktop/source/lib/lokclipboard.hxx
@@ -11,24 +11,60 @@
 #define INCLUDED_DESKTOP_SOURCE_LIB_LOKCLIPBOARD_HXX
 
 #include <vector>
+#include <unordered_map>
 
+#include <rtl/ref.hxx>
 #include <cppuhelper/implbase.hxx>
-#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
+#include <cppuhelper/compbase.hxx>
+#include <comphelper/sequence.hxx>
+#include <com/sun/star/lang/XServiceInfo.hpp>
+#include <com/sun/star/lang/XSingleServiceFactory.hpp>
+#include <com/sun/star/datatransfer/clipboard/XSystemClipboard.hpp>
+
+using namespace css::uno;
 
 /// A clipboard implementation for LibreOfficeKit.
-class LOKClipboard : public cppu::WeakImplHelper<css::datatransfer::clipboard::XClipboard>
+class LOKClipboard
+    : public cppu::WeakComponentImplHelper<css::datatransfer::clipboard::XSystemClipboard,
+                                           css::lang::XServiceInfo>
 {
-    css::uno::Reference<css::datatransfer::XTransferable> m_xTransferable;
+    osl::Mutex m_aMutex;
+    css::uno::Reference<css::datatransfer::clipboard::XClipboardOwner> m_aOwner;
+    std::vector<css::uno::Reference<css::datatransfer::clipboard::XClipboardListener>> m_aListeners;
 
 public:
-    css::uno::Reference<css::datatransfer::XTransferable> SAL_CALL getContents() override;
+    LOKClipboard();
+
+    /// get an XInterface easily.
+    css::uno::Reference<css::uno::XInterface> getXI()
+    {
+        return css::uno::Reference<css::uno::XInterface>(static_cast<cppu::OWeakObject*>(this));
+    }
+
+    // XServiceInfo
+    virtual OUString SAL_CALL getImplementationName() override;
+    virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
+    virtual Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
+    static Sequence<OUString> getSupportedServiceNames_static();
 
+    // XClipboard
+    css::uno::Reference<css::datatransfer::XTransferable> SAL_CALL getContents() override;
     void SAL_CALL setContents(
         const css::uno::Reference<css::datatransfer::XTransferable>& xTransferable,
         const css::uno::Reference<css::datatransfer::clipboard::XClipboardOwner>& xClipboardOwner)
         override;
+    OUString SAL_CALL getName() override { return OUString("CLIPBOARD"); }
+
+    // XClipboardEx
+    sal_Int8 SAL_CALL getRenderingCapabilities() override { return 0; }
 
-    OUString SAL_CALL getName() override;
+    // XClipboardNotifier
+    void SAL_CALL addClipboardListener(
+        const css::uno::Reference<css::datatransfer::clipboard::XClipboardListener>& listener)
+        override;
+    void SAL_CALL removeClipboardListener(
+        const css::uno::Reference<css::datatransfer::clipboard::XClipboardListener>& listener)
+        override;
 };
 
 /// Represents the contents of LOKClipboard.
@@ -38,10 +74,8 @@ class LOKTransferable : public cppu::WeakImplHelper<css::datatransfer::XTransfer
     std::vector<css::uno::Any> m_aContent;
 
 public:
-    LOKTransferable(const size_t   nInCount,
-                    const char   **pInMimeTypes,
-                    const size_t  *pInSizes,
-                    const char   **pInStreams);
+    LOKTransferable(const size_t nInCount, const char** pInMimeTypes, const size_t* pInSizes,
+                    const char** pInStreams);
 
     css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor& rFlavor) override;
 
@@ -50,6 +84,27 @@ public:
     sal_Bool SAL_CALL isDataFlavorSupported(const css::datatransfer::DataFlavor& rFlavor) override;
 };
 
+/// Theoretically to hook into the (horrible) vcl dtranscomp.cxx code.
+class LOKClipboardFactory : public ::cppu::WeakComponentImplHelper<css::lang::XSingleServiceFactory>
+{
+    static osl::Mutex gMutex;
+    static std::unordered_map<int, rtl::Reference<LOKClipboard>> gClipboards;
+
+public:
+    LOKClipboardFactory()
+        : cppu::WeakComponentImplHelper<css::lang::XSingleServiceFactory>(gMutex)
+    {
+    }
+
+    css::uno::Reference<css::uno::XInterface> SAL_CALL createInstance() override
+    {
+        return createInstanceWithArguments(css::uno::Sequence<css::uno::Any>());
+    }
+    css::uno::Reference<css::uno::XInterface> SAL_CALL
+    createInstanceWithArguments(const css::uno::Sequence<css::uno::Any>& /* rArgs */) override;
+    static rtl::Reference<LOKClipboard> getClipboardForCurView();
+};
+
 #endif
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h
index abdc9863c1f0..93b430f5da0b 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.h
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
@@ -390,8 +390,8 @@ struct _LibreOfficeKitDocumentClass
                           const int width, const int height);
 
     /// Pass a nullptr terminated array of mime-type strings
-    /// @see lok::Document::getSelection for more details
-    int (*getSelection) (LibreOfficeKitDocument* pThis,
+    /// @see lok::Document::getClipboard for more details
+    int (*getClipboard) (LibreOfficeKitDocument* pThis,
                          const char **pMimeTypes,
                          size_t      *pOutCount,
                          char      ***pOutMimeTypes,
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
index f38d2b5109f6..bd6609c321a4 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -362,7 +362,7 @@ public:
     }
 
     /**
-     * Gets the selected content for the current view as a series of binary streams.
+     * Gets the content on the clipboard for the current view as a series of binary streams.
      *
      * NB. returns a complete set of possible selection types if nullptr is passed for pMimeTypes.
      *
@@ -374,13 +374,13 @@ public:
      *
      * @returns: true on success, false on error.
      */
-    bool getSelection(const char **pMimeTypes,
+    bool getClipboard(const char **pMimeTypes,
                       size_t      *pOutCount,
                       char      ***pOutMimeTypes,
                       size_t     **pOutSizes,
                       char      ***pOutStreams)
     {
-        return mpDoc->pClass->getSelection(mpDoc, pMimeTypes, pOutCount, pOutMimeTypes, pOutSizes, pOutStreams);
+        return mpDoc->pClass->getClipboard(mpDoc, pMimeTypes, pOutCount, pOutMimeTypes, pOutSizes, pOutStreams);
     }
 
     /**
commit 482419ffeab01642db00cd16a2b46a5790a3323e
Author:     Andras Timar <andras.timar at collabora.com>
AuthorDate: Mon Jun 24 10:33:51 2019 +0200
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:48 2019 -0400

    build fix: missing #include <ndgrf.hxx>
    
    Change-Id: I72caa16d3c5b5698226cc7d3f07d3d32820032d8

diff --git a/sw/source/filter/html/wrthtml.cxx b/sw/source/filter/html/wrthtml.cxx
index bba11082b6db..54afa80ba7c9 100644
--- a/sw/source/filter/html/wrthtml.cxx
+++ b/sw/source/filter/html/wrthtml.cxx
@@ -55,6 +55,7 @@
 #include <docary.hxx>
 #include <pam.hxx>
 #include <doc.hxx>
+#include <ndgrf.hxx>
 #include <ndtxt.hxx>
 #include <mdiexp.hxx>
 #include <fltini.hxx>
commit 639d9376685c5393bf3c69a414bb96e2ff1408a2
Author:     Szymon Kłos <szymon.klos at collabora.com>
AuthorDate: Mon Jun 24 09:24:59 2019 +0200
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:47 2019 -0400

    Fix build: unused argument
    
    Change-Id: Id79d9c093da21b9f68bb38a51ac5f142a69364ed

diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
index ca0287a6b584..f38d2b5109f6 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -356,7 +356,7 @@ public:
      *
      * @return an element of the LibreOfficeKitSelectionType enum.
      */
-    int getSelectionType(LibreOfficeKitDocument* pThis)
+    int getSelectionType(LibreOfficeKitDocument* /*pThis*/)
     {
         return mpDoc->pClass->getSelectionType(mpDoc);
     }
commit 7e174f327a3cf16a09e4b2f8e2ffba2225c21901
Author:     Ashod Nakashian <ashod.nakashian at collabora.co.uk>
AuthorDate: Fri Jul 19 23:51:16 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:47 2019 -0400

    LOK: Support detecting complex selections
    
    Includes unit-test, but is otherwise not complete
    implementation.
    
    Change-Id: I5fdcd543358caab1858f6351238c21312665839c

diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 36e7d727efb0..4ebb21cdfe0a 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -139,6 +139,7 @@ public:
     void testInsertCertificate_PEM_DOCX();
     void testSignDocument_PEM_PDF();
     void testTextSelectionHandles();
+    void testComplexSelection();
     void testDialogPaste();
     void testShowHideDialog();
     void testABI();
@@ -193,6 +194,7 @@ public:
     CPPUNIT_TEST(testSignDocument_PEM_PDF);
 #endif
     CPPUNIT_TEST(testTextSelectionHandles);
+    CPPUNIT_TEST(testComplexSelection);
     CPPUNIT_TEST(testDialogPaste);
     CPPUNIT_TEST(testShowHideDialog);
     CPPUNIT_TEST(testABI);
@@ -586,7 +588,7 @@ void DesktopLOKTest::testPasteWriter()
     free(pText);
 
     // textt/plain should be rejected.
-    CPPUNIT_ASSERT(!pDocument->pClass->paste(pDocument, "textt/plain;charset=utf-8", aText.getStr(), aText.getLength()));
+    CPPUNIT_ASSERT(!pDocument->pClass->paste(pDocument, "text/plain;charset=utf-8", aText.getStr(), aText.getLength()));
     // Writer is expected to support text/html.
     CPPUNIT_ASSERT(pDocument->pClass->paste(pDocument, "text/html", aText.getStr(), aText.getLength()));
 
@@ -2637,6 +2639,52 @@ void DesktopLOKTest::testShowHideDialog()
     CPPUNIT_ASSERT_EQUAL(std::string("invalidate"), aView.m_aCallbackWindowResult.get<std::string>("action"));
 }
 
+void DesktopLOKTest::testComplexSelection()
+{
+    // Start with a blank text file and add contents.
+    LibLODocument_Impl* pDocument = loadDoc("blank_text.odt");
+    // LibLODocument_Impl* pDocument = loadDoc("sheet_with_image.ods");
+    static const OString aText("hello world");
+
+    // Paste text.
+    CPPUNIT_ASSERT(pDocument->pClass->paste(pDocument, "text/plain;charset=utf-8", aText.getStr(), aText.getLength()));
+
+    // Paste an image.
+    OUString aFileURL;
+    createFileURL("paste.jpg", aFileURL);
+    std::ifstream aImageStream(aFileURL.toUtf8().copy(strlen("file://")).getStr());
+    std::vector<char> aImageContents((std::istreambuf_iterator<char>(aImageStream)), std::istreambuf_iterator<char>());
+    CPPUNIT_ASSERT(pDocument->pClass->paste(pDocument, "image/jpeg", aImageContents.data(), aImageContents.size()));
+
+    // Now select-all.
+    pDocument->pClass->postUnoCommand(pDocument, ".uno:SelectAll", nullptr, false);
+    Scheduler::ProcessEventsToIdle();
+
+    // We expect this to be complex.
+    const int type = pDocument->pClass->getSelectionType(pDocument);
+    CPPUNIT_ASSERT_EQUAL((int)LOK_SELTYPE_COMPLEX, type);
+
+    // Export as plain text, we should get only the text part "hello".
+    char* pText = pDocument->pClass->getTextSelection(pDocument, "text/plain;charset=utf-8", nullptr);
+    CPPUNIT_ASSERT(pText != nullptr);
+    CPPUNIT_ASSERT_EQUAL(aText, OString(pText));
+    free(pText);
+
+    // Export as rtf, we should also get the image.
+    pText = pDocument->pClass->getTextSelection(pDocument, "text/rtf", nullptr);
+    CPPUNIT_ASSERT(pText != nullptr);
+    CPPUNIT_ASSERT(std::string(pText).find(aText.getStr()) != std::string::npos); // Must have the text.
+    CPPUNIT_ASSERT(std::string(pText).find("pict{") != std::string::npos); // Must have the image as well.
+    free(pText);
+
+    // Export as html, we should also get the image.
+    pText = pDocument->pClass->getTextSelection(pDocument, "text/html", nullptr);
+    CPPUNIT_ASSERT(pText != nullptr);
+    CPPUNIT_ASSERT(std::string(pText).find(aText.getStr()) != std::string::npos); // Must have the text.
+    // CPPUNIT_ASSERT(std::string(pText).find("<img") != std::string::npos); // Must have the image as well.
+    free(pText);
+}
+
 namespace {
 
 constexpr size_t classOffset(int i)
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 3b835c0d66b8..eb9c7854bc13 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -78,6 +78,7 @@
 #include <com/sun/star/util/URLTransformer.hpp>
 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
 #include <com/sun/star/datatransfer/UnsupportedFlavorException.hpp>
+#include <com/sun/star/datatransfer/XTransferable2.hpp>
 #include <com/sun/star/text/TextContentAnchorType.hpp>
 #include <com/sun/star/document/XRedlinesSupplier.hpp>
 #include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
@@ -788,6 +789,7 @@ static void doc_setTextSelection (LibreOfficeKitDocument* pThis,
 static char* doc_getTextSelection(LibreOfficeKitDocument* pThis,
                                   const char* pMimeType,
                                   char** pUsedMimeType);
+static int doc_getSelectionType(LibreOfficeKitDocument* pThis);
 static int doc_getSelection (LibreOfficeKitDocument* pThis,
                              const char **pMimeTypes,
                              size_t      *pOutCount,
@@ -898,6 +900,7 @@ LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XCompone
         m_pDocumentClass->postUnoCommand = doc_postUnoCommand;
         m_pDocumentClass->setTextSelection = doc_setTextSelection;
         m_pDocumentClass->getTextSelection = doc_getTextSelection;
+        m_pDocumentClass->getSelectionType = doc_getSelectionType;
         m_pDocumentClass->getSelection = doc_getSelection;
         m_pDocumentClass->setClipboard = doc_setClipboard;
         m_pDocumentClass->paste = doc_paste;
@@ -3499,6 +3502,39 @@ static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMi
     return convertOString(aRet);
 }
 
+static int doc_getSelectionType(LibreOfficeKitDocument* pThis)
+{
+    SolarMutexGuard aGuard;
+    SetLastExceptionMsg();
+
+    ITiledRenderable* pDoc = getTiledRenderable(pThis);
+    if (!pDoc)
+    {
+        SetLastExceptionMsg("Document doesn't support tiled rendering");
+        return LOK_SELTYPE_NONE;
+    }
+
+    css::uno::Reference<css::datatransfer::XTransferable2> xTransferable(pDoc->getSelection(), css::uno::UNO_QUERY);
+    if (!xTransferable)
+    {
+        SetLastExceptionMsg("No selection available");
+        return LOK_SELTYPE_NONE;
+    }
+
+    if (xTransferable->isComplex())
+        return LOK_SELTYPE_COMPLEX;
+
+    OString aRet;
+    bool bSuccess = getFromTransferrable(xTransferable, OString("text/plain;charset=utf-8"), aRet);
+    if (!bSuccess)
+        return LOK_SELTYPE_NONE;
+
+    if (aRet.getLength() > 1000) // About 2 paragraphs.
+        return LOK_SELTYPE_COMPLEX;
+
+    return aRet.getLength() ? LOK_SELTYPE_TEXT : LOK_SELTYPE_NONE;
+}
+
 static int doc_getSelection(LibreOfficeKitDocument* pThis,
                             const char **pMimeTypes,
                             size_t      *pOutCount,
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h
index 14b7bb995b10..abdc9863c1f0 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.h
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
@@ -405,6 +405,9 @@ struct _LibreOfficeKitDocumentClass
                          const size_t  *pInSizes,
                          const char   **pInStreams);
 
+    /// @see lok::Document::getSelectionType
+    int (*getSelectionType) (LibreOfficeKitDocument* pThis);
+
 #endif // defined LOK_USE_UNSTABLE_API || defined LIBO_INTERNAL_ONLY
 };
 
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
index cb1ca1e4c2d3..ca0287a6b584 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -352,6 +352,16 @@ public:
     }
 
     /**
+     * Gets the type of the selected content.
+     *
+     * @return an element of the LibreOfficeKitSelectionType enum.
+     */
+    int getSelectionType(LibreOfficeKitDocument* pThis)
+    {
+        return mpDoc->pClass->getSelectionType(mpDoc);
+    }
+
+    /**
      * Gets the selected content for the current view as a series of binary streams.
      *
      * NB. returns a complete set of possible selection types if nullptr is passed for pMimeTypes.
diff --git a/include/LibreOfficeKit/LibreOfficeKitEnums.h b/include/LibreOfficeKit/LibreOfficeKitEnums.h
index 4277adbe6dbc..0680c7e6c44f 100644
--- a/include/LibreOfficeKit/LibreOfficeKitEnums.h
+++ b/include/LibreOfficeKit/LibreOfficeKitEnums.h
@@ -47,6 +47,15 @@ typedef enum
 }
 LibreOfficeKitWindowAction;
 
+typedef enum
+{
+    LOK_SELTYPE_NONE,
+    LOK_SELTYPE_TEXT,
+    LOK_SELTYPE_LARGE_TEXT,
+    LOK_SELTYPE_COMPLEX
+}
+LibreOfficeKitSelectionType;
+
 /** Optional features of LibreOfficeKit, in particular callbacks that block
  *  LibreOfficeKit until the corresponding reply is received, which would
  *  deadlock if the client does not support the feature.
diff --git a/include/vcl/transfer.hxx b/include/vcl/transfer.hxx
index 888fe254e115..722d3f26b42b 100644
--- a/include/vcl/transfer.hxx
+++ b/include/vcl/transfer.hxx
@@ -184,6 +184,7 @@ private:
     // Transferable2
     virtual css::uno::Any SAL_CALL getTransferData2(
         const css::datatransfer::DataFlavor& rFlavor, const OUString& rDestDoc ) override;
+    virtual sal_Bool SAL_CALL isComplex() override;
 
     // XEventListener
     virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
diff --git a/offapi/com/sun/star/datatransfer/XTransferable2.idl b/offapi/com/sun/star/datatransfer/XTransferable2.idl
index f792a969ded8..a6b53f0671e2 100644
--- a/offapi/com/sun/star/datatransfer/XTransferable2.idl
+++ b/offapi/com/sun/star/datatransfer/XTransferable2.idl
@@ -31,6 +31,11 @@ interface XTransferable2 : com::sun::star::datatransfer::XTransferable
      */
     any getTransferData2( [in] DataFlavor aFlavor, [in] string aDestShellID )
         raises ( UnsupportedFlavorException, com::sun::star::io::IOException );
+
+    /**
+     * Returns true if the selection contains embedded objects or is a large text blob.
+     */
+    boolean isComplex();
 };
 
 }; }; }; };
diff --git a/sw/source/filter/html/wrthtml.cxx b/sw/source/filter/html/wrthtml.cxx
index 51b7317f64e3..bba11082b6db 100644
--- a/sw/source/filter/html/wrthtml.cxx
+++ b/sw/source/filter/html/wrthtml.cxx
@@ -822,6 +822,13 @@ void SwHTMLWriter::Out_SwDoc( SwPaM* pPam )
                 OutHTML_Section( *this, *rNd.GetSectionNode() );
                 m_nBkmkTabPos = m_bWriteAll ? FindPos_Bkmk( *m_pCurrentPam->GetPoint() ) : -1;
             }
+            else if( rNd.IsGrfNode() )
+            {
+                SwGrfNode* pGrfNd = rNd.GetGrfNode();
+                assert(pGrfNd && !"FIXME: Implement Graphic copy as HTML.");
+                // if (pGrfNd)
+                //     OutHTML_SwGrfNode( *this, *pGrfNd );
+            }
             else if( &rNd == &m_pDoc->GetNodes().GetEndOfContent() )
                 break;
 
diff --git a/vcl/source/treelist/transfer.cxx b/vcl/source/treelist/transfer.cxx
index a33c719f86ee..67e71396e8dc 100644
--- a/vcl/source/treelist/transfer.cxx
+++ b/vcl/source/treelist/transfer.cxx
@@ -388,6 +388,11 @@ Any SAL_CALL TransferableHelper::getTransferData2( const DataFlavor& rFlavor, co
     return maAny;
 }
 
+sal_Bool SAL_CALL TransferableHelper::isComplex()
+{
+    //FIXME: get from each document.
+    return sal_True;
+}
 
 Sequence< DataFlavor > SAL_CALL TransferableHelper::getTransferDataFlavors()
 {
commit dc4e514f17d1923c19d2b61f733b196e5acf7dec
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Sat Jun 22 12:48:49 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:47 2019 -0400

    lok: use utf-8 for text/plain always.
    
    Change-Id: Ie91f89b3dc6ba3eddcf8383948dddc7878eae4d8

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index bb50736df0fe..3b835c0d66b8 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -3557,10 +3557,13 @@ static int doc_getSelection(LibreOfficeKitDocument* pThis,
     *pOutStreams = static_cast<char **>(malloc(*pOutCount * sizeof(char *)));
     for (size_t i = 0; i < aMimeTypes.size(); ++i)
     {
-        (*pOutMimeTypes)[i] = strdup(aMimeTypes[i].getStr());
+        if (aMimeTypes[i] == "text/plain;charset=utf-16")
+            (*pOutMimeTypes)[i] = strdup("text/plain;charset=utf-8");
+        else
+            (*pOutMimeTypes)[i] = strdup(aMimeTypes[i].getStr());
 
         OString aRet;
-        bool bSuccess = getFromTransferrable(xTransferable, aMimeTypes[i], aRet);
+        bool bSuccess = getFromTransferrable(xTransferable, (*pOutMimeTypes)[i], aRet);
         if (!bSuccess || aRet.getLength() < 1)
         {
             (*pOutSizes)[i] = 0;
commit 3dbf6e43ebd6a52b665fab906711f173f0e5ba82
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Fri Jul 19 23:44:20 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri Aug 2 11:41:47 2019 -0400

    Add more powerful selection fetch & clipboard set methods.
    
    Change-Id: I6633356d13480377a83a006588ec69ebcb56a93f

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 4b808f4b1c1b..bb50736df0fe 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -170,6 +170,7 @@ static std::weak_ptr< LibreOfficeKitDocumentClass > gDocumentClass;
 
 static void SetLastExceptionMsg(const OUString& s = OUString())
 {
+    SAL_WARN("lok", "lok exception " + s);
     if (gImpl)
         gImpl->maLastExceptionMsg = s;
 }
@@ -787,6 +788,17 @@ static void doc_setTextSelection (LibreOfficeKitDocument* pThis,
 static char* doc_getTextSelection(LibreOfficeKitDocument* pThis,
                                   const char* pMimeType,
                                   char** pUsedMimeType);
+static int doc_getSelection (LibreOfficeKitDocument* pThis,

... etc. - the rest is truncated


More information about the Libreoffice-commits mailing list