1 | /* |
2 | Copyright (C) 2010 Klarälvdalens Datakonsult AB, |
3 | a KDAB Group company, info@kdab.net, |
4 | author Tobias Koenig <tokoe@kdab.com> |
5 | |
6 | This library is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU Library General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or (at your |
9 | option) any later version. |
10 | |
11 | This library is distributed in the hope that it will be useful, but WITHOUT |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
14 | License for more details. |
15 | |
16 | You should have received a copy of the GNU Library General Public License |
17 | along with this library; see the file COPYING.LIB. If not, write to the |
18 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | 02110-1301, USA. |
20 | */ |
21 | |
22 | #include "sentactionattribute.h" |
23 | |
24 | #include <QtCore/QDataStream> |
25 | #include <QtCore/QSharedData> |
26 | |
27 | using namespace Akonadi; |
28 | using namespace MailTransport; |
29 | |
30 | class SentActionAttribute::Action::Private : public QSharedData |
31 | { |
32 | public: |
33 | Private() |
34 | : mType( Invalid ) |
35 | { |
36 | } |
37 | |
38 | Private( const Private &other ) |
39 | : QSharedData( other ) |
40 | { |
41 | mType = other.mType; |
42 | mValue = other.mValue; |
43 | } |
44 | |
45 | Type mType; |
46 | QVariant mValue; |
47 | }; |
48 | |
49 | SentActionAttribute::Action::Action() |
50 | : d( new Private ) |
51 | { |
52 | } |
53 | |
54 | SentActionAttribute::Action::Action( Type type, const QVariant &value ) |
55 | : d( new Private ) |
56 | { |
57 | d->mType = type; |
58 | d->mValue = value; |
59 | } |
60 | |
61 | SentActionAttribute::Action::Action( const Action &other ) |
62 | : d( other.d ) |
63 | { |
64 | } |
65 | |
66 | SentActionAttribute::Action::~Action() |
67 | { |
68 | } |
69 | |
70 | SentActionAttribute::Action::Type SentActionAttribute::Action::type() const |
71 | { |
72 | return d->mType; |
73 | } |
74 | |
75 | QVariant SentActionAttribute::Action::value() const |
76 | { |
77 | return d->mValue; |
78 | } |
79 | |
80 | SentActionAttribute::Action &SentActionAttribute::Action::operator=( const Action &other ) |
81 | { |
82 | if ( this != &other ) { |
83 | d = other.d; |
84 | } |
85 | |
86 | return *this; |
87 | } |
88 | |
89 | bool SentActionAttribute::Action::operator==( const Action &other ) const |
90 | { |
91 | return ( ( d->mType == other.d->mType ) && ( d->mValue == other.d->mValue ) ); |
92 | } |
93 | |
94 | class SentActionAttribute::Private |
95 | { |
96 | public: |
97 | Action::List mActions; |
98 | }; |
99 | |
100 | SentActionAttribute::SentActionAttribute() |
101 | : d( new Private ) |
102 | { |
103 | } |
104 | |
105 | SentActionAttribute::~SentActionAttribute() |
106 | { |
107 | delete d; |
108 | } |
109 | |
110 | void SentActionAttribute::addAction( Action::Type type, const QVariant &value ) |
111 | { |
112 | d->mActions.append( Action( type, value ) ); |
113 | } |
114 | |
115 | SentActionAttribute::Action::List SentActionAttribute::actions() const |
116 | { |
117 | return d->mActions; |
118 | } |
119 | |
120 | SentActionAttribute *SentActionAttribute::clone() const |
121 | { |
122 | SentActionAttribute *attribute = new SentActionAttribute; |
123 | attribute->d->mActions = d->mActions; |
124 | |
125 | return attribute; |
126 | } |
127 | |
128 | QByteArray SentActionAttribute::type() const |
129 | { |
130 | static const QByteArray sType( "SentActionAttribute" ); |
131 | return sType; |
132 | } |
133 | |
134 | QByteArray SentActionAttribute::serialized() const |
135 | { |
136 | QVariantList list; |
137 | foreach ( const Action &action, d->mActions ) { |
138 | QVariantMap map; |
139 | map.insert( QString::number( action.type() ), action.value() ); |
140 | |
141 | list << QVariant( map ); |
142 | } |
143 | |
144 | QByteArray data; |
145 | QDataStream stream( &data, QIODevice::WriteOnly ); |
146 | stream.setVersion( QDataStream::Qt_4_6 ); |
147 | stream << list; |
148 | |
149 | return data; |
150 | } |
151 | |
152 | void SentActionAttribute::deserialize( const QByteArray &data ) |
153 | { |
154 | d->mActions.clear(); |
155 | |
156 | QDataStream stream( data ); |
157 | stream.setVersion( QDataStream::Qt_4_6 ); |
158 | |
159 | QVariantList list; |
160 | stream >> list; |
161 | |
162 | foreach ( const QVariant &variant, list ) { |
163 | const QVariantMap map = variant.toMap(); |
164 | QMapIterator<QString, QVariant> it( map ); |
165 | while ( it.hasNext() ) { |
166 | it.next(); |
167 | d->mActions << Action( static_cast<Action::Type>( it.key().toInt() ), it.value() ); |
168 | } |
169 | } |
170 | } |
171 | |