From: Ingo Ruhnke Date: Tue, 26 Aug 2014 19:44:58 +0000 (+0200) Subject: Use a chain of callbacks instead of a single one in TransferStatusPtr X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=74773542651d1f9dd3ee8fc78a7fbf0e0ac3fe10;p=supertux.git Use a chain of callbacks instead of a single one in TransferStatusPtr This allows reusing it in AddonManager and not bothering with creating it's own, but basically empty InstallStatus. --- diff --git a/src/addon/addon_manager.cpp b/src/addon/addon_manager.cpp index 1db62a815..87ff4cbb1 100644 --- a/src/addon/addon_manager.cpp +++ b/src/addon/addon_manager.cpp @@ -90,7 +90,6 @@ AddonManager::AddonManager(const std::string& addon_directory, m_repository_addons(), m_has_been_updated(false), m_install_request(), - m_install_status(), m_transfer_status() { PHYSFS_mkdir(m_addon_directory.c_str()); @@ -203,12 +202,12 @@ AddonManager::has_been_updated() const return m_has_been_updated; } -AddonManager::InstallStatusPtr +TransferStatusPtr AddonManager::request_check_online() { - if (m_install_status) + if (m_transfer_status) { - throw std::runtime_error("only one addon install request allowed at a time"); + throw std::runtime_error("only async request can be made to AddonManager at a time"); } else { @@ -218,17 +217,10 @@ AddonManager::request_check_online() m_repository_addons = parse_addon_infos("/addons/repository.nfo"); m_has_been_updated = true; - if (m_install_status->callback) - { - m_install_status->callback(); - } - - m_install_status = {}; m_transfer_status = {}; }); - m_install_status = std::make_shared(); - return m_install_status; + return m_transfer_status; } } @@ -240,10 +232,10 @@ AddonManager::check_online() m_has_been_updated = true; } -AddonManager::InstallStatusPtr +TransferStatusPtr AddonManager::request_install_addon(const AddonId& addon_id) { - if (m_install_status) + if (m_transfer_status) { throw std::runtime_error("only one addon install request allowed at a time"); } @@ -310,20 +302,11 @@ AddonManager::request_install_addon(const AddonId& addon_id) } } - // signal that the request is done and cleanup - if (m_install_status->callback) - { - m_install_status->callback(); - } - m_install_request = {}; - m_install_status = {}; m_transfer_status = {}; }); - m_install_status = std::make_shared(); - - return m_install_status; + return m_transfer_status; } } @@ -603,12 +586,6 @@ void AddonManager::update() { m_downloader.update(); - - if (m_install_status) - { - m_install_status->now = m_transfer_status->dlnow; - m_install_status->total = m_transfer_status->dltotal; - } } void @@ -619,7 +596,6 @@ AddonManager::abort_install() m_downloader.abort(m_transfer_status->id); m_install_request = {}; - m_install_status = {}; m_transfer_status = {}; } diff --git a/src/addon/addon_manager.hpp b/src/addon/addon_manager.hpp index 713d70db4..3dfe84333 100644 --- a/src/addon/addon_manager.hpp +++ b/src/addon/addon_manager.hpp @@ -31,6 +31,8 @@ class Addon; class AddonRepository; +class TransferStatus; +using TransferStatusPtr = std::shared_ptr; typedef std::string AddonId; @@ -38,26 +40,6 @@ typedef std::string AddonId; class AddonManager : public Currenton { public: - struct InstallStatus - { - InstallStatus() : - now(0), - total(0), - done(false), - callback() - {} - - int now; - int total; - bool done; - std::function callback; - - void then(const std::function& callback_) - { - callback = callback_; - } - }; - struct InstallRequest { InstallRequest() : @@ -69,7 +51,6 @@ public: std::string install_filename; }; - using InstallStatusPtr = std::shared_ptr; using InstallRequestPtr = std::shared_ptr; private: @@ -86,7 +67,6 @@ private: bool m_has_been_updated; InstallRequestPtr m_install_request; - InstallStatusPtr m_install_status; TransferStatusPtr m_transfer_status; public: @@ -97,7 +77,7 @@ public: bool has_online_support() const; bool has_been_updated() const; void check_online(); - InstallStatusPtr request_check_online(); + TransferStatusPtr request_check_online(); std::vector get_repository_addons() const; std::vector get_installed_addons() const; @@ -105,7 +85,7 @@ public: Addon& get_repository_addon(const AddonId& addon); Addon& get_installed_addon(const AddonId& addon); - InstallStatusPtr request_install_addon(const AddonId& addon_id); + TransferStatusPtr request_install_addon(const AddonId& addon_id); void abort_install(); void install_addon(const AddonId& addon_id); void uninstall_addon(const AddonId& addon_id); diff --git a/src/addon/downloader.cpp b/src/addon/downloader.cpp index f07915ec5..14d4dc4cb 100644 --- a/src/addon/downloader.cpp +++ b/src/addon/downloader.cpp @@ -283,9 +283,9 @@ Downloader::update() m_transfers.erase(it); status->status = TransferStatus::COMPLETED; - if (status->callback) + for(auto& callback : status->callbacks) { - status->callback(); + callback(); } } break; diff --git a/src/addon/downloader.hpp b/src/addon/downloader.hpp index 5aab70dee..64d2384b6 100644 --- a/src/addon/downloader.hpp +++ b/src/addon/downloader.hpp @@ -39,7 +39,7 @@ public: int dlnow; int ultotal; int ulnow; - std::function callback; + std::vector > callbacks; TransferStatus(TransferId id_) : id(id_), @@ -48,16 +48,16 @@ public: dlnow(0), ultotal(0), ulnow(0), - callback() + callbacks() {} - void then(const std::function& callback_) + void then(const std::function& callback) { - callback = callback_; + callbacks.push_back(callback); } }; -typedef std::shared_ptr TransferStatusPtr; +using TransferStatusPtr = std::shared_ptr; class Transfer; diff --git a/src/supertux/menu/addon_dialog.cpp b/src/supertux/menu/addon_dialog.cpp index d4f47ec38..188e8aad2 100644 --- a/src/supertux/menu/addon_dialog.cpp +++ b/src/supertux/menu/addon_dialog.cpp @@ -21,7 +21,7 @@ #include "gui/menu_manager.hpp" #include "util/gettext.hpp" -AddonDialog::AddonDialog(AddonManager::InstallStatusPtr status) : +AddonDialog::AddonDialog(TransferStatusPtr status) : m_status(status), m_title() { @@ -51,14 +51,14 @@ AddonDialog::update_text() std::ostringstream out; out << m_title << "\n"; - if (m_status->total == 0) + if (m_status->dltotal == 0) { out << "---\n---"; } else { - int percent = 100 * m_status->now / m_status->total; - out << m_status->now/1000 << "/" << m_status->total/1000 << " kB\n" << percent << "%"; + int percent = 100 * m_status->dlnow / m_status->dltotal; + out << m_status->dlnow/1000 << "/" << m_status->dltotal/1000 << " kB\n" << percent << "%"; } set_text(out.str()); diff --git a/src/supertux/menu/addon_dialog.hpp b/src/supertux/menu/addon_dialog.hpp index 1710ff8a1..1b9ee1279 100644 --- a/src/supertux/menu/addon_dialog.hpp +++ b/src/supertux/menu/addon_dialog.hpp @@ -18,16 +18,20 @@ #define HEADER_SUPERTUX_SUPERTUX_MENU_ADDON_DIALOG_HPP #include "addon/addon_manager.hpp" +#include "addon/addon_manager.hpp" #include "gui/dialog.hpp" +class TransferStatus; +using TransferStatusPtr = std::shared_ptr; + class AddonDialog : public Dialog { private: - AddonManager::InstallStatusPtr m_status; + TransferStatusPtr m_status; std::string m_title; public: - AddonDialog(AddonManager::InstallStatusPtr status); + AddonDialog(TransferStatusPtr status); void set_title(const std::string& title); void update() override; diff --git a/src/supertux/menu/addon_menu.cpp b/src/supertux/menu/addon_menu.cpp index 5011e9557..05f3e5771 100644 --- a/src/supertux/menu/addon_menu.cpp +++ b/src/supertux/menu/addon_menu.cpp @@ -184,7 +184,7 @@ AddonMenu::menu_action(MenuItem* item) { try { - AddonManager::InstallStatusPtr status = m_addon_manager.request_check_online(); + TransferStatusPtr status = m_addon_manager.request_check_online(); status->then([this]{ MenuManager::instance().set_dialog({}); refresh(); @@ -225,7 +225,7 @@ AddonMenu::menu_action(MenuItem* item) { const Addon& addon = m_addon_manager.get_repository_addon(m_repository_addons[idx]); auto addon_id = addon.get_id(); - AddonManager::InstallStatusPtr status = m_addon_manager.request_install_addon(addon_id); + TransferStatusPtr status = m_addon_manager.request_install_addon(addon_id); status->then([this, addon_id]{ try