LCOV - code coverage report
Current view: top level - desktop/source/app - appinit.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 77 104 74.0 %
Date: 2014-04-11 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             : 
      47             : #include <tools/rcid.h>
      48             : 
      49             : #include <rtl/instance.hxx>
      50             : #include <comphelper/processfactory.hxx>
      51             : #include <unotools/localfilehelper.hxx>
      52             : #include <unotools/ucbhelper.hxx>
      53             : #include <unotools/tempfile.hxx>
      54             : #include <vcl/svapp.hxx>
      55             : #include <unotools/pathoptions.hxx>
      56             : 
      57             : using namespace desktop;
      58             : using namespace ::com::sun::star::uno;
      59             : using namespace ::com::sun::star::lang;
      60             : using namespace ::com::sun::star::beans;
      61             : using namespace ::com::sun::star::registry;
      62             : using namespace ::com::sun::star::ucb;
      63             : 
      64             : namespace desktop
      65             : {
      66             : 
      67             : 
      68             : 
      69          80 : static void configureUcb()
      70             : {
      71             :     SAL_INFO( "desktop.app", "desktop (sb93797) ::configureUcb" );
      72             : 
      73             :     // For backwards compatibility, in case some code still uses plain
      74             :     // createInstance w/o args directly to obtain an instance:
      75          80 :     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          80 : }
     104             : 
     105          80 : void Desktop::InitApplicationServiceManager()
     106             : {
     107             :     SAL_INFO( "desktop.app", "desktop (cd100003) ::createApplicationServiceManager" );
     108          80 :     Reference<XMultiServiceFactory> sm;
     109             : #ifdef ANDROID
     110             :     OUString aUnoRc( "file:///assets/program/unorc" );
     111             :     sm.set(
     112             :         cppu::defaultBootstrap_InitialComponentContext( aUnoRc )->getServiceManager(),
     113             :         UNO_QUERY_THROW);
     114             : #else
     115             :     sm.set(
     116         160 :         cppu::defaultBootstrap_InitialComponentContext()->getServiceManager(),
     117          80 :         UNO_QUERY_THROW);
     118             : #endif
     119          80 :     comphelper::setProcessServiceFactory(sm);
     120          80 : }
     121             : 
     122          80 : void Desktop::RegisterServices(Reference< XComponentContext > const & context)
     123             : {
     124          80 :     if( !m_bServicesRegistered )
     125             :     {
     126             :         SAL_INFO( "desktop.app", "desktop (cd100003) ::registerServices" );
     127             : 
     128             :         // interpret command line arguments
     129          80 :         CommandLineArgs& rCmdLine = GetCommandLineArgs();
     130             : 
     131             :         // Headless mode for FAT Office
     132          80 :         sal_Bool bHeadlessMode = rCmdLine.IsHeadless();
     133          80 :         if ( bHeadlessMode )
     134          80 :             Application::EnableHeadlessMode(false);
     135             : 
     136             :         // read accept string from configuration
     137             :         OUString conDcpCfg(
     138          80 :             officecfg::Setup::Office::ooSetupConnectionURL::get(context));
     139          80 :         if (!conDcpCfg.isEmpty()) {
     140           0 :             createAcceptor(conDcpCfg);
     141             :         }
     142             : 
     143          80 :         std::vector< OUString > const & conDcp = rCmdLine.GetAccept();
     144         480 :         for (std::vector< OUString >::const_iterator i(conDcp.begin());
     145         320 :              i != conDcp.end(); ++i)
     146             :         {
     147          80 :             createAcceptor(*i);
     148             :         }
     149             : 
     150          80 :         configureUcb();
     151             : 
     152          80 :         CreateTemporaryDirectory();
     153          80 :         m_bServicesRegistered = true;
     154             :     }
     155          80 : }
     156             : 
     157             : namespace
     158             : {
     159             :     struct acceptorMap : public rtl::Static< AcceptorMap, acceptorMap > {};
     160             :     struct CurrentTempURL : public rtl::Static< OUString, CurrentTempURL > {};
     161             : }
     162             : 
     163             : static sal_Bool bAccept = sal_False;
     164             : 
     165          80 : void Desktop::createAcceptor(const OUString& aAcceptString)
     166             : {
     167             :     // check whether the requested acceptor already exists
     168          80 :     AcceptorMap &rMap = acceptorMap::get();
     169          80 :     AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
     170          80 :     if (pIter == rMap.end() )
     171             :     {
     172          80 :         Sequence< Any > aSeq( 2 );
     173          80 :         aSeq[0] <<= aAcceptString;
     174          80 :         aSeq[1] <<= bAccept;
     175         160 :         Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     176             :         Reference<XInitialization> rAcceptor(
     177         160 :             xContext->getServiceManager()->createInstanceWithContext("com.sun.star.office.Acceptor", xContext),
     178         160 :             UNO_QUERY );
     179          80 :         if ( rAcceptor.is() )
     180             :         {
     181             :             try
     182             :             {
     183          80 :                 rAcceptor->initialize( aSeq );
     184          80 :                 rMap.insert(AcceptorMap::value_type(aAcceptString, rAcceptor));
     185             :             }
     186           0 :             catch (const com::sun::star::uno::Exception& e)
     187             :             {
     188             :                 // no error handling needed...
     189             :                 // acceptor just won't come up
     190             :                 SAL_WARN( "desktop.app", "Acceptor could not be created: " << e.Message);
     191             :             }
     192             :         }
     193             :         else
     194             :         {
     195             :             // there is already an acceptor with this description
     196             :             SAL_WARN( "desktop.app", "Acceptor already exists.");
     197          80 :         }
     198             :     }
     199          80 : }
     200             : 
     201         144 : class enable
     202             : {
     203             :     private:
     204             :     Sequence<Any> m_aSeq;
     205             :     public:
     206          48 :     enable() : m_aSeq(1) {
     207          48 :         m_aSeq[0] <<= sal_True;
     208          48 :     }
     209          48 :     void operator() (const AcceptorMap::value_type& val) {
     210          48 :         if (val.second.is()) {
     211          48 :             val.second->initialize(m_aSeq);
     212             :         }
     213          48 :     }
     214             : };
     215             : 
     216          48 : void Desktop::enableAcceptors()
     217             : {
     218             :     SAL_INFO( "desktop.app", "desktop (lo119109) Desktop::enableAcceptors");
     219          48 :     if (!bAccept)
     220             :     {
     221             :         // from now on, all new acceptors are enabled
     222          48 :         bAccept = sal_True;
     223             :         // enable existing acceptors by calling initialize(true)
     224             :         // on all existing acceptors
     225          48 :         AcceptorMap &rMap = acceptorMap::get();
     226          48 :         std::for_each(rMap.begin(), rMap.end(), enable());
     227             :     }
     228          48 : }
     229             : 
     230           0 : void Desktop::destroyAcceptor(const OUString& aAcceptString)
     231             : {
     232             :     // special case stop all acceptors
     233           0 :     AcceptorMap &rMap = acceptorMap::get();
     234           0 :     if (aAcceptString.equalsAscii("all")) {
     235           0 :         rMap.clear();
     236             : 
     237             :     } else {
     238             :         // try to remove acceptor from map
     239           0 :         AcceptorMap::const_iterator pIter = rMap.find(aAcceptString);
     240           0 :         if (pIter != rMap.end() ) {
     241             :             // remove reference from map
     242             :             // this is the last reference and the acceptor will be destructed
     243           0 :             rMap.erase(aAcceptString);
     244             :         } else {
     245             :             SAL_WARN( "desktop.app", "Found no acceptor to remove");
     246             :         }
     247             :     }
     248           0 : }
     249             : 
     250             : 
     251          80 : void Desktop::DeregisterServices()
     252             : {
     253             :     // stop all acceptors by clearing the map
     254          80 :     acceptorMap::get().clear();
     255          80 : }
     256             : 
     257          80 : void Desktop::CreateTemporaryDirectory()
     258             : {
     259             :     SAL_INFO( "desktop.app", "desktop (cd100003) ::createTemporaryDirectory" );
     260             : 
     261          80 :     OUString aTempBaseURL;
     262             :     try
     263             :     {
     264          80 :         SvtPathOptions aOpt;
     265          80 :         aTempBaseURL = aOpt.GetTempPath();
     266             :     }
     267           0 :     catch (RuntimeException& e)
     268             :     {
     269             :         // Catch runtime exception here: We have to add language dependent info
     270             :         // to the exception message. Fallback solution uses hard coded string.
     271           0 :         OUString aMsg;
     272           0 :         DesktopResId aResId( STR_BOOTSTRAP_ERR_NO_PATHSET_SERVICE );
     273           0 :         aResId.SetRT( RSC_STRING );
     274           0 :         if ( aResId.GetResMgr()->IsAvailable( aResId ))
     275           0 :             aMsg = OUString( aResId );
     276             :         else
     277           0 :             aMsg = "The path manager is not available.\n";
     278           0 :         e.Message = aMsg + e.Message;
     279           0 :         throw;
     280             :     }
     281             : 
     282             :     // set temp base directory
     283          80 :     sal_Int32 nLength = aTempBaseURL.getLength();
     284          80 :     if ( aTempBaseURL.matchAsciiL( "/", 1, nLength-1 ) )
     285           0 :         aTempBaseURL = aTempBaseURL.copy( 0, nLength - 1 );
     286             : 
     287         160 :     OUString aRet;
     288         160 :     OUString aTempPath( aTempBaseURL );
     289             : 
     290             :     // create new current temporary directory
     291          80 :     ::utl::LocalFileHelper::ConvertURLToPhysicalName( aTempBaseURL, aRet );
     292          80 :     ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
     293          80 :     aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
     294          80 :     if ( aTempPath.isEmpty() )
     295             :     {
     296           0 :         ::osl::File::getTempDirURL( aTempBaseURL );
     297             : 
     298           0 :         nLength = aTempBaseURL.getLength();
     299           0 :         if ( aTempBaseURL.matchAsciiL( "/", 1, nLength-1 ) )
     300           0 :             aTempBaseURL = aTempBaseURL.copy( 0, nLength - 1 );
     301             : 
     302           0 :         aTempPath = aTempBaseURL;
     303           0 :         ::osl::FileBase::getFileURLFromSystemPath( aRet, aTempPath );
     304           0 :         aTempPath = ::utl::TempFile::SetTempNameBaseDirectory( aTempPath );
     305             :     }
     306             : 
     307             :     // set new current temporary directory
     308          80 :     ::utl::LocalFileHelper::ConvertPhysicalNameToURL( aTempPath, aRet );
     309         160 :     CurrentTempURL::get() = aRet;
     310          80 : }
     311             : 
     312          80 : void Desktop::RemoveTemporaryDirectory()
     313             : {
     314             :     SAL_INFO( "desktop.app", "desktop (cd100003) ::removeTemporaryDirectory" );
     315             : 
     316             :     // remove current temporary directory
     317          80 :     OUString &rCurrentTempURL = CurrentTempURL::get();
     318          80 :     if ( !rCurrentTempURL.isEmpty() )
     319             :     {
     320          80 :         ::utl::UCBContentHelper::Kill( rCurrentTempURL );
     321             :     }
     322          80 : }
     323             : 
     324             : } // namespace desktop
     325             : 
     326             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10