File indexing completed on 2025-06-29 04:48:11

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "archivemailagentutil.h"
0008 
0009 QDate ArchiveMailAgentUtil::diffDate(ArchiveMailInfo *info)
0010 {
0011     QDate diffDate(info->lastDateSaved());
0012     switch (info->archiveUnit()) {
0013     case ArchiveMailInfo::ArchiveDays:
0014         diffDate = diffDate.addDays(info->archiveAge());
0015         break;
0016     case ArchiveMailInfo::ArchiveWeeks:
0017         diffDate = diffDate.addDays(info->archiveAge() * 7);
0018         break;
0019     case ArchiveMailInfo::ArchiveMonths:
0020         diffDate = diffDate.addMonths(info->archiveAge());
0021         break;
0022     case ArchiveMailInfo::ArchiveYears:
0023         diffDate = diffDate.addYears(info->archiveAge());
0024         break;
0025     }
0026     return diffDate;
0027 }
0028 
0029 bool ArchiveMailAgentUtil::timeIsInRange(const QList<int> &range, QTime time)
0030 {
0031     const int hour = time.hour();
0032     const int startRange = range.at(0);
0033     const int endRange = range.at(1);
0034     if ((hour >= startRange) && (hour <= endRange)) {
0035         return true;
0036     } else {
0037         // Range as 23h -> 5h
0038         if ((hour >= startRange) && (hour > endRange)) {
0039             return true;
0040         } else if ((startRange > endRange) && (hour < startRange && (hour <= endRange))) { // Range as 23h -> 5h
0041             return true;
0042         }
0043         return false;
0044     }
0045 }
0046 
0047 bool ArchiveMailAgentUtil::needToArchive(ArchiveMailInfo *info)
0048 {
0049     if (!info->isEnabled()) {
0050         return false;
0051     }
0052     if (info->url().isEmpty()) {
0053         return false;
0054     }
0055     if (!info->lastDateSaved().isValid()) {
0056         if (info->useRange()) {
0057             return ArchiveMailAgentUtil::timeIsInRange(info->range(), QTime::currentTime());
0058         }
0059         return true;
0060     } else {
0061         if (QDate::currentDate() >= diffDate(info)) {
0062             if (info->useRange()) {
0063                 return ArchiveMailAgentUtil::timeIsInRange(info->range(), QTime::currentTime());
0064             }
0065             return true;
0066         }
0067     }
0068     return false;
0069 }