chatlogrotate.cpp
1 //------------------------------------------------------------------------------
2 // chatlogrotate.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 "chatlogrotate.h"
24 
25 #include <QDateTime>
26 #include <QDir>
27 #include <QFile>
28 #include "irc/entities/ircnetworkentity.h"
29 #include "irc/chatlogarchive.h"
30 #include "irc/chatlogs.h"
31 #include "log.h"
32 
33 
34 DClass<ChatLogRotate>
35 {
36 public:
37  int maxSize;
38  int removalAgeDaysThreshold;
39 };
40 
41 DPointered(ChatLogRotate)
42 
43 
45 {
46  d->removalAgeDaysThreshold = -1;
47  d->maxSize = 5 * 1024 * 1024;
48 }
49 
50 ChatLogRotate::~ChatLogRotate()
51 {
52 }
53 
54 void ChatLogRotate::setMaxSize(int size)
55 {
56  d->maxSize = size;
57 }
58 
60 {
61  d->removalAgeDaysThreshold = age;
62 }
63 
64 void ChatLogRotate::rotate(const IRCNetworkEntity &network, const QString &recipient)
65 {
66  archivizeCurrent(network, recipient);
67  purgeOld(network, recipient);
68 }
69 
70 void ChatLogRotate::archivizeCurrent(const IRCNetworkEntity &network, const QString &recipient)
71 {
72  if (d->maxSize <= 0)
73  {
74  return;
75  }
76  QFile file(ChatLogs().logFilePath(network, recipient));
77  qint64 size = file.size();
78  if (size > d->maxSize)
79  {
80  mkBackupDir(network, recipient);
81  QString newName = ChatLogArchive::mkArchiveFilePath(network, recipient);
82  gLog << QString("IRC: Archiving log to file '%1'").arg(newName);
83  file.rename(newName);
84  }
85 }
86 
87 void ChatLogRotate::purgeOld(const IRCNetworkEntity &network, const QString &recipient)
88 {
89  if (d->removalAgeDaysThreshold < 0)
90  {
91  return;
92  }
93  QString dirPath = ChatLogArchive::archiveDirPath(network, recipient);
94  foreach (const QString &entry, ChatLogArchive::listArchivedLogsSortedByTime(network, recipient))
95  {
96  QString entryPath = QString("%1/%2").arg(dirPath, entry);
97  if (isEligibleForRemoval(entryPath))
98  {
99  gLog << QString("IRC: Removed archived log file '%1'").arg(entryPath);;
100  QFile file(entryPath);
101  file.remove();
102  }
103  }
104 }
105 
106 bool ChatLogRotate::isEligibleForRemoval(const QFileInfo &entry) const
107 {
108  return entry.isFile() && entry.lastModified().daysTo(QDateTime::currentDateTime()) > d->removalAgeDaysThreshold;
109 }
110 
111 void ChatLogRotate::mkBackupDir(const IRCNetworkEntity &network, const QString &recipient)
112 {
113  QDir().mkpath(ChatLogArchive::archiveDirPath(network, recipient));
114 }
void setRemovalAgeDaysThreshold(int age)
Data structure that describes and defines a connection to an IRC network or server.