LCOV - code coverage report
Current view: top level - desktop/source/app - appinit.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 76 102 74.5 %
Date: 2015-06-13 12:38:46 Functions: 12 13 92.3 %
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             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <algorithm>
      22             : 
      23             : #include <config_vclplug.h>
      24             : 
      25             : #include "app.hxx"
      26             : #include "cmdlineargs.hxx"
      27             : #include "desktopresid.hxx"
      28             : #include "desktop.hrc"
      29             : #include <com/sun/star/registry/XSimpleRegistry.hpp>
      30             : #include <com/sun/star/lang/XComponent.hpp>
      31             : #include <com/sun/star/lang/XInitialization.hpp>
      32             : #include <com/sun/star/uno/Exception.hpp>
      33             : #include <com/sun/star/uno/XCurrentContext.hpp>
      34             : #include <com/sun/star/packages/zip/ZipIOException.hpp>
      35             : #include <com/sun/star/beans/XPropertySet.hpp>
      36             : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
      37             : #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
      38             : #include <uno/current_context.hxx>
      39             : #include <cppuhelper/bootstrap.hxx>
      40             : #include <officecfg/Setup.hxx>
      41             : #include <osl/file.hxx>
      42             : #include <osl/module.h>
      43             : #include <rtl/uri.hxx>
      44             : #include <rtl/ustrbuf.hxx>
      45             : #include <rtl/bootstrap.hxx>
      46             : #include <sal/log.hxx>
      47             : 
      48             : #include <tools/rcid.h>
      49             : 
      50             : #include <rtl/instance.hxx>
      51             : #include <comphelper/processfactory.hxx>
      52             : #include <unotools/localfilehelper.hxx>
      53             : #include <unotools/ucbhelper.hxx>
      54             : #include <unotools/tempfile.hxx>
      55             : #include <vcl/svapp.hxx>
      56             : #include <unotools/pathoptions.hxx>
      57             : #include <map>
      58             : 
      59             : using namespace desktop;
      60             : using namespace ::com::sun::star::uno;
      61             : using namespace ::com::sun::star::lang;
      62             : using namespace ::com::sun::star::beans;
      63             : using namespace ::com::sun::star::registry;
      64             : using namespace ::com::sun::star::ucb;
      65             : 
      66             : namespace desktop
      67             : {
      68             : 
      69             : 
      70             : 
      71         116 : static void configureUcb()
      72             : {
      73             :     // For backwards compatibility, in case some code still uses plain
      74             :     // createInstance w/o args directly to obtain an instance:
      75         116 :     UniversalContentBroker::create(comphelper::getProcessComponentContext());
      76             : 
      77             : #if ENABLE_GNOME_VFS
      78             :     try {
      79             :         // Instantiate GNOME-VFS UCP in the thread that initialized GNOME in order
      80             :         // to avoid a deadlock that may occur in case the UCP gets initialized from
      81             :         // a different thread (which may happen when calling remotely via UNO); this
      82             :         // is not a fix, just a workaround:
      83             :         Reference< XCurrentContext > xCurrentContext(getCurrentContext());
      84             :         Any aValue(xCurrentContext->getValueByName("system.desktop-environment"));
      85             :         OUString aDesktopEnvironment;
      86             :         if ((aValue >>= aDesktopEnvironment) && aDesktopEnvironment == "GNOME")
      87             :         {
      88             :             Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
      89             :             UniversalContentBroker::create(xContext)
      90             :                 ->registerContentProvider(
      91             :                         Reference<XContentProvider>(
      92             :                         xContext->getServiceManager()->createInstanceWithContext(
      93             :                                 "com.sun.star.ucb.GnomeVFSContentProvider", xContext),
      94             :                         UNO_QUERY_THROW),
      95             :                         ".*", false);
      96             :         }
      97             :     }
      98             :     catch ( const Exception & )
      99             :     {
     100             :         SAL_WARN( "desktop.app", "missing gnome-vfs component to initialize thread workaround" );
     101             :     }
     102             : #endif // ENABLE_GNOME_VFS
     103         116 : }
     104             : 
     105         116 : void Desktop::InitApplicationServiceManager()
     106             : {
     107         116 :     Reference<XMultiServiceFactory> sm;
     108             : #ifdef ANDROID
     109             :     OUString aUnoRc( "file:///assets/program/unorc" );
     110             :     sm.set(
     111             :         cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
     112             :         UNO_QUERY_THROW);
     113             : #else
     114             :     sm.set(
     115         232 :         cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
     116         116 :         UNO_QUERY_THROW);
     117             : #endif
     118         116 :     comphelper::setProcessServiceFactory(sm);
     119         116 : }
     120             : 
     121         116 : void Desktop::RegisterServices(Reference< XComponentContext > const & context)
     122             : {
     123         116 :     if( !m_bServicesRegistered )
     124             :     {
     125             :         // interpret command line arguments
     126         116 :         CommandLineArgs& rCmdLine = GetCommandLineArgs();
     127             : 
     128             :         // Headless mode for FAT Office
     129         116 :         bool bHeadlessMode = rCmdLine.IsHeadless();
     130         116 :         if ( bHeadlessMode )
     131         116 :             Application::EnableHeadlessMode(false);
     132             : 
     133             :         // read accept string from configuration
     134             :         OUString conDcpCfg(
     135         116 :             officecfg::Setup::Office::ooSetupConnectionURL::get(context));
     136         116 :         if (!conDcpCfg.isEmpty()) {
     137           0 :             createAcceptor(conDcpCfg);
     138             :         }
     139             : 
     140         116 :         std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
     141         693 :         for (std::vector< OUString >::const_iterator i(conDcp.begin());
     142         462 :              i != conDcp.end(); ++i)
     143             :         {
     144         115 :             createAcceptor(*i);
     145             :         }
     146             : 
     147         116 :         configureUcb();
     148             : 
     149         116 :         CreateTemporaryDirectory();
     150         116 :         m_bServicesRegistered = true;
     151             :     }
     152         116 : }
     153             : 
     154             : typedef std::map< OUString, css::uno::Reference<css::lang::XInitialization> > AcceptorMap;
     155             : 
     156             : namespace
     157             : {
     158             :     struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
     159             :     struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
     160             : }
     161             : 
     162             : static bool bAccept = false;
     163             : 
     164         115 : void Desktop::createAcceptor(const OUString& aAcceptString)
     165             : {
     166             :     // check whether the requested acceptor already exists
     167         115 :     AcceptorMap &rMap = acceptorMap::get();
     168         115 :     AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
     169         115 :     if (pIter == rMap.end() )
     170             :     {
     171         115 :         Sequence< Any > aSeq( 2 );
     172         115 :         aSeq[0] <<= aAcceptString;
     173         115 :         aSeq[1] <<= bAccept;
     174         230 :         Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     175             :         Reference<XInitialization> rAcceptor(
     176         230 :             xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
     177         230 :             UNO_QUERY );
     178         115 :         if ( rAcceptor.is() )
     179             :         {
     180             :             try
     181             :             {
     182         115 :                 rAcceptor->initialize( aSeq );
     183         115 :                 rMap.insert(AcceptorMap::value_type(aAcceptString, rAcceptor));
     184             :             }
     185           0 :             catch (const com::sun::star::uno::Exception& e)
     186             :             {
     187             :                 // no error handling needed...
     188             :                 // acceptor just won't come up
     189             :                 SAL_WARN( "desktop.app", "Acceptor could not be created: " << e.Message);
     190             :             }
     191             :         }
     192             :         else
     193             :         {
     194             :             // there is already an acceptor with this description
     195             :             SAL_WARN( "desktop.app", "Acceptor already exists.");
     196         115 :         }
     197             :     }
     198         115 : }
     199             : 
     200         192 : class enable
     201             : {
     202             :     private:
     203             :     Sequence<Any> m_aSeq;
     204             :     public:
     205          64 :     enable() : m_aSeq(1) {
     206          64 :         m_aSeq[0] <<= sal_True;
     207          64 :     }
     208          63 :     void operator() (const AcceptorMap::value_type& val) {
     209          63 :         if (val.second.is()) {
     210          63 :             val.second->initialize(m_aSeq);
     211             :         }
     212          63 :     }
     213             : };
     214             : 
     215          64 : void Desktop::enableAcceptors()
     216             : {
     217          64 :     if (!bAccept)
     218             :     {
     219             :         // from now on, all new acceptors are enabled
     220          64 :         bAccept = true;
     221             :         // enable existing acceptors by calling initialize(true)
     222             :         // on all existing acceptors
     223          64 :         AcceptorMap &rMap = acceptorMap::get();
     224          64 :         std::for_each(rMap.begin(), rMap.end(), enable());
     225             :     }
     226          64 : }
     227             : 
     228           0 : void Desktop::destroyAcceptor(const OUString& aAcceptString)
     229             : {
     230             :     // special case stop all acceptors
     231           0 :     AcceptorMap &rMap = acceptorMap::get();
     232           0 :     if (aAcceptString == "all") {
     233           0 :         rMap.clear();
     234             : 
     235             :     } else {
     236             :         // try to remove acceptor from map
     237           0 :         AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
     238           0 :         if (pIter != rMap.end() ) {
     239             :             // remove reference from map
     240             :             // this is the last reference and the acceptor will be destructed
     241           0 :             rMap.erase(aAcceptString);
     242             :         } else {
     243             :             SAL_WARN( "desktop.app", "Found no acceptor to remove");
     244             :         }
     245             :     }
     246           0 : }
     247             : 
     248             : 
     249         116 : void Desktop::DeregisterServices()
     250             : {
     251             :     // stop all acceptors by clearing the map
     252         116 :     acceptorMap::get().clear();
     253         116 : }
     254             : 
     255         116 : void Desktop::CreateTemporaryDirectory()
     256             : {
     257         116 :     OUString aTempBaseURL;
     258             :     try
     259             :     {
     260         116 :         SvtPathOptions aOpt;
     261         116 :         aTempBaseURL = aOpt.GetTempPath();
     262             :     }
     263           0 :     catch (RuntimeException& e)
     264             :     {
     265             :         // Catch runtime exception here: We have to add language dependent info
     266             :         // to the exception message. Fallback solution uses hard coded string.
     267           0 :         OUString aMsg;
     268           0 :         DesktopResId aResId( STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE );
     269           0 :         aResId.SetRT( RSC_STRING );
     270           0 :         if ( aResId.GetResMgr()->IsAvailable( aResId ))
     271           0 :             aMsg = OUString( aResId );
     272             :         else
     273           0 :             aMsg = "The path manager is not available.\n";
     274           0 :         e.Message = aMsg + e.Message;
     275           0 :         throw;
     276             :     }
     277             : 
     278             :     // set temp base directory
     279         116 :     if ( aTempBaseURL.endsWith( "/" ) )
     280           0 :         aTempBaseURL = aTempBaseURL.copy( 0, aTempBaseURL.getLength() - 1 );
     281             : 
     282         232 :     OUString aRet;
     283         232 :     OUString aTempPath( aTempBaseURL );
     284             : 
     285             :     // create new current temporary directory
     286         116 :     ::utl::LocalFileHelper::ConvertURLToPhysicalName( aTempBaseURL, aRet );
     287         116 :     ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
     288         116 :     aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
     289         116 :     if ( aTempPath.isEmpty() )
     290             :     {
     291           0 :         ::osl::File::getTempDirURL( aTempBaseURL );
     292             : 
     293           0 :         if ( aTempBaseURL.endsWith( "/" ) )
     294           0 :             aTempBaseURL = aTempBaseURL.copy( 0, aTempBaseURL.getLength() - 1 );
     295             : 
     296           0 :         aTempPath = aTempBaseURL;
     297           0 :         ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
     298           0 :         aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
     299             :     }
     300             : 
     301             :     // set new current temporary directory
     302         116 :     ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aTempPath, aRet );
     303         232 :     CurrentTempURL::get() = aRet;
     304         116 : }
     305             : 
     306         116 : void Desktop::RemoveTemporaryDirectory()
     307             : {
     308             :     // remove current temporary directory
     309         116 :     OUString &rCurrentTempURL = CurrentTempURL::get();
     310         116 :     if ( !rCurrentTempURL.isEmpty() )
     311             :     {
     312         116 :         ::utl::UCBContentHelper::Kill( rCurrentTempURL );
     313             :     }
     314         116 : }
     315             : 
     316             : } // namespace desktop
     317             : 
     318             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11