Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / ftp / ftpcontentprovider.cxx
blob0f28bcb3bc4e2cf1fbb045a622f9f9cfc41d530c
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 .
21 /**************************************************************************
22 TODO
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 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;
44 //=========================================================================
45 //=========================================================================
47 // ContentProvider Implementation.
49 //=========================================================================
50 //=========================================================================
52 FTPContentProvider::FTPContentProvider(
53 const Reference< XComponentContext >& rxContext)
54 : ::ucbhelper::ContentProviderImplHelper(rxContext),
55 m_ftpLoaderThread(0),
56 m_pProxyDecider(0)
60 //=========================================================================
61 // virtual
62 FTPContentProvider::~FTPContentProvider()
64 delete m_ftpLoaderThread;
65 delete m_pProxyDecider;
68 //=========================================================================
70 // XInterface methods.
72 //=========================================================================
74 XINTERFACE_IMPL_3(FTPContentProvider,
75 XTypeProvider,
76 XServiceInfo,
77 XContentProvider)
79 //=========================================================================
81 // XTypeProvider methods.
83 //=========================================================================
85 XTYPEPROVIDER_IMPL_3(FTPContentProvider,
86 XTypeProvider,
87 XServiceInfo,
88 XContentProvider)
90 //=========================================================================
92 // XServiceInfo methods.
94 //=========================================================================
96 XSERVICEINFO_IMPL_1_CTX(
97 FTPContentProvider,
98 rtl::OUString("com.sun.star.comp.FTPContentProvider"),
99 rtl::OUString(FTP_CONTENT_PROVIDER_SERVICE_NAME));
101 //=========================================================================
103 // Service factory implementation.
105 //=========================================================================
107 ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
110 //=========================================================================
112 // XContentProvider methods.
114 //=========================================================================
116 // virtual
117 Reference<XContent> SAL_CALL
118 FTPContentProvider::queryContent(
119 const Reference< XContentIdentifier >& xCanonicId
121 throw(
122 IllegalIdentifierException,
123 RuntimeException
126 // Check, if a content with given id already exists...
127 Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
128 if(xContent.is())
129 return xContent;
131 // A new content has to be returned:
133 // Initialize
134 osl::MutexGuard aGuard( m_aMutex );
135 if(!m_ftpLoaderThread || !m_pProxyDecider)
137 try {
138 init();
139 } catch( ... ) {
140 throw RuntimeException();
143 if(!m_ftpLoaderThread || !m_pProxyDecider)
144 throw RuntimeException();
148 try {
149 FTPURL aURL(xCanonicId->getContentIdentifier(),
150 this);
152 if(!m_pProxyDecider->shouldUseProxy(
153 rtl::OUString("ftp"),
154 aURL.host(),
155 aURL.port().toInt32()))
157 xContent = new FTPContent( m_xContext, this,xCanonicId,aURL);
158 registerNewContent(xContent);
160 else {
161 Reference<XContentProvider>
162 xProvider(getHttpProvider());
163 if(xProvider.is())
164 return xProvider->queryContent(xCanonicId);
165 else
166 throw RuntimeException();
168 } catch(const malformed_exception&) {
169 throw IllegalIdentifierException();
172 // may throw IllegalIdentifierException
173 return xContent;
179 void FTPContentProvider::init() {
180 m_ftpLoaderThread = new FTPLoaderThread();
181 m_pProxyDecider = new ucbhelper::InternetProxyDecider( m_xContext );
186 CURL* FTPContentProvider::handle() {
187 // Cannot be zero if called from here;
188 return m_ftpLoaderThread->handle();
192 bool FTPContentProvider::forHost(
193 const rtl::OUString& host,
194 const rtl::OUString& port,
195 const rtl::OUString& username,
196 rtl::OUString& password,
197 rtl::OUString& account)
199 osl::MutexGuard aGuard(m_aMutex);
200 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
201 if(host == m_ServerInfo[i].host &&
202 port == m_ServerInfo[i].port &&
203 username == m_ServerInfo[i].username )
205 password = m_ServerInfo[i].password;
206 account = m_ServerInfo[i].account;
207 return true;
210 return false;
214 bool FTPContentProvider::setHost(
215 const rtl::OUString& host,
216 const rtl::OUString& port,
217 const rtl::OUString& username,
218 const rtl::OUString& password,
219 const rtl::OUString& account)
221 ServerInfo inf;
222 inf.host = host;
223 inf.port = port;
224 inf.username = username;
225 inf.password = password;
226 inf.account = account;
228 bool present(false);
229 osl::MutexGuard aGuard(m_aMutex);
230 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
231 if(host == m_ServerInfo[i].host &&
232 port == m_ServerInfo[i].port &&
233 username == m_ServerInfo[i].username)
235 present = true;
236 m_ServerInfo[i].password = password;
237 m_ServerInfo[i].account = account;
240 if(!present)
241 m_ServerInfo.push_back(inf);
243 return !present;
248 Reference<XContentProvider>
249 FTPContentProvider::getHttpProvider()
250 throw(RuntimeException)
252 // used for access to ftp-proxy
253 return UniversalContentBroker::create( m_xContext )->queryContentProvider("http:");
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */