ManaPlus
quitdialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "gui/windows/quitdialog.h"
24 
25 #include "client.h"
26 #include "configuration.h"
27 #include "soundmanager.h"
28 
29 #include "const/sound.h"
30 
31 #include "gui/dialogsmanager.h"
32 #include "gui/viewport.h"
33 
35 #include "gui/widgets/button.h"
37 
38 #include "net/charserverhandler.h"
39 #include "net/gamehandler.h"
40 
41 #include "resources/map/map.h"
42 
43 #include "utils/delete2.h"
44 #include "utils/gettext.h"
45 #include "utils/process.h"
46 
47 #include "debug.h"
48 
49 QuitDialog::QuitDialog(QuitDialog **const pointerToMe) :
50  // TRANSLATORS: quit dialog name
51  Window(_("Quit"), Modal_true, nullptr, "quit.xml"),
53  KeyListener(),
54  mOptions(),
55  // TRANSLATORS: quit dialog button
56  mLogoutQuit(new RadioButton(this, _("Quit"), "quitdialog", false)),
57  // TRANSLATORS: quit dialog button
58  mForceQuit(new RadioButton(this, _("Quit"), "quitdialog", false)),
59  mSwitchAccountServer(new RadioButton(this,
60  // TRANSLATORS: quit dialog button
61  _("Switch server"), "quitdialog", false)),
62  mSwitchCharacter(new RadioButton(this,
63  // TRANSLATORS: quit dialog button
64  _("Switch character"), "quitdialog", false)),
65  mRate(nullptr),
66  // TRANSLATORS: quit dialog button
67  mOkButton(new Button(this, _("OK"), "ok", BUTTON_SKIN, this)),
68  // TRANSLATORS: quit dialog button
69  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
70  mMyPointer(pointerToMe),
71  mNeedForceQuit(false)
72 {
73  addKeyListener(this);
74 
75  ContainerPlacer placer = getPlacer(0, 0);
76  const StateT state = client->getState();
78  || state == State::CONNECT_SERVER || state == State::LOGIN
79  || state == State::PRE_LOGIN || state == State::LOGIN_ATTEMPT
80  || state == State::UPDATE || state == State::LOAD_DATA);
81 
82  // All states, when we're not logged in to someone.
83  if (mNeedForceQuit)
84  {
85  placeOption(placer, mForceQuit);
86  }
87  else
88  {
89  // Only added if we are connected to an accountserver or gameserver
90  placeOption(placer, mLogoutQuit);
92 
93  // Only added if we are connected to a gameserver
94  if (state == State::GAME)
96  }
97 
98 /*
99 #ifdef ANDROID
100  if (config.getBoolValue("rated") == false
101  && config.getIntValue("gamecount") > 3)
102  {
103  // TRANSLATORS: rate button
104  mRate = new RadioButton(this, _("Rate in google play"), "quitdialog",
105  false);
106  placeOption(placer, mRate);
107  mOptions[mOptions.size() - 1]->setSelected(true);
108  }
109  else
110 #endif
111 */
112  {
113  mOptions[0]->setSelected(true);
114  }
115 
116  placer = getPlacer(0, 1);
117  placer(1, 0, mOkButton, 1, 1);
118  placer(2, 0, mCancelButton, 1, 1);
119  reflowLayout(200, 0);
120 }
121 
123 {
130 }
131 
133 {
134  if (mMyPointer != nullptr)
135  *mMyPointer = nullptr;
140 }
141 
143  RadioButton *const option)
144 {
145  placer(0, CAST_S32(mOptions.size()), option, 3, 1);
146  mOptions.push_back(option);
147 }
148 
149 void QuitDialog::action(const ActionEvent &event)
150 {
152  if (event.getId() == "ok")
153  {
154  if (viewport != nullptr)
155  {
156  const Map *const map = viewport->getMap();
157  if (map != nullptr)
158  map->saveExtraLayer();
159  }
160 
161  if (mForceQuit->isSelected())
162  {
164  }
165  else if (mLogoutQuit->isSelected())
166  {
169  }
170  else if ((mRate != nullptr) && mRate->isSelected())
171  {
172  openBrowser("https://play.google.com/store/apps/details?"
173  "id=org.evolonline.beta.manaplus");
174  config.setValue("rated", true);
175  if (mNeedForceQuit)
176  {
178  }
179  else
180  {
183  }
184  }
185  else if (gameHandler->isConnected()
187  {
190  }
191  else if (mSwitchCharacter->isSelected())
192  {
193  if (client->getState() == State::GAME)
194  {
198  }
199  }
200  }
201  scheduleDelete();
202 }
203 
205 {
206  const InputActionT actionId = event.getActionId();
207  int dir = 0;
208 
209  PRAGMA45(GCC diagnostic push)
210  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
211  switch (actionId)
212  {
216  break;
219  break;
220  case InputAction::GUI_UP:
221  dir = -1;
222  break;
224  dir = 1;
225  break;
226  default:
227  break;
228  }
229  PRAGMA45(GCC diagnostic pop)
230 
231  if (dir != 0)
232  {
233  STD_VECTOR<RadioButton*>::const_iterator it = mOptions.begin();
234  const STD_VECTOR<RadioButton*>::const_iterator
235  it_end = mOptions.end();
236 
237  for (; it < it_end; ++it)
238  {
239  if ((*it)->isSelected())
240  break;
241  }
242 
243  if (it == mOptions.end())
244  {
245  if (mOptions[0] != nullptr)
246  mOptions[0]->setSelected(true);
247  return;
248  }
249  else if (it == mOptions.begin() && dir < 0)
250  {
251  it = mOptions.end();
252  }
253 
254  it += dir;
255 
256  if (it == mOptions.end())
257  it = mOptions.begin();
258 
259  (*it)->setSelected(true);
260  }
261 }
ItemOptionDb::OptionInfos mOptions
static const std::string SOUND_HIDE_WINDOW
Definition: sound.h:37
#define _(s)
Definition: gettext.h:34
RadioButton * mRate
Definition: quitdialog.h:76
RadioButton * mForceQuit
Definition: quitdialog.h:73
void requestModalFocus()
Definition: widget.cpp:379
void setState(const StateT state)
Definition: client.h:65
const bool Visible_true
Definition: visible.h:29
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wunused-result") int TestLauncher
void postInit()
Definition: quitdialog.cpp:122
Button * mCancelButton
Definition: quitdialog.h:78
virtual void switchCharacter() const =0
Definition: window.h:98
Definition: button.h:96
SoundManager soundManager
Configuration config
QuitDialog(QuitDialog **const pointerToMe)
Definition: quitdialog.cpp:49
void placeOption(ContainerPlacer &placer, RadioButton *const option)
Definition: quitdialog.cpp:142
State ::T StateT
Definition: state.h:70
const std::string & getActionEventId() const
Definition: widget.h:604
QuitDialog ** mMyPointer
Definition: quitdialog.h:80
Client * client
Definition: client.cpp:117
#define delete2(var)
Definition: delete2.h:24
Configuration serverConfig
#define new
Definition: debug_new.h:147
#define CAST_S32
Definition: cast.h:29
virtual void requestFocus()
Definition: widget.cpp:203
const std::string BUTTON_SKIN
Definition: button.h:88
const std::string & getId() const
Definition: actionevent.h:121
RadioButton * mLogoutQuit
Definition: quitdialog.h:72
InputAction ::T InputActionT
Definition: inputaction.h:715
#define nullptr
Definition: localconsts.h:44
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
static void closeDialogs()
bool isSelected() const
Definition: radiobutton.h:141
RadioButton * mSwitchAccountServer
Definition: quitdialog.h:74
void action(const ActionEvent &event)
Definition: quitdialog.cpp:149
bool openBrowser(std::string url)
Definition: process.cpp:309
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1386
Definition: map.h:71
bool mNeedForceQuit
Definition: quitdialog.h:81
virtual void scheduleDelete()
Definition: window.cpp:826
void saveExtraLayer() const
Definition: map.cpp:1220
RadioButton * mSwitchCharacter
Definition: quitdialog.h:75
Widget * getParent() const
Definition: widget.h:201
virtual void setVisible(Visible visible)
Definition: window.cpp:773
static const std::string SOUND_SHOW_WINDOW
Definition: sound.h:36
StateT getState() const
Definition: client.h:68
void keyPressed(KeyEvent &event)
Definition: quitdialog.cpp:204
virtual bool isConnected() const =0
Button * mOkButton
Definition: quitdialog.h:77
std::vector< RadioButton * > mOptions
Definition: quitdialog.h:70
Map * getMap() const
Definition: viewport.h:134
Net::GameHandler * gameHandler
Definition: net.cpp:87
Net::CharServerHandler * charServerHandler
Definition: net.cpp:81
void setValue(const std::string &key, const std::string &value)
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:508
void reflowLayout(int w, int h)
Definition: window.cpp:1391
void postInit()
Definition: window.cpp:248
Viewport * viewport
Definition: viewport.cpp:35
void playGuiSound(const std::string &name)