chatlogs.cpp
1 //------------------------------------------------------------------------------
2 // chatlogs.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 "chatlogs.h"
24 
25 #include "irc/configuration/chatlogscfg.h"
26 #include "irc/entities/ircnetworkentity.h"
27 #include "templatedpathresolver.h"
28 #include <QDir>
29 #include <QMessageBox>
30 
31 DClass<ChatLogs>
32 {
33 public:
34  QString rootPath() const
35  {
36  return gDoomseekerTemplatedPathResolver().resolve(ChatLogsCfg().chatLogsRootDir());
37  }
38 };
39 
40 DPointered(ChatLogs)
41 
42 
44 {
45 }
46 
47 ChatLogs::~ChatLogs()
48 {
49 }
50 
51 QString ChatLogs::logFilePath(const IRCNetworkEntity &entity, const QString &recipient) const
52 {
53  return QString("%1/%2.txt").arg(networkDirPath(entity), logFileName(recipient));
54 }
55 
56 QString ChatLogs::logFileName(const QString &recipient) const
57 {
58  if (!recipient.trimmed().isEmpty())
59  return recipient.trimmed().toLower();
60  else
61  return "@main";
62 }
63 
64 bool ChatLogs::mkLogDir(const IRCNetworkEntity &entity)
65 {
66  QDir dir(networkDirPath(entity));
67  return dir.mkpath(".");
68 }
69 
70 QString ChatLogs::networkDirPath(const IRCNetworkEntity &entity) const
71 {
72  return QString("%1/%2").arg(d->rootPath(), entity.description().trimmed().toLower());
73 }
74 
75 bool ChatLogs::renameNetwork(QWidget *parentUi, QString oldName, QString newName)
76 {
77  oldName = oldName.trimmed().toLower();
78  newName = newName.trimmed().toLower();
79  if (oldName == newName)
80  return true;
81  QDir dir(d->rootPath());
82  if (!dir.exists(oldName))
83  return true;
84  while (true)
85  {
86  QMessageBox::StandardButton result = QMessageBox::Ok;
87  QString error;
88  if (dir.exists(newName))
89  {
90  error = tr("Won't transfer chat logs from \"%1\" to \"%2\" as directory \"%2\""
91  " already exists.").arg(oldName, newName);
92  }
93  else if (!dir.rename(oldName, newName))
94  error = tr(R"(Failed to transfer chat from "%1" to "%2")").arg(oldName, newName);
95 
96  if (error.isEmpty())
97  break;
98  result = QMessageBox::warning(parentUi, tr("Chat logs transfer"), error,
99  QMessageBox::Ignore | QMessageBox::Retry | QMessageBox::Abort);
100  if (result != QMessageBox::Retry)
101  return result == QMessageBox::Ignore;
102  }
103  return true;
104 }