addon_browse_menu.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SuperTux
  2. // Copyright (C) 2022 Vankata453
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/menu/addon_browse_menu.hpp"
  17. #include <fmt/format.h>
  18. #include "addon/addon.hpp"
  19. #include "addon/addon_manager.hpp"
  20. #include "gui/dialog.hpp"
  21. #include "gui/menu_item.hpp"
  22. #include "gui/menu_manager.hpp"
  23. #include "supertux/menu/addon_preview_menu.hpp"
  24. #include "supertux/menu/download_dialog.hpp"
  25. #include "util/log.hpp"
  26. #define IS_REPOSITORY_MENU_ID(idx) (((idx) - MNID_ADDON_LIST_START) % 2 == 0)
  27. #define MAKE_REPOSITORY_MENU_ID(idx) (MNID_ADDON_LIST_START + 2 * (idx) + 0)
  28. #define UNPACK_REPOSITORY_MENU_ID(idx) ((((idx) - MNID_ADDON_LIST_START) - 0) / 2)
  29. AddonBrowseMenu::AddonBrowseMenu(bool langpacks_only, bool auto_install_langpack) :
  30. m_addon_manager(*AddonManager::current()),
  31. m_repository_addons(),
  32. m_langpacks_only(langpacks_only),
  33. m_auto_install_langpack(auto_install_langpack),
  34. m_browse_page(1),
  35. m_max_addons_on_page(10)
  36. {
  37. refresh();
  38. if (auto_install_langpack)
  39. {
  40. const std::string& language = g_dictionary_manager->get_language().get_language();
  41. if (language == "en")
  42. return;
  43. check_online();
  44. }
  45. }
  46. AddonBrowseMenu::~AddonBrowseMenu()
  47. {
  48. }
  49. void
  50. AddonBrowseMenu::refresh()
  51. {
  52. m_repository_addons = m_addon_manager.get_repository_addons();
  53. rebuild_menu();
  54. }
  55. void
  56. AddonBrowseMenu::rebuild_menu()
  57. {
  58. clear();
  59. add_label(m_langpacks_only ? _("Browse Language Packs") : _("Browse Add-ons"));
  60. add_hl();
  61. int idx = 0;
  62. int addons_count = 0;
  63. std::vector<int> addons_to_list;
  64. for (const auto& addon_id : m_repository_addons)
  65. {
  66. const Addon& addon = m_addon_manager.get_repository_addon(addon_id);
  67. try
  68. {
  69. // Add-on is already installed, so check if they are the same.
  70. Addon& installed_addon = m_addon_manager.get_installed_addon(addon_id);
  71. if (installed_addon.get_md5() == addon.get_md5() ||
  72. installed_addon.get_version() > addon.get_version())
  73. {
  74. log_debug << "ignoring already installed add-on " << installed_addon.get_id() << std::endl;
  75. }
  76. }
  77. catch(const std::exception&)
  78. {
  79. // Add-on is not installed.
  80. if (addon.is_visible())
  81. {
  82. if ((m_langpacks_only && addon.get_type() == Addon::LANGUAGEPACK) || !m_langpacks_only)
  83. {
  84. if (addons_count >= m_max_addons_on_page * (m_browse_page - 1) && addons_count < m_max_addons_on_page * m_browse_page) {
  85. addons_to_list.push_back(idx);
  86. }
  87. addons_count++;
  88. }
  89. }
  90. }
  91. idx += 1;
  92. }
  93. int last_browse_page = 0;
  94. if (addons_count > 0)
  95. {
  96. last_browse_page = static_cast<int>(std::ceil(static_cast<double>(addons_count) / static_cast<double>(m_max_addons_on_page)));
  97. add_inactive(fmt::format(fmt::runtime(_("Page {}/{}")), m_browse_page, last_browse_page));
  98. add_hl();
  99. }
  100. for (const auto& index : addons_to_list)
  101. {
  102. const std::string text = addon_string_util::generate_menu_item_text(m_addon_manager.get_repository_addon(m_repository_addons[index]));
  103. add_entry(MAKE_REPOSITORY_MENU_ID(index), text);
  104. }
  105. if (addons_count <= 0 && m_addon_manager.has_been_updated())
  106. {
  107. add_inactive(m_langpacks_only ? _("No new language packs available") : _("No new Add-ons available"));
  108. }
  109. else if (addons_count <= 0) {
  110. add_inactive(m_langpacks_only ? _("No language packs available") : _("No Add-ons available"));
  111. }
  112. add_hl();
  113. if (addons_count > 0)
  114. {
  115. if (m_browse_page <= 1)
  116. {
  117. add_inactive(_("Previous page"));
  118. }
  119. else
  120. {
  121. add_entry(MNID_PREV_PAGE, _("Previous page"));
  122. }
  123. if (m_browse_page >= last_browse_page)
  124. {
  125. add_inactive(_("Next page"));
  126. }
  127. else
  128. {
  129. add_entry(MNID_NEXT_PAGE, _("Next page"));
  130. }
  131. add_hl();
  132. }
  133. if (!m_addon_manager.has_online_support())
  134. {
  135. add_inactive(_("Check Online (disabled)"));
  136. }
  137. else
  138. {
  139. add_entry(MNID_CHECK_ONLINE, _("Check Online"));
  140. }
  141. add_hl();
  142. add_back(_("Back"));
  143. }
  144. void
  145. AddonBrowseMenu::check_online()
  146. {
  147. try
  148. {
  149. TransferStatusPtr status = m_addon_manager.request_check_online();
  150. status->then([this](bool success)
  151. {
  152. if (success)
  153. {
  154. if (m_auto_install_langpack)
  155. {
  156. const std::string& langpack_id = "language-pack";
  157. MenuManager::instance().push_menu(std::make_unique<AddonPreviewMenu>(m_addon_manager.get_repository_addon(langpack_id), true), true);
  158. }
  159. else
  160. {
  161. refresh();
  162. }
  163. }
  164. else
  165. {
  166. if (m_auto_install_langpack)
  167. {
  168. MenuManager::instance().set_dialog({});
  169. MenuManager::instance().clear_menu_stack();
  170. }
  171. }
  172. });
  173. auto dialog = std::make_unique<DownloadDialog>(status, false);
  174. dialog->set_title(_("Downloading Add-On Repository Index"));
  175. MenuManager::instance().set_dialog(std::move(dialog));
  176. }
  177. catch (std::exception& e)
  178. {
  179. log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
  180. }
  181. }
  182. void
  183. AddonBrowseMenu::menu_action(MenuItem& item)
  184. {
  185. const int index = item.get_id();
  186. if (index == MNID_CHECK_ONLINE) // Check if "Check Online" was chosen.
  187. {
  188. check_online();
  189. }
  190. else if (index == MNID_PREV_PAGE || index == MNID_NEXT_PAGE) {
  191. index == MNID_PREV_PAGE ? m_browse_page-- : m_browse_page++;
  192. rebuild_menu();
  193. set_active_item(index);
  194. if (get_active_item_id() != index) // Check if the item wasn't set as active, because it's disabled.
  195. {
  196. set_active_item(index == MNID_PREV_PAGE ? MNID_NEXT_PAGE : MNID_PREV_PAGE);
  197. }
  198. }
  199. else if (IS_REPOSITORY_MENU_ID(index))
  200. {
  201. int idx = UNPACK_REPOSITORY_MENU_ID(index);
  202. if (0 <= idx && idx < static_cast<int>(m_repository_addons.size()))
  203. {
  204. const Addon& addon = m_addon_manager.get_repository_addon(m_repository_addons[idx]);
  205. MenuManager::instance().push_menu(std::make_unique<AddonPreviewMenu>(addon));
  206. }
  207. }
  208. else
  209. {
  210. log_warning << "Unknown menu item clicked: " << index << std::endl;
  211. }
  212. }
  213. /* EOF */