Backport r950340 | aacid | 2009-04-06 23:21:18 +0200 (Mon, 06 Apr 2009) | 4 lines
[kdepim.git] / kmail / acljobs.cpp
blob63439ba084fd05103c09fa1d9f9ab3220fe0bf87
1 /**
2 * acljobs.cpp
4 * Copyright (c) 2004 David Faure <faure@kde.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give
21 * permission to link the code of this program with any edition of
22 * the Qt library by Trolltech AS, Norway (or with modified versions
23 * of Qt that use the same license as Qt), and distribute linked
24 * combinations including the two. You must obey the GNU General
25 * Public License in all respects for all of the code used other than
26 * Qt. If you modify this file, you may extend this exception to
27 * your version of the file, but you are not obligated to do so. If
28 * you do not wish to do so, delete this exception statement from
29 * your version.
33 #include "acljobs.h"
34 #include <kio/scheduler.h>
35 #include <kdebug.h>
37 using namespace KMail;
39 // Convert str to an ACLPermissions value.
40 // url and user are there only for the error message
41 static unsigned int IMAPRightsToPermission( const QString& str, const KUrl& url, const QString& user ) {
42 unsigned int perm = 0;
43 uint len = str.length();
44 for (uint i = 0; i < len; ++i) {
45 QChar ch = str[i];
46 switch ( ch.toLatin1() ) {
47 case 'l': perm |= ACLJobs::List; break;
48 case 'r': perm |= ACLJobs::Read; break;
49 case 's': perm |= ACLJobs::WriteSeenFlag; break;
50 case 'w': perm |= ACLJobs::WriteFlags; break;
51 case 'i': perm |= ACLJobs::Insert; break;
52 case 'p': perm |= ACLJobs::Post; break;
53 case 'c': perm |= ACLJobs::Create; break;
54 case 'd': perm |= ACLJobs::Delete; break;
55 case 'a': perm |= ACLJobs::Administer; break;
56 default: break;
59 if ( ( perm & ACLJobs::Read ) && !( perm & ACLJobs::WriteSeenFlag ) ) {
60 // Reading without 'seen' is, well, annoying. Unusable, even.
61 // So we treat 'rs' as a single one.
62 // But if the permissions were set out of kmail, better check that both are set
63 kWarning(5006) <<"IMAPRightsToPermission: found read (r) but not seen (s). Things will not work well for folder" << url <<" and user" << ( user.isEmpty() ?"myself" : user );
64 if ( perm & ACLJobs::Administer )
65 kWarning(5006) <<"You can change this yourself in the ACL dialog";
66 else
67 kWarning(5006) <<"Ask your admin for 's' permissions.";
68 // Is the above correct enough to be turned into a KMessageBox?
71 return perm;
74 static QByteArray permissionsToIMAPRights( unsigned int permissions ) {
75 QByteArray str = "";
76 if ( permissions & ACLJobs::List )
77 str += 'l';
78 if ( permissions & ACLJobs::Read )
79 str += 'r';
80 if ( permissions & ACLJobs::WriteSeenFlag )
81 str += 's';
82 if ( permissions & ACLJobs::WriteFlags )
83 str += 'w';
84 if ( permissions & ACLJobs::Insert )
85 str += 'i';
86 if ( permissions & ACLJobs::Post )
87 str += 'p';
88 if ( permissions & ACLJobs::Create )
89 str += 'c';
90 if ( permissions & ACLJobs::Delete )
91 str += 'd';
92 if ( permissions & ACLJobs::Administer )
93 str += 'a';
94 return str;
97 #ifndef NDEBUG
98 QString ACLJobs::permissionsToString( unsigned int permissions )
100 QString str;
101 if ( permissions & ACLJobs::List )
102 str += "List ";
103 if ( permissions & ACLJobs::Read )
104 str += "Read ";
105 if ( permissions & ACLJobs::WriteFlags )
106 str += "Write ";
107 if ( permissions & ACLJobs::Insert )
108 str += "Insert ";
109 if ( permissions & ACLJobs::Post )
110 str += "Post ";
111 if ( permissions & ACLJobs::Create )
112 str += "Create ";
113 if ( permissions & ACLJobs::Delete )
114 str += "Delete ";
115 if ( permissions & ACLJobs::Administer )
116 str += "Administer ";
117 if ( !str.isEmpty() )
118 str.truncate( str.length() - 1 );
119 return str;
121 #endif
123 KIO::SimpleJob* ACLJobs::setACL( KIO::Slave* slave, const KUrl& url, const QString& user, unsigned int permissions )
125 QString perm = QString::fromLatin1( permissionsToIMAPRights( permissions ) );
127 QByteArray packedArgs;
128 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
129 stream << (int)'A' << (int)'S' << url << user << perm;
131 KIO::SimpleJob* job = KIO::special( url, packedArgs, KIO::HideProgressInfo );
132 KIO::Scheduler::assignJobToSlave( slave, job );
133 return job;
136 ACLJobs::DeleteACLJob* ACLJobs::deleteACL( KIO::Slave* slave, const KUrl& url, const QString& user )
138 QByteArray packedArgs;
139 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
140 stream << (int)'A' << (int)'D' << url << user;
142 ACLJobs::DeleteACLJob* job = new ACLJobs::DeleteACLJob( url, user, packedArgs);
143 KIO::Scheduler::assignJobToSlave( slave, job );
144 return job;
147 ACLJobs::GetACLJob* ACLJobs::getACL( KIO::Slave* slave, const KUrl& url )
149 QByteArray packedArgs;
150 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
151 stream << (int)'A' << (int)'G' << url;
153 ACLJobs::GetACLJob* job = new ACLJobs::GetACLJob( url, packedArgs);
154 KIO::Scheduler::assignJobToSlave( slave, job );
155 return job;
158 ACLJobs::GetUserRightsJob* ACLJobs::getUserRights( KIO::Slave* slave, const KUrl& url )
160 QByteArray packedArgs;
161 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
162 stream << (int)'A' << (int)'M' << url;
164 ACLJobs::GetUserRightsJob* job = new ACLJobs::GetUserRightsJob( url, packedArgs );
165 KIO::Scheduler::assignJobToSlave( slave, job );
166 return job;
169 ACLJobs::GetACLJob::GetACLJob( const KUrl& url, const QByteArray &packedArgs)
170 : KIO::SpecialJob( url, packedArgs )
172 connect( this, SIGNAL(infoMessage(KJob*,const QString&,const QString&)),
173 SLOT(slotInfoMessage(KJob*,const QString&,const QString&)) );
176 void ACLJobs::GetACLJob::slotInfoMessage( KJob*, const QString& str,const QString& )
178 // Parse the result
179 QStringList lst = str.split( '\"', QString::KeepEmptyParts );
180 while ( lst.count() >= 2 ) // we take items 2 by 2
182 QString user = lst.front(); lst.pop_front();
183 QString imapRights = lst.front(); lst.pop_front();
184 unsigned int perm = IMAPRightsToPermission( imapRights, url(), user );
185 m_entries.append( ACLListEntry( user, imapRights, perm ) );
189 ACLJobs::GetUserRightsJob::GetUserRightsJob( const KUrl& url, const QByteArray &packedArgs)
190 : KIO::SpecialJob( url, packedArgs)
192 connect( this, SIGNAL(infoMessage(KJob*,const QString&,const QString&)),
193 SLOT(slotInfoMessage(KJob*,const QString&,const QString&)) );
196 void ACLJobs::GetUserRightsJob::slotInfoMessage( KJob*, const QString& str,const QString& )
198 // Parse the result
199 m_permissions = IMAPRightsToPermission( str, url(), QString() );
202 ACLJobs::DeleteACLJob::DeleteACLJob( const KUrl& url, const QString& userId,
203 const QByteArray &packedArgs)
204 : KIO::SpecialJob( url, packedArgs ),
205 mUserId( userId )
209 ////
211 ACLJobs::MultiSetACLJob::MultiSetACLJob( KIO::Slave* slave, const KUrl& url, const ACLList& acl )
212 : KIO::Job(),
213 mSlave( slave ),
214 mUrl( url ), mACLList( acl ), mACLListIterator( mACLList.begin() )
216 QTimer::singleShot(0, this, SLOT(slotStart()));
219 void ACLJobs::MultiSetACLJob::slotStart()
221 // Skip over unchanged entries
222 while ( mACLListIterator != mACLList.end() && !(*mACLListIterator).changed )
223 ++mACLListIterator;
225 if ( mACLListIterator != mACLList.end() )
227 const ACLListEntry& entry = *mACLListIterator;
228 KIO::Job* job = 0;
229 if ( entry.permissions > -1 )
230 job = setACL( mSlave, mUrl, entry.userId, entry.permissions );
231 else
232 job = deleteACL( mSlave, mUrl, entry.userId );
234 addSubjob( job );
235 } else { // done!
236 emitResult();
240 void ACLJobs::MultiSetACLJob::slotResult( KJob *job )
242 if ( job->error() ) {
243 KIO::Job::slotResult( job ); // will set the error and emit result(this)
244 return;
246 removeSubjob(job);
247 const ACLListEntry& entry = *mACLListIterator;
248 emit aclChanged( entry.userId, entry.permissions );
250 // Move on to next one
251 ++mACLListIterator;
252 slotStart();
255 ACLJobs::MultiSetACLJob* ACLJobs::multiSetACL( KIO::Slave* slave, const KUrl& url, const ACLList& acl )
257 return new MultiSetACLJob( slave, url, acl);
260 #include "acljobs.moc"