[cppcheck] Part 1: Performance
[supertux.git] / src / addon / downloader.hpp
index 79c1056..6e7e482 100644 (file)
@@ -1,5 +1,6 @@
 //  SuperTux
-//  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
+//  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
+//                2014 Ingo Ruhnke <grumbel@gmail.com>
 //
 //  This program is free software: you can redistribute it and/or modify
 //  it under the terms of the GNU General Public License as published by
 #ifndef HEADER_SUPERTUX_ADDON_DOWNLOADER_HPP
 #define HEADER_SUPERTUX_ADDON_DOWNLOADER_HPP
 
+#include <curl/curl.h>
+#include <curl/easy.h>
+#include <functional>
+#include <memory>
 #include <string>
+#include <vector>
+
+typedef int TransferId;
+class Downloader;
+
+class TransferStatus
+{
+public:
+  Downloader& m_downloader;
+  TransferId id;
+  std::vector<std::function<void (bool)> > callbacks;
+
+  int dltotal;
+  int dlnow;
+  int ultotal;
+  int ulnow;
+
+  std::string error_msg;
+
+  TransferStatus(Downloader& downloader, TransferId id_) :
+    m_downloader(downloader),
+    id(id_),
+    callbacks(),
+    dltotal(0),
+    dlnow(0),
+    ultotal(0),
+    ulnow(0),
+    error_msg()
+  {}
+
+  void abort();
+  void update();
+
+  void then(const std::function<void (bool)>& callback)
+  {
+    callbacks.push_back(callback);
+  }
+};
+
+using TransferStatusPtr = std::shared_ptr<TransferStatus>;
+
+class Transfer;
 
 class Downloader
 {
 private:
+  CURLM* m_multi_handle;
+  std::vector<std::unique_ptr<Transfer> > m_transfers;
+  int m_next_transfer_id;
+
 public:
   Downloader();
   ~Downloader();
@@ -36,6 +87,11 @@ public:
                 size_t (*write_func)(void* ptr, size_t size, size_t nmemb, void* userdata),
                 void* userdata);
 
+  void update();
+
+  TransferStatusPtr request_download(const std::string& url, const std::string& filename);
+  void abort(TransferId id);
+
 private:
   Downloader(const Downloader&) = delete;
   Downloader& operator=(const Downloader&) = delete;