typo found by Andrey Cherepanov
[kdepim.git] / akonadi / resources / shared / imapquotaattribute.cpp
blob10571e498e09ad1cbdf14c75f941c5572a2c9f7d
1 /*
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
17 02110-1301, USA.
20 #include "imapquotaattribute.h"
22 #include <QtCore/QByteArray>
23 #include <QtCore/QString>
24 #include <QtCore/QStringList>
26 #include <KDE/KDebug>
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() );
50 mRoots = roots;
51 mLimits = limits;
52 mUsages = usages;
55 QList<QByteArray> Akonadi::ImapQuotaAttribute::roots() const
57 return mRoots;
60 QList< QMap<QByteArray, qint64> > Akonadi::ImapQuotaAttribute::limits() const
62 return mLimits;
65 QList< QMap<QByteArray, qint64> > Akonadi::ImapQuotaAttribute::usages() const
67 return mUsages;
70 QByteArray ImapQuotaAttribute::type() const
72 return "imapquota";
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 ) {
87 result+=root+' ';
89 result.chop( 1 );
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() ) {
97 result+= key;
98 result+= ' ';
99 result+= QByteArray::number( limits[key] );
100 result+= " % "; // We use this separator as '%' is not allowed in keys or values
102 result.chop( 3 );
103 result+= " %% "; // Maps separator
105 result.chop( 4 );
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() ) {
113 result+= key;
114 result+= ' ';
115 result+= QByteArray::number( usages[key] );
116 result+= " % "; // We use this separator as '%' is not allowed in keys or values
118 result.chop( 3 );
119 result+= " %% "; // Maps separator
121 result.chop( 4 );
123 return result;
126 void ImapQuotaAttribute::deserialize( const QByteArray &data )
128 mRoots.clear();
129 mLimits.clear();
130 mUsages.clear();
132 // Nothing was saved.
133 if ( data.trimmed().isEmpty() ) {
134 return;
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";
144 return;
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;