gameexecutablepicker.cpp
1 //------------------------------------------------------------------------------
2 // gameexecutablepicker.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gameexecutablepicker.h"
24 
25 #include "ui_gameexecutablepicker.h"
26 #include "configuration/doomseekerconfig.h"
27 #include "gui/commongui.h"
28 #include "ini/ini.h"
29 #include "plugins/engineplugin.h"
30 #include "serverapi/exefile.h"
31 #include "serverapi/gameexefactory.h"
32 #include "serverapi/gamefile.h"
33 #include "filefilter.h"
34 #include <QFileDialog>
35 
36 DClass<GameExecutablePicker> : public Ui::GameExecutablePicker
37 {
38 public:
39  int allowedExecs;
40  EnginePlugin *plugin;
41 };
42 DPointered(GameExecutablePicker)
43 
45 : QWidget(parent)
46 {
47  d->setupUi(this);
48  d->allowedExecs = 0;
49  d->plugin = NULL;
50 
51  showWarning("");
52 }
53 
54 GameExecutablePicker::~GameExecutablePicker()
55 {
56 }
57 
58 void GameExecutablePicker::add(const QString &path)
59 {
60  if (!path.trimmed().isEmpty() && d->executableInput->findText(path) < 0)
61  {
62  d->executableInput->addItem(path);
63  }
64 }
65 
66 void GameExecutablePicker::browse()
67 {
68  showWarning("");
69  QString dialogDir = gConfig.doomseeker.previousCreateServerExecDir;
70  QString path = QFileDialog::getOpenFileName(this, tr("Doomseeker - Browse executable"),
71  dialogDir, FileFilter::executableFilesFilter());
72 
73  if (!path.isEmpty())
74  {
75  QFileInfo fi(path);
76  gConfig.doomseeker.previousCreateServerExecDir = fi.absolutePath();
77 
78  CommonGUI::setCurrentText(d->executableInput, fi.absoluteFilePath());
79  add(path);
80  }
81 }
82 
83 
84 GameFileList GameExecutablePicker::gameExecutables() const
85 {
86  GameFileList files = d->plugin->gameExe()->gameFiles();
87  GameFileList candidates = GameFiles::allFlagMatchExecutables(files, d->allowedExecs);
88  if (d->allowedExecs & GameFile::Client)
89  {
90  candidates.prepend(GameFiles::defaultClientExecutable(files));
91  }
92  else if (d->allowedExecs & GameFile::Server)
93  {
94  candidates.prepend(GameFiles::defaultServerExecutable(files));
95  }
96  else if (d->allowedExecs & GameFile::Offline)
97  {
98  candidates.prepend(GameFiles::defaultOfflineExecutable(files));
99  }
100  return candidates;
101 }
102 
103 QString GameExecutablePicker::path() const
104 {
105  return d->executableInput->currentText();
106 }
107 
108 void GameExecutablePicker::setPath(const QString &path)
109 {
110  CommonGUI::setCurrentText(d->executableInput, path);
111 }
112 
113 void GameExecutablePicker::setExecutableToDefault()
114 {
115  showWarning("");
116  IniSection *cfg = d->plugin->data()->pConfig;
117  if (cfg == NULL)
118  {
119  showWarning(tr("Plugin doesn't support configuration."));
120  return;
121  }
122 
123  GameFileList execs = gameExecutables();
124  if (execs.isEmpty())
125  {
126  showWarning(tr("Game doesn't define any executables for this game setup."));
127  return;
128  }
129 
130  foreach (const GameFile &candidate, execs.asQList())
131  {
132  QString path = cfg->value(candidate.configName()).toString();
133  if (!path.isEmpty())
134  {
135  CommonGUI::setCurrentText(d->executableInput, path);
136  return;
137  }
138  }
139  showWarning(tr("Default executable for this game isn't configured."));
140 }
141 
143 {
144  d->allowedExecs = execs;
145  reloadExecutables();
146 }
147 
148 void GameExecutablePicker::setPlugin(EnginePlugin *plugin)
149 {
150  d->plugin = plugin;
151  reloadExecutables();
152 }
153 
154 void GameExecutablePicker::showWarning(const QString &msg)
155 {
156  d->lblWarning->setVisible(!msg.trimmed().isEmpty());
157  d->lblWarning->setToolTip(msg);
158 }
159 
160 void GameExecutablePicker::reloadExecutables()
161 {
162  showWarning("");
163  if (d->plugin == NULL)
164  {
165  showWarning(tr("Game plugin not set."));
166  return;
167  }
168  QString currentExec = d->executableInput->currentText();
169  d->executableInput->clear();
170  IniSection *cfg = d->plugin->data()->pConfig;
171  if (cfg == NULL)
172  {
173  CommonGUI::setCurrentText(d->executableInput, currentExec);
174  return;
175  }
176 
177  GameFileList files = gameExecutables();
178  foreach (const GameFile &file, files.asQList())
179  {
180  add(cfg->value(file.configName()).toString());
181  }
182  foreach (const ExeFilePath &exe, d->plugin->gameExe()->additionalExecutables(d->allowedExecs))
183  {
184  QFileInfo fileInfo(exe.path());
185  if (fileInfo.isFile())
186  {
187  add(exe.path());
188  }
189  }
190 
191  if (d->executableInput->findText(currentExec) >= 0)
192  {
193  CommonGUI::setCurrentText(d->executableInput, currentExec);
194  }
195  else
196  {
197  setExecutableToDefault();
198  }
199 }
GameFile collection.
Definition: gamefile.h:145
const QString & configName() const
Setting name where path will be stored in plugin&#39;s IniSection.
Definition: gamefile.cpp:48
QVariant value(const QString &key) const
Retrieves a variable directly; omits the IniVariable system.
Definition: inisection.cpp:164
A simple executable path & working dir structure.
Definition: exefile.h:147
static void setCurrentText(QComboBox *box, const QString &text)
Qt4 compliant implementation of Qt5&#39;s QComboBox::setCurrentText().
Definition: commongui.cpp:128
INI section representation.
Definition: inisection.h:40
Game file definition allows to browse this file in configuration box.
Definition: gamefile.h:72
void setAllowedExecutables(int execs)