m_repository_addons(),
m_has_been_updated(false),
m_install_request(),
- m_install_status(),
m_transfer_status()
{
PHYSFS_mkdir(m_addon_directory.c_str());
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
{
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<InstallStatus>();
- return m_install_status;
+ return m_transfer_status;
}
}
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");
}
}
}
- // 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<InstallStatus>();
-
- return m_install_status;
+ return m_transfer_status;
}
}
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
m_downloader.abort(m_transfer_status->id);
m_install_request = {};
- m_install_status = {};
m_transfer_status = {};
}
class Addon;
class AddonRepository;
+class TransferStatus;
+using TransferStatusPtr = std::shared_ptr<TransferStatus>;
typedef std::string AddonId;
class AddonManager : public Currenton<AddonManager>
{
public:
- struct InstallStatus
- {
- InstallStatus() :
- now(0),
- total(0),
- done(false),
- callback()
- {}
-
- int now;
- int total;
- bool done;
- std::function<void ()> callback;
-
- void then(const std::function<void ()>& callback_)
- {
- callback = callback_;
- }
- };
-
struct InstallRequest
{
InstallRequest() :
std::string install_filename;
};
- using InstallStatusPtr = std::shared_ptr<InstallStatus>;
using InstallRequestPtr = std::shared_ptr<InstallRequest>;
private:
bool m_has_been_updated;
InstallRequestPtr m_install_request;
- InstallStatusPtr m_install_status;
TransferStatusPtr m_transfer_status;
public:
bool has_online_support() const;
bool has_been_updated() const;
void check_online();
- InstallStatusPtr request_check_online();
+ TransferStatusPtr request_check_online();
std::vector<AddonId> get_repository_addons() const;
std::vector<AddonId> get_installed_addons() const;
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);
m_transfers.erase(it);
status->status = TransferStatus::COMPLETED;
- if (status->callback)
+ for(auto& callback : status->callbacks)
{
- status->callback();
+ callback();
}
}
break;
int dlnow;
int ultotal;
int ulnow;
- std::function<void ()> callback;
+ std::vector<std::function<void ()> > callbacks;
TransferStatus(TransferId id_) :
id(id_),
dlnow(0),
ultotal(0),
ulnow(0),
- callback()
+ callbacks()
{}
- void then(const std::function<void ()>& callback_)
+ void then(const std::function<void ()>& callback)
{
- callback = callback_;
+ callbacks.push_back(callback);
}
};
-typedef std::shared_ptr<TransferStatus> TransferStatusPtr;
+using TransferStatusPtr = std::shared_ptr<TransferStatus>;
class Transfer;
#include "gui/menu_manager.hpp"
#include "util/gettext.hpp"
-AddonDialog::AddonDialog(AddonManager::InstallStatusPtr status) :
+AddonDialog::AddonDialog(TransferStatusPtr status) :
m_status(status),
m_title()
{
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());
#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<TransferStatus>;
+
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;
{
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();
{
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