Make use of safe file writing in auth handler (fixes #6576)
[oweals/minetest.git] / src / mainmenumanager.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 /*
23         All kinds of stuff that needs to be exposed from main.cpp
24 */
25 #include "modalMenu.h"
26 #include <cassert>
27 #include <list>
28
29 class IGameCallback
30 {
31 public:
32         virtual void exitToOS() = 0;
33         virtual void keyConfig() = 0;
34         virtual void disconnect() = 0;
35         virtual void changePassword() = 0;
36         virtual void changeVolume() = 0;
37
38         virtual void signalKeyConfigChange() = 0;
39 };
40
41 extern gui::IGUIEnvironment *guienv;
42 extern gui::IGUIStaticText *guiroot;
43
44 // Handler for the modal menus
45
46 class MainMenuManager : public IMenuManager
47 {
48 public:
49         virtual void createdMenu(gui::IGUIElement *menu)
50         {
51 #ifndef NDEBUG
52                 for (gui::IGUIElement *i : m_stack) {
53                         assert(i != menu);
54                 }
55 #endif
56
57                 if(!m_stack.empty())
58                         m_stack.back()->setVisible(false);
59                 m_stack.push_back(menu);
60         }
61
62         virtual void deletingMenu(gui::IGUIElement *menu)
63         {
64                 // Remove all entries if there are duplicates
65                 bool removed_entry;
66                 do{
67                         removed_entry = false;
68                         for(std::list<gui::IGUIElement*>::iterator
69                                         i = m_stack.begin();
70                                         i != m_stack.end(); ++i)
71                         {
72                                 if(*i == menu)
73                                 {
74                                         m_stack.erase(i);
75                                         removed_entry = true;
76                                         break;
77                                 }
78                         }
79                 }while(removed_entry);
80
81                 /*core::list<GUIModalMenu*>::Iterator i = m_stack.getLast();
82                 assert(*i == menu);
83                 m_stack.erase(i);*/
84
85                 if(!m_stack.empty())
86                         m_stack.back()->setVisible(true);
87         }
88
89         // Returns true to prevent further processing
90         virtual bool preprocessEvent(const SEvent& event)
91         {
92                 if (m_stack.empty())
93                         return false;
94                 GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(m_stack.back());
95                 return mm && mm->preprocessEvent(event);
96         }
97
98         u32 menuCount()
99         {
100                 return m_stack.size();
101         }
102
103         bool pausesGame()
104         {
105                 for (gui::IGUIElement *i : m_stack) {
106                         GUIModalMenu *mm = dynamic_cast<GUIModalMenu*>(i);
107                         if (mm && mm->pausesGame())
108                                 return true;
109                 }
110                 return false;
111         }
112
113         std::list<gui::IGUIElement*> m_stack;
114 };
115
116 extern MainMenuManager g_menumgr;
117
118 extern bool isMenuActive();
119
120 class MainGameCallback : public IGameCallback
121 {
122 public:
123         MainGameCallback() = default;
124         virtual ~MainGameCallback() = default;
125
126         virtual void exitToOS()
127         {
128                 shutdown_requested = true;
129         }
130
131         virtual void disconnect()
132         {
133                 disconnect_requested = true;
134         }
135
136         virtual void changePassword()
137         {
138                 changepassword_requested = true;
139         }
140
141         virtual void changeVolume()
142         {
143                 changevolume_requested = true;
144         }
145
146         virtual void keyConfig()
147         {
148                 keyconfig_requested = true;
149         }
150
151         virtual void signalKeyConfigChange()
152         {
153                 keyconfig_changed = true;
154         }
155
156
157         bool disconnect_requested = false;
158         bool changepassword_requested = false;
159         bool changevolume_requested = false;
160         bool keyconfig_requested = false;
161         bool shutdown_requested = false;
162
163         bool keyconfig_changed = false;
164 };
165
166 extern MainGameCallback *g_gamecallback;