diff options
author | Katarina Behrens <Katarina.Behrens@cib.de> | 2017-02-19 23:58:52 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2017-02-28 09:50:11 +0000 |
commit | e7729f458409f7fef2b7dab752205e3b230acb65 (patch) | |
tree | 32b71b7dedfb50b887ce2dd2313bb27d273c419a /desktop | |
parent | 6603947329a7b372a173a3c60e013e532d0bc5cf (diff) |
Run macro:// via LOKit
Change-Id: I54d3c734d86d4b44e1dfe1ad7728e2f51cd26d53
Reviewed-on: https://gerrit.libreoffice.org/34538
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/qa/desktop_lib/test_desktop_lib.cxx | 15 | ||||
-rw-r--r-- | desktop/source/lib/init.cxx | 88 |
2 files changed, 103 insertions, 0 deletions
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx index 62ec69d63576..81cbecedc686 100644 --- a/desktop/qa/desktop_lib/test_desktop_lib.cxx +++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx @@ -107,6 +107,7 @@ public: void testCommentsCalc(); void testCommentsImpress(); void testCommentsCallbacksWriter(); + void testRunMacro(); CPPUNIT_TEST_SUITE(DesktopLOKTest); CPPUNIT_TEST(testGetStyles); @@ -145,6 +146,7 @@ public: CPPUNIT_TEST(testCommentsCalc); CPPUNIT_TEST(testCommentsImpress); CPPUNIT_TEST(testCommentsCallbacksWriter); + CPPUNIT_TEST(testRunMacro); CPPUNIT_TEST_SUITE_END(); uno::Reference<lang::XComponent> mxComponent; @@ -2096,6 +2098,19 @@ void DesktopLOKTest::testCommentsCallbacksWriter() comphelper::LibreOfficeKit::setActive(false); } +void DesktopLOKTest::testRunMacro() +{ + LibLibreOffice_Impl aOffice; + bool bGoodMacro, bNonExistentMacro; + + // Tools macros come pre-installed in system share/basic folder, + bGoodMacro = aOffice.m_pOfficeClass->runMacro(&aOffice, OString("macro:///Tools.Debug.ActivateReadOnlyFlag()").getStr()); + CPPUNIT_ASSERT(bGoodMacro); + + bNonExistentMacro = aOffice.m_pOfficeClass->runMacro(&aOffice, OString("macro:///I.Am.Not(There)").getStr()); + CPPUNIT_ASSERT(!bNonExistentMacro); +} + CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 61fbc61889a3..56d70b261849 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -40,6 +40,8 @@ #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/XDispatchProvider.hpp> +#include <com/sun/star/frame/XSynchronousDispatch.hpp> #include <com/sun/star/frame/XStorable.hpp> #include <com/sun/star/lang/Locale.hpp> #include <com/sun/star/lang/XComponent.hpp> @@ -1054,6 +1056,7 @@ static void doc_destroy(LibreOfficeKitDocument *pThis) static void lo_destroy (LibreOfficeKit* pThis); static int lo_initialize (LibreOfficeKit* pThis, const char* pInstallPath, const char* pUserProfilePath); static LibreOfficeKitDocument* lo_documentLoad (LibreOfficeKit* pThis, const char* pURL); +static bool lo_runMacro (LibreOfficeKit* pThis, const char* pURL); static char * lo_getError (LibreOfficeKit* pThis); static void lo_freeError (char* pFree); static LibreOfficeKitDocument* lo_documentLoadWithOptions (LibreOfficeKit* pThis, @@ -1082,6 +1085,7 @@ LibLibreOffice_Impl::LibLibreOffice_Impl() m_pOfficeClass->destroy = lo_destroy; m_pOfficeClass->documentLoad = lo_documentLoad; + m_pOfficeClass->runMacro = lo_runMacro; m_pOfficeClass->getError = lo_getError; m_pOfficeClass->freeError = lo_freeError; m_pOfficeClass->documentLoadWithOptions = lo_documentLoadWithOptions; @@ -1211,6 +1215,90 @@ static LibreOfficeKitDocument* lo_documentLoadWithOptions(LibreOfficeKit* pThis, return nullptr; } +static bool lo_runMacro( LibreOfficeKit* pThis, const char *pURL) +{ + SolarMutexGuard aGuard; + + LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis); + + OUString sURL( pURL, strlen(pURL), RTL_TEXTENCODING_UTF8 ); + if (sURL.isEmpty()) + { + pLib->maLastExceptionMsg = "Macro to run was not provided."; + SAL_INFO("lok", "Macro URL is empty"); + return false; + } + + if (!sURL.startsWith("macro://")) + { + pLib->maLastExceptionMsg = "This doesn't look like macro URL"; + SAL_INFO("lok", "Macro URL is invalid"); + return false; + } + + pLib->maLastExceptionMsg.clear(); + + if (!xContext.is()) + { + pLib->maLastExceptionMsg = "ComponentContext is not available"; + SAL_INFO("lok", "ComponentContext is not available"); + return false; + } + + util::URL aURL; + aURL.Complete = sURL; + + uno::Reference < util::XURLTransformer > xParser( util::URLTransformer::create( xContext ) ); + + if( xParser.is() ) + xParser->parseStrict( aURL ); + + uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(xContext); + + if (!xComponentLoader.is()) + { + pLib->maLastExceptionMsg = "ComponentLoader is not available"; + SAL_INFO("lok", "ComponentLoader is not available"); + return false; + } + + xFactory = xContext->getServiceManager(); + + if (xFactory.is()) + { + uno::Reference<frame::XDispatchProvider> xDP; + xSFactory.set(xFactory, uno::UNO_QUERY_THROW); + xDP.set( xSFactory->createInstance("com.sun.star.comp.sfx2.SfxMacroLoader"), uno::UNO_QUERY ); + uno::Reference<frame::XDispatch> xD = xDP->queryDispatch( aURL, OUString(), 0); + + if (!xD.is()) + { + pLib->maLastExceptionMsg = "Macro loader is not available"; + SAL_INFO("lok", "Macro loader is not available"); + return false; + } + + uno::Reference < frame::XSynchronousDispatch > xSyncDisp( xD, uno::UNO_QUERY_THROW ); + uno::Sequence<css::beans::PropertyValue> aEmpty; + css::beans::PropertyValue aErr; + uno::Any aRet; + + aRet = xSyncDisp->dispatchWithReturnValue( aURL, aEmpty ); + aRet >>= aErr; + + if (aErr.Name == "ErrorCode") + { + pLib->maLastExceptionMsg = "An error occured running macro"; + SAL_INFO("lok", "Macro execution terminated with errors"); + return false; + } + + return true; + } + + return false; +} + static void lo_registerCallback (LibreOfficeKit* pThis, LibreOfficeKitCallback pCallback, void* pData) |