kmail

subscriptiondialog.cpp

00001 /*  -*- c++ -*-
00002     subscriptiondialog.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (C) 2002 Carsten Burghardt <burghardt@kde.org>
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMail is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
00030 */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035 
00036 #include "subscriptiondialog.h"
00037 #include "folderstorage.h"
00038 #include "listjob.h"
00039 #include "imapaccountbase.h"
00040 
00041 #include <klocale.h>
00042 #include <kdebug.h>
00043 #include <kmessagebox.h>
00044 
00045 
00046 namespace KMail {
00047 
00048 SubscriptionDialog::SubscriptionDialog( QWidget *parent, const QString &caption,
00049     KAccount *acct, QString startPath )
00050   : KSubscription( parent, caption, acct, User1, QString::null, false ),
00051     mStartPath( startPath ), mSubscribed( false ), mForceSubscriptionEnable( false)
00052 {
00053   // hide unneeded checkboxes
00054   hideTreeCheckbox();
00055   hideNewOnlyCheckbox();
00056 
00057   // ok-button
00058   connect(this, SIGNAL(okClicked()), SLOT(slotSave()));
00059 
00060   // reload-list button
00061   connect(this, SIGNAL(user1Clicked()), SLOT(slotLoadFolders()));
00062 
00063   // get the folders
00064   slotLoadFolders();
00065 }
00066 
00067 //------------------------------------------------------------------------------
00068 void SubscriptionDialog::slotListDirectory( const QStringList& subfolderNames,
00069                                             const QStringList& subfolderPaths,
00070                                             const QStringList& subfolderMimeTypes,
00071                                             const QStringList& subfolderAttributes,
00072                                             const ImapAccountBase::jobData& jobData )
00073 {
00074   mFolderNames = subfolderNames;
00075   mFolderPaths = subfolderPaths;
00076   mFolderMimeTypes = subfolderMimeTypes;
00077   mFolderAttributes = subfolderAttributes;
00078   mJobData = jobData;
00079 
00080   mCount = 0;
00081 
00082   createItems();
00083 }
00084 
00085 //------------------------------------------------------------------------------
00086 void SubscriptionDialog::createItems()
00087 {
00088   bool onlySubscribed = mJobData.onlySubscribed;
00089 
00090   GroupItem *parent = 0;
00091   uint done = 0;
00092 //  kdDebug(5006) << "createItems subscribed=" << onlySubscribed <<",folders="
00093 //    << mFolderNames.join(",") << endl;
00094 
00095   for (uint i = mCount; i < mFolderNames.count(); ++i)
00096   {
00097     // give the dialog a chance to repaint
00098     if (done == 1000)
00099     {
00100       emit listChanged();
00101       QTimer::singleShot(0, this, SLOT(createItems()));
00102       return;
00103     }
00104     ++mCount;
00105     ++done;
00106     GroupItem *item = 0;
00107     if (!onlySubscribed && mFolderPaths.size() > 0)
00108     {
00109       // get the parent
00110       GroupItem *oldItem = 0;
00111       QString parentPath;
00112       findParentItem( mFolderNames[i], mFolderPaths[i], parentPath, &parent, &oldItem );
00113 
00114       if (!parent && parentPath != "/")
00115       {
00116         // the parent is not available and it's no root-item
00117         // this happens when the folders do not arrive in hierarchical order
00118         // so we create each parent in advance
00119         QStringList folders = QStringList::split(mDelimiter, parentPath);
00120         uint i = 0;
00121         for ( QStringList::Iterator it = folders.begin(); it != folders.end(); ++it ) 
00122         {
00123           QString name = *it;
00124           if (name.startsWith("/"))
00125             name = name.right(name.length()-1);
00126           if (name.endsWith("/"))
00127             name.truncate(name.length()-1);
00128           KGroupInfo info(name);
00129           info.subscribed = false;
00130 
00131           QStringList tmpPath;
00132           for ( uint j = 0; j <= i; ++j )
00133             tmpPath << folders[j];
00134           QString path = tmpPath.join(mDelimiter);
00135           if (!path.startsWith("/"))
00136             path = "/" + path;
00137           if (!path.endsWith("/"))
00138             path = path + "/";
00139           info.path = path;
00140           item = 0;
00141           if (folders.count() > 1)
00142           {
00143             // we have to create more then one level, so better check if this
00144             // folder already exists somewhere
00145             item = mItemDict[path];
00146           }
00147           // as these items are "dummies" we create them non-checkable
00148           if (!item)
00149           {
00150             if (parent)
00151               item = new GroupItem(parent, info, this, false);
00152             else
00153               item = new GroupItem(folderTree(), info, this, false);
00154             mItemDict.insert(info.path, item);
00155           }
00156 
00157           parent = item;
00158           ++i;
00159         } // folders
00160       } // parent
00161     
00162       KGroupInfo info(mFolderNames[i]);
00163       info.path = mFolderPaths[i];
00164 
00165       if ( info.path == "/INBOX/" )
00166         info.name = i18n("inbox");
00167 
00168       // only checkable when the folder is selectable
00169       bool checkable = ( mFolderMimeTypes[i] == "inode/directory" ) ? false : true;
00170       // create a new item
00171       if (parent)
00172         item = new GroupItem(parent, info, this, checkable);
00173       else
00174         item = new GroupItem(folderTree(), info, this, checkable);
00175 
00176       if (oldItem) // remove old item
00177         mItemDict.remove(info.path);
00178       
00179       mItemDict.insert(info.path, item);
00180       if (oldItem)
00181       {
00182         // move the old childs to the new item
00183         QPtrList<QListViewItem> itemsToMove;
00184         QListViewItem * myChild = oldItem->firstChild();
00185         while (myChild)
00186         {
00187           itemsToMove.append(myChild);
00188           myChild = myChild->nextSibling();
00189         }
00190         QPtrListIterator<QListViewItem> it( itemsToMove );
00191         QListViewItem *cur;
00192         while ((cur = it.current()))
00193         {
00194           oldItem->takeItem(cur);
00195           item->insertItem(cur);
00196           if ( cur->isSelected() ) // we have new parents so open them
00197             folderTree()->ensureItemVisible( cur );
00198           ++it;
00199         }
00200         delete oldItem;
00201         itemsToMove.clear();
00202       }
00203       // select the start item
00204       if ( mFolderPaths[i] == mStartPath )
00205       {
00206         item->setSelected( true );
00207         folderTree()->ensureItemVisible( item );
00208       }
00209 
00210     } else if (onlySubscribed)
00211     {
00212       // find the item
00213       if ( mItemDict[mFolderPaths[i]] )
00214       {
00215         GroupItem* item = mItemDict[mFolderPaths[i]];
00216         item->setOn( true );
00217       }
00218     }
00219   }
00220   processNext();
00221 }
00222 
00223 //------------------------------------------------------------------------------
00224 void SubscriptionDialog::findParentItem( QString &name, QString &path, QString &parentPath,
00225     GroupItem **parent, GroupItem **oldItem )
00226 {
00227   // remove the name (and the separator) from the path to get the parent path
00228   int start = path.length() - (name.length()+2);
00229   int length = name.length()+1;
00230   if (start < 0) start = 0;
00231   parentPath = path;
00232   parentPath.remove(start, length);
00233 
00234   // find the parent by it's path
00235   *parent = mItemDict[parentPath];
00236   
00237   // check if the item already exists
00238   *oldItem = mItemDict[path];
00239 }
00240 
00241 //------------------------------------------------------------------------------
00242 void SubscriptionDialog::slotSave()
00243 {
00244   KMail::ImapAccountBase *account = static_cast<KMail::ImapAccountBase*>(mAcct);
00245   if( !account->onlySubscribedFolders() ) {
00246       int result = KMessageBox::questionYesNoCancel( this,
00247               i18n("Currently subscriptions are not used for server %1\ndo you want to enable subscriptions?")
00248               .arg( account->name() ),
00249               i18n("Enable Subscriptions?"), i18n("Enable"), i18n("Do Not Enable"));
00250       switch(result) {
00251           case KMessageBox::Yes:
00252               mForceSubscriptionEnable = true;
00253               break;
00254           case KMessageBox::No:
00255               break;
00256           case KMessageBox::Cancel:
00257               cancel();
00258       }
00259   }
00260 
00261   // subscribe
00262   QListViewItemIterator it(subView);
00263   for ( ; it.current(); ++it)
00264   {
00265     account->changeSubscription(true,
00266         static_cast<GroupItem*>(it.current())->info().path);
00267   }
00268 
00269   // unsubscribe
00270   QListViewItemIterator it2(unsubView);
00271   for ( ; it2.current(); ++it2)
00272   {
00273     account->changeSubscription(false,
00274         static_cast<GroupItem*>(it2.current())->info().path);
00275   }
00276 
00277   if ( mForceSubscriptionEnable ) {
00278     account->setOnlySubscribedFolders(true);
00279   }
00280 }
00281 
00282 //------------------------------------------------------------------------------
00283 void SubscriptionDialog::slotLoadFolders()
00284 {
00285   ImapAccountBase* ai = static_cast<ImapAccountBase*>(account());
00286   // we need a connection
00287   if ( ai->makeConnection() == ImapAccountBase::Error )
00288   {
00289     kdWarning(5006) << "SubscriptionDialog - got no connection" << endl;
00290     return;
00291   } else if ( ai->makeConnection() == ImapAccountBase::Connecting )
00292   {
00293     // We'll wait for the connectionResult signal from the account.
00294     kdDebug(5006) << "SubscriptionDialog - waiting for connection" << endl;
00295     connect( ai, SIGNAL( connectionResult(int, const QString&) ),
00296         this, SLOT( slotConnectionResult(int, const QString&) ) );
00297     return;
00298   }
00299   // clear the views
00300   KSubscription::slotLoadFolders();
00301   mItemDict.clear();
00302   mSubscribed = false;
00303   mLoading = true;
00304 
00305   initPrefixList();
00306 
00307   processNext();
00308 }
00309 
00310 //------------------------------------------------------------------------------
00311 void SubscriptionDialog::processNext()
00312 {
00313   if ( mPrefixList.isEmpty() ) 
00314   {
00315     if ( !mSubscribed )
00316     {
00317       mSubscribed = true;
00318       initPrefixList();
00319       if ( mPrefixList.isEmpty() )
00320       {
00321           // still empty? then we have nothing to do here as this is an error
00322           slotLoadingComplete();
00323           return;
00324       }
00325     } else {
00326       slotLoadingComplete();
00327       return;
00328     }
00329   }
00330   ImapAccountBase* ai = static_cast<ImapAccountBase*>(account());
00331   ImapAccountBase::ListType type = ( mSubscribed ? 
00332       ImapAccountBase::ListSubscribedNoCheck : ImapAccountBase::List );
00333 
00334   bool completeListing = true;
00335   mCurrentNamespace = mPrefixList.first();
00336   mDelimiter = ai->delimiterForNamespace( mCurrentNamespace );
00337   mPrefixList.pop_front();
00338   if ( mCurrentNamespace == "/INBOX/" )
00339   {
00340     type = mSubscribed ? 
00341       ImapAccountBase::ListFolderOnlySubscribed : ImapAccountBase::ListFolderOnly;
00342     completeListing = false;
00343   }
00344 
00345 //  kdDebug(5006) << "process " << mCurrentNamespace << ",subscribed=" << mSubscribed << endl;
00346   ListJob* job = new ListJob( ai, type, 0, ai->addPathToNamespace( mCurrentNamespace ), completeListing );
00347   connect( job, SIGNAL(receivedFolders(const QStringList&, const QStringList&,
00348           const QStringList&, const QStringList&, const ImapAccountBase::jobData&)),
00349       this, SLOT(slotListDirectory(const QStringList&, const QStringList&,
00350           const QStringList&, const QStringList&, const ImapAccountBase::jobData&)));
00351   job->start();
00352 }
00353 
00354 //------------------------------------------------------------------------------
00355 void SubscriptionDialog::initPrefixList()
00356 {
00357   ImapAccountBase* ai = static_cast<ImapAccountBase*>(account());
00358   ImapAccountBase::nsMap map = ai->namespaces();
00359   mPrefixList.clear();
00360 
00361   bool hasInbox = false;
00362   const QStringList ns = map[ImapAccountBase::PersonalNS];
00363   for ( QStringList::ConstIterator it = ns.begin(); it != ns.end(); ++it )
00364   {
00365     if ( (*it).isEmpty() )
00366       hasInbox = true;
00367   }
00368   if ( !hasInbox && !ns.isEmpty() ) 
00369   {
00370     // the namespaces includes no listing for the root so start a special
00371     // listing for the INBOX to make sure we get it
00372     mPrefixList += "/INBOX/";
00373   }
00374   
00375   mPrefixList += map[ImapAccountBase::PersonalNS];
00376   mPrefixList += map[ImapAccountBase::OtherUsersNS];
00377   mPrefixList += map[ImapAccountBase::SharedNS];
00378 }
00379 
00380 void SubscriptionDialog::slotConnectionResult( int errorCode, const QString& errorMsg )
00381 {
00382   Q_UNUSED( errorMsg );
00383   if ( !errorCode )
00384     slotLoadFolders();
00385 }
00386 
00387 void SubscriptionDialog::show()
00388 {
00389   KDialogBase::show();
00390 }
00391 
00392 } // namespace
00393 
00394 #include "subscriptiondialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys