KIMAP Library
getquotarootjob.cpp
00001 /* 00002 Copyright (c) 2009 Andras Mantia <amantia@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "getquotarootjob.h" 00021 00022 #include <KDE/KLocale> 00023 #include <KDE/KDebug> 00024 00025 #include "quotajobbase_p.h" 00026 #include "message_p.h" 00027 #include "session_p.h" 00028 #include "rfccodecs.h" 00029 00030 namespace KIMAP 00031 { 00032 class GetQuotaRootJobPrivate : public QuotaJobBasePrivate 00033 { 00034 public: 00035 GetQuotaRootJobPrivate( Session *session, const QString& name ) : QuotaJobBasePrivate(session, name) { } 00036 ~GetQuotaRootJobPrivate() { } 00037 00038 QString mailBox; 00039 QList<QByteArray> rootList; 00040 uint rootIndex; 00041 QMap< QByteArray, QMap<QByteArray, QPair<qint64, qint64> > > quotas; 00042 }; 00043 } 00044 00045 using namespace KIMAP; 00046 00047 GetQuotaRootJob::GetQuotaRootJob( Session *session ) 00048 : QuotaJobBase( *new GetQuotaRootJobPrivate(session, i18n("GetQuotaRoot")) ) 00049 { 00050 } 00051 00052 GetQuotaRootJob::~GetQuotaRootJob() 00053 { 00054 } 00055 00056 void GetQuotaRootJob::doStart() 00057 { 00058 Q_D(GetQuotaRootJob); 00059 d->tags << d->sessionInternal()->sendCommand( "GETQUOTAROOT", '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + '\"'); 00060 } 00061 00062 void GetQuotaRootJob::handleResponse(const Message &response) 00063 { 00064 Q_D(GetQuotaRootJob); 00065 if (handleErrorReplies(response) == NotHandled) { 00066 if ( response.content.size() >= 4 ) { 00067 if (response.content[1].toString() == "QUOTAROOT" ) { 00068 d->rootList.clear(); 00069 int i = 3; 00070 while ( i < response.content.size()) 00071 { 00072 d->rootList.append(response.content[i].toString()); 00073 i++; 00074 } 00075 d->rootIndex = 0; 00076 } else 00077 if (response.content[1].toString() == "QUOTA" ) { 00078 //TODO: check if we should use the roots in order it came in QUOTAROOT response or the root name from the QUOTA response itself 00079 d->quotas[ d->rootList[d->rootIndex] ] = d->readQuota(response.content[3]); 00080 d->rootIndex++; 00081 } 00082 } 00083 } 00084 } 00085 00086 00087 void GetQuotaRootJob::setMailBox(const QString& mailBox) 00088 { 00089 Q_D(GetQuotaRootJob); 00090 00091 d->mailBox = mailBox; 00092 } 00093 00094 QString GetQuotaRootJob::mailBox() const 00095 { 00096 Q_D(const GetQuotaRootJob); 00097 00098 return d->mailBox; 00099 } 00100 00101 QList<QByteArray> GetQuotaRootJob::roots() const 00102 { 00103 Q_D(const GetQuotaRootJob); 00104 00105 return d->rootList; 00106 } 00107 00108 qint64 GetQuotaRootJob::usage(const QByteArray &root, const QByteArray &resource) const 00109 { 00110 Q_D(const GetQuotaRootJob); 00111 00112 QByteArray r = resource.toUpper(); 00113 00114 if (d->quotas.contains(root) && d->quotas[root].contains(r)) { 00115 return d->quotas[root][r].first; 00116 } 00117 00118 return -1; 00119 } 00120 00121 qint64 GetQuotaRootJob::limit(const QByteArray &root, const QByteArray &resource) const 00122 { 00123 Q_D(const GetQuotaRootJob); 00124 00125 QByteArray r = resource.toUpper(); 00126 00127 if (d->quotas.contains(root) && d->quotas[root].contains(r)) { 00128 return d->quotas[root][r].second; 00129 } 00130 00131 return -1; 00132 } 00133 00134 QMap<QByteArray, qint64> GetQuotaRootJob::allUsages(const QByteArray &root) const 00135 { 00136 Q_D(const GetQuotaRootJob); 00137 00138 QMap<QByteArray, qint64> result; 00139 00140 if (d->quotas.contains(root)) { 00141 const QMap< QByteArray, QPair<qint64, qint64> > quota = d->quotas[root]; 00142 QMapIterator<QByteArray, QPair<qint64, qint64> > it( quota ); 00143 while ( it.hasNext() ) { 00144 it.next(); 00145 result[it.key()] = it.value().first; 00146 } 00147 } 00148 00149 return result; 00150 } 00151 00152 QMap<QByteArray, qint64> GetQuotaRootJob::allLimits(const QByteArray &root) const 00153 { 00154 Q_D(const GetQuotaRootJob); 00155 00156 QMap<QByteArray, qint64> result; 00157 00158 if (d->quotas.contains(root)) { 00159 const QMap< QByteArray, QPair<qint64, qint64> > quota = d->quotas[root]; 00160 QMapIterator<QByteArray, QPair<qint64, qint64> > it( quota ); 00161 while ( it.hasNext() ) { 00162 it.next(); 00163 result[it.key()] = it.value().second; 00164 } 00165 } 00166 00167 return result; 00168 } 00169 00170 #include "getquotarootjob.moc"