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