chatlogrotate.cpp
1 //------------------------------------------------------------------------------
2 // chatlogrotate.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 "chatlogrotate.h"
24 
25 #include "irc/chatlogarchive.h"
26 #include "irc/chatlogs.h"
27 #include "irc/entities/ircnetworkentity.h"
28 #include "log.h"
29 #include <QDateTime>
30 #include <QDir>
31 #include <QFile>
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  return;
74  QFile file(ChatLogs().logFilePath(network, recipient));
75  qint64 size = file.size();
76  if (size > d->maxSize)
77  {
78  mkBackupDir(network, recipient);
79  QString newName = ChatLogArchive::mkArchiveFilePath(network, recipient);
80  gLog << QString("IRC: Archiving log to file '%1'").arg(newName);
81  file.rename(newName);
82  }
83 }
84 
85 void ChatLogRotate::purgeOld(const IRCNetworkEntity &network, const QString &recipient)
86 {
87  if (d->removalAgeDaysThreshold < 0)
88  return;
89  QString dirPath = ChatLogArchive::archiveDirPath(network, recipient);
90  for (const QString &entry : ChatLogArchive::listArchivedLogsSortedByTime(network, recipient))
91  {
92  QString entryPath = QString("%1/%2").arg(dirPath, entry);
93  if (isEligibleForRemoval(entryPath))
94  {
95  gLog << QString("IRC: Removed archived log file '%1'").arg(entryPath);
96  QFile file(entryPath);
97  file.remove();
98  }
99  }
100 }
101 
102 bool ChatLogRotate::isEligibleForRemoval(const QFileInfo &entry) const
103 {
104  return entry.isFile() && entry.lastModified().daysTo(QDateTime::currentDateTime()) > d->removalAgeDaysThreshold;
105 }
106 
107 void ChatLogRotate::mkBackupDir(const IRCNetworkEntity &network, const QString &recipient)
108 {
109  QDir().mkpath(ChatLogArchive::archiveDirPath(network, recipient));
110 }