iwadpicker.cpp
1 //------------------------------------------------------------------------------
2 // iwadpicker.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) 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 "pathfinder/wadpathfinder.h"
29 #include "strings.hpp"
30 #include <QFileDialog>
31 #include <QFileInfo>
32 
33 DClass<IwadPicker> : public Ui::IwadPicker
34 {
35 };
36 
37 DPointered(IwadPicker)
38 
39 IwadPicker::IwadPicker(QWidget *parent)
40 : QWidget(parent)
41 {
42  d->setupUi(this);
43  loadIwads();
44 }
45 
46 IwadPicker::~IwadPicker()
47 {
48 }
49 
50 void IwadPicker::addIwad(const QString& path)
51 {
52  if (path.trimmed().isEmpty())
53  {
54  return;
55  }
56 
57  for (int i = 0; i < d->cboIwad->count(); ++i)
58  {
59  if (d->cboIwad->itemText(i).compare(path) == 0)
60  {
61  d->cboIwad->setCurrentIndex(i);
62  return;
63  }
64  }
65 
66  d->cboIwad->addItem(Strings::normalizePath(path));
67  d->cboIwad->setCurrentIndex(d->cboIwad->count() - 1);
68 }
69 
70 void IwadPicker::browse()
71 {
72  QString dialogDir = gConfig.doomseeker.previousCreateServerWadDir;
73  QString strFile = QFileDialog::getOpenFileName(this, tr("Doomseeker - select IWAD"), dialogDir);
74 
75  if (!strFile.isEmpty())
76  {
77  QFileInfo fi(strFile);
78  gConfig.doomseeker.previousCreateServerWadDir = fi.absolutePath();
79 
80  addIwad(strFile);
81  }
82 }
83 
84 QString IwadPicker::currentIwad() const
85 {
86  return d->cboIwad->currentText();
87 }
88 
89 void IwadPicker::loadIwads()
90 {
91  const QString iwads[] = {
92  "doom.wad", "doom1.wad", "doom2.wad",
93  "tnt.wad", "plutonia.wad", "heretic.wad",
94  "hexen.wad", "freedoom.wad", "freedm.wad",
95  "freedoom1.wad", "freedoom2.wad", "strife1.wad", ""
96  };
97 
98  QString currentSelection = d->cboIwad->currentText();
99  d->cboIwad->clear();
100  for (int i = 0; !iwads[i].isEmpty(); ++i)
101  {
102  WadPathFinder wadPathFinder = WadPathFinder(PathFinder());
103  wadPathFinder.setAllowAliases(false);
104  QString path = wadPathFinder.find(iwads[i]).path();
105  if (!path.isEmpty())
106  {
107  d->cboIwad->addItem(path);
108  }
109  }
110  if (!currentSelection.isEmpty())
111  {
112  int selectionIdx = d->cboIwad->findText(currentSelection);
113  if (selectionIdx >= 0)
114  d->cboIwad->setCurrentIndex(selectionIdx);
115  else
116  d->cboIwad->setEditText(currentSelection);
117  }
118 }
119 
120 void IwadPicker::setIwadByName(const QString &iwad)
121 {
122  for (int i = 0; i < d->cboIwad->count(); ++i)
123  {
124  QFileInfo fi(d->cboIwad->itemText(i));
125  if (fi.fileName().compare(iwad, Qt::CaseInsensitive) == 0)
126  {
127  d->cboIwad->setCurrentIndex(i);
128  return;
129  }
130  }
131  // If IWAD with given name isn't present on the list try to find it anyway.
132  PathFinder pathFinder;
133  QString path = pathFinder.findFile(iwad);
134  if (!path.isEmpty())
135  {
136  d->cboIwad->addItem(path);
137  d->cboIwad->setCurrentIndex(d->cboIwad->count() - 1);
138  }
139 }
Performs a case-insensitive (OS independent) file searches.
Definition: pathfinder.h:81
void setAllowAliases(bool allowed)
Can disable WAD aliasing for contexts where only specific WADs should be found.
static QString normalizePath(QString path)
Creates a clean path.
Definition: strings.cpp:360
Wrapper for PathFinder that specializes in findings WADs.
Definition: wadpathfinder.h:76
QString findFile(const QString &fileName) const
Performs a search for a single file.
Definition: pathfinder.cpp:156