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