| 1 | #include "cachemanagerplugin.h" |
| 2 | #include <QCoreApplication> |
| 3 | #include <QDateTime> |
| 4 | #include <QDBusPendingReply> |
| 5 | #include <QDBusPendingCallWatcher> |
| 6 | #include <QDebug> |
| 7 | #include <SnapStandardPaths.h> |
| 8 | #include <MailServiceClient.h> |
| 9 | |
| 10 | #define CACHE_MGR_NS "cache-mgr" |
| 11 | #define CACHE_MGR_PERIOD "period" |
| 12 | |
| 13 | namespace { |
| 14 | inline QMailMessageKey to_msg_key(const QByteArray &key) { |
| 15 | QMailMessageKey mKey; |
| 16 | QByteArray m = key; |
| 17 | QDataStream keystream(&m, QIODevice::ReadWrite); |
| 18 | mKey.deserialize(keystream); |
| 19 | return mKey; |
| 20 | } |
| 21 | |
| 22 | inline QByteArray msg_key_bytes(const QMailMessageKey &key) { |
| 23 | QByteArray msgKey; |
| 24 | QDataStream keystream(&msgKey, QIODevice::WriteOnly); |
| 25 | |
| 26 | key.serialize(keystream); |
| 27 | return msgKey; |
| 28 | } |
| 29 | |
| 30 | inline QMailMessageSortKey to_msg_sort_key(const QByteArray &key) { |
| 31 | QMailMessageSortKey sKey; |
| 32 | QByteArray s = key; |
| 33 | QDataStream skeystream(&s, QIODevice::ReadWrite); |
| 34 | sKey.deserialize(skeystream); |
| 35 | return sKey; |
| 36 | } |
| 37 | |
| 38 | inline QByteArray msg_sort_key_bytes(const QMailMessageSortKey &key) { |
| 39 | QByteArray sortKey; |
| 40 | QDataStream keystream(&sortKey, QIODevice::WriteOnly); |
| 41 | |
| 42 | key.serialize(keystream); |
| 43 | |
| 44 | return sortKey; |
| 45 | } |
| 46 | |
| 47 | inline QMailMessageIdList from_dbus_msglist(const QList<quint64> &ids) { |
| 48 | QMailMessageIdList list; |
| 49 | foreach(const quint64 &id, ids) { |
| 50 | QMailMessageId msgId(id); |
| 51 | list << msgId; |
| 52 | } |
| 53 | return list; |
| 54 | } |
| 55 | |
| 56 | inline QList<quint64> to_dbus_msglist(const QMailMessageIdList &ids) { |
| 57 | QList<quint64> list; |
| 58 | foreach(const QMailMessageId &id, ids) { |
| 59 | list << id.toULongLong(); |
| 60 | } |
| 61 | return list; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | CacheManagerService::CacheManagerService(QObject *parent): ServicePlugin(parent) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | QString CacheManagerService::pluginId() const |
| 70 | { |
| 71 | return QStringLiteral("cache-manager"); |
| 72 | } |
| 73 | |
| 74 | QString CacheManagerService::location() const |
| 75 | { |
| 76 | return QStringLiteral("Dekko::Service"); |
| 77 | } |
| 78 | |
| 79 | QString CacheManagerService::i18n() const |
| 80 | { |
| 81 | return QString(); |
| 82 | } |
| 83 | |
| 84 | void CacheManagerService::start() |
| 85 | { |
| 86 | qDebug() << "[CacheManagerService] >> Ready"; |
| 87 | } |
| 88 | |
| 89 | void CacheManagerService::stop() |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | void CacheManagerService::messageReceived(const QString &key, const QVariant &value) |
| 94 | { |
| 95 | if (key.startsWith("prune:")) { |
| 96 | QString id = key.split(':').last(); |
| 97 | quint64 aId = id.toUInt(); |
| 98 | QMailAccountId account(aId); |
| 99 | if (!account.isValid()) { |
| 100 | qDebug() << "[CacheManagerService] >> Invalid account id."; |
| 101 | return; |
| 102 | } |
| 103 | int p = value.toInt(); |
| 104 | if (p < WEEK && p > ALL) { |
| 105 | qDebug() << "[CacheManagerService] >> Invalid period"; |
| 106 | return; |
| 107 | } |
| 108 | Period period = static_cast<Period>(p); |
| 109 | buildQuery(account, period); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void CacheManagerService::buildQuery(const QMailAccountId &id, const CacheManagerService::Period &period) |
| 114 | { |
| 115 | QMailMessageKey periodKey; |
| 116 | |
| 117 | QDateTime now = QDateTime::currentDateTime(); |
| 118 | |
| 119 | switch (static_cast<Period>(period)) { |
| 120 | case WEEK: |
| 121 | { |
| 122 | qDebug() << "[CacheManagerService] >> Querying for messages older than 1 week"; |
| 123 | QDateTime week = now.addDays(-7); |
| 124 | periodKey = QMailMessageKey::timeStamp(week, QMailDataComparator::LessThan); |
| 125 | break; |
| 126 | } |
| 127 | case FORTNIGHT: |
| 128 | { |
| 129 | qDebug() << "[CacheManagerService] >> Querying for messages older than 2 weeks"; |
| 130 | QDateTime fortnight = now.addDays(-14); |
| 131 | periodKey = QMailMessageKey::timeStamp(fortnight, QMailDataComparator::LessThan); |
| 132 | break; |
| 133 | } |
| 134 | case MONTH: |
| 135 | { |
| 136 | qDebug() << "[CacheManagerService] >> Querying for messages older than 1 month"; |
| 137 | QDateTime month = now.addMonths(-1); |
| 138 | periodKey = QMailMessageKey::timeStamp(month, QMailDataComparator::LessThan); |
| 139 | break; |
| 140 | } |
| 141 | case THREE_MONTH: |
| 142 | { |
| 143 | qDebug() << "[CacheManagerService] >> Querying for messages older than 3 months"; |
| 144 | QDateTime month = now.addMonths(-3); |
| 145 | periodKey = QMailMessageKey::timeStamp(month, QMailDataComparator::LessThan); |
| 146 | break; |
| 147 | } |
| 148 | case SIX_MONTH: |
| 149 | { |
| 150 | qDebug() << "[CacheManagerService] >> Querying for messages older than 6 months"; |
| 151 | QDateTime month = now.addMonths(-6); |
| 152 | periodKey = QMailMessageKey::timeStamp(month, QMailDataComparator::LessThan); |
| 153 | break; |
| 154 | } |
| 155 | case ALL: |
| 156 | return; // do nothing as we don't want to nuqe it all for now |
| 157 | } |
| 158 | |
| 159 | QMailMessageKey query = QMailMessageKey::parentAccountId(id) & periodKey; |
| 160 | m_queries << query; |
| 161 | if (!m_queries.isEmpty()) |
| 162 | QTimer::singleShot(1000, this, &CacheManagerService::sendQueries); |
| 163 | } |
| 164 | |
| 165 | void CacheManagerService::sendQueries() |
| 166 | { |
| 167 | qDebug() << "[CacheManagerService::sendQueries] >> Sending queries"; |
| 168 | foreach(const QMailMessageKey &key, m_queries) { |
| 169 | queryMessages(key); |
| 170 | m_queries.removeAll(key); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void CacheManagerService::removePending() |
| 175 | { |
| 176 | qDebug() << "[CacheManagerService::removePending] >> Removing " << m_toRemove.count() << "messages from cache"; |
| 177 | Client::instance()->pruneCache(m_toRemove); |
| 178 | m_toRemove.clear(); |
| 179 | } |
| 180 | |
| 181 | void CacheManagerService::queryMessages(const QMailMessageKey &key) |
| 182 | { |
| 183 | QMailMessageSortKey sortKey; |
| 184 | QDBusPendingReply<QList<quint64> > reply = Client::instance()->bus()->queryMessages( |
| 185 | msg_key_bytes(key), |
| 186 | msg_sort_key_bytes(sortKey), |
| 187 | 0); |
| 188 | |
| 189 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); |
| 190 | |
| 191 | connect(watcher, &QDBusPendingCallWatcher::finished, this, &CacheManagerService::handleQueryResponse); |
| 192 | } |
| 193 | |
| 194 | void CacheManagerService::handleQueryResponse(QDBusPendingCallWatcher *call) |
| 195 | { |
| 196 | qDebug() << "[CacheManagerService::handleQueryResponse] >> handling response"; |
| 197 | QDBusPendingReply<QList<quint64> > reply = *call; |
| 198 | if (reply.isError()) { |
| 199 | qDebug() << "Reply error for refresh response"; |
| 200 | return; |
| 201 | } |
| 202 | QList<quint64> ids = reply.argumentAt<0>(); |
| 203 | m_toRemove << from_dbus_msglist(ids); |
| 204 | QTimer::singleShot(1000, this, &CacheManagerService::removePending); |
| 205 | call->deleteLater(); |
| 206 | } |
| 207 | |
| 208 | QVariantMap CacheManagerService::documentation() const |
| 209 | { |
| 210 | return QVariantMap(); |
| 211 | } |
| 212 | |
| 213 | QString CacheManagerPlugin::name() const |
| 214 | { |
| 215 | return QStringLiteral("cache-manager"); |
| 216 | } |
| 217 | |
| 218 | QString CacheManagerPlugin::description() const |
| 219 | { |
| 220 | return QStringLiteral("Dekko cache manager"); |
| 221 | } |
| 222 | |
| 223 | PluginInfo *CacheManagerPlugin::create(QObject *parent) const |
| 224 | { |
| 225 | return new CacheManagerService(parent); |
| 226 | } |