00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "sentactionattribute.h"
00023
00024 #include <QtCore/QDataStream>
00025 #include <QtCore/QSharedData>
00026
00027 using namespace Akonadi;
00028 using namespace MailTransport;
00029
00030 class SentActionAttribute::Action::Private : public QSharedData
00031 {
00032 public:
00033 Private()
00034 : mType( Invalid )
00035 {
00036 }
00037
00038 Private( const Private &other )
00039 : QSharedData( other )
00040 {
00041 mType = other.mType;
00042 mValue = other.mValue;
00043 }
00044
00045 Type mType;
00046 QVariant mValue;
00047 };
00048
00049 SentActionAttribute::Action::Action()
00050 : d( new Private )
00051 {
00052 }
00053
00054 SentActionAttribute::Action::Action( Type type, const QVariant &value )
00055 : d( new Private )
00056 {
00057 d->mType = type;
00058 d->mValue = value;
00059 }
00060
00061 SentActionAttribute::Action::Action( const Action &other )
00062 : d( other.d )
00063 {
00064 }
00065
00066 SentActionAttribute::Action::~Action()
00067 {
00068 }
00069
00070 SentActionAttribute::Action::Type SentActionAttribute::Action::type() const
00071 {
00072 return d->mType;
00073 }
00074
00075 QVariant SentActionAttribute::Action::value() const
00076 {
00077 return d->mValue;
00078 }
00079
00080 SentActionAttribute::Action& SentActionAttribute::Action::operator=( const Action &other )
00081 {
00082 if ( this != &other )
00083 d = other.d;
00084
00085 return *this;
00086 }
00087
00088 bool SentActionAttribute::Action::operator==( const Action &other ) const
00089 {
00090 return ((d->mType == other.d->mType) && (d->mValue == other.d->mValue));
00091 }
00092
00093
00094 class SentActionAttribute::Private
00095 {
00096 public:
00097 Action::List mActions;
00098 };
00099
00100 SentActionAttribute::SentActionAttribute()
00101 : d( new Private )
00102 {
00103 }
00104
00105 SentActionAttribute::~SentActionAttribute()
00106 {
00107 delete d;
00108 }
00109
00110 void SentActionAttribute::addAction( Action::Type type, const QVariant &value )
00111 {
00112 d->mActions.append( Action( type, value ) );
00113 }
00114
00115 SentActionAttribute::Action::List SentActionAttribute::actions() const
00116 {
00117 return d->mActions;
00118 }
00119
00120 SentActionAttribute *SentActionAttribute::clone() const
00121 {
00122 SentActionAttribute *attribute = new SentActionAttribute;
00123 attribute->d->mActions = d->mActions;
00124
00125 return attribute;
00126 }
00127
00128 QByteArray SentActionAttribute::type() const
00129 {
00130 static const QByteArray sType( "SentActionAttribute" );
00131 return sType;
00132 }
00133
00134 QByteArray SentActionAttribute::serialized() const
00135 {
00136 QVariantList list;
00137 foreach ( const Action &action, d->mActions ) {
00138 QVariantMap map;
00139 map.insert( QString::number( action.type() ), action.value() );
00140
00141 list << QVariant( map );
00142 }
00143
00144 QByteArray data;
00145 QDataStream stream( &data, QIODevice::WriteOnly );
00146 stream.setVersion( QDataStream::Qt_4_7 );
00147 stream << list;
00148
00149 return data;
00150 }
00151
00152 void SentActionAttribute::deserialize( const QByteArray &data )
00153 {
00154 d->mActions.clear();
00155
00156 QDataStream stream( data );
00157 stream.setVersion( QDataStream::Qt_4_7 );
00158
00159 QVariantList list;
00160 stream >> list;
00161
00162 foreach ( const QVariant &variant, list ) {
00163 const QVariantMap map = variant.toMap();
00164 QMapIterator<QString, QVariant> it( map );
00165 while ( it.hasNext() ) {
00166 it.next();
00167 d->mActions << Action( static_cast<Action::Type>( it.key().toInt() ), it.value() );
00168 }
00169 }
00170 }