Index: kmail/kmfoldertree.cpp
===================================================================
--- kmail/kmfoldertree.cpp	(revision 774935)
+++ kmail/kmfoldertree.cpp	(working copy)
@@ -1545,7 +1545,7 @@
   {
     KMFolderImap *folder = static_cast<KMFolderImap*>( fti->folder()->storage() );
     // if we should list all folders we limit this to the root folder
-    if ( !folder->account()->listOnlyOpenFolders() &&
+    if ( !folder->account() || !folder->account()->listOnlyOpenFolders() &&
          fti->parent() )
       return;
     if ( folder->getSubfolderState() == KMFolderImap::imapNoInformation )
Index: kmail/accountmanager.cpp
===================================================================
--- kmail/accountmanager.cpp	(revision 774935)
+++ kmail/accountmanager.cpp	(working copy)
@@ -44,25 +44,31 @@
 void AccountManager::writeConfig( bool withSync )
 {
   KConfig* config = KMKernel::config();
-  QString groupName;
 
-  KConfigGroup group(config, "General");
-  group.writeEntry("accounts", mAcctList.count());
-
-  // first delete all account groups in the config file:
+  // Delete all account enabled groups in the config file
+  // and replace them with new account groups
+  uint accounts = 0;
   QStringList accountGroups =
     config->groupList().filter( QRegExp( "Account \\d+" ) );
-  for ( QStringList::Iterator it = accountGroups.begin() ;
-	it != accountGroups.end() ; ++it )
-    config->deleteGroup( *it );
-
-  // now write new account groups:
-  int i = 1;
-  for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it, ++i ) {
-    groupName.sprintf("Account %d", i);
+  AccountList::ConstIterator enabledAccountIt = mAcctList.constBegin();
+  foreach ( const QString& groupName, accountGroups ) {
     KConfigGroup group(config, groupName);
-    (*it)->writeConfig(group);
+    uint id = group.readEntry( "Id", 0 );
+    if ( mDisabledAccounts.contains( id ) )
+      accounts++;
+    else {
+      config->deleteGroup( groupName );
+      if ( enabledAccountIt != mAcctList.constEnd() ) {
+        (*enabledAccountIt)->writeConfig( group );
+        ++enabledAccountIt;
+        accounts++;
+      }
+    }
   }
+
+  KConfigGroup group(config, "General");
+  group.writeEntry("accounts", accounts);
+
   if (withSync) config->sync();
 }
 
@@ -75,7 +81,6 @@
   QString acctName;
   QString groupName;
   int i, num;
-  uint id;
 
   for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
       delete *it;
@@ -88,10 +93,14 @@
   {
     groupName.sprintf("Account %d", i);
     KConfigGroup group(config, groupName);
+    uint id = group.readEntry( "Id", 0 );
+    if ( !group.readEntry("Enabled", true) ) {
+      mDisabledAccounts += id;
+      continue;
+    }
 
     KAccount::Type acctType = KAccount::typeForName( group.readEntry( "Type" ) );
     acctName = group.readEntry("Name");
-    id = group.readEntry( "Id", 0 );
     if (acctName.isEmpty()) acctName = i18n("Account %1", i);
     acct = create(acctType, acctName, id);
     if (!acct) continue;
Index: kmail/accountmanager.h
===================================================================
--- kmail/accountmanager.h	(revision 774935)
+++ kmail/accountmanager.h	(working copy)
@@ -80,6 +80,13 @@
     KMAccount *find( const uint id ) const;
 
     /**
+      @return true if account with ID @p id is enabled. 
+      Accounts can be disabled by setting "enabled" flag to false 
+      in "Account #" groups of kmailrc.
+    */
+    bool isEnabled( const uint id ) const { return !mDisabledAccounts.contains(id); }
+
+    /**
       Physically remove account. Also deletes the given account object !
       Returns false and does nothing if the account cannot be removed.
     */
@@ -169,6 +176,9 @@
 
     // if a summary should be displayed
     bool mDisplaySummary;
+
+    // IDs of disabled accounts, used in AccountManager::writeConfig()
+    QSet<uint> mDisabledAccounts;
 };
 
 } // namespace KMail
Index: kmail/kmkernel.cpp
===================================================================
--- kmail/kmkernel.cpp	(revision 774935)
+++ kmail/kmkernel.cpp	(working copy)
@@ -1458,7 +1458,8 @@
   {
     KMFolderNode *node = *it;
     if (node->isDir() || ((acct = the_acctMgr->find(node->id()))
-                      && ( acct->type() == KAccount::Imap )) )
+                          && ( acct->type() == KAccount::Imap )) 
+                      || !the_acctMgr->isEnabled( node->id() ))
     {
       ++it;
     } else {
