1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #if defined WNT
28cdf0e10cSrcweir #ifdef _MSC_VER
29cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir #include <curl/curl.h>
32cdf0e10cSrcweir #ifdef _MSC_VER
33cdf0e10cSrcweir #pragma warning(pop)
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #else
36cdf0e10cSrcweir #include <curl/curl.h>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
39cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
40cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include "download.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace beans = com::sun::star::beans ;
45cdf0e10cSrcweir namespace container = com::sun::star::container ;
46cdf0e10cSrcweir namespace lang = com::sun::star::lang ;
47cdf0e10cSrcweir namespace uno = com::sun::star::uno ;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s))
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
52cdf0e10cSrcweir struct OutData
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     rtl::Reference< DownloadInteractionHandler >Handler;
55cdf0e10cSrcweir     rtl::OUString   File;
56cdf0e10cSrcweir     rtl::OUString   DestinationDir;
57cdf0e10cSrcweir     oslFileHandle   FileHandle;
58cdf0e10cSrcweir     sal_uInt64      Offset;
59cdf0e10cSrcweir     osl::Condition& StopCondition;
60cdf0e10cSrcweir     CURL *curl;
61cdf0e10cSrcweir 
OutDataOutData62cdf0e10cSrcweir     OutData(osl::Condition& rCondition) : FileHandle(NULL), Offset(0), StopCondition(rCondition), curl(NULL) {};
63cdf0e10cSrcweir };
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //------------------------------------------------------------------------------
66cdf0e10cSrcweir 
openFile(OutData & out)67cdf0e10cSrcweir static void openFile( OutData& out )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     char * effective_url;
70cdf0e10cSrcweir     curl_easy_getinfo(out.curl, CURLINFO_EFFECTIVE_URL, &effective_url);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     double fDownloadSize;
73cdf0e10cSrcweir     curl_easy_getinfo(out.curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fDownloadSize);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     rtl::OString aURL(effective_url);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     // ensure no trailing '/'
78cdf0e10cSrcweir     sal_Int32 nLen = aURL.getLength();
79cdf0e10cSrcweir     while( (nLen > 0) && ('/' == aURL[nLen-1]) )
80cdf0e10cSrcweir         aURL = aURL.copy(0, --nLen);
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // extract file name last '/'
83cdf0e10cSrcweir     sal_Int32 nIndex = aURL.lastIndexOf('/');
84cdf0e10cSrcweir     if( nIndex > 0 )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         out.File = out.DestinationDir + rtl::OStringToOUString(aURL.copy(nIndex), RTL_TEXTENCODING_UTF8);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         oslFileError rc;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         // Give the user an overwrite warning if the target file exists
91cdf0e10cSrcweir         const sal_Int32 openFlags = osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
92cdf0e10cSrcweir         do
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             rc = osl_openFile(out.File.pData, &out.FileHandle, openFlags);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir             if( osl_File_E_EXIST == rc && ! out.Handler->downloadTargetExists(out.File) )
97cdf0e10cSrcweir             {
98cdf0e10cSrcweir                 out.StopCondition.set();
99cdf0e10cSrcweir                 break;
100cdf0e10cSrcweir             }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         } while( osl_File_E_EXIST == rc );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         if( osl_File_E_None == rc )
105cdf0e10cSrcweir             out.Handler->downloadStarted(out.File, (sal_Int64) fDownloadSize);
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir //------------------------------------------------------------------------------
110cdf0e10cSrcweir 
111cdf0e10cSrcweir static inline rtl::OString
getStringValue(const uno::Reference<container::XNameAccess> & xNameAccess,const rtl::OUString & aName)112cdf0e10cSrcweir getStringValue(const uno::Reference< container::XNameAccess >& xNameAccess, const rtl::OUString& aName)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     rtl::OString aRet;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     OSL_ASSERT(xNameAccess->hasByName(aName));
117cdf0e10cSrcweir     uno::Any aValue = xNameAccess->getByName(aName);
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     return rtl::OUStringToOString(aValue.get<rtl::OUString>(), RTL_TEXTENCODING_UTF8);
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir //------------------------------------------------------------------------------
123cdf0e10cSrcweir 
124cdf0e10cSrcweir static inline sal_Int32
getInt32Value(const uno::Reference<container::XNameAccess> & xNameAccess,const rtl::OUString & aName,sal_Int32 nDefault=-1)125cdf0e10cSrcweir getInt32Value(const uno::Reference< container::XNameAccess >& xNameAccess,
126cdf0e10cSrcweir                     const rtl::OUString& aName, sal_Int32 nDefault=-1)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     OSL_ASSERT(xNameAccess->hasByName(aName));
129cdf0e10cSrcweir     uno::Any aValue = xNameAccess->getByName(aName);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     sal_Int32 n=nDefault;
132cdf0e10cSrcweir     aValue >>= n;
133cdf0e10cSrcweir     return n;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir //------------------------------------------------------------------------------
137cdf0e10cSrcweir 
138cdf0e10cSrcweir static size_t
write_function(void * ptr,size_t size,size_t nmemb,void * stream)139cdf0e10cSrcweir write_function( void *ptr, size_t size, size_t nmemb, void *stream )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     OutData *out = reinterpret_cast < OutData * > (stream);
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     if( NULL == out->FileHandle )
144cdf0e10cSrcweir         openFile(*out);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     sal_uInt64 nBytesWritten = 0;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     if( NULL != out->FileHandle )
149cdf0e10cSrcweir         osl_writeFile(out->FileHandle, ptr, size * nmemb, &nBytesWritten);
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     return (size_t) nBytesWritten;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir //------------------------------------------------------------------------------
155cdf0e10cSrcweir 
156cdf0e10cSrcweir static int
progress_callback(void * clientp,double dltotal,double dlnow,double ultotal,double ulnow)157cdf0e10cSrcweir progress_callback( void *clientp, double dltotal, double dlnow, double ultotal, double ulnow )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     (void) ultotal;
160cdf0e10cSrcweir     (void) ulnow;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     OutData *out = reinterpret_cast < OutData * > (clientp);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     OSL_ASSERT( out );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     if( ! out->StopCondition.check() )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         double fPercent = 0;
169cdf0e10cSrcweir         if ( dltotal + out->Offset )
170cdf0e10cSrcweir             fPercent = (dlnow + out->Offset) * 100 / (dltotal + out->Offset);
171cdf0e10cSrcweir         if( fPercent < 0 )
172cdf0e10cSrcweir             fPercent = 0;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         // Do not report progress for redirection replies
175cdf0e10cSrcweir         long nCode;
176cdf0e10cSrcweir         curl_easy_getinfo(out->curl, CURLINFO_RESPONSE_CODE, &nCode);
177cdf0e10cSrcweir         if( (nCode != 302) && (nCode != 303) && (dltotal > 0) )
178cdf0e10cSrcweir             out->Handler->downloadProgressAt((sal_Int8)fPercent);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         return 0;
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     // If stop condition is set, return non 0 value to abort
184cdf0e10cSrcweir     return -1;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir //------------------------------------------------------------------------------
188cdf0e10cSrcweir 
189cdf0e10cSrcweir void
getProxyForURL(const rtl::OUString & rURL,rtl::OString & rHost,sal_Int32 & rPort) const190cdf0e10cSrcweir Download::getProxyForURL(const rtl::OUString& rURL, rtl::OString& rHost, sal_Int32& rPort) const
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     if( !m_xContext.is() )
193cdf0e10cSrcweir         throw uno::RuntimeException(
194cdf0e10cSrcweir             UNISTRING( "Download: empty component context" ),
195cdf0e10cSrcweir             uno::Reference< uno::XInterface >() );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     uno::Reference< lang::XMultiComponentFactory > xServiceManager(m_xContext->getServiceManager());
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     if( !xServiceManager.is() )
200cdf0e10cSrcweir         throw uno::RuntimeException(
201cdf0e10cSrcweir             UNISTRING( "Download: unable to obtain service manager from component context" ),
202cdf0e10cSrcweir             uno::Reference< uno::XInterface >() );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xConfigProvider(
205cdf0e10cSrcweir         xServiceManager->createInstanceWithContext( UNISTRING( "com.sun.star.configuration.ConfigurationProvider" ), m_xContext ),
206cdf0e10cSrcweir         uno::UNO_QUERY_THROW);
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     beans::PropertyValue aProperty;
209cdf0e10cSrcweir     aProperty.Name  = UNISTRING( "nodepath" );
210cdf0e10cSrcweir     aProperty.Value = uno::makeAny( UNISTRING("org.openoffice.Inet/Settings") );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     uno::Sequence< uno::Any > aArgumentList( 1 );
213cdf0e10cSrcweir     aArgumentList[0] = uno::makeAny( aProperty );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     uno::Reference< container::XNameAccess > xNameAccess(
216cdf0e10cSrcweir         xConfigProvider->createInstanceWithArguments(
217cdf0e10cSrcweir             UNISTRING("com.sun.star.configuration.ConfigurationAccess"), aArgumentList ),
218cdf0e10cSrcweir         uno::UNO_QUERY_THROW );
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     OSL_ASSERT(xNameAccess->hasByName(UNISTRING("ooInetProxyType")));
221cdf0e10cSrcweir     uno::Any aValue = xNameAccess->getByName(UNISTRING("ooInetProxyType"));
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     sal_Int32 nProxyType = aValue.get< sal_Int32 >();
224cdf0e10cSrcweir     if( 0 != nProxyType ) // type 0 means "direct connection to the internet
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         if( rURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("http:")) )
227cdf0e10cSrcweir         {
228cdf0e10cSrcweir             rHost = getStringValue(xNameAccess, UNISTRING("ooInetHTTPProxyName"));
229cdf0e10cSrcweir             rPort = getInt32Value(xNameAccess, UNISTRING("ooInetHTTPProxyPort"));
230cdf0e10cSrcweir         }
231cdf0e10cSrcweir         else if( rURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("https:")) )
232cdf0e10cSrcweir         {
233cdf0e10cSrcweir             rHost = getStringValue(xNameAccess, UNISTRING("ooInetHTTPSProxyName"));
234cdf0e10cSrcweir             rPort = getInt32Value(xNameAccess, UNISTRING("ooInetHTTPSProxyPort"));
235cdf0e10cSrcweir         }
236cdf0e10cSrcweir         else if( rURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("ftp:")) )
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             rHost = getStringValue(xNameAccess, UNISTRING("ooInetFTPProxyName"));
239cdf0e10cSrcweir             rPort = getInt32Value(xNameAccess, UNISTRING("ooInetFTPProxyPort"));
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir //------------------------------------------------------------------------------
245cdf0e10cSrcweir 
curl_run(const rtl::OUString & rURL,OutData & out,const rtl::OString & aProxyHost,sal_Int32 nProxyPort)246cdf0e10cSrcweir bool curl_run(const rtl::OUString& rURL, OutData& out, const rtl::OString& aProxyHost, sal_Int32 nProxyPort)
247cdf0e10cSrcweir {
248cdf0e10cSrcweir     /* Need to investigate further whether it is necessary to call
249cdf0e10cSrcweir      * curl_global_init or not - leave it for now (as the ftp UCB content
250cdf0e10cSrcweir      * provider does as well).
251cdf0e10cSrcweir      */
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     CURL * pCURL = curl_easy_init();
254cdf0e10cSrcweir     bool ret = false;
255cdf0e10cSrcweir 
256cdf0e10cSrcweir     if( NULL != pCURL )
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         out.curl = pCURL;
259cdf0e10cSrcweir 
260cdf0e10cSrcweir         rtl::OString aURL(rtl::OUStringToOString(rURL, RTL_TEXTENCODING_UTF8));
261cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_URL, aURL.getStr());
262cdf0e10cSrcweir 
263cdf0e10cSrcweir         // abort on http errors
264cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_FAILONERROR, 1);
265cdf0e10cSrcweir 
266cdf0e10cSrcweir         // enable redirection
267cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_FOLLOWLOCATION, 1);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         // write function
270cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, &out);
271cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, &write_function);
272cdf0e10cSrcweir 
273cdf0e10cSrcweir         // progress handler - Condition::check unfortunatly is not defined const
274cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 0);
275cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_PROGRESSFUNCTION, &progress_callback);
276cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_PROGRESSDATA, &out);
277cdf0e10cSrcweir 
278cdf0e10cSrcweir         // proxy
279cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_PROXY, aProxyHost.getStr());
280cdf0e10cSrcweir         curl_easy_setopt(pCURL, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
281cdf0e10cSrcweir         if( -1 != nProxyPort )
282cdf0e10cSrcweir             curl_easy_setopt(pCURL, CURLOPT_PROXYPORT, nProxyPort);
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         if( out.Offset > 0 )
285cdf0e10cSrcweir         {
286cdf0e10cSrcweir             // curl_off_t offset = nOffset; libcurl seems to be compiled with large
287cdf0e10cSrcweir             // file support (and we not) ..
288cdf0e10cSrcweir             sal_Int64 offset = (sal_Int64) out.Offset;
289cdf0e10cSrcweir             curl_easy_setopt(pCURL, CURLOPT_RESUME_FROM_LARGE, offset);
290cdf0e10cSrcweir         }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir         CURLcode cc = curl_easy_perform(pCURL);
293cdf0e10cSrcweir 
294cdf0e10cSrcweir         // treat zero byte downloads as errors
295cdf0e10cSrcweir         if( NULL == out.FileHandle )
296cdf0e10cSrcweir             openFile(out);
297cdf0e10cSrcweir 
298cdf0e10cSrcweir         if( CURLE_OK == cc )
299cdf0e10cSrcweir         {
300cdf0e10cSrcweir             out.Handler->downloadFinished(out.File);
301cdf0e10cSrcweir             ret = true;
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir         if ( CURLE_PARTIAL_FILE  == cc )
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir             // this sometimes happens, when a user throws away his user data, but has already
307cdf0e10cSrcweir             // completed the download of an update.
308cdf0e10cSrcweir             double fDownloadSize;
309cdf0e10cSrcweir             curl_easy_getinfo( pCURL, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fDownloadSize );
310cdf0e10cSrcweir             if ( -1 == fDownloadSize )
311cdf0e10cSrcweir             {
312cdf0e10cSrcweir                 out.Handler->downloadFinished(out.File);
313cdf0e10cSrcweir                 ret = true;
314cdf0e10cSrcweir             }
315cdf0e10cSrcweir         }
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         // Avoid target file being removed
318cdf0e10cSrcweir         else if( (CURLE_ABORTED_BY_CALLBACK == cc) || out.StopCondition.check() )
319cdf0e10cSrcweir             ret = true;
320cdf0e10cSrcweir 
321cdf0e10cSrcweir         // Only report errors when not stopped
322cdf0e10cSrcweir         else
323cdf0e10cSrcweir         {
324cdf0e10cSrcweir             rtl::OString aMessage(RTL_CONSTASCII_STRINGPARAM("Unknown error"));
325cdf0e10cSrcweir 
326cdf0e10cSrcweir             const char * error_message = curl_easy_strerror(cc);
327cdf0e10cSrcweir             if( NULL != error_message )
328cdf0e10cSrcweir                 aMessage = error_message;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir             if ( CURLE_HTTP_RETURNED_ERROR == cc )
331cdf0e10cSrcweir             {
332cdf0e10cSrcweir                 long nError;
333cdf0e10cSrcweir                 curl_easy_getinfo( pCURL, CURLINFO_RESPONSE_CODE, &nError );
334cdf0e10cSrcweir 
335cdf0e10cSrcweir                 if ( 403 == nError )
336cdf0e10cSrcweir                     aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( " 403: Access denied!" ) );
337cdf0e10cSrcweir                 else if ( 404 == nError )
338cdf0e10cSrcweir                     aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( " 404: File not found!" ) );
339cdf0e10cSrcweir                 else if ( 416 == nError )
340cdf0e10cSrcweir                 {
341cdf0e10cSrcweir                     // we got this error probably, because we already downloaded the file
342cdf0e10cSrcweir                     out.Handler->downloadFinished(out.File);
343cdf0e10cSrcweir                     ret = true;
344cdf0e10cSrcweir                 }
345cdf0e10cSrcweir                 else
346cdf0e10cSrcweir                 {
347cdf0e10cSrcweir                     aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( ":error code = " ) );
348cdf0e10cSrcweir                     aMessage += aMessage.valueOf( nError );
349cdf0e10cSrcweir                     aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( " !" ) );
350cdf0e10cSrcweir                 }
351cdf0e10cSrcweir             }
352cdf0e10cSrcweir             if ( !ret )
353cdf0e10cSrcweir                 out.Handler->downloadStalled( rtl::OStringToOUString(aMessage, RTL_TEXTENCODING_UTF8) );
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir         curl_easy_cleanup(pCURL);
357cdf0e10cSrcweir     }
358cdf0e10cSrcweir 
359cdf0e10cSrcweir     return ret;
360cdf0e10cSrcweir }
361cdf0e10cSrcweir 
362cdf0e10cSrcweir //------------------------------------------------------------------------------
363cdf0e10cSrcweir 
364cdf0e10cSrcweir bool
start(const rtl::OUString & rURL,const rtl::OUString & rFile,const rtl::OUString & rDestinationDir)365cdf0e10cSrcweir Download::start(const rtl::OUString& rURL, const rtl::OUString& rFile, const rtl::OUString& rDestinationDir)
366cdf0e10cSrcweir {
367cdf0e10cSrcweir     OSL_ASSERT( m_aHandler.is() );
368cdf0e10cSrcweir 
369cdf0e10cSrcweir     OutData out(m_aCondition);
370cdf0e10cSrcweir     rtl::OUString aFile( rFile );
371cdf0e10cSrcweir 
372cdf0e10cSrcweir     // when rFile is empty, there is no remembered file name. If there is already a file with the
373cdf0e10cSrcweir     // same name ask the user if she wants to resume a download or restart the download
374cdf0e10cSrcweir     if ( !aFile.getLength() )
375cdf0e10cSrcweir     {
376cdf0e10cSrcweir         // GetFileName()
377cdf0e10cSrcweir         rtl::OUString aURL( rURL );
378cdf0e10cSrcweir         // ensure no trailing '/'
379cdf0e10cSrcweir         sal_Int32 nLen = aURL.getLength();
380cdf0e10cSrcweir         while( (nLen > 0) && ('/' == aURL[ nLen-1 ]) )
381cdf0e10cSrcweir             aURL = aURL.copy( 0, --nLen );
382cdf0e10cSrcweir 
383cdf0e10cSrcweir         // extract file name last '/'
384cdf0e10cSrcweir         sal_Int32 nIndex = aURL.lastIndexOf('/');
385cdf0e10cSrcweir         aFile = rDestinationDir + aURL.copy( nIndex );
386cdf0e10cSrcweir 
387cdf0e10cSrcweir         // check for existing file
388cdf0e10cSrcweir         oslFileError rc = osl_openFile( aFile.pData, &out.FileHandle, osl_File_OpenFlag_Write | osl_File_OpenFlag_Create );
389cdf0e10cSrcweir         osl_closeFile(out.FileHandle);
390cdf0e10cSrcweir         out.FileHandle = NULL;
391cdf0e10cSrcweir 
392cdf0e10cSrcweir         if( osl_File_E_EXIST == rc )
393cdf0e10cSrcweir         {
394cdf0e10cSrcweir             if ( m_aHandler->checkDownloadDestination( aURL.copy( nIndex+1 ) ) )
395cdf0e10cSrcweir             {
396cdf0e10cSrcweir                 osl_removeFile( aFile.pData );
397cdf0e10cSrcweir                 aFile = rtl::OUString();
398cdf0e10cSrcweir             }
399cdf0e10cSrcweir             else
400cdf0e10cSrcweir                 m_aHandler->downloadStarted( aFile, 0 );
401cdf0e10cSrcweir         }
402cdf0e10cSrcweir         else
403cdf0e10cSrcweir         {
404cdf0e10cSrcweir             osl_removeFile( aFile.pData );
405cdf0e10cSrcweir             aFile = rtl::OUString();
406cdf0e10cSrcweir         }
407cdf0e10cSrcweir     }
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     out.File = aFile;
410cdf0e10cSrcweir     out.DestinationDir = rDestinationDir;
411cdf0e10cSrcweir     out.Handler = m_aHandler;
412cdf0e10cSrcweir 
413cdf0e10cSrcweir     if( aFile.getLength() > 0 )
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         oslFileError rc = osl_openFile(aFile.pData, &out.FileHandle, osl_File_OpenFlag_Write);
416cdf0e10cSrcweir 
417cdf0e10cSrcweir         if( osl_File_E_None == rc )
418cdf0e10cSrcweir         {
419cdf0e10cSrcweir             // Set file pointer to the end of the file on resume
420cdf0e10cSrcweir             if( osl_File_E_None == osl_setFilePos(out.FileHandle, osl_Pos_End, 0) )
421cdf0e10cSrcweir             {
422cdf0e10cSrcweir                 osl_getFilePos(out.FileHandle, &out.Offset);
423cdf0e10cSrcweir             }
424cdf0e10cSrcweir         }
425cdf0e10cSrcweir         else if( osl_File_E_NOENT == rc ) // file has been deleted meanwhile ..
426cdf0e10cSrcweir             out.File = rtl::OUString();
427cdf0e10cSrcweir     }
428cdf0e10cSrcweir 
429cdf0e10cSrcweir     rtl::OString aProxyHost;
430cdf0e10cSrcweir     sal_Int32    nProxyPort = -1;
431cdf0e10cSrcweir     getProxyForURL(rURL, aProxyHost, nProxyPort);
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     bool ret = curl_run(rURL, out, aProxyHost, nProxyPort);
434cdf0e10cSrcweir 
435cdf0e10cSrcweir     if( NULL != out.FileHandle )
436cdf0e10cSrcweir     {
437cdf0e10cSrcweir         osl_syncFile(out.FileHandle);
438cdf0e10cSrcweir         osl_closeFile(out.FileHandle);
439cdf0e10cSrcweir 
440cdf0e10cSrcweir // #i90930# Don't remove already downloaded bits, when curl_run reports an error
441cdf0e10cSrcweir // because later calls might be successful
442cdf0e10cSrcweir //        if( ! ret )
443cdf0e10cSrcweir //            osl_removeFile(out.File.pData);
444cdf0e10cSrcweir     }
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     m_aCondition.reset();
447cdf0e10cSrcweir     return ret;
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir //------------------------------------------------------------------------------
451cdf0e10cSrcweir 
452cdf0e10cSrcweir void
stop()453cdf0e10cSrcweir Download::stop()
454cdf0e10cSrcweir {
455cdf0e10cSrcweir     m_aCondition.set();
456cdf0e10cSrcweir }
457