Implemented non-blocking download for the repository index list
[supertux.git] / src / supertux / menu / addon_menu.cpp
index 43ed81c..5011e95 100644 (file)
 #include "addon/addon_manager.hpp"
 #include "gui/menu.hpp"
 #include "gui/menu_item.hpp"
+#include "gui/menu_manager.hpp"
+#include "supertux/menu/addon_dialog.hpp"
 #include "util/gettext.hpp"
 
+namespace {
+
+#define IS_REPOSITORY_MENU_ID(idx) ((idx - MNID_ADDON_LIST_START) % 2 == 0)
+#define IS_INSTALLED_MENU_ID(idx) ((idx - MNID_ADDON_LIST_START) % 2 == 1)
+
+#define MAKE_REPOSITORY_MENU_ID(idx) (MNID_ADDON_LIST_START + 2*idx+0)
+#define MAKE_INSTALLED_MENU_ID(idx) (MNID_ADDON_LIST_START + 2*idx+1)
+
+#define UNPACK_REPOSITORY_MENU_ID(idx) (((idx - MNID_ADDON_LIST_START) - 0) / 2)
+#define UNPACK_INSTALLED_MENU_ID(idx) (((idx - MNID_ADDON_LIST_START) - 1) / 2)
+
+std::string addon_type_to_translated_string(Addon::Type type)
+{
+  switch (type)
+  {
+    case Addon::LEVELSET:
+      return _("Levelset");
+
+    case Addon::WORLDMAP:
+      return _("Worldmap");
+
+    case Addon::WORLD:
+      return _("World");
+
+    default:
+      return _("Unknown");
+  }
+}
+
+std::string generate_menu_item_text(const Addon& addon)
+{
+  std::string text;
+  std::string type = addon_type_to_translated_string(addon.get_type());
+
+  if(!addon.get_author().empty())
+  {
+    text = str(boost::format(_("%s \"%s\" by \"%s\""))
+               % type % addon.get_title() % addon.get_author());
+  }
+  else
+  {
+    // Only addon type and name, no need for translation.
+    text = str(boost::format("%s \"%s\"")
+               % type % addon.get_title());
+  }
+
+  return text;
+}
+
+} // namespace
+
 AddonMenu::AddonMenu() :
-  m_addon_manager(*AddonManager::current())
+  m_addon_manager(*AddonManager::current()),
+  m_installed_addons(),
+  m_repository_addons()
 {
   refresh();
 }
@@ -35,84 +90,87 @@ AddonMenu::AddonMenu() :
 void
 AddonMenu::refresh()
 {
-  clear();
+  m_installed_addons = m_addon_manager.get_installed_addons();
+  m_repository_addons = m_addon_manager.get_repository_addons();
 
-  // refresh list of addons
-  const auto& addons_ref = m_addon_manager.get_addons();
-  std::vector<std::reference_wrapper<Addon> > addons;
-  std::transform(addons_ref.begin(), addons_ref.end(), std::back_inserter(addons),
-                 [](const std::unique_ptr<Addon>& addon) -> Addon& {
-                   return *addon.get();
-                 });
-
-  // sort list
-  std::sort(addons.begin(), addons.end(),
-            [](const Addon& lhs, const Addon& rhs)
-            {
-              return lhs.title < lhs.title;
-            });
+  rebuild_menu();
+}
 
+void
+AddonMenu::rebuild_menu()
+{
+  clear();
   add_label(_("Add-ons"));
   add_hl();
 
-  if (!m_addon_manager.has_online_support())
+
+  if (m_installed_addons.empty())
   {
-    add_inactive(MNID_CHECK_ONLINE, std::string(_("Check Online (disabled)")));
+    add_inactive(MNID_NOTHING_NEW, _("No Addons installed"));
   }
   else
   {
-    add_entry(MNID_CHECK_ONLINE, std::string(_("Check Online")));
+    int idx = 0;
+    for (const auto& addon_id : m_installed_addons)
+    {
+      const Addon& addon = m_addon_manager.get_installed_addon(addon_id);
+      std::string text = generate_menu_item_text(addon);
+      add_toggle(MAKE_INSTALLED_MENU_ID(idx), text, addon.is_enabled());
+      idx += 1;
+    }
   }
 
-  //add_hl();
+  add_hl();
 
-  for (auto& addon_ : addons)
   {
-    Addon& addon = addon_.get();
-    std::string text = "";
-
-    if (!addon.kind.empty())
+    bool have_new_stuff = false;
+    int idx = 0;
+    for (const auto& addon_id : m_repository_addons)
     {
-      std::string kind = addon.kind;
-      if(addon.kind == "Levelset") {
-        kind = _("Levelset");
-      }
-      else if(addon.kind == "Worldmap") {
-        kind = _("Worldmap");
-      }
-      else if(addon.kind == "World") {
-        kind = _("World");
-      }
-      else if(addon.kind == "Level") {
-        kind = _("Level");
-      }
-
-      if(!addon.author.empty())
+      const Addon& addon = m_addon_manager.get_repository_addon(addon_id);
+      try
       {
-        text = str(boost::format(_("%s \"%s\" by \"%s\""))
-                   % kind % addon.title % addon.author);
+        // addon is already installed, so check if they are the same
+        Addon& installed_addon = m_addon_manager.get_installed_addon(addon_id);
+        if (installed_addon.get_md5() == addon.get_md5() ||
+            installed_addon.get_version() > addon.get_version())
+        {
+          log_debug << "ignoring already installed addon " << installed_addon.get_id() << std::endl;
+        }
+        else
+        {
+          log_debug << installed_addon.get_id() << " is installed, but updated: '"
+                    << installed_addon.get_md5() << "' vs '" << addon.get_md5() << "'  '"
+                    << installed_addon.get_version() << "' vs '" << addon.get_version() << "'"
+                    << std::endl;
+          std::string text = generate_menu_item_text(addon);
+          add_entry(MAKE_REPOSITORY_MENU_ID(idx), "Install " + text + " *NEW*");
+          have_new_stuff = true;
+        }
       }
-      else
+      catch(const std::exception& err)
       {
-        // Only addon type and name, no need for translation.
-        text = str(boost::format("%s \"%s\"")
-                   % kind % addon.title);
+        // addon is not installed
+        std::string text = generate_menu_item_text(addon);
+        add_entry(MAKE_REPOSITORY_MENU_ID(idx), "Install " + text);
+        have_new_stuff = true;
       }
+      idx += 1;
     }
-    else
+
+    if (!have_new_stuff && m_addon_manager.has_been_updated())
     {
-      if (!addon.author.empty())
-      {
-        text = str(boost::format(_("\"%s\" by \"%s\""))
-                   % addon.title % addon.author);
-      }
-      else {
-        // Only addon name, no need for translation.
-        text = str(boost::format("\"%s\"")
-                   % addon.title);
-      }
+      add_inactive(MNID_NOTHING_NEW, _("No new Addons found"));
     }
-    add_toggle(MNID_ADDON_LIST_START + addon.id, text, addon.loaded);
+  }
+
+  if (!m_addon_manager.has_online_support())
+  {
+    add_inactive(MNID_CHECK_ONLINE, std::string(_("Check Online (disabled)")));
+  }
+  else
+  {
+    add_entry(MNID_CHECK_ONLINE, std::string(_("Check Online")));
   }
 
   add_hl();
@@ -126,56 +184,72 @@ AddonMenu::menu_action(MenuItem* item)
   {
     try
     {
-      m_addon_manager.check_online();
-      refresh();
-      set_active_item(item->id);
+      AddonManager::InstallStatusPtr status = m_addon_manager.request_check_online();
+      status->then([this]{
+          MenuManager::instance().set_dialog({});
+          refresh();
+        });
+      std::unique_ptr<AddonDialog> dialog(new AddonDialog(status));
+      dialog->set_title("Downloading Add-On Repository Index");
+      MenuManager::instance().set_dialog(std::move(dialog));
     }
     catch (std::exception& e)
     {
       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
     }
   }
-  else if ((MNID_ADDON_LIST_START <= item->id) && (item->id < MNID_ADDON_LIST_START + m_addon_manager.get_num_addons()))
+  else if (MNID_ADDON_LIST_START <= item->id)
   {
-    int addon_id = item->id - MNID_ADDON_LIST_START;
-    Addon& addon = m_addon_manager.get_addon(addon_id);
-    if (!addon.installed)
+    if (IS_INSTALLED_MENU_ID(item->id))
     {
-      try
+      int idx = UNPACK_INSTALLED_MENU_ID(item->id);
+      if (0 <= idx && idx < static_cast<int>(m_installed_addons.size()))
       {
-        m_addon_manager.install(addon);
+        const Addon& addon = m_addon_manager.get_installed_addon(m_installed_addons[idx]);
+        if(addon.is_enabled())
+        {
+          m_addon_manager.disable_addon(addon.get_id());
+          set_toggled(item->id, addon.is_enabled());
+        }
+        else
+        {
+          m_addon_manager.enable_addon(addon.get_id());
+          set_toggled(item->id, addon.is_enabled());
+        }
       }
-      catch (std::exception& e)
-      {
-        log_warning << "Installing Add-on failed: " << e.what() << std::endl;
-      }
-      set_toggled(item->id, addon.loaded);
-    }
-    else if (!addon.loaded)
-    {
-      try
-      {
-        m_addon_manager.enable(addon);
-      }
-      catch (std::exception& e)
-      {
-        log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
-      }
-      set_toggled(item->id, addon.loaded);
     }
-    else
+    else if (IS_REPOSITORY_MENU_ID(item->id))
     {
-      try
-      {
-        m_addon_manager.disable(addon);
-      }
-      catch (std::exception& e)
+      int idx = UNPACK_REPOSITORY_MENU_ID(item->id);
+      if (0 <= idx && idx < static_cast<int>(m_repository_addons.size()))
       {
-        log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
+        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);
+
+        status->then([this, addon_id]{
+            try
+            {
+              m_addon_manager.enable_addon(addon_id);
+            }
+            catch(const std::exception& err)
+            {
+              log_warning << "Enabling addon failed: " << err.what() << std::endl;
+            }
+            MenuManager::instance().set_dialog({});
+            refresh();
+          });
+
+        std::unique_ptr<AddonDialog> dialog(new AddonDialog(status));
+        dialog->set_title("Downloading " + generate_menu_item_text(addon));
+        MenuManager::instance().set_dialog(std::move(dialog));
       }
-      set_toggled(item->id, addon.loaded);
     }
   }
+  else
+  {
+    log_warning << "Unknown menu item clicked: " << item->id << std::endl;
+  }
 }
 
 /* EOF */