Merged KIO::JobFlags branch
[kdepim.git] / kmail / annotationjobs.cpp
blob9ea994e017e04f323fed5ea0fb7a95bd74244542
1 /**
2 * annotationjobs.cpp
4 * Copyright (c) 2004 David Faure <faure@kde.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give
21 * permission to link the code of this program with any edition of
22 * the Qt library by Trolltech AS, Norway (or with modified versions
23 * of Qt that use the same license as Qt), and distribute linked
24 * combinations including the two. You must obey the GNU General
25 * Public License in all respects for all of the code used other than
26 * Qt. If you modify this file, you may extend this exception to
27 * your version of the file, but you are not obligated to do so. If
28 * you do not wish to do so, delete this exception statement from
29 * your version.
33 #include "annotationjobs.h"
34 #include <kio/scheduler.h>
35 #include <kdebug.h>
37 using namespace KMail;
39 KIO::SimpleJob* AnnotationJobs::setAnnotation(
40 KIO::Slave* slave, const KUrl& url, const QString& entry,
41 const QMap<QString,QString>& attributes )
43 QByteArray packedArgs;
44 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
45 stream << (int)'M' << (int)'S' << url << entry << attributes;
47 KIO::SimpleJob* job = KIO::special( url, packedArgs, KIO::HideProgressInfo );
48 KIO::Scheduler::assignJobToSlave( slave, job );
49 return job;
52 AnnotationJobs::GetAnnotationJob* AnnotationJobs::getAnnotation(
53 KIO::Slave* slave, const KUrl& url, const QString& entry,
54 const QStringList& attributes )
56 QByteArray packedArgs;
57 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
58 stream << (int)'M' << (int)'G' << url << entry << attributes;
60 GetAnnotationJob* job = new GetAnnotationJob( url, entry, packedArgs);
61 KIO::Scheduler::assignJobToSlave( slave, job );
62 return job;
65 AnnotationJobs::GetAnnotationJob::GetAnnotationJob( const KUrl& url, const QString& entry,
66 const QByteArray &packedArgs)
67 : KIO::SpecialJob( url, packedArgs),
68 mEntry( entry )
70 connect( this, SIGNAL(infoMessage(KJob*,const QString&,const QString&)),
71 SLOT(slotInfoMessage(KJob*,const QString&,const QString&)) );
74 void AnnotationJobs::GetAnnotationJob::slotInfoMessage( KJob*, const QString& str,const QString& )
76 // Parse the result
77 QStringList lst = str.split( "\r", QString::SkipEmptyParts );
78 while ( lst.count() >= 2 ) // we take items 2 by 2
80 QString name = lst.front(); lst.pop_front();
81 QString value = lst.front(); lst.pop_front();
82 mAnnotations.append( AnnotationAttribute( mEntry, name, value ) );
86 AnnotationJobs::MultiGetAnnotationJob::MultiGetAnnotationJob(
87 KIO::Slave* slave, const KUrl& url, const QStringList& entries )
88 : KIO::Job(),
89 mSlave( slave ),
90 mUrl( url ), mEntryList( entries ), mEntryListIterator( mEntryList.begin() )
92 QTimer::singleShot(0, this, SLOT(slotStart()));
96 void AnnotationJobs::MultiGetAnnotationJob::slotStart()
98 if ( mEntryListIterator != mEntryList.end() ) {
99 QStringList attributes;
100 attributes << "value";
101 KIO::Job* job = getAnnotation( mSlave, mUrl, *mEntryListIterator, attributes );
102 addSubjob( job );
103 } else { // done!
104 emitResult();
108 void AnnotationJobs::MultiGetAnnotationJob::slotResult( KJob *job )
110 if ( job->error() ) {
111 KIO::Job::slotResult( job ); // will set the error and emit result(this)
112 return;
114 removeSubjob( job );
115 const QString& entry = *mEntryListIterator;
116 QString value;
117 bool found = false;
118 GetAnnotationJob* getJob = static_cast<GetAnnotationJob *>( job );
119 const AnnotationList& lst = getJob->annotations();
120 for ( int i = 0 ; i < lst.size() ; ++ i ) {
121 kDebug(5006) <<"found annotation" << lst[i].name <<" =" << lst[i].value;
122 if ( lst[i].name.startsWith( "value." ) ) { // value.priv or value.shared
123 found = true;
124 value = lst[i].value;
125 break;
128 emit annotationResult( entry, value, found );
129 // Move on to next one
130 ++mEntryListIterator;
131 slotStart();
134 AnnotationJobs::MultiGetAnnotationJob* AnnotationJobs::multiGetAnnotation( KIO::Slave* slave, const KUrl& url, const QStringList& entries )
136 return new MultiGetAnnotationJob( slave, url, entries );
139 ////
141 AnnotationJobs::MultiSetAnnotationJob::MultiSetAnnotationJob(
142 KIO::Slave* slave, const KUrl& url, const AnnotationList& annotations )
143 : KIO::Job(),
144 mSlave( slave ),
145 mUrl( url ), mAnnotationList( annotations ), mAnnotationListIterator( mAnnotationList.begin() )
147 QTimer::singleShot(0, this, SLOT(slotStart()));
151 void AnnotationJobs::MultiSetAnnotationJob::slotStart()
153 if ( mAnnotationListIterator != mAnnotationList.end() ) {
154 const AnnotationAttribute& attr = *mAnnotationListIterator;
155 // setAnnotation can set multiple attributes for a given entry.
156 // So in theory we could group entries coming from our list. Bah.
157 QMap<QString, QString> attributes;
158 attributes.insert( attr.name, attr.value );
159 kDebug(5006) <<" setting annotation" << attr.entry << attr.name << attr.value;
160 KIO::Job* job = setAnnotation( mSlave, mUrl, attr.entry, attributes );
161 addSubjob( job );
162 } else { // done!
163 emitResult();
167 void AnnotationJobs::MultiSetAnnotationJob::slotResult( KJob *job )
169 if ( job->error() ) {
170 KIO::Job::slotResult( job ); // will set the error and emit result(this)
171 return;
173 removeSubjob( job );
174 const AnnotationAttribute& attr = *mAnnotationListIterator;
175 emit annotationChanged( attr.entry, attr.name, attr.value );
177 // Move on to next one
178 ++mAnnotationListIterator;
179 slotStart();
182 AnnotationJobs::MultiSetAnnotationJob* AnnotationJobs::multiSetAnnotation(
183 KIO::Slave* slave, const KUrl& url, const AnnotationList& annotations )
185 return new MultiSetAnnotationJob( slave, url, annotations );
189 AnnotationJobs::MultiUrlGetAnnotationJob::MultiUrlGetAnnotationJob( KIO::Slave* slave,
190 const KUrl& baseUrl,
191 const QStringList& paths,
192 const QString& annotation )
193 : KIO::Job(),
194 mSlave( slave ),
195 mUrl( baseUrl ),
196 mPathList( paths ),
197 mPathListIterator( mPathList.begin() ),
198 mAnnotation( annotation )
200 QTimer::singleShot(0, this, SLOT(slotStart()));
204 void AnnotationJobs::MultiUrlGetAnnotationJob::slotStart()
206 if ( mPathListIterator != mPathList.end() ) {
207 QStringList attributes;
208 attributes << "value";
209 KUrl url(mUrl);
210 url.setPath( *mPathListIterator );
211 KIO::Job* job = getAnnotation( mSlave, url, mAnnotation, attributes );
212 addSubjob( job );
213 } else { // done!
214 emitResult();
218 void AnnotationJobs::MultiUrlGetAnnotationJob::slotResult( KJob *job )
220 if ( job->error() ) {
221 KIO::Job::slotResult( job ); // will set the error and emit result(this)
222 return;
224 removeSubjob( job );
225 const QString& path = *mPathListIterator;
226 GetAnnotationJob* getJob = static_cast<GetAnnotationJob *>( job );
227 const AnnotationList& lst = getJob->annotations();
228 for ( int i = 0 ; i < lst.size() ; ++ i ) {
229 kDebug(5006) <<"MultiURL: found annotation" << lst[i].name <<" =" << lst[i].value <<" for path:" << path;
230 if ( lst[i].name.startsWith( "value." ) ) { // value.priv or value.shared
231 mAnnotations.insert( path, lst[i].value );
232 break;
235 // Move on to next one
236 ++mPathListIterator;
237 slotStart();
240 QMap<QString, QString> AnnotationJobs::MultiUrlGetAnnotationJob::annotations() const
242 return mAnnotations;
245 AnnotationJobs::MultiUrlGetAnnotationJob* AnnotationJobs::multiUrlGetAnnotation( KIO::Slave* slave,
246 const KUrl& baseUrl,
247 const QStringList& paths,
248 const QString& annotation )
250 return new MultiUrlGetAnnotationJob( slave, baseUrl, paths, annotation );
254 #include "annotationjobs.moc"