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 "datapaths.h"
27 #include "irc/configuration/chatlogscfg.h"
28 #include "templatedpathresolver.h"
29 #include <QDesktopServices>
30 #include <QFileDialog>
31 #include <QFileInfo>
32 #include <QMessageBox>
33 #include <QStyle>
34 #include <QUrl>
35 
36 DClass<CfgChatLogsPage> : public Ui::CfgChatLogsPage
37 {
38 };
39 
40 DPointered(CfgChatLogsPage)
41 
42 
43 CfgChatLogsPage::CfgChatLogsPage(QWidget *parent)
44  : ConfigPage(parent)
45 {
46  d->setupUi(this);
47  d->lblDirWarning->hide();
48  d->btnBrowseDir->setIcon(style()->standardIcon(QStyle::SP_DirOpenIcon));
49 
50  this->connect(d->leDir, SIGNAL(editingFinished()),
51  SIGNAL(validationRequested()));
52 }
53 
54 CfgChatLogsPage::~CfgChatLogsPage()
55 {
56 }
57 
58 void CfgChatLogsPage::browseStorageDirectory()
59 {
60  QString path = gDefaultDataPaths->portablizePath(QFileDialog::getExistingDirectory(this,
61  tr("Browse chat logs storage directory"),
62  gDoomseekerTemplatedPathResolver().resolve(d->leDir->text())));
63  if (!path.isEmpty())
64  {
65  d->leDir->setText(path);
66  emit validationRequested();
67  }
68 }
69 
70 bool CfgChatLogsPage::checkDir(const QString &directory)
71 {
72  if (directory.trimmed().isEmpty())
73  {
74  QMessageBox::critical(this, tr("Directory error"), tr("Directory not specified."));
75  return false;
76  }
77 
78  QFileInfo dir(directory);
79  if (!dir.exists())
80  {
81  QMessageBox::critical(this, tr("Directory error"), tr("Directory doesn't exist."));
82  return false;
83  }
84  QString validationError = validateChatLogsPath(dir);
85  if (!validationError.isEmpty())
86  {
87  QMessageBox::critical(this, tr("Directory error"), validationError);
88  return false;
89  }
90  return true;
91 }
92 
93 void CfgChatLogsPage::exploreStorageDirectory()
94 {
95  QString path = gDoomseekerTemplatedPathResolver().resolve(d->leDir->text().trimmed());
96  if (checkDir(path))
97  {
98  QDir dir(path);
99  QDesktopServices::openUrl(QString("file:///%1").arg(
100  QDir::toNativeSeparators(dir.absolutePath())));
101  }
102 }
103 
105 {
106  ChatLogsCfg cfg;
107  d->leDir->setText(cfg.chatLogsRootDir());
108  d->cbStoreLogs->setChecked(cfg.isStoreLogs());
109  d->cbRestoreLogs->setChecked(cfg.isRestoreChatFromLogs());
110  d->groupRemoveOldArchives->setChecked(cfg.isRemoveOldLogs());
111  d->spinLogRemovalAge->setValue(cfg.oldLogsRemovalDaysThreshold());
112 }
113 
115 {
116  ChatLogsCfg cfg;
117  cfg.setChatLogsRootDir(d->leDir->text().trimmed());
118  cfg.setStoreLogs(d->cbStoreLogs->isChecked());
119  cfg.setRestoreChatFromLogs(d->cbRestoreLogs->isChecked());
120  cfg.setRemoveOldLogs(d->groupRemoveOldArchives->isChecked());
121  cfg.setOldLogsRemovalDaysThreshold(d->spinLogRemovalAge->value());
122 }
123 
125 {
126  QString error = validateChatLogsPath(QFileInfo(
127  gDoomseekerTemplatedPathResolver().resolve(d->leDir->text().trimmed())));
128  d->lblDirWarning->setToolTip(error);
129  d->lblDirWarning->setVisible(!error.isEmpty());
130  return error.isEmpty() ? VALIDATION_OK : VALIDATION_ERROR;
131 }
132 
133 QString CfgChatLogsPage::validateChatLogsPath(const QFileInfo &path) const
134 {
135  if (path.exists() && !path.isDir())
136  return tr("The specified path isn't a directory.");
137  return "";
138 }