Removed old Menu::check_menu() and replaced it with Menu::menu_action()
[supertux.git] / src / supertux / menu / addon_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
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
17 #include "supertux/menu/addon_menu.hpp"
18
19 #include <config.h>
20 #include <algorithm>
21 #include <boost/format.hpp>
22
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"
28
29 namespace {
30
31 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
32 {
33   return a1->title < a2->title;
34 }
35
36 } // namespace
37
38 AddonMenu::AddonMenu() :
39   m_addons()
40 {
41   refresh();
42 }
43
44 void
45 AddonMenu::refresh()
46 {
47   clear();
48
49   AddonManager& adm = AddonManager::get_instance();
50
51   // refresh list of addons
52   m_addons = adm.get_addons();
53
54   // sort list
55   std::sort(m_addons.begin(), m_addons.end(), generate_addons_menu_sorter);
56
57
58   add_label(_("Add-ons"));
59   add_hl();
60
61   // FIXME: don't use macro, use AddonManager::online_available() or so
62 #ifdef HAVE_LIBCURL
63   add_entry(0, std::string(_("Check Online")));
64 #else
65   add_inactive(0, std::string(_("Check Online (disabled)")));
66 #endif
67
68   //add_hl();
69
70   for (unsigned int i = 0; i < m_addons.size(); i++)
71   {
72     const Addon& addon = *m_addons[i];
73     std::string text = "";
74
75     if (!addon.kind.empty())
76     {
77       std::string kind = addon.kind;
78       if(addon.kind == "Levelset") {
79         kind = _("Levelset");
80       }
81       else if(addon.kind == "Worldmap") {
82         kind = _("Worldmap");
83       }
84       else if(addon.kind == "World") {
85         kind = _("World");
86       }
87       else if(addon.kind == "Level") {
88         kind = _("Level");
89       }
90
91
92       if(!addon.author.empty())
93       {
94         text = str(boost::format(_("%s \"%s\" by \"%s\""))
95                    % kind % addon.title % addon.author);
96       }
97       else
98       {
99         // Only addon type and name, no need for translation.
100         text = str(boost::format("%s \"%s\"")
101                    % kind % addon.title);
102       }
103     }
104     else
105     {
106       if (!addon.author.empty())
107       {
108         text = str(boost::format(_("\"%s\" by \"%s\""))
109                    % addon.title % addon.author);
110       }
111       else {
112         // Only addon name, no need for translation.
113         text = str(boost::format("\"%s\"")
114                    % addon.title);
115       }
116     }
117     add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
118   }
119
120   add_hl();
121   add_back(_("Back"));
122 }
123
124 void
125 AddonMenu::menu_action(MenuItem* item)
126 {
127   int index = item->id;
128
129   if (index == -1)
130   {
131     // do nothing
132   }
133   else if (index == 0) // check if "Check Online" was chosen
134   {
135     try
136     {
137       AddonManager::get_instance().check_online();
138       refresh();
139       set_active_item(index);
140     }
141     catch (std::exception& e)
142     {
143       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
144     }
145   }
146   else
147   {
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())
150     {
151       Addon& addon = *m_addons[index - ADDON_LIST_START_ID];
152       if (!addon.installed)
153       {
154         try
155         {
156           AddonManager::get_instance().install(&addon);
157         }
158         catch (std::exception& e)
159         {
160           log_warning << "Installing Add-on failed: " << e.what() << std::endl;
161         }
162         set_toggled(index, addon.loaded);
163       }
164       else if (!addon.loaded)
165       {
166         try
167         {
168           AddonManager::get_instance().enable(&addon);
169         }
170         catch (std::exception& e)
171         {
172           log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
173         }
174         set_toggled(index, addon.loaded);
175       }
176       else
177       {
178         try
179         {
180           AddonManager::get_instance().disable(&addon);
181         }
182         catch (std::exception& e)
183         {
184           log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
185         }
186         set_toggled(index, addon.loaded);
187       }
188     }
189   }
190 }
191
192 /* EOF */