Blob Blame History Raw
From 89ad7ba2b6f480c20d332bc3c171045afe21e23b Mon Sep 17 00:00:00 2001
From: Miklos Vajna <vmiklos@collabora.co.uk>
Date: Thu, 22 Oct 2015 11:26:13 +0200
Subject: [PATCH 225/398] LOK: add Document::paste()

Change-Id: I34998229e7f5cac4c62c859861783be3c161f9bf
(cherry picked from commit 6552767aa5ed61215eb64dac0cc026a5f7a9aad1)
---
 desktop/Library_sofficeapp.mk             |  2 +
 desktop/source/lib/init.cxx               | 38 ++++++++++++++
 desktop/source/lib/lokclipboard.cxx       | 83 +++++++++++++++++++++++++++++++
 desktop/source/lib/lokclipboard.hxx       | 58 +++++++++++++++++++++
 include/LibreOfficeKit/LibreOfficeKit.h   |  6 +++
 include/LibreOfficeKit/LibreOfficeKit.hxx | 12 +++++
 6 files changed, 199 insertions(+)
 create mode 100644 desktop/source/lib/lokclipboard.cxx
 create mode 100644 desktop/source/lib/lokclipboard.hxx

diff --git a/desktop/Library_sofficeapp.mk b/desktop/Library_sofficeapp.mk
index 2e3acb79b3c6..fb73cfa78f4b 100644
--- a/desktop/Library_sofficeapp.mk
+++ b/desktop/Library_sofficeapp.mk
@@ -122,6 +122,7 @@ ifneq ($(filter $(OS),ANDROID IOS),)
 $(eval $(call gb_Library_add_exception_objects,sofficeapp,\
 	desktop/source/lib/init \
 	desktop/source/lib/lokinteractionhandler \
+	desktop/source/lib/lokclipboard \
 	$(if $(filter $(OS),ANDROID), \
 		desktop/source/lib/lokandroid) \
 ))
@@ -130,6 +131,7 @@ ifeq ($(GUIBASE),unx)
 $(eval $(call gb_Library_add_exception_objects,sofficeapp,\
 	desktop/source/lib/init \
 	desktop/source/lib/lokinteractionhandler \
+	desktop/source/lib/lokclipboard \
 ))
 endif
 endif
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 01fe099ccbe9..0459176c24a0 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -43,6 +43,7 @@
 #include <com/sun/star/ucb/XContentProvider.hpp>
 #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
 #include <com/sun/star/util/URLTransformer.hpp>
+#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
 
 #include <editeng/fontitem.hxx>
 #include <editeng/flstitem.hxx>
@@ -78,6 +79,7 @@
 #include "../../inc/lib/init.hxx"
 
 #include "lokinteractionhandler.hxx"
+#include <lokclipboard.hxx>
 
 using namespace css;
 using namespace vcl;
@@ -246,6 +248,10 @@ static void doc_setTextSelection (LibreOfficeKitDocument* pThis,
 static char* doc_getTextSelection(LibreOfficeKitDocument* pThis,
                                   const char* pMimeType,
                                   char** pUsedMimeType);
+static bool doc_paste(LibreOfficeKitDocument* pThis,
+                      const char* pMimeType,
+                      const char* pData,
+                      size_t nSize);
 static void doc_setGraphicSelection (LibreOfficeKitDocument* pThis,
                                   int nType,
                                   int nX,
@@ -286,6 +292,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->paste = doc_paste;
         m_pDocumentClass->setGraphicSelection = doc_setGraphicSelection;
         m_pDocumentClass->resetSelection = doc_resetSelection;
         m_pDocumentClass->getCommandValues = doc_getCommandValues;
@@ -989,6 +996,37 @@ static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMi
     return pMemory;
 }
 
+static bool doc_paste(LibreOfficeKitDocument* pThis, const char* pMimeType, const char* pData, size_t nSize)
+{
+    ITiledRenderable* pDoc = getTiledRenderable(pThis);
+    if (!pDoc)
+    {
+        gImpl->maLastExceptionMsg = "Document doesn't support tiled rendering";
+        return false;
+    }
+
+    uno::Reference<datatransfer::XTransferable> xTransferable(new LOKTransferable(pMimeType, pData, nSize));
+    uno::Reference<datatransfer::clipboard::XClipboard> xClipboard(new LOKClipboard());
+    xClipboard->setContents(xTransferable, uno::Reference<datatransfer::clipboard::XClipboardOwner>());
+    vcl::Window* pWindow = pDoc->getWindow();
+    if (!pWindow)
+    {
+        gImpl->maLastExceptionMsg = "Document did not provide a window";
+        return false;
+    }
+
+    pWindow->SetClipboard(xClipboard);
+    OUString aCommand(".uno:Paste");
+    uno::Sequence<beans::PropertyValue> aPropertyValues;
+    if (!comphelper::dispatchCommand(aCommand, aPropertyValues))
+    {
+        gImpl->maLastExceptionMsg = "Failed to dispatch the .uno: command";
+        return false;
+    }
+
+    return true;
+}
+
 static void doc_setGraphicSelection(LibreOfficeKitDocument* pThis, int nType, int nX, int nY)
 {
     ITiledRenderable* pDoc = getTiledRenderable(pThis);
diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx
new file mode 100644
index 000000000000..a81902b15b6b
--- /dev/null
+++ b/desktop/source/lib/lokclipboard.cxx
@@ -0,0 +1,83 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <lokclipboard.hxx>
+#include <comphelper/sequence.hxx>
+
+using namespace com::sun::star;
+
+uno::Reference<datatransfer::XTransferable> SAL_CALL LOKClipboard::getContents()
+throw (uno::RuntimeException, std::exception)
+{
+    return m_xTransferable;
+}
+
+void SAL_CALL LOKClipboard::setContents(const uno::Reference<datatransfer::XTransferable>& xTransferable,
+                                        const uno::Reference<datatransfer::clipboard::XClipboardOwner>& /*xClipboardOwner*/)
+throw (uno::RuntimeException, std::exception)
+{
+    m_xTransferable = xTransferable;
+}
+
+OUString SAL_CALL LOKClipboard::getName() throw (uno::RuntimeException, std::exception)
+{
+    return OUString();
+}
+
+LOKTransferable::LOKTransferable(const char* pMimeType, const char* pData, size_t nSize)
+    : m_aMimeType(pMimeType),
+      m_aText(pData, nSize)
+{
+}
+
+uno::Any SAL_CALL LOKTransferable::getTransferData(const datatransfer::DataFlavor& rFlavor)
+throw(datatransfer::UnsupportedFlavorException, io::IOException, uno::RuntimeException, std::exception)
+{
+    uno::Any aRet;
+    if (m_aMimeType == "text/plain;charset=utf-8" && rFlavor.MimeType == "text/plain;charset=utf-16")
+        aRet <<= OStringToOUString(m_aText, RTL_TEXTENCODING_UTF8);
+    return aRet;
+}
+
+std::vector<datatransfer::DataFlavor> LOKTransferable::getTransferDataFlavorsAsVector()
+{
+    std::vector<datatransfer::DataFlavor> aRet;
+    datatransfer::DataFlavor aFlavor;
+    aFlavor.MimeType = OUString::fromUtf8(m_aMimeType.getStr());
+    aFlavor.DataType = cppu::UnoType< uno::Sequence<sal_Int8> >::get();
+
+    sal_Int32 nIndex(0);
+    if (m_aMimeType.getToken(0, ';', nIndex) == "text/plain")
+    {
+        if (m_aMimeType.getToken(0, ';', nIndex) != "charset=utf-16")
+            aFlavor.MimeType = "text/plain;charset=utf-16";
+        aFlavor.DataType = cppu::UnoType<OUString>::get();
+    }
+    aRet.push_back(aFlavor);
+
+    return aRet;
+}
+
+uno::Sequence<datatransfer::DataFlavor> SAL_CALL LOKTransferable::getTransferDataFlavors()
+throw(uno::RuntimeException, std::exception)
+{
+    return comphelper::containerToSequence(getTransferDataFlavorsAsVector());
+}
+
+sal_Bool SAL_CALL LOKTransferable::isDataFlavorSupported(const datatransfer::DataFlavor& rFlavor)
+throw(uno::RuntimeException, std::exception)
+{
+    const std::vector<datatransfer::DataFlavor> aFlavors = getTransferDataFlavorsAsVector();
+    return std::find_if(aFlavors.begin(), aFlavors.end(), [&rFlavor](const datatransfer::DataFlavor& i)
+    {
+        return i.MimeType == rFlavor.MimeType && i.DataType == rFlavor.DataType;
+    }) != aFlavors.end();
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/source/lib/lokclipboard.hxx b/desktop/source/lib/lokclipboard.hxx
new file mode 100644
index 000000000000..b982e1c734ee
--- /dev/null
+++ b/desktop/source/lib/lokclipboard.hxx
@@ -0,0 +1,58 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_DESKTOP_SOURCE_LIB_LOKCLIPBOARD_HXX
+#define INCLUDED_DESKTOP_SOURCE_LIB_LOKCLIPBOARD_HXX
+
+#include <vector>
+
+#include <cppuhelper/implbase.hxx>
+#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
+
+/// A clipboard implementation for LibreOfficeKit.
+class LOKClipboard : public cppu::WeakImplHelper<css::datatransfer::clipboard::XClipboard>
+{
+    css::uno::Reference<css::datatransfer::XTransferable> m_xTransferable;
+
+public:
+    virtual css::uno::Reference<css::datatransfer::XTransferable> SAL_CALL getContents()
+    throw(css::uno::RuntimeException, std::exception) override;
+
+    virtual void SAL_CALL setContents(const css::uno::Reference<css::datatransfer::XTransferable>& xTransferable,
+                                      const css::uno::Reference<css::datatransfer::clipboard::XClipboardOwner>& xClipboardOwner)
+    throw(css::uno::RuntimeException, std::exception) override;
+
+    virtual OUString SAL_CALL getName() throw(css::uno::RuntimeException, std::exception) override;
+};
+
+/// Represents the contents of LOKClipboard.
+class LOKTransferable : public cppu::WeakImplHelper<css::datatransfer::XTransferable>
+{
+    OString m_aMimeType;
+    OString m_aText;
+
+    /// Provides a list of flavors, used by getTransferDataFlavors() and isDataFlavorSupported().
+    std::vector<css::datatransfer::DataFlavor> getTransferDataFlavorsAsVector();
+
+public:
+    LOKTransferable(const char* pMimeType, const char* pData, size_t nSize);
+
+    virtual css::uno::Any SAL_CALL getTransferData(const css::datatransfer::DataFlavor& rFlavor)
+    throw(css::datatransfer::UnsupportedFlavorException, css::io::IOException, css::uno::RuntimeException, std::exception) override;
+
+    virtual css::uno::Sequence<css::datatransfer::DataFlavor> SAL_CALL getTransferDataFlavors()
+    throw(css::uno::RuntimeException, std::exception) override;
+
+    virtual sal_Bool SAL_CALL isDataFlavorSupported(const css::datatransfer::DataFlavor& rFlavor)
+    throw(css::uno::RuntimeException, std::exception) override;
+};
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h
index 83dcc9803d8a..d83717b4a809 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.h
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
@@ -157,6 +157,12 @@ struct _LibreOfficeKitDocumentClass
                                const char* pMimeType,
                                char** pUsedMimeType);
 
+    /// @see lok::Document::paste().
+    bool (*paste) (LibreOfficeKitDocument* pThis,
+                   const char* pMimeType,
+                   const char* pData,
+                   size_t nSize);
+
     /// @see lok::Document::setGraphicSelection
     void (*setGraphicSelection) (LibreOfficeKitDocument* pThis,
                                  int nType,
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
index e9167c510110..db7807d999ea 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -247,6 +247,18 @@ public:
     }
 
     /**
+     * Pastes content at the current cursor position.
+     *
+     * @param pMimeType format of pData, for example text/plain;charset=utf-8.
+     * @param pData the actual data to be pasted.
+     * @return if the supplied data was pasted successfully.
+     */
+    inline bool paste(const char* pMimeType, const char* pData, size_t nSize)
+    {
+        return mpDoc->pClass->paste(mpDoc, pMimeType, pData, nSize);
+    }
+
+    /**
      * Adjusts the graphic selection.
      *
      * @param nType @see LibreOfficeKitSetGraphicSelectionType
-- 
2.12.0