1 /* -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 "ios/iosinst.hxx"
22 #include "clipboard.hxx"
24 #include "DataFlavorMapping.hxx"
25 #include "iOSTransferable.hxx"
26 #include <com/sun/star/datatransfer/MimeContentTypeFactory.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <comphelper/processfactory.hxx>
29 #include <cppuhelper/supportsservice.hxx>
31 iOSClipboard::iOSClipboard()
32 : WeakComponentImplHelper
<XSystemClipboard
, XServiceInfo
>(m_aMutex
)
34 auto xContext
= comphelper::getProcessComponentContext();
36 mrXMimeCntFactory
= css::datatransfer::MimeContentTypeFactory::create(xContext
);
38 mpDataFlavorMapper
.reset(new DataFlavorMapper());
40 mPasteboard
= [UIPasteboard generalPasteboard
];
41 assert(mPasteboard
!= nil
);
44 iOSClipboard::~iOSClipboard() {}
46 css::uno::Reference
<css::datatransfer::XTransferable
> SAL_CALL
iOSClipboard::getContents()
48 osl::MutexGuard
aGuard(m_aMutex
);
50 return css::uno::Reference
<css::datatransfer::XTransferable
>(
51 new iOSTransferable(mrXMimeCntFactory
, mpDataFlavorMapper
, mPasteboard
));
54 void SAL_CALL
iOSClipboard::setContents(
55 const css::uno::Reference
<css::datatransfer::XTransferable
>& xTransferable
,
56 const css::uno::Reference
<css::datatransfer::clipboard::XClipboardOwner
>& /*xClipboardOwner*/)
58 NSArray
* types
= xTransferable
.is() ? mpDataFlavorMapper
->flavorSequenceToTypesArray(
59 xTransferable
->getTransferDataFlavors())
62 osl::ClearableMutexGuard
aGuard(m_aMutex
);
64 NSMutableDictionary
* dict
= [NSMutableDictionary dictionaryWithCapacity
:1];
65 NSArray
* array
= @
[ dict
];
67 for (sal_uInt32 i
= 0; i
< [types count
]; ++i
)
69 DataProviderPtr_t dp
= mpDataFlavorMapper
->getDataProvider(types
[i
], xTransferable
);
71 if (dp
.get() != nullptr)
73 NSData
* pBoardData
= (NSData
*)dp
->getSystemData();
74 dict
[types
[i
]] = pBoardData
;
77 [mPasteboard setItems
:array options
:@
{}];
79 // We don't keep a copy of the clipboard contents around in-process, so fire the lost clipboard
80 // ownership event right away.
81 // fireLostClipboardOwnershipEvent(xClipboardOwner, xTransferable);
83 // fireClipboardChangedEvent(xTransferable);
86 OUString SAL_CALL
iOSClipboard::getName() { return OUString(); }
88 sal_Int8 SAL_CALL
iOSClipboard::getRenderingCapabilities() { return 0; }
90 void SAL_CALL
iOSClipboard::addClipboardListener(
91 const css::uno::Reference
<css::datatransfer::clipboard::XClipboardListener
>& listener
)
93 osl::MutexGuard
aGuard(m_aMutex
);
96 throw css::lang::IllegalArgumentException(
97 "empty reference", static_cast<css::datatransfer::clipboard::XClipboardEx
*>(this), 1);
99 mClipboardListeners
.push_back(listener
);
102 void SAL_CALL
iOSClipboard::removeClipboardListener(
103 const css::uno::Reference
<css::datatransfer::clipboard::XClipboardListener
>& listener
)
105 osl::MutexGuard
aGuard(m_aMutex
);
108 throw css::lang::IllegalArgumentException(
109 "empty reference", static_cast<css::datatransfer::clipboard::XClipboardEx
*>(this), 1);
111 mClipboardListeners
.remove(listener
);
114 void iOSClipboard::fireClipboardChangedEvent(
115 css::uno::Reference
<css::datatransfer::XTransferable
> xNewContents
)
117 osl::ClearableMutexGuard
aGuard(m_aMutex
);
119 std::list
<css::uno::Reference
<css::datatransfer::clipboard::XClipboardListener
>> listeners(
120 mClipboardListeners
);
121 css::datatransfer::clipboard::ClipboardEvent aEvent
;
123 if (!listeners
.empty())
125 aEvent
= css::datatransfer::clipboard::ClipboardEvent(static_cast<OWeakObject
*>(this),
131 while (!listeners
.empty())
133 if (listeners
.front().is())
137 listeners
.front()->changedContents(aEvent
);
139 catch (const css::uno::RuntimeException
&)
143 listeners
.pop_front();
147 void iOSClipboard::fireLostClipboardOwnershipEvent(
148 css::uno::Reference
<css::datatransfer::clipboard::XClipboardOwner
> const& oldOwner
,
149 css::uno::Reference
<css::datatransfer::XTransferable
> const& oldContent
)
151 assert(oldOwner
.is());
155 oldOwner
->lostOwnership(static_cast<css::datatransfer::clipboard::XClipboardEx
*>(this),
158 catch (const css::uno::RuntimeException
&)
163 OUString SAL_CALL
iOSClipboard::getImplementationName()
165 return OUString("com.sun.star.datatransfer.clipboard.iOSClipboard");
168 sal_Bool SAL_CALL
iOSClipboard::supportsService(const OUString
& ServiceName
)
170 return cppu::supportsService(this, ServiceName
);
173 css::uno::Sequence
<OUString
> SAL_CALL
iOSClipboard::getSupportedServiceNames()
175 return { OUString("com.sun.star.datatransfer.clipboard.SystemClipboard") };
178 css::uno::Reference
<css::uno::XInterface
>
179 IosSalInstance::CreateClipboard(const css::uno::Sequence
<css::uno::Any
>&)
181 return css::uno::Reference
<css::uno::XInterface
>(
182 static_cast<cppu::OWeakObject
*>(new iOSClipboard()));
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */