krop's commit fixes my problem in a better way, reverting
[kdepim.git] / kmail / folderdialogquotatab.cpp
blobe343983ec1616a265857c07a99c0b6b0ccc52898
1 // -*- mode: C++; c-file-style: "gnu" -*-
2 /**
3 * folderdialogquotatab.cpp
5 * Copyright (c) 2006 Till Adam <adam@kde.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give
22 * permission to link the code of this program with any edition of
23 * the Qt library by Trolltech AS, Norway (or with modified versions
24 * of Qt that use the same license as Qt), and distribute linked
25 * combinations including the two. You must obey the GNU General
26 * Public License in all respects for all of the code used other than
27 * Qt. If you modify this file, you may extend this exception to
28 * your version of the file, but you are not obligated to do so. If
29 * you do not wish to do so, delete this exception statement from
30 * your version.
33 #include "folderdialogquotatab.h"
34 #include "folderdialogquotatab_p.h"
36 #include "kmfolder.h"
37 #include "kmfoldertype.h"
38 #include "kmfolderimap.h"
39 #include "kmfoldercachedimap.h"
40 #include "kmacctcachedimap.h"
41 #include "imapaccountbase.h"
43 #include <qstackedwidget.h>
44 #include <qlayout.h>
45 #include <qlabel.h>
46 #include <qprogressbar.h>
47 #include <qwhatsthis.h>
49 #include <assert.h>
51 using namespace KMail;
53 KMail::FolderDialogQuotaTab::FolderDialogQuotaTab( KMFolderDialog* dlg, QWidget* parent, const char* name )
54 : FolderDialogTab( dlg, parent, name ),
55 mImapAccount( 0 )
57 QVBoxLayout* topLayout = new QVBoxLayout( this );
58 // We need a widget stack to show either a label ("no qutoa support", "please wait"...)
59 // or quota info
60 mStack = new QStackedWidget( this );
61 topLayout->addWidget( mStack );
63 mLabel = new QLabel( mStack );
64 mLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
65 mLabel->setWordWrap( true );
66 mStack->addWidget( mLabel );
68 mQuotaWidget = new KMail::QuotaWidget( mStack );
69 mStack->addWidget( mQuotaWidget );
73 void KMail::FolderDialogQuotaTab::initializeWithValuesFromFolder( KMFolder* folder )
75 // This can be simplified once KMFolderImap and KMFolderCachedImap have a common base class
76 mFolderType = folder->folderType();
77 if ( mFolderType == KMFolderTypeImap ) {
78 KMFolderImap* folderImap = static_cast<KMFolderImap*>( folder->storage() );
79 mImapAccount = folderImap->account();
80 mImapPath = folderImap->imapPath();
82 else if ( mFolderType == KMFolderTypeCachedImap ) {
83 KMFolderCachedImap* folderImap = static_cast<KMFolderCachedImap*>( folder->storage() );
84 mImapAccount = folderImap->account();
85 mQuotaInfo = folderImap->quotaInfo();
87 else
88 assert( 0 ); // see KMFolderDialog constructor
91 void KMail::FolderDialogQuotaTab::load()
93 if ( mDlg->folder() ) {
94 // existing folder
95 initializeWithValuesFromFolder( mDlg->folder() );
96 } else if ( mDlg->parentFolder() ) {
97 // new folder
98 initializeWithValuesFromFolder( mDlg->parentFolder() );
101 if ( mFolderType == KMFolderTypeCachedImap ) {
102 showQuotaWidget();
103 return;
106 assert( mFolderType == KMFolderTypeImap );
108 // Loading, for online IMAP, consists of two steps:
109 // 1) connect
110 // 2) get quota info
112 // First ensure we are connected
113 mStack->setCurrentWidget( mLabel );
114 if ( !mImapAccount ) { // hmmm?
115 mLabel->setText( i18n( "Error: no IMAP account defined for this folder" ) );
116 return;
118 KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
119 if ( folder && folder->storage() == mImapAccount->rootFolder() )
120 return; // nothing to be done for the (virtual) account folder
121 mLabel->setText( i18n( "Connecting to server %1, please wait...", mImapAccount->host() ) );
122 ImapAccountBase::ConnectionState state = mImapAccount->makeConnection();
123 if ( state == ImapAccountBase::Error ) { // Cancelled by user, or slave can't start
124 slotConnectionResult( -1, QString() );
125 } else if ( state == ImapAccountBase::Connecting ) {
126 connect( mImapAccount, SIGNAL( connectionResult(int, const QString&) ),
127 this, SLOT( slotConnectionResult(int, const QString&) ) );
128 } else { // Connected
129 slotConnectionResult( 0, QString() );
134 void KMail::FolderDialogQuotaTab::slotConnectionResult( int errorCode, const QString& errorMsg )
136 disconnect( mImapAccount, SIGNAL( connectionResult(int, const QString&) ),
137 this, SLOT( slotConnectionResult(int, const QString&) ) );
138 if ( errorCode ) {
139 if ( errorCode == -1 ) // unspecified error
140 mLabel->setText( i18n( "Error connecting to server %1", mImapAccount->host() ) );
141 else
142 // Connection error (error message box already shown by the account)
143 mLabel->setText( KIO::buildErrorString( errorCode, errorMsg ) );
144 return;
146 connect( mImapAccount, SIGNAL( receivedStorageQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& ) ),
147 this, SLOT( slotReceivedQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& ) ) );
148 KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
149 mImapAccount->getStorageQuotaInfo( folder, mImapPath );
152 void KMail::FolderDialogQuotaTab::slotReceivedQuotaInfo( KMFolder* folder,
153 KIO::Job* job,
154 const KMail::QuotaInfo& info )
156 if ( folder == mDlg->folder() ? mDlg->folder() : mDlg->parentFolder() ) {
157 //KMFolderImap* folderImap = static_cast<KMFolderImap*>( folder->storage() );
159 disconnect( mImapAccount, SIGNAL(receivedStorageQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& )),
160 this, SLOT(slotReceivedQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& )) );
162 if ( job && job->error() ) {
163 if ( job->error() == KIO::ERR_UNSUPPORTED_ACTION )
164 mLabel->setText( i18n( "This account does not have support for quota information." ) );
165 else
166 mLabel->setText( i18n( "Error retrieving quota information from server\n%1", job->errorString() ) );
167 } else {
168 mQuotaInfo = info;
170 showQuotaWidget();
174 void KMail::FolderDialogQuotaTab::showQuotaWidget()
176 if ( !mQuotaInfo.isValid() ) {
177 if ( !mImapAccount->hasQuotaSupport() ) {
178 mLabel->setText( i18n( "This account does not have support for quota information." ) );
180 } else {
181 if ( !mQuotaInfo.isEmpty() ) {
182 mStack->setCurrentWidget( mQuotaWidget );
183 mQuotaWidget->setQuotaInfo( mQuotaInfo );
184 } else {
185 mLabel->setText( i18n( "No quota is set for this folder." ) );
191 KMail::FolderDialogTab::AcceptStatus KMail::FolderDialogQuotaTab::accept()
193 if ( mFolderType == KMFolderTypeCachedImap || mFolderType == KMFolderTypeImap )
194 return Accepted;
195 else
196 assert(0);
197 return Canceled;
200 bool KMail::FolderDialogQuotaTab::save()
202 // nothing to do, we are read-only
203 return true;
206 bool KMail::FolderDialogQuotaTab::supports( KMFolder* refFolder )
208 ImapAccountBase* imapAccount = 0;
209 if ( refFolder->folderType() == KMFolderTypeImap )
210 imapAccount = static_cast<KMFolderImap*>( refFolder->storage() )->account();
211 else if ( refFolder->folderType() == KMFolderTypeCachedImap )
212 imapAccount = static_cast<KMFolderCachedImap*>( refFolder->storage() )->account();
213 return imapAccount && imapAccount->hasQuotaSupport(); // support for Quotas (or not tried connecting yet)
216 #include "folderdialogquotatab.moc"