summaryrefslogtreecommitdiff
path: root/desktop
diff options
context:
space:
mode:
authorMichael Meeks <michael.meeks@collabora.com>2019-06-06 22:20:33 +0100
committerMichael Meeks <michael.meeks@collabora.com>2019-06-07 12:44:44 +0100
commit6804b001d4255a38f680182099c6d23d7e53a976 (patch)
treeecf7e2c3c2be357737cfe38adced8fdac906fd78 /desktop
parenta4ec8b5d3df1d78b0288c06109887490c74801ba (diff)
lok: re-factor getTextSelection.
Change-Id: I2c27c213ee980e19d6020e9599b2b72115e7f28e
Diffstat (limited to 'desktop')
-rw-r--r--desktop/source/lib/init.cxx96
1 files changed, 89 insertions, 7 deletions
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 2bafd7dc62e4..4ab1f67d7e68 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -76,6 +76,7 @@
#include <com/sun/star/ucb/XUniversalContentBroker.hpp>
#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/text/TextContentAnchorType.hpp>
#include <com/sun/star/document/XRedlinesSupplier.hpp>
#include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
@@ -3366,6 +3367,71 @@ static void doc_setTextSelection(LibreOfficeKitDocument* pThis, int nType, int n
pDoc->setTextSelection(nType, nX, nY);
}
+static bool getFromTransferrable(
+ const css::uno::Reference<css::datatransfer::XTransferable> &xTransferable,
+ const char *pMimeType, OString &aRet)
+{
+ // Take care of UTF-8 text here.
+ OString aMimeType(pMimeType);
+ bool bConvert = false;
+ sal_Int32 nIndex = 0;
+ if (aMimeType.getToken(0, ';', nIndex) == "text/plain")
+ {
+ if (aMimeType.getToken(0, ';', nIndex) == "charset=utf-8")
+ {
+ aMimeType = "text/plain;charset=utf-16";
+ bConvert = true;
+ }
+ }
+
+ datatransfer::DataFlavor aFlavor;
+ aFlavor.MimeType = OUString::fromUtf8(aMimeType.getStr());
+ if (aMimeType == "text/plain;charset=utf-16")
+ aFlavor.DataType = cppu::UnoType<OUString>::get();
+ else
+ aFlavor.DataType = cppu::UnoType< uno::Sequence<sal_Int8> >::get();
+
+ if (!xTransferable->isDataFlavorSupported(aFlavor))
+ {
+ SetLastExceptionMsg("Flavor " + aFlavor.MimeType + " is not supported");
+ return false;
+ }
+
+ uno::Any aAny;
+ try
+ {
+ aAny = xTransferable->getTransferData(aFlavor);
+ }
+ catch (const css::datatransfer::UnsupportedFlavorException& e)
+ {
+ SetLastExceptionMsg("Unsupported flavor " + aFlavor.MimeType + " exception " + e.Message);
+ return false;
+ }
+ catch (const css::uno::Exception& e)
+ {
+ SetLastExceptionMsg("Exception getting " + aFlavor.MimeType + " exception " + e.Message);
+ return false;
+ }
+
+ if (aFlavor.DataType == cppu::UnoType<OUString>::get())
+ {
+ OUString aString;
+ aAny >>= aString;
+ if (bConvert)
+ aRet = OUStringToOString(aString, RTL_TEXTENCODING_UTF8);
+ else
+ aRet = OString(reinterpret_cast<const sal_Char *>(aString.getStr()), aString.getLength() * sizeof(sal_Unicode));
+ }
+ else
+ {
+ uno::Sequence<sal_Int8> aSequence;
+ aAny >>= aSequence;
+ aRet = OString(reinterpret_cast<sal_Char*>(aSequence.getArray()), aSequence.getLength());
+ }
+
+ return true;;
+}
+
static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMimeType, char** pUsedMimeType)
{
comphelper::ProfileZone aZone("doc_getTextSelection");
@@ -3380,18 +3446,34 @@ static char* doc_getTextSelection(LibreOfficeKitDocument* pThis, const char* pMi
return nullptr;
}
- OString aUsedMimeType;
- OString aRet = pDoc->getTextSelection(pMimeType, aUsedMimeType);
- if (aUsedMimeType.isEmpty())
- aRet = pDoc->getTextSelection("text/plain;charset=utf-8", aUsedMimeType);
+ css::uno::Reference<css::datatransfer::XTransferable> xTransferable = pDoc->getSelection();
+ if (!xTransferable)
+ {
+ SetLastExceptionMsg("No selection available");
+ return nullptr;
+ }
+
+ const char *pType = pMimeType;
+ if (!pType || pType[0] == '\0')
+ pType = "text/plain;charset=utf-8";
+
+ OString aRet;
+ bool bSuccess = getFromTransferrable(xTransferable, pType, aRet);
+ if (!bSuccess)
+ return nullptr;
char* pMemory = static_cast<char*>(malloc(aRet.getLength() + 1));
strcpy(pMemory, aRet.getStr());
- if (pUsedMimeType)
+ if (pUsedMimeType) // legacy
{
- *pUsedMimeType = static_cast<char*>(malloc(aUsedMimeType.getLength() + 1));
- strcpy(*pUsedMimeType, aUsedMimeType.getStr());
+ if (pMimeType)
+ {
+ *pUsedMimeType = static_cast<char*>(malloc(strlen(pMimeType) + 1));
+ strcpy(*pUsedMimeType, pMimeType);
+ }
+ else
+ *pUsedMimeType = nullptr;
}
return pMemory;