bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / fsstor / fsfactory.cxx
blob5fa5339f8ea5952be0d4d2f3f350b4b48c0c5f3e
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/XSeekable.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <cppuhelper/supportsservice.hxx>
29 #include <ucbhelper/fileidentifierconverter.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 > SAL_CALL 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 SAL_CALL 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()
60 throw ( uno::Exception,
61 uno::RuntimeException, std::exception )
63 OUString aTempURL;
65 aTempURL = ::utl::TempFile( NULL, true ).GetURL();
67 if ( aTempURL.isEmpty() )
68 throw uno::RuntimeException(); // TODO: can not create tempfile
70 ::ucbhelper::Content aResultContent(
71 aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
72 comphelper::getProcessComponentContext() );
74 return uno::Reference< uno::XInterface >(
75 static_cast< OWeakObject* >(
76 new FSStorage( aResultContent,
77 embed::ElementModes::READWRITE,
78 m_xContext ) ),
79 uno::UNO_QUERY );
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.
88 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
89 const uno::Sequence< uno::Any >& aArguments )
90 throw ( uno::Exception,
91 uno::RuntimeException, std::exception )
93 sal_Int32 nArgNum = aArguments.getLength();
94 OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
96 if ( !nArgNum )
97 return createInstance();
99 // first try to retrieve storage open mode if any
100 // by default the storage will be open in readonly mode
101 sal_Int32 nStorageMode = embed::ElementModes::READ;
102 if ( nArgNum >= 2 )
104 if( !( aArguments[1] >>= nStorageMode ) )
106 throw lang::IllegalArgumentException(
107 ("second argument to css.embed.FileSystemStorageFactory."
108 "createInstanceWithArguments must be a"
109 " css.embed.ElementModes"),
110 static_cast< OWeakObject * >(this), -1);
112 // it's always possible to read written storage in this implementation
113 nStorageMode |= embed::ElementModes::READ;
116 // retrieve storage source URL
117 OUString aURL;
119 if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
121 throw lang::IllegalArgumentException(
122 ("first argument to"
123 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
124 " must be a (non-empty) URL"),
125 static_cast< OWeakObject * >(this), -1);
128 // allow to use other ucp's
129 // if ( !isLocalNotFile_Impl( aURL ) )
130 if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
131 || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
132 || ::utl::UCBContentHelper::IsDocument( aURL ) )
134 throw lang::IllegalArgumentException(
135 ("URL \"" + aURL + "\" passed as first argument to"
136 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
137 " must be a file URL denoting a directory"),
138 static_cast< OWeakObject * >(this), -1);
141 if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
142 FSStorage::MakeFolderNoUI( aURL );
143 else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
144 throw io::IOException(
145 ("URL \"" + aURL + "\" passed to"
146 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
147 " does not denote an existing directory"),
148 static_cast< OWeakObject * >(this));
150 ::ucbhelper::Content aResultContent(
151 aURL, uno::Reference< ucb::XCommandEnvironment >(),
152 comphelper::getProcessComponentContext() );
154 // create storage based on source
155 return uno::Reference< uno::XInterface >(
156 static_cast< OWeakObject* >( new FSStorage( aResultContent,
157 nStorageMode,
158 m_xContext ) ),
159 uno::UNO_QUERY );
162 OUString SAL_CALL FSStorageFactory::getImplementationName()
163 throw ( uno::RuntimeException, std::exception )
165 return impl_staticGetImplementationName();
168 sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
169 throw ( uno::RuntimeException, std::exception )
171 return cppu::supportsService(this, ServiceName);
174 uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
175 throw ( uno::RuntimeException, std::exception )
177 return impl_staticGetSupportedServiceNames();
181 extern "C"
183 SAL_DLLPUBLIC_EXPORT void * SAL_CALL fsstorage_component_getFactory (
184 const sal_Char * pImplementationName, void * pServiceManager,
185 SAL_UNUSED_PARAMETER void * /* pRegistryKey */)
187 void * pResult = 0;
188 if (pServiceManager)
190 uno::Reference< lang::XSingleServiceFactory > xFactory;
191 if (FSStorageFactory::impl_staticGetImplementationName().equalsAscii(pImplementationName))
193 xFactory = cppu::createOneInstanceFactory (
194 static_cast< lang::XMultiServiceFactory* >(pServiceManager),
195 FSStorageFactory::impl_staticGetImplementationName(),
196 FSStorageFactory::impl_staticCreateSelfInstance,
197 FSStorageFactory::impl_staticGetSupportedServiceNames() );
199 if (xFactory.is())
201 xFactory->acquire();
202 pResult = xFactory.get();
205 return pResult;
208 } // extern "C"
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */