kmail Library API Documentation

folderdiaacltab.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00033 #include "folderdiaacltab.h"
00034 #include "acljobs.h"
00035 #include "kmfolderimap.h"
00036 #include "kmfoldercachedimap.h"
00037 #include "kmacctcachedimap.h"
00038 #include "kmfolder.h"
00039 
00040 #include <addressesdialog.h>
00041 #include <kabc/addresseelist.h>
00042 #include <distributionlist.h>
00043 #include <kabc/stdaddressbook.h>
00044 #include <kaddrbook.h>
00045 #include <kpushbutton.h>
00046 #include <kdebug.h>
00047 #include <klocale.h>
00048 
00049 #include <qlayout.h>
00050 #include <qlabel.h>
00051 #include <qvbox.h>
00052 #include <qvbuttongroup.h>
00053 #include <qwidgetstack.h>
00054 #include <qradiobutton.h>
00055 #include <qwhatsthis.h>
00056 
00057 #include <assert.h>
00058 #include <kmessagebox.h>
00059 
00060 using namespace KMail;
00061 
00062 // In case your kdelibs is < 3.3
00063 #ifndef I18N_NOOP2
00064 #define I18N_NOOP2( comment,x ) x
00065 #endif
00066 
00067 // The set of standard permission sets
00068 static const struct {
00069   unsigned int permissions;
00070   const char* userString;
00071 } standardPermissions[] = {
00072   { 0, I18N_NOOP2( "Permissions", "None" ) },
00073   { ACLJobs::List | ACLJobs::Read, I18N_NOOP2( "Permissions", "Read" ) },
00074   { ACLJobs::List | ACLJobs::Read | ACLJobs::Insert | ACLJobs::Post, I18N_NOOP2( "Permissions", "Append" ) },
00075   { ACLJobs::AllWrite, I18N_NOOP2( "Permissions", "Write" ) },
00076   { ACLJobs::All, I18N_NOOP2( "Permissions", "All" ) }
00077 };
00078 
00079 
00080 KMail::ACLEntryDialog::ACLEntryDialog( IMAPUserIdFormat userIdFormat, const QString& caption, QWidget* parent, const char* name )
00081   : KDialogBase( parent, name, true /*modal*/, caption,
00082                  KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true /*sep*/ )
00083   , mUserIdFormat( userIdFormat )
00084 {
00085   QWidget *page = new QWidget( this );
00086   setMainWidget(page);
00087   QGridLayout *topLayout = new QGridLayout( page, 3 /*rows*/, 3 /*cols*/, 0, spacingHint() );
00088 
00089   QLabel *label = new QLabel( i18n( "&User identifier:" ), page );
00090   topLayout->addWidget( label, 0, 0 );
00091 
00092   mUserIdLineEdit = new KLineEdit( page );
00093   topLayout->addWidget( mUserIdLineEdit, 0, 1 );
00094   label->setBuddy( mUserIdLineEdit );
00095   QWhatsThis::add( mUserIdLineEdit, i18n( "The User Identifier is the login of the user on the IMAP server. This can be a simple user name or the full email address of the user; the login for your own account on the server will tell you which one it is." ) );
00096 
00097   QPushButton* kabBtn = new QPushButton( "...", page );
00098   topLayout->addWidget( kabBtn, 0, 2 );
00099 
00100   mButtonGroup = new QVButtonGroup( i18n( "Permissions" ), page );
00101   topLayout->addMultiCellWidget( mButtonGroup, 1, 1, 0, 2 );
00102 
00103   for ( unsigned int i = 0;
00104         i < sizeof( standardPermissions ) / sizeof( *standardPermissions );
00105         ++i ) {
00106     QRadioButton* cb = new QRadioButton( i18n( "Permissions", standardPermissions[i].userString ), mButtonGroup );
00107     // We store the permission value (bitfield) as the id of the radiobutton in the group
00108     mButtonGroup->insert( cb, standardPermissions[i].permissions );
00109   }
00110   topLayout->setRowStretch(2, 10);
00111 
00112   connect( mUserIdLineEdit, SIGNAL( textChanged( const QString& ) ), SLOT( slotChanged() ) );
00113   connect( kabBtn, SIGNAL( clicked() ), SLOT( slotSelectAddresses() ) );
00114   connect( mButtonGroup, SIGNAL( clicked( int ) ), SLOT( slotChanged() ) );
00115   enableButtonOK( false );
00116 
00117   mUserIdLineEdit->setFocus();
00118   // Ensure the lineedit is rather wide so that email addresses can be read in it
00119   incInitialSize( QSize( 200, 0 ) );
00120 }
00121 
00122 void KMail::ACLEntryDialog::slotChanged()
00123 {
00124   enableButtonOK( !mUserIdLineEdit->text().isEmpty() && mButtonGroup->selected() != 0 );
00125 }
00126 
00127 static QString addresseeToUserId( const KABC::Addressee& addr, IMAPUserIdFormat userIdFormat )
00128 {
00129   QString email = addr.preferredEmail();
00130   if ( userIdFormat == FullEmail )
00131     return email;
00132   else { // mUserIdFormat == UserName
00133     email.truncate( email.find( '@' ) );
00134     return email;
00135   }
00136 }
00137 
00138 void KMail::ACLEntryDialog::slotSelectAddresses()
00139 {
00140   KPIM::AddressesDialog dlg( this );
00141   dlg.setShowCC( false );
00142   dlg.setShowBCC( false );
00143   if ( mUserIdFormat == FullEmail ) // otherwise we have no way to go back from userid to email
00144     dlg.setSelectedTo( userIds() );
00145   if ( dlg.exec() != QDialog::Accepted )
00146     return;
00147 
00148   const QStringList distrLists = dlg.toDistributionLists();
00149   QString txt;
00150   if ( !distrLists.isEmpty() ) {
00151     for( QStringList::ConstIterator it = distrLists.begin(); it != distrLists.end(); ++it ) {
00152       if ( !txt.isEmpty() )
00153         txt += ", ";
00154       txt += *it; // put the distr list name here, don't expand until saving
00155     }
00156   }
00157   const KABC::Addressee::List lst = dlg.toAddresses();
00158   if ( !lst.isEmpty() ) {
00159     for( QValueList<KABC::Addressee>::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
00160       if ( !txt.isEmpty() )
00161         txt += ", ";
00162       txt += addresseeToUserId( *it, mUserIdFormat );
00163     }
00164   }
00165   mUserIdLineEdit->setText( txt );
00166 }
00167 
00168 void KMail::ACLEntryDialog::setValues( const QString& userId, unsigned int permissions )
00169 {
00170   mUserIdLineEdit->setText( userId );
00171   mButtonGroup->setButton( permissions );
00172   enableButtonOK( !userId.isEmpty() );
00173 }
00174 
00175 QString KMail::ACLEntryDialog::userId() const
00176 {
00177   return mUserIdLineEdit->text();
00178 }
00179 
00180 QStringList KMail::ACLEntryDialog::userIds() const
00181 {
00182   QStringList lst = QStringList::split( ",", mUserIdLineEdit->text() );
00183   for( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
00184     // Strip white space (in particular, due to ", ")
00185     *it = (*it).stripWhiteSpace();
00186   }
00187   return lst;
00188 }
00189 
00190 unsigned int KMail::ACLEntryDialog::permissions() const
00191 {
00192   return mButtonGroup->selectedId();
00193 }
00194 
00195 // class KMail::FolderDiaACLTab::ListView : public KListView
00196 // {
00197 // public:
00198 //   ListView( QWidget* parent, const char* name = 0 ) : KListView( parent, name ) {}
00199 // };
00200 
00201 class KMail::FolderDiaACLTab::ListViewItem : public KListViewItem
00202 {
00203 public:
00204   ListViewItem( QListView* listview )
00205     : KListViewItem( listview, listview->lastItem() ),
00206       mModified( false ), mNew( false ) {}
00207 
00208   void load( const ACLListEntry& entry );
00209   void save( ACLList& list, KABC::AddressBook* addressBook, IMAPUserIdFormat userIdFormat );
00210 
00211   QString userId() const { return text( 0 ); }
00212   void setUserId( const QString& userId ) { setText( 0, userId ); }
00213 
00214   unsigned int permissions() const { return mPermissions; }
00215   void setPermissions( unsigned int permissions );
00216 
00217   bool isModified() const { return mModified; }
00218   void setModified( bool b ) { mModified = b; }
00219 
00220   // The fact that an item is new doesn't matter much.
00221   // This bool is only used to handle deletion differently
00222   bool isNew() const { return mNew; }
00223   void setNew( bool b ) { mNew = b; }
00224 
00225 private:
00226   unsigned int mPermissions;
00227   QString mInternalRightsList; 
00228   bool mModified;
00229   bool mNew;
00230 };
00231 
00232 // internalRightsList is only used if permissions doesn't match the standard set
00233 static QString permissionsToUserString( unsigned int permissions, const QString& internalRightsList )
00234 {
00235   for ( unsigned int i = 0;
00236         i < sizeof( standardPermissions ) / sizeof( *standardPermissions );
00237         ++i ) {
00238     if ( permissions == standardPermissions[i].permissions )
00239       return i18n( "Permissions", standardPermissions[i].userString );
00240   }
00241   if ( internalRightsList.isEmpty() )
00242     return i18n( "Custom Permissions" ); // not very helpful, but shouldn't happen
00243   else
00244     return i18n( "Custom Permissions (%1)" ).arg( internalRightsList );
00245 }
00246 
00247 void KMail::FolderDiaACLTab::ListViewItem::setPermissions( unsigned int permissions )
00248 {
00249   mPermissions = permissions;
00250   setText( 1, permissionsToUserString( permissions, QString::null ) );
00251 }
00252 
00253 void KMail::FolderDiaACLTab::ListViewItem::load( const ACLListEntry& entry )
00254 {
00255   // Don't allow spaces in userids. If you need this, fix the slave->app communication,
00256   // since it uses space as a separator (imap4.cc, look for GETACL)
00257   // It's ok in distribution list names though, that's why this check is only done here
00258   // and also why there's no validator on the lineedit.
00259   if ( entry.userId.contains( ' ' ) )
00260     kdWarning(5006) << "Userid contains a space!!!  '" << entry.userId << "'" << endl;
00261 
00262   setUserId( entry.userId );
00263   mPermissions = entry.permissions;
00264   mInternalRightsList = entry.internalRightsList;
00265   setText( 1, permissionsToUserString( entry.permissions, entry.internalRightsList ) );
00266   mModified = entry.changed; // for dimap, so that earlier changes are still marked as changes
00267 }
00268 
00269 void KMail::FolderDiaACLTab::ListViewItem::save( ACLList& aclList, KABC::AddressBook* addressBook, IMAPUserIdFormat userIdFormat )
00270 {
00271   // expand distribution lists
00272   KPIM::DistributionList list = KPIM::DistributionList::findByName( addressBook, userId(), false );
00273   if ( !list.isEmpty() ) {
00274     Q_ASSERT( mModified ); // it has to be new, it couldn't be stored as a distr list name....
00275     KPIM::DistributionList::Entry::List entryList = list.entries(addressBook);
00276     KPIM::DistributionList::Entry::List::ConstIterator it; // nice number of "::"!
00277     for( it = entryList.begin(); it != entryList.end(); ++it ) {
00278       QString email = (*it).email;
00279       if ( email.isEmpty() )
00280         email = addresseeToUserId( (*it).addressee, userIdFormat );
00281       ACLListEntry entry( email, QString::null, mPermissions );
00282       entry.changed = true;
00283       aclList.append( entry );
00284     }
00285   } else { // it wasn't a distribution list
00286     ACLListEntry entry( userId(), mInternalRightsList, mPermissions );
00287     if ( mModified ) {
00288       entry.internalRightsList = QString::null;
00289       entry.changed = true;
00290     }
00291     aclList.append( entry );
00292   }
00293 }
00294 
00296 
00297 KMail::FolderDiaACLTab::FolderDiaACLTab( KMFolderDialog* dlg, QWidget* parent, const char* name )
00298   : FolderDiaTab( parent, name ),
00299     mImapAccount( 0 ),
00300     mUserRights( 0 ),
00301     mDlg( dlg ),
00302     mChanged( false ), mAccepting( false ), mSaving( false )
00303 {
00304   QVBoxLayout* topLayout = new QVBoxLayout( this );
00305   // We need a widget stack to show either a label ("no acl support", "please wait"...)
00306   // or a listview.
00307   mStack = new QWidgetStack( this );
00308   topLayout->addWidget( mStack );
00309 
00310   mLabel = new QLabel( mStack );
00311   mLabel->setAlignment( AlignHCenter | AlignVCenter | WordBreak );
00312   mStack->addWidget( mLabel );
00313 
00314   mACLWidget = new QHBox( mStack );
00315   mACLWidget->setSpacing( KDialog::spacingHint() );
00316   mListView = new KListView( mACLWidget );
00317   mListView->setAllColumnsShowFocus( true );
00318   mStack->addWidget( mACLWidget );
00319   mListView->addColumn( i18n( "User Id" ) );
00320   mListView->addColumn( i18n( "Permissions" ) );
00321 
00322   connect( mListView, SIGNAL(doubleClicked(QListViewItem*,const QPoint&,int)),
00323        SLOT(slotEditACL(QListViewItem*)) );
00324   connect( mListView, SIGNAL(returnPressed(QListViewItem*)),
00325        SLOT(slotEditACL(QListViewItem*)) );
00326   connect( mListView, SIGNAL(selectionChanged(QListViewItem*)),
00327        SLOT(slotSelectionChanged(QListViewItem*)) );
00328 
00329   QVBox* buttonBox = new QVBox( mACLWidget );
00330   buttonBox->setSpacing( KDialog::spacingHint() );
00331   mAddACL = new KPushButton( i18n( "Add Entry" ), buttonBox );
00332   mEditACL = new KPushButton( i18n( "Modify Entry" ), buttonBox );
00333   mRemoveACL = new KPushButton( i18n( "Remove Entry" ), buttonBox );
00334   QSpacerItem* spacer = new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
00335   static_cast<QBoxLayout *>( buttonBox->layout() )->addItem( spacer );
00336 
00337   connect( mAddACL, SIGNAL( clicked() ), SLOT( slotAddACL() ) );
00338   connect( mEditACL, SIGNAL( clicked() ), SLOT( slotEditACL() ) );
00339   connect( mRemoveACL, SIGNAL( clicked() ), SLOT( slotRemoveACL() ) );
00340   mEditACL->setEnabled( false );
00341   mRemoveACL->setEnabled( false );
00342 
00343   connect( this, SIGNAL( changed(bool) ), SLOT( slotChanged(bool) ) );
00344 }
00345 
00346 // Warning before save() this will return the url of the _parent_ folder, when creating a new one
00347 KURL KMail::FolderDiaACLTab::imapURL() const
00348 {
00349   KURL url = mImapAccount->getUrl();
00350   url.setPath( mImapPath );
00351   return url;
00352 }
00353 
00354 void KMail::FolderDiaACLTab::initializeWithValuesFromFolder( KMFolder* folder )
00355 {
00356   // This can be simplified once KMFolderImap and KMFolderCachedImap have a common base class
00357   mFolderType = folder->folderType();
00358   if ( mFolderType == KMFolderTypeImap ) {
00359     KMFolderImap* folderImap = static_cast<KMFolderImap*>( folder->storage() );
00360     mImapPath = folderImap->imapPath();
00361     mImapAccount = folderImap->account();
00362     mUserRights = folderImap->userRights();
00363   }
00364   else if ( mFolderType == KMFolderTypeCachedImap ) {
00365     KMFolderCachedImap* folderImap = static_cast<KMFolderCachedImap*>( folder->storage() );
00366     mImapPath = folderImap->imapPath();
00367     mImapAccount = folderImap->account();
00368     mUserRights = folderImap->userRights();
00369   }
00370   else
00371     assert( 0 ); // see KMFolderDialog constructor
00372 }
00373 
00374 void KMail::FolderDiaACLTab::load()
00375 {
00376   if ( mDlg->folder() ) {
00377     // existing folder
00378     initializeWithValuesFromFolder( mDlg->folder() );
00379   } else if ( mDlg->parentFolder() ) {
00380     // new folder
00381     initializeWithValuesFromFolder( mDlg->parentFolder() );
00382     mChanged = true; // ensure that saving happens
00383   }
00384 
00385   // KABC knows email addresses.
00386   // We want LDAP userids.
00387   // Depending on the IMAP server setup, the userid can be the full email address,
00388   // or just the username part of it.
00389   // To know which one it is, we currently have a hidden config option,
00390   // but the default value is determined from the current user's own id.
00391   QString defaultFormat = "fullemail";
00392   // warning mImapAccount can be 0 if creating a subsubsubfolder with dimap...  (bug?)
00393   if ( mImapAccount && mImapAccount->login().find('@') == -1 )
00394     defaultFormat = "username"; // no @ found, so we assume it's just the username
00395   KConfigGroup configGroup( kmkernel->config(), "IMAP" );
00396   QString str = configGroup.readEntry( "UserIdFormat", defaultFormat );
00397   mUserIdFormat = FullEmail;
00398   if ( str == "username" )
00399     mUserIdFormat = UserName;
00400 
00401   if ( mFolderType == KMFolderTypeCachedImap ) {
00402     KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
00403     KMFolderCachedImap* folderImap = static_cast<KMFolderCachedImap*>( folder->storage() );
00404     if ( mUserRights == -1 ) { // error
00405       mLabel->setText( i18n( "Error retrieving user permissions." ) );
00406     } else if ( mUserRights == 0 /* can't happen anymore */ || folderImap->aclList().isEmpty() ) {
00407       /* We either synced, or we read user rights from the config, so we can
00408          assume the server supports acls and an empty list means we haven't
00409          synced yet. */
00410         mLabel->setText( i18n( "Information not retrieved from server yet, please use \"Check Mail\"." ) );
00411     } else {
00412       loadFinished( folderImap->aclList() );
00413     }
00414     return;
00415   }
00416 
00417   // Loading, for online IMAP, consists of four steps:
00418   // 1) connect
00419   // 2) get user rights
00420   // 3) load ACLs
00421 
00422   // First ensure we are connected
00423   mStack->raiseWidget( mLabel );
00424   if ( !mImapAccount ) { // hmmm?
00425     mLabel->setText( i18n( "Error: no IMAP account defined for this folder" ) );
00426     return;
00427   }
00428   KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
00429   if ( folder && folder->storage() == mImapAccount->rootFolder() )
00430     return; // nothing to be done for the (virtual) account folder
00431   mLabel->setText( i18n( "Connecting to server %1, please wait..." ).arg( mImapAccount->host() ) );
00432   ImapAccountBase::ConnectionState state = mImapAccount->makeConnection();
00433   if ( state == ImapAccountBase::Error ) { // Cancelled by user, or slave can't start
00434     slotConnectionResult( -1, QString::null );
00435   } else if ( state == ImapAccountBase::Connecting ) {
00436     connect( mImapAccount, SIGNAL( connectionResult(int, const QString&) ),
00437              this, SLOT( slotConnectionResult(int, const QString&) ) );
00438   } else { // Connected
00439     slotConnectionResult( 0, QString::null );
00440   }
00441 }
00442 
00443 void KMail::FolderDiaACLTab::slotConnectionResult( int errorCode, const QString& errorMsg )
00444 {
00445   disconnect( mImapAccount, SIGNAL( connectionResult(int, const QString&) ),
00446               this, SLOT( slotConnectionResult(int, const QString&) ) );
00447   if ( errorCode ) {
00448     if ( errorCode == -1 ) // unspecified error
00449       mLabel->setText( i18n( "Error connecting to server %1" ).arg( mImapAccount->host() ) );
00450     else
00451       // Connection error (error message box already shown by the account)
00452       mLabel->setText( KIO::buildErrorString( errorCode, errorMsg ) );
00453     return;
00454   }
00455 
00456   if ( mUserRights == 0 ) {
00457     connect( mImapAccount, SIGNAL( receivedUserRights( KMFolder* ) ),
00458              this, SLOT( slotReceivedUserRights( KMFolder* ) ) );
00459     KMFolder* folder = mDlg->folder() ? mDlg->folder() : mDlg->parentFolder();
00460     mImapAccount->getUserRights( folder, mImapPath );
00461   }
00462   else
00463     startListing();
00464 }
00465 
00466 void KMail::FolderDiaACLTab::slotReceivedUserRights( KMFolder* folder )
00467 {
00468   if ( !mImapAccount->hasACLSupport() ) {
00469     mLabel->setText( i18n( "This IMAP server does not have support for access control lists (ACL)" ) );
00470     return;
00471   }
00472 
00473   if ( folder == mDlg->folder() ? mDlg->folder() : mDlg->parentFolder() ) {
00474     KMFolderImap* folderImap = static_cast<KMFolderImap*>( folder->storage() );
00475     mUserRights = folderImap->userRights();
00476     startListing();
00477   }
00478 }
00479 
00480 void KMail::FolderDiaACLTab::startListing()
00481 {
00482   // List ACLs of folder - or its parent, if creating a new folder
00483   mImapAccount->getACL( mDlg->folder() ? mDlg->folder() : mDlg->parentFolder(), mImapPath );
00484   connect( mImapAccount, SIGNAL(receivedACL( KMFolder*, KIO::Job*, const KMail::ACLList& )),
00485            this, SLOT(slotReceivedACL( KMFolder*, KIO::Job*, const KMail::ACLList& )) );
00486 }
00487 
00488 void KMail::FolderDiaACLTab::slotReceivedACL( KMFolder* folder, KIO::Job* job, const KMail::ACLList& aclList )
00489 {
00490   if ( folder == ( mDlg->folder() ? mDlg->folder() : mDlg->parentFolder() ) ) {
00491     disconnect( mImapAccount, SIGNAL(receivedACL( KMFolder*, KIO::Job*, const KMail::ACLList& )),
00492                 this, SLOT(slotReceivedACL( KMFolder*, KIO::Job*, const KMail::ACLList& )) );
00493 
00494     if ( job && job->error() ) {
00495       if ( job->error() == KIO::ERR_UNSUPPORTED_ACTION )
00496         mLabel->setText( i18n( "This IMAP server does not have support for access control lists (ACL)" ) );
00497       else
00498         mLabel->setText( i18n( "Error retrieving access control list (ACL) from server\n%1" ).arg( job->errorString() ) );
00499       return;
00500     }
00501 
00502     loadFinished( aclList );
00503   }
00504 }
00505 
00506 void KMail::FolderDiaACLTab::loadListView( const ACLList& aclList )
00507 {
00508   mListView->clear();
00509   for( ACLList::const_iterator it = aclList.begin(); it != aclList.end(); ++it ) {
00510     // -1 means deleted (for cachedimap), don't show those
00511     if ( (*it).permissions > -1 ) {
00512       ListViewItem* item = new ListViewItem( mListView );
00513       item->load( *it );
00514       if ( !mDlg->folder() ) // new folder? everything is new then
00515           item->setModified( true );
00516     }
00517   }
00518 }
00519 
00520 void KMail::FolderDiaACLTab::loadFinished( const ACLList& aclList )
00521 {
00522   loadListView( aclList );
00523   if ( mDlg->folder() ) // not when creating a new folder
00524     mInitialACLList = aclList;
00525   mStack->raiseWidget( mACLWidget );
00526   slotSelectionChanged( mListView->selectedItem() );
00527 }
00528 
00529 void KMail::FolderDiaACLTab::slotEditACL(QListViewItem* item)
00530 {
00531   if ( !item ) return;
00532   bool canAdmin = ( mUserRights & ACLJobs::Administer );
00533   // Same logic as in slotSelectionChanged, but this is also needed for double-click IIRC
00534   if ( canAdmin && mImapAccount && item ) {
00535     // Don't allow users to remove their own admin permissions - there's no way back
00536     ListViewItem* ACLitem = static_cast<ListViewItem *>( item );
00537     if ( mImapAccount->login() == ACLitem->userId() && ACLitem->permissions() == ACLJobs::All )
00538       canAdmin = false;
00539   }
00540   if ( !canAdmin ) return;
00541 
00542   ListViewItem* ACLitem = static_cast<ListViewItem *>( mListView->currentItem() );
00543   ACLEntryDialog dlg( mUserIdFormat, i18n( "Modify Permissions" ), this );
00544   dlg.setValues( ACLitem->userId(), ACLitem->permissions() );
00545   if ( dlg.exec() == QDialog::Accepted ) {
00546     QStringList userIds = dlg.userIds();
00547     Q_ASSERT( !userIds.isEmpty() ); // impossible, the OK button is disabled in that case
00548     ACLitem->setUserId( dlg.userIds().front() );
00549     ACLitem->setPermissions( dlg.permissions() );
00550     ACLitem->setModified( true );
00551     emit changed(true);
00552     if ( userIds.count() > 1 ) { // more emails were added, append them
00553       userIds.pop_front();
00554       addACLs( userIds, dlg.permissions() );
00555     }
00556   }
00557 }
00558 
00559 void KMail::FolderDiaACLTab::slotEditACL()
00560 {
00561   slotEditACL( mListView->currentItem() );
00562 }
00563 
00564 void KMail::FolderDiaACLTab::addACLs( const QStringList& userIds, unsigned int permissions )
00565 {
00566   for( QStringList::const_iterator it = userIds.begin(); it != userIds.end(); ++it ) {
00567     ListViewItem* ACLitem = new ListViewItem( mListView );
00568     ACLitem->setUserId( *it );
00569     ACLitem->setPermissions( permissions );
00570     ACLitem->setModified( true );
00571     ACLitem->setNew( true );
00572   }
00573 }
00574 
00575 void KMail::FolderDiaACLTab::slotAddACL()
00576 {
00577   ACLEntryDialog dlg( mUserIdFormat, i18n( "Add Permissions" ), this );
00578   if ( dlg.exec() == QDialog::Accepted ) {
00579     const QStringList userIds = dlg.userIds();
00580     addACLs( dlg.userIds(), dlg.permissions() );
00581     emit changed(true);
00582   }
00583 }
00584 
00585 void KMail::FolderDiaACLTab::slotSelectionChanged(QListViewItem* item)
00586 {
00587   bool canAdmin = ( mUserRights & ACLJobs::Administer );
00588   bool canAdminThisItem = canAdmin;
00589   if ( canAdmin && mImapAccount && item ) {
00590     // Don't allow users to remove their own admin permissions - there's no way back
00591     ListViewItem* ACLitem = static_cast<ListViewItem *>( item );
00592     if ( mImapAccount->login() == ACLitem->userId() && ACLitem->permissions() == ACLJobs::All )
00593       canAdminThisItem = false;
00594   }
00595 
00596   bool lvVisible = mStack->visibleWidget() == mACLWidget;
00597   mAddACL->setEnabled( lvVisible && canAdmin && !mSaving );
00598   mEditACL->setEnabled( item && lvVisible && canAdminThisItem && !mSaving );
00599   mRemoveACL->setEnabled( item && lvVisible && canAdminThisItem && !mSaving );
00600 }
00601 
00602 void KMail::FolderDiaACLTab::slotRemoveACL()
00603 {
00604   ListViewItem* ACLitem = static_cast<ListViewItem *>( mListView->currentItem() );
00605   if ( !ACLitem )
00606     return;
00607   if ( !ACLitem->isNew() ) {
00608     if ( mImapAccount && mImapAccount->login() == ACLitem->userId() ) {
00609       if ( KMessageBox::Cancel == KMessageBox::warningContinueCancel( topLevelWidget(),
00610          i18n( "Do you really want to remove your own permissions for this folder? You will not be able to access it afterwards." ), i18n( "Remove" ) ) )
00611         return;
00612     }
00613     mRemovedACLs.append( ACLitem->userId() );
00614   }
00615   delete ACLitem;
00616   emit changed(true);
00617 }
00618 
00619 KMail::FolderDiaTab::AcceptStatus KMail::FolderDiaACLTab::accept()
00620 {
00621   if ( !mChanged || !mImapAccount )
00622     return Accepted; // (no change made), ok for accepting the dialog immediately
00623   // If there were changes, we need to apply them first (which is async)
00624   save();
00625   if ( mFolderType == KMFolderTypeCachedImap )
00626     return Accepted; // cached imap: changes saved immediately into the folder
00627   // disconnected imap: async job[s] running
00628   mAccepting = true;
00629   return Delayed;
00630 }
00631 
00632 bool KMail::FolderDiaACLTab::save()
00633 {
00634   if ( !mChanged || !mImapAccount ) // no changes
00635     return true;
00636   assert( mDlg->folder() ); // should have been created already
00637 
00638   // Expand distribution lists. This is necessary because after Apply
00639   // we would otherwise be able to "modify" the permissions for a distr list,
00640   // which wouldn't work since the ACLList and the server only know about the
00641   // individual addresses.
00642   // slotACLChanged would have trouble matching the item too.
00643   // After reloading we'd see the list expanded anyway,
00644   // so this is more consistent.
00645   // But we do it now and not when inserting it, because this allows to
00646   // immediately remove a wrongly inserted distr list without having to
00647   // remove 100 items.
00648   // Now, how to expand them? Playing with listviewitem iterators and inserting
00649   // listviewitems at the same time sounds dangerous, so let's just save into
00650   // ACLList and reload that.
00651   KABC::AddressBook *addressBook = KABC::StdAddressBook::self();
00652   ACLList aclList;
00653   for ( QListViewItem* item = mListView->firstChild(); item; item = item->nextSibling() ) {
00654     ListViewItem* ACLitem = static_cast<ListViewItem *>( item );
00655     ACLitem->save( aclList, addressBook, mUserIdFormat );
00656   }
00657   loadListView( aclList );
00658 
00659   // Now compare with the initial ACLList, because if the user renamed a userid
00660   // we have to add the old userid to the "to be deleted" list.
00661   for( ACLList::ConstIterator init = mInitialACLList.begin(); init != mInitialACLList.end(); ++init ) {
00662     bool isInNewList = false;
00663     QString uid = (*init).userId;
00664     for( ACLList::ConstIterator it = aclList.begin(); it != aclList.end() && !isInNewList; ++it )
00665       isInNewList = uid == (*it).userId;
00666     if ( !isInNewList && !mRemovedACLs.contains(uid) )
00667       mRemovedACLs.append( uid );
00668   }
00669 
00670   for ( QStringList::ConstIterator rit = mRemovedACLs.begin(); rit != mRemovedACLs.end(); ++rit ) {
00671     // We use permissions == -1 to signify deleting. At least on cyrus, setacl(0) or deleteacl are the same,
00672     // but I'm not sure if that's true for all servers.
00673     ACLListEntry entry( *rit, QString::null, -1 );
00674     entry.changed = true;
00675     aclList.append( entry );
00676   }
00677 
00678   // aclList is finally ready. We can save it (dimap) or apply it (imap).
00679 
00680   if ( mFolderType == KMFolderTypeCachedImap ) {
00681     // Apply the changes to the aclList stored in the folder.
00682     // We have to do this now and not before, so that cancel really cancels.
00683     KMFolderCachedImap* folderImap = static_cast<KMFolderCachedImap*>( mDlg->folder()->storage() );
00684     folderImap->setACLList( aclList );
00685     return true;
00686   }
00687 
00688   mACLList = aclList;
00689 
00690   KMFolderImap* parentImap = mDlg->parentFolder() ? static_cast<KMFolderImap*>( mDlg->parentFolder()->storage() ) : 0;
00691 
00692   if ( mDlg->isNewFolder() ) {
00693     // The folder isn't created yet, wait for it
00694     // It's a two-step process (mkdir+listDir) so we wait for the dir listing to be complete
00695     connect( parentImap, SIGNAL( directoryListingFinished(KMFolderImap*) ),
00696              this, SLOT( slotDirectoryListingFinished(KMFolderImap*) ) );
00697   } else {
00698       slotDirectoryListingFinished( parentImap );
00699   }
00700   return true;
00701 }
00702 
00703 void KMail::FolderDiaACLTab::slotDirectoryListingFinished(KMFolderImap* f)
00704 {
00705   if ( !f ||
00706        f != static_cast<KMFolderImap*>( mDlg->parentFolder()->storage() ) ||
00707        !mDlg->folder() ||
00708        !mDlg->folder()->storage() ) {
00709     emit readyForAccept();
00710     return;
00711   }
00712 
00713   // When creating a new folder with online imap, update mImapPath
00714   KMFolderImap* folderImap = static_cast<KMFolderImap*>( mDlg->folder()->storage() );
00715   if ( !folderImap || folderImap->imapPath().isEmpty() )
00716     return;
00717   mImapPath = folderImap->imapPath();
00718 
00719   KIO::Job* job = ACLJobs::multiSetACL( mImapAccount->slave(), imapURL(), mACLList );
00720   ImapAccountBase::jobData jd;
00721   jd.total = 1; jd.done = 0; jd.parent = 0;
00722   mImapAccount->insertJob(job, jd);
00723 
00724   connect(job, SIGNAL(result(KIO::Job *)),
00725           SLOT(slotMultiSetACLResult(KIO::Job *)));
00726   connect(job, SIGNAL(aclChanged( const QString&, int )),
00727           SLOT(slotACLChanged( const QString&, int )) );
00728 }
00729 
00730 void KMail::FolderDiaACLTab::slotMultiSetACLResult(KIO::Job* job)
00731 {
00732   ImapAccountBase::JobIterator it = mImapAccount->findJob( job );
00733   if ( it == mImapAccount->jobsEnd() ) return;
00734   mImapAccount->removeJob( it );
00735 
00736   if ( job->error() ) {
00737     job->showErrorDialog( this );
00738     if ( mAccepting ) {
00739       emit cancelAccept();
00740       mAccepting = false; // don't emit readyForAccept anymore
00741     }
00742   } else {
00743     if ( mAccepting )
00744       emit readyForAccept();
00745   }
00746 }
00747 
00748 void KMail::FolderDiaACLTab::slotACLChanged( const QString& userId, int permissions )
00749 {
00750   // The job indicates success in changing the permissions for this user
00751   // -> we note that it's been done.
00752   bool ok = false;
00753   if ( permissions > -1 ) {
00754     for ( QListViewItem* item = mListView->firstChild(); item; item = item->nextSibling() ) {
00755       ListViewItem* ACLitem = static_cast<ListViewItem *>( item );
00756       if ( ACLitem->userId() == userId ) {
00757         ACLitem->setModified( false );
00758         ACLitem->setNew( false );
00759         ok = true;
00760         break;
00761       }
00762     }
00763   } else {
00764     uint nr = mRemovedACLs.remove( userId );
00765     ok = ( nr > 0 );
00766   }
00767   if ( !ok )
00768     kdWarning(5006) << k_funcinfo << " no item found for userId " << userId << endl;
00769 }
00770 
00771 void KMail::FolderDiaACLTab::slotChanged( bool b )
00772 {
00773   mChanged = b;
00774 }
00775 
00776 bool KMail::FolderDiaACLTab::supports( KMFolder* refFolder )
00777 {
00778   ImapAccountBase* imapAccount = 0;
00779   if ( refFolder->folderType() == KMFolderTypeImap )
00780     imapAccount = static_cast<KMFolderImap*>( refFolder->storage() )->account();
00781   else
00782     imapAccount = static_cast<KMFolderCachedImap*>( refFolder->storage() )->account();
00783   return imapAccount && imapAccount->hasACLSupport(); // support for ACLs (or not tried connecting yet)
00784 }
00785 
00786 #include "folderdiaacltab.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 4 14:42:11 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003