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
34 #include <kio/scheduler.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
) {
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;
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";
67 kWarning(5006) <<"Ask your admin for 's' permissions.";
68 // Is the above correct enough to be turned into a KMessageBox?
74 static QByteArray
permissionsToIMAPRights( unsigned int permissions
) {
76 if ( permissions
& ACLJobs::List
)
78 if ( permissions
& ACLJobs::Read
)
80 if ( permissions
& ACLJobs::WriteSeenFlag
)
82 if ( permissions
& ACLJobs::WriteFlags
)
84 if ( permissions
& ACLJobs::Insert
)
86 if ( permissions
& ACLJobs::Post
)
88 if ( permissions
& ACLJobs::Create
)
90 if ( permissions
& ACLJobs::Delete
)
92 if ( permissions
& ACLJobs::Administer
)
98 QString
ACLJobs::permissionsToString( unsigned int permissions
)
101 if ( permissions
& ACLJobs::List
)
103 if ( permissions
& ACLJobs::Read
)
105 if ( permissions
& ACLJobs::WriteFlags
)
107 if ( permissions
& ACLJobs::Insert
)
109 if ( permissions
& ACLJobs::Post
)
111 if ( permissions
& ACLJobs::Create
)
113 if ( permissions
& ACLJobs::Delete
)
115 if ( permissions
& ACLJobs::Administer
)
116 str
+= "Administer ";
117 if ( !str
.isEmpty() )
118 str
.truncate( str
.length() - 1 );
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
);
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
);
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
);
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
);
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
& )
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
& )
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
),
211 ACLJobs::MultiSetACLJob::MultiSetACLJob( KIO::Slave
* slave
, const KUrl
& url
, const ACLList
& acl
)
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
)
225 if ( mACLListIterator
!= mACLList
.end() )
227 const ACLListEntry
& entry
= *mACLListIterator
;
229 if ( entry
.permissions
> -1 )
230 job
= setACL( mSlave
, mUrl
, entry
.userId
, entry
.permissions
);
232 job
= deleteACL( mSlave
, mUrl
, entry
.userId
);
240 void ACLJobs::MultiSetACLJob::slotResult( KJob
*job
)
242 if ( job
->error() ) {
243 KIO::Job::slotResult( job
); // will set the error and emit result(this)
247 const ACLListEntry
& entry
= *mACLListIterator
;
248 emit
aclChanged( entry
.userId
, entry
.permissions
);
250 // Move on to next one
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"