Updated email address, grumbel@gmx.de -> grumbel@gmail.com
[supertux.git] / src / supertux / menu / contrib_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/contrib_menu.hpp"
18
19 #include <physfs.h>
20 #include <sstream>
21
22 #include "gui/menu_manager.hpp"
23 #include "supertux/game_manager.hpp"
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/menu/contrib_world_menu.hpp"
26 #include "supertux/menu/menu_storage.hpp"
27 #include "supertux/title_screen.hpp"
28 #include "supertux/world.hpp"
29 #include "util/file_system.hpp"
30 #include "util/gettext.hpp"
31
32 ContribMenu::ContribMenu() :
33   m_contrib_worlds()
34 {
35   /** Generating contrib levels list by making use of Level Subset  */
36   std::vector<std::string> level_worlds;
37   char** files = PHYSFS_enumerateFiles("levels/");
38   for(const char* const* filename = files; *filename != 0; ++filename) {
39     std::string filepath = std::string("levels/") + *filename;
40     if(PHYSFS_isDirectory(filepath.c_str()))
41       level_worlds.push_back(filepath);
42   }
43   PHYSFS_freeList(files);
44
45   add_label(_("Contrib Levels"));
46   add_hl();
47
48   int i = 0;
49   for (std::vector<std::string>::const_iterator it = level_worlds.begin(); it != level_worlds.end(); ++it)
50   {
51     try
52     {
53       std::unique_ptr<World> world (new World);
54
55       world->load(*it + "/info");
56
57       if (!world->hide_from_contribs) 
58       {
59         { // FIXME: yuck, this should be easier
60           std::ostringstream stream;
61           std::string worlddirname = FileSystem::basename(*it);
62           stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
63           std::string slotfile = stream.str();
64           world->set_savegame_filename(stream.str());
65           world->load_state();
66         }
67
68         std::ostringstream title;
69         title << world->get_title() << " (" << world->get_num_solved_levels() << "/" << world->get_num_levels() << ")";
70         add_entry(i++, title.str());
71         m_contrib_worlds.push_back(std::move(world));
72       }
73     }
74     catch(std::exception& e)
75     {
76       log_info << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
77     }
78   }
79
80   add_hl();
81   add_back(_("Back"));
82 }
83
84 ContribMenu::~ContribMenu()
85 {
86 }
87
88 void
89 ContribMenu::check_menu()
90 {
91   int index = check();
92   if (index != -1)
93   {
94     World* world = m_contrib_worlds[index].get();
95     if (!world->is_levelset) 
96     {
97       // FIXME: not the most elegant of solutions to std::move() the
98       // World, but the ContribMenu should get destructed after this,
99       // so it might be ok
100       GameManager::current()->start_game(std::move(m_contrib_worlds[index]));
101     }
102     else 
103     {
104       MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribWorldMenu(std::move(m_contrib_worlds[index]))));
105     }
106   }
107 }
108
109 /* EOF */