1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
28 #include <ucbhelper/fileidentifierconverter.hxx>
29 #include <ucbhelper/content.hxx>
31 #include <unotools/tempfile.hxx>
32 #include <unotools/ucbhelper.hxx>
34 #include "fsstorage.hxx"
37 using namespace ::com::sun::star
;
39 //-------------------------------------------------------------------------
40 uno::Sequence
< OUString
> SAL_CALL
FSStorageFactory::impl_staticGetSupportedServiceNames()
42 uno::Sequence
< OUString
> aRet(2);
43 aRet
[0] = OUString("com.sun.star.embed.FileSystemStorageFactory");
44 aRet
[1] = OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
48 //-------------------------------------------------------------------------
49 OUString SAL_CALL
FSStorageFactory::impl_staticGetImplementationName()
51 return OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
54 //-------------------------------------------------------------------------
55 uno::Reference
< uno::XInterface
> SAL_CALL
FSStorageFactory::impl_staticCreateSelfInstance(
56 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
58 return uno::Reference
< uno::XInterface
>( *new FSStorageFactory( xServiceManager
) );
61 //-------------------------------------------------------------------------
62 uno::Reference
< uno::XInterface
> SAL_CALL
FSStorageFactory::createInstance()
63 throw ( uno::Exception
,
64 uno::RuntimeException
)
68 aTempURL
= ::utl::TempFile( NULL
, sal_True
).GetURL();
70 if ( aTempURL
.isEmpty() )
71 throw uno::RuntimeException(); // TODO: can not create tempfile
73 ::ucbhelper::Content
aResultContent(
74 aTempURL
, uno::Reference
< ucb::XCommandEnvironment
>(),
75 comphelper::getProcessComponentContext() );
77 return uno::Reference
< uno::XInterface
>(
78 static_cast< OWeakObject
* >(
79 new FSStorage( aResultContent
,
80 embed::ElementModes::READWRITE
,
85 //-------------------------------------------------------------------------
86 uno::Reference
< uno::XInterface
> SAL_CALL
FSStorageFactory::createInstanceWithArguments(
87 const uno::Sequence
< uno::Any
>& aArguments
)
88 throw ( uno::Exception
,
89 uno::RuntimeException
)
91 // The request for storage can be done with up to three arguments
93 // The first argument specifies a source for the storage
95 // The second value is a mode the storage should be open in.
96 // And the third value is a media descriptor.
98 sal_Int32 nArgNum
= aArguments
.getLength();
99 OSL_ENSURE( nArgNum
< 4, "Wrong parameter number" );
102 return createInstance();
104 // first try to retrieve storage open mode if any
105 // by default the storage will be open in readonly mode
106 sal_Int32 nStorageMode
= embed::ElementModes::READ
;
109 if( !( aArguments
[1] >>= nStorageMode
) )
111 throw lang::IllegalArgumentException(
112 ("second argument to css.embed.FileSystemStorageFactory."
113 "createInstanceWithArguments must be a"
114 " css.embed.ElementModes"),
115 static_cast< OWeakObject
* >(this), -1);
117 // it's always possible to read written storage in this implementation
118 nStorageMode
|= embed::ElementModes::READ
;
121 // retrieve storage source URL
124 if ( !( aArguments
[0] >>= aURL
) || aURL
.isEmpty() )
126 throw lang::IllegalArgumentException(
128 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
129 " must be a (non-empty) URL"),
130 static_cast< OWeakObject
* >(this), -1);
133 // allow to use other ucp's
134 // if ( !isLocalNotFile_Impl( aURL ) )
135 if ( aURL
.equalsIgnoreAsciiCase("vnd.sun.star.pkg")
136 || aURL
.equalsIgnoreAsciiCase("vnd.sun.star.zip")
137 || ::utl::UCBContentHelper::IsDocument( aURL
) )
139 throw lang::IllegalArgumentException(
140 ("URL \"" + aURL
+ "\" passed as first argument to"
141 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
142 " must be a file URL denoting a directory"),
143 static_cast< OWeakObject
* >(this), -1);
146 if ( ( nStorageMode
& embed::ElementModes::WRITE
) && !( nStorageMode
& embed::ElementModes::NOCREATE
) )
147 FSStorage::MakeFolderNoUI( aURL
);
148 else if ( !::utl::UCBContentHelper::IsFolder( aURL
) )
149 throw io::IOException(
150 ("URL \"" + aURL
+ "\" passed to"
151 " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
152 " does not denote an existing directory"),
153 static_cast< OWeakObject
* >(this));
155 ::ucbhelper::Content
aResultContent(
156 aURL
, uno::Reference
< ucb::XCommandEnvironment
>(),
157 comphelper::getProcessComponentContext() );
159 // create storage based on source
160 return uno::Reference
< uno::XInterface
>(
161 static_cast< OWeakObject
* >( new FSStorage( aResultContent
,
167 //-------------------------------------------------------------------------
168 OUString SAL_CALL
FSStorageFactory::getImplementationName()
169 throw ( uno::RuntimeException
)
171 return impl_staticGetImplementationName();
174 //-------------------------------------------------------------------------
175 sal_Bool SAL_CALL
FSStorageFactory::supportsService( const OUString
& ServiceName
)
176 throw ( uno::RuntimeException
)
178 uno::Sequence
< OUString
> aSeq
= impl_staticGetSupportedServiceNames();
180 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
181 if ( ServiceName
== aSeq
[nInd
] )
187 //-------------------------------------------------------------------------
188 uno::Sequence
< OUString
> SAL_CALL
FSStorageFactory::getSupportedServiceNames()
189 throw ( uno::RuntimeException
)
191 return impl_staticGetSupportedServiceNames();
194 //-------------------------------------------------------------------------
198 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
fsstorage_component_getFactory (
199 const sal_Char
* pImplementationName
, void * pServiceManager
,
200 SAL_UNUSED_PARAMETER
void * /* pRegistryKey */)
205 uno::Reference
< lang::XSingleServiceFactory
> xFactory
;
206 if (FSStorageFactory::impl_staticGetImplementationName().compareToAscii (pImplementationName
) == 0)
208 xFactory
= cppu::createOneInstanceFactory (
209 reinterpret_cast< lang::XMultiServiceFactory
* >(pServiceManager
),
210 FSStorageFactory::impl_staticGetImplementationName(),
211 FSStorageFactory::impl_staticCreateSelfInstance
,
212 FSStorageFactory::impl_staticGetSupportedServiceNames() );
217 pResult
= xFactory
.get();
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */