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 /**************************************************************************
23 **************************************************************************
25 *************************************************************************/
27 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
28 #include <comphelper/processfactory.hxx>
29 #include <osl/socket.hxx>
30 #include "ftpcontentprovider.hxx"
31 #include "ftpcontent.hxx"
32 #include "ftploaderthread.hxx"
35 using namespace com::sun::star::lang
;
36 using namespace com::sun::star::container
;
37 using namespace com::sun::star::uno
;
38 using namespace com::sun::star::ucb
;
39 using namespace com::sun::star::beans
;
41 // ContentProvider Implementation.
43 FTPContentProvider::FTPContentProvider( const Reference
< XComponentContext
>& rxContext
)
44 : ::ucbhelper::ContentProviderImplHelper(rxContext
)
45 , m_ftpLoaderThread(0)
52 FTPContentProvider::~FTPContentProvider()
54 delete m_ftpLoaderThread
;
55 delete m_pProxyDecider
;
58 // XInterface methods.
59 void SAL_CALL
FTPContentProvider::acquire()
62 OWeakObject::acquire();
65 void SAL_CALL
FTPContentProvider::release()
68 OWeakObject::release();
71 css::uno::Any SAL_CALL
FTPContentProvider::queryInterface( const css::uno::Type
& rType
)
72 throw( css::uno::RuntimeException
, std::exception
)
74 css::uno::Any aRet
= cppu::queryInterface( rType
,
75 (static_cast< XTypeProvider
* >(this)),
76 (static_cast< XServiceInfo
* >(this)),
77 (static_cast< XContentProvider
* >(this))
79 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
82 // XTypeProvider methods.
83 css::uno::Sequence
< sal_Int8
> SAL_CALL
FTPContentProvider::getImplementationId()
84 throw( css::uno::RuntimeException
,
87 return css::uno::Sequence
<sal_Int8
>();
90 css::uno::Sequence
< css::uno::Type
> SAL_CALL
FTPContentProvider::getTypes()
91 throw( css::uno::RuntimeException
,
94 static cppu::OTypeCollection
* pCollection
= NULL
;
97 osl::Guard
< osl::Mutex
> aGuard( osl::Mutex::getGlobalMutex() );
100 static cppu::OTypeCollection
collection(
101 cppu::UnoType
<XTypeProvider
>::get(),
102 cppu::UnoType
<XServiceInfo
>::get(),
103 cppu::UnoType
<XContentProvider
>::get()
105 pCollection
= &collection
;
108 return (*pCollection
).getTypes();
113 // XServiceInfo methods.
115 OUString SAL_CALL
FTPContentProvider::getImplementationName()
116 throw( css::uno::RuntimeException
, std::exception
)
118 return getImplementationName_Static();
121 OUString
FTPContentProvider::getImplementationName_Static()
123 return OUString("com.sun.star.comp.FTPContentProvider");
126 sal_Bool SAL_CALL
FTPContentProvider::supportsService( const OUString
& ServiceName
)
127 throw( css::uno::RuntimeException
, std::exception
)
129 return cppu::supportsService( this, ServiceName
);
132 css::uno::Sequence
< OUString
> SAL_CALL
FTPContentProvider::getSupportedServiceNames()
133 throw( css::uno::RuntimeException
, std::exception
)
135 return getSupportedServiceNames_Static();
138 static css::uno::Reference
< css::uno::XInterface
> SAL_CALL
139 FTPContentProvider_CreateInstance( const css::uno::Reference
<
140 css::lang::XMultiServiceFactory
> & rSMgr
)
141 throw( css::uno::Exception
)
143 css::lang::XServiceInfo
* pX
= (css::lang::XServiceInfo
*)
144 new FTPContentProvider( ucbhelper::getComponentContext(rSMgr
) );
145 return css::uno::Reference
< css::uno::XInterface
>::query( pX
);
148 css::uno::Sequence
< OUString
> FTPContentProvider::getSupportedServiceNames_Static()
150 css::uno::Sequence
< OUString
> aSNS( 1 );
151 aSNS
.getArray()[ 0 ] = FTP_CONTENT_PROVIDER_SERVICE_NAME
;
155 // Service factory implementation.
157 css::uno::Reference
< css::lang::XSingleServiceFactory
>
158 FTPContentProvider::createServiceFactory( const css::uno::Reference
<
159 css::lang::XMultiServiceFactory
>& rxServiceMgr
)
161 return css::uno::Reference
<
162 css::lang::XSingleServiceFactory
>(
163 cppu::createOneInstanceFactory(
165 FTPContentProvider::getImplementationName_Static(),
166 FTPContentProvider_CreateInstance
,
167 FTPContentProvider::getSupportedServiceNames_Static() ) );
170 // XContentProvider methods.
173 Reference
<XContent
> SAL_CALL
FTPContentProvider::queryContent(
174 const Reference
< XContentIdentifier
>& xCanonicId
)
175 throw( IllegalIdentifierException
,
179 // Check, if a content with given id already exists...
180 Reference
<XContent
> xContent
= queryExistingContent(xCanonicId
).get();
184 // A new content has to be returned:
187 osl::MutexGuard
aGuard( m_aMutex
);
188 if(!m_ftpLoaderThread
|| !m_pProxyDecider
)
193 throw RuntimeException();
196 if(!m_ftpLoaderThread
|| !m_pProxyDecider
)
197 throw RuntimeException();
202 FTPURL
aURL(xCanonicId
->getContentIdentifier(),
205 if(!m_pProxyDecider
->shouldUseProxy(
208 aURL
.port().toInt32()))
210 xContent
= new FTPContent( m_xContext
, this,xCanonicId
,aURL
);
211 registerNewContent(xContent
);
214 Reference
<XContentProvider
>
215 xProvider(getHttpProvider());
217 return xProvider
->queryContent(xCanonicId
);
219 throw RuntimeException();
221 } catch(const malformed_exception
&) {
222 throw IllegalIdentifierException();
225 // may throw IllegalIdentifierException
229 void FTPContentProvider::init()
231 m_ftpLoaderThread
= new FTPLoaderThread();
232 m_pProxyDecider
= new ucbhelper::InternetProxyDecider( m_xContext
);
235 CURL
* FTPContentProvider::handle()
237 // Cannot be zero if called from here;
238 return m_ftpLoaderThread
->handle();
242 bool FTPContentProvider::forHost( const OUString
& host
,
243 const OUString
& port
,
244 const OUString
& username
,
248 osl::MutexGuard
aGuard(m_aMutex
);
249 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
250 if(host
== m_ServerInfo
[i
].host
&&
251 port
== m_ServerInfo
[i
].port
&&
252 username
== m_ServerInfo
[i
].username
)
254 password
= m_ServerInfo
[i
].password
;
255 account
= m_ServerInfo
[i
].account
;
262 bool FTPContentProvider::setHost( const OUString
& host
,
263 const OUString
& port
,
264 const OUString
& username
,
265 const OUString
& password
,
266 const OUString
& account
)
271 inf
.username
= username
;
272 inf
.password
= password
;
273 inf
.account
= account
;
276 osl::MutexGuard
aGuard(m_aMutex
);
277 for(unsigned int i
= 0; i
< m_ServerInfo
.size(); ++i
)
278 if(host
== m_ServerInfo
[i
].host
&&
279 port
== m_ServerInfo
[i
].port
&&
280 username
== m_ServerInfo
[i
].username
)
283 m_ServerInfo
[i
].password
= password
;
284 m_ServerInfo
[i
].account
= account
;
288 m_ServerInfo
.push_back(inf
);
295 Reference
<XContentProvider
> FTPContentProvider::getHttpProvider()
296 throw(RuntimeException
)
298 // used for access to ftp-proxy
299 return UniversalContentBroker::create( m_xContext
)->queryContentProvider("http:");
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */