1 //  SuperTux
2 //  Copyright (C) 2008 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/profile_menu.hpp"
18 
19 #include <boost/format.hpp>
20 #include <sstream>
21 
22 #include "gui/dialog.hpp"
23 #include "gui/menu_manager.hpp"
24 #include "gui/menu_item.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/globals.hpp"
27 #include "util/file_system.hpp"
28 #include "util/gettext.hpp"
29 
30 #include <physfs.h>
31 
ProfileMenu()32 ProfileMenu::ProfileMenu()
33 {
34   add_label(_("Select Profile"));
35   add_hl();
36   for (int i = 1; i <= 5; ++i)
37   {
38     std::ostringstream out;
39     if (i == g_config->profile)
40     {
41       out << str(boost::format(_("[Profile %s]")) %i);
42     }
43     else
44     {
45       out << str(boost::format(_("Profile %s")) %i);
46     }
47     add_entry(i, out.str());
48   }
49   add_hl();
50   add_entry(6, _("Reset profile"));
51   add_entry(7, _("Reset all profiles"));
52 
53   add_hl();
54   add_back(_("Back"));
55 }
56 
57 void
menu_action(MenuItem & item)58 ProfileMenu::menu_action(MenuItem& item)
59 {
60   const auto& id = item.get_id();
61   if(id <= 5)
62   {
63     g_config->profile = item.get_id();
64   }
65   else if(id == 6)
66   {
67     Dialog::show_confirmation(_("Deleting your profile will reset your game progress. Are you sure?"), [this]() {
68       delete_savegames(g_config->profile);
69     });
70   }
71   else if(id == 7)
72   {
73     Dialog::show_confirmation(_("This will reset your game progress on all profiles. Are you sure?"), [this]() {
74       for (int i = 1; i <= 5; i++) {
75         delete_savegames(i);
76       }
77     });
78   }
79   MenuManager::instance().clear_menu_stack();
80 }
81 
82 void
delete_savegames(int idx) const83 ProfileMenu::delete_savegames(int idx) const
84 {
85   const auto& profile_path = "profile" + std::to_string(idx);
86   std::unique_ptr<char*, decltype(&PHYSFS_freeList)>
87     files(PHYSFS_enumerateFiles(profile_path.c_str()),
88           PHYSFS_freeList);
89   for (const char* const* filename = files.get(); *filename != nullptr; ++filename)
90   {
91     std::string filepath = FileSystem::join(profile_path.c_str(), *filename);
92     PHYSFS_delete(filepath.c_str());
93   }
94   PHYSFS_delete(profile_path.c_str());
95 }
96 
97 /* EOF */
98