/**
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the TQt library by Trolltech AS, Norway (or with modified versions
 *  of TQt that use the same license as TQt), 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
 *  TQt.  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 <tqvariant.h>

#include <tdeio/job.h>
#include <tdelocale.h>
#include <tqvaluevector.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 TQValueVector
  QuotaInfo( const TQString& _name, const TQString& _root, const TQVariant& _current, const TQVariant& _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() ); }

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

  TQString 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 TQString();
  }

 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;
           }
   }

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

typedef TQValueVector<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( TDEIO::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( TDEIO::Slave* slave, const KURL& url );

/// for getQuotaroot()
class GetQuotarootJob : public TDEIO::SimpleJob
{
  Q_OBJECT
  
public:
  GetQuotarootJob( const KURL& url, const TQByteArray &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 TQStringList& 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( TDEIO::Job*, const TQString& );
};

/// for getStorageQuota()
class GetStorageQuotaJob : public TDEIO::Job
{
  Q_OBJECT
  
public:
  GetStorageQuotaJob( TDEIO::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 TQStringList& roots );
  void slotQuotaInfoReceived( const QuotaInfoList& roots );
private:
  QuotaInfo mStorageQuotaInfo;
};

} // QuotaJobs namespace

} // KMail namespace


#endif /* QUOTAJOBS_H */