kmail

acljobs.cpp
1 
31 #include "acljobs.h"
32 #include <tdeio/scheduler.h>
33 #include <kdebug.h>
34 
35 using namespace KMail;
36 
37 // Convert str to an ACLPermissions value.
38 // url and user are there only for the error message
39 static unsigned int IMAPRightsToPermission( const TQString& str, const KURL& url, const TQString& user ) {
40  unsigned int perm = 0;
41  uint len = str.length();
42  for (uint i = 0; i < len; ++i) {
43  TQChar ch = str[i];
44  switch ( ch.latin1() ) {
45  case 'l': perm |= ACLJobs::List; break;
46  case 'r': perm |= ACLJobs::Read; break;
47  case 's': perm |= ACLJobs::WriteSeenFlag; break;
48  case 'w': perm |= ACLJobs::WriteFlags; break;
49  case 'i': perm |= ACLJobs::Insert; break;
50  case 'p': perm |= ACLJobs::Post; break;
51  case 'k': // fall through
52  case 'c': perm |= ACLJobs::Create; break;
53  case 'x': // fall through
54  case 'd': perm |= ACLJobs::Delete; break;
55  case 'a': perm |= ACLJobs::Administer; break;
56  default: break;
57  }
58  }
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  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;
64  if ( perm & ACLJobs::Administer )
65  kdWarning(5006) << "You can change this yourself in the ACL dialog" << endl;
66  else
67  kdWarning(5006) << "Ask your admin for 's' permissions." << endl;
68  // Is the above correct enough to be turned into a KMessageBox?
69  }
70 
71  return perm;
72 }
73 
74 static TQCString permissionsToIMAPRights( unsigned int permissions ) {
75  TQCString 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;
95 }
96 
97 #ifndef NDEBUG
98 TQString ACLJobs::permissionsToString( unsigned int permissions )
99 {
100  TQString 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;
120 }
121 #endif
122 
123 TDEIO::SimpleJob* ACLJobs::setACL( TDEIO::Slave* slave, const KURL& url, const TQString& user, unsigned int permissions )
124 {
125  TQString perm = TQString::fromLatin1( permissionsToIMAPRights( permissions ) );
126 
127  TQByteArray packedArgs;
128  TQDataStream stream( packedArgs, IO_WriteOnly );
129  stream << (int)'A' << (int)'S' << url << user << perm;
130 
131  TDEIO::SimpleJob* job = TDEIO::special( url, packedArgs, false );
132  TDEIO::Scheduler::assignJobToSlave( slave, job );
133  return job;
134 }
135 
136 ACLJobs::DeleteACLJob* ACLJobs::deleteACL( TDEIO::Slave* slave, const KURL& url, const TQString& user )
137 {
138  TQByteArray packedArgs;
139  TQDataStream stream( packedArgs, IO_WriteOnly );
140  stream << (int)'A' << (int)'D' << url << user;
141 
142  ACLJobs::DeleteACLJob* job = new ACLJobs::DeleteACLJob( url, user, packedArgs, false );
143  TDEIO::Scheduler::assignJobToSlave( slave, job );
144  return job;
145 }
146 
147 ACLJobs::GetACLJob* ACLJobs::getACL( TDEIO::Slave* slave, const KURL& url )
148 {
149  TQByteArray packedArgs;
150  TQDataStream stream( packedArgs, IO_WriteOnly );
151  stream << (int)'A' << (int)'G' << url;
152 
153  ACLJobs::GetACLJob* job = new ACLJobs::GetACLJob( url, packedArgs, false );
154  TDEIO::Scheduler::assignJobToSlave( slave, job );
155  return job;
156 }
157 
158 ACLJobs::GetUserRightsJob* ACLJobs::getUserRights( TDEIO::Slave* slave, const KURL& url )
159 {
160  TQByteArray packedArgs;
161  TQDataStream stream( packedArgs, IO_WriteOnly );
162  stream << (int)'A' << (int)'M' << url;
163 
164  ACLJobs::GetUserRightsJob* job = new ACLJobs::GetUserRightsJob( url, packedArgs, false );
165  TDEIO::Scheduler::assignJobToSlave( slave, job );
166  return job;
167 }
168 
169 ACLJobs::GetACLJob::GetACLJob( const KURL& url, const TQByteArray &packedArgs,
170  bool showProgressInfo )
171  : TDEIO::SimpleJob( url, TDEIO::CMD_SPECIAL, packedArgs, showProgressInfo )
172 {
173  connect( this, TQ_SIGNAL(infoMessage(TDEIO::Job*,const TQString&)),
174  TQ_SLOT(slotInfoMessage(TDEIO::Job*,const TQString&)) );
175 }
176 
177 void ACLJobs::GetACLJob::slotInfoMessage( TDEIO::Job*, const TQString& str )
178 {
179  // Parse the result
180  TQStringList lst = TQStringList::split( "\"", str, true );
181  while ( lst.count() >= 2 ) // we take items 2 by 2
182  {
183  TQString user = lst.front(); lst.pop_front();
184  TQString imapRights = lst.front(); lst.pop_front();
185  unsigned int perm = IMAPRightsToPermission( imapRights, url(), user );
186  m_entries.append( ACLListEntry( user, imapRights, perm ) );
187  }
188 }
189 
190 ACLJobs::GetUserRightsJob::GetUserRightsJob( const KURL& url, const TQByteArray &packedArgs,
191  bool showProgressInfo )
192  : TDEIO::SimpleJob( url, TDEIO::CMD_SPECIAL, packedArgs, showProgressInfo )
193 {
194  connect( this, TQ_SIGNAL(infoMessage(TDEIO::Job*,const TQString&)),
195  TQ_SLOT(slotInfoMessage(TDEIO::Job*,const TQString&)) );
196 }
197 
198 void ACLJobs::GetUserRightsJob::slotInfoMessage( TDEIO::Job*, const TQString& str )
199 {
200  // Parse the result
201  m_permissions = IMAPRightsToPermission( str, url(), TQString() );
202 }
203 
204 ACLJobs::DeleteACLJob::DeleteACLJob( const KURL& url, const TQString& userId,
205  const TQByteArray &packedArgs,
206  bool showProgressInfo )
207  : TDEIO::SimpleJob( url, TDEIO::CMD_SPECIAL, packedArgs, showProgressInfo ),
208  mUserId( userId )
209 {
210 }
211 
213 
214 ACLJobs::MultiSetACLJob::MultiSetACLJob( TDEIO::Slave* slave, const KURL& url, const ACLList& acl, bool showProgressInfo )
215  : TDEIO::Job( showProgressInfo ),
216  mSlave( slave ),
217  mUrl( url ), mACLList( acl ), mACLListIterator( mACLList.begin() )
218 {
219  TQTimer::singleShot(0, this, TQ_SLOT(slotStart()));
220 }
221 
222 void ACLJobs::MultiSetACLJob::slotStart()
223 {
224  // Skip over unchanged entries
225  while ( mACLListIterator != mACLList.end() && !(*mACLListIterator).changed )
226  ++mACLListIterator;
227 
228  if ( mACLListIterator != mACLList.end() )
229  {
230  const ACLListEntry& entry = *mACLListIterator;
231  TDEIO::Job* job = 0;
232  if ( entry.permissions > -1 )
233  job = setACL( mSlave, mUrl, entry.userId, entry.permissions );
234  else
235  job = deleteACL( mSlave, mUrl, entry.userId );
236 
237  addSubjob( job );
238  } else { // done!
239  emitResult();
240  }
241 }
242 
243 void ACLJobs::MultiSetACLJob::slotResult( TDEIO::Job *job )
244 {
245  if ( job->error() ) {
246  TDEIO::Job::slotResult( job ); // will set the error and emit result(this)
247  return;
248  }
249  subjobs.remove(job);
250  const ACLListEntry& entry = *mACLListIterator;
251  emit aclChanged( entry.userId, entry.permissions );
252 
253  // Move on to next one
254  ++mACLListIterator;
255  slotStart();
256 }
257 
258 ACLJobs::MultiSetACLJob* ACLJobs::multiSetACL( TDEIO::Slave* slave, const KURL& url, const ACLList& acl )
259 {
260  return new MultiSetACLJob( slave, url, acl, false /*showProgressInfo*/ );
261 }
262 
263 #include "acljobs.moc"
Delete the permissions for a given user on a given url This class only exists to store the userid in ...
Definition: acljobs.h:143
List all ACLs for a given url.
Definition: acljobs.h:109
Get the users' rights for a given url.
Definition: acljobs.h:126
Set and delete a list of permissions for different users on a given url.
Definition: acljobs.h:158
DeleteACLJob * deleteACL(TDEIO::Slave *slave, const KURL &url, const TQString &user)
Delete the permissions for a given user on a given url.
Definition: acljobs.cpp:136
TDEIO::SimpleJob * setACL(TDEIO::Slave *slave, const KURL &url, const TQString &user, unsigned int permissions)
Set the permissions for a given user on a given url.
Definition: acljobs.cpp:123
GetUserRightsJob * getUserRights(TDEIO::Slave *slave, const KURL &url)
Get the users' rights for a given url.
Definition: acljobs.cpp:158
MultiSetACLJob * multiSetACL(TDEIO::Slave *slave, const KURL &url, const ACLList &acl)
Set and delete a list of permissions for different users on a given url.
Definition: acljobs.cpp:258
GetACLJob * getACL(TDEIO::Slave *slave, const KURL &url)
List all ACLs for a given url.
Definition: acljobs.cpp:147
folderdiaquotatab.h
Definition: aboutdata.cpp:40
One entry in the ACL list: user and permissions.
Definition: acljobs.h:41
int permissions
based on the ACLPermissions enum
Definition: acljobs.h:47