imapaclattribute.cpp 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
    Copyright (C) 2009 Kevin Ottens <ervin@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "imapaclattribute.h"

#include <QtCore/QByteArray>

using namespace Akonadi;

ImapAclAttribute::ImapAclAttribute()
{
}

30 31 32
ImapAclAttribute::ImapAclAttribute( const QMap<QByteArray, KIMAP::Acl::Rights> &rights,
                                    const QMap<QByteArray, KIMAP::Acl::Rights> &oldRights )
  : mRights( rights ), mOldRights( oldRights )
33 34 35
{
}

36
void ImapAclAttribute::setRights( const QMap<QByteArray, KIMAP::Acl::Rights> &rights )
37
{
38
  mOldRights = mRights;
39 40 41 42 43 44 45 46
  mRights = rights;
}

QMap<QByteArray, KIMAP::Acl::Rights> ImapAclAttribute::rights() const
{
  return mRights;
}

47 48 49 50 51
QMap<QByteArray, KIMAP::Acl::Rights> ImapAclAttribute::oldRights() const
{
  return mOldRights;
}

52 53
QByteArray ImapAclAttribute::type() const
{
Laurent Montel's avatar
Laurent Montel committed
54 55
    static const QByteArray sType( "imapacl" );
    return sType;
56 57 58 59
}

Akonadi::Attribute* ImapAclAttribute::clone() const
{
60
  return new ImapAclAttribute( mRights, mOldRights );
61 62 63 64 65 66
}

QByteArray ImapAclAttribute::serialized() const
{
  QByteArray result = "";

67
  bool added = false;
68 69 70 71 72
  foreach ( const QByteArray &id, mRights.keys() ) {
    result+= id;
    result+= ' ';
    result+= KIMAP::Acl::rightsToString( mRights[id] );
    result+= " % "; // We use this separator as '%' is not allowed in keys or values
73
    added = true;
74 75
  }

76 77 78 79 80 81 82 83 84 85 86 87 88
  if ( added )
    result.chop( 3 );

  result+= " %% ";

  added = false;
  foreach ( const QByteArray &id, mOldRights.keys() ) {
    result+= id;
    result+= ' ';
    result+= KIMAP::Acl::rightsToString( mOldRights[id] );
    result+= " % "; // We use this separator as '%' is not allowed in keys or values
    added = true;
  }
89

90 91 92 93
  if ( added )
    result.chop( 3 );

  return result;
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static void fillRightsMap( const QList<QByteArray> &rights, QMap <QByteArray, KIMAP::Acl::Rights> &map )
{
  foreach ( const QByteArray &right, rights ) {
    const QByteArray trimmed = right.trimmed();
    const int wsIndex = trimmed.indexOf( ' ' );
    const QByteArray id = trimmed.mid( 0, wsIndex ).trimmed();
    if ( !id.isEmpty() ) {
      const bool noValue = ( wsIndex == -1 );
      if ( noValue ) {
        map[id] = KIMAP::Acl::None;
      } else {
        const QByteArray value = trimmed.mid( wsIndex + 1, right.length() - wsIndex ).trimmed();
        map[id] = KIMAP::Acl::rightsFromString( value );
      }
    }
  }
}

114 115 116
void ImapAclAttribute::deserialize( const QByteArray &data )
{
  mRights.clear();
117 118 119 120 121 122 123
  mOldRights.clear();
  const int pos = data.indexOf( " %% " );
  if ( pos == -1 )
    return;

  const QByteArray leftPart = data.left( pos );
  const QByteArray rightPart = data.mid( pos + 4 );
124 125
  fillRightsMap( leftPart.split( '%' ), mRights );
  fillRightsMap( rightPart.split( '%' ), mOldRights );
126
}