1/*
2 Copyright 2009 Constantin Berzan <exit3219@gmail.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "dispatchmodeattribute.h"
21
22#include <KDebug>
23
24#include "akonadi/attributefactory.h"
25
26using namespace Akonadi;
27using namespace MailTransport;
28
29class DispatchModeAttribute::Private
30{
31 public:
32 DispatchMode mMode;
33 QDateTime mDueDate;
34};
35
36DispatchModeAttribute::DispatchModeAttribute( DispatchMode mode )
37 : d( new Private )
38{
39 d->mMode = mode;
40}
41
42DispatchModeAttribute::~DispatchModeAttribute()
43{
44 delete d;
45}
46
47DispatchModeAttribute *DispatchModeAttribute::clone() const
48{
49 DispatchModeAttribute * const cloned = new DispatchModeAttribute( d->mMode );
50 cloned->setSendAfter( d->mDueDate );
51 return cloned;
52}
53
54QByteArray DispatchModeAttribute::type() const
55{
56 static const QByteArray sType( "DispatchModeAttribute" );
57 return sType;
58}
59
60QByteArray DispatchModeAttribute::serialized() const
61{
62 switch ( d->mMode ) {
63 case Automatic:
64 {
65 if ( !d->mDueDate.isValid() ) {
66 return "immediately";
67 } else {
68 return "after" + d->mDueDate.toString( Qt::ISODate ).toLatin1();
69 }
70 }
71 case Manual: return "never";
72 }
73
74 Q_ASSERT( false );
75 return QByteArray(); // suppress control-reaches-end-of-non-void-function warning
76}
77
78void DispatchModeAttribute::deserialize( const QByteArray &data )
79{
80 d->mDueDate = QDateTime();
81 if ( data == "immediately" ) {
82 d->mMode = Automatic;
83 } else if ( data == "never" ) {
84 d->mMode = Manual;
85 } else if ( data.startsWith( QByteArray( "after" ) ) ) {
86 d->mMode = Automatic;
87 d->mDueDate = QDateTime::fromString( QString::fromLatin1( data.mid( 5 ) ), Qt::ISODate );
88 // NOTE: 5 is the strlen of "after".
89 } else {
90 kWarning() << "Failed to deserialize data [" << data << "]";
91 }
92}
93
94DispatchModeAttribute::DispatchMode DispatchModeAttribute::dispatchMode() const
95{
96 return d->mMode;
97}
98
99void DispatchModeAttribute::setDispatchMode( DispatchMode mode )
100{
101 d->mMode = mode;
102}
103
104QDateTime DispatchModeAttribute::sendAfter() const
105{
106 return d->mDueDate;
107}
108
109void DispatchModeAttribute::setSendAfter( const QDateTime &date )
110{
111 d->mDueDate = date;
112}
113
114