2 Copyright (C) 2009 Kevin Ottens <ervin@kde.org>
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.
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.
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
20 #include "imapquotaattribute.h"
22 #include <QtCore/QByteArray>
23 #include <QtCore/QString>
24 #include <QtCore/QStringList>
28 using namespace Akonadi
;
30 ImapQuotaAttribute::ImapQuotaAttribute()
34 Akonadi::ImapQuotaAttribute::ImapQuotaAttribute( const QList
<QByteArray
> &roots
,
35 const QList
< QMap
<QByteArray
, qint64
> > &limits
,
36 const QList
< QMap
<QByteArray
, qint64
> > &usages
)
37 : mRoots( roots
), mLimits( limits
), mUsages( usages
)
39 Q_ASSERT( roots
.size()==limits
.size() );
40 Q_ASSERT( roots
.size()==usages
.size() );
43 void Akonadi::ImapQuotaAttribute::setQuotas( const QList
<QByteArray
> &roots
,
44 const QList
< QMap
<QByteArray
, qint64
> > &limits
,
45 const QList
< QMap
<QByteArray
, qint64
> > &usages
)
47 Q_ASSERT( roots
.size()==limits
.size() );
48 Q_ASSERT( roots
.size()==usages
.size() );
55 QList
<QByteArray
> Akonadi::ImapQuotaAttribute::roots() const
60 QList
< QMap
<QByteArray
, qint64
> > Akonadi::ImapQuotaAttribute::limits() const
65 QList
< QMap
<QByteArray
, qint64
> > Akonadi::ImapQuotaAttribute::usages() const
70 QByteArray
ImapQuotaAttribute::type() const
75 Akonadi::Attribute
* ImapQuotaAttribute::clone() const
77 return new ImapQuotaAttribute( mRoots
, mLimits
, mUsages
);
80 QByteArray
ImapQuotaAttribute::serialized() const
82 typedef QMap
<QByteArray
, qint64
> QuotaMap
;
83 QByteArray result
= "";
85 // First the roots list
86 foreach ( const QByteArray
&root
, mRoots
) {
91 result
+= " %%% "; // Members separator
93 // Then the limit maps list
94 for ( int i
=0; i
<mRoots
.size(); ++i
) {
95 const QMap
<QByteArray
, qint64
> limits
= mLimits
[i
];
96 foreach ( const QByteArray
&key
, limits
.keys() ) {
99 result
+= QByteArray::number( limits
[key
] );
100 result
+= " % "; // We use this separator as '%' is not allowed in keys or values
103 result
+= " %% "; // Maps separator
107 result
+= " %%% "; // Members separator
109 // Then the usage maps list
110 for ( int i
=0; i
<mRoots
.size(); ++i
) {
111 const QMap
<QByteArray
, qint64
> usages
= mUsages
[i
];
112 foreach ( const QByteArray
&key
, usages
.keys() ) {
115 result
+= QByteArray::number( usages
[key
] );
116 result
+= " % "; // We use this separator as '%' is not allowed in keys or values
119 result
+= " %% "; // Maps separator
126 void ImapQuotaAttribute::deserialize( const QByteArray
&data
)
132 // Nothing was saved.
133 if ( data
.trimmed().isEmpty() ) {
137 QString string
= QString::fromUtf8(data
); // QByteArray has no proper split, so we're forced to convert to QString...
139 QStringList members
= string
.split( "%%%" );
141 // We expect exactly three members (roots, limits and usages), otherwise something is funky
142 if ( members
.size() != 3 ) {
143 kWarning() << "We didn't find exactly three members in this quota serialization";
147 QStringList roots
= members
[0].trimmed().simplified().split( ' ' );
148 foreach ( const QString
&root
, roots
) {
149 mRoots
<< root
.toUtf8();
152 QStringList allLimits
= members
[1].trimmed().split( "%%" );
154 foreach ( const QString
&limits
, allLimits
) {
155 QMap
<QByteArray
, qint64
> limitsMap
;
156 QList
<QByteArray
> lines
= limits
.toUtf8().split( '%' );
158 foreach ( const QByteArray
&line
, lines
) {
159 QByteArray trimmed
= line
.trimmed();
160 int wsIndex
= trimmed
.indexOf( ' ' );
161 const QByteArray key
= trimmed
.mid( 0, wsIndex
).trimmed();
162 const QByteArray value
= trimmed
.mid( wsIndex
+1, line
.length()-wsIndex
).trimmed();
163 limitsMap
[key
] = value
.toLongLong();
166 mLimits
<< limitsMap
;
169 QStringList allUsages
= members
[2].trimmed().split( "%%" );
171 foreach ( const QString
&usages
, allUsages
) {
172 QMap
<QByteArray
, qint64
> usagesMap
;
173 QList
<QByteArray
> lines
= usages
.toUtf8().split( '%' );
175 foreach ( const QByteArray
&line
, lines
) {
176 QByteArray trimmed
= line
.trimmed();
177 int wsIndex
= trimmed
.indexOf( ' ' );
178 const QByteArray key
= trimmed
.mid( 0, wsIndex
).trimmed();
179 const QByteArray value
= trimmed
.mid( wsIndex
+1, line
.length()-wsIndex
).trimmed();
180 usagesMap
[key
] = value
.toLongLong();
183 mUsages
<< usagesMap
;