File indexing completed on 2024-12-08 10:25:50

0001 /*
0002    SPDX-FileCopyrightText: 2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "previewurlcachemanager.h"
0008 #include "rocketchataccount.h"
0009 #include "ruqola_previewurlcache_debug.h"
0010 #include <KConfigGroup>
0011 #include <KSharedConfig>
0012 #include <QDir>
0013 #include <QTimer>
0014 #include <chrono>
0015 using namespace std::chrono_literals;
0016 
0017 PreviewUrlCacheManager::PreviewUrlCacheManager(RocketChatAccount *account, QObject *parent)
0018     : QObject{parent}
0019     , mRocketChatAccount(account)
0020 {
0021 }
0022 
0023 PreviewUrlCacheManager::~PreviewUrlCacheManager() = default;
0024 
0025 int PreviewUrlCacheManager::embedCacheExpirationDays() const
0026 {
0027     return mEmbedCacheExpirationDays;
0028 }
0029 
0030 void PreviewUrlCacheManager::setEmbedCacheExpirationDays(int newEmbedCacheExpirationDays)
0031 {
0032     if (mEmbedCacheExpirationDays != newEmbedCacheExpirationDays) {
0033         mEmbedCacheExpirationDays = newEmbedCacheExpirationDays;
0034         checkCache();
0035     }
0036 }
0037 
0038 bool PreviewUrlCacheManager::needToCheck() const
0039 {
0040     if (mRocketChatAccount) {
0041         return mRocketChatAccount->settings()->lastCheckedPreviewUrlCacheDate() != mCurrentDate;
0042     }
0043     return true;
0044 }
0045 
0046 void PreviewUrlCacheManager::saveLastCheckedDateTime()
0047 {
0048     if (mRocketChatAccount) {
0049         mRocketChatAccount->settings()->setLastCheckedPreviewUrlCacheDate(mCurrentDate);
0050     }
0051 }
0052 
0053 void PreviewUrlCacheManager::checkCache()
0054 {
0055     if (needToCheck()) {
0056         if (mCachePath.isEmpty()) {
0057             qCWarning(RUQOLA_PREVIEWURLCACHE_LOG) << "mCachePath is empty it's a bug!!! ";
0058             return;
0059         }
0060         QDir dir(mCachePath);
0061         const QFileInfoList infoLists = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0062         // qDebug() << " cachePath " << mCachePath;
0063         // qDebug() << " infoLists-- " << infoLists.count() << infoLists;
0064         for (const QFileInfo &info : infoLists) {
0065             // qDebug() << " info " << info << "  info.birthTime() " << info.birthTime();
0066             if (info.lastModified().date().addDays(mEmbedCacheExpirationDays) < mCurrentDate) {
0067                 const QString filePath{info.filePath()};
0068                 if (!QFile::remove(filePath)) {
0069                     qCWarning(RUQOLA_PREVIEWURLCACHE_LOG) << "Impossible to remove " << filePath;
0070                 }
0071             }
0072         }
0073         saveLastCheckedDateTime();
0074     }
0075 
0076     // Reactivate check each day
0077     QTimer::singleShot(24h, this, &PreviewUrlCacheManager::checkCache);
0078 }
0079 
0080 QDate PreviewUrlCacheManager::currentDate() const
0081 {
0082     return mCurrentDate;
0083 }
0084 
0085 void PreviewUrlCacheManager::setCurrentDate(const QDate &newCurrentDateTime)
0086 {
0087     mCurrentDate = newCurrentDateTime;
0088 }
0089 
0090 QString PreviewUrlCacheManager::cachePath() const
0091 {
0092     return mCachePath;
0093 }
0094 
0095 void PreviewUrlCacheManager::setCachePath(const QString &newCachePath)
0096 {
0097     mCachePath = newCachePath;
0098 }
0099 
0100 #include "moc_previewurlcachemanager.cpp"