~ubuntu-branches/ubuntu/raring/kdepim/raring-proposed

« back to all changes in this revision

Viewing changes to mailcommon/aclmanager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-07 07:56:38 UTC
  • mfrom: (0.2.27)
  • Revision ID: package-import@ubuntu.com-20120607075638-0luhdq11z7sgvs4m
Tags: 4:4.8.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "imapsettings.h"
26
26
#include "mailutil.h"
27
27
 
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>
33
 
#include <klocale.h>
34
 
#include <kmessagebox.h>
35
 
#include <kpimutils/email.h>
36
 
 
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>
 
33
 
 
34
#include <KPIMUtils/Email>
 
35
 
 
36
#include <KLocale>
 
37
#include <KMessageBox>
 
38
 
 
39
#include <QAbstractListModel>
 
40
#include <QAction>
 
41
#include <QItemSelectionModel>
40
42
 
41
43
using namespace MailCommon;
42
44
 
56
58
 
57
59
    virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const
58
60
    {
59
 
      if ( index.row() < 0 || index.row() >= mRights.count() )
 
61
      if ( index.row() < 0 || index.row() >= mRights.count() ) {
60
62
        return QVariant();
 
63
      }
61
64
 
62
65
      const QPair<QByteArray, KIMAP::Acl::Rights> right = mRights.at( index.row() );
63
66
      switch ( role ) {
64
 
        case Qt::DisplayRole:
65
 
          return QString::fromLatin1( "%1: %2" ).arg( QString::fromLatin1( right.first ) )
66
 
                                                .arg( AclUtils::permissionsToUserString( right.second ) );
67
 
          break;
68
 
        case UserIdRole:
69
 
          return QString::fromLatin1( right.first );
70
 
          break;
71
 
        case PermissionsRole:
72
 
          return QVariant( static_cast<int>( right.second ) );
73
 
          break;
74
 
        case PermissionsTextRole:
75
 
          return AclUtils::permissionsToUserString( right.second );
76
 
          break;
77
 
        default:
78
 
          return QVariant();
79
 
          break;
 
67
      case Qt::DisplayRole:
 
68
        return QString::fromLatin1( "%1: %2" ).
 
69
                 arg( QString::fromLatin1( right.first ) ).
 
70
                 arg( AclUtils::permissionsToUserString( right.second ) );
 
71
        break;
 
72
      case UserIdRole:
 
73
        return QString::fromLatin1( right.first );
 
74
        break;
 
75
      case PermissionsRole:
 
76
        return QVariant( static_cast<int>( right.second ) );
 
77
        break;
 
78
      case PermissionsTextRole:
 
79
        return AclUtils::permissionsToUserString( right.second );
 
80
        break;
 
81
      default:
 
82
        return QVariant();
 
83
        break;
80
84
      }
81
85
 
82
86
      return QVariant();
84
88
 
85
89
    virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole )
86
90
    {
87
 
      if ( index.row() < 0 || index.row() >= mRights.count() )
 
91
      if ( index.row() < 0 || index.row() >= mRights.count() ) {
88
92
        return false;
 
93
      }
89
94
 
90
95
      QPair<QByteArray, KIMAP::Acl::Rights> &right = mRights[ index.row() ];
91
96
      switch ( role ) {
92
 
        case UserIdRole:
93
 
          right.first = value.toByteArray();
94
 
          emit dataChanged( index, index );
95
 
          return true;
96
 
          break;
97
 
        case PermissionsRole:
98
 
          right.second = static_cast<KIMAP::Acl::Rights>( value.toInt() );
99
 
          emit dataChanged( index, index );
100
 
          return true;
101
 
          break;
102
 
        default:
103
 
          return false;
104
 
          break;
 
97
      case UserIdRole:
 
98
        right.first = value.toByteArray();
 
99
        emit dataChanged( index, index );
 
100
        return true;
 
101
        break;
 
102
      case PermissionsRole:
 
103
        right.second = static_cast<KIMAP::Acl::Rights>( value.toInt() );
 
104
        emit dataChanged( index, index );
 
105
        return true;
 
106
        break;
 
107
      default:
 
108
        return false;
 
109
        break;
105
110
      }
106
111
 
107
112
      return false;
109
114
 
110
115
    virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const
111
116
    {
112
 
      if ( parent.isValid() )
 
117
      if ( parent.isValid() ) {
113
118
        return 0;
114
 
      else
 
119
      } else {
115
120
        return mRights.count();
 
121
      }
116
122
    }
117
123
 
118
124
    void setRights( const QMap<QByteArray, KIMAP::Acl::Rights> &rights )
133
139
      QMap<QByteArray, KIMAP::Acl::Rights> result;
134
140
 
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 );
 
144
      }
138
145
 
139
146
      return result;
140
147
    }
143
150
    virtual bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() )
144
151
    {
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() ) );
 
155
      }
148
156
      endInsertRows();
149
157
 
150
158
      return true;
153
161
    virtual bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() )
154
162
    {
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 );
 
166
      }
158
167
      endRemoveRows();
159
168
 
160
169
      return true;
200
209
    {
201
210
      const bool itemSelected = !mSelectionModel->selectedIndexes().isEmpty();
202
211
 
203
 
      bool canAdmin = (mUserRights & KIMAP::Acl::Admin);
 
212
      bool canAdmin = ( mUserRights & KIMAP::Acl::Admin );
204
213
 
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() );
210
220
 
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;
 
224
        }
214
225
      }
215
226
 
216
227
      mAddAction->setEnabled( canAdmin );
223
234
      AclEntryDialog dlg;
224
235
      dlg.setCaption( i18n( "Add ACL" ) );
225
236
 
226
 
      if ( !dlg.exec() )
 
237
      if ( !dlg.exec() ) {
227
238
        return;
 
239
      }
228
240
 
229
241
      if ( mModel->insertRow( mModel->rowCount() ) ) {
230
242
        const QModelIndex index = mModel->index( mModel->rowCount() - 1, 0 );
239
251
    {
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() );
243
256
 
244
257
      AclEntryDialog dlg;
245
258
      dlg.setCaption( i18n( "Edit ACL" ) );
246
259
      dlg.setUserId( userId );
247
260
      dlg.setPermissions( permissions );
248
261
 
249
 
      if ( !dlg.exec() )
 
262
      if ( !dlg.exec() ) {
250
263
        return;
 
264
      }
251
265
 
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();
261
275
 
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(
 
278
               0,
 
279
               i18n( "Do you really want to remove your own permissions for this folder? "
 
280
                     "You will not be able to access it afterwards." ),
 
281
               i18n( "Remove" ) ) )
265
282
          return;
266
283
      }
267
284
 
284
301
        return loginName.left( loginName.indexOf( QLatin1Char( '@' ) ) );
285
302
      } else {
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 );
 
306
        }
289
307
 
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 );
293
 
        else
 
311
        } else {
294
312
          return QString::fromLatin1( "%1@%2" ).arg( loginName ).arg( serverName.mid( pos + 1 ) );
 
313
        }
295
314
      }
296
315
    }
297
316
 
300
319
      mCollection = collection;
301
320
      mChanged = false;
302
321
 
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();
305
325
 
306
 
      OrgKdeAkonadiImapSettingsInterface *imapSettingsInterface = MailCommon::Util::createImapSettingsInterface( collection.resource() );
307
 
      
 
326
      OrgKdeAkonadiImapSettingsInterface *imapSettingsInterface =
 
327
        MailCommon::Util::createImapSettingsInterface( collection.resource() );
 
328
 
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;
 
335
        }
314
336
 
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;
 
350
        }
328
351
      }
329
352
 
330
353
      mUserRights = rights[ mImapUserName.toUtf8() ];
367
390
  return d->mCollection;
368
391
}
369
392
 
370
 
QAbstractItemModel* AclManager::model() const
 
393
QAbstractItemModel *AclManager::model() const
371
394
{
372
395
  return d->mModel;
373
396
}
374
397
 
375
 
QItemSelectionModel* AclManager::selectionModel() const
 
398
QItemSelectionModel *AclManager::selectionModel() const
376
399
{
377
 
  return d->mSelectionModel; 
 
400
  return d->mSelectionModel;
378
401
}
379
402
 
380
 
QAction* AclManager::addAction() const
 
403
QAction *AclManager::addAction() const
381
404
{
382
405
  return d->mAddAction;
383
406
}
384
407
 
385
 
QAction* AclManager::editAction() const
 
408
QAction *AclManager::editAction() const
386
409
{
387
410
  return d->mEditAction;
388
411
}
389
412
 
390
 
QAction* AclManager::deleteAction() const
 
413
QAction *AclManager::deleteAction() const
391
414
{
392
415
  return d->mDeleteAction;
393
416
}
394
417
 
395
418
void AclManager::save()
396
419
{
397
 
  if ( !d->mCollection.isValid() || !d->mChanged )
 
420
  if ( !d->mCollection.isValid() || !d->mChanged ) {
398
421
    return;
 
422
  }
399
423
 
400
424
  // refresh the collection, it might be outdated in the meantime
401
 
  Akonadi::CollectionFetchJob *job = new Akonadi::CollectionFetchJob( d->mCollection, Akonadi::CollectionFetchJob::Base );
402
 
  if ( !job->exec() )
 
425
  Akonadi::CollectionFetchJob *job =
 
426
    new Akonadi::CollectionFetchJob( d->mCollection, Akonadi::CollectionFetchJob::Base );
 
427
  if ( !job->exec() ) {
403
428
    return;
 
429
  }
404
430
 
405
 
  if ( job->collections().isEmpty() )
 
431
  if ( job->collections().isEmpty() ) {
406
432
    return;
 
433
  }
407
434
 
408
435
  d->mCollection = job->collections().first();
409
436
 
410
437
  d->mChanged = false;
411
438
 
412
 
  MailCommon::ImapAclAttribute *attribute = d->mCollection.attribute<MailCommon::ImapAclAttribute>();
 
439
  MailCommon::ImapAclAttribute *attribute =
 
440
    d->mCollection.attribute<MailCommon::ImapAclAttribute>();
 
441
 
413
442
  QMap<QByteArray, KIMAP::Acl::Rights> newRights;
414
443
 
415
444
  const QMap<QByteArray, KIMAP::Acl::Rights> rights = d->mModel->rights();
426
455
    }
427
456
 
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();
 
466
          }
435
467
        }
436
468
      }
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();
 
473
      }
441
474
    }
442
475
  }
443
476