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  return;
54 
55  for (int i = 0; i < d->cboIwad->count(); ++i)
56  {
57  if (d->cboIwad->itemText(i).compare(path) == 0)
58  {
59  d->cboIwad->setCurrentIndex(i);
60  return;
61  }
62  }
63 
64  d->cboIwad->addItem(Strings::normalizePath(path));
65  d->cboIwad->setCurrentIndex(d->cboIwad->count() - 1);
66 }
67 
68 void IwadPicker::browse()
69 {
70  QString dialogDir = gConfig.doomseeker.previousCreateServerWadDir;
71  QString strFile = QFileDialog::getOpenFileName(this, tr("Doomseeker - select IWAD"), dialogDir);
72 
73  if (!strFile.isEmpty())
74  {
75  QFileInfo fi(strFile);
76  gConfig.doomseeker.previousCreateServerWadDir = fi.absolutePath();
77 
78  addIwad(strFile);
79  }
80 }
81 
82 QString IwadPicker::currentIwad() const
83 {
84  return d->cboIwad->currentText();
85 }
86 
87 void IwadPicker::loadIwads()
88 {
89  const QString iwads[] = {
90  "doom.wad", "doom1.wad", "doom2.wad",
91  "tnt.wad", "plutonia.wad", "heretic.wad",
92  "hexen.wad", "freedoom.wad", "freedm.wad",
93  "freedoom1.wad", "freedoom2.wad", "strife1.wad", ""
94  };
95 
96  QString currentSelection = d->cboIwad->currentText();
97  d->cboIwad->clear();
98  for (int i = 0; !iwads[i].isEmpty(); ++i)
99  {
100  WadPathFinder wadPathFinder = WadPathFinder(PathFinder());
101  wadPathFinder.setAllowAliases(false);
102  QString path = wadPathFinder.find(iwads[i]).path();
103  if (!path.isEmpty())
104  d->cboIwad->addItem(path);
105  }
106  if (!currentSelection.isEmpty())
107  {
108  int selectionIdx = d->cboIwad->findText(currentSelection);
109  if (selectionIdx >= 0)
110  d->cboIwad->setCurrentIndex(selectionIdx);
111  else
112  d->cboIwad->setEditText(currentSelection);
113  }
114 }
115 
116 void IwadPicker::setIwadByName(const QString &iwad)
117 {
118  for (int i = 0; i < d->cboIwad->count(); ++i)
119  {
120  QFileInfo fi(d->cboIwad->itemText(i));
121  if (fi.fileName().compare(iwad, Qt::CaseInsensitive) == 0)
122  {
123  d->cboIwad->setCurrentIndex(i);
124  return;
125  }
126  }
127  // If IWAD with given name isn't present on the list try to find it anyway.
128  PathFinder pathFinder;
129  QString path = pathFinder.findFile(iwad);
130  if (!path.isEmpty())
131  {
132  d->cboIwad->addItem(path);
133  d->cboIwad->setCurrentIndex(d->cboIwad->count() - 1);
134  }
135 }