File indexing completed on 2024-12-22 05:00:50

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "archivemailinfo.h"
0007 
0008 #include "archivemailagent_debug.h"
0009 #include <KLocalizedString>
0010 #include <QDir>
0011 
0012 ArchiveMailInfo::ArchiveMailInfo() = default;
0013 
0014 ArchiveMailInfo::ArchiveMailInfo(const KConfigGroup &config)
0015 {
0016     readConfig(config);
0017 }
0018 
0019 ArchiveMailInfo::ArchiveMailInfo(const ArchiveMailInfo &info)
0020     : mLastDateSaved(info.lastDateSaved())
0021     , mArchiveAge(info.archiveAge())
0022     , mArchiveType(info.archiveType())
0023     , mArchiveUnit(info.archiveUnit())
0024     , mSaveCollectionId(info.saveCollectionId())
0025     , mPath(info.url())
0026     , mRanges(info.range())
0027     , mMaximumArchiveCount(info.maximumArchiveCount())
0028     , mSaveSubCollection(info.saveSubCollection())
0029     , mIsEnabled(info.isEnabled())
0030     , mUseRange(info.useRange())
0031 {
0032 }
0033 
0034 ArchiveMailInfo::~ArchiveMailInfo() = default;
0035 
0036 ArchiveMailInfo &ArchiveMailInfo::operator=(const ArchiveMailInfo &old)
0037 {
0038     mLastDateSaved = old.lastDateSaved();
0039     mArchiveAge = old.archiveAge();
0040     mArchiveType = old.archiveType();
0041     mArchiveUnit = old.archiveUnit();
0042     mSaveCollectionId = old.saveCollectionId();
0043     mMaximumArchiveCount = old.maximumArchiveCount();
0044     mSaveSubCollection = old.saveSubCollection();
0045     mPath = old.url();
0046     mIsEnabled = old.isEnabled();
0047     mUseRange = old.useRange();
0048     mRanges = old.range();
0049     return *this;
0050 }
0051 
0052 QString normalizeFolderName(const QString &folderName)
0053 {
0054     QString adaptFolderName(folderName);
0055     adaptFolderName.replace(QLatin1Char('/'), QLatin1Char('_'));
0056     return adaptFolderName;
0057 }
0058 
0059 QString ArchiveMailInfo::dirArchive(bool &dirExit) const
0060 {
0061     const QDir dir(url().path());
0062     QString dirPath = url().path();
0063     if (!dir.exists()) {
0064         dirExit = false;
0065         dirPath = QDir::homePath();
0066         qCWarning(ARCHIVEMAILAGENT_LOG) << " Path doesn't exist" << dir.path();
0067     } else {
0068         dirExit = true;
0069     }
0070     return dirPath;
0071 }
0072 
0073 QList<int> ArchiveMailInfo::range() const
0074 {
0075     return mRanges;
0076 }
0077 
0078 void ArchiveMailInfo::setRange(const QList<int> &newRanges)
0079 {
0080     mRanges = newRanges;
0081 }
0082 
0083 bool ArchiveMailInfo::useRange() const
0084 {
0085     return mUseRange;
0086 }
0087 
0088 void ArchiveMailInfo::setUseRange(bool newUseRange)
0089 {
0090     mUseRange = newUseRange;
0091 }
0092 
0093 QUrl ArchiveMailInfo::realUrl(const QString &folderName, bool &dirExist) const
0094 {
0095     const int numExtensions = 4;
0096     // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
0097     const char *extensions[numExtensions] = {".zip", ".tar", ".tar.bz2", ".tar.gz"};
0098     const QString dirPath = dirArchive(dirExist);
0099 
0100     const QString path = dirPath + QLatin1Char('/') + i18nc("Start of the filename for a mail archive file", "Archive") + QLatin1Char('_')
0101         + normalizeFolderName(folderName) + QLatin1Char('_') + QDate::currentDate().toString(Qt::ISODate) + QString::fromLatin1(extensions[mArchiveType]);
0102     const QUrl real(QUrl::fromLocalFile(path));
0103     return real;
0104 }
0105 
0106 QStringList ArchiveMailInfo::listOfArchive(const QString &folderName, bool &dirExist) const
0107 {
0108     const int numExtensions = 4;
0109     // The extensions here are also sorted, like the enum order of BackupJob::ArchiveType
0110     const char *extensions[numExtensions] = {".zip", ".tar", ".tar.bz2", ".tar.gz"};
0111     const QString dirPath = dirArchive(dirExist);
0112 
0113     QDir dir(dirPath);
0114 
0115     QStringList nameFilters;
0116     nameFilters << i18nc("Start of the filename for a mail archive file", "Archive") + QLatin1Char('_') + normalizeFolderName(folderName) + QLatin1Char('_')
0117             + QLatin1StringView("*") + QString::fromLatin1(extensions[mArchiveType]);
0118     const QStringList lst = dir.entryList(nameFilters, QDir::Files | QDir::NoDotAndDotDot, QDir::Time | QDir::Reversed);
0119     return lst;
0120 }
0121 
0122 bool ArchiveMailInfo::isValid() const
0123 {
0124     return mSaveCollectionId != -1;
0125 }
0126 
0127 void ArchiveMailInfo::setArchiveAge(int age)
0128 {
0129     mArchiveAge = age;
0130 }
0131 
0132 int ArchiveMailInfo::archiveAge() const
0133 {
0134     return mArchiveAge;
0135 }
0136 
0137 void ArchiveMailInfo::setArchiveUnit(ArchiveMailInfo::ArchiveUnit unit)
0138 {
0139     mArchiveUnit = unit;
0140 }
0141 
0142 ArchiveMailInfo::ArchiveUnit ArchiveMailInfo::archiveUnit() const
0143 {
0144     return mArchiveUnit;
0145 }
0146 
0147 void ArchiveMailInfo::setArchiveType(MailCommon::BackupJob::ArchiveType type)
0148 {
0149     mArchiveType = type;
0150 }
0151 
0152 MailCommon::BackupJob::ArchiveType ArchiveMailInfo::archiveType() const
0153 {
0154     return mArchiveType;
0155 }
0156 
0157 void ArchiveMailInfo::setLastDateSaved(QDate date)
0158 {
0159     mLastDateSaved = date;
0160 }
0161 
0162 QDate ArchiveMailInfo::lastDateSaved() const
0163 {
0164     return mLastDateSaved;
0165 }
0166 
0167 void ArchiveMailInfo::readConfig(const KConfigGroup &config)
0168 {
0169     mPath = QUrl::fromUserInput(config.readEntry("storePath"));
0170 
0171     if (config.hasKey(QStringLiteral("lastDateSaved"))) {
0172         mLastDateSaved = QDate::fromString(config.readEntry("lastDateSaved"), Qt::ISODate);
0173     }
0174     mSaveSubCollection = config.readEntry("saveSubCollection", false);
0175     mArchiveType = static_cast<MailCommon::BackupJob::ArchiveType>(config.readEntry("archiveType", (int)MailCommon::BackupJob::Zip));
0176     mArchiveUnit = static_cast<ArchiveUnit>(config.readEntry("archiveUnit", (int)ArchiveDays));
0177     Akonadi::Collection::Id tId = config.readEntry("saveCollectionId", mSaveCollectionId);
0178     mArchiveAge = config.readEntry("archiveAge", 1);
0179     mMaximumArchiveCount = config.readEntry("maximumArchiveCount", 0);
0180     mUseRange = config.readEntry("useRange", false);
0181     mRanges = config.readEntry("ranges", QList<int>());
0182     if (tId >= 0) {
0183         mSaveCollectionId = tId;
0184     }
0185     mIsEnabled = config.readEntry("enabled", true);
0186 }
0187 
0188 void ArchiveMailInfo::writeConfig(KConfigGroup &config)
0189 {
0190     if (!isValid()) {
0191         return;
0192     }
0193     config.writeEntry("storePath", mPath.toLocalFile());
0194 
0195     if (mLastDateSaved.isValid()) {
0196         config.writeEntry("lastDateSaved", mLastDateSaved.toString(Qt::ISODate));
0197     }
0198 
0199     config.writeEntry("saveSubCollection", mSaveSubCollection);
0200     config.writeEntry("archiveType", static_cast<int>(mArchiveType));
0201     config.writeEntry("archiveUnit", static_cast<int>(mArchiveUnit));
0202     config.writeEntry("saveCollectionId", mSaveCollectionId);
0203     config.writeEntry("archiveAge", mArchiveAge);
0204     config.writeEntry("maximumArchiveCount", mMaximumArchiveCount);
0205     config.writeEntry("enabled", mIsEnabled);
0206     config.writeEntry("useRange", mUseRange);
0207     config.writeEntry("ranges", mRanges);
0208     config.sync();
0209 }
0210 
0211 QUrl ArchiveMailInfo::url() const
0212 {
0213     return mPath;
0214 }
0215 
0216 void ArchiveMailInfo::setUrl(const QUrl &url)
0217 {
0218     mPath = url;
0219 }
0220 
0221 bool ArchiveMailInfo::saveSubCollection() const
0222 {
0223     return mSaveSubCollection;
0224 }
0225 
0226 void ArchiveMailInfo::setSaveSubCollection(bool saveSubCol)
0227 {
0228     mSaveSubCollection = saveSubCol;
0229 }
0230 
0231 void ArchiveMailInfo::setSaveCollectionId(Akonadi::Collection::Id collectionId)
0232 {
0233     mSaveCollectionId = collectionId;
0234 }
0235 
0236 Akonadi::Collection::Id ArchiveMailInfo::saveCollectionId() const
0237 {
0238     return mSaveCollectionId;
0239 }
0240 
0241 int ArchiveMailInfo::maximumArchiveCount() const
0242 {
0243     return mMaximumArchiveCount;
0244 }
0245 
0246 void ArchiveMailInfo::setMaximumArchiveCount(int max)
0247 {
0248     mMaximumArchiveCount = max;
0249 }
0250 
0251 bool ArchiveMailInfo::isEnabled() const
0252 {
0253     return mIsEnabled;
0254 }
0255 
0256 void ArchiveMailInfo::setEnabled(bool b)
0257 {
0258     mIsEnabled = b;
0259 }
0260 
0261 bool ArchiveMailInfo::operator==(const ArchiveMailInfo &other) const
0262 {
0263     return saveCollectionId() == other.saveCollectionId() && saveSubCollection() == other.saveSubCollection() && url() == other.url()
0264         && archiveType() == other.archiveType() && archiveUnit() == other.archiveUnit() && archiveAge() == other.archiveAge()
0265         && lastDateSaved() == other.lastDateSaved() && maximumArchiveCount() == other.maximumArchiveCount() && isEnabled() == other.isEnabled()
0266         && useRange() == other.useRange() && range() == other.range();
0267 }