LCOV - code coverage report
Current view: top level - svl/source/fsstor - fsfactory.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 39 60 65.0 %
Date: 2014-11-03 Functions: 5 9 55.6 %
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 "fsfactory.hxx"
      22             : #include <cppuhelper/factory.hxx>
      23             : #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
      24             : #include <com/sun/star/embed/ElementModes.hpp>
      25             : #include <com/sun/star/io/XSeekable.hpp>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <cppuhelper/supportsservice.hxx>
      28             : 
      29             : #include <ucbhelper/fileidentifierconverter.hxx>
      30             : #include <ucbhelper/content.hxx>
      31             : 
      32             : #include <unotools/tempfile.hxx>
      33             : #include <unotools/ucbhelper.hxx>
      34             : 
      35             : #include "fsstorage.hxx"
      36             : 
      37             : 
      38             : using namespace ::com::sun::star;
      39             : 
      40         160 : uno::Sequence< OUString > SAL_CALL FSStorageFactory::impl_staticGetSupportedServiceNames()
      41             : {
      42         160 :     uno::Sequence< OUString > aRet(2);
      43         160 :     aRet[0] = "com.sun.star.embed.FileSystemStorageFactory";
      44         160 :     aRet[1] = "com.sun.star.comp.embed.FileSystemStorageFactory";
      45         160 :     return aRet;
      46             : }
      47             : 
      48         320 : OUString SAL_CALL FSStorageFactory::impl_staticGetImplementationName()
      49             : {
      50         320 :     return OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
      51             : }
      52             : 
      53         160 : uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::impl_staticCreateSelfInstance(
      54             :             const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
      55             : {
      56         160 :     return uno::Reference< uno::XInterface >( *new FSStorageFactory( comphelper::getComponentContext(xServiceManager) ) );
      57             : }
      58             : 
      59           0 : uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
      60             :     throw ( uno::Exception,
      61             :             uno::RuntimeException, std::exception )
      62             : {
      63           0 :     OUString aTempURL;
      64             : 
      65           0 :     aTempURL = ::utl::TempFile( NULL, true ).GetURL();
      66             : 
      67           0 :     if ( aTempURL.isEmpty() )
      68           0 :         throw uno::RuntimeException(); // TODO: can not create tempfile
      69             : 
      70             :     ::ucbhelper::Content aResultContent(
      71             :         aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
      72           0 :         comphelper::getProcessComponentContext() );
      73             : 
      74             :     return uno::Reference< uno::XInterface >(
      75             :         static_cast< OWeakObject* >(
      76             :             new FSStorage(  aResultContent,
      77             :                             embed::ElementModes::READWRITE,
      78           0 :                             m_xContext ) ),
      79           0 :         uno::UNO_QUERY );
      80             : }
      81             : 
      82             : /**
      83             :  * The request for storage can be done with up to three arguments.
      84             :  * The first argument specifies a source for the storage it must be URL.
      85             :  * The second value is a mode the storage should be open in.
      86             :  * The third value is a media descriptor.
      87             :  */
      88         316 : uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
      89             :             const uno::Sequence< uno::Any >& aArguments )
      90             :     throw ( uno::Exception,
      91             :             uno::RuntimeException, std::exception )
      92             : {
      93         316 :     sal_Int32 nArgNum = aArguments.getLength();
      94             :     OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
      95             : 
      96         316 :     if ( !nArgNum )
      97           0 :         return createInstance();
      98             : 
      99             :     // first try to retrieve storage open mode if any
     100             :     // by default the storage will be open in readonly mode
     101         316 :     sal_Int32 nStorageMode = embed::ElementModes::READ;
     102         316 :     if ( nArgNum >= 2 )
     103             :     {
     104         316 :         if( !( aArguments[1] >>= nStorageMode ) )
     105             :         {
     106             :             throw lang::IllegalArgumentException(
     107             :                 ("second argument to css.embed.FileSystemStorageFactory."
     108             :                  "createInstanceWithArguments must be a"
     109             :                  " css.embed.ElementModes"),
     110           0 :                 static_cast< OWeakObject * >(this), -1);
     111             :         }
     112             :         // it's always possible to read written storage in this implementation
     113         316 :         nStorageMode |= embed::ElementModes::READ;
     114             :     }
     115             : 
     116             :     // retrieve storage source URL
     117         316 :     OUString aURL;
     118             : 
     119         316 :     if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
     120             :     {
     121             :         throw lang::IllegalArgumentException(
     122             :             ("first argument to"
     123             :              " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
     124             :              " must be a (non-empty) URL"),
     125           0 :             static_cast< OWeakObject * >(this), -1);
     126             :     }
     127             : 
     128             :     // allow to use other ucp's
     129             :     // if ( !isLocalNotFile_Impl( aURL ) )
     130         632 :     if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
     131         316 :       || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
     132         632 :       || ::utl::UCBContentHelper::IsDocument( aURL ) )
     133             :     {
     134             :         throw lang::IllegalArgumentException(
     135           0 :             ("URL \"" + aURL + "\" passed as first argument to"
     136             :              " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
     137             :              " must be a file URL denoting a directory"),
     138           0 :             static_cast< OWeakObject * >(this), -1);
     139             :     }
     140             : 
     141         316 :     if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
     142         158 :         FSStorage::MakeFolderNoUI( aURL );
     143         158 :     else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
     144             :         throw io::IOException(
     145           0 :             ("URL \"" + aURL + "\" passed to"
     146             :              " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
     147             :              " does not denote an existing directory"),
     148           0 :             static_cast< OWeakObject * >(this));
     149             : 
     150             :     ::ucbhelper::Content aResultContent(
     151             :         aURL, uno::Reference< ucb::XCommandEnvironment >(),
     152         632 :         comphelper::getProcessComponentContext() );
     153             : 
     154             :     // create storage based on source
     155             :     return uno::Reference< uno::XInterface >(
     156             :         static_cast< OWeakObject* >( new FSStorage( aResultContent,
     157             :                                                     nStorageMode,
     158         632 :                                                     m_xContext ) ),
     159         632 :         uno::UNO_QUERY );
     160             : }
     161             : 
     162           0 : OUString SAL_CALL FSStorageFactory::getImplementationName()
     163             :     throw ( uno::RuntimeException, std::exception )
     164             : {
     165           0 :     return impl_staticGetImplementationName();
     166             : }
     167             : 
     168           0 : sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
     169             :     throw ( uno::RuntimeException, std::exception )
     170             : {
     171           0 :     return cppu::supportsService(this, ServiceName);
     172             : }
     173             : 
     174           0 : uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
     175             :     throw ( uno::RuntimeException, std::exception )
     176             : {
     177           0 :     return impl_staticGetSupportedServiceNames();
     178             : }
     179             : 
     180             : 
     181             : extern "C"
     182             : {
     183         160 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL fsstorage_component_getFactory (
     184             :     const sal_Char * pImplementationName, void * pServiceManager,
     185             :     SAL_UNUSED_PARAMETER void * /* pRegistryKey */)
     186             : {
     187         160 :     void * pResult = 0;
     188         160 :     if (pServiceManager)
     189             :     {
     190         160 :         uno::Reference< lang::XSingleServiceFactory > xFactory;
     191         160 :         if (FSStorageFactory::impl_staticGetImplementationName().equalsAscii(pImplementationName))
     192             :         {
     193         320 :             xFactory = cppu::createOneInstanceFactory (
     194             :                 reinterpret_cast< lang::XMultiServiceFactory* >(pServiceManager),
     195             :                 FSStorageFactory::impl_staticGetImplementationName(),
     196             :                 FSStorageFactory::impl_staticCreateSelfInstance,
     197         160 :                 FSStorageFactory::impl_staticGetSupportedServiceNames() );
     198             :         }
     199         160 :         if (xFactory.is())
     200             :         {
     201         160 :             xFactory->acquire();
     202         160 :             pResult = xFactory.get();
     203         160 :         }
     204             :     }
     205         160 :     return pResult;
     206             : }
     207             : 
     208             : } // extern "C"
     209             : 
     210             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10