cfgchatlogspage.cpp
1 //------------------------------------------------------------------------------
2 // cfgchatlogspage.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 "cfgchatlogspage.h"
24 #include "ui_cfgchatlogspage.h"
25 
26 #include "irc/configuration/chatlogscfg.h"
27 #include <QDesktopServices>
28 #include <QFileDialog>
29 #include <QFileInfo>
30 #include <QMessageBox>
31 #include <QUrl>
32 
33 DClass<CfgChatLogsPage> : public Ui::CfgChatLogsPage
34 {
35 };
36 
37 DPointered(CfgChatLogsPage)
38 
39 
40 CfgChatLogsPage::CfgChatLogsPage(QWidget *parent)
41 : ConfigPage(parent)
42 {
43  d->setupUi(this);
44  d->lblDirWarning->hide();
45 
46  this->connect(d->leDir, SIGNAL(editingFinished()),
47  SIGNAL(validationRequested()));
48 }
49 
50 CfgChatLogsPage::~CfgChatLogsPage()
51 {
52 }
53 
54 void CfgChatLogsPage::browseStorageDirectory()
55 {
56  QString path = QFileDialog::getExistingDirectory(this,
57  tr("Browse chat logs storage directory"), d->leDir->text());
58  if (!path.isEmpty())
59  {
60  d->leDir->setText(path);
61  emit validationRequested();
62  }
63 }
64 
65 bool CfgChatLogsPage::checkDir(const QString &directory)
66 {
67  if (directory.trimmed().isEmpty())
68  {
69  QMessageBox::critical(this, tr("Directory error"), tr("Directory not specified."));
70  return false;
71  }
72 
73  QFileInfo dir(directory);
74  if (!dir.exists())
75  {
76  QMessageBox::critical(this, tr("Directory error"), tr("Directory doesn't exist."));
77  return false;
78  }
79  QString validationError = validateChatLogsPath(directory);
80  if (!validationError.isEmpty())
81  {
82  QMessageBox::critical(this, tr("Directory error"), validationError);
83  return false;
84  }
85  return true;
86 }
87 
88 void CfgChatLogsPage::exploreStorageDirectory()
89 {
90  QString path = d->leDir->text().trimmed();
91  if (checkDir(path))
92  {
93  QDesktopServices::openUrl(QString("file:///%1").arg(
94  QDir::toNativeSeparators(path)));
95  }
96 }
97 
99 {
100  ChatLogsCfg cfg;
101  d->leDir->setText(cfg.chatLogsRootDir());
102  d->cbStoreLogs->setChecked(cfg.isStoreLogs());
103  d->cbRestoreLogs->setChecked(cfg.isRestoreChatFromLogs());
104  d->groupRemoveOldArchives->setChecked(cfg.isRemoveOldLogs());
105  d->spinLogRemovalAge->setValue(cfg.oldLogsRemovalDaysThreshold());
106 }
107 
109 {
110  ChatLogsCfg cfg;
111  cfg.setChatLogsRootDir(d->leDir->text().trimmed());
112  cfg.setStoreLogs(d->cbStoreLogs->isChecked());
113  cfg.setRestoreChatFromLogs(d->cbRestoreLogs->isChecked());
114  cfg.setRemoveOldLogs(d->groupRemoveOldArchives->isChecked());
115  cfg.setOldLogsRemovalDaysThreshold(d->spinLogRemovalAge->value());
116 }
117 
119 {
120  QString error = validateChatLogsPath(d->leDir->text().trimmed());
121  d->lblDirWarning->setToolTip(error);
122  d->lblDirWarning->setVisible(!error.isEmpty());
123  return error.isEmpty() ? VALIDATION_OK : VALIDATION_ERROR;
124 }
125 
126 QString CfgChatLogsPage::validateChatLogsPath(const QFileInfo &path) const
127 {
128  if (path.exists() && !path.isDir())
129  {
130  return tr("Specified path isn't a directory.");
131  }
132  return "";
133 }
Validation
Result of validate()
Definition: configpage.h:50
Validation validate()
Validate settings on this page.
void validationRequested()
Request that the page should be (re-)validated.
void readSettings()
Reimplement this to read settings from config into widgets.
Validation detected no problems.
Definition: configpage.h:53
void saveSettings()
Reimplement this to write settings to config from widgets.
Validation detected at least one problem.
Definition: configpage.h:55
Base class for configuration pages.
Definition: configpage.h:44