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 #include "datapaths.h"
28 #include "templatedpathresolver.h"
29 
30 #include <QDir>
31 #include <QFileDialog>
32 #include <QStyle>
33 
34 DClass<LogDirectoryPicker> : public Ui::LogDirectoryPicker
35 {
36 public:
37  bool loggingEnabled;
38  QString dialogDir;
39 };
40 
41 DPointeredNoCopy(LogDirectoryPicker)
42 
43 LogDirectoryPicker::LogDirectoryPicker(QWidget *parent)
44  : QWidget(parent)
45 {
46  d->setupUi(this);
47 
48  d->btnBrowse->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon));
49 
50  d->loggingEnabled = gConfig.doomseeker.bLogCreatedServer;
51  d->dialogDir = gConfig.doomseeker.previousCreateServerLogDir;
52 
53  d->cbLoggingEnabled->setChecked(d->loggingEnabled);
54  setPathAndUpdate(d->dialogDir);
55 }
56 
57 LogDirectoryPicker::~LogDirectoryPicker()
58 {
59 }
60 
61 void LogDirectoryPicker::cbLoggingEnabledChecked(bool isChecked)
62 {
63  d->loggingEnabled = isChecked;
64  gConfig.doomseeker.bLogCreatedServer = d->loggingEnabled;
65 }
66 
67 void LogDirectoryPicker::textEdited()
68 {
69  d->dialogDir = d->pathTextBox->text();
70  updateLoggingStatus(d->dialogDir);
71 }
72 
73 void LogDirectoryPicker::browse()
74 {
75  QString strPath = QFileDialog::getExistingDirectory(this,
76  tr("Doomseeker - select Log path"),
77  gDoomseekerTemplatedPathResolver().resolve(d->dialogDir));
78  if (!strPath.isEmpty())
79  setPathAndUpdate(gDefaultDataPaths->portablizePath(strPath));
80 }
81 
82 void LogDirectoryPicker::updateLoggingStatus(const QString &path)
83 {
84  if (!path.isEmpty())
85  {
86  bool exists = QDir(gDoomseekerTemplatedPathResolver().resolve(path)).exists();
87  d->pathNotFound->setVisible(!exists);
88  d->cbLoggingEnabled->setEnabled(exists);
89  if (!exists)
90  setLoggingEnabled(false);
91  }
92  else
93  {
94  d->pathNotFound->hide();
95  d->cbLoggingEnabled->setEnabled(true);
96  }
97  gConfig.doomseeker.previousCreateServerLogDir = d->dialogDir;
98 }
99 
100 void LogDirectoryPicker::setPathAndUpdate(const QString &path)
101 {
102  d->dialogDir = path;
103  d->pathTextBox->setText(path);
104  updateLoggingStatus(path);
105 }
106 
107 void LogDirectoryPicker::setLoggingEnabled(const bool &enabled)
108 {
109  d->loggingEnabled = enabled;
110  gConfig.doomseeker.bLogCreatedServer = d->loggingEnabled;
111  d->cbLoggingEnabled->setChecked(d->loggingEnabled);
112 }
113 
114 const bool &LogDirectoryPicker::isLoggingEnabled() const
115 {
116  return d->loggingEnabled;
117 }
118 
119 const QString &LogDirectoryPicker::currentPath() const
120 {
121  return d->dialogDir;
122 }
123 
125 {
126  if (!d->loggingEnabled || !QDir(gDoomseekerTemplatedPathResolver().resolve(d->dialogDir)).exists())
127  return QString();
128  else
129  return d->dialogDir;
130 }