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