1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include <com/sun/star/uno/XComponentContext.hpp>
23 
24 #include <rtl/ref.hxx>
25 #include <rtl/ustring.hxx>
26 #include <osl/conditn.hxx>
27 #include <salhelper/simplereferenceobject.hxx>
28 
29 struct DownloadInteractionHandler : public virtual salhelper::SimpleReferenceObject
30 {
31     virtual bool checkDownloadDestination(const OUString& rFileName) = 0;
32 
33     // called if the destination file already exists, but resume is false
34     virtual bool downloadTargetExists(const OUString& rFileName) = 0;
35 
36     // called when curl reports an error
37     virtual void downloadStalled(const OUString& rErrorMessage) = 0;
38 
39     // progress handler
40     virtual void downloadProgressAt(sal_Int8 nPercent) = 0;
41 
42     // called on first progress notification
43     virtual void downloadStarted(const OUString& rFileName, sal_Int64 nFileSize) = 0;
44 
45     // called when download has been finished
46     virtual void downloadFinished(const OUString& rFileName) = 0;
47 
48 protected:
~DownloadInteractionHandlerDownloadInteractionHandler49     virtual ~DownloadInteractionHandler() override {}
50 };
51 
52 class Download
53 {
54 public:
Download(const css::uno::Reference<css::uno::XComponentContext> & xContext,const rtl::Reference<DownloadInteractionHandler> & rHandler)55     Download(const css::uno::Reference<css::uno::XComponentContext>& xContext,
56              const rtl::Reference<DownloadInteractionHandler>& rHandler)
57         : m_xContext(xContext)
58         , m_aHandler(rHandler){};
59 
60     // returns true when the content of rURL was successfully written to rLocalFile
61     bool start(const OUString& rURL, const OUString& rFile, const OUString& rDestinationDir);
62 
63     // stops the download after the next write operation
64     void stop();
65 
66 protected:
67     // Determines the appropriate proxy settings for the given URL. Returns true if a proxy should be used
68     void getProxyForURL(const OUString& rURL, OString& rHost, sal_Int32& rPort) const;
69 
70 private:
71     osl::Condition m_aCondition;
72     const css::uno::Reference<css::uno::XComponentContext>& m_xContext;
73     const rtl::Reference<DownloadInteractionHandler> m_aHandler;
74 };
75 
76 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
77