logdirectorypicker.cpp
1 //------------------------------------------------------------------------------
2 // logdirectorypicker.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) 2019 Pol Marcet Sardà <polmarcetsarda@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "logdirectorypicker.h"
24 #include "ui_logdirectorypicker.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 
28 #include <QDir>
29 #include <QFileDialog>
30 
31 DClass<LogDirectoryPicker> : public Ui::LogDirectoryPicker
32 {
33 public:
34  bool loggingEnabled;
35  QString dialogDir;
36 };
37 
38 DPointeredNoCopy(LogDirectoryPicker)
39 
40 LogDirectoryPicker::LogDirectoryPicker(QWidget *parent)
41  : QWidget(parent)
42 {
43  d->setupUi(this);
44 
45  d->loggingEnabled = gConfig.doomseeker.bLogCreatedServer;
46  d->dialogDir = gConfig.doomseeker.previousCreateServerLogDir;
47 
48  d->cbLoggingEnabled->setChecked(d->loggingEnabled);
49  setPathAndUpdate(d->dialogDir);
50 }
51 
52 LogDirectoryPicker::~LogDirectoryPicker()
53 {
54 }
55 
56 void LogDirectoryPicker::cbLoggingEnabledChecked(bool isChecked)
57 {
58  d->loggingEnabled = isChecked;
59  gConfig.doomseeker.bLogCreatedServer = d->loggingEnabled;
60 }
61 
62 void LogDirectoryPicker::textEdited()
63 {
64  d->dialogDir = d->pathTextBox->text();
65  updateLoggingStatus(d->dialogDir);
66 }
67 
68 void LogDirectoryPicker::browse()
69 {
70  QString strPath = QFileDialog::getExistingDirectory(this,
71  tr("Doomseeker - select Log path"), d->dialogDir);
72  if (!strPath.isEmpty())
73  setPathAndUpdate(QDir(strPath).absolutePath());
74 }
75 
76 void LogDirectoryPicker::updateLoggingStatus(const QString &path)
77 {
78  if (!path.isEmpty())
79  {
80  bool exists = QDir(path).exists();
81  d->pathNotFound->setVisible(!exists);
82  d->cbLoggingEnabled->setEnabled(exists);
83  if (!exists)
84  setLoggingEnabled(false);
85  }
86  else
87  {
88  d->pathNotFound->hide();
89  d->cbLoggingEnabled->setEnabled(true);
90  }
91  gConfig.doomseeker.previousCreateServerLogDir = d->dialogDir;
92 }
93 
94 void LogDirectoryPicker::setPathAndUpdate(const QString &path)
95 {
96  d->dialogDir = path;
97  d->pathTextBox->setText(path);
98  updateLoggingStatus(path);
99 }
100 
101 void LogDirectoryPicker::setLoggingEnabled(const bool &enabled)
102 {
103  d->loggingEnabled = enabled;
104  gConfig.doomseeker.bLogCreatedServer = d->loggingEnabled;
105  d->cbLoggingEnabled->setChecked(d->loggingEnabled);
106 }
107 
108 const bool &LogDirectoryPicker::isLoggingEnabled() const
109 {
110  return d->loggingEnabled;
111 }
112 
113 const QString &LogDirectoryPicker::currentPath() const
114 {
115  return d->dialogDir;
116 }
117 
119 {
120  if (!d->loggingEnabled || !QDir(d->dialogDir).exists())
121  return QString();
122  else
123  return QDir(d->dialogDir).absolutePath();
124 }