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 9 : uno::Sequence< ::rtl::OUString > SAL_CALL FSStorageFactory::impl_staticGetSupportedServiceNames()
41 : {
42 9 : uno::Sequence< ::rtl::OUString > aRet(2);
43 9 : aRet[0] = ::rtl::OUString("com.sun.star.embed.FileSystemStorageFactory");
44 9 : aRet[1] = ::rtl::OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
45 9 : return aRet;
46 : }
47 :
48 : //-------------------------------------------------------------------------
49 18 : ::rtl::OUString SAL_CALL FSStorageFactory::impl_staticGetImplementationName()
50 : {
51 18 : return ::rtl::OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
52 : }
53 :
54 : //-------------------------------------------------------------------------
55 9 : uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::impl_staticCreateSelfInstance(
56 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
57 : {
58 9 : return uno::Reference< uno::XInterface >( *new FSStorageFactory( xServiceManager ) );
59 : }
60 :
61 : //-------------------------------------------------------------------------
62 0 : uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
63 : throw ( uno::Exception,
64 : uno::RuntimeException )
65 : {
66 0 : ::rtl::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_xFactory ) ),
82 0 : uno::UNO_QUERY );
83 : }
84 :
85 : //-------------------------------------------------------------------------
86 18 : 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 18 : sal_Int32 nArgNum = aArguments.getLength();
99 : OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
100 :
101 18 : 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 18 : sal_Int32 nStorageMode = embed::ElementModes::READ;
107 18 : if ( nArgNum >= 2 )
108 : {
109 18 : if( !( aArguments[1] >>= nStorageMode ) )
110 : {
111 : OSL_FAIL( "Wrong second argument!\n" );
112 0 : throw uno::Exception(); // TODO: Illegal argument
113 : }
114 : // it's allways possible to read written storage in this implementation
115 18 : nStorageMode |= embed::ElementModes::READ;
116 : }
117 :
118 : // retrieve storage source URL
119 18 : ::rtl::OUString aURL;
120 :
121 18 : if ( aArguments[0] >>= aURL )
122 : {
123 18 : if ( aURL.isEmpty() )
124 : {
125 : OSL_FAIL( "Empty URL is provided!\n" );
126 0 : throw uno::Exception(); // TODO: illegal argument
127 : }
128 : }
129 : else
130 : {
131 : OSL_FAIL( "Wrong first argument!\n" );
132 0 : throw uno::Exception(); // TODO: Illegal argument
133 : }
134 :
135 : // allow to use other ucp's
136 : // if ( !isLocalNotFile_Impl( aURL ) )
137 54 : if ( aURL.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.pkg"))
138 18 : || aURL.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.zip"))
139 18 : || ::utl::UCBContentHelper::IsDocument( aURL ) )
140 : {
141 : OSL_FAIL( "File system storages can be based only on file URLs!\n" ); // ???
142 0 : throw uno::Exception(); // TODO: illegal argument
143 : }
144 :
145 18 : if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
146 9 : FSStorage::MakeFolderNoUI( aURL );
147 9 : else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
148 0 : throw io::IOException(); // there is no such folder
149 :
150 : ::ucbhelper::Content aResultContent(
151 : aURL, uno::Reference< ucb::XCommandEnvironment >(),
152 18 : comphelper::getProcessComponentContext() );
153 :
154 : // create storage based on source
155 : return uno::Reference< uno::XInterface >(
156 : static_cast< OWeakObject* >( new FSStorage( aResultContent,
157 : nStorageMode,
158 36 : m_xFactory ) ),
159 36 : uno::UNO_QUERY );
160 : }
161 :
162 : //-------------------------------------------------------------------------
163 0 : ::rtl::OUString SAL_CALL FSStorageFactory::getImplementationName()
164 : throw ( uno::RuntimeException )
165 : {
166 0 : return impl_staticGetImplementationName();
167 : }
168 :
169 : //-------------------------------------------------------------------------
170 0 : sal_Bool SAL_CALL FSStorageFactory::supportsService( const ::rtl::OUString& ServiceName )
171 : throw ( uno::RuntimeException )
172 : {
173 0 : uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
174 :
175 0 : for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
176 0 : if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
177 0 : return sal_True;
178 :
179 0 : return sal_False;
180 : }
181 :
182 : //-------------------------------------------------------------------------
183 0 : uno::Sequence< ::rtl::OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
184 : throw ( uno::RuntimeException )
185 : {
186 0 : return impl_staticGetSupportedServiceNames();
187 : }
188 :
189 : //-------------------------------------------------------------------------
190 :
191 : extern "C"
192 : {
193 9 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL fsstorage_component_getFactory (
194 : const sal_Char * pImplementationName, void * pServiceManager,
195 : SAL_UNUSED_PARAMETER void * /* pRegistryKey */)
196 : {
197 9 : void * pResult = 0;
198 9 : if (pServiceManager)
199 : {
200 9 : uno::Reference< lang::XSingleServiceFactory > xFactory;
201 9 : if (FSStorageFactory::impl_staticGetImplementationName().compareToAscii (pImplementationName) == 0)
202 : {
203 : xFactory = cppu::createOneInstanceFactory (
204 : reinterpret_cast< lang::XMultiServiceFactory* >(pServiceManager),
205 : FSStorageFactory::impl_staticGetImplementationName(),
206 : FSStorageFactory::impl_staticCreateSelfInstance,
207 9 : FSStorageFactory::impl_staticGetSupportedServiceNames() );
208 : }
209 9 : if (xFactory.is())
210 : {
211 9 : xFactory->acquire();
212 9 : pResult = xFactory.get();
213 9 : }
214 : }
215 9 : return pResult;
216 : }
217 :
218 : } // extern "C"
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|