00001
00031 #include "acljobs.h"
00032 #include <kio/scheduler.h>
00033 #include <kdebug.h>
00034
00035 using namespace KMail;
00036
00037
00038
00039 static unsigned int IMAPRightsToPermission( const QString& str, const KURL& url, const QString& user ) {
00040 unsigned int perm = 0;
00041 uint len = str.length();
00042 for (uint i = 0; i < len; ++i) {
00043 QChar ch = str[i];
00044 switch ( ch.latin1() ) {
00045 case 'l': perm |= ACLJobs::List; break;
00046 case 'r': perm |= ACLJobs::Read; break;
00047 case 's': perm |= ACLJobs::WriteSeenFlag; break;
00048 case 'w': perm |= ACLJobs::WriteFlags; break;
00049 case 'i': perm |= ACLJobs::Insert; break;
00050 case 'p': perm |= ACLJobs::Post; break;
00051 case 'c': perm |= ACLJobs::Create; break;
00052 case 'd': perm |= ACLJobs::Delete; break;
00053 case 'a': perm |= ACLJobs::Administer; break;
00054 default: break;
00055 }
00056 }
00057 if ( ( perm & ACLJobs::Read ) && !( perm & ACLJobs::WriteSeenFlag ) ) {
00058
00059
00060
00061 kdWarning(5006) << "IMAPRightsToPermission: found read (r) but not seen (s). Things will not work well for folder " << url << " and user " << ( user.isEmpty() ? "myself" : user ) << endl;
00062 if ( perm & ACLJobs::Administer )
00063 kdWarning(5006) << "You can change this yourself in the ACL dialog" << endl;
00064 else
00065 kdWarning(5006) << "Ask your admin for 's' permissions." << endl;
00066
00067 }
00068
00069 return perm;
00070 }
00071
00072 static QCString permissionsToIMAPRights( unsigned int permissions ) {
00073 QCString str = "";
00074 if ( permissions & ACLJobs::List )
00075 str += 'l';
00076 if ( permissions & ACLJobs::Read )
00077 str += 'r';
00078 if ( permissions & ACLJobs::WriteSeenFlag )
00079 str += 's';
00080 if ( permissions & ACLJobs::WriteFlags )
00081 str += 'w';
00082 if ( permissions & ACLJobs::Insert )
00083 str += 'i';
00084 if ( permissions & ACLJobs::Post )
00085 str += 'p';
00086 if ( permissions & ACLJobs::Create )
00087 str += 'c';
00088 if ( permissions & ACLJobs::Delete )
00089 str += 'd';
00090 if ( permissions & ACLJobs::Administer )
00091 str += 'a';
00092 return str;
00093 }
00094
00095 #ifndef NDEBUG
00096 QString ACLJobs::permissionsToString( unsigned int permissions )
00097 {
00098 QString str;
00099 if ( permissions & ACLJobs::List )
00100 str += "List ";
00101 if ( permissions & ACLJobs::Read )
00102 str += "Read ";
00103 if ( permissions & ACLJobs::WriteFlags )
00104 str += "Write ";
00105 if ( permissions & ACLJobs::Insert )
00106 str += "Insert ";
00107 if ( permissions & ACLJobs::Post )
00108 str += "Post ";
00109 if ( permissions & ACLJobs::Create )
00110 str += "Create ";
00111 if ( permissions & ACLJobs::Delete )
00112 str += "Delete ";
00113 if ( permissions & ACLJobs::Administer )
00114 str += "Administer ";
00115 if ( !str.isEmpty() )
00116 str.truncate( str.length() - 1 );
00117 return str;
00118 }
00119 #endif
00120
00121 KIO::SimpleJob* ACLJobs::setACL( KIO::Slave* slave, const KURL& url, const QString& user, unsigned int permissions )
00122 {
00123 QString perm = QString::fromLatin1( permissionsToIMAPRights( permissions ) );
00124
00125 QByteArray packedArgs;
00126 QDataStream stream( packedArgs, IO_WriteOnly );
00127 stream << (int)'A' << (int)'S' << url << user << perm;
00128
00129 KIO::SimpleJob* job = KIO::special( url, packedArgs, false );
00130 KIO::Scheduler::assignJobToSlave( slave, job );
00131 return job;
00132 }
00133
00134 ACLJobs::DeleteACLJob* ACLJobs::deleteACL( KIO::Slave* slave, const KURL& url, const QString& user )
00135 {
00136 QByteArray packedArgs;
00137 QDataStream stream( packedArgs, IO_WriteOnly );
00138 stream << (int)'A' << (int)'D' << url << user;
00139
00140 ACLJobs::DeleteACLJob* job = new ACLJobs::DeleteACLJob( url, user, packedArgs, false );
00141 KIO::Scheduler::assignJobToSlave( slave, job );
00142 return job;
00143 }
00144
00145 ACLJobs::GetACLJob* ACLJobs::getACL( KIO::Slave* slave, const KURL& url )
00146 {
00147 QByteArray packedArgs;
00148 QDataStream stream( packedArgs, IO_WriteOnly );
00149 stream << (int)'A' << (int)'G' << url;
00150
00151 ACLJobs::GetACLJob* job = new ACLJobs::GetACLJob( url, packedArgs, false );
00152 KIO::Scheduler::assignJobToSlave( slave, job );
00153 return job;
00154 }
00155
00156 ACLJobs::GetUserRightsJob* ACLJobs::getUserRights( KIO::Slave* slave, const KURL& url )
00157 {
00158 QByteArray packedArgs;
00159 QDataStream stream( packedArgs, IO_WriteOnly );
00160 stream << (int)'A' << (int)'M' << url;
00161
00162 ACLJobs::GetUserRightsJob* job = new ACLJobs::GetUserRightsJob( url, packedArgs, false );
00163 KIO::Scheduler::assignJobToSlave( slave, job );
00164 return job;
00165 }
00166
00167 ACLJobs::GetACLJob::GetACLJob( const KURL& url, const QByteArray &packedArgs,
00168 bool showProgressInfo )
00169 : KIO::SimpleJob( url, KIO::CMD_SPECIAL, packedArgs, showProgressInfo )
00170 {
00171 connect( this, SIGNAL(infoMessage(KIO::Job*,const QString&)),
00172 SLOT(slotInfoMessage(KIO::Job*,const QString&)) );
00173 }
00174
00175 void ACLJobs::GetACLJob::slotInfoMessage( KIO::Job*, const QString& str )
00176 {
00177
00178 QStringList lst = QStringList::split( "\"", str, true );
00179 while ( lst.count() >= 2 )
00180 {
00181 QString user = lst.front(); lst.pop_front();
00182 QString imapRights = lst.front(); lst.pop_front();
00183 unsigned int perm = IMAPRightsToPermission( imapRights, url(), user );
00184 m_entries.append( ACLListEntry( user, imapRights, perm ) );
00185 }
00186 }
00187
00188 ACLJobs::GetUserRightsJob::GetUserRightsJob( const KURL& url, const QByteArray &packedArgs,
00189 bool showProgressInfo )
00190 : KIO::SimpleJob( url, KIO::CMD_SPECIAL, packedArgs, showProgressInfo )
00191 {
00192 connect( this, SIGNAL(infoMessage(KIO::Job*,const QString&)),
00193 SLOT(slotInfoMessage(KIO::Job*,const QString&)) );
00194 }
00195
00196 void ACLJobs::GetUserRightsJob::slotInfoMessage( KIO::Job*, const QString& str )
00197 {
00198
00199 m_permissions = IMAPRightsToPermission( str, url(), QString::null );
00200 }
00201
00202 ACLJobs::DeleteACLJob::DeleteACLJob( const KURL& url, const QString& userId,
00203 const QByteArray &packedArgs,
00204 bool showProgressInfo )
00205 : KIO::SimpleJob( url, KIO::CMD_SPECIAL, packedArgs, showProgressInfo ),
00206 mUserId( userId )
00207 {
00208 }
00209
00211
00212 ACLJobs::MultiSetACLJob::MultiSetACLJob( KIO::Slave* slave, const KURL& url, const ACLList& acl, bool showProgressInfo )
00213 : KIO::Job( showProgressInfo ),
00214 mSlave( slave ),
00215 mUrl( url ), mACLList( acl ), mACLListIterator( mACLList.begin() )
00216 {
00217 QTimer::singleShot(0, this, SLOT(slotStart()));
00218 }
00219
00220 void ACLJobs::MultiSetACLJob::slotStart()
00221 {
00222
00223 while ( mACLListIterator != mACLList.end() && !(*mACLListIterator).changed )
00224 ++mACLListIterator;
00225
00226 if ( mACLListIterator != mACLList.end() )
00227 {
00228 const ACLListEntry& entry = *mACLListIterator;
00229 KIO::Job* job = 0;
00230 if ( entry.permissions > -1 )
00231 job = setACL( mSlave, mUrl, entry.userId, entry.permissions );
00232 else
00233 job = deleteACL( mSlave, mUrl, entry.userId );
00234
00235 addSubjob( job );
00236 } else {
00237 emitResult();
00238 }
00239 }
00240
00241 void ACLJobs::MultiSetACLJob::slotResult( KIO::Job *job )
00242 {
00243 if ( job->error() ) {
00244 KIO::Job::slotResult( job );
00245 return;
00246 }
00247 subjobs.remove(job);
00248 const ACLListEntry& entry = *mACLListIterator;
00249 emit aclChanged( entry.userId, entry.permissions );
00250
00251
00252 ++mACLListIterator;
00253 slotStart();
00254 }
00255
00256 ACLJobs::MultiSetACLJob* ACLJobs::multiSetACL( KIO::Slave* slave, const KURL& url, const ACLList& acl )
00257 {
00258 return new MultiSetACLJob( slave, url, acl, false );
00259 }
00260
00261 #include "acljobs.moc"