xref: /trunk/main/svl/source/fsstor/fsfactory.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 
27 #include "fsfactory.hxx"
28 #include "cppuhelper/factory.hxx"
29 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
30 #include <com/sun/star/embed/ElementModes.hpp>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include <com/sun/star/io/XSeekable.hpp>
33 
34 
35 #include <ucbhelper/fileidentifierconverter.hxx>
36 #include <ucbhelper/contentbroker.hxx>
37 #include <ucbhelper/content.hxx>
38 
39 #include <unotools/tempfile.hxx>
40 #include <unotools/ucbhelper.hxx>
41 
42 #include "fsstorage.hxx"
43 
44 
45 using namespace ::com::sun::star;
46 
47 //-------------------------------------------------------------------------
impl_staticGetSupportedServiceNames()48 uno::Sequence< ::rtl::OUString > SAL_CALL FSStorageFactory::impl_staticGetSupportedServiceNames()
49 {
50     uno::Sequence< ::rtl::OUString > aRet(2);
51     aRet[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.FileSystemStorageFactory");
52     aRet[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.FileSystemStorageFactory");
53     return aRet;
54 }
55 
56 //-------------------------------------------------------------------------
impl_staticGetImplementationName()57 ::rtl::OUString SAL_CALL FSStorageFactory::impl_staticGetImplementationName()
58 {
59     return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.FileSystemStorageFactory");
60 }
61 
62 //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)63 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::impl_staticCreateSelfInstance(
64             const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
65 {
66     return uno::Reference< uno::XInterface >( *new FSStorageFactory( xServiceManager ) );
67 }
68 
69 //-------------------------------------------------------------------------
createInstance()70 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
71 {
72     ::rtl::OUString aTempURL;
73 
74     aTempURL = ::utl::TempFile( NULL, sal_True ).GetURL();
75 
76     if ( !aTempURL.getLength() )
77         throw uno::RuntimeException(); // TODO: can not create tempfile
78 
79     ::ucbhelper::Content aResultContent(
80         aTempURL, uno::Reference< ucb::XCommandEnvironment >() );
81 
82     return uno::Reference< uno::XInterface >(
83         static_cast< OWeakObject* >(
84             new FSStorage(  aResultContent,
85                             embed::ElementModes::READWRITE,
86                             uno::Sequence< beans::PropertyValue >(),
87                             m_xFactory ) ),
88         uno::UNO_QUERY );
89 }
90 
91 //-------------------------------------------------------------------------
createInstanceWithArguments(const uno::Sequence<uno::Any> & aArguments)92 uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
93             const uno::Sequence< uno::Any >& aArguments )
94 {
95     // The request for storage can be done with up to three arguments
96 
97     // The first argument specifies a source for the storage
98     // it must be URL.
99     // The second value is a mode the storage should be open in.
100     // And the third value is a media descriptor.
101 
102     sal_Int32 nArgNum = aArguments.getLength();
103     OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
104 
105     if ( !nArgNum )
106         return createInstance();
107 
108     // first try to retrieve storage open mode if any
109     // by default the storage will be open in readonly mode
110     sal_Int32 nStorageMode = embed::ElementModes::READ;
111     if ( nArgNum >= 2 )
112     {
113         if( !( aArguments[1] >>= nStorageMode ) )
114         {
115             OSL_ENSURE( sal_False, "Wrong second argument!\n" );
116             throw uno::Exception(); // TODO: Illegal argument
117         }
118         // it's always possible to read written storage in this implementation
119         nStorageMode |= embed::ElementModes::READ;
120     }
121 
122     // retrieve storage source URL
123     ::rtl::OUString aURL;
124 
125     if ( aArguments[0] >>= aURL )
126     {
127         if ( !aURL.getLength() )
128         {
129             OSL_ENSURE( sal_False, "Empty URL is provided!\n" );
130             throw uno::Exception(); // TODO: illegal argument
131         }
132     }
133     else
134     {
135         OSL_ENSURE( sal_False, "Wrong first argument!\n" );
136         throw uno::Exception(); // TODO: Illegal argument
137     }
138 
139     // retrieve mediadescriptor and set storage properties
140     uno::Sequence< beans::PropertyValue > aDescr;
141     uno::Sequence< beans::PropertyValue > aPropsToSet;
142 
143     if ( nArgNum >= 3 )
144     {
145         if( aArguments[2] >>= aDescr )
146         {
147             aPropsToSet.realloc(1);
148             aPropsToSet[0].Name = ::rtl::OUString::createFromAscii( "URL" );
149             aPropsToSet[0].Value <<= aURL;
150 
151             for ( sal_Int32 nInd = 0, nNumArgs = 1; nInd < aDescr.getLength(); nInd++ )
152             {
153                 if ( aDescr[nInd].Name.equalsAscii( "InteractionHandler" ) )
154                 {
155                     aPropsToSet.realloc( ++nNumArgs );
156                     aPropsToSet[nNumArgs-1].Name = aDescr[nInd].Name;
157                     aPropsToSet[nNumArgs-1].Value = aDescr[nInd].Value;
158                     break;
159                 }
160                 else
161                     OSL_ENSURE( sal_False, "Unacceptable property, will be ignored!\n" );
162             }
163         }
164         else
165         {
166             OSL_ENSURE( sal_False, "Wrong third argument!\n" );
167             throw uno::Exception(); // TODO: Illegal argument
168         }
169     }
170 
171     // allow to use other ucp's
172     // if ( !isLocalNotFile_Impl( aURL ) )
173     if ( aURL.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.pkg", 16 )
174       || aURL.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.zip", 16 )
175       || ::utl::UCBContentHelper::IsDocument( aURL ) )
176     {
177         OSL_ENSURE( sal_False, "File system storages can be based only on file URLs!\n" ); // ???
178         throw uno::Exception(); // TODO: illegal argument
179     }
180 
181     if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
182         FSStorage::MakeFolderNoUI( aURL, sal_False );
183     else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
184         throw io::IOException(); // there is no such folder
185 
186     ::ucbhelper::Content aResultContent(
187         aURL, uno::Reference< ucb::XCommandEnvironment >() );
188 
189     // create storage based on source
190     return uno::Reference< uno::XInterface >(
191         static_cast< OWeakObject* >( new FSStorage( aResultContent,
192                                                     nStorageMode,
193                                                     aPropsToSet,
194                                                     m_xFactory ) ),
195         uno::UNO_QUERY );
196 }
197 
198 //-------------------------------------------------------------------------
getImplementationName()199 ::rtl::OUString SAL_CALL FSStorageFactory::getImplementationName()
200 {
201     return impl_staticGetImplementationName();
202 }
203 
204 //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)205 sal_Bool SAL_CALL FSStorageFactory::supportsService( const ::rtl::OUString& ServiceName )
206 {
207     uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
208 
209     for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
210         if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
211             return sal_True;
212 
213     return sal_False;
214 }
215 
216 //-------------------------------------------------------------------------
getSupportedServiceNames()217 uno::Sequence< ::rtl::OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
218 {
219     return impl_staticGetSupportedServiceNames();
220 }
221 
222 //-------------------------------------------------------------------------
223 
224 extern "C"
225 {
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)226 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment (
227     const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */)
228 {
229     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
230 }
231 
component_getFactory(const sal_Char * pImplementationName,void * pServiceManager,void *)232 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory (
233     const sal_Char * pImplementationName, void * pServiceManager, void * /* pRegistryKey */)
234 {
235     void * pResult = 0;
236     if (pServiceManager)
237     {
238         uno::Reference< lang::XSingleServiceFactory > xFactory;
239         if (FSStorageFactory::impl_staticGetImplementationName().compareToAscii (pImplementationName) == 0)
240         {
241             xFactory = cppu::createOneInstanceFactory (
242                 reinterpret_cast< lang::XMultiServiceFactory* >(pServiceManager),
243                 FSStorageFactory::impl_staticGetImplementationName(),
244                 FSStorageFactory::impl_staticCreateSelfInstance,
245                 FSStorageFactory::impl_staticGetSupportedServiceNames() );
246         }
247         if (xFactory.is())
248         {
249             xFactory->acquire();
250             pResult = xFactory.get();
251         }
252     }
253     return pResult;
254 }
255 
256 } // extern "C"
257