Update ooo320-m1
[ooovba.git] / ucb / source / ucp / ftp / ftpcontentprovider.cxx
blob15fcf5378bfea949330d03ab60a30496d7e7f9e0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ftpcontentprovider.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 /**************************************************************************
35 TODO
36 **************************************************************************
38 *************************************************************************/
40 #include <ucbhelper/contentbroker.hxx>
41 #include <osl/socket.hxx>
42 #include "ftpcontentprovider.hxx"
43 #include "ftpcontent.hxx"
44 #include "ftploaderthread.hxx"
47 using namespace ftp;
48 using namespace com::sun::star::lang;
49 using namespace com::sun::star::container;
50 using namespace com::sun::star::uno;
51 using namespace com::sun::star::ucb;
52 using namespace com::sun::star::beans;
56 //=========================================================================
57 //=========================================================================
59 // ContentProvider Implementation.
61 //=========================================================================
62 //=========================================================================
64 FTPContentProvider::FTPContentProvider(
65 const Reference< XMultiServiceFactory >& rSMgr)
66 : ::ucbhelper::ContentProviderImplHelper(rSMgr),
67 m_ftpLoaderThread(0),
68 m_pProxyDecider(0)
72 //=========================================================================
73 // virtual
74 FTPContentProvider::~FTPContentProvider()
76 delete m_ftpLoaderThread;
77 delete m_pProxyDecider;
80 //=========================================================================
82 // XInterface methods.
84 //=========================================================================
86 XINTERFACE_IMPL_3(FTPContentProvider,
87 XTypeProvider,
88 XServiceInfo,
89 XContentProvider)
91 //=========================================================================
93 // XTypeProvider methods.
95 //=========================================================================
97 XTYPEPROVIDER_IMPL_3(FTPContentProvider,
98 XTypeProvider,
99 XServiceInfo,
100 XContentProvider)
102 //=========================================================================
104 // XServiceInfo methods.
106 //=========================================================================
108 XSERVICEINFO_IMPL_1(
109 FTPContentProvider,
110 rtl::OUString::createFromAscii("com.sun.star.comp.FTPContentProvider"),
111 rtl::OUString::createFromAscii(FTP_CONTENT_PROVIDER_SERVICE_NAME));
113 //=========================================================================
115 // Service factory implementation.
117 //=========================================================================
119 ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
122 //=========================================================================
124 // XContentProvider methods.
126 //=========================================================================
128 // virtual
129 Reference<XContent> SAL_CALL
130 FTPContentProvider::queryContent(
131 const Reference< XContentIdentifier >& xCanonicId
133 throw(
134 IllegalIdentifierException,
135 RuntimeException
138 // Check, if a content with given id already exists...
139 Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
140 if(xContent.is())
141 return xContent;
143 // A new content has to be returned:
145 // Initialize
146 osl::MutexGuard aGuard( m_aMutex );
147 if(!m_ftpLoaderThread || !m_pProxyDecider)
149 try {
150 init();
151 } catch( ... ) {
152 throw RuntimeException();
155 if(!m_ftpLoaderThread || !m_pProxyDecider)
156 throw RuntimeException();
160 try {
161 FTPURL aURL(xCanonicId->getContentIdentifier(),
162 this);
164 if(!m_pProxyDecider->shouldUseProxy(
165 rtl::OUString::createFromAscii("ftp"),
166 aURL.host(),
167 aURL.port().toInt32()))
169 xContent = new FTPContent(m_xSMgr,this,xCanonicId,aURL);
170 registerNewContent(xContent);
172 else {
173 Reference<XContentProvider>
174 xProvider(getHttpProvider());
175 if(xProvider.is())
176 return xProvider->queryContent(xCanonicId);
177 else
178 throw RuntimeException();
180 } catch(const malformed_exception&) {
181 throw IllegalIdentifierException();
184 // may throw IllegalIdentifierException
185 return xContent;
191 void FTPContentProvider::init() {
192 m_ftpLoaderThread = new FTPLoaderThread();
193 m_pProxyDecider = new ucbhelper::InternetProxyDecider(m_xSMgr);
198 CURL* FTPContentProvider::handle() {
199 // Cannot be zero if called from here;
200 return m_ftpLoaderThread->handle();
204 bool FTPContentProvider::forHost(
205 const rtl::OUString& host,
206 const rtl::OUString& port,
207 const rtl::OUString& username,
208 rtl::OUString& password,
209 rtl::OUString& account)
211 osl::MutexGuard aGuard(m_aMutex);
212 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
213 if(host == m_ServerInfo[i].host &&
214 port == m_ServerInfo[i].port &&
215 username == m_ServerInfo[i].username )
217 password = m_ServerInfo[i].password;
218 account = m_ServerInfo[i].account;
219 return true;
222 return false;
226 bool FTPContentProvider::setHost(
227 const rtl::OUString& host,
228 const rtl::OUString& port,
229 const rtl::OUString& username,
230 const rtl::OUString& password,
231 const rtl::OUString& account)
233 ServerInfo inf;
234 inf.host = host;
235 inf.port = port;
236 inf.username = username;
237 inf.password = password;
238 inf.account = account;
240 bool present(false);
241 osl::MutexGuard aGuard(m_aMutex);
242 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
243 if(host == m_ServerInfo[i].host &&
244 port == m_ServerInfo[i].port &&
245 username == m_ServerInfo[i].username)
247 present = true;
248 m_ServerInfo[i].password = password;
249 m_ServerInfo[i].account = account;
252 if(!present)
253 m_ServerInfo.push_back(inf);
255 return !present;
260 Reference<XContentProvider>
261 FTPContentProvider::getHttpProvider()
262 throw(RuntimeException)
264 // used for access to ftp-proxy
265 ucbhelper::ContentBroker *pBroker = ucbhelper::ContentBroker::get();
267 if(pBroker) {
268 Reference<XContentProviderManager > xManager(
269 pBroker->getContentProviderManagerInterface());
271 if(xManager.is())
272 return
273 xManager->queryContentProvider(
274 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("http:")));
275 else
276 throw RuntimeException(
277 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
278 "bad ucbhelper::ContentBroker")),
279 *this);
280 } else
281 return 0;