supertux
downloader.hpp
1 // SuperTux
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 // 2014 Ingo Ruhnke <grumbel@gmail.com>
4 // 2023 Vankata453
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef HEADER_SUPERTUX_ADDON_DOWNLOADER_HPP
20 #define HEADER_SUPERTUX_ADDON_DOWNLOADER_HPP
21 
22 #ifndef EMSCRIPTEN
23 #include <curl/curl.h>
24 #include <curl/easy.h>
25 #endif
26 #include <functional>
27 #include <map>
28 #include <string>
29 #include <vector>
30 
31 #include "addon/downloader_defines.hpp"
32 
33 typedef int TransferId;
34 
35 class TransferStatus final
36 {
37  friend class Downloader;
38  friend class TransferStatusList;
39 
40 public:
41  Downloader& m_downloader;
42  TransferId id;
43  const std::string file;
44  std::vector<std::function<void (bool)> > callbacks;
45 
46  int dltotal;
47  int dlnow;
48  int ultotal;
49  int ulnow;
50 
51  std::string error_msg;
52 
53 private:
54  TransferStatusList* parent_list;
55 
56 public:
57  TransferStatus(Downloader& downloader, TransferId id_,
58  const std::string& url);
59 
60  void abort();
61  void update();
62 
63  void then(const std::function<void (bool)>& callback)
64  {
65  callbacks.push_back(callback);
66  }
67 
68 private:
69  TransferStatus(const TransferStatus&) = delete;
70  TransferStatus& operator=(const TransferStatus&) = delete;
71 };
72 
73 class TransferStatusList final
74 {
75  friend class Downloader;
76 
77 private:
78  std::vector<TransferStatusPtr> m_transfer_statuses;
79  int m_successful_count;
80  std::vector<std::function<void (bool)> > m_callbacks;
81 
82  std::string m_error_msg;
83 
84 public:
86  TransferStatusList(const std::vector<TransferStatusPtr>& list);
87 
88  void abort();
89  void update();
90 
91  void push(TransferStatusPtr status);
92  void push(TransferStatusListPtr statuses);
93 
94  void then(const std::function<void (bool)>& callback)
95  {
96  m_callbacks.push_back(callback);
97  }
98 
99  int get_download_now() const;
100  int get_download_total() const;
101  const std::string& get_error() const { return m_error_msg; }
102 
103  bool is_active() const;
104 
105 private:
106  // Called when one of the transfers completes.
107  void on_transfer_complete(TransferStatusPtr this_status, bool successful);
108 
109  void reset();
110 
111 private:
112  TransferStatusList(const TransferStatusList&) = delete;
113  TransferStatusList& operator=(const TransferStatusList&) = delete;
114 };
115 
116 class Transfer;
117 
118 class Downloader final
119 {
120 private:
121 #ifndef EMSCRIPTEN
122  CURLM* m_multi_handle;
123 #endif
124  std::map<TransferId, std::unique_ptr<Transfer> > m_transfers;
125  int m_next_transfer_id;
126 
127  float m_last_update_time;
128 
129 public:
130  Downloader();
131  ~Downloader();
132 
134  std::string download(const std::string& url);
135 
137  void download(const std::string& url, const std::string& filename);
138 
139  void download(const std::string& url,
140  size_t (*write_func)(void* ptr, size_t size, size_t nmemb, void* userdata),
141  void* userdata);
142 
143  void update();
144 
145  TransferStatusPtr request_download(const std::string& url, const std::string& filename);
146  void abort(TransferId id);
147 
148 #ifdef EMSCRIPTEN
149  void onDownloadProgress(int id, int loaded, int total);
150  void onDownloadFinished(int id);
151  void onDownloadError(int id);
152  void onDownloadAborted(int id);
153 #endif
154 
155 private:
156  Downloader(const Downloader&) = delete;
157  Downloader& operator=(const Downloader&) = delete;
158 };
159 
160 #endif
161 
162 /* EOF */
Definition: downloader.hpp:118
Definition: downloader.hpp:35
Definition: downloader.hpp:73
Definition: downloader.cpp:235