LCOV - code coverage report
Current view: top level - desktop/source/lib - init.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 4 156 2.6 %
Date: 2014-04-14 Functions: 2 19 10.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include <config_folders.h>
      11             : 
      12             : #include <stdio.h>
      13             : #include <string.h>
      14             : #include <stdlib.h>
      15             : 
      16             : #include "liblibreoffice.h"
      17             : 
      18             : #include <tools/errinf.hxx>
      19             : #include <osl/file.hxx>
      20             : #include <osl/process.h>
      21             : #include <rtl/strbuf.hxx>
      22             : #include <rtl/bootstrap.hxx>
      23             : #include <cppuhelper/bootstrap.hxx>
      24             : #include <comphelper/processfactory.hxx>
      25             : 
      26             : #include <com/sun/star/beans/XPropertySet.hpp>
      27             : #include <com/sun/star/frame/XModel.hpp>
      28             : #include <com/sun/star/frame/Desktop.hpp>
      29             : #include <com/sun/star/frame/XStorable.hpp>
      30             : #include <com/sun/star/lang/Locale.hpp>
      31             : #include <com/sun/star/lang/XComponent.hpp>
      32             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      33             : #include <com/sun/star/ucb/XContentProvider.hpp>
      34             : #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
      35             : 
      36             : #include <vcl/svapp.hxx>
      37             : #include <tools/resmgr.hxx>
      38             : #include <vcl/graphicfilter.hxx>
      39             : #include <unotools/syslocaleoptions.hxx>
      40             : #include <unotools/mediadescriptor.hxx>
      41             : 
      42             : using namespace css;
      43             : using namespace utl;
      44             : 
      45             : struct LibLODocument_Impl;
      46             : struct LibLibreOffice_Impl;
      47             : 
      48             : static LibLibreOffice_Impl *gImpl = NULL;
      49             : 
      50             : typedef struct
      51             : {
      52             :     const char *extn;
      53             :     const char *filterName;
      54             : } ExtensionMap;
      55             : 
      56             : static const ExtensionMap aWriterExtensionMap[] =
      57             : {
      58             :     { "doc",   "MS Word 97" },
      59             :     { "docx",  "MS Word 2007 XML" },
      60             :     { "fodt",  "OpenDocument Text Flat XML" },
      61             :     { "html",  "HTML (StarWriter)" },
      62             :     { "odt",   "writer8" },
      63             :     { "ott",   "writer8_template" },
      64             :     { "pdf",   "writer_pdf_Export" },
      65             :     { "txt",   "Text" },
      66             :     { "xhtml", "XHTML Writer File" },
      67             :     { NULL, NULL }
      68             : };
      69             : 
      70             : static const ExtensionMap aCalcExtensionMap[] =
      71             : {
      72             :     { "csv",   "Text - txt - csv (StarCalc)" },
      73             :     { "fods",  "OpenDocument Spreadsheet Flat XML" },
      74             :     { "html",  "HTML (StarCalc)" },
      75             :     { "ods",   "calc8" },
      76             :     { "ots",   "calc8_template" },
      77             :     { "pdf",   "calc_pdf_Export" },
      78             :     { "xhtml", "XHTML Calc File" },
      79             :     { "xls",   "MS Excel 97" },
      80             :     { "xlsx",  "Calc MS Excel 2007 XML" },
      81             :     { NULL, NULL }
      82             : };
      83             : 
      84             : static const ExtensionMap aImpressExtensionMap[] =
      85             : {
      86             :     { "fodp",  "OpenDocument Presentation Flat XML" },
      87             :     { "html",  "impress_html_Export" },
      88             :     { "odg",   "impress8_draw" },
      89             :     { "odp",   "impress8" },
      90             :     { "otp",   "impress8_template" },
      91             :     { "pdf",   "impress_pdf_Export" },
      92             :     { "potm",  "Impress MS PowerPoint 2007 XML Template" },
      93             :     { "pot",   "MS PowerPoint 97 Vorlage" },
      94             :     { "pptx",  "Impress MS PowerPoint 2007 XML" },
      95             :     { "pps",   "MS PowerPoint 97 Autoplay" },
      96             :     { "ppt",   "MS PowerPoint 97" },
      97             :     { "svg",   "impress_svg_Export" },
      98             :     { "swf",   "impress_flash_Export" },
      99             :     { "xhtml", "XHTML Impress File" },
     100             :     { NULL, NULL }
     101             : };
     102             : 
     103           0 : static OUString getUString(const char* pString)
     104             : {
     105           0 :     if (pString == NULL)
     106           0 :         return OUString();
     107             : 
     108           0 :     OString sString(pString, strlen(pString));
     109           0 :     return OStringToOUString(sString, RTL_TEXTENCODING_UTF8);
     110             : }
     111             : 
     112             : // Try to convert a relative URL to an absolute one
     113           0 : static OUString getAbsoluteURL(const char* pURL)
     114             : {
     115           0 :     OUString aURL( getUString( pURL ) );
     116           0 :     OUString sAbsoluteDocUrl, sWorkingDir, sDocPathUrl;
     117             : 
     118             :     // FIXME: this would appear to kill non-file URLs.
     119           0 :     osl_getProcessWorkingDir(&sWorkingDir.pData);
     120           0 :     osl::FileBase::getFileURLFromSystemPath( aURL, sDocPathUrl );
     121           0 :     osl::FileBase::getAbsoluteFileURL(sWorkingDir, sDocPathUrl, sAbsoluteDocUrl);
     122             : 
     123           0 :     return sAbsoluteDocUrl;
     124             : }
     125             : 
     126             : extern "C"
     127             : {
     128             : 
     129             : SAL_DLLPUBLIC_EXPORT LibreOffice *liblibreoffice_hook(void);
     130             : 
     131             : static void doc_destroy(LibreOfficeDocument* pThis);
     132             : static int  doc_saveAs(LibreOfficeDocument* pThis, const char* pUrl, const char* pFormat);
     133             : static int  doc_saveAsWithOptions(LibreOfficeDocument* pThis, const char* pUrl, const char* pFormat, const char* pFilterOptions);
     134             : 
     135           0 : struct LibLODocument_Impl : public _LibreOfficeDocument
     136             : {
     137             :     uno::Reference<css::lang::XComponent> mxComponent;
     138             : 
     139           0 :     LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent) :
     140           0 :         mxComponent( xComponent )
     141             :     {
     142           0 :         nSize = sizeof(LibreOffice);
     143             : 
     144           0 :         destroy = doc_destroy;
     145           0 :         saveAs = doc_saveAs;
     146           0 :         saveAsWithOptions = doc_saveAsWithOptions;
     147           0 :     }
     148             : };
     149             : 
     150           0 : static void doc_destroy(LibreOfficeDocument *pThis)
     151             : {
     152           0 :     LibLODocument_Impl *pDocument = static_cast<LibLODocument_Impl*>(pThis);
     153           0 :     delete pDocument;
     154           0 : }
     155             : 
     156             : static void                 lo_destroy       (LibreOffice* pThis);
     157             : static int                  lo_initialize    (LibreOffice* pThis, const char* pInstallPath);
     158             : static LibreOfficeDocument* lo_documentLoad  (LibreOffice* pThis, const char* pURL);
     159             : static char *               lo_getError      (LibreOffice* pThis);
     160             : 
     161           0 : struct LibLibreOffice_Impl : public _LibreOffice
     162             : {
     163             :     OUString maLastExceptionMsg;
     164             : 
     165           0 :     LibLibreOffice_Impl()
     166           0 :     {
     167           0 :         nSize = sizeof(LibreOfficeDocument);
     168             : 
     169           0 :         destroy = lo_destroy;
     170           0 :         initialize = lo_initialize;
     171           0 :         documentLoad = lo_documentLoad;
     172           0 :         getError = lo_getError;
     173           0 :     }
     174             : };
     175             : 
     176             : // Wonder global state ...
     177           1 : static uno::Reference<css::uno::XComponentContext> xContext;
     178           1 : static uno::Reference<css::lang::XMultiServiceFactory> xSFactory;
     179           1 : static uno::Reference<css::lang::XMultiComponentFactory> xFactory;
     180             : 
     181           0 : static LibreOfficeDocument* lo_documentLoad(LibreOffice* pThis, const char* pURL)
     182             : {
     183           0 :     LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
     184             : 
     185           0 :     OUString aURL = getAbsoluteURL(pURL);
     186             : 
     187           0 :     uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(xContext);
     188             : 
     189           0 :     pLib->maLastExceptionMsg = "";
     190             : 
     191             :     try
     192             :     {
     193           0 :         uno::Reference<lang::XComponent> xComponent;
     194           0 :         xComponent = xComponentLoader->loadComponentFromURL(
     195             :                                             aURL, OUString("_blank"), 0,
     196           0 :                                             uno::Sequence<css::beans::PropertyValue>());
     197             : 
     198           0 :         if (xComponentLoader.is())
     199           0 :             return new LibLODocument_Impl(xComponent);
     200             :         else
     201           0 :             pLib->maLastExceptionMsg = "unknown load failure";
     202             : 
     203             :     }
     204           0 :     catch (const uno::Exception& exception)
     205             :     {
     206           0 :         pLib->maLastExceptionMsg = exception.Message;
     207             :     }
     208             : 
     209           0 :     return NULL;
     210             : }
     211             : 
     212           0 : static int doc_saveAs(LibreOfficeDocument* pThis, const char* sUrl, const char* pFormat)
     213             : {
     214           0 :     return doc_saveAsWithOptions(pThis, sUrl, pFormat, NULL);
     215             : }
     216             : 
     217           0 : static int doc_saveAsWithOptions(LibreOfficeDocument* pThis, const char* sUrl, const char* pFormat, const char* pFilterOptions)
     218             : {
     219           0 :     LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
     220             : 
     221           0 :     OUString sFormat = getUString(pFormat);
     222           0 :     OUString aURL = getAbsoluteURL(sUrl);
     223             : 
     224             :     try
     225             :     {
     226           0 :         uno::Reference<frame::XModel> xDocument(pDocument->mxComponent, uno::UNO_QUERY_THROW);
     227           0 :         uno::Sequence<beans::PropertyValue> aSequence = xDocument->getArgs();
     228             : 
     229           0 :         MediaDescriptor aMediaDescriptor(aSequence);
     230           0 :         OUString sPropertyName = MediaDescriptor::PROP_DOCUMENTSERVICE();
     231           0 :         OUString aDocumentService = aMediaDescriptor.getUnpackedValueOrDefault(sPropertyName, OUString());
     232             : 
     233           0 :         if (aDocumentService.isEmpty())
     234             :         {
     235           0 :             gImpl->maLastExceptionMsg = "Unknown document type";
     236           0 :             return false;
     237             :         }
     238             : 
     239             :         const ExtensionMap* pMap;
     240             : 
     241           0 :         if( aDocumentService == "com.sun.star.sheet.SpreadsheetDocument" )
     242           0 :             pMap = (const ExtensionMap *)aCalcExtensionMap;
     243           0 :         else if( aDocumentService == "com.sun.star.presentation.PresentationDocument" )
     244           0 :             pMap = (const ExtensionMap *)aImpressExtensionMap;
     245             :         else // for the sake of argument only writer documents ...
     246           0 :             pMap = (const ExtensionMap *)aWriterExtensionMap;
     247             : 
     248           0 :         if (pFormat == NULL)
     249             :         {
     250             :             // sniff from the extension
     251           0 :             sal_Int32 idx = aURL.lastIndexOf(".");
     252           0 :             if( idx > 0 )
     253             :             {
     254           0 :                 sFormat = aURL.copy( idx + 1 );
     255             :             }
     256             :             else
     257             :             {
     258           0 :                 gImpl->maLastExceptionMsg = "input filename without a suffix";
     259           0 :                 return false;
     260             :             }
     261             :         }
     262             : 
     263           0 :         OUString aFilterName;
     264           0 :         for (sal_Int32 i = 0; pMap[i].extn; ++i)
     265             :         {
     266           0 :             if (sFormat.equalsIgnoreAsciiCaseAscii(pMap[i].extn))
     267             :             {
     268           0 :                 aFilterName = getUString(pMap[i].filterName);
     269           0 :                 break;
     270             :             }
     271             :         }
     272           0 :         if (aFilterName.isEmpty())
     273             :         {
     274           0 :             gImpl->maLastExceptionMsg = "no output filter found for provided suffix";
     275           0 :             return false;
     276             :         }
     277             : 
     278           0 :         OUString aFilterOptions = getUString(pFilterOptions);
     279             : 
     280           0 :         MediaDescriptor aSaveMediaDescriptor;
     281           0 :         aSaveMediaDescriptor["Overwrite"] <<= sal_True;
     282           0 :         aSaveMediaDescriptor["FilterName"] <<= aFilterName;
     283           0 :         aSaveMediaDescriptor[MediaDescriptor::PROP_FILTEROPTIONS()] <<= aFilterOptions;
     284             : 
     285           0 :         uno::Reference<frame::XStorable> xStorable(pDocument->mxComponent, uno::UNO_QUERY_THROW);
     286           0 :         xStorable->storeToURL(aURL, aSaveMediaDescriptor.getAsConstPropertyValueList());
     287             : 
     288           0 :         return true;
     289             :     }
     290           0 :     catch (const uno::Exception& exception)
     291             :     {
     292           0 :         gImpl->maLastExceptionMsg = "exception: " + exception.Message;
     293             :     }
     294           0 :     return false;
     295             : }
     296             : 
     297           0 : static char* lo_getError (LibreOffice *pThis)
     298             : {
     299           0 :     LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
     300           0 :     OString aString = OUStringToOString(pLib->maLastExceptionMsg, RTL_TEXTENCODING_UTF8);
     301           0 :     char* pMemory = (char*) malloc(aString.getLength() + 1);
     302           0 :     strcpy(pMemory, aString.getStr());
     303           0 :     return pMemory;
     304             : }
     305             : 
     306           0 : static void force_c_locale(void)
     307             : {
     308             :     // force locale (and resource files loaded) to en-US
     309           0 :     OUString aLangISO("en-US");
     310           0 :     LanguageTag aLocale(aLangISO);
     311           0 :     ResMgr::SetDefaultLocale(aLocale);
     312           0 :     SvtSysLocaleOptions aLocalOptions;
     313           0 :     aLocalOptions.SetLocaleConfigString(aLangISO);
     314           0 :     aLocalOptions.SetUILocaleConfigString(aLangISO);
     315           0 : }
     316             : 
     317           0 : static void aBasicErrorFunc(const OUString& rError, const OUString& rAction)
     318             : {
     319           0 :     OStringBuffer aBuffer("Unexpected dialog: ");
     320           0 :     aBuffer.append(OUStringToOString(rAction, RTL_TEXTENCODING_ASCII_US));
     321           0 :     aBuffer.append(" Error: ");
     322           0 :     aBuffer.append(OUStringToOString(rError, RTL_TEXTENCODING_ASCII_US));
     323             : 
     324           0 :     fprintf(stderr, "Unexpected basic error dialog '%s'\n", aBuffer.getStr());
     325           0 : }
     326             : 
     327           0 : static void initialize_uno(const OUString &aAppURL)
     328             : {
     329           0 :     rtl::Bootstrap::setIniFilename( aAppURL + "/fundamentalrc" );
     330             : 
     331             :     rtl::Bootstrap::set( "CONFIGURATION_LAYERS",
     332             :                          "xcsxcu:${BRAND_BASE_DIR}/" LIBO_SHARE_FOLDER "/registry "
     333             :                          "res:${BRAND_BASE_DIR}/" LIBO_SHARE_FOLDER "/registry "
     334             : //                       "bundledext:${${BRAND_BASE_DIR}/" LIBO_ETC_FOLDER "/unorc:BUNDLED_EXTENSIONS_USER}/registry/com.sun.star.comp.deployment.configuration.PackageRegistryBackend/configmgr.ini " );
     335             : //                       "sharedext:${${BRAND_BASE_DIR}/" LIBO_ETC_FOLDER "/unorc:SHARED_EXTENSIONS_USER}/registry/com.sun.star.comp.deployment.configuration.PackageRegistryBackend/configmgr.ini "
     336             : //                       "userext:${${BRAND_BASE_DIR}/" LIBO_ETC_FOLDER "/unorc:UNO_USER_PACKAGES_CACHE}/registry/com.sun.star.comp.deployment.configuration.PackageRegistryBackend/configmgr.ini "
     337             : //                         "user:${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/bootstraprc:UserInstallation}/user/registrymodifications.xcu"
     338           0 :                          );
     339             : 
     340           0 :     xContext = cppu::defaultBootstrap_InitialComponentContext();
     341           0 :     fprintf(stderr, "Uno initialized %d\n", xContext.is());
     342           0 :     xFactory = xContext->getServiceManager();
     343           0 :     xSFactory = uno::Reference<lang::XMultiServiceFactory>(xFactory, uno::UNO_QUERY_THROW);
     344           0 :     comphelper::setProcessServiceFactory(xSFactory);
     345             : 
     346             :     // set UserInstallation to user profile dir in test/user-template
     347             : //    rtl::Bootstrap aDefaultVars;
     348             : //    aDefaultVars.set(OUString("UserInstallation"), aAppURL + "../registry" );
     349             :     // configmgr setup ?
     350           0 : }
     351             : 
     352           0 : static int lo_initialize(LibreOffice* pThis, const char* pAppPath)
     353             : {
     354             :     (void) pThis;
     355             : 
     356             :     static bool bInitialized = false;
     357           0 :     if (bInitialized)
     358           0 :         return 1;
     359             : 
     360           0 :     if (!pAppPath)
     361           0 :         return 0;
     362             : 
     363           0 :     OUString aAppPath(pAppPath, strlen(pAppPath), RTL_TEXTENCODING_UTF8);
     364           0 :     OUString aAppURL;
     365           0 :     if (osl::FileBase::getFileURLFromSystemPath(aAppPath, aAppURL) != osl::FileBase::E_None)
     366           0 :         return 0;
     367             : 
     368             :     try
     369             :     {
     370           0 :         osl_setCommandArgs(0, NULL);
     371           0 :         initialize_uno(aAppURL);
     372           0 :         force_c_locale();
     373             : 
     374             :         // Force headless
     375           0 :         rtl::Bootstrap::set("SAL_USE_VCLPLUGIN", "svp");
     376           0 :         InitVCL();
     377           0 :         Application::EnableHeadlessMode(true);
     378             : 
     379           0 :         ErrorHandler::RegisterDisplay(aBasicErrorFunc);
     380             : 
     381           0 :         fprintf(stderr, "initialized\n");
     382           0 :         bInitialized = true;
     383             :     }
     384           0 :     catch (css::uno::Exception& exception)
     385             :     {
     386             :         fprintf(stderr, "bootstrapping exception '%s'\n",
     387           0 :                  OUStringToOString(exception.Message, RTL_TEXTENCODING_UTF8).getStr());
     388             :     }
     389           0 :     return bInitialized;
     390             : }
     391             : 
     392           0 : LibreOffice *liblibreoffice_hook(void)
     393             : {
     394           0 :     if (!gImpl)
     395             :     {
     396           0 :         fprintf(stderr, "create libreoffice object\n");
     397           0 :         gImpl = new LibLibreOffice_Impl();
     398             :     }
     399           0 :     return static_cast<LibreOffice*>(gImpl);
     400             : }
     401             : 
     402           0 : static void lo_destroy(LibreOffice *pThis)
     403             : {
     404           0 :     LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
     405           0 :     delete pLib;
     406           0 :     gImpl = NULL;
     407           0 : }
     408             : 
     409           3 : }
     410             : 
     411             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10