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 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
33 return a1->title < a2->title;
38 AddonMenu::AddonMenu() :
49 AddonManager& adm = AddonManager::get_instance();
51 // refresh list of addons
52 m_addons = adm.get_addons();
55 std::sort(m_addons.begin(), m_addons.end(), generate_addons_menu_sorter);
58 add_label(_("Add-ons"));
61 // FIXME: don't use macro, use AddonManager::online_available() or so
63 add_entry(0, std::string(_("Check Online")));
65 add_inactive(0, std::string(_("Check Online (disabled)")));
70 for (unsigned int i = 0; i < m_addons.size(); i++)
72 const Addon& addon = *m_addons[i];
73 std::string text = "";
75 if (!addon.kind.empty())
77 std::string kind = addon.kind;
78 if(addon.kind == "Levelset") {
81 else if(addon.kind == "Worldmap") {
84 else if(addon.kind == "World") {
87 else if(addon.kind == "Level") {
92 if(!addon.author.empty())
94 text = str(boost::format(_("%s \"%s\" by \"%s\""))
95 % kind % addon.title % addon.author);
99 // Only addon type and name, no need for translation.
100 text = str(boost::format("%s \"%s\"")
101 % kind % addon.title);
106 if (!addon.author.empty())
108 text = str(boost::format(_("\"%s\" by \"%s\""))
109 % addon.title % addon.author);
112 // Only addon name, no need for translation.
113 text = str(boost::format("\"%s\"")
117 add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
125 AddonMenu::menu_action(MenuItem* item)
127 int index = item->id;
133 else if (index == 0) // check if "Check Online" was chosen
137 AddonManager::get_instance().check_online();
139 set_active_item(index);
141 catch (std::exception& e)
143 log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
148 // if one of the Addons listed was chosen, take appropriate action
149 if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + m_addons.size())
151 Addon& addon = *m_addons[index - ADDON_LIST_START_ID];
152 if (!addon.installed)
156 AddonManager::get_instance().install(&addon);
158 catch (std::exception& e)
160 log_warning << "Installing Add-on failed: " << e.what() << std::endl;
162 set_toggled(index, addon.loaded);
164 else if (!addon.loaded)
168 AddonManager::get_instance().enable(&addon);
170 catch (std::exception& e)
172 log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
174 set_toggled(index, addon.loaded);
180 AddonManager::get_instance().disable(&addon);
182 catch (std::exception& e)
184 log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
186 set_toggled(index, addon.loaded);