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"
29 AddonMenu::AddonMenu() :
30 m_addon_manager(*AddonManager::current())
40 // refresh list of addons
41 const auto& addons_ref = m_addon_manager.get_addons();
42 std::vector<std::reference_wrapper<Addon> > addons;
43 std::transform(addons_ref.begin(), addons_ref.end(), std::back_inserter(addons),
44 [](const std::unique_ptr<Addon>& addon) -> Addon& {
49 std::sort(addons.begin(), addons.end(),
50 [](const Addon& lhs, const Addon& rhs)
52 return lhs.title < lhs.title;
55 add_label(_("Add-ons"));
58 if (!m_addon_manager.has_online_support())
60 add_inactive(MNID_CHECK_ONLINE, std::string(_("Check Online (disabled)")));
64 add_entry(MNID_CHECK_ONLINE, std::string(_("Check Online")));
69 for (auto& addon_ : addons)
71 Addon& addon = addon_.get();
72 std::string text = "";
74 if (!addon.kind.empty())
76 std::string kind = addon.kind;
77 if(addon.kind == "Levelset") {
80 else if(addon.kind == "Worldmap") {
83 else if(addon.kind == "World") {
86 else if(addon.kind == "Level") {
90 if(!addon.author.empty())
92 text = str(boost::format(_("%s \"%s\" by \"%s\""))
93 % kind % addon.title % addon.author);
97 // Only addon type and name, no need for translation.
98 text = str(boost::format("%s \"%s\"")
99 % kind % addon.title);
104 if (!addon.author.empty())
106 text = str(boost::format(_("\"%s\" by \"%s\""))
107 % addon.title % addon.author);
110 // Only addon name, no need for translation.
111 text = str(boost::format("\"%s\"")
115 add_toggle(MNID_ADDON_LIST_START + addon.id, text, addon.loaded);
123 AddonMenu::menu_action(MenuItem* item)
125 if (item->id == MNID_CHECK_ONLINE) // check if "Check Online" was chosen
129 m_addon_manager.check_online();
131 set_active_item(item->id);
133 catch (std::exception& e)
135 log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
138 else if ((MNID_ADDON_LIST_START <= item->id) && (item->id < MNID_ADDON_LIST_START + m_addon_manager.get_num_addons()))
140 int addon_id = item->id - MNID_ADDON_LIST_START;
141 Addon& addon = m_addon_manager.get_addon(addon_id);
142 if (!addon.installed)
146 m_addon_manager.install(addon);
148 catch (std::exception& e)
150 log_warning << "Installing Add-on failed: " << e.what() << std::endl;
152 set_toggled(item->id, addon.loaded);
154 else if (!addon.loaded)
158 m_addon_manager.enable(addon);
160 catch (std::exception& e)
162 log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
164 set_toggled(item->id, addon.loaded);
170 m_addon_manager.disable(addon);
172 catch (std::exception& e)
174 log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
176 set_toggled(item->id, addon.loaded);