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 <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  return recipient.trimmed().toLower();
59  else
60  return "@main";
61 }
62 
63 bool ChatLogs::mkLogDir(const IRCNetworkEntity &entity)
64 {
65  QDir dir(networkDirPath(entity));
66  return dir.mkpath(".");
67 }
68 
69 QString ChatLogs::networkDirPath(const IRCNetworkEntity &entity) const
70 {
71  return QString("%1/%2").arg(d->rootPath(), entity.description().trimmed().toLower());
72 }
73 
74 bool ChatLogs::renameNetwork(QWidget *parentUi, QString oldName, QString newName)
75 {
76  oldName = oldName.trimmed().toLower();
77  newName = newName.trimmed().toLower();
78  if (oldName == newName)
79  return true;
80  QDir dir(d->rootPath());
81  if (!dir.exists(oldName))
82  return true;
83  while (true)
84  {
85  QMessageBox::StandardButton result = QMessageBox::Ok;
86  QString error;
87  if (dir.exists(newName))
88  {
89  error = tr("Won't transfer chat logs from \"%1\" to \"%2\" as directory \"%2\""
90  "already exists.").arg(oldName, newName);
91  }
92  else if (!dir.rename(oldName, newName))
93  error = tr(R"(Failed to transfer chat from "%1" to "%2")").arg(oldName, newName);
94 
95  if (error.isEmpty())
96  break;
97  result = QMessageBox::warning(parentUi, tr("Chat logs transfer"), error,
98  QMessageBox::Ignore | QMessageBox::Retry | QMessageBox::Abort);
99  if (result != QMessageBox::Retry)
100  return result == QMessageBox::Ignore;
101  }
102  return true;
103 }