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