Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / ftp / ftpcontentprovider.cxx
bloba7b281129e6207becb8fe8b006dbd5aa3dc0c336
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 /**************************************************************************
31 TODO
32 **************************************************************************
34 *************************************************************************/
36 #include <ucbhelper/contentbroker.hxx>
37 #include <osl/socket.hxx>
38 #include "ftpcontentprovider.hxx"
39 #include "ftpcontent.hxx"
40 #include "ftploaderthread.hxx"
43 using namespace ftp;
44 using namespace com::sun::star::lang;
45 using namespace com::sun::star::container;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::ucb;
48 using namespace com::sun::star::beans;
52 //=========================================================================
53 //=========================================================================
55 // ContentProvider Implementation.
57 //=========================================================================
58 //=========================================================================
60 FTPContentProvider::FTPContentProvider(
61 const Reference< XMultiServiceFactory >& rSMgr)
62 : ::ucbhelper::ContentProviderImplHelper(rSMgr),
63 m_ftpLoaderThread(0),
64 m_pProxyDecider(0)
68 //=========================================================================
69 // virtual
70 FTPContentProvider::~FTPContentProvider()
72 delete m_ftpLoaderThread;
73 delete m_pProxyDecider;
76 //=========================================================================
78 // XInterface methods.
80 //=========================================================================
82 XINTERFACE_IMPL_3(FTPContentProvider,
83 XTypeProvider,
84 XServiceInfo,
85 XContentProvider)
87 //=========================================================================
89 // XTypeProvider methods.
91 //=========================================================================
93 XTYPEPROVIDER_IMPL_3(FTPContentProvider,
94 XTypeProvider,
95 XServiceInfo,
96 XContentProvider)
98 //=========================================================================
100 // XServiceInfo methods.
102 //=========================================================================
104 XSERVICEINFO_IMPL_1(
105 FTPContentProvider,
106 rtl::OUString("com.sun.star.comp.FTPContentProvider"),
107 rtl::OUString(FTP_CONTENT_PROVIDER_SERVICE_NAME));
109 //=========================================================================
111 // Service factory implementation.
113 //=========================================================================
115 ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
118 //=========================================================================
120 // XContentProvider methods.
122 //=========================================================================
124 // virtual
125 Reference<XContent> SAL_CALL
126 FTPContentProvider::queryContent(
127 const Reference< XContentIdentifier >& xCanonicId
129 throw(
130 IllegalIdentifierException,
131 RuntimeException
134 // Check, if a content with given id already exists...
135 Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
136 if(xContent.is())
137 return xContent;
139 // A new content has to be returned:
141 // Initialize
142 osl::MutexGuard aGuard( m_aMutex );
143 if(!m_ftpLoaderThread || !m_pProxyDecider)
145 try {
146 init();
147 } catch( ... ) {
148 throw RuntimeException();
151 if(!m_ftpLoaderThread || !m_pProxyDecider)
152 throw RuntimeException();
156 try {
157 FTPURL aURL(xCanonicId->getContentIdentifier(),
158 this);
160 if(!m_pProxyDecider->shouldUseProxy(
161 rtl::OUString("ftp"),
162 aURL.host(),
163 aURL.port().toInt32()))
165 xContent = new FTPContent(m_xSMgr,this,xCanonicId,aURL);
166 registerNewContent(xContent);
168 else {
169 Reference<XContentProvider>
170 xProvider(getHttpProvider());
171 if(xProvider.is())
172 return xProvider->queryContent(xCanonicId);
173 else
174 throw RuntimeException();
176 } catch(const malformed_exception&) {
177 throw IllegalIdentifierException();
180 // may throw IllegalIdentifierException
181 return xContent;
187 void FTPContentProvider::init() {
188 m_ftpLoaderThread = new FTPLoaderThread();
189 m_pProxyDecider = new ucbhelper::InternetProxyDecider(m_xSMgr);
194 CURL* FTPContentProvider::handle() {
195 // Cannot be zero if called from here;
196 return m_ftpLoaderThread->handle();
200 bool FTPContentProvider::forHost(
201 const rtl::OUString& host,
202 const rtl::OUString& port,
203 const rtl::OUString& username,
204 rtl::OUString& password,
205 rtl::OUString& account)
207 osl::MutexGuard aGuard(m_aMutex);
208 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
209 if(host == m_ServerInfo[i].host &&
210 port == m_ServerInfo[i].port &&
211 username == m_ServerInfo[i].username )
213 password = m_ServerInfo[i].password;
214 account = m_ServerInfo[i].account;
215 return true;
218 return false;
222 bool FTPContentProvider::setHost(
223 const rtl::OUString& host,
224 const rtl::OUString& port,
225 const rtl::OUString& username,
226 const rtl::OUString& password,
227 const rtl::OUString& account)
229 ServerInfo inf;
230 inf.host = host;
231 inf.port = port;
232 inf.username = username;
233 inf.password = password;
234 inf.account = account;
236 bool present(false);
237 osl::MutexGuard aGuard(m_aMutex);
238 for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
239 if(host == m_ServerInfo[i].host &&
240 port == m_ServerInfo[i].port &&
241 username == m_ServerInfo[i].username)
243 present = true;
244 m_ServerInfo[i].password = password;
245 m_ServerInfo[i].account = account;
248 if(!present)
249 m_ServerInfo.push_back(inf);
251 return !present;
256 Reference<XContentProvider>
257 FTPContentProvider::getHttpProvider()
258 throw(RuntimeException)
260 // used for access to ftp-proxy
261 ucbhelper::ContentBroker *pBroker = ucbhelper::ContentBroker::get();
263 if(pBroker) {
264 Reference<XContentProviderManager > xManager(
265 pBroker->getContentProviderManagerInterface());
267 if(xManager.is())
268 return
269 xManager->queryContentProvider(
270 rtl::OUString("http:"));
271 else
272 throw RuntimeException(
273 rtl::OUString( "bad ucbhelper::ContentBroker"),
274 *this);
275 } else
276 return 0;
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */