File indexing completed on 2024-05-19 05:17:41

0001 /*
0002     Copyright (c) 2009 Andras Mantia <amantia@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #include "getquotarootjob.h"
0021 
0022 #include "quotajobbase_p.h"
0023 #include "message_p.h"
0024 #include "session_p.h"
0025 #include "rfccodecs.h"
0026 
0027 namespace KIMAP2
0028 {
0029 class GetQuotaRootJobPrivate : public QuotaJobBasePrivate
0030 {
0031 public:
0032     GetQuotaRootJobPrivate(Session *session, const QString &name) : QuotaJobBasePrivate(session, name) { }
0033     ~GetQuotaRootJobPrivate() { }
0034 
0035     QString mailBox;
0036     QList<QByteArray> rootList;
0037     QMap< QByteArray, QMap<QByteArray, QPair<qint64, qint64> > > quotas;
0038 };
0039 }
0040 
0041 using namespace KIMAP2;
0042 
0043 GetQuotaRootJob::GetQuotaRootJob(Session *session)
0044     : QuotaJobBase(*new GetQuotaRootJobPrivate(session, "GetQuotaRoot"))
0045 {
0046 }
0047 
0048 GetQuotaRootJob::~GetQuotaRootJob()
0049 {
0050 }
0051 
0052 void GetQuotaRootJob::doStart()
0053 {
0054     Q_D(GetQuotaRootJob);
0055     d->sendCommand("GETQUOTAROOT", '\"' + KIMAP2::encodeImapFolderName(d->mailBox.toUtf8()) + '\"');
0056 }
0057 
0058 void GetQuotaRootJob::handleResponse(const Message &response)
0059 {
0060     Q_D(GetQuotaRootJob);
0061     if (handleErrorReplies(response) == NotHandled) {
0062         if (response.content.size() >= 3) {
0063             if (response.content[1].toString() == "QUOTAROOT") {
0064                 d->rootList.clear();
0065                 //some impls don't give the root a name which for us seems as if
0066                 //there were no message part
0067                 if (response.content.size() == 3) {
0068                     d->rootList.append("");
0069                 } else {
0070                     int i = 3;
0071                     while (i < response.content.size()) {
0072                         d->rootList.append(response.content[i].toString());
0073                         i++;
0074                     }
0075                 }
0076             } else if (response.content[1].toString() == "QUOTA") {
0077                 QByteArray rootName;
0078                 int  quotaContentIndex = 3;
0079                 //some impls don't give the root a name in the response
0080                 if (response.content.size() == 3) {
0081                     quotaContentIndex = 2;
0082                 } else {
0083                     rootName = response.content[2].toString();
0084                 }
0085 
0086                 const QMap<QByteArray, QPair<qint64, qint64> > &quota = d->readQuota(response.content[quotaContentIndex]);
0087                 if (d->quotas.contains(rootName)) {
0088                     d->quotas[ rootName ].unite(quota);
0089                 } else {
0090                     d->quotas[ rootName ] = quota;
0091                 }
0092             }
0093         }
0094     }
0095 }
0096 
0097 void GetQuotaRootJob::setMailBox(const QString &mailBox)
0098 {
0099     Q_D(GetQuotaRootJob);
0100     d->mailBox = mailBox;
0101 }
0102 
0103 QString GetQuotaRootJob::mailBox() const
0104 {
0105     Q_D(const GetQuotaRootJob);
0106     return d->mailBox;
0107 }
0108 
0109 QList<QByteArray> GetQuotaRootJob::roots() const
0110 {
0111     Q_D(const GetQuotaRootJob);
0112     return d->rootList;
0113 }
0114 
0115 qint64 GetQuotaRootJob::usage(const QByteArray &root, const QByteArray &resource) const
0116 {
0117     Q_D(const GetQuotaRootJob);
0118     QByteArray r = resource.toUpper();
0119 
0120     if (d->quotas.contains(root) && d->quotas[root].contains(r)) {
0121         return d->quotas[root][r].first;
0122     }
0123     return -1;
0124 }
0125 
0126 qint64 GetQuotaRootJob::limit(const QByteArray &root, const QByteArray &resource) const
0127 {
0128     Q_D(const GetQuotaRootJob);
0129 
0130     QByteArray r = resource.toUpper();
0131 
0132     if (d->quotas.contains(root) && d->quotas[root].contains(r)) {
0133         return d->quotas[root][r].second;
0134     }
0135     return -1;
0136 }
0137 
0138 QMap<QByteArray, qint64> GetQuotaRootJob::allUsages(const QByteArray &root) const
0139 {
0140     Q_D(const GetQuotaRootJob);
0141 
0142     QMap<QByteArray, qint64> result;
0143 
0144     if (d->quotas.contains(root)) {
0145         const QMap< QByteArray, QPair<qint64, qint64> > quota = d->quotas[root];
0146         QMapIterator<QByteArray, QPair<qint64, qint64> > it(quota);
0147         while (it.hasNext()) {
0148             it.next();
0149             result[it.key()] = it.value().first;
0150         }
0151     }
0152     return result;
0153 }
0154 
0155 QMap<QByteArray, qint64> GetQuotaRootJob::allLimits(const QByteArray &root) const
0156 {
0157     Q_D(const GetQuotaRootJob);
0158 
0159     QMap<QByteArray, qint64> result;
0160 
0161     if (d->quotas.contains(root)) {
0162         const QMap< QByteArray, QPair<qint64, qint64> > quota = d->quotas[root];
0163         QMapIterator<QByteArray, QPair<qint64, qint64> > it(quota);
0164         while (it.hasNext()) {
0165             it.next();
0166             result[it.key()] = it.value().second;
0167         }
0168     }
0169     return result;
0170 }