diff options
author | Miklos Vajna <vmiklos@collabora.co.uk> | 2015-10-22 11:26:13 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2015-10-22 13:34:20 +0200 |
commit | 6552767aa5ed61215eb64dac0cc026a5f7a9aad1 (patch) | |
tree | 63064bc17570fe7b74d0103d0cba5f49e8c79957 /desktop | |
parent | 981a974824642a81f86c526dea682cd27cd437db (diff) |
LOK: add Document::paste()
Change-Id: I34998229e7f5cac4c62c859861783be3c161f9bf
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/Library_sofficeapp.mk | 2 | ||||
-rw-r--r-- | desktop/source/lib/init.cxx | 38 | ||||
-rw-r--r-- | desktop/source/lib/lokclipboard.cxx | 83 | ||||
-rw-r--r-- | desktop/source/lib/lokclipboard.hxx | 58 |
4 files changed, 181 insertions, 0 deletions
diff --git a/desktop/Library_sofficeapp.mk b/desktop/Library_sofficeapp.mk index 1b8c5f7c214d..0254b74b9f66 100644 --- a/desktop/Library_sofficeapp.mk +++ b/desktop/Library_sofficeapp.mk @@ -122,6 +122,7 @@ ifneq ($(filter $(OS),ANDROID IOS MACOSX),) $(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 ($(USING_X11),TRUE) $(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 405e5ff12165..9ac9347d35c0 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -44,6 +44,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> @@ -79,6 +80,7 @@ #include "../../inc/lib/init.hxx" #include "lokinteractionhandler.hxx" +#include <lokclipboard.hxx> using namespace css; using namespace vcl; @@ -247,6 +249,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, @@ -287,6 +293,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; @@ -990,6 +997,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: */ |