Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / osx / OSXTransferable.cxx
blob120f6b16922018596d4d3f99fd3864b588f05df3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <osl/diagnose.h>
23 #include "OSXTransferable.hxx"
25 #include "DataFlavorMapping.hxx"
27 using namespace std;
28 using namespace osl;
29 using namespace cppu;
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::datatransfer;
32 using namespace com::sun::star::io;
33 using namespace com::sun::star::lang;
34 using namespace com::sun::star::container;
36 namespace // private
38 bool isValidFlavor( const DataFlavor& aFlavor )
40 size_t len = aFlavor.MimeType.getLength();
41 Type dtype = aFlavor.DataType;
42 return ((len > 0) && ((dtype == cppu::UnoType<Sequence<sal_Int8>>::get()) || (dtype == cppu::UnoType<OUString>::get())));
45 bool cmpAllContentTypeParameter(const Reference<XMimeContentType> xLhs,
46 const Reference<XMimeContentType> xRhs)
48 Sequence<OUString> xLhsFlavors = xLhs->getParameters();
49 Sequence<OUString> xRhsFlavors = xRhs->getParameters();
51 // Stop here if the number of parameters is different already
52 if (xLhsFlavors.getLength() != xRhsFlavors.getLength())
53 return false;
55 try
57 OUString pLhs;
58 OUString pRhs;
60 for (sal_Int32 i = 0; i < xLhsFlavors.getLength(); i++)
62 pLhs = xLhs->getParameterValue(xLhsFlavors[i]);
63 pRhs = xRhs->getParameterValue(xLhsFlavors[i]);
65 if (!pLhs.equalsIgnoreAsciiCase(pRhs))
67 return false;
71 catch(IllegalArgumentException&)
73 return false;
76 return true;
79 } // namespace private
81 OSXTransferable::OSXTransferable(const Reference<XMimeContentTypeFactory> rXMimeCntFactory,
82 DataFlavorMapperPtr_t pDataFlavorMapper,
83 NSPasteboard* pasteboard) :
84 mrXMimeCntFactory(rXMimeCntFactory),
85 mDataFlavorMapper(pDataFlavorMapper),
86 mPasteboard(pasteboard)
88 [mPasteboard retain];
90 initClipboardItemList();
93 OSXTransferable::~OSXTransferable()
95 [mPasteboard release];
98 Any SAL_CALL OSXTransferable::getTransferData( const DataFlavor& aFlavor )
99 throw( UnsupportedFlavorException, IOException, RuntimeException, std::exception )
101 if (!isValidFlavor(aFlavor) || !isDataFlavorSupported(aFlavor))
103 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
104 static_cast<XTransferable*>(this));
107 bool bInternal(false);
108 NSString* sysFormat =
109 (aFlavor.MimeType.startsWith("image/png"))
110 ? DataFlavorMapper::openOfficeImageToSystemFlavor( mPasteboard )
111 : mDataFlavorMapper->openOfficeToSystemFlavor(aFlavor, bInternal);
112 DataProviderPtr_t dp;
114 if ([sysFormat caseInsensitiveCompare: NSFilenamesPboardType] == NSOrderedSame)
116 NSArray* sysData = [mPasteboard propertyListForType: sysFormat];
117 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
119 else
121 NSData* sysData = [mPasteboard dataForType: sysFormat];
122 dp = DataFlavorMapper::getDataProvider(sysFormat, sysData);
125 if (dp.get() == NULL)
127 throw UnsupportedFlavorException("AquaClipboard: Unsupported data flavor",
128 static_cast<XTransferable*>(this));
131 return dp->getOOoData();
134 Sequence< DataFlavor > SAL_CALL OSXTransferable::getTransferDataFlavors( )
135 throw( RuntimeException, std::exception )
137 return mFlavorList;
140 sal_Bool SAL_CALL OSXTransferable::isDataFlavorSupported(const DataFlavor& aFlavor)
141 throw( RuntimeException, std::exception )
143 for (sal_Int32 i = 0; i < mFlavorList.getLength(); i++)
144 if (compareDataFlavors(aFlavor, mFlavorList[i]))
145 return true;
147 return false;
150 void OSXTransferable::initClipboardItemList()
152 NSArray* pboardFormats = [mPasteboard types];
154 if (pboardFormats == NULL)
156 throw RuntimeException("AquaClipboard: Cannot get clipboard data",
157 static_cast<XTransferable*>(this));
160 mFlavorList = mDataFlavorMapper->typesArrayToFlavorSequence(pboardFormats);
163 /* Compares two DataFlavors. Returns true if both DataFlavor have the same media type
164 and the number of parameter and all parameter values do match otherwise false
165 is returned.
167 bool OSXTransferable::compareDataFlavors(const DataFlavor& lhs, const DataFlavor& rhs )
171 Reference<XMimeContentType> xLhs(mrXMimeCntFactory->createMimeContentType(lhs.MimeType));
172 Reference<XMimeContentType> xRhs(mrXMimeCntFactory->createMimeContentType(rhs.MimeType));
174 if (!xLhs->getFullMediaType().equalsIgnoreAsciiCase(xRhs->getFullMediaType()) ||
175 !cmpAllContentTypeParameter(xLhs, xRhs))
177 return false;
180 catch( IllegalArgumentException& )
182 OSL_FAIL( "Invalid content type detected" );
183 return false;
186 return true;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */