doomseekerconfigurationdialog.cpp
1 //------------------------------------------------------------------------------
2 // doomseekerconfigurationdialog.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 "doomseekerconfigurationdialog.h"
24 #include "configuration/doomseekerconfig.h"
25 #include "gui/configuration/cfgappearance.h"
26 #include "gui/configuration/cfgautoupdates.h"
27 #include "gui/configuration/cfgcustomservers.h"
28 #include "gui/configuration/cfgfilepaths.h"
29 #include "gui/configuration/cfgip2country.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/engineconfigurationbasebox.h"
38 #include "gui/mainwindow.h"
39 #include "plugins/engineplugin.h"
40 #include "plugins/pluginloader.h"
41 #include "updater/updatechannel.h"
42 #include "application.h"
43 #include "log.h"
44 #include "qtmetapointer.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 }
54 
55 QStandardItem* DoomseekerConfigurationDialog::addConfigurationBox(QStandardItem* rootItem, ConfigurationBaseBox* pConfigurationBox, int position)
56 {
57  QStandardItem* pItem = ConfigurationDialog::addConfigurationBox(rootItem, pConfigurationBox, position);
58 
59  if (pItem != NULL)
60  {
61  connect(pConfigurationBox, SIGNAL( appearanceChanged() ),
62  SLOT( appearanceChangedSlot() ) );
63  }
64 
65  return pItem;
66 }
67 
68 bool DoomseekerConfigurationDialog::addEngineConfiguration(ConfigurationBaseBox* pConfigurationBox)
69 {
70  if (enginesRoot != NULL)
71  {
72  return addConfigurationBox(enginesRoot, pConfigurationBox);
73  }
74  return false;
75 }
76 
77 void DoomseekerConfigurationDialog::appearanceChangedSlot()
78 {
79  this->bAppearanceChanged = true;
80 }
81 
82 void DoomseekerConfigurationDialog::appendFilePathsConfigurationBoxes()
83 {
84  QStandardItem *itemFilePaths = addConfigurationBox(NULL, new CFGFilePaths(this));
85  addConfigurationBox(itemFilePaths, new CFGWadAlias(this));
86 }
87 
88 void DoomseekerConfigurationDialog::appendWadseekerConfigurationBoxes()
89 {
90  QStandardItem* wadseekerRoot = addLabel(NULL, tr("Wadseeker"));
91  wadseekerRoot->setIcon(QIcon(":/icons/get-wad.png"));
92 
93  ConfigurationBaseBox* pConfigBox = NULL;
94 
95  pConfigBox = new CFGWadseekerAppearance(this);
96  addConfigurationBox(wadseekerRoot, pConfigBox);
97 
98  pConfigBox = new CFGWadseekerGeneral(this);
99  addConfigurationBox(wadseekerRoot, pConfigBox);
100 
101  pConfigBox = new CFGWadseekerSites(this);
102  addConfigurationBox(wadseekerRoot, pConfigBox);
103 
104  pConfigBox = new CFGWadseekerIdgames(this);
105  addConfigurationBox(wadseekerRoot, pConfigBox);
106 }
107 
108 void DoomseekerConfigurationDialog::doSaveSettings()
109 {
110  bCustomServersChanged = customServersCfgBox->allowSave();
111  if (gConfig.saveToFile())
112  {
113  gLog << tr("Settings saved!");
114  }
115  else
116  {
117  gLog << tr("Settings save failed!");
118  }
119 }
120 
121 void DoomseekerConfigurationDialog::initOptionsList()
122 {
123  enginesRoot = addLabel(NULL, tr("Engines"));
124  enginesRoot->setIcon(QIcon(":/icons/joystick.png"));
125 
126  ConfigurationBaseBox* pConfigBox = NULL;
127 
128  pConfigBox = new CFGAppearance(this);
129  addConfigurationBox(NULL, pConfigBox);
130 
131  // Add only if supported on target platform.
132  #ifdef WITH_AUTOUPDATES
133  pConfigBox = new CFGAutoUpdates(this);
134  addConfigurationBox(NULL, pConfigBox);
135  #endif
136 
137  pConfigBox = new CFGCustomServers(this);
138  addConfigurationBox(NULL, pConfigBox);
139  customServersCfgBox = pConfigBox;
140 
141  pConfigBox = new CFGServerPasswords(this);
142  addConfigurationBox(NULL, pConfigBox);
143 
144  pConfigBox = new CFGIP2Country(this);
145  addConfigurationBox(NULL, pConfigBox);
146 
147  pConfigBox = new CFGQuery(this);
148  addConfigurationBox(NULL, pConfigBox);
149 
150  appendFilePathsConfigurationBoxes();
151  appendWadseekerConfigurationBoxes();
152 
153  optionsTree()->expandAll();
154 }
155 
156 void DoomseekerConfigurationDialog::openConfiguration(const EnginePlugin *openPlugin)
157 {
158  MainWindow *mw = gApp->mainWindow();
159 
160  DoomseekerConfigurationDialog configDialog(mw);
161  configDialog.initOptionsList();
162 
163  for(unsigned i = 0; i < gPlugins->numPlugins(); ++i)
164  {
165  const EnginePlugin* pPluginInfo = gPlugins->info(i);
166 
167  // Create the config box.
168  ConfigurationBaseBox* pConfigurationBox = pPluginInfo->configuration(&configDialog);
169  configDialog.addEngineConfiguration(pConfigurationBox);
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  mw->stopAutoRefreshTimer();
177 
178  if(openPlugin)
179  configDialog.showPluginConfiguration(openPlugin);
180 
181  configDialog.exec();
182 
183  // Do some cleanups after config box finishes.
184  mw->initAutoRefreshTimer();
185 
186  // If update channel or update mode changed then we should discard the
187  // current updates.
188  UpdateChannel updateChannelAfter = UpdateChannel::fromName(gConfig.autoUpdates.updateChannelName);
189  if (updateChannelBefore != updateChannelAfter
190  || updateModeBefore != gConfig.autoUpdates.updateMode)
191  {
192  mw->abortAutoUpdater();
193  gConfig.autoUpdates.bPerformUpdateOnNextRun = false;
194  gConfig.saveToFile();
195  }
196 
197  mw->finishConfiguration(configDialog, !bLookupHostsSettingBefore && gConfig.doomseeker.bLookupHosts);
198 }
199 
200 void DoomseekerConfigurationDialog::showPluginConfiguration(const EnginePlugin *plugin)
201 {
202  // Find plugin page and make it the active page
203  for(int i = 0;i < enginesRoot->rowCount();++i)
204  {
205  QStandardItem *page = enginesRoot->child(i);
206  QtMetaPointer metaPointer = qVariantValue<QtMetaPointer>(page->data());
207  void* pointer = metaPointer;
208  EngineConfigurationBaseBox *engineConfig = (EngineConfigurationBaseBox*)pointer;
209 
210  if(engineConfig->plugin() == plugin)
211  {
212  switchToItem(page->index());
213  }
214  }
215 }
QTreeView * optionsTree()
Returns pointer to the tree widget that contains configuration sections list.
static UpdateChannel fromName(const QString &name)
Creates object from its internal name.
const EnginePlugin * plugin() const
Parent plugin handled by this page.
void finishConfiguration(DoomseekerConfigurationDialog &, bool)
Definition: mainwindow.cpp:628
QStandardItem * addConfigurationBox(QStandardItem *rootItem, ConfigurationBaseBox *pConfigurationBox, int position=-1)
Adds a new configuration box to the options tree view.
virtual ConfigurationBaseBox * configuration(QWidget *parent) const
Engine's configuration widget.
QStandardItem * addLabel(QStandardItem *rootItem, const QString &label, int position=-1)
Adds a label node to the options tree view.
Base for configuration pages for plugins; provides some default behavior.
bool allowSave()
Does this page allow to save the new settings?
virtual QStandardItem * addConfigurationBox(QStandardItem *rootItem, ConfigurationBaseBox *pConfigurationBox, int position=-1)
Adds a new configuration box to the options tree view.
Base class for configuration pages.
void initAutoRefreshTimer()
Definition: mainwindow.cpp:734