kmail

folderdiaquotatab.cpp
1 // -*- mode: C++; c-file-style: "gnu" -*-
33 #include "folderdiaquotatab.h"
34 #include "kmfolder.h"
35 #include "kmfoldertype.h"
36 #include "kmfolderimap.h"
37 #include "kmfoldercachedimap.h"
38 #include "kmacctcachedimap.h"
39 #include "imapaccountbase.h"
40 
41 #include <tqwidgetstack.h>
42 #include <tqlayout.h>
43 #include <tqlabel.h>
44 #include <tqprogressbar.h>
45 #include <tqwhatsthis.h>
46 
47 #include "folderdiaquotatab_p.h"
48 
49 #include <assert.h>
50 
51 using namespace KMail;
52 
53 KMail::FolderDiaQuotaTab::FolderDiaQuotaTab( KMFolderDialog* dlg, TQWidget* parent, const char* name )
54  : FolderDiaTab( parent, name ),
55  mImapAccount( 0 ),
56  mDlg( dlg )
57 {
58  TQVBoxLayout* topLayout = new TQVBoxLayout( this );
59  // We need a widget stack to show either a label ("no qutoa support", "please wait"...)
60  // or quota info
61  mStack = new TQWidgetStack( this );
62  topLayout->addWidget( mStack );
63 
64  mLabel = new TQLabel( mStack );
65  mLabel->setAlignment( AlignHCenter | AlignVCenter | WordBreak );
66  mStack->addWidget( mLabel );
67 
68  mQuotaWidget = new KMail::QuotaWidget( mStack );
69 }
70 
71 
72 void KMail::FolderDiaQuotaTab::initializeWithValuesFromFolder( KMFolder* folder )
73 {
74  // This can be simplified once KMFolderImap and KMFolderCachedImap have a common base class
75  mFolderType = folder->folderType();
76  if ( mFolderType == KMFolderTypeImap ) {
77  KMFolderImap* folderImap = static_cast<KMFolderImap*>( folder->storage() );
78  mImapAccount = folderImap->account();
79  mImapPath = folderImap->imapPath();
80  }
81  else if ( mFolderType == KMFolderTypeCachedImap ) {
82  KMFolderCachedImap* folderImap = static_cast<KMFolderCachedImap*>( folder->storage() );
83  mImapAccount = folderImap->account();
84  mQuotaInfo = folderImap->quotaInfo();
85  }
86  else
87  assert( 0 ); // see KMFolderDialog constructor
88 }
89 
90 void KMail::FolderDiaQuotaTab::load()
91 {
92  if ( mDlg->folder() ) {
93  // existing folder
94  initializeWithValuesFromFolder( mDlg->folder() );
95  } else if ( mDlg->parentFolder() ) {
96  // new folder
97  initializeWithValuesFromFolder( mDlg->parentFolder() );
98  }
99 
100  if ( mFolderType == KMFolderTypeCachedImap ) {
101  showQuotaWidget();
102  return;
103  }
104 
105  assert( mFolderType == KMFolderTypeImap );
106 
107  // Loading, for online IMAP, consists of two steps:
108  // 1) connect
109  // 2) get quota info
110 
111  // First ensure we are connected
112  mStack->raiseWidget( mLabel );
113  if ( !mImapAccount ) { // hmmm?
114  mLabel->setText( i18n( "Error: no IMAP account defined for this folder" ) );
115  return;
116  }
117  KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
118  if ( folder && folder->storage() == mImapAccount->rootFolder() )
119  return; // nothing to be done for the (virtual) account folder
120  mLabel->setText( i18n( "Connecting to server %1, please wait..." ).arg( mImapAccount->host() ) );
121  ImapAccountBase::ConnectionState state = mImapAccount->makeConnection();
122  if ( state == ImapAccountBase::Error ) { // Cancelled by user, or slave can't start
123  slotConnectionResult( -1, TQString() );
124  } else if ( state == ImapAccountBase::Connecting ) {
125  connect( mImapAccount, TQT_SIGNAL( connectionResult(int, const TQString&) ),
126  this, TQT_SLOT( slotConnectionResult(int, const TQString&) ) );
127  } else { // Connected
128  slotConnectionResult( 0, TQString() );
129  }
130 
131 }
132 
133 void KMail::FolderDiaQuotaTab::slotConnectionResult( int errorCode, const TQString& errorMsg )
134 {
135  disconnect( mImapAccount, TQT_SIGNAL( connectionResult(int, const TQString&) ),
136  this, TQT_SLOT( slotConnectionResult(int, const TQString&) ) );
137  if ( errorCode ) {
138  if ( errorCode == -1 ) // unspecified error
139  mLabel->setText( i18n( "Error connecting to server %1" ).arg( mImapAccount->host() ) );
140  else
141  // Connection error (error message box already shown by the account)
142  mLabel->setText( KIO::buildErrorString( errorCode, errorMsg ) );
143  return;
144  }
145  connect( mImapAccount, TQT_SIGNAL( receivedStorageQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& ) ),
146  this, TQT_SLOT( slotReceivedQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& ) ) );
147  KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
148  mImapAccount->getStorageQuotaInfo( folder, mImapPath );
149 }
150 
151 void KMail::FolderDiaQuotaTab::slotReceivedQuotaInfo( KMFolder* folder,
152  KIO::Job* job,
153  const KMail::QuotaInfo& info )
154 {
155  if ( folder == mDlg->folder() ? mDlg->folder() : mDlg->parentFolder() ) {
156  //KMFolderImap* folderImap = static_cast<KMFolderImap*>( folder->storage() );
157 
158  disconnect( mImapAccount, TQT_SIGNAL(receivedStorageQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& )),
159  this, TQT_SLOT(slotReceivedQuotaInfo( KMFolder*, KIO::Job*, const KMail::QuotaInfo& )) );
160 
161  if ( job && job->error() ) {
162  if ( job->error() == KIO::ERR_UNSUPPORTED_ACTION )
163  mLabel->setText( i18n( "This account does not have support for quota information." ) );
164  else
165  mLabel->setText( i18n( "Error retrieving quota information from server\n%1" ).arg( job->errorString() ) );
166  } else {
167  mQuotaInfo = info;
168  }
169  showQuotaWidget();
170  }
171 }
172 
173 void KMail::FolderDiaQuotaTab::showQuotaWidget()
174 {
175  if ( !mQuotaInfo.isValid() ) {
176  if ( !mImapAccount->hasQuotaSupport() ) {
177  mLabel->setText( i18n( "This account does not have support for quota information." ) );
178  }
179  } else {
180  if ( !mQuotaInfo.isEmpty() ) {
181  mStack->raiseWidget( mQuotaWidget );
182  mQuotaWidget->setQuotaInfo( mQuotaInfo );
183  } else {
184  mLabel->setText( i18n( "No quota is set for this folder." ) );
185  }
186  }
187 }
188 
189 
190 KMail::FolderDiaTab::AccepStatus KMail::FolderDiaQuotaTab::accept()
191 {
192  if ( mFolderType == KMFolderTypeCachedImap || mFolderType == KMFolderTypeImap )
193  return Accepted;
194  else
195  assert(0);
196  return Accepted; // our code sanity checker doesn't know there is no coming back from assert(0)
197 }
198 
200 {
201  // nothing to do, we are read-only
202  return true;
203 }
204 
205 bool KMail::FolderDiaQuotaTab::supports( KMFolder* refFolder )
206 {
207  ImapAccountBase* imapAccount = 0;
208  if ( refFolder->folderType() == KMFolderTypeImap )
209  imapAccount = static_cast<KMFolderImap*>( refFolder->storage() )->account();
210  else if ( refFolder->folderType() == KMFolderTypeCachedImap )
211  imapAccount = static_cast<KMFolderCachedImap*>( refFolder->storage() )->account();
212  return imapAccount && imapAccount->hasQuotaSupport(); // support for Quotas (or not tried connecting yet)
213 }
214 
215 #include "folderdiaquotatab.moc"