summaryrefslogtreecommitdiff
path: root/kimap/appendjob.cpp
blob: 7d8104cae57595bdd1400e4f405591e9270b01f1 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
    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 "appendjob.h"

#include <KDE/KLocale>

#include "job_p.h"
#include "message_p.h"
#include "session_p.h"
#include "rfccodecs.h"

namespace KIMAP
{
  class AppendJobPrivate : public JobPrivate
  {
    public:
      AppendJobPrivate( Session *session, const QString& name ) : JobPrivate( session, name ), uid( 0 ) { }
      ~AppendJobPrivate() { }

      QString mailBox;
      QList<QByteArray> flags;
      QByteArray content;
      qint64 uid;
  };
}

using namespace KIMAP;

AppendJob::AppendJob( Session *session )
  : Job( *new AppendJobPrivate(session, i18n("Append")) )
{
}

AppendJob::~AppendJob()
{
}

void AppendJob::setMailBox( const QString &mailBox )
{
  Q_D(AppendJob);
  d->mailBox = mailBox;
}

QString AppendJob::mailBox() const
{
  Q_D(const AppendJob);
  return d->mailBox;
}

void AppendJob::setFlags( const QList<QByteArray> &flags)
{
  Q_D(AppendJob);
  d->flags = flags;
}

QList<QByteArray> AppendJob::flags() const
{
  Q_D(const AppendJob);
  return d->flags;
}

void AppendJob::setContent( const QByteArray &content )
{
  Q_D(AppendJob);
  d->content = content;
}

QByteArray AppendJob::content() const
{
  Q_D(const AppendJob);
  return d->content;
}

qint64 AppendJob::uid() const
{
  Q_D(const AppendJob);
  return d->uid;
}

void AppendJob::doStart()
{
  Q_D(AppendJob);

  QByteArray parameters = '\"'+KIMAP::encodeImapFolderName( d->mailBox.toUtf8() )+'\"';

  if ( !d->flags.isEmpty() ) {
    parameters+=" (";
    foreach ( const QByteArray &flag, d->flags ) {
      parameters+= flag+' ';
    }
    parameters.chop(1);
    parameters+=')';
  }

  parameters+=" {"+QByteArray::number(d->content.size())+'}';

  d->tags << d->sessionInternal()->sendCommand( "APPEND", parameters );
}

void AppendJob::handleResponse( const Message &response )
{
  Q_D(AppendJob);

  for ( QList<Message::Part>::ConstIterator it = response.responseCode.begin();
        it != response.responseCode.end(); ++it ) {
    if ( it->toString()=="APPENDUID" ) {
      it = it + 2;
      if ( it != response.responseCode.end() ) {
        d->uid = it->toString().toLongLong();
      }
      break;
    }
  }

  if (handleErrorReplies(response) == NotHandled ) {
    if ( response.content[0].toString() == "+" ) {
      d->sessionInternal()->sendData( d->content );
    }
  }
}

#include "appendjob.moc"