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 "configuration/doomseekerconfig.h"
26 #include "filefilter.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 "ui_gameexecutablepicker.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 = nullptr;
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  d->executableInput->addItem(path);
62 }
63 
64 void GameExecutablePicker::browse()
65 {
66  showWarning("");
67  QString dialogDir = gConfig.doomseeker.previousCreateServerExecDir;
68  QString path = QFileDialog::getOpenFileName(this, tr("Doomseeker - Browse executable"),
69  dialogDir, FileFilter::executableFilesFilter());
70 
71  if (!path.isEmpty())
72  {
73  QFileInfo fi(path);
74  gConfig.doomseeker.previousCreateServerExecDir = fi.absolutePath();
75 
76  d->executableInput->setCurrentText(fi.absoluteFilePath());
77  add(path);
78  }
79 }
80 
81 
82 GameFileList GameExecutablePicker::gameExecutables() const
83 {
84  GameFileList files = d->plugin->gameExe()->gameFiles();
85  GameFileList candidates = GameFiles::allFlagMatchExecutables(files, d->allowedExecs);
86  if (d->allowedExecs & GameFile::Client)
87  candidates.prepend(GameFiles::defaultClientExecutable(files));
88  else if (d->allowedExecs & GameFile::Server)
89  candidates.prepend(GameFiles::defaultServerExecutable(files));
90  else if (d->allowedExecs & GameFile::Offline)
91  candidates.prepend(GameFiles::defaultOfflineExecutable(files));
92  return candidates;
93 }
94 
95 QString GameExecutablePicker::path() const
96 {
97  return d->executableInput->currentText();
98 }
99 
100 void GameExecutablePicker::setPath(const QString &path)
101 {
102  d->executableInput->setCurrentText(path);
103 }
104 
105 void GameExecutablePicker::setExecutableToDefault()
106 {
107  showWarning("");
108  IniSection *cfg = d->plugin->data()->pConfig;
109  if (cfg == nullptr)
110  {
111  showWarning(tr("Plugin doesn't support configuration."));
112  return;
113  }
114 
115  GameFileList execs = gameExecutables();
116  if (execs.isEmpty())
117  {
118  showWarning(tr("Game doesn't define any executables for this game setup."));
119  return;
120  }
121 
122  for (const GameFile &candidate : execs.asQList())
123  {
124  QString path = cfg->value(candidate.configName()).toString();
125  if (!path.isEmpty())
126  {
127  d->executableInput->setCurrentText(path);
128  return;
129  }
130  }
131  showWarning(tr("Default executable for this game isn't configured."));
132 }
133 
135 {
136  d->allowedExecs = execs;
137  reloadExecutables();
138 }
139 
140 void GameExecutablePicker::setPlugin(EnginePlugin *plugin)
141 {
142  d->plugin = plugin;
143  reloadExecutables();
144 }
145 
146 void GameExecutablePicker::showWarning(const QString &msg)
147 {
148  d->lblWarning->setVisible(!msg.trimmed().isEmpty());
149  d->lblWarning->setToolTip(msg);
150 }
151 
152 void GameExecutablePicker::reloadExecutables()
153 {
154  showWarning("");
155  if (d->plugin == nullptr)
156  {
157  showWarning(tr("Game plugin not set."));
158  return;
159  }
160  QString currentExec = d->executableInput->currentText();
161  d->executableInput->clear();
162  IniSection *cfg = d->plugin->data()->pConfig;
163  if (cfg == nullptr)
164  {
165  d->executableInput->setCurrentText(currentExec);
166  return;
167  }
168 
169  GameFileList files = gameExecutables();
170  for (const GameFile &file : files.asQList())
171  {
172  add(cfg->value(file.configName()).toString());
173  }
174  for (const ExeFilePath &exe : d->plugin->gameExe()->additionalExecutables(d->allowedExecs))
175  {
176  QFileInfo fileInfo(exe.path());
177  if (fileInfo.isFile())
178  add(exe.path());
179  }
180 
181  if (d->executableInput->findText(currentExec) >= 0)
182  d->executableInput->setCurrentText(currentExec);
183  else
184  setExecutableToDefault();
185 }