cid#1636690 Dereference after null check
[LibreOffice.git] / svl / source / fsstor / fsfactory.cxx
blob64989a9b32dba451a5fab0f34732ca8bbeb0d81e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <fsfactory.hxx>
22 #include <com/sun/star/embed/ElementModes.hpp>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <comphelper/processfactory.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <ucbhelper/content.hxx>
29 #include <unotools/tempfile.hxx>
30 #include <unotools/ucbhelper.hxx>
32 #include "fsstorage.hxx"
35 using namespace ::com::sun::star;
38 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
40 OUString aTempURL = ::utl::CreateTempURL( nullptr, true );
41 if ( aTempURL.isEmpty() )
42 throw uno::RuntimeException(u"Cannot create tempfile."_ustr);
44 ::ucbhelper::Content aResultContent(
45 aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
46 comphelper::getProcessComponentContext() );
48 return cppu::getXWeak(
49 new FSStorage( aResultContent,
50 embed::ElementModes::READWRITE,
51 m_xContext ) );
54 /**
55 * The request for storage can be done with up to three arguments.
56 * The first argument specifies a source for the storage it must be URL.
57 * The second value is a mode the storage should be open in.
58 * The third value is a media descriptor.
60 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
61 const uno::Sequence< uno::Any >& aArguments )
63 sal_Int32 nArgNum = aArguments.getLength();
64 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
66 if ( !nArgNum )
67 return createInstance();
69 // first try to retrieve storage open mode if any
70 // by default the storage will be open in readonly mode
71 sal_Int32 nStorageMode = embed::ElementModes::READ;
72 if ( nArgNum >= 2 )
74 if( !( aArguments[1] >>= nStorageMode ) )
76 throw lang::IllegalArgumentException(
77 (u"second argument to css.embed.FileSystemStorageFactory."
78 "createInstanceWithArguments must be a"
79 " css.embed.ElementModes"_ustr),
80 getXWeak(), -1);
82 // it's always possible to read written storage in this implementation
83 nStorageMode |= embed::ElementModes::READ;
86 // retrieve storage source URL
87 OUString aURL;
89 if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
91 throw lang::IllegalArgumentException(
92 (u"first argument to"
93 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
94 " must be a (non-empty) URL"_ustr),
95 getXWeak(), -1);
98 // allow to use other ucp's
99 // if ( !isLocalNotFile_Impl( aURL ) )
100 if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
101 || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
102 || ::utl::UCBContentHelper::IsDocument( aURL ) )
104 throw lang::IllegalArgumentException(
105 ("URL \"" + aURL + "\" passed as first argument to"
106 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
107 " must be a file URL denoting a directory"),
108 getXWeak(), -1);
111 if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
112 FSStorage::MakeFolderNoUI( aURL );
113 else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
114 throw io::IOException(
115 ("URL \"" + aURL + "\" passed to"
116 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
117 " does not denote an existing directory"),
118 getXWeak());
120 ::ucbhelper::Content aResultContent(
121 aURL, uno::Reference< ucb::XCommandEnvironment >(),
122 comphelper::getProcessComponentContext() );
124 // create storage based on source
125 return cppu::getXWeak( new FSStorage( aResultContent,
126 nStorageMode,
127 m_xContext ) );
130 OUString SAL_CALL FSStorageFactory::getImplementationName()
132 return u"com.sun.star.comp.embed.FileSystemStorageFactory"_ustr;
135 sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
137 return cppu::supportsService(this, ServiceName);
140 uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
142 return { u"com.sun.star.embed.FileSystemStorageFactory"_ustr,
143 u"com.sun.star.comp.embed.FileSystemStorageFactory"_ustr };
148 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
149 svl_FSStorageFactory_get_implementation(
150 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
152 return cppu::acquire(new FSStorageFactory(context));
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */