25
25
#include "imapsettings.h"
26
26
#include "mailutil.h"
28
#include <akonadi/collection.h>
29
#include <akonadi/collectionfetchjob.h>
30
#include <akonadi/collectionmodifyjob.h>
31
#include <akonadi/contact/contactgroupexpandjob.h>
32
#include <akonadi/contact/contactgroupsearchjob.h>
34
#include <kmessagebox.h>
35
#include <kpimutils/email.h>
37
#include <QtCore/QAbstractListModel>
38
#include <QtGui/QAction>
39
#include <QtGui/QItemSelectionModel>
28
#include <Akonadi/Collection>
29
#include <Akonadi/CollectionFetchJob>
30
#include <Akonadi/CollectionModifyJob>
31
#include <Akonadi/Contact/ContactGroupExpandJob>
32
#include <Akonadi/Contact/ContactGroupSearchJob>
34
#include <KPIMUtils/Email>
37
#include <KMessageBox>
39
#include <QAbstractListModel>
41
#include <QItemSelectionModel>
41
43
using namespace MailCommon;
57
59
virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const
59
if ( index.row() < 0 || index.row() >= mRights.count() )
61
if ( index.row() < 0 || index.row() >= mRights.count() ) {
62
65
const QPair<QByteArray, KIMAP::Acl::Rights> right = mRights.at( index.row() );
65
return QString::fromLatin1( "%1: %2" ).arg( QString::fromLatin1( right.first ) )
66
.arg( AclUtils::permissionsToUserString( right.second ) );
69
return QString::fromLatin1( right.first );
72
return QVariant( static_cast<int>( right.second ) );
74
case PermissionsTextRole:
75
return AclUtils::permissionsToUserString( right.second );
68
return QString::fromLatin1( "%1: %2" ).
69
arg( QString::fromLatin1( right.first ) ).
70
arg( AclUtils::permissionsToUserString( right.second ) );
73
return QString::fromLatin1( right.first );
76
return QVariant( static_cast<int>( right.second ) );
78
case PermissionsTextRole:
79
return AclUtils::permissionsToUserString( right.second );
85
89
virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole )
87
if ( index.row() < 0 || index.row() >= mRights.count() )
91
if ( index.row() < 0 || index.row() >= mRights.count() ) {
90
95
QPair<QByteArray, KIMAP::Acl::Rights> &right = mRights[ index.row() ];
93
right.first = value.toByteArray();
94
emit dataChanged( index, index );
98
right.second = static_cast<KIMAP::Acl::Rights>( value.toInt() );
99
emit dataChanged( index, index );
98
right.first = value.toByteArray();
99
emit dataChanged( index, index );
102
case PermissionsRole:
103
right.second = static_cast<KIMAP::Acl::Rights>( value.toInt() );
104
emit dataChanged( index, index );
133
139
QMap<QByteArray, KIMAP::Acl::Rights> result;
135
141
typedef QPair<QByteArray, KIMAP::Acl::Rights> RightPair;
136
foreach ( const RightPair &right, mRights )
142
foreach ( const RightPair &right, mRights ) {
137
143
result.insert( right.first, right.second );
143
150
virtual bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() )
145
152
beginInsertRows( parent, row, row + count - 1 );
146
for ( int i = 0; i < count; ++i )
153
for ( int i = 0; i < count; ++i ) {
147
154
mRights.insert( row, qMakePair( QByteArray(), KIMAP::Acl::Rights() ) );
153
161
virtual bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() )
155
163
beginRemoveRows( parent, row, row + count - 1 );
156
for ( int i = 0; i < count; ++i )
164
for ( int i = 0; i < count; ++i ) {
157
165
mRights.remove( row, count );
201
210
const bool itemSelected = !mSelectionModel->selectedIndexes().isEmpty();
203
bool canAdmin = (mUserRights & KIMAP::Acl::Admin);
212
bool canAdmin = ( mUserRights & KIMAP::Acl::Admin );
205
214
bool canAdminThisItem = canAdmin;
206
215
if ( canAdmin && itemSelected ) {
207
216
const QModelIndex index = mSelectionModel->selectedIndexes().first();
208
217
const QString userId = index.data( AclModel::UserIdRole ).toString();
209
const KIMAP::Acl::Rights rights = static_cast<KIMAP::Acl::Rights>( index.data( AclModel::PermissionsRole ).toInt() );
218
const KIMAP::Acl::Rights rights =
219
static_cast<KIMAP::Acl::Rights>( index.data( AclModel::PermissionsRole ).toInt() );
211
221
// Don't allow users to remove their own admin permissions - there's no way back
212
if ( mImapUserName == userId && (rights & KIMAP::Acl::Admin) )
222
if ( mImapUserName == userId && ( rights & KIMAP::Acl::Admin ) ) {
213
223
canAdminThisItem = false;
216
227
mAddAction->setEnabled( canAdmin );
240
252
const QModelIndex index = mSelectionModel->selectedIndexes().first();
241
253
const QString userId = index.data( AclModel::UserIdRole ).toString();
242
const KIMAP::Acl::Rights permissions = static_cast<KIMAP::Acl::Rights>( index.data( AclModel::PermissionsRole ).toInt() );
254
const KIMAP::Acl::Rights permissions =
255
static_cast<KIMAP::Acl::Rights>( index.data( AclModel::PermissionsRole ).toInt() );
244
257
AclEntryDialog dlg;
245
258
dlg.setCaption( i18n( "Edit ACL" ) );
246
259
dlg.setUserId( userId );
247
260
dlg.setPermissions( permissions );
252
266
mModel->setData( index, dlg.userId(), AclModel::UserIdRole );
253
267
mModel->setData( index, static_cast<int>( dlg.permissions() ), AclModel::PermissionsRole );
260
274
const QString userId = index.data( AclModel::UserIdRole ).toString();
262
276
if ( mImapUserName == userId ) {
263
if ( KMessageBox::Cancel == KMessageBox::warningContinueCancel( 0,
264
i18n( "Do you really want to remove your own permissions for this folder? You will not be able to access it afterwards." ), i18n( "Remove" ) ) )
277
if ( KMessageBox::Cancel == KMessageBox::warningContinueCancel(
279
i18n( "Do you really want to remove your own permissions for this folder? "
280
"You will not be able to access it afterwards." ),
284
301
return loginName.left( loginName.indexOf( QLatin1Char( '@' ) ) );
286
303
int pos = serverName.lastIndexOf( QLatin1Char( '.' ) );
287
if ( pos == -1 ) // no qualified domain name, only hostname
304
if ( pos == -1 ) { // no qualified domain name, only hostname
288
305
return QString::fromLatin1( "%1@%2" ).arg( loginName ).arg( serverName );
290
308
pos = serverName.lastIndexOf( QLatin1Char( '.' ), pos - 1 );
291
if ( pos == -1 ) // a simple domain name e.g. mydomain.org
309
if ( pos == -1 ) { // a simple domain name e.g. mydomain.org
292
310
return QString::fromLatin1( "%1@%2" ).arg( loginName ).arg( serverName );
294
312
return QString::fromLatin1( "%1@%2" ).arg( loginName ).arg( serverName.mid( pos + 1 ) );
300
319
mCollection = collection;
301
320
mChanged = false;
303
const MailCommon::ImapAclAttribute *attribute = collection.attribute<MailCommon::ImapAclAttribute>();
322
const MailCommon::ImapAclAttribute *attribute =
323
collection.attribute<MailCommon::ImapAclAttribute>();
304
324
const QMap<QByteArray, KIMAP::Acl::Rights> rights = attribute->rights();
306
OrgKdeAkonadiImapSettingsInterface *imapSettingsInterface = MailCommon::Util::createImapSettingsInterface( collection.resource() );
326
OrgKdeAkonadiImapSettingsInterface *imapSettingsInterface =
327
MailCommon::Util::createImapSettingsInterface( collection.resource() );
308
329
QString loginName;
309
330
QString serverName;
310
331
if ( imapSettingsInterface->isValid() ) {
311
332
QDBusReply<QString> reply = imapSettingsInterface->userName();
312
if ( reply.isValid() )
333
if ( reply.isValid() ) {
313
334
loginName = reply;
315
337
reply = imapSettingsInterface->imapServer();
316
338
if ( reply.isValid() ) {
323
345
mImapUserName = loginName;
324
346
if ( !rights.contains( loginName.toUtf8() ) ) {
325
347
const QString guessedUserName = guessUserName( loginName, serverName );
326
if ( rights.contains( guessedUserName.toUtf8() ) )
348
if ( rights.contains( guessedUserName.toUtf8() ) ) {
327
349
mImapUserName = guessedUserName;
330
353
mUserRights = rights[ mImapUserName.toUtf8() ];
367
390
return d->mCollection;
370
QAbstractItemModel* AclManager::model() const
393
QAbstractItemModel *AclManager::model() const
372
395
return d->mModel;
375
QItemSelectionModel* AclManager::selectionModel() const
398
QItemSelectionModel *AclManager::selectionModel() const
377
return d->mSelectionModel;
400
return d->mSelectionModel;
380
QAction* AclManager::addAction() const
403
QAction *AclManager::addAction() const
382
405
return d->mAddAction;
385
QAction* AclManager::editAction() const
408
QAction *AclManager::editAction() const
387
410
return d->mEditAction;
390
QAction* AclManager::deleteAction() const
413
QAction *AclManager::deleteAction() const
392
415
return d->mDeleteAction;
395
418
void AclManager::save()
397
if ( !d->mCollection.isValid() || !d->mChanged )
420
if ( !d->mCollection.isValid() || !d->mChanged ) {
400
424
// refresh the collection, it might be outdated in the meantime
401
Akonadi::CollectionFetchJob *job = new Akonadi::CollectionFetchJob( d->mCollection, Akonadi::CollectionFetchJob::Base );
425
Akonadi::CollectionFetchJob *job =
426
new Akonadi::CollectionFetchJob( d->mCollection, Akonadi::CollectionFetchJob::Base );
427
if ( !job->exec() ) {
405
if ( job->collections().isEmpty() )
431
if ( job->collections().isEmpty() ) {
408
435
d->mCollection = job->collections().first();
410
437
d->mChanged = false;
412
MailCommon::ImapAclAttribute *attribute = d->mCollection.attribute<MailCommon::ImapAclAttribute>();
439
MailCommon::ImapAclAttribute *attribute =
440
d->mCollection.attribute<MailCommon::ImapAclAttribute>();
413
442
QMap<QByteArray, KIMAP::Acl::Rights> newRights;
415
444
const QMap<QByteArray, KIMAP::Acl::Rights> rights = d->mModel->rights();
428
457
if ( !searchJob->contactGroups().isEmpty() ) { // it has been a distribution list
429
Akonadi::ContactGroupExpandJob *expandJob = new Akonadi::ContactGroupExpandJob( searchJob->contactGroups().first(), this );
458
Akonadi::ContactGroupExpandJob *expandJob =
459
new Akonadi::ContactGroupExpandJob( searchJob->contactGroups().first(), this );
430
460
if ( expandJob->exec() ) {
431
461
foreach ( const KABC::Addressee &contact, expandJob->contacts() ) {
432
const QByteArray rawEmail = KPIMUtils::extractEmailAddress( contact.preferredEmail().toUtf8() );
433
if ( !rawEmail.isEmpty() )
462
const QByteArray rawEmail =
463
KPIMUtils::extractEmailAddress( contact.preferredEmail().toUtf8() );
464
if ( !rawEmail.isEmpty() ) {
434
465
newRights[ rawEmail ] = it.value();
437
469
} else { // it has been a normal contact
438
470
const QByteArray rawEmail = KPIMUtils::extractEmailAddress( it.key() );
439
if ( !rawEmail.isEmpty() )
471
if ( !rawEmail.isEmpty() ) {
440
472
newRights[ rawEmail ] = it.value();