diff options
author | Jan Holesovsky <kendy@collabora.com> | 2015-11-03 13:20:06 +0100 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2015-11-03 13:25:23 +0100 |
commit | 8c987fababbddb6e4f81b0cd717b59b9a9ff9be0 (patch) | |
tree | 269bb73e21a8afebd8ea916d1280d8f858a571bc /desktop | |
parent | c0f37892a24b202c0a28836ed1046c90c7631e03 (diff) |
lok: Introduce LOK_CALLBACK_UNO_COMMAND_RESULT callback.
Posting of the .uno:Something commands is asynchronous. To be able to find
out when eg. .uno:Save finished, this commit introduces a callback that fires
when that happens.
To be able to receive such a notification, the appropriate postUnoCommand()
must be called with 'true' as the parameter for bNotifyWhenFinished (defaults
to 'false').
Change-Id: I254939ebc8ea5f309ae39686dcaaeddd5148b0c9
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/inc/lib/init.hxx | 2 | ||||
-rw-r--r-- | desktop/source/lib/init.cxx | 74 |
2 files changed, 71 insertions, 5 deletions
diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx index da31adc27a50..9003859e5eba 100644 --- a/desktop/inc/lib/init.hxx +++ b/desktop/inc/lib/init.hxx @@ -22,6 +22,8 @@ namespace desktop { { css::uno::Reference<css::lang::XComponent> mxComponent; std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass; + LibreOfficeKitCallback mpCallback; + void *mpCallbackData; explicit LibLODocument_Impl(const css::uno::Reference <css::lang::XComponent> &xComponent); ~LibLODocument_Impl(); diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 9999acd93fb1..8d09c3933384 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -36,6 +36,8 @@ #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/container/XNameAccess.hpp> #include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/DispatchResultEvent.hpp> +#include <com/sun/star/frame/DispatchResultState.hpp> #include <com/sun/star/frame/XStorable.hpp> #include <com/sun/star/lang/Locale.hpp> #include <com/sun/star/lang/XComponent.hpp> @@ -241,7 +243,8 @@ static void doc_postMouseEvent (LibreOfficeKitDocument* pThis, int nModifier); static void doc_postUnoCommand(LibreOfficeKitDocument* pThis, const char* pCommand, - const char* pArguments); + const char* pArguments, + bool bNotifyWhenFinished); static void doc_setTextSelection (LibreOfficeKitDocument* pThis, int nType, int nX, @@ -884,9 +887,14 @@ static void doc_initializeForRendering(LibreOfficeKitDocument* pThis) } static void doc_registerCallback(LibreOfficeKitDocument* pThis, - LibreOfficeKitCallback pCallback, - void* pData) + LibreOfficeKitCallback pCallback, + void* pData) { + LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis); + + pDocument->mpCallback = pCallback; + pDocument->mpCallbackData = pData; + if (comphelper::LibreOfficeKit::isViewCallback()) { if (SfxViewShell* pViewShell = SfxViewFrame::Current()->GetViewShell()) @@ -951,13 +959,69 @@ static void jsonToPropertyValues(const char* pJSON, uno::Sequence<beans::Propert rPropertyValues = comphelper::containerToSequence(aArguments); } -static void doc_postUnoCommand(LibreOfficeKitDocument* /*pThis*/, const char* pCommand, const char* pArguments) +/** Class to react on finishing of a dispatched command. + + This will call a LOK_COMMAND_FINISHED callback when postUnoCommand was + called with the parameter requesting the notification. + + @see LibreOfficeKitCallbackType::LOK_CALLBACK_UNO_COMMAND_RESULT. +*/ +class DispatchResultListener : public cppu::WeakImplHelper<css::frame::XDispatchResultListener> +{ + OString maCommand; ///< Command for which this is the result. + LibreOfficeKitCallback mpCallback; ///< Callback to call. + void* mpCallbackData; ///< The callback's data. + +public: + DispatchResultListener(const char* pCommand, LibreOfficeKitCallback pCallback, void* pCallbackData) + : maCommand(pCommand) + , mpCallback(pCallback) + , mpCallbackData(pCallbackData) + { + assert(mpCallback); + } + + virtual void SAL_CALL dispatchFinished(const css::frame::DispatchResultEvent& rEvent) throw(css::uno::RuntimeException, std::exception) override + { + boost::property_tree::ptree aTree; + aTree.put("commandName", maCommand.getStr()); + + if (rEvent.State != frame::DispatchResultState::DONTKNOW) + { + bool bSuccess = (rEvent.State == frame::DispatchResultState::SUCCESS); + aTree.put("success", bSuccess); + } + + // TODO UNO Any rEvent.Result -> JSON + // aTree.put("result": "..."); + + std::stringstream aStream; + boost::property_tree::write_json(aStream, aTree); + mpCallback(LOK_CALLBACK_UNO_COMMAND_RESULT, strdup(aStream.str().c_str()), mpCallbackData); + } + + virtual void SAL_CALL disposing(const css::lang::EventObject&) throw (css::uno::RuntimeException, std::exception) override {} +}; + +static void doc_postUnoCommand(LibreOfficeKitDocument* pThis, const char* pCommand, const char* pArguments, bool bNotifyWhenFinished) { OUString aCommand(pCommand, strlen(pCommand), RTL_TEXTENCODING_UTF8); uno::Sequence<beans::PropertyValue> aPropertyValues; jsonToPropertyValues(pArguments, aPropertyValues); - if (!comphelper::dispatchCommand(aCommand, aPropertyValues)) + bool bResult = false; + + LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis); + + if (bNotifyWhenFinished && pDocument->mpCallback) + { + bResult = comphelper::dispatchCommand(aCommand, aPropertyValues, + new DispatchResultListener(pCommand, pDocument->mpCallback, pDocument->mpCallbackData)); + } + else + bResult = comphelper::dispatchCommand(aCommand, aPropertyValues); + + if (!bResult) { gImpl->maLastExceptionMsg = "Failed to dispatch the .uno: command"; } |