bump product version to 6.3.0.0.beta1
[LibreOffice.git] / svl / source / fsstor / fsfactory.cxx
blob31fd68bd54a6b16258660d432d6a96dcd35be0a1
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 <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/IOException.hpp>
26 #include <com/sun/star/io/XSeekable.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <ucbhelper/content.hxx>
32 #include <unotools/tempfile.hxx>
33 #include <unotools/ucbhelper.hxx>
35 #include "fsstorage.hxx"
38 using namespace ::com::sun::star;
40 uno::Sequence< OUString > FSStorageFactory::impl_staticGetSupportedServiceNames()
42 uno::Sequence< OUString > aRet(2);
43 aRet[0] = "com.sun.star.embed.FileSystemStorageFactory";
44 aRet[1] = "com.sun.star.comp.embed.FileSystemStorageFactory";
45 return aRet;
48 OUString FSStorageFactory::impl_staticGetImplementationName()
50 return OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
53 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::impl_staticCreateSelfInstance(
54 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
56 return uno::Reference< uno::XInterface >( *new FSStorageFactory( comphelper::getComponentContext(xServiceManager) ) );
59 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
61 OUString aTempURL = ::utl::TempFile( nullptr, true ).GetURL();
63 if ( aTempURL.isEmpty() )
64 throw uno::RuntimeException(); // TODO: can not create tempfile
66 ::ucbhelper::Content aResultContent(
67 aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
68 comphelper::getProcessComponentContext() );
70 return uno::Reference< uno::XInterface >(
71 static_cast< OWeakObject* >(
72 new FSStorage( aResultContent,
73 embed::ElementModes::READWRITE,
74 m_xContext ) ),
75 uno::UNO_QUERY );
78 /**
79 * The request for storage can be done with up to three arguments.
80 * The first argument specifies a source for the storage it must be URL.
81 * The second value is a mode the storage should be open in.
82 * The third value is a media descriptor.
84 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
85 const uno::Sequence< uno::Any >& aArguments )
87 sal_Int32 nArgNum = aArguments.getLength();
88 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
90 if ( !nArgNum )
91 return createInstance();
93 // first try to retrieve storage open mode if any
94 // by default the storage will be open in readonly mode
95 sal_Int32 nStorageMode = embed::ElementModes::READ;
96 if ( nArgNum >= 2 )
98 if( !( aArguments[1] >>= nStorageMode ) )
100 throw lang::IllegalArgumentException(
101 ("second argument to css.embed.FileSystemStorageFactory."
102 "createInstanceWithArguments must be a"
103 " css.embed.ElementModes"),
104 static_cast< OWeakObject * >(this), -1);
106 // it's always possible to read written storage in this implementation
107 nStorageMode |= embed::ElementModes::READ;
110 // retrieve storage source URL
111 OUString aURL;
113 if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
115 throw lang::IllegalArgumentException(
116 ("first argument to"
117 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
118 " must be a (non-empty) URL"),
119 static_cast< OWeakObject * >(this), -1);
122 // allow to use other ucp's
123 // if ( !isLocalNotFile_Impl( aURL ) )
124 if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
125 || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
126 || ::utl::UCBContentHelper::IsDocument( aURL ) )
128 throw lang::IllegalArgumentException(
129 ("URL \"" + aURL + "\" passed as first argument to"
130 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
131 " must be a file URL denoting a directory"),
132 static_cast< OWeakObject * >(this), -1);
135 if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
136 FSStorage::MakeFolderNoUI( aURL );
137 else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
138 throw io::IOException(
139 ("URL \"" + aURL + "\" passed to"
140 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
141 " does not denote an existing directory"),
142 static_cast< OWeakObject * >(this));
144 ::ucbhelper::Content aResultContent(
145 aURL, uno::Reference< ucb::XCommandEnvironment >(),
146 comphelper::getProcessComponentContext() );
148 // create storage based on source
149 return uno::Reference< uno::XInterface >(
150 static_cast< OWeakObject* >( new FSStorage( aResultContent,
151 nStorageMode,
152 m_xContext ) ),
153 uno::UNO_QUERY );
156 OUString SAL_CALL FSStorageFactory::getImplementationName()
158 return impl_staticGetImplementationName();
161 sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
163 return cppu::supportsService(this, ServiceName);
166 uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
168 return impl_staticGetSupportedServiceNames();
172 extern "C"
174 SAL_DLLPUBLIC_EXPORT void * fsstorage_component_getFactory (
175 const sal_Char * pImplementationName, void * pServiceManager,
176 SAL_UNUSED_PARAMETER void * /* pRegistryKey */)
178 void * pResult = nullptr;
179 if (pServiceManager)
181 uno::Reference< lang::XSingleServiceFactory > xFactory;
182 if (FSStorageFactory::impl_staticGetImplementationName().equalsAscii(pImplementationName))
184 xFactory = cppu::createOneInstanceFactory (
185 static_cast< lang::XMultiServiceFactory* >(pServiceManager),
186 FSStorageFactory::impl_staticGetImplementationName(),
187 FSStorageFactory::impl_staticCreateSelfInstance,
188 FSStorageFactory::impl_staticGetSupportedServiceNames() );
190 if (xFactory.is())
192 xFactory->acquire();
193 pResult = xFactory.get();
196 return pResult;
199 } // extern "C"
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */