summaryrefslogtreecommitdiffstats
path: root/kmail/quotajobs.h
blob: a73e3084ae585928b82a4734add3368f7c51cdcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
 * quotajobs.h
 *
 * Copyright (c) 2006 Till Adam <adam@kde.org>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the Qt library by Trolltech AS, Norway (or with modified versions
 *  of Qt that use the same license as Qt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  Qt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */

#ifndef QUOTAJOBS_H
#define QUOTAJOBS_H

#include <qvariant.h>

#include <kio/job.h>
#include <klocale.h>
#include <qvaluevector.h>

#include <math.h>

#include "globalsettings.h"

namespace KMail {

// One quota entry consisting of a name, the quota root,
// the current value and the maximal value
class QuotaInfo {
  public :
  QuotaInfo() {} // for QValueVector
  QuotaInfo( const QString& _name, const QString& _root, const QVariant& _current, const QVariant& _max )
    : mName( _name ), mRoot( _root ), mCurrent( _current ),mMax( _max )  {}
  bool operator==( const QuotaInfo & other ) const {
    return mName == other.mName && mRoot == other.mRoot && mMax == other.mMax && mCurrent == other.mCurrent;
  }
  bool operator!=( const QuotaInfo & other ) const {
    return !(operator==(other) );
  }
  bool isValid() const { return !mName.isEmpty(); }
  bool isEmpty() const { return mName.isEmpty() || ( mRoot.isEmpty() && !mCurrent.isValid() && !mMax.isValid() ); }

  QString name() const { return mName; }
  void setName( const QString& n ) { mName = n; }
  QString root() const { return mRoot; }
  void setRoot( const QString& r ) { mRoot = r; }
  QVariant max() const { return mMax; }
  void setMax( const QVariant& m ) { mMax = m; }
  QVariant current() const { return mCurrent; }
  void setCurrent( const QVariant& c ) { mCurrent = c; }

  QString toString() const {
    if ( isValid() && !isEmpty() ) {
      readConfig();
      int factor = static_cast<int> ( pow( 1000, mFactor ) );
      return i18n("%1 of %2 %3 used").arg( mCurrent.toInt() / factor )
                                .arg( mMax.toInt() / factor ).arg( mUnits );
    }
    return QString();
  }

 private:
  void readConfig() const {
      if( GlobalSettings::self()->quotaUnit() == GlobalSettings::EnumQuotaUnit::KB )
      {
            mUnits = i18n("KB");
            mFactor = 0;
      }
      else if( GlobalSettings::self()->quotaUnit() == GlobalSettings::EnumQuotaUnit::MB )
           {
                mUnits = i18n("MB");
                mFactor = 1;
           }
      else if( GlobalSettings::self()->quotaUnit() == GlobalSettings::EnumQuotaUnit::GB )
           {
               mUnits = i18n("GB");
               mFactor = 2;
           }
   }

  QString mName;  // e.g. STORAGE
  QString mRoot; /// e.g. INBOX
  QVariant mCurrent;
  QVariant mMax;
  mutable QString mUnits; //used by readConfig (const) privately and is modified
  mutable int mFactor;
};

typedef QValueVector<QuotaInfo> QuotaInfoList;

/**
 * This namespace contains functions that return jobs for quota operations.
 *
 * The current implementation is tied to IMAP.
 * If someone wants to extend this to other protocols, turn the namespace into a class
 * and use virtual methods.
 */
namespace QuotaJobs {

class GetQuotarootJob;
/**
 * Get the quotaroots for a mailbox
 * @param slave Slave object the job should be assigned to
 * @param url URL for which to get the quotaroot
 */
GetQuotarootJob* getQuotaroot( KIO::Slave* slave, const KURL& url );

class GetStorageQuotaJob;
/**
 * Get the storage quota for a mailbox, if there is one.
 * @param slave Slave object the job should be assigned to
 * @param url URL for which to get the storage quota
 */
GetStorageQuotaJob* getStorageQuota( KIO::Slave* slave, const KURL& url );

/// for getQuotaroot()
class GetQuotarootJob : public KIO::SimpleJob
{
  Q_OBJECT
public:
  GetQuotarootJob( const KURL& url, const QByteArray &packedArgs, bool showProgressInfo );

signals:
  /** Emitted when the server returns a (potentially empty) list of
   * quota roots for the specified mailbox.
   * @param roots List of quota roots for the mailbox
   */
  void quotaRootResult( const QStringList& roots );

  /**
   * Emitted when the server returns a list of quota infos for the specified
   * mailbox. This is an aggregate of all quotas for all applicable roots for
   * the mailbox. It happens as a side effect of root listing.
   * @param info List of quota infos for the mailbox
   */
  void quotaInfoReceived( const QuotaInfoList& info );

protected slots:
  void slotInfoMessage( KIO::Job*, const QString& );
};

/// for getStorageQuota()
class GetStorageQuotaJob : public KIO::Job
{
  Q_OBJECT
public:
  GetStorageQuotaJob( KIO::Slave* slave, const KURL& url );

  /**  Returns the storage quota info, if any, can be queried on result(). */
  QuotaInfo storageQuotaInfo() const;

signals:
  /** Emitted to indicate that storage quota information has
   * been received. Is not emitted if there is no such info
   * on the server, so users need to rely on the normal
   * result() signal to be informed when the job is done.
   */
  void storageQuotaResult( const QuotaInfo& info );


protected slots:
  void slotQuotarootResult( const QStringList& roots );
  void slotQuotaInfoReceived( const QuotaInfoList& roots );
private:
  QuotaInfo mStorageQuotaInfo;
};

} // QuotaJobs namespace

} // KMail namespace


#endif /* QUOTAJOBS_H */