generalgamesetuppanel.cpp
1 //------------------------------------------------------------------------------
2 // generalgamesetuppanel.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (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 General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "generalgamesetuppanel.h"
24 #include "ui_generalgamesetuppanel.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "gui/createserver/maplistpanel.h"
28 #include "gui/createserverdialog.h"
29 #include "ini/ini.h"
30 #include "plugins/engineplugin.h"
31 #include "serverapi/gamecreateparams.h"
32 #include "serverapi/gameexefactory.h"
33 #include "serverapi/gamefile.h"
34 #include "filefilter.h"
35 #include <QFileDialog>
36 #include <QFileInfo>
37 #include <QMessageBox>
38 #include <QTimer>
39 #include <cassert>
40 
41 DClass<GeneralGameSetupPanel> : public Ui::GeneralGameSetupPanel
42 {
43 public:
44  EnginePlugin *currentEngine;
45  bool iwadSetExplicitly;
46  CreateServerDialog *parentDialog;
47  bool remoteGameSetup;
48 };
49 
50 DPointered(GeneralGameSetupPanel)
51 
52 
54 : QWidget(parent)
55 {
56  d->setupUi(this);
57  d->iwadSetExplicitly = false;
58  d->remoteGameSetup = false;
59  d->parentDialog = NULL;
60 
61  d->executableInput->setAllowedExecutables(GameFile::CreateGame);
62 
63  this->connect(d->cboEngine, SIGNAL(currentPluginChanged(EnginePlugin*)),
64  SIGNAL(pluginChanged(EnginePlugin*)));
65 }
66 
67 GeneralGameSetupPanel::~GeneralGameSetupPanel()
68 {
69 }
70 
71 void GeneralGameSetupPanel::fillInParams(GameCreateParams &params)
72 {
73  params.setExecutablePath(pathToExe());
74  params.setIwadPath(d->iwadPicker->currentIwad());
75  params.setPwadsPaths(d->wadsPicker->filePaths());
76  params.setPwadsOptional(d->wadsPicker->fileOptional());
77  params.setBroadcastToLan(d->cbBroadcastToLAN->isChecked());
78  params.setBroadcastToMaster(d->cbBroadcastToMaster->isChecked());
79  params.setMap(d->leMap->text());
80  params.setName(d->leServername->text());
81  params.setPort(d->spinPort->isEnabled() ? d->spinPort->value() : 0);
82  params.setGameMode(currentGameMode());
83  params.setUpnp(d->cbUpnp->isChecked());
84  params.setUpnpPort(d->spinUpnpPort->value());
85 }
86 
87 void GeneralGameSetupPanel::loadConfig(Ini &config, bool loadingPrevious)
88 {
89  IniSection general = config.section("General");
90 
91  // General
92  if (!d->remoteGameSetup)
93  {
94  QString currentExecutable = d->executableInput->path();
95  QString engineName = general["engine"];
96  const EnginePlugin* prevEngine = d->currentEngine;
97  if(!setEngine(engineName))
98  return;
99 
100  bool bChangeExecutable = (prevEngine != d->currentEngine || !d->cbLockExecutable->isChecked());
101  QString executablePath = *general["executable"];
102  QFileInfo fileInfo(executablePath);
103  if (!executablePath.isEmpty() && fileInfo.isFile() && bChangeExecutable)
104  {
105  d->executableInput->setPath(executablePath);
106  }
107  else if (!bChangeExecutable)
108  {
109  d->executableInput->setPath(currentExecutable);
110  }
111  }
112 
113  d->leServername->setText(general["name"]);
114  d->spinPort->setValue(general["port"]);
115  d->cboGamemode->setCurrentIndex(general["gamemode"]);
116  d->leMap->setText(general["map"]);
117 
118  if (!(loadingPrevious && d->iwadSetExplicitly))
119  {
120  d->iwadPicker->addIwad(general["iwad"]);
121  }
122 
123  QList<bool> optionalWads;
124  foreach(QString value, general["pwadsOptional"].valueString().split(";"))
125  {
126  optionalWads << (value != "0");
127  }
128  d->wadsPicker->setFilePaths(general["pwads"].valueString().split(";"), optionalWads);
129 
130  d->cbBroadcastToLAN->setChecked(general["broadcastToLAN"]);
131  d->cbBroadcastToMaster->setChecked(general["broadcastToMaster"]);
132  d->cbUpnp->setChecked(general["upnp"]);
133  d->spinUpnpPort->setValue(general["upnpPort"]);
134 
135  // Timer triggers slot after config is fully loaded.
136  QTimer::singleShot(0, this, SLOT(updateMapWarningVisibility()));
137 }
138 
139 void GeneralGameSetupPanel::saveConfig(Ini &config)
140 {
141  IniSection general = config.section("General");
142  general["engine"] = d->cboEngine->currentText();
143  general["executable"] = pathToExe();
144  general["name"] = d->leServername->text();
145  general["port"] = d->spinPort->value();
146  general["gamemode"] = d->cboGamemode->currentIndex();
147  general["map"] = d->leMap->text();
148  general["iwad"] = d->iwadPicker->currentIwad();
149 
150  general["pwads"] = d->wadsPicker->filePaths().join(";");
151  QList<bool> optionalWads = d->wadsPicker->fileOptional();
152  QStringList optionalList;
153  foreach(bool optional, optionalWads)
154  optionalList << (optional ? "1" : "0");
155  general["pwadsOptional"] = optionalList.join(";");
156 
157  general["broadcastToLAN"] = d->cbBroadcastToLAN->isChecked();
158  general["broadcastToMaster"] = d->cbBroadcastToMaster->isChecked();
159  general["upnp"] = d->cbUpnp->isChecked();
160  general["upnpPort"] = d->spinUpnpPort->value();
161 }
162 
163 void GeneralGameSetupPanel::setupForEngine(EnginePlugin *engine)
164 {
165  d->currentEngine = engine;
166 
167  d->labelIwad->setVisible(engine->data()->hasIwad);
168  d->iwadPicker->setVisible(engine->data()->hasIwad);
169  d->upnpArea->setVisible(engine->data()->allowsUpnp);
170  d->spinUpnpPort->setVisible(engine->data()->allowsUpnpPort);
171 
172  d->executableInput->setPlugin(engine);
173 
174  d->spinPort->setValue(d->currentEngine->data()->defaultServerPort);
175 
176  d->cboGamemode->clear();
177  QList<GameMode> gameModes = d->currentEngine->gameModes();
178  if (!gameModes.isEmpty())
179  {
180  for (int i = 0; i < gameModes.count(); ++i)
181  {
182  d->cboGamemode->addItem(gameModes[i].name(), i);
183  }
184  }
185 }
186 
187 void GeneralGameSetupPanel::setupForRemoteGame()
188 {
189  d->remoteGameSetup = true;
190  d->cbAllowTheGameToChoosePort->hide();
191  QWidget *disableControls[] =
192  {
193  d->cboEngine, d->leServername, d->spinPort,
194  d->cbBroadcastToLAN, d->cbBroadcastToMaster,
195  d->upnpArea,
196 
197  NULL
198  };
199  for(int i = 0;disableControls[i] != NULL;++i)
200  disableControls[i]->setDisabled(true);
201 }
202 
203 void GeneralGameSetupPanel::setCreateServerDialog(CreateServerDialog *dialog)
204 {
205  d->parentDialog = dialog;
206 }
207 
208 void GeneralGameSetupPanel::setIwadByName(const QString &iwad)
209 {
210  d->iwadSetExplicitly = true;
211  d->iwadPicker->setIwadByName(iwad);
212 }
213 
214 void GeneralGameSetupPanel::showEvent(QShowEvent *event)
215 {
216  updateMapWarningVisibility();
217 }
218 
219 QString GeneralGameSetupPanel::mapName() const
220 {
221  return d->leMap->text();
222 }
223 
224 QString GeneralGameSetupPanel::pathToExe()
225 {
226  return d->executableInput->path();
227 }
228 
229 void GeneralGameSetupPanel::onGameModeChanged(int index)
230 {
231  if (index >= 0)
232  {
233  QList<GameMode> gameModes = d->currentEngine->gameModes();
234  emit gameModeChanged(gameModes[index]);
235  }
236 }
237 
238 GameMode GeneralGameSetupPanel::currentGameMode() const
239 {
240  QList<GameMode> gameModes = d->currentEngine->gameModes();
241  foreach (const GameMode& mode, gameModes)
242  {
243  if (mode.name().compare(d->cboGamemode->currentText()) == 0)
244  {
245  return mode;
246  }
247  }
248  return GameMode();
249 }
250 
251 EnginePlugin *GeneralGameSetupPanel::currentPlugin() const
252 {
253  return d->cboEngine->currentPlugin();
254 }
255 
256 bool GeneralGameSetupPanel::setEngine(const QString &engineName)
257 {
258  if (!d->cboEngine->setPluginByName(engineName))
259  {
260  QMessageBox::critical(this, tr("Doomseeker - load server config"),
261  tr("Plugin for engine \"%1\" is not present!").arg(engineName));
262  return false;
263  }
264  return true;
265 }
266 
267 void GeneralGameSetupPanel::updateMapWarningVisibility()
268 {
269  assert(d->parentDialog != NULL);
270  MapListPanel *mapList = d->parentDialog->mapListPanel();
271  d->lblMapWarning->setVisible(mapList->hasMaps() && !mapList->isMapOnList(mapName()));
272 }
const QString & name() const
User-friendly name to display for game mode.
Game parametrization data used when creating new games.
Dialog window allowing user to host a game.
Game mode representation.
Configuration handler.
Definition: ini.h:69
INI section representation.
Definition: inisection.h:40
IniSection section(const QString &name)
Access configuration file section.
Definition: ini.cpp:91