2013-03-05 16:40:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2013-03-05 13:16:36 +00:00
|
|
|
/*
|
|
|
|
* 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 <stdio.h>
|
2013-07-26 18:21:45 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "liblibreoffice.hxx"
|
2013-03-05 13:16:36 +00:00
|
|
|
|
|
|
|
#include <tools/errinf.hxx>
|
|
|
|
#include <osl/file.hxx>
|
2013-03-05 14:29:26 +00:00
|
|
|
#include <osl/process.h>
|
2013-03-05 13:16:36 +00:00
|
|
|
#include <rtl/strbuf.hxx>
|
|
|
|
#include <rtl/bootstrap.hxx>
|
|
|
|
#include <cppuhelper/bootstrap.hxx>
|
|
|
|
#include <comphelper/processfactory.hxx>
|
|
|
|
|
2013-03-05 14:29:26 +00:00
|
|
|
#include <com/sun/star/beans/XPropertySet.hpp>
|
2013-07-26 18:21:45 +01:00
|
|
|
#include <com/sun/star/frame/XModel.hpp>
|
2013-03-05 14:29:26 +00:00
|
|
|
#include <com/sun/star/frame/Desktop.hpp>
|
2013-07-26 18:21:45 +01:00
|
|
|
#include <com/sun/star/frame/XStorable.hpp>
|
2013-03-05 13:16:36 +00:00
|
|
|
#include <com/sun/star/lang/Locale.hpp>
|
|
|
|
#include <com/sun/star/lang/XComponent.hpp>
|
|
|
|
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
|
|
|
|
#include <com/sun/star/ucb/XContentProvider.hpp>
|
|
|
|
#include <com/sun/star/ucb/XUniversalContentBroker.hpp>
|
|
|
|
|
|
|
|
#include <vcl/svapp.hxx>
|
|
|
|
#include <tools/resmgr.hxx>
|
|
|
|
#include <vcl/graphicfilter.hxx>
|
|
|
|
#include <unotools/syslocaleoptions.hxx>
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
2013-07-26 18:21:45 +01:00
|
|
|
class LibLODocument_Impl;
|
|
|
|
class LibLibreOffice_Impl;
|
|
|
|
|
|
|
|
static LibLibreOffice_Impl *gImpl = NULL;
|
|
|
|
|
|
|
|
class LibLODocument_Impl : public LODocument
|
|
|
|
{
|
|
|
|
uno::Reference < css::lang::XComponent > mxComponent;
|
|
|
|
public:
|
|
|
|
LibLODocument_Impl( const uno::Reference < css::lang::XComponent > &xComponent )
|
|
|
|
: mxComponent( xComponent )
|
|
|
|
{ }
|
|
|
|
virtual bool saveAs (const char *url);
|
|
|
|
};
|
|
|
|
|
|
|
|
class LibLibreOffice_Impl : public LibLibreOffice
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
rtl::OUString maLastExceptionMsg;
|
|
|
|
|
|
|
|
virtual bool initialize (const char *installPath);
|
|
|
|
|
|
|
|
virtual LODocument *documentLoad (const char *url);
|
|
|
|
|
|
|
|
virtual char *getError();
|
|
|
|
|
|
|
|
virtual ~LibLibreOffice_Impl ();
|
|
|
|
};
|
|
|
|
|
2013-03-05 13:16:36 +00:00
|
|
|
// Wonder global state ...
|
|
|
|
static uno::Reference<css::uno::XComponentContext> xContext;
|
|
|
|
static uno::Reference<css::lang::XMultiServiceFactory> xSFactory;
|
|
|
|
static uno::Reference<css::lang::XMultiComponentFactory> xFactory;
|
|
|
|
|
2013-07-26 18:21:45 +01:00
|
|
|
static OUString getUString( const char *str )
|
|
|
|
{
|
|
|
|
if( !str )
|
|
|
|
return OUString( "" );
|
|
|
|
return OStringToOUString( OString( str, strlen (str) ),
|
|
|
|
RTL_TEXTENCODING_UTF8 );
|
|
|
|
}
|
|
|
|
|
2013-03-05 16:19:58 +00:00
|
|
|
LODocument *
|
|
|
|
LibLibreOffice_Impl::documentLoad( const char *docUrl )
|
2013-03-05 13:16:36 +00:00
|
|
|
{
|
2013-07-26 18:21:45 +01:00
|
|
|
OUString sUrl = getUString( docUrl );
|
2013-03-05 14:29:26 +00:00
|
|
|
OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl;
|
|
|
|
|
|
|
|
uno::Reference < css::frame::XDesktop2 > xComponentLoader =
|
|
|
|
css::frame::Desktop::create(xContext);
|
|
|
|
|
|
|
|
osl_getProcessWorkingDir(&sWorkingDir.pData);
|
2013-07-26 18:21:45 +01:00
|
|
|
osl::FileBase::getFileURLFromSystemPath( sUrl, sDocPathUrl );
|
2013-03-05 14:29:26 +00:00
|
|
|
osl::FileBase::getAbsoluteFileURL(sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
|
|
|
|
|
2013-07-26 18:21:45 +01:00
|
|
|
maLastExceptionMsg = "";
|
|
|
|
try {
|
|
|
|
uno::Reference < css::lang::XComponent > xComponent =
|
|
|
|
xComponentLoader->loadComponentFromURL(
|
|
|
|
sAbsoluteDocUrl, OUString("_blank"), 0,
|
|
|
|
uno::Sequence < css::beans::PropertyValue >());
|
|
|
|
if( xComponentLoader.is() )
|
|
|
|
return new LibLODocument_Impl( xComponent );
|
|
|
|
else
|
|
|
|
maLastExceptionMsg = "unknown load failure";
|
|
|
|
} catch (const uno::Exception &ex) {
|
|
|
|
maLastExceptionMsg = ex.Message;
|
|
|
|
}
|
2013-03-05 13:16:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-26 18:21:45 +01:00
|
|
|
bool LibLODocument_Impl::saveAs (const char *url)
|
2013-03-05 13:16:36 +00:00
|
|
|
{
|
2013-07-26 18:21:45 +01:00
|
|
|
OUString sURL = getUString( url );
|
|
|
|
|
|
|
|
try {
|
|
|
|
uno::Reference< frame::XModel > xDocument( mxComponent, uno::UNO_QUERY_THROW );
|
|
|
|
uno::Sequence< beans::PropertyValue > aSeq = xDocument->getArgs();
|
|
|
|
|
|
|
|
OUString aFilterName;
|
|
|
|
for( sal_Int32 i = 0; i < aSeq.getLength(); ++i )
|
|
|
|
{
|
|
|
|
if( aSeq[i].Name == "FilterName" )
|
|
|
|
aSeq[i].Value >>= aFilterName;
|
|
|
|
}
|
|
|
|
aSeq.realloc(2);
|
|
|
|
aSeq[0].Name = "Overwrite";
|
|
|
|
aSeq[0].Value <<= sal_True;
|
|
|
|
aSeq[1].Name = "FilterName";
|
|
|
|
aSeq[1].Value <<= aFilterName;
|
|
|
|
|
|
|
|
uno::Reference< frame::XStorable > xStorable( mxComponent, uno::UNO_QUERY_THROW );
|
|
|
|
xStorable->storeAsURL( sURL, aSeq );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (const uno::Exception &ex) {
|
|
|
|
gImpl->maLastExceptionMsg = "exception " + ex.Message;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *LibLibreOffice_Impl::getError()
|
|
|
|
{
|
|
|
|
OString aStr = rtl::OUStringToOString( maLastExceptionMsg, RTL_TEXTENCODING_UTF8 );
|
|
|
|
return strndup( aStr.getStr(), aStr.getLength() );
|
2013-03-05 13:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
force_c_locale( void )
|
|
|
|
{
|
|
|
|
// force locale (and resource files loaded) to en-US
|
2013-03-29 14:07:33 +01:00
|
|
|
OUString aLangISO( "en-US" );
|
|
|
|
LanguageTag aLocale( aLangISO );
|
2013-03-05 13:16:36 +00:00
|
|
|
ResMgr::SetDefaultLocale( aLocale );
|
|
|
|
SvtSysLocaleOptions aLocalOptions;
|
|
|
|
aLocalOptions.SetLocaleConfigString( aLangISO );
|
|
|
|
aLocalOptions.SetUILocaleConfigString( aLangISO );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
aBasicErrorFunc( const OUString &rErr, const OUString &rAction )
|
|
|
|
{
|
|
|
|
OStringBuffer aErr( "Unexpected dialog: " );
|
|
|
|
aErr.append( OUStringToOString( rAction, RTL_TEXTENCODING_ASCII_US ) );
|
|
|
|
aErr.append( " Error: " );
|
|
|
|
aErr.append( OUStringToOString( rErr, RTL_TEXTENCODING_ASCII_US ) );
|
|
|
|
fprintf( stderr, "Unexpected basic error dialog '%s'\n", aErr.getStr() );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-07 12:06:47 +02:00
|
|
|
initialize_uno( const OUString &aAppURL )
|
2013-03-05 13:16:36 +00:00
|
|
|
{
|
2013-03-05 22:21:57 +00:00
|
|
|
rtl::Bootstrap::setIniFilename( aAppURL + "/fundamentalrc" );
|
|
|
|
|
|
|
|
rtl::Bootstrap::set( "CONFIGURATION_LAYERS",
|
|
|
|
"xcsxcu:${BRAND_BASE_DIR}/share/registry "
|
|
|
|
"res:${BRAND_BASE_DIR}/share/registry "
|
|
|
|
// "bundledext:${${BRAND_BASE_DIR}/program/unorc:BUNDLED_EXTENSIONS_USER}/registry/com.sun.star.comp.deployment.configuration.PackageRegistryBackend/configmgr.ini " );
|
|
|
|
// "sharedext:${${BRAND_BASE_DIR}/program/unorc:SHARED_EXTENSIONS_USER}/registry/com.sun.star.comp.deployment.configuration.PackageRegistryBackend/configmgr.ini "
|
|
|
|
// "userext:${${BRAND_BASE_DIR}/program/unorc:UNO_USER_PACKAGES_CACHE}/registry/com.sun.star.comp.deployment.configuration.PackageRegistryBackend/configmgr.ini "
|
|
|
|
// "user:${$BRAND_BASE_DIR/program/bootstraprc:UserInstallation}/user/registrymodifications.xcu"
|
|
|
|
);
|
|
|
|
|
2013-03-05 16:40:01 +00:00
|
|
|
xContext = cppu::defaultBootstrap_InitialComponentContext();
|
|
|
|
fprintf( stderr, "Uno initialized %d\n", xContext.is() );
|
|
|
|
xFactory = xContext->getServiceManager();
|
|
|
|
xSFactory = uno::Reference<lang::XMultiServiceFactory>(xFactory, uno::UNO_QUERY_THROW);
|
|
|
|
comphelper::setProcessServiceFactory(xSFactory);
|
|
|
|
|
2013-03-05 13:16:36 +00:00
|
|
|
// set UserInstallation to user profile dir in test/user-template
|
2013-03-05 22:21:57 +00:00
|
|
|
// rtl::Bootstrap aDefaultVars;
|
2013-04-07 12:06:47 +02:00
|
|
|
// aDefaultVars.set(OUString("UserInstallation"), aAppURL + "../registry" );
|
2013-03-05 22:21:57 +00:00
|
|
|
// configmgr setup ?
|
2013-03-05 13:16:36 +00:00
|
|
|
}
|
|
|
|
|
2013-03-05 16:40:01 +00:00
|
|
|
bool
|
2013-03-05 16:19:58 +00:00
|
|
|
LibLibreOffice_Impl::initialize( const char *app_path )
|
2013-03-05 13:16:36 +00:00
|
|
|
{
|
|
|
|
static bool bInitialized = false;
|
|
|
|
if( bInitialized )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( !app_path )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
OUString aAppPath( app_path, strlen( app_path ), RTL_TEXTENCODING_UTF8 );
|
|
|
|
OUString aAppURL;
|
|
|
|
if( osl::FileBase::getFileURLFromSystemPath( aAppPath, aAppURL ) !=
|
|
|
|
osl::FileBase::E_None )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
2013-03-05 22:21:57 +00:00
|
|
|
initialize_uno( aAppURL );
|
2013-03-05 13:16:36 +00:00
|
|
|
force_c_locale();
|
2013-03-05 22:21:57 +00:00
|
|
|
|
|
|
|
// Force headless
|
|
|
|
rtl::Bootstrap::set( "SAL_USE_VCLPLUGIN", "svp" );
|
2013-03-05 13:16:36 +00:00
|
|
|
InitVCL();
|
2013-03-05 22:21:57 +00:00
|
|
|
Application::EnableHeadlessMode(true);
|
2013-03-05 13:16:36 +00:00
|
|
|
|
|
|
|
ErrorHandler::RegisterDisplay( aBasicErrorFunc );
|
|
|
|
|
2013-03-05 22:21:57 +00:00
|
|
|
fprintf( stderr, "initialized\n" );
|
2013-03-05 13:16:36 +00:00
|
|
|
bInitialized = true;
|
|
|
|
} catch (css::uno::Exception & e) {
|
|
|
|
fprintf( stderr, "bootstrapping exception '%s'\n",
|
|
|
|
OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
|
|
|
|
}
|
|
|
|
return bInitialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
SAL_DLLPUBLIC_EXPORT LibLibreOffice *liblibreoffice_hook(void);
|
|
|
|
}
|
|
|
|
|
|
|
|
LibLibreOffice *liblibreoffice_hook(void)
|
|
|
|
{
|
2013-07-26 18:21:45 +01:00
|
|
|
if( !gImpl )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "create libreoffice object\n" );
|
|
|
|
gImpl = new LibLibreOffice_Impl();
|
|
|
|
}
|
|
|
|
return gImpl;
|
2013-03-05 16:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LibLibreOffice_Impl::~LibLibreOffice_Impl ()
|
|
|
|
{
|
2013-07-26 18:21:45 +01:00
|
|
|
gImpl = NULL;
|
2013-03-05 13:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|