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