kmail

folderdiaacltab.cpp

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