src/supertux/menu/addon_menu.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "supertux/menu/addon_menu.hpp"
00018 
00019 #include <config.h>
00020 #include <algorithm>
00021 
00022 #include "addon/addon.hpp"
00023 #include "addon/addon_manager.hpp"
00024 #include "util/gettext.hpp"
00025 
00026 namespace {
00027 
00028 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
00029 {
00030   return a1->title < a2->title;
00031 }
00032 
00033 } // namespace
00034 
00035 AddonMenu::AddonMenu() :
00036   m_addons()
00037 {
00038   refresh();
00039 }
00040 
00041 void
00042 AddonMenu::refresh()
00043 {
00044   clear();
00045 
00046   AddonManager& adm = AddonManager::get_instance();
00047 
00048   // refresh list of addons
00049   m_addons = adm.get_addons();
00050   
00051   // sort list
00052   std::sort(m_addons.begin(), m_addons.end(), generate_addons_menu_sorter);
00053 
00054 
00055   add_label(_("Add-ons"));
00056   add_hl();
00057 
00058   // FIXME: don't use macro, use AddonManager::online_available() or so
00059 #ifdef HAVE_LIBCURL
00060   add_entry(0, std::string(_("Check Online")));
00061 #else
00062   add_inactive(0, std::string(_("Check Online (disabled)")));
00063 #endif
00064 
00065   //add_hl();
00066 
00067   for (unsigned int i = 0; i < m_addons.size(); i++) 
00068   {
00069     const Addon& addon = *m_addons[i];
00070     std::string text = "";
00071     
00072     if (!addon.kind.empty())
00073     {
00074       text += addon.kind + " ";
00075     }
00076     text += std::string("\"") + addon.title + "\"";
00077 
00078     if (!addon.author.empty())
00079     {
00080       text += " by \"" + addon.author + "\"";
00081     }
00082     add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
00083   }
00084 
00085   add_hl();
00086   add_back(_("Back"));
00087 }
00088 
00089 void
00090 AddonMenu::check_menu()
00091 {
00092   int index = check();
00093 
00094   if (index == -1) 
00095   {
00096     // do nothing
00097   }
00098   else if (index == 0) // check if "Check Online" was chosen
00099   {
00100     try 
00101     {
00102       AddonManager::get_instance().check_online();
00103       refresh();
00104       set_active_item(index);
00105     } 
00106     catch (std::exception& e)
00107     {
00108       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
00109     }
00110   }
00111   else
00112   {
00113     // if one of the Addons listed was chosen, take appropriate action
00114     if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + m_addons.size()) 
00115     {
00116       Addon& addon = *m_addons[index - ADDON_LIST_START_ID];
00117       if (!addon.installed) 
00118       {
00119         try 
00120         {
00121           AddonManager::get_instance().install(&addon);
00122         } 
00123         catch (std::exception& e) 
00124         {
00125           log_warning << "Installing Add-on failed: " << e.what() << std::endl;
00126         }
00127         set_toggled(index, addon.loaded);
00128       } 
00129       else if (!addon.loaded) 
00130       {
00131         try 
00132         {
00133           AddonManager::get_instance().enable(&addon);
00134         } 
00135         catch (std::exception& e) 
00136         {
00137           log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
00138         }
00139         set_toggled(index, addon.loaded);
00140       } 
00141       else 
00142       {
00143         try 
00144         {
00145           AddonManager::get_instance().disable(&addon);
00146         } 
00147         catch (std::exception& e) 
00148         {
00149           log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
00150         }
00151         set_toggled(index, addon.loaded);
00152       }
00153     }
00154   }
00155 }
00156 
00157 /* EOF */

Generated on Mon Jun 9 03:38:22 2014 for SuperTux by  doxygen 1.5.1