1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 
27 #include <rtl/ref.hxx>
28 #include <rtl/ustring.hxx>
29 #include <osl/conditn.hxx>
30 #include <osl/file.h>
31 
32 struct DownloadInteractionHandler : public rtl::IReference
33 {
34     virtual bool checkDownloadDestination(const rtl::OUString& rFileName) = 0;
35 
36     // called if the destination file already exists, but resume is false
37     virtual bool downloadTargetExists(const rtl::OUString& rFileName) = 0;
38 
39     // called when curl reports an error
40     virtual void downloadStalled(const rtl::OUString& rErrorMessage) = 0;
41 
42     // progress handler
43     virtual void downloadProgressAt(sal_Int8 nPercent) = 0;
44 
45     // called on first progress notification
46     virtual void downloadStarted(const rtl::OUString& rFileName, sal_Int64 nFileSize) = 0;
47 
48     // called when download has been finished
49     virtual void downloadFinished(const rtl::OUString& rFileName) = 0;
50 };
51 
52 
53 class Download
54 {
55 public:
Download(const com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> & xContext,const rtl::Reference<DownloadInteractionHandler> & rHandler)56     Download(const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& xContext,
57              const rtl::Reference< DownloadInteractionHandler >& rHandler) : m_xContext(xContext), m_aHandler(rHandler) {};
58 
59     // returns true when the content of rURL was successfully written to rLocalFile
60     bool start(const rtl::OUString& rURL, const rtl::OUString& rFile, const rtl::OUString& rDestinationDir);
61 
62     // stops the download after the next write operation
63     void stop();
64 
65     // returns true if the stop condition is set
isStopped() const66     bool isStopped() const
67         { return sal_True == const_cast <Download *> (this)->m_aCondition.check(); };
68 
69 protected:
70 
71     // Determines the appropriate proxy settings for the given URL. Returns true if a proxy should be used
72     void getProxyForURL(const rtl::OUString& rURL, rtl::OString& rHost, sal_Int32& rPort) const;
73 
74 private:
75     osl::Condition m_aCondition;
76     const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >& m_xContext;
77     const rtl::Reference< DownloadInteractionHandler > m_aHandler;
78 };
79 
80 
81