23 #include "chatlogrotate.h"
28 #include "irc/entities/ircnetworkentity.h"
29 #include "irc/chatlogarchive.h"
30 #include "irc/chatlogs.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)
76 QFile file(
ChatLogs().logFilePath(network, recipient));
77 qint64 size = file.size();
78 if (size > d->maxSize)
80 mkBackupDir(network, recipient);
81 QString newName = ChatLogArchive::mkArchiveFilePath(network, recipient);
82 gLog << QString(
"IRC: Archiving log to file '%1'").arg(newName);
87 void ChatLogRotate::purgeOld(
const IRCNetworkEntity &network,
const QString &recipient)
89 if (d->removalAgeDaysThreshold < 0)
93 QString dirPath = ChatLogArchive::archiveDirPath(network, recipient);
94 foreach (
const QString &entry, ChatLogArchive::listArchivedLogsSortedByTime(network, recipient))
96 QString entryPath = QString(
"%1/%2").arg(dirPath, entry);
97 if (isEligibleForRemoval(entryPath))
99 gLog << QString(
"IRC: Removed archived log file '%1'").arg(entryPath);;
100 QFile file(entryPath);
106 bool ChatLogRotate::isEligibleForRemoval(
const QFileInfo &entry)
const
108 return entry.isFile() && entry.lastModified().daysTo(QDateTime::currentDateTime()) > d->removalAgeDaysThreshold;
111 void ChatLogRotate::mkBackupDir(
const IRCNetworkEntity &network,
const QString &recipient)
113 QDir().mkpath(ChatLogArchive::archiveDirPath(network, recipient));
void setRemovalAgeDaysThreshold(int age)
Data structure that describes and defines a connection to an IRC network or server.