missingwadsdialog.cpp
1 //------------------------------------------------------------------------------
2 // missingwadsdialog.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) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "missingwadsdialog.h"
24 
25 #include "ui_missingwadsdialog.h"
26 #include "gui/mainwindow.h"
27 #include "gui/wadseekerinterface.h"
29 #include "application.h"
30 #include <wadseeker/freedoom.h>
31 #include <wadseeker/wadseeker.h>
32 #include <QListWidgetItem>
33 
34 DClass<MissingWadsDialog> : public Ui::MissingWadsDialog
35 {
36 public:
37  ::MissingWadsDialog::MissingWadsProceed decision;
38  QList<PWad> wads;
39 };
40 DPointeredNoCopy(MissingWadsDialog)
41 
42 MissingWadsDialog::MissingWadsDialog(const QList<PWad> &wads, QWidget *parent)
43 : QDialog(parent)
44 {
45  d->setupUi(this);
46  d->decision = Cancel;
47  d->wads = wads;
48 
49  setup();
50  adjustSize();
51 }
52 
53 MissingWadsDialog::~MissingWadsDialog()
54 {
55 }
56 
57 void MissingWadsDialog::setAllowIgnore(bool allow)
58 {
59  d->btnIgnore->setVisible(allow);
60  d->lblUseIgnore->setVisible(allow);
61  d->lblUseIgnoreCantRun->setVisible(allow);
62 }
63 
64 void MissingWadsDialog::setup()
65 {
66  d->btnInstallFreedoom->hide();
67  if (WadseekerInterface::isInstantiated())
68  {
69  setupWadseekerIsRunning();
70  }
71  else
72  {
73  setupWadseekerNotRunning();
74  }
75 }
76 
77 void MissingWadsDialog::setupWadseekerIsRunning()
78 {
79  d->areaWadseekerAlreadyRunning->show();
80  d->areaWadseekerCanBeRun->hide();
81  d->btnInstall->hide();
82 }
83 
84 void MissingWadsDialog::setupWadseekerNotRunning()
85 {
86  d->areaWadseekerAlreadyRunning->hide();
87  d->areaWadseekerCanBeRun->show();
88 
89  setupForbiddenFilesArea();
90  setupDownloadableFilesArea();
91  setupOptionalFilesArea();
92 
93  d->btnInstall->setVisible(hasAnyAllowedFile());
94 }
95 
96 void MissingWadsDialog::setupForbiddenFilesArea()
97 {
98  QStringList files = forbiddenFiles();
99  if (!files.isEmpty())
100  {
101  d->areaCantBeDownloaded->show();
102  d->lblCantBeDownloadedFiles->setText(files.join(", "));
103 
104  bool installFreedoom = isFreedoomReplaceableOnList(files);
105  d->lblInstallFreedoom->setVisible(installFreedoom);
106  d->btnInstallFreedoom->setVisible(installFreedoom);
107  }
108  else
109  {
110  d->areaCantBeDownloaded->hide();
111  }
112 }
113 
114 void MissingWadsDialog::setupDownloadableFilesArea()
115 {
116  QStringList files = downloadableFiles();
117  if (!files.isEmpty())
118  {
119  d->areaCanBeDownloadedFiles->show();
120  d->lblCanBeDownloadedFiles->setText(files.join(", "));
121  }
122  else
123  {
124  d->areaCanBeDownloadedFiles->hide();
125  }
126 }
127 
128 void MissingWadsDialog::setupOptionalFilesArea()
129 {
130  QStringList files = optionalFiles();
131  if (!files.isEmpty())
132  {
133  d->areaOptionalFiles->show();
134  foreach (const QString &file, files)
135  {
136  QListWidgetItem *item = new QListWidgetItem(file, d->optionalFilesList);
137  item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
138  item->setCheckState(Qt::Checked);
139  }
140  }
141  else
142  {
143  d->areaOptionalFiles->hide();
144  }
145 }
146 
147 bool MissingWadsDialog::isFreedoomReplaceableOnList(const QStringList &files) const
148 {
149  foreach (const QString &file, files)
150  {
151  if (Freedoom::hasFreedoomReplacement(file))
152  {
153  return true;
154  }
155  }
156  return false;
157 }
158 
159 void MissingWadsDialog::updateStateAccordingToFileSelection()
160 {
161  d->btnInstall->setEnabled(!filesToDownload().isEmpty());
162 }
163 
164 void MissingWadsDialog::ignoreMissingFiles()
165 {
166  d->decision = Ignore;
167  accept();
168 }
169 
170 void MissingWadsDialog::installFreedoom()
171 {
172  QTimer::singleShot(0, gApp->mainWindow(), SLOT(showInstallFreedoomDialog()));
173  accept();
174 }
175 
176 void MissingWadsDialog::installMissingFiles()
177 {
178  d->decision = Install;
179  accept();
180 }
181 
182 QStringList MissingWadsDialog::downloadableFiles() const
183 {
184  QStringList result;
185  foreach (const PWad &file, d->wads)
186  {
187  if (!Wadseeker::isForbiddenWad(file.name()) && !file.isOptional())
188  {
189  result << file.name();
190  }
191  }
192  return result;
193 }
194 
195 QStringList MissingWadsDialog::forbiddenFiles() const
196 {
197  QStringList result;
198  foreach (const PWad &file, d->wads)
199  {
200  if (Wadseeker::isForbiddenWad(file.name()))
201  {
202  result << file.name();
203  }
204  }
205  return result;
206 }
207 
208 QStringList MissingWadsDialog::optionalFiles() const
209 {
210  QStringList result;
211  foreach (const PWad &file, d->wads)
212  {
213  if (!Wadseeker::isForbiddenWad(file.name()) && file.isOptional())
214  {
215  result << file.name();
216  }
217  }
218  return result;
219 }
220 
221 QStringList MissingWadsDialog::filesToDownload() const
222 {
223  QStringList result = downloadableFiles();
224  result << selectedOptionalFiles();
225  return result;
226 }
227 
228 QStringList MissingWadsDialog::selectedOptionalFiles() const
229 {
230  QStringList result;
231  for (int i = 0; i < d->optionalFilesList->count(); ++i)
232  {
233  QListWidgetItem *item = d->optionalFilesList->item(i);
234  if (item->checkState() == Qt::Checked)
235  {
236  result << item->text();
237  }
238  }
239  return result;
240 }
241 
242 bool MissingWadsDialog::hasAnyAllowedFile() const
243 {
244  return !downloadableFiles().isEmpty() || !optionalFiles().isEmpty();
245 }
246 
247 MissingWadsDialog::MissingWadsProceed MissingWadsDialog::decision() const
248 {
249  return d->decision;
250 }
PWAD hosted on a server.
bool isOptional() const
Is this WAD required to join the server?
const QString & name() const
File name of the WAD.