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