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 .
20 #include <sal/config.h>
22 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
23 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
24 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
25 #include <cppuhelper/exc_hlp.hxx>
26 #include <cppuhelper/queryinterface.hxx>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <cppuhelper/weak.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
)
50 FTPContentProvider::~FTPContentProvider()
52 m_ftpLoaderThread
.reset();
53 m_pProxyDecider
.reset();
56 // XInterface methods.
57 void SAL_CALL
FTPContentProvider::acquire()
60 OWeakObject::acquire();
63 void SAL_CALL
FTPContentProvider::release()
66 OWeakObject::release();
69 css::uno::Any SAL_CALL
FTPContentProvider::queryInterface( const css::uno::Type
& rType
)
71 css::uno::Any aRet
= cppu::queryInterface( rType
,
72 static_cast< XTypeProvider
* >(this),
73 static_cast< XServiceInfo
* >(this),
74 static_cast< XContentProvider
* >(this)
76 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
);
79 // XTypeProvider methods.
80 css::uno::Sequence
< sal_Int8
> SAL_CALL
FTPContentProvider::getImplementationId()
82 return css::uno::Sequence
<sal_Int8
>();
85 css::uno::Sequence
< css::uno::Type
> SAL_CALL
FTPContentProvider::getTypes()
87 static cppu::OTypeCollection
s_aCollection(
88 cppu::UnoType
<XTypeProvider
>::get(),
89 cppu::UnoType
<XServiceInfo
>::get(),
90 cppu::UnoType
<XContentProvider
>::get()
93 return s_aCollection
.getTypes();
97 // XServiceInfo methods.
99 OUString SAL_CALL
FTPContentProvider::getImplementationName()
101 return "com.sun.star.comp.FTPContentProvider";
104 sal_Bool SAL_CALL
FTPContentProvider::supportsService( const OUString
& ServiceName
)
106 return cppu::supportsService( this, ServiceName
);
109 css::uno::Sequence
< OUString
> SAL_CALL
FTPContentProvider::getSupportedServiceNames()
111 return { FTP_CONTENT_PROVIDER_SERVICE_NAME
};
116 // XContentProvider methods.
119 Reference
<XContent
> SAL_CALL
FTPContentProvider::queryContent(
120 const Reference
< XContentIdentifier
>& xCanonicId
)
122 // Check, if a content with given id already exists...
123 Reference
<XContent
> xContent
= queryExistingContent(xCanonicId
);
127 // A new content has to be returned:
130 osl::MutexGuard
aGuard( m_aMutex
);
131 if(!m_ftpLoaderThread
|| !m_pProxyDecider
)
135 } catch (css::uno::Exception
const & ex
) {
136 css::uno::Any anyEx
= cppu::getCaughtException();
137 throw css::lang::WrappedTargetRuntimeException( ex
.Message
,
138 css::uno::Reference
< css::uno::XInterface
>(),
141 throw RuntimeException();
144 if(!m_ftpLoaderThread
|| !m_pProxyDecider
)
145 throw RuntimeException();
150 FTPURL
aURL(xCanonicId
->getContentIdentifier(),
153 if(!m_pProxyDecider
->shouldUseProxy(
156 aURL
.port().toInt32()))
158 xContent
= new FTPContent( m_xContext
, this,xCanonicId
,aURL
);
159 registerNewContent(xContent
);
162 Reference
<XContentProvider
> xProvider(UniversalContentBroker::create( m_xContext
)->queryContentProvider("http:"));
164 throw RuntimeException();
165 return xProvider
->queryContent(xCanonicId
);
167 } catch(const malformed_exception
&) {
168 throw IllegalIdentifierException();
171 // may throw IllegalIdentifierException
175 void FTPContentProvider::init()
177 m_ftpLoaderThread
.reset( new FTPLoaderThread() );
178 m_pProxyDecider
.reset( new ucbhelper::InternetProxyDecider( m_xContext
) );
181 CURL
* FTPContentProvider::handle()
183 // Cannot be zero if called from here;
184 return m_ftpLoaderThread
->handle();
188 void FTPContentProvider::forHost( std::u16string_view host
,
189 std::u16string_view port
,
190 std::u16string_view username
,
194 osl::MutexGuard
aGuard(m_aMutex
);
195 for(const ServerInfo
& i
: m_ServerInfo
)
198 username
== i
.username
)
200 password
= i
.password
;
206 bool FTPContentProvider::setHost( const OUString
& host
,
207 const OUString
& port
,
208 const OUString
& username
,
209 const OUString
& password
,
210 const OUString
& account
)
215 inf
.username
= username
;
216 inf
.password
= password
;
217 inf
.account
= account
;
220 osl::MutexGuard
aGuard(m_aMutex
);
221 for(ServerInfo
& i
: m_ServerInfo
)
224 username
== i
.username
)
227 i
.password
= password
;
232 m_ServerInfo
.push_back(inf
);
237 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
238 ucb_ftp_FTPContentProvider_get_implementation(
239 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
241 return cppu::acquire(new FTPContentProvider(context
));
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */