doomseekerconfigurationdialog.cpp
1 //------------------------------------------------------------------------------
2 // doomseekerconfigurationdialog.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) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "application.h"
24 #include "configuration/doomseekerconfig.h"
25 #include "doomseekerconfigurationdialog.h"
26 #include "gui/configuration/cfgappearance.h"
27 #include "gui/configuration/cfgautoupdates.h"
28 #include "gui/configuration/cfgcustomservers.h"
29 #include "gui/configuration/cfgfilepaths.h"
30 #include "gui/configuration/cfgip2c.h"
31 #include "gui/configuration/cfgquery.h"
32 #include "gui/configuration/cfgserverpasswords.h"
33 #include "gui/configuration/cfgwadalias.h"
34 #include "gui/configuration/cfgwadseekerappearance.h"
35 #include "gui/configuration/cfgwadseekergeneral.h"
36 #include "gui/configuration/cfgwadseekeridgames.h"
37 #include "gui/configuration/cfgwadseekersites.h"
38 #include "gui/configuration/engineconfigpage.h"
39 #include "gui/mainwindow.h"
40 #include "log.h"
41 #include "plugins/engineplugin.h"
42 #include "plugins/pluginloader.h"
43 #include "qtmetapointer.h"
44 #include "updater/updatechannel.h"
45 #include <QStandardItem>
46 #include <QTreeView>
47 
48 DoomseekerConfigurationDialog::DoomseekerConfigurationDialog(QWidget *parent)
49  : ConfigurationDialog(parent)
50 {
51  this->bAppearanceChanged = false;
52  this->bCustomServersChanged = false;
53  this->bRestartNeeded = false;
54 }
55 
56 QStandardItem *DoomseekerConfigurationDialog::addConfigPage(QStandardItem *rootItem, ConfigPage *configPage, int position)
57 {
58  QStandardItem *pItem = ConfigurationDialog::addConfigPage(rootItem, configPage, position);
59 
60  if (pItem != nullptr)
61  {
62  connect(configPage, SIGNAL(appearanceChanged()),
63  SLOT(appearanceChangedSlot()));
64  connect(configPage, SIGNAL(restartNeeded()),
65  SLOT(restartNeededSlot()));
66  }
67 
68  return pItem;
69 }
70 
71 bool DoomseekerConfigurationDialog::addEngineConfiguration(ConfigPage *configPage)
72 {
73  if (enginesRoot != nullptr)
74  return addConfigPage(enginesRoot, configPage);
75  return false;
76 }
77 
78 void DoomseekerConfigurationDialog::appearanceChangedSlot()
79 {
80  this->bAppearanceChanged = true;
81  emit appearanceChanged();
82 }
83 
84 void DoomseekerConfigurationDialog::restartNeededSlot()
85 {
86  this->bRestartNeeded = true;
87 }
88 
89 void DoomseekerConfigurationDialog::appendFilePathsConfigurationBoxes()
90 {
91  QStandardItem *itemFilePaths = addConfigPage(nullptr, new CFGFilePaths(this));
92  addConfigPage(itemFilePaths, new CFGWadAlias(this));
93 }
94 
95 void DoomseekerConfigurationDialog::appendWadseekerConfigurationBoxes()
96 {
97  QStandardItem *wadseekerRoot = addConfigPage(nullptr, new CFGWadseekerGeneral(this));
98 
99  addConfigPage(wadseekerRoot, new CFGWadseekerAppearance(this));
100  addConfigPage(wadseekerRoot, new CFGWadseekerSites(this));
101  addConfigPage(wadseekerRoot, new CFGWadseekerIdgames(this));
102 }
103 
104 void DoomseekerConfigurationDialog::doSaveSettings()
105 {
106  bCustomServersChanged = customServersCfgBox->allowSave();
107  if (gConfig.saveToFile())
108  gLog << tr("Settings saved!");
109  else
110  gLog << tr("Settings save failed!");
111 }
112 
113 void DoomseekerConfigurationDialog::initOptionsList()
114 {
115  enginesRoot = addLabel(nullptr, tr("Games"));
116  enginesRoot->setIcon(QIcon(":/icons/joystick.png"));
117 
118  addConfigPage(nullptr, new CFGAppearance(this));
119  #ifdef WITH_AUTOUPDATES
120  addConfigPage(nullptr, new CFGAutoUpdates(this));
121  #endif
122 
123  customServersCfgBox = new CFGCustomServers(this);
124  addConfigPage(nullptr, customServersCfgBox);
125 
126  addConfigPage(nullptr, new CFGServerPasswords(this));
127  addConfigPage(nullptr, new CFGQuery(this));
128  addConfigPage(nullptr, new CFGIP2C(this));
129 
130  appendFilePathsConfigurationBoxes();
131  appendWadseekerConfigurationBoxes();
132 
133  optionsTree()->expandAll();
134 }
135 
136 bool DoomseekerConfigurationDialog::isOpen()
137 {
138  if (gApp->mainWindow() == nullptr)
139  return false;
140  for (QObject *obj : gApp->mainWindow()->children())
141  {
142  if (qobject_cast<DoomseekerConfigurationDialog *>(obj) != nullptr)
143  return true;
144  }
145  return false;
146 }
147 
148 void DoomseekerConfigurationDialog::openConfiguration(QWidget *parent, const EnginePlugin *openPlugin)
149 {
150  DoomseekerConfigurationDialog configDialog(parent);
151 
152  MainWindow *mainWindow = gApp->mainWindow();
153  if (mainWindow != nullptr)
154  mainWindow->connect(&configDialog, SIGNAL(appearanceChanged()), SLOT(updateDynamicAppearance()));
155 
156  configDialog.initOptionsList();
157 
158  for (unsigned i = 0; i < gPlugins->numPlugins(); ++i)
159  {
160  EnginePlugin *pPluginInfo = gPlugins->info(i);
161 
162  // Create the config box.
163  ConfigPage *configPage = pPluginInfo->configuration(&configDialog);
164  configDialog.addEngineConfiguration(configPage);
165  }
166 
167  bool bLookupHostsSettingBefore = gConfig.doomseeker.bLookupHosts;
168  DoomseekerConfig::AutoUpdates::UpdateMode updateModeBefore = gConfig.autoUpdates.updateMode;
169  UpdateChannel updateChannelBefore = UpdateChannel::fromName(gConfig.autoUpdates.updateChannelName);
170  // Stop the auto refresh timer during configuration.
171  if (mainWindow != nullptr)
172  mainWindow->stopAutoRefreshTimer();
173 
174  if (openPlugin)
175  configDialog.showPluginConfiguration(openPlugin);
176 
177  configDialog.exec();
178 
179  // Do some cleanups after config box finishes.
180  if (mainWindow != nullptr)
181  mainWindow->initAutoRefreshTimer();
182 
183  // If update channel or update mode changed then we should discard the
184  // current updates.
185  UpdateChannel updateChannelAfter = UpdateChannel::fromName(gConfig.autoUpdates.updateChannelName);
186  if (updateChannelBefore != updateChannelAfter
187  || updateModeBefore != gConfig.autoUpdates.updateMode)
188  {
189  if (mainWindow != nullptr)
190  mainWindow->abortAutoUpdater();
191  gConfig.autoUpdates.bPerformUpdateOnNextRun = false;
192  gConfig.saveToFile();
193  }
194 
195  if (mainWindow != nullptr)
196  mainWindow->finishConfiguration(configDialog, !bLookupHostsSettingBefore && gConfig.doomseeker.bLookupHosts);
197 }
198 
199 void DoomseekerConfigurationDialog::showPluginConfiguration(const EnginePlugin *plugin)
200 {
201  // Find plugin page and make it the active page
202  for (int i = 0; i < enginesRoot->rowCount(); ++i)
203  {
204  QStandardItem *page = enginesRoot->child(i);
205  QtMetaPointer metaPointer = page->data(Qt::UserRole).value<QtMetaPointer>();
206  void *pointer = metaPointer;
207  auto engineConfig = (EngineConfigPage *)pointer;
208 
209  if (engineConfig->plugin() == plugin)
210  showConfigPage(engineConfig);
211  }
212 }