diff options
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/Library_comphelper.mk | 1 | ||||
-rw-r--r-- | comphelper/source/misc/expandmacro.cxx | 59 |
2 files changed, 60 insertions, 0 deletions
diff --git a/comphelper/Library_comphelper.mk b/comphelper/Library_comphelper.mk index 09a68895f81f..4b303386b953 100644 --- a/comphelper/Library_comphelper.mk +++ b/comphelper/Library_comphelper.mk @@ -86,6 +86,7 @@ $(eval $(call gb_Library_add_exception_objects,comphelper,\ comphelper/source/misc/documentiologring \ comphelper/source/misc/evtlistenerhlp \ comphelper/source/misc/evtmethodhelper \ + comphelper/source/misc/expandmacro \ comphelper/source/misc/ihwrapnofilter \ comphelper/source/misc/instancelocker \ comphelper/source/misc/interaction \ diff --git a/comphelper/source/misc/expandmacro.cxx b/comphelper/source/misc/expandmacro.cxx new file mode 100644 index 000000000000..a7eae4b12548 --- /dev/null +++ b/comphelper/source/misc/expandmacro.cxx @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <comphelper/expandmacro.hxx> + +#include <com/sun/star/uno/Reference.hxx> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/util/theMacroExpander.hpp> +#include <rtl/ustring.hxx> +#include <rtl/uri.hxx> +#include <osl/file.h> +#include <comphelper/processfactory.hxx> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using ::com::sun::star::lang::XMultiServiceFactory; + +namespace comphelper +{ + rtl::OUString getExpandedFilePath(const rtl::OUString& filepath) + { + const Reference<XComponentContext> xContext( ::comphelper::getProcessComponentContext() ); + return getExpandedFilePath(filepath, xContext); + } + + rtl::OUString getExpandedFilePath(const rtl::OUString& filepath, const Reference<XComponentContext>& xContext) + { + Reference< util::XMacroExpander > xMacroExpander = util::theMacroExpander::get( xContext ); + + rtl::OUString aFilename = filepath; + + if( aFilename.startsWith( "vnd.sun.star.expand:" ) ) + { + // cut protocol + rtl::OUString aMacro( aFilename.copy( sizeof ( "vnd.sun.star.expand:" ) -1 ) ); + + // decode uric class chars + aMacro = rtl::Uri::decode( aMacro, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 ); + + // expand macro string + aFilename = xMacroExpander->expandMacros( aMacro ); + } + + if( aFilename.startsWith( "file://" ) ) + { + rtl::OUString aSysPath; + if( osl_getSystemPathFromFileURL( aFilename.pData, &aSysPath.pData ) == osl_File_E_None ) + aFilename = aSysPath; + } + + return aFilename; + } +} |