iwadpicker.cpp
1 //------------------------------------------------------------------------------
2 // iwadpicker.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "iwadpicker.h"
24 #include "ui_iwadpicker.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "pathfinder/pathfinder.h"
28 #include "strings.h"
29 #include <QFileDialog>
30 #include <QFileInfo>
31 
32 DClass<IwadPicker> : public Ui::IwadPicker
33 {
34 };
35 
36 DPointered(IwadPicker)
37 
38 IwadPicker::IwadPicker(QWidget *parent)
39 : QWidget(parent)
40 {
41  d->setupUi(this);
42  loadIwads();
43 }
44 
45 IwadPicker::~IwadPicker()
46 {
47 }
48 
49 void IwadPicker::addIwad(const QString& path)
50 {
51  if (path.trimmed().isEmpty())
52  {
53  return;
54  }
55 
56  for (int i = 0; i < d->cboIwad->count(); ++i)
57  {
58  if (d->cboIwad->itemText(i).compare(path) == 0)
59  {
60  d->cboIwad->setCurrentIndex(i);
61  return;
62  }
63  }
64 
65  d->cboIwad->addItem(Strings::normalizePath(path));
66  d->cboIwad->setCurrentIndex(d->cboIwad->count() - 1);
67 }
68 
69 void IwadPicker::browse()
70 {
71  QString dialogDir = gConfig.doomseeker.previousCreateServerWadDir;
72  QString strFile = QFileDialog::getOpenFileName(this, tr("Doomseeker - select IWAD"), dialogDir);
73 
74  if (!strFile.isEmpty())
75  {
76  QFileInfo fi(strFile);
77  gConfig.doomseeker.previousCreateServerWadDir = fi.absolutePath();
78 
79  addIwad(strFile);
80  }
81 }
82 
83 QString IwadPicker::currentIwad() const
84 {
85  return d->cboIwad->currentText();
86 }
87 
88 void IwadPicker::loadIwads()
89 {
90  const QString iwads[] = {
91  "doom.wad", "doom1.wad", "doom2.wad",
92  "tnt.wad", "plutonia.wad", "heretic.wad",
93  "hexen.wad", "freedoom.wad", "freedm.wad",
94  "freedoom1.wad", "freedoom2.wad", "strife1.wad", ""
95  };
96 
97  d->cboIwad->clear();
98  for (int i = 0; !iwads[i].isEmpty(); ++i)
99  {
100  PathFinder pathFinder;
101  QString path = pathFinder.findFile(iwads[i]);
102  if (!path.isEmpty())
103  {
104  d->cboIwad->addItem(path);
105  }
106  }
107 }
108 
109 void IwadPicker::setIwadByName(const QString &iwad)
110 {
111  for (int i = 0; i < d->cboIwad->count(); ++i)
112  {
113  QFileInfo fi(d->cboIwad->itemText(i));
114  if (fi.fileName().compare(iwad, Qt::CaseInsensitive) == 0)
115  {
116  d->cboIwad->setCurrentIndex(i);
117  return;
118  }
119  }
120  // If IWAD with given name isn't present on the list try to find it anyway.
121  PathFinder pathFinder;
122  QString path = pathFinder.findFile(iwad);
123  if (!path.isEmpty())
124  {
125  d->cboIwad->addItem(path);
126  d->cboIwad->setCurrentIndex(d->cboIwad->count() - 1);
127  }
128 }
Performs a case-insensitive (OS independent) file searches.
Definition: pathfinder.h:81
static QString normalizePath(QString path)
Creates a clean path.
Definition: strings.cpp:359
QString findFile(const QString &fileName) const
Performs a search for a single file.
Definition: pathfinder.cpp:163