cfgwadseekergeneral.cpp
1 //------------------------------------------------------------------------------
2 // cfgwadseekergeneral.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) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "cfgwadseekergeneral.h"
24 #include "ui_cfgwadseekergeneral.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "pathfinder/filesearchpath.h"
28 #include "templatedpathresolver.h"
29 #include <QCompleter>
30 #include <QDebug>
31 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
32 #include <QFileSystemModel>
33 #else
34 #include <QDirModel>
35 #endif
36 #include <QMessageBox>
37 
38 DClass<CFGWadseekerGeneral> : public Ui::CFGWadseekerGeneral
39 {
40 public:
41  bool completerActive;
42 
43  QString targetDirectory() const
44  {
45  return cbTargetDirectory->currentText().trimmed();
46  }
47 };
48 
49 DPointered(CFGWadseekerGeneral)
50 
52  : ConfigPage(parent)
53 {
54  d->setupUi(this);
55  d->completerActive = false;
56 
57  // Settings defined in this widget are ATM unused.
58  d->lblDirectoryWarning->setPixmap(QPixmap(":/icons/exclamation_16.png"));
59  d->lblDirectoryWarning->hide();
60  d->lblDirectoryWarning->setWordWrap(true);
61 
62  this->connect(d->cbTargetDirectory, SIGNAL(editTextChanged(QString)),
63  SIGNAL(validationRequested()));
64 }
65 
66 CFGWadseekerGeneral::~CFGWadseekerGeneral()
67 {
68 }
69 
70 void CFGWadseekerGeneral::activateCompleter()
71 {
72  // Lazy activation of completer prevents floppy drive clicking
73  // on QComboBox::addItem() if you're on Windows and if you
74  // actually have a floppy drive.
75  //
76  // The floppy drive will still click *one time* when user tries to type
77  // anything, but at least it won't click on readSettings() anymore
78  // (so, essentially, when user opens config box).
79  if (!d->completerActive)
80  {
81 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
82  // NOTE: Although QFileSystemModel is available since Qt 4, there were
83  // some issues with it and QCompleter until at least Qt 5.12 (i.e.
84  // QTBUG-38014). We'll conservatively use QFileSystemModel on Qt 6 only.
85  auto completer = new QCompleter(this);
86  auto model = new QFileSystemModel(completer);
87  model->setRootPath(gDoomseekerTemplatedPathResolver().resolve(d->targetDirectory()));
88  completer->setModel(model);
89  d->cbTargetDirectory->setCompleter(completer);
90 #else
91  d->cbTargetDirectory->setCompleter(new QCompleter(new QDirModel()));
92 #endif
93  d->completerActive = true;
94  }
95 }
96 
97 void CFGWadseekerGeneral::fillTargetDirectoryComboBox()
98 {
99  d->cbTargetDirectory->clear();
100  d->cbTargetDirectory->addItems(gConfig.doomseeker.wadPathsOnly());
101 }
102 
104 {
105  fillTargetDirectoryComboBox();
106 
107  d->cbTargetDirectory->setEditText(gConfig.wadseeker.targetDirectory);
108  d->spinMaxConcurrentSiteSeeks->setValue(gConfig.wadseeker.maxConcurrentSiteDownloads);
109  d->spinMaxConcurrentWadDownloads->setValue(gConfig.wadseeker.maxConcurrentWadDownloads);
110 }
111 
113 {
114  gConfig.wadseeker.targetDirectory = d->targetDirectory();
115  gConfig.wadseeker.maxConcurrentSiteDownloads = d->spinMaxConcurrentSiteSeeks->value();
116  gConfig.wadseeker.maxConcurrentWadDownloads = d->spinMaxConcurrentWadDownloads->value();
117 }
118 
119 void CFGWadseekerGeneral::showEvent(QShowEvent *event)
120 {
121  activateCompleter();
122  ConfigPage::showEvent(event);
123 }
124 
126 {
127  QString error;
128 
129  QFileInfo targetDirectory(gDoomseekerTemplatedPathResolver().resolve(d->targetDirectory()));
130  if (error.isEmpty() && d->targetDirectory().isEmpty())
131  error = tr("No path specified.");
132 
133  if (error.isEmpty() && !targetDirectory.exists())
134  error = tr("This path doesn't exist.");
135 
136  if (error.isEmpty() && !targetDirectory.isDir())
137  error = tr("This is not a directory.");
138 
139  if (error.isEmpty() && !targetDirectory.isWritable())
140  error = tr("This directory cannot be written to.");
141 
142  d->lblDirectoryWarning->setVisible(!error.isEmpty());
143  d->lblDirectoryWarning->setText(error);
144  return error.isEmpty() ? VALIDATION_OK : VALIDATION_ERROR;
145 }