chatlogs.cpp
1 //------------------------------------------------------------------------------
2 // chatlogs.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 <QDir>
28 #include <QMessageBox>
29 
30 DClass<ChatLogs>
31 {
32 public:
33  QString rootPath() const
34  {
35  return ChatLogsCfg().chatLogsRootDir();
36  }
37 };
38 
39 DPointered(ChatLogs)
40 
41 
43 {
44 }
45 
46 ChatLogs::~ChatLogs()
47 {
48 }
49 
50 QString ChatLogs::logFilePath(const IRCNetworkEntity &entity, const QString &recipient) const
51 {
52  return QString("%1/%2.txt").arg(networkDirPath(entity), logFileName(recipient));
53 }
54 
55 QString ChatLogs::logFileName(const QString &recipient) const
56 {
57  if (!recipient.trimmed().isEmpty())
58  {
59  return recipient.trimmed().toLower();
60  }
61  else
62  {
63  return "@main";
64  }
65 }
66 
67 bool ChatLogs::mkLogDir(const IRCNetworkEntity &entity)
68 {
69  QDir dir(networkDirPath(entity));
70  return dir.mkpath(".");
71 }
72 
73 QString ChatLogs::networkDirPath(const IRCNetworkEntity &entity) const
74 {
75  return QString("%1/%2").arg(d->rootPath(), entity.description().trimmed().toLower());
76 }
77 
78 bool ChatLogs::renameNetwork(QWidget *parentUi, QString oldName, QString newName)
79 {
80  oldName = oldName.trimmed().toLower();
81  newName = newName.trimmed().toLower();
82  if (oldName == newName)
83  {
84  return true;
85  }
86  QDir dir(d->rootPath());
87  if (!dir.exists(oldName))
88  {
89  return true;
90  }
91  while (true)
92  {
93  QMessageBox::StandardButton result = QMessageBox::Ok;
94  QString error;
95  if (dir.exists(newName))
96  {
97  error = tr("Won't transfer chat logs from \"%1\" to \"%2\" as directory \"%2\""
98  "already exists.").arg(oldName, newName);
99  }
100  else if (!dir.rename(oldName, newName))
101  {
102  error = tr("Failed to transfer chat from \"%1\" to \"%2\"").arg(oldName, newName);
103  }
104 
105  if (error.isEmpty())
106  {
107  break;
108  }
109  result = QMessageBox::warning(parentUi, tr("Chat logs transfer"), error,
110  QMessageBox::Ignore | QMessageBox::Retry | QMessageBox::Abort);
111  if (result != QMessageBox::Retry)
112  {
113  return result == QMessageBox::Ignore;
114  }
115  }
116  return true;
117 }
const QString & description() const
A short, human-readable description for the network. (Preferably a single word).
Data structure that describes and defines a connection to an IRC network or server.