1/*
2 Copyright (c) 2009 Andras Mantia <amantia@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "getquotarootjob.h"
21
22#include <KDE/KLocalizedString>
23#include <KDE/KDebug>
24
25#include "quotajobbase_p.h"
26#include "message_p.h"
27#include "session_p.h"
28#include "rfccodecs.h"
29
30namespace KIMAP
31{
32 class GetQuotaRootJobPrivate : public QuotaJobBasePrivate
33 {
34 public:
35 GetQuotaRootJobPrivate( Session *session, const QString& name ) : QuotaJobBasePrivate( session, name ) { }
36 ~GetQuotaRootJobPrivate() { }
37
38 QString mailBox;
39 QList<QByteArray> rootList;
40 QMap< QByteArray, QMap<QByteArray, QPair<qint64, qint64> > > quotas;
41 };
42}
43
44using namespace KIMAP;
45
46GetQuotaRootJob::GetQuotaRootJob( Session *session )
47 : QuotaJobBase( *new GetQuotaRootJobPrivate( session, i18n( "GetQuotaRoot" ) ) )
48{
49}
50
51GetQuotaRootJob::~GetQuotaRootJob()
52{
53}
54
55void GetQuotaRootJob::doStart()
56{
57 Q_D( GetQuotaRootJob );
58 d->tags << d->sessionInternal()->sendCommand( "GETQUOTAROOT", '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + '\"' );
59}
60
61void GetQuotaRootJob::handleResponse(const Message &response)
62{
63 Q_D( GetQuotaRootJob );
64 if ( handleErrorReplies( response ) == NotHandled ) {
65 if ( response.content.size() >= 3 ) {
66 if ( response.content[1].toString() == "QUOTAROOT" ) {
67 d->rootList.clear();
68 //some impls don't give the root a name which for us seems as if
69 //there were no message part
70 if ( response.content.size() == 3 ) {
71 d->rootList.append( "" );
72 } else {
73 int i = 3;
74 while ( i < response.content.size() ) {
75 d->rootList.append( response.content[i].toString() );
76 i++;
77 }
78 }
79 } else if ( response.content[1].toString() == "QUOTA" ) {
80 QByteArray rootName;
81 int quotaContentIndex = 3;
82 //some impls don't give the root a name in the response
83 if ( response.content.size() == 3 ) {
84 quotaContentIndex = 2;
85 } else {
86 rootName = response.content[2].toString();
87 }
88
89 const QMap<QByteArray, QPair<qint64, qint64> >& quota = d->readQuota(response.content[quotaContentIndex]);
90 if ( d->quotas.contains( rootName ) ) {
91 d->quotas[ rootName ].unite( quota );
92 } else {
93 d->quotas[ rootName ] = quota;
94 }
95 }
96 }
97 }
98}
99
100void GetQuotaRootJob::setMailBox(const QString& mailBox)
101{
102 Q_D( GetQuotaRootJob );
103 d->mailBox = mailBox;
104}
105
106QString GetQuotaRootJob::mailBox() const
107{
108 Q_D( const GetQuotaRootJob );
109 return d->mailBox;
110}
111
112QList<QByteArray> GetQuotaRootJob::roots() const
113{
114 Q_D( const GetQuotaRootJob );
115 return d->rootList;
116}
117
118qint64 GetQuotaRootJob::usage(const QByteArray &root, const QByteArray &resource) const
119{
120 Q_D( const GetQuotaRootJob );
121 QByteArray r = resource.toUpper();
122
123 if ( d->quotas.contains( root ) && d->quotas[root].contains( r ) ) {
124 return d->quotas[root][r].first;
125 }
126 return -1;
127}
128
129qint64 GetQuotaRootJob::limit(const QByteArray &root, const QByteArray &resource) const
130{
131 Q_D( const GetQuotaRootJob );
132
133 QByteArray r = resource.toUpper();
134
135 if ( d->quotas.contains( root ) && d->quotas[root].contains( r ) ) {
136 return d->quotas[root][r].second;
137 }
138 return -1;
139}
140
141QMap<QByteArray, qint64> GetQuotaRootJob::allUsages(const QByteArray &root) const
142{
143 Q_D( const GetQuotaRootJob );
144
145 QMap<QByteArray, qint64> result;
146
147 if ( d->quotas.contains( root ) ) {
148 const QMap< QByteArray, QPair<qint64, qint64> > quota = d->quotas[root];
149 QMapIterator<QByteArray, QPair<qint64, qint64> > it( quota );
150 while ( it.hasNext() ) {
151 it.next();
152 result[it.key()] = it.value().first;
153 }
154 }
155 return result;
156}
157
158QMap<QByteArray, qint64> GetQuotaRootJob::allLimits(const QByteArray &root) const
159{
160 Q_D( const GetQuotaRootJob );
161
162 QMap<QByteArray, qint64> result;
163
164 if ( d->quotas.contains( root ) ) {
165 const QMap< QByteArray, QPair<qint64, qint64> > quota = d->quotas[root];
166 QMapIterator<QByteArray, QPair<qint64, qint64> > it( quota );
167 while ( it.hasNext() ) {
168 it.next();
169 result[it.key()] = it.value().second;
170 }
171 }
172 return result;
173}
174