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