1/*
2 Copyright (c) 2009 Andras Mantia <amantia@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 "copyjob.h"
21
22#include <KDE/KLocalizedString>
23#include <KDE/KDebug>
24
25#include "job_p.h"
26#include "message_p.h"
27#include "session_p.h"
28#include "rfccodecs.h"
29
30//TODO: when custom error codes are introduced, handle the NO [TRYCREATE] response
31
32namespace KIMAP
33{
34 class CopyJobPrivate : public JobPrivate
35 {
36 public:
37 CopyJobPrivate( Session *session, const QString& name ) : JobPrivate( session, name ) { }
38 ~CopyJobPrivate() { }
39
40 QString mailBox;
41 ImapSet set;
42 bool uidBased;
43 ImapSet resultingUids;
44 };
45}
46
47using namespace KIMAP;
48
49CopyJob::CopyJob( Session *session )
50 : Job( *new CopyJobPrivate( session, i18n( "Copy" ) ) )
51{
52 Q_D( CopyJob );
53 d->uidBased = false;
54}
55
56CopyJob::~CopyJob()
57{
58}
59
60void CopyJob::setMailBox( const QString &mailBox )
61{
62 Q_D( CopyJob );
63 d->mailBox = mailBox;
64}
65
66QString CopyJob::mailBox() const
67{
68 Q_D( const CopyJob );
69 return d->mailBox;
70}
71
72void CopyJob::setSequenceSet( const ImapSet &set )
73{
74 Q_D( CopyJob );
75 d->set = set;
76}
77
78ImapSet CopyJob::sequenceSet() const
79{
80 Q_D( const CopyJob );
81 return d->set;
82}
83
84void CopyJob::setUidBased( bool uidBased )
85{
86 Q_D( CopyJob );
87 d->uidBased = uidBased;
88}
89
90bool CopyJob::isUidBased() const
91{
92 Q_D( const CopyJob );
93 return d->uidBased;
94}
95
96ImapSet CopyJob::resultingUids() const
97{
98 Q_D( const CopyJob );
99 return d->resultingUids;
100}
101
102void CopyJob::doStart()
103{
104 Q_D( CopyJob );
105
106 QByteArray parameters = d->set.toImapSequenceSet()+' ';
107 parameters += '\"' + KIMAP::encodeImapFolderName( d->mailBox.toUtf8() ) + '\"';
108
109 QByteArray command = "COPY";
110 if ( d->uidBased ) {
111 command = "UID "+command;
112 }
113
114 d->tags << d->sessionInternal()->sendCommand( command, parameters );
115}
116
117void CopyJob::handleResponse( const Message &response )
118{
119 Q_D( CopyJob );
120
121 for ( QList<Message::Part>::ConstIterator it = response.responseCode.begin();
122 it != response.responseCode.end(); ++it ) {
123 if ( it->toString() == "COPYUID" ) {
124 it = it + 3;
125 if ( it < response.responseCode.end() ) {
126 d->resultingUids = ImapSet::fromImapSequenceSet( it->toString() );
127 }
128 break;
129 }
130 }
131
132 handleErrorReplies( response );
133}
134