kmail

annotationjobs.cpp
1 
31 #include "annotationjobs.h"
32 #include <tdeio/scheduler.h>
33 #include <kdebug.h>
34 
35 using namespace KMail;
36 
38  TDEIO::Slave* slave, const KURL& url, const TQString& entry,
39  const TQMap<TQString,TQString>& attributes )
40 {
41  TQByteArray packedArgs;
42  TQDataStream stream( packedArgs, IO_WriteOnly );
43  stream << (int)'M' << (int)'S' << url << entry << attributes;
44 
45  TDEIO::SimpleJob* job = TDEIO::special( url, packedArgs, false );
46  TDEIO::Scheduler::assignJobToSlave( slave, job );
47  return job;
48 }
49 
51  TDEIO::Slave* slave, const KURL& url, const TQString& entry,
52  const TQStringList& attributes )
53 {
54  TQByteArray packedArgs;
55  TQDataStream stream( packedArgs, IO_WriteOnly );
56  stream << (int)'M' << (int)'G' << url << entry << attributes;
57 
58  GetAnnotationJob* job = new GetAnnotationJob( url, entry, packedArgs, false );
59  TDEIO::Scheduler::assignJobToSlave( slave, job );
60  return job;
61 }
62 
63 AnnotationJobs::GetAnnotationJob::GetAnnotationJob( const KURL& url, const TQString& entry,
64  const TQByteArray &packedArgs,
65  bool showProgressInfo )
66  : TDEIO::SimpleJob( url, TDEIO::CMD_SPECIAL, packedArgs, showProgressInfo ),
67  mEntry( entry )
68 {
69  connect( this, TQ_SIGNAL(infoMessage(TDEIO::Job*,const TQString&)),
70  TQ_SLOT(slotInfoMessage(TDEIO::Job*,const TQString&)) );
71 }
72 
73 void AnnotationJobs::GetAnnotationJob::slotInfoMessage( TDEIO::Job*, const TQString& str )
74 {
75  // Parse the result
76  TQStringList lst = TQStringList::split( "\r", str );
77  while ( lst.count() >= 2 ) // we take items 2 by 2
78  {
79  TQString name = lst.front(); lst.pop_front();
80  TQString value = lst.front(); lst.pop_front();
81  mAnnotations.append( AnnotationAttribute( mEntry, name, value ) );
82  }
83 }
84 
85 AnnotationJobs::MultiGetAnnotationJob::MultiGetAnnotationJob(
86  TDEIO::Slave* slave, const KURL& url, const TQStringList& entries, bool showProgressInfo )
87  : TDEIO::Job( showProgressInfo ),
88  mSlave( slave ),
89  mUrl( url ), mEntryList( entries ), mEntryListIterator( mEntryList.begin() )
90 {
91  TQTimer::singleShot(0, this, TQ_SLOT(slotStart()));
92 }
93 
94 
95 void AnnotationJobs::MultiGetAnnotationJob::slotStart()
96 {
97  if ( mEntryListIterator != mEntryList.end() ) {
98  TQStringList attributes;
99  attributes << "value";
100  TDEIO::Job* job = getAnnotation( mSlave, mUrl, *mEntryListIterator, attributes );
101  addSubjob( job );
102  } else { // done!
103  emitResult();
104  }
105 }
106 
107 void AnnotationJobs::MultiGetAnnotationJob::slotResult( TDEIO::Job *job )
108 {
109  if ( job->error() ) {
110  TDEIO::Job::slotResult( job ); // will set the error and emit result(this)
111  return;
112  }
113  subjobs.remove( job );
114  const TQString& entry = *mEntryListIterator;
115  TQString value;
116  bool found = false;
117  GetAnnotationJob* getJob = static_cast<GetAnnotationJob *>( job );
118  const AnnotationList& lst = getJob->annotations();
119  for ( unsigned int i = 0 ; i < lst.size() ; ++ i ) {
120  //kdDebug(5006) << "found annotation " << lst[i].name << " = " << lst[i].value << endl;
121  if ( lst[i].name.startsWith( "value." ) ) { // value.priv or value.shared
122  found = true;
123  value = lst[i].value;
124  break;
125  }
126  }
127  emit annotationResult( entry, value, found );
128  // Move on to next one
129  ++mEntryListIterator;
130  slotStart();
131 }
132 
133 AnnotationJobs::MultiGetAnnotationJob* AnnotationJobs::multiGetAnnotation( TDEIO::Slave* slave, const KURL& url, const TQStringList& entries )
134 {
135  return new MultiGetAnnotationJob( slave, url, entries, false /*showProgressInfo*/ );
136 }
137 
139 
140 AnnotationJobs::MultiSetAnnotationJob::MultiSetAnnotationJob(
141  TDEIO::Slave* slave, const KURL& url, const AnnotationList& annotations, bool showProgressInfo )
142  : TDEIO::Job( showProgressInfo ),
143  mSlave( slave ),
144  mUrl( url ), mAnnotationList( annotations ), mAnnotationListIterator( mAnnotationList.begin() )
145 {
146  TQTimer::singleShot(0, this, TQ_SLOT(slotStart()));
147 }
148 
149 
150 void AnnotationJobs::MultiSetAnnotationJob::slotStart()
151 {
152  if ( mAnnotationListIterator != mAnnotationList.end() ) {
153  const AnnotationAttribute& attr = *mAnnotationListIterator;
154  // setAnnotation can set multiple attributes for a given entry.
155  // So in theory we could group entries coming from our list. Bah.
156  TQMap<TQString, TQString> attributes;
157  attributes.insert( attr.name, attr.value );
158  kdDebug() << k_funcinfo << " setting annotation " << attr.entry << " " << attr.name << " " << attr.value << endl;
159  TDEIO::Job* job = setAnnotation( mSlave, mUrl, attr.entry, attributes );
160  addSubjob( job );
161  } else { // done!
162  emitResult();
163  }
164 }
165 
166 void AnnotationJobs::MultiSetAnnotationJob::slotResult( TDEIO::Job *job )
167 {
168  if ( job->error() ) {
169  TDEIO::Job::slotResult( job ); // will set the error and emit result(this)
170  return;
171  }
172  subjobs.remove( job );
173  const AnnotationAttribute& attr = *mAnnotationListIterator;
174  emit annotationChanged( attr.entry, attr.name, attr.value );
175 
176  // Move on to next one
177  ++mAnnotationListIterator;
178  slotStart();
179 }
180 
182  TDEIO::Slave* slave, const KURL& url, const AnnotationList& annotations )
183 {
184  return new MultiSetAnnotationJob( slave, url, annotations, false /*showProgressInfo*/ );
185 }
186 
187 
188 AnnotationJobs::MultiUrlGetAnnotationJob::MultiUrlGetAnnotationJob( TDEIO::Slave* slave,
189  const KURL& baseUrl,
190  const TQStringList& paths,
191  const TQString& annotation )
192  : TDEIO::Job( false ),
193  mSlave( slave ),
194  mUrl( baseUrl ),
195  mPathList( paths ),
196  mPathListIterator( mPathList.begin() ),
197  mAnnotation( annotation )
198 {
199  TQTimer::singleShot(0, this, TQ_SLOT(slotStart()));
200 }
201 
202 
203 void AnnotationJobs::MultiUrlGetAnnotationJob::slotStart()
204 {
205  if ( mPathListIterator != mPathList.end() ) {
206  TQStringList attributes;
207  attributes << "value";
208  KURL url(mUrl);
209  url.setPath( *mPathListIterator );
210  TDEIO::Job* job = getAnnotation( mSlave, url, mAnnotation, attributes );
211  addSubjob( job );
212  } else { // done!
213  emitResult();
214  }
215 }
216 
217 void AnnotationJobs::MultiUrlGetAnnotationJob::slotResult( TDEIO::Job *job )
218 {
219  if ( job->error() ) {
220  TDEIO::Job::slotResult( job ); // will set the error and emit result(this)
221  return;
222  }
223  subjobs.remove( job );
224  const TQString& path = *mPathListIterator;
225  GetAnnotationJob* getJob = static_cast<GetAnnotationJob *>( job );
226  const AnnotationList& lst = getJob->annotations();
227  for ( unsigned int i = 0 ; i < lst.size() ; ++ i ) {
228  kdDebug(5006) << "MultiURL: found annotation " << lst[i].name << " = " << lst[i].value << " for path: " << path << endl;
229  if ( lst[i].name.startsWith( "value." ) ) { // value.priv or value.shared
230  mAnnotations.insert( path, lst[i].value );
231  break;
232  }
233  }
234  // Move on to next one
235  ++mPathListIterator;
236  slotStart();
237 }
238 
239 TQMap<TQString, TQString> AnnotationJobs::MultiUrlGetAnnotationJob::annotations() const
240 {
241  return mAnnotations;
242 }
243 
245  const KURL& baseUrl,
246  const TQStringList& paths,
247  const TQString& annotation )
248 {
249  return new MultiUrlGetAnnotationJob( slave, baseUrl, paths, annotation );
250 }
251 
252 
253 #include "annotationjobs.moc"
TDEIO::SimpleJob * setAnnotation(TDEIO::Slave *slave, const KURL &url, const TQString &entry, const TQMap< TQString, TQString > &attributes)
Set an annotation entry (note that it can have multiple attributes)
MultiGetAnnotationJob * multiGetAnnotation(TDEIO::Slave *slave, const KURL &url, const TQStringList &entries)
Get multiple annotation entries Currently we assume we want to get the "value" for each,...
GetAnnotationJob * getAnnotation(TDEIO::Slave *slave, const KURL &url, const TQString &entry, const TQStringList &attributes)
Get an annotation entry.
MultiUrlGetAnnotationJob * multiUrlGetAnnotation(TDEIO::Slave *slave, const KURL &baseUrl, const TQStringList &paths, const TQString &annotation)
Get annotation entries for multiple folders.
MultiSetAnnotationJob * multiSetAnnotation(TDEIO::Slave *slave, const KURL &url, const AnnotationList &annotations)
Set multiple annotation entries.
folderdiaquotatab.h
Definition: aboutdata.cpp:40
One entry in the annotation list: attribute name and attribute value.