krop's commit fixes my problem in a better way, reverting
[kdepim.git] / kmail / quotajobs.h
blob5de2e08103d546e5d3211bddf930a46db7a82900
1 /**
2 * quotajobs.h
4 * Copyright (c) 2006 Till Adam <adam@kde.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give
21 * permission to link the code of this program with any edition of
22 * the Qt library by Trolltech AS, Norway (or with modified versions
23 * of Qt that use the same license as Qt), and distribute linked
24 * combinations including the two. You must obey the GNU General
25 * Public License in all respects for all of the code used other than
26 * Qt. If you modify this file, you may extend this exception to
27 * your version of the file, but you are not obligated to do so. If
28 * you do not wish to do so, delete this exception statement from
29 * your version.
32 #ifndef QUOTAJOBS_H
33 #define QUOTAJOBS_H
35 #include <qvariant.h>
37 #include <kio/job.h>
38 #include <klocale.h>
39 #include <qvector.h>
41 #include <math.h>
43 #include "globalsettings.h"
45 #undef max
47 namespace KIO { class Slave; }
49 namespace KMail {
51 // One quota entry consisting of a name, the quota root,
52 // the current value and the maximal value
53 class QuotaInfo {
54 public :
55 QuotaInfo() {} // for mStorageQuotaInfo
56 QuotaInfo( const QString& _name, const QString& _root, const QVariant& _current, const QVariant& _max )
57 : mName( _name ), mRoot( _root ), mCurrent( _current ),mMax( _max ) {}
58 bool operator==( const QuotaInfo & other ) const {
59 return mName == other.mName && mRoot == other.mRoot && mMax == other.mMax && mCurrent == other.mCurrent;
61 bool operator!=( const QuotaInfo & other ) const {
62 return !(operator==(other) );
64 bool isValid() const { return !mName.isEmpty(); }
65 bool isEmpty() const { return mName.isEmpty() || ( mRoot.isEmpty() && !mCurrent.isValid() && !mMax.isValid() ); }
67 QString name() const { return mName; }
68 void setName( const QString& n ) { mName = n; }
69 QString root() const { return mRoot; }
70 void setRoot( const QString& r ) { mRoot = r; }
71 QVariant max() const { return mMax; }
72 void setMax( const QVariant& m ) { mMax = m; }
73 QVariant current() const { return mCurrent; }
74 void setCurrent( const QVariant& c ) { mCurrent = c; }
76 QString toString() const {
77 if ( isValid() && !isEmpty() ) {
78 readConfig();
79 int factor = static_cast<int> ( pow( 1000.0, mFactor ) );
80 return i18n("%1 of %2 %3 used", mCurrent.toInt() / factor,
81 mMax.toInt() / factor, mUnits );
83 return QString();
86 private:
87 void readConfig() const {
88 if( GlobalSettings::self()->quotaUnit() == GlobalSettings::EnumQuotaUnit::KB )
90 mUnits = i18n("KB");
91 mFactor = 0;
93 else if( GlobalSettings::self()->quotaUnit() == GlobalSettings::EnumQuotaUnit::MB )
95 mUnits = i18n("MB");
96 mFactor = 1;
98 else if( GlobalSettings::self()->quotaUnit() == GlobalSettings::EnumQuotaUnit::GB )
100 mUnits = i18n("GB");
101 mFactor = 2;
105 QString mName; // e.g. STORAGE
106 QString mRoot; /// e.g. INBOX
107 QVariant mCurrent;
108 QVariant mMax;
109 mutable QString mUnits; //used by readConfig (const) privately and is modified
110 mutable int mFactor;
113 typedef QVector<QuotaInfo> QuotaInfoList;
116 * This namespace contains functions that return jobs for quota operations.
118 * The current implementation is tied to IMAP.
119 * If someone wants to extend this to other protocols, turn the namespace into a class
120 * and use virtual methods.
122 namespace QuotaJobs {
124 class GetQuotarootJob;
126 * Get the quotaroots for a mailbox
127 * @param slave Slave object the job should be assigned to
128 * @param url URL for which to get the quotaroot
130 GetQuotarootJob* getQuotaroot( KIO::Slave* slave, const KUrl& url );
132 class GetStorageQuotaJob;
134 * Get the storage quota for a mailbox, if there is one.
135 * @param slave Slave object the job should be assigned to
136 * @param url URL for which to get the storage quota
138 GetStorageQuotaJob* getStorageQuota( KIO::Slave* slave, const KUrl& url );
140 /// for getQuotaroot()
141 class GetQuotarootJob : public KIO::SpecialJob
143 Q_OBJECT
144 public:
145 GetQuotarootJob( const KUrl& url, const QByteArray &packedArgs);
147 signals:
148 /** Emitted when the server returns a (potentially empty) list of
149 * quota roots for the specified mailbox.
150 * @param roots List of quota roots for the mailbox
152 void quotaRootResult( const QStringList& roots );
155 * Emitted when the server returns a list of quota information for the specified
156 * mailbox. This is an aggregate of all quotas for all applicable roots for
157 * the mailbox. It happens as a side effect of root listing.
158 * @param info List of quota information for the mailbox
160 void quotaInfoReceived( const QuotaInfoList& info );
162 protected slots:
163 void slotInfoMessage( KJob*, const QString&, const QString& );
166 /// for getStorageQuota()
167 class GetStorageQuotaJob : public KIO::Job
169 Q_OBJECT
170 public:
171 GetStorageQuotaJob( KIO::Slave* slave, const KUrl& url );
173 /** Returns the storage quota info, if any, can be queried on result(). */
174 QuotaInfo storageQuotaInfo() const;
176 signals:
177 /** Emitted to indicate that storage quota information has
178 * been received. Is not emitted if there is no such info
179 * on the server, so users need to rely on the normal
180 * result() signal to be informed when the job is done.
182 void storageQuotaResult( const QuotaInfo& info );
185 protected slots:
186 void slotQuotarootResult( const QStringList& roots );
187 void slotQuotaInfoReceived( const QuotaInfoList& roots );
188 private:
189 QuotaInfo mStorageQuotaInfo;
192 } // QuotaJobs namespace
194 } // KMail namespace
197 #endif /* QUOTAJOBS_H */