Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : /**************************************************************************
31 : : TODO
32 : : **************************************************************************
33 : :
34 : : *************************************************************************/
35 : :
36 : : #include <ucbhelper/contentbroker.hxx>
37 : : #include <osl/socket.hxx>
38 : : #include "ftpcontentprovider.hxx"
39 : : #include "ftpcontent.hxx"
40 : : #include "ftploaderthread.hxx"
41 : :
42 : :
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;
49 : :
50 : :
51 : :
52 : : //=========================================================================
53 : : //=========================================================================
54 : : //
55 : : // ContentProvider Implementation.
56 : : //
57 : : //=========================================================================
58 : : //=========================================================================
59 : :
60 : 4 : FTPContentProvider::FTPContentProvider(
61 : : const Reference< XMultiServiceFactory >& rSMgr)
62 : : : ::ucbhelper::ContentProviderImplHelper(rSMgr),
63 : : m_ftpLoaderThread(0),
64 [ + - ][ + - ]: 4 : m_pProxyDecider(0)
65 : : {
66 : 4 : }
67 : :
68 : : //=========================================================================
69 : : // virtual
70 [ + - ]: 4 : FTPContentProvider::~FTPContentProvider()
71 : : {
72 [ + - ][ + - ]: 4 : delete m_ftpLoaderThread;
73 [ + - ][ + - ]: 4 : delete m_pProxyDecider;
74 [ - + ]: 8 : }
75 : :
76 : : //=========================================================================
77 : : //
78 : : // XInterface methods.
79 : : //
80 : : //=========================================================================
81 : :
82 [ + - ][ + + ]: 230 : XINTERFACE_IMPL_3(FTPContentProvider,
[ + - ]
83 : : XTypeProvider,
84 : : XServiceInfo,
85 : : XContentProvider)
86 : :
87 : : //=========================================================================
88 : : //
89 : : // XTypeProvider methods.
90 : : //
91 : : //=========================================================================
92 : :
93 [ + - ][ + - ]: 2 : XTYPEPROVIDER_IMPL_3(FTPContentProvider,
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
94 : : XTypeProvider,
95 : : XServiceInfo,
96 : : XContentProvider)
97 : :
98 : : //=========================================================================
99 : : //
100 : : // XServiceInfo methods.
101 : : //
102 : : //=========================================================================
103 : :
104 [ + - ][ + - ]: 20 : XSERVICEINFO_IMPL_1(
[ + - ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
105 : : FTPContentProvider,
106 : : rtl::OUString("com.sun.star.comp.FTPContentProvider"),
107 : 4 : rtl::OUString(FTP_CONTENT_PROVIDER_SERVICE_NAME));
108 : :
109 : : //=========================================================================
110 : : //
111 : : // Service factory implementation.
112 : : //
113 : : //=========================================================================
114 : :
115 [ + - ][ + - ]: 4 : ONE_INSTANCE_SERVICE_FACTORY_IMPL(FTPContentProvider);
116 : :
117 : :
118 : : //=========================================================================
119 : : //
120 : : // XContentProvider methods.
121 : : //
122 : : //=========================================================================
123 : :
124 : : // virtual
125 : : Reference<XContent> SAL_CALL
126 : 4 : FTPContentProvider::queryContent(
127 : : const Reference< XContentIdentifier >& xCanonicId
128 : : )
129 : : throw(
130 : : IllegalIdentifierException,
131 : : RuntimeException
132 : : )
133 : : {
134 : : // Check, if a content with given id already exists...
135 [ + - ][ - + ]: 4 : Reference<XContent> xContent = queryExistingContent(xCanonicId).get();
[ + - ]
136 [ - + ]: 4 : if(xContent.is())
137 : 0 : return xContent;
138 : :
139 : : // A new content has to be returned:
140 : : {
141 : : // Initialize
142 [ + - ]: 4 : osl::MutexGuard aGuard( m_aMutex );
143 [ - + ][ # # ]: 4 : if(!m_ftpLoaderThread || !m_pProxyDecider)
144 : : {
145 : : try {
146 [ + - ]: 4 : init();
147 : 0 : } catch( ... ) {
148 [ # # ]: 0 : throw RuntimeException();
149 : : }
150 : :
151 [ + - ][ - + ]: 4 : if(!m_ftpLoaderThread || !m_pProxyDecider)
152 [ # # ]: 0 : throw RuntimeException();
153 [ + - ]: 4 : }
154 : : }
155 : :
156 : : try {
157 [ + - ]: 4 : FTPURL aURL(xCanonicId->getContentIdentifier(),
158 [ + - ][ + - ]: 4 : this);
159 : :
160 [ + - ]: 4 : if(!m_pProxyDecider->shouldUseProxy(
161 : : rtl::OUString("ftp"),
162 : : aURL.host(),
163 [ + - ]: 4 : aURL.port().toInt32()))
164 : : {
165 [ + - ][ + - ]: 4 : xContent = new FTPContent(m_xSMgr,this,xCanonicId,aURL);
[ + - ]
166 [ + - ]: 4 : registerNewContent(xContent);
167 : : }
168 : : else {
169 : : Reference<XContentProvider>
170 [ # # ]: 0 : xProvider(getHttpProvider());
171 [ # # ]: 0 : if(xProvider.is())
172 [ # # ][ # # ]: 0 : return xProvider->queryContent(xCanonicId);
173 : : else
174 [ # # ]: 4 : throw RuntimeException();
175 [ + - ][ + - ]: 4 : }
176 [ # # ]: 0 : } catch(const malformed_exception&) {
177 [ # # ]: 0 : throw IllegalIdentifierException();
178 : : }
179 : :
180 : : // may throw IllegalIdentifierException
181 : 4 : return xContent;
182 : : }
183 : :
184 : :
185 : :
186 : :
187 : 4 : void FTPContentProvider::init() {
188 [ + - ]: 4 : m_ftpLoaderThread = new FTPLoaderThread();
189 [ + - ]: 4 : m_pProxyDecider = new ucbhelper::InternetProxyDecider(m_xSMgr);
190 : 4 : }
191 : :
192 : :
193 : :
194 : 2 : CURL* FTPContentProvider::handle() {
195 : : // Cannot be zero if called from here;
196 : 2 : return m_ftpLoaderThread->handle();
197 : : }
198 : :
199 : :
200 : 2 : 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)
206 : : {
207 [ + - ]: 2 : osl::MutexGuard aGuard(m_aMutex);
208 [ + - ]: 2 : for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
209 [ + - + - : 6 : if(host == m_ServerInfo[i].host &&
+ - ][ + - ]
210 : 2 : port == m_ServerInfo[i].port &&
211 : 2 : username == m_ServerInfo[i].username )
212 : : {
213 : 2 : password = m_ServerInfo[i].password;
214 : 2 : account = m_ServerInfo[i].account;
215 : 2 : return true;
216 : : }
217 : :
218 [ + - ]: 2 : return false;
219 : : }
220 : :
221 : :
222 : 2 : 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)
228 : : {
229 : 2 : ServerInfo inf;
230 : 2 : inf.host = host;
231 : 2 : inf.port = port;
232 : 2 : inf.username = username;
233 : 2 : inf.password = password;
234 : 2 : inf.account = account;
235 : :
236 : 2 : bool present(false);
237 [ + - ]: 2 : osl::MutexGuard aGuard(m_aMutex);
238 [ - + ]: 2 : for(unsigned int i = 0; i < m_ServerInfo.size(); ++i)
239 [ # # # # : 0 : if(host == m_ServerInfo[i].host &&
# # ][ # # ]
240 : 0 : port == m_ServerInfo[i].port &&
241 : 0 : username == m_ServerInfo[i].username)
242 : : {
243 : 0 : present = true;
244 : 0 : m_ServerInfo[i].password = password;
245 : 0 : m_ServerInfo[i].account = account;
246 : : }
247 : :
248 [ + - ]: 2 : if(!present)
249 [ + - ]: 2 : m_ServerInfo.push_back(inf);
250 : :
251 [ + - ]: 2 : return !present;
252 : : }
253 : :
254 : :
255 : :
256 : : Reference<XContentProvider>
257 : 0 : FTPContentProvider::getHttpProvider()
258 : : throw(RuntimeException)
259 : : {
260 : : // used for access to ftp-proxy
261 : 0 : ucbhelper::ContentBroker *pBroker = ucbhelper::ContentBroker::get();
262 : :
263 [ # # ]: 0 : if(pBroker) {
264 : : Reference<XContentProviderManager > xManager(
265 [ # # ]: 0 : pBroker->getContentProviderManagerInterface());
266 : :
267 [ # # ]: 0 : if(xManager.is())
268 : : return
269 [ # # ]: 0 : xManager->queryContentProvider(
270 [ # # ]: 0 : rtl::OUString("http:"));
271 : : else
272 : : throw RuntimeException(
273 : : rtl::OUString( "bad ucbhelper::ContentBroker"),
274 [ # # ][ # # ]: 0 : *this);
275 : : } else
276 : 0 : return 0;
277 : :
278 : : }
279 : :
280 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|