diff options
author | Michael Meeks <michael.meeks@collabora.com> | 2019-07-20 10:43:39 +0100 |
---|---|---|
committer | Michael Meeks <michael.meeks@collabora.com> | 2019-08-02 11:41:48 -0400 |
commit | f59e986d0dc04a2d62cc1f236acedfcf727449ce (patch) | |
tree | d4739978f593ba969cac9467f1d87e5c1a109186 /desktop/source/lib/lokclipboard.cxx | |
parent | 482419ffeab01642db00cd16a2b46a5790a3323e (diff) |
lok: clipboard: per view clipboard creation.
A bit brutal, but the mess around clipboard instantiation is awful.
Change-Id: I62d6af8bf6813e6bab81123417ea8bfb28394e29
Diffstat (limited to 'desktop/source/lib/lokclipboard.cxx')
-rw-r--r-- | desktop/source/lib/lokclipboard.cxx | 98 |
1 files changed, 89 insertions, 9 deletions
diff --git a/desktop/source/lib/lokclipboard.cxx b/desktop/source/lib/lokclipboard.cxx index 015690bd9bc4..74e5131f435d 100644 --- a/desktop/source/lib/lokclipboard.cxx +++ b/desktop/source/lib/lokclipboard.cxx @@ -8,23 +8,103 @@ */ #include "lokclipboard.hxx" -#include <comphelper/sequence.hxx> +#include <sfx2/lokhelper.hxx> -using namespace com::sun::star; +using namespace css; +using namespace css::uno; -uno::Reference<datatransfer::XTransferable> SAL_CALL LOKClipboard::getContents() +osl::Mutex LOKClipboardFactory::gMutex; +std::unordered_map<int, rtl::Reference<LOKClipboard>> LOKClipboardFactory::gClipboards; + +rtl::Reference<LOKClipboard> LOKClipboardFactory::getClipboardForCurView() +{ + int nViewId = SfxLokHelper::getView(); // currently active. + + osl::MutexGuard aGuard(gMutex); + + auto it = gClipboards.find(nViewId); + if (it != gClipboards.end()) + return it->second; + rtl::Reference<LOKClipboard> xClip(new LOKClipboard()); + gClipboards[nViewId] = xClip; + SAL_INFO("lok", "Created clipboard for view " << nViewId << " as " << xClip.get()); + return xClip; +} + +uno::Reference<uno::XInterface> + SAL_CALL LOKClipboardFactory::createInstanceWithArguments(const Sequence<Any>& /* rArgs */) +{ + return uno::Reference<uno::XInterface>( + static_cast<cppu::OWeakObject*>(getClipboardForCurView().get())); +} + +LOKClipboard::LOKClipboard() + : cppu::WeakComponentImplHelper<css::datatransfer::clipboard::XSystemClipboard, + css::lang::XServiceInfo>(m_aMutex) +{ +} + +Sequence<OUString> LOKClipboard::getSupportedServiceNames_static() +{ + Sequence<OUString> aRet{ "com.sun.star.datatransfer.clipboard.SystemClipboard" }; + return aRet; +} + +OUString LOKClipboard::getImplementationName() +{ + return OUString("com.sun.star.datatransfer.LOKClipboard"); +} + +Sequence<OUString> LOKClipboard::getSupportedServiceNames() +{ + return getSupportedServiceNames_static(); +} + +sal_Bool LOKClipboard::supportsService(const OUString& ServiceName) +{ + return cppu::supportsService(this, ServiceName); +} + +Reference<css::datatransfer::XTransferable> LOKClipboard::getContents() { return m_xTransferable; } + +void LOKClipboard::setContents( + const Reference<css::datatransfer::XTransferable>& xTrans, + const Reference<css::datatransfer::clipboard::XClipboardOwner>& xClipboardOwner) { - return m_xTransferable; + osl::ClearableMutexGuard aGuard(m_aMutex); + Reference<datatransfer::clipboard::XClipboardOwner> xOldOwner(m_aOwner); + Reference<datatransfer::XTransferable> xOldContents(m_xTransferable); + m_xTransferable = xTrans; + m_aOwner = xClipboardOwner; + + std::vector<Reference<datatransfer::clipboard::XClipboardListener>> aListeners(m_aListeners); + datatransfer::clipboard::ClipboardEvent aEv; + aEv.Contents = m_xTransferable; + + aGuard.clear(); + + if (xOldOwner.is() && xOldOwner != xClipboardOwner) + xOldOwner->lostOwnership(this, xOldContents); + for (auto const& listener : aListeners) + { + listener->changedContents(aEv); + } } -void SAL_CALL LOKClipboard::setContents( - const uno::Reference<datatransfer::XTransferable>& xTransferable, - const uno::Reference<datatransfer::clipboard::XClipboardOwner>& /*xClipboardOwner*/) +void LOKClipboard::addClipboardListener( + const Reference<datatransfer::clipboard::XClipboardListener>& listener) { - m_xTransferable = xTransferable; + osl::ClearableMutexGuard aGuard(m_aMutex); + m_aListeners.push_back(listener); } -OUString SAL_CALL LOKClipboard::getName() { return OUString(); } +void LOKClipboard::removeClipboardListener( + const Reference<datatransfer::clipboard::XClipboardListener>& listener) +{ + osl::ClearableMutexGuard aGuard(m_aMutex); + m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), listener), + m_aListeners.end()); +} LOKTransferable::LOKTransferable(const char* pMimeType, const char* pData, std::size_t nSize) : m_aMimeType(OUString::fromUtf8(pMimeType)) |