1/*
2 Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
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 "appendjob.h"
21
22#include <KDE/KLocalizedString>
23
24#include "job_p.h"
25#include "message_p.h"
26#include "session_p.h"
27#include "rfccodecs.h"
28
29namespace KIMAP
30{
31 class AppendJobPrivate : public JobPrivate
32 {
33 public:
34 AppendJobPrivate( Session *session, const QString& name ) : JobPrivate( session, name ), uid( 0 ) { }
35 ~AppendJobPrivate() { }
36
37 QString mailBox;
38 QList<QByteArray> flags;
39 KDateTime internalDate;
40 QByteArray content;
41 qint64 uid;
42 };
43}
44
45using namespace KIMAP;
46
47AppendJob::AppendJob( Session *session )
48 : Job( *new AppendJobPrivate( session, i18n( "Append" ) ) )
49{
50}
51
52AppendJob::~AppendJob()
53{
54}
55
56void AppendJob::setMailBox( const QString &mailBox )
57{
58 Q_D( AppendJob );
59 d->mailBox = mailBox;
60}
61
62QString AppendJob::mailBox() const
63{
64 Q_D( const AppendJob );
65 return d->mailBox;
66}
67
68void AppendJob::setFlags( const QList<QByteArray> &flags)
69{
70 Q_D( AppendJob );
71 d->flags = flags;
72}
73
74QList<QByteArray> AppendJob::flags() const
75{
76 Q_D( const AppendJob );
77 return d->flags;
78}
79
80void AppendJob::setInternalDate( const KDateTime &internalDate )
81{
82 Q_D( AppendJob );
83 d->internalDate = internalDate;
84}
85
86KDateTime AppendJob::internalDate() const
87{
88 Q_D( const AppendJob );
89 return d->internalDate;
90}
91
92void AppendJob::setContent( const QByteArray &content )
93{
94 Q_D( AppendJob );
95 d->content = content;
96}
97
98QByteArray AppendJob::content() const
99{
100 Q_D( const AppendJob );
101 return d->content;
102}
103
104qint64 AppendJob::uid() const
105{
106 Q_D( const AppendJob );
107 return d->uid;
108}
109
110void AppendJob::doStart()
111{
112 Q_D( AppendJob );
113
114 QByteArray parameters = '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + '\"';
115
116 if ( !d->flags.isEmpty() ) {
117 parameters += " (";
118 foreach ( const QByteArray &flag, d->flags ) {
119 parameters+= flag + ' ';
120 }
121 parameters.chop( 1 );
122 parameters += ')';
123 }
124
125 if ( !d->internalDate.isNull() ) {
126 parameters += " \"" + d->internalDate.toString( QString::fromAscii( "%d-%:b-%Y %H:%M:%S %z" ) ).toLatin1() + '\"';
127 }
128
129 parameters += " {" + QByteArray::number( d->content.size() ) + '}';
130
131 d->tags << d->sessionInternal()->sendCommand( "APPEND", parameters );
132}
133
134void AppendJob::handleResponse( const Message &response )
135{
136 Q_D( AppendJob );
137
138 for ( QList<Message::Part>::ConstIterator it = response.responseCode.begin();
139 it != response.responseCode.end(); ++it ) {
140 if ( it->toString() == "APPENDUID" ) {
141 it = it + 2;
142 if ( it != response.responseCode.end() ) {
143 d->uid = it->toString().toLongLong();
144 }
145 break;
146 }
147 }
148
149 if ( handleErrorReplies( response ) == NotHandled ) {
150 if ( !response.content.isEmpty() && response.content[0].toString() == "+" ) {
151 d->sessionInternal()->sendData( d->content );
152 }
153 }
154}
155