23 #include "chatlogrotate.h"
25 #include "irc/chatlogarchive.h"
26 #include "irc/chatlogs.h"
27 #include "irc/entities/ircnetworkentity.h"
38 int removalAgeDaysThreshold;
46 d->removalAgeDaysThreshold = -1;
47 d->maxSize = 5 * 1024 * 1024;
50 ChatLogRotate::~ChatLogRotate()
54 void ChatLogRotate::setMaxSize(
int size)
61 d->removalAgeDaysThreshold = age;
64 void ChatLogRotate::rotate(
const IRCNetworkEntity &network,
const QString &recipient)
66 archivizeCurrent(network, recipient);
67 purgeOld(network, recipient);
70 void ChatLogRotate::archivizeCurrent(
const IRCNetworkEntity &network,
const QString &recipient)
74 QFile file(
ChatLogs().logFilePath(network, recipient));
75 qint64 size = file.size();
76 if (size > d->maxSize)
78 mkBackupDir(network, recipient);
79 QString newName = ChatLogArchive::mkArchiveFilePath(network, recipient);
80 gLog << QString(
"IRC: Archiving log to file '%1'").arg(newName);
85 void ChatLogRotate::purgeOld(
const IRCNetworkEntity &network,
const QString &recipient)
87 if (d->removalAgeDaysThreshold < 0)
89 QString dirPath = ChatLogArchive::archiveDirPath(network, recipient);
90 for (
const QString &entry : ChatLogArchive::listArchivedLogsSortedByTime(network, recipient))
92 QString entryPath = QString(
"%1/%2").arg(dirPath, entry);
93 if (isEligibleForRemoval(QFileInfo(entryPath)))
95 gLog << QString(
"IRC: Removed archived log file '%1'").arg(entryPath);
96 QFile file(entryPath);
102 bool ChatLogRotate::isEligibleForRemoval(
const QFileInfo &entry)
const
104 return entry.isFile() && entry.lastModified().daysTo(QDateTime::currentDateTime()) > d->removalAgeDaysThreshold;
107 void ChatLogRotate::mkBackupDir(
const IRCNetworkEntity &network,
const QString &recipient)
109 QDir().mkpath(ChatLogArchive::archiveDirPath(network, recipient));