2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
17 #include "supertux/menu/addon_menu.hpp"
21 #include <boost/format.hpp>
23 #include "addon/addon.hpp"
24 #include "addon/addon_manager.hpp"
25 #include "gui/menu.hpp"
26 #include "gui/menu_item.hpp"
27 #include "util/gettext.hpp"
31 #define IS_REPOSITORY_MENU_ID(idx) ((idx - MNID_ADDON_LIST_START) % 2 == 0)
32 #define IS_INSTALLED_MENU_ID(idx) ((idx - MNID_ADDON_LIST_START) % 2 == 1)
34 #define MAKE_REPOSITORY_MENU_ID(idx) (MNID_ADDON_LIST_START + 2*idx+0)
35 #define MAKE_INSTALLED_MENU_ID(idx) (MNID_ADDON_LIST_START + 2*idx+1)
37 #define UNPACK_REPOSITORY_MENU_ID(idx) (((idx - MNID_ADDON_LIST_START) - 0) / 2)
38 #define UNPACK_INSTALLED_MENU_ID(idx) (((idx - MNID_ADDON_LIST_START) - 1) / 2)
40 std::string addon_type_to_translated_string(Addon::Type type)
58 std::string generate_menu_item_text(const Addon& addon)
61 std::string type = addon_type_to_translated_string(addon.get_type());
63 if(!addon.get_author().empty())
65 text = str(boost::format(_("%s \"%s\" by \"%s\""))
66 % type % addon.get_title() % addon.get_author());
70 // Only addon type and name, no need for translation.
71 text = str(boost::format("%s \"%s\"")
72 % type % addon.get_title());
80 AddonMenu::AddonMenu() :
81 m_addon_manager(*AddonManager::current()),
91 m_installed_addons = m_addon_manager.get_installed_addons();
92 m_repository_addons = m_addon_manager.get_repository_addons();
95 std::sort(m_addons.begin(), m_addons.end(),
96 [](const Addon& lhs, const Addon& rhs)
98 return lhs.title < lhs.title;
106 AddonMenu::rebuild_menu()
109 add_label(_("Add-ons"));
113 if (!m_installed_addons.empty())
116 for (const auto& addon_id : m_installed_addons)
118 const Addon& addon = m_addon_manager.get_installed_addon(addon_id);
119 std::string text = generate_menu_item_text(addon);
120 add_toggle(MAKE_INSTALLED_MENU_ID(idx), text, addon.is_enabled());
127 if (!m_addon_manager.has_online_support())
129 add_inactive(MNID_CHECK_ONLINE, std::string(_("Check Online (disabled)")));
133 add_entry(MNID_CHECK_ONLINE, std::string(_("Check Online")));
138 for (const auto& addon_id : m_repository_addons)
140 const Addon& addon = m_addon_manager.get_repository_addon(addon_id);
141 std::string text = generate_menu_item_text(addon);
142 add_entry(MAKE_REPOSITORY_MENU_ID(idx), "Install " + text);
152 AddonMenu::menu_action(MenuItem* item)
154 if (item->id == MNID_CHECK_ONLINE) // check if "Check Online" was chosen
158 m_addon_manager.check_online();
161 catch (std::exception& e)
163 log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
166 else if (MNID_ADDON_LIST_START <= item->id)
168 if (IS_INSTALLED_MENU_ID(item->id))
170 int idx = UNPACK_INSTALLED_MENU_ID(item->id);
171 if (0 <= idx && idx < static_cast<int>(m_installed_addons.size()))
173 const Addon& addon = m_addon_manager.get_installed_addon(m_installed_addons[idx]);
174 if(addon.is_enabled())
176 m_addon_manager.enable_addon(addon.get_id());
177 set_toggled(item->id, addon.is_enabled());
181 m_addon_manager.enable_addon(addon.get_id());
182 set_toggled(item->id, addon.is_enabled());
186 else if (IS_REPOSITORY_MENU_ID(item->id))
188 int idx = UNPACK_REPOSITORY_MENU_ID(item->id);
189 if (0 <= idx && idx < static_cast<int>(m_repository_addons.size()))
191 const Addon& addon = m_addon_manager.get_repository_addon(m_repository_addons[idx]);
194 m_addon_manager.install_addon(addon.get_id());
195 m_addon_manager.enable_addon(addon.get_id());
197 catch(const std::exception& err)
199 log_warning << "Enabling addon failed: " << err.what() << std::endl;
207 log_warning << "Unknown menu item clicked: " << item->id << std::endl;