bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / ftp / ftpcontentprovider.cxx
blob7a237da552586ad95eb52877951f220cf05f3fa0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/factory.hxx>
30 #include <cppuhelper/weak.hxx>
31 #include "ftpcontentprovider.hxx"
32 #include "ftpcontent.hxx"
33 #include "ftploaderthread.hxx"
35 using namespace ftp;
36 using namespace com::sun::star::lang;
37 using namespace com::sun::star::container;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::ucb;
40 using namespace com::sun::star::beans;
42 // ContentProvider Implementation.
44 FTPContentProvider::FTPContentProvider( const Reference< XComponentContext >& rxContext)
45 : ::ucbhelper::ContentProviderImplHelper(rxContext)
50 // virtual
51 FTPContentProvider::~FTPContentProvider()
53 m_ftpLoaderThread.reset();
54 m_pProxyDecider.reset();
57 // XInterface methods.
58 void SAL_CALL FTPContentProvider::acquire()
59 noexcept
61 OWeakObject::acquire();
64 void SAL_CALL FTPContentProvider::release()
65 noexcept
67 OWeakObject::release();
70 css::uno::Any SAL_CALL FTPContentProvider::queryInterface( const css::uno::Type & rType )
72 css::uno::Any aRet = cppu::queryInterface( rType,
73 static_cast< XTypeProvider* >(this),
74 static_cast< XServiceInfo* >(this),
75 static_cast< XContentProvider* >(this)
77 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
80 // XTypeProvider methods.
81 css::uno::Sequence< sal_Int8 > SAL_CALL FTPContentProvider::getImplementationId()
83 return css::uno::Sequence<sal_Int8>();
86 css::uno::Sequence< css::uno::Type > SAL_CALL FTPContentProvider::getTypes()
88 static cppu::OTypeCollection s_aCollection(
89 cppu::UnoType<XTypeProvider>::get(),
90 cppu::UnoType<XServiceInfo>::get(),
91 cppu::UnoType<XContentProvider>::get()
94 return s_aCollection.getTypes();
98 // XServiceInfo methods.
100 OUString SAL_CALL FTPContentProvider::getImplementationName()
102 return "com.sun.star.comp.FTPContentProvider";
105 sal_Bool SAL_CALL FTPContentProvider::supportsService( const OUString& ServiceName )
107 return cppu::supportsService( this, ServiceName );
110 css::uno::Sequence< OUString > SAL_CALL FTPContentProvider::getSupportedServiceNames()
112 return { FTP_CONTENT_PROVIDER_SERVICE_NAME };
117 // XContentProvider methods.
119 // virtual
120 Reference<XContent> SAL_CALL FTPContentProvider::queryContent(
121 const Reference< XContentIdentifier >& xCanonicId)
123 // Check, if a content with given id already exists...
124 Reference<XContent> xContent = queryExistingContent(xCanonicId);
125 if(xContent.is())
126 return xContent;
128 // A new content has to be returned:
130 // Initialize
131 osl::MutexGuard aGuard( m_aMutex );
132 if(!m_ftpLoaderThread || !m_pProxyDecider)
134 try {
135 init();
136 } catch (css::uno::Exception const & ex) {
137 css::uno::Any anyEx = cppu::getCaughtException();
138 throw css::lang::WrappedTargetRuntimeException( ex.Message,
139 css::uno::Reference< css::uno::XInterface >(),
140 anyEx );
141 } catch( ... ) {
142 throw RuntimeException();
145 if(!m_ftpLoaderThread || !m_pProxyDecider)
146 throw RuntimeException();
150 try {
151 FTPURL aURL(xCanonicId->getContentIdentifier(),
152 this);
154 if(!m_pProxyDecider->shouldUseProxy(
155 "ftp",
156 aURL.host(),
157 aURL.port().toInt32()))
159 xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
160 registerNewContent(xContent);
162 else {
163 Reference<XContentProvider> xProvider(UniversalContentBroker::create( m_xContext )->queryContentProvider("http:"));
164 if(!xProvider.is())
165 throw RuntimeException();
166 return xProvider->queryContent(xCanonicId);
168 } catch(const malformed_exception&) {
169 throw IllegalIdentifierException();
172 // may throw IllegalIdentifierException
173 return xContent;
176 void FTPContentProvider::init()
178 m_ftpLoaderThread.reset( new FTPLoaderThread() );
179 m_pProxyDecider.reset( new ucbhelper::InternetProxyDecider( m_xContext ) );
182 CURL* FTPContentProvider::handle()
184 // Cannot be zero if called from here;
185 return m_ftpLoaderThread->handle();
189 void FTPContentProvider::forHost( std::u16string_view host,
190 std::u16string_view port,
191 std::u16string_view username,
192 OUString& password,
193 OUString& account)
195 osl::MutexGuard aGuard(m_aMutex);
196 for(const ServerInfo & i : m_ServerInfo)
197 if(host == i.host &&
198 port == i.port &&
199 username == i.username )
201 password = i.password;
202 account = i.account;
203 return;
207 bool FTPContentProvider::setHost( const OUString& host,
208 const OUString& port,
209 const OUString& username,
210 const OUString& password,
211 const OUString& account)
213 ServerInfo inf;
214 inf.host = host;
215 inf.port = port;
216 inf.username = username;
217 inf.password = password;
218 inf.account = account;
220 bool present(false);
221 osl::MutexGuard aGuard(m_aMutex);
222 for(ServerInfo & i : m_ServerInfo)
223 if(host == i.host &&
224 port == i.port &&
225 username == i.username)
227 present = true;
228 i.password = password;
229 i.account = account;
232 if(!present)
233 m_ServerInfo.push_back(inf);
235 return !present;
238 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
239 ucb_ftp_FTPContentProvider_get_implementation(
240 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
242 return cppu::acquire(new FTPContentProvider(context));
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */