Renamed namespaces to all lowercase
[supertux.git] / src / supertux / menu / addon_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
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
22 #include "addon/addon.hpp"
23 #include "addon/addon_manager.hpp"
24 #include "util/gettext.hpp"
25
26 namespace {
27
28 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
29 {
30   return a1->title < a2->title;
31 }
32
33 } // namespace
34
35 AddonMenu::AddonMenu() :
36   m_addons()
37 {
38   refresh();
39 }
40
41 void
42 AddonMenu::refresh()
43 {
44   clear();
45
46   AddonManager& adm = AddonManager::get_instance();
47
48   // refresh list of addons
49   m_addons = adm.get_addons();
50   
51   // sort list
52   std::sort(m_addons.begin(), m_addons.end(), generate_addons_menu_sorter);
53
54
55   add_label(_("Add-ons"));
56   add_hl();
57
58   // FIXME: don't use macro, use AddonManager::online_available() or so
59 #ifdef HAVE_LIBCURL
60   add_entry(0, std::string(_("Check Online")));
61 #else
62   add_inactive(0, std::string(_("Check Online (disabled)")));
63 #endif
64
65   //add_hl();
66
67   for (unsigned int i = 0; i < m_addons.size(); i++) 
68   {
69     const Addon& addon = *m_addons[i];
70     std::string text = "";
71     
72     if (!addon.kind.empty())
73     {
74       text += addon.kind + " ";
75     }
76     text += std::string("\"") + addon.title + "\"";
77
78     if (!addon.author.empty())
79     {
80       text += " by \"" + addon.author + "\"";
81     }
82     add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
83   }
84
85   add_hl();
86   add_back(_("Back"));
87 }
88
89 void
90 AddonMenu::check_menu()
91 {
92   int index = check();
93
94   if (index == -1) 
95   {
96     // do nothing
97   }
98   else if (index == 0) // check if "Check Online" was chosen
99   {
100     try 
101     {
102       AddonManager::get_instance().check_online();
103       refresh();
104       set_active_item(index);
105     } 
106     catch (std::exception& e)
107     {
108       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
109     }
110   }
111   else
112   {
113     // if one of the Addons listed was chosen, take appropriate action
114     if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + m_addons.size()) 
115     {
116       Addon& addon = *m_addons[index - ADDON_LIST_START_ID];
117       if (!addon.installed) 
118       {
119         try 
120         {
121           AddonManager::get_instance().install(&addon);
122         } 
123         catch (std::exception& e) 
124         {
125           log_warning << "Installing Add-on failed: " << e.what() << std::endl;
126         }
127         set_toggled(index, addon.loaded);
128       } 
129       else if (!addon.loaded) 
130       {
131         try 
132         {
133           AddonManager::get_instance().enable(&addon);
134         } 
135         catch (std::exception& e) 
136         {
137           log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
138         }
139         set_toggled(index, addon.loaded);
140       } 
141       else 
142       {
143         try 
144         {
145           AddonManager::get_instance().disable(&addon);
146         } 
147         catch (std::exception& e) 
148         {
149           log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
150         }
151         set_toggled(index, addon.loaded);
152       }
153     }
154   }
155 }
156
157 /* EOF */