Cache Nepomuk tag information.
[kdepim.git] / knode / knjobdata.h
blob919cfb2bdbe68832e12e43fa676917b9ff9ce97d
1 /*
2 KNode, the KDE newsreader
3 Copyright (c) 1999-2006 the KNode authors.
4 See file AUTHORS for details
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software Foundation,
12 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
15 #ifndef KNJOBDATA_H
16 #define KNJOBDATA_H
18 #include "knserverinfo.h"
20 #include <QPointer>
21 #include <kurl.h>
22 #include <kio/global.h>
23 #include <QObject>
24 #include <QList>
25 #include <libkdepim/progressmanager.h>
28 class KJob;
30 namespace KIO {
31 class Job;
34 class KNJobItem;
35 class KNJobData;
38 /** Base class for classes that want to create and schedule jobs. */
39 class KNJobConsumer {
41 public:
42 KNJobConsumer();
43 virtual ~KNJobConsumer();
45 /** Send the job to the scheduler and append it to the
46 * job queue.
48 void emitJob(KNJobData *j);
50 /** Remove the job from the joblist and process it by
51 * calling @ref processJob
53 void jobDone(KNJobData *j);
55 /** Returns true if we are waiting for at least one job
56 * to be completed
58 bool jobsPending() const { return !mJobs.isEmpty(); }
60 /** Find any job related to a job item and cancel it.
62 void cancelJobs( boost::shared_ptr<KNJobItem> item );
64 protected:
65 /** The actual work is done here */
66 virtual void processJob(KNJobData *j);
67 /** List of all active jobs. */
68 QList<KNJobData*> mJobs;
73 /** Base class for data structures used in jobs. */
74 class KNJobItem {
76 public:
77 /**
78 Shared pointer to a KNJobItem. To be used instead of raw KNJobItem*.
80 typedef boost::shared_ptr<KNJobItem> Ptr;
82 KNJobItem() {}
83 virtual ~KNJobItem() {}
85 virtual bool isLocked() { return false; }
86 virtual void setLocked(bool) { }
88 virtual QString prepareForExecution() { return QString(); }
93 /** Abstract base class for all KNode internal jobs.
94 * This class takes care of:
95 * - progress/status reporting and user interaction (cancellation).
96 * - error handling/reporting.
97 * - easy handling of associated KIO jobs.
98 * To imlpement a new job class, you need to sub-class this class and
99 * implement the execute() method.
101 class KNJobData : public QObject
103 Q_OBJECT
105 public:
107 friend class KNJobConsumer;
109 enum jobType { JTLoadGroups=1,
110 JTFetchGroups,
111 JTfetchNewHeaders,
112 JTfetchArticle,
113 JTpostArticle,
114 JTmail,
115 JTfetchSource };
117 KNJobData( jobType t, KNJobConsumer *c, KNServerInfo::Ptr a, KNJobItem::Ptr i );
118 ~KNJobData();
120 jobType type() const { return t_ype; }
122 KNServerInfo::Ptr account() const { return a_ccount; }
123 KNJobItem::Ptr data() const { return d_ata; }
125 /** Returns the error code (see KIO::Error). */
126 int error() const { return mError; }
127 /** Returns the error message. */
128 QString errorString() const { return mErrorString; }
129 /** Returns true if the job finished successfully. */
130 bool success() const { return mErrorString.isEmpty() && mError == 0; }
131 /** Returns true if the job has been canceled by the user. */
132 bool canceled() const { return mCanceled; }
134 /** Cancels this job.
135 * If the job is currently active, this cancels the associated KIO job and
136 * emits the finished signal.
138 void cancel();
140 /** Set job error information.
141 * @param err The error code (see KIO::Error).
142 * @param errMsg A translated error message.
144 void setError( int err, const QString &errMsg );
146 void prepareForExecution() { mErrorString = d_ata->prepareForExecution(); }
147 void notifyConsumer();
149 /** Performs the actual operation of a job, needs to be reimplemented for
150 * every job.
151 * Note that a job might be executed multiple times e.g. in case of an
152 * authentication error.
154 virtual void execute() = 0;
156 /** Returns the progress item for this job. */
157 KPIM::ProgressItem* progressItem() const { return mProgressItem; }
158 /** Creates a KPIM::ProgressItem for this job. */
159 void createProgressItem();
161 /** Set the status message of the progress item if available.
162 * @param msg The new status message.
164 void setStatus( const QString &msg ) { if ( mProgressItem ) mProgressItem->setStatus( msg ); }
165 /** Set the progress value of the progress item if available.
166 * @param progress The new progress value.
168 void setProgress( unsigned int progress ) { if ( mProgressItem ) mProgressItem->setProgress( progress ); }
169 /** Tells the progress item to indicate that the job has finished if
170 * available. This causes the destruction of the progress item.
172 void setComplete() { if ( mProgressItem ) { mProgressItem->setComplete(); mProgressItem = 0; } }
174 signals:
175 /** Emitted when a job has been finished.
176 * It's recommended to to emit it via emitFinished().
178 void finished( KNJobData* );
180 protected:
181 /** Emits the finished() signal via a single-shot timer. */
182 void emitFinished();
184 /** Returns a correctly set up KUrl according to the encryption and
185 * authentication settings for KIO slave operations.
187 KUrl baseUrl() const;
190 Connects progress signals.
191 @param job The KJob to setup.
193 void setupKJob( KJob *job );
195 /** Sets TLS metadata and connects the given KIO job to the progress item.
196 * @param job The KIO job to setup.
198 void setupKIOJob( KIO::Job *job );
200 protected:
201 jobType t_ype;
202 KNJobItem::Ptr d_ata;
203 KNServerInfo::Ptr a_ccount;
204 /** The job error code (see KIO::Error). */
205 int mError;
206 /** The error message. */
207 QString mErrorString;
208 /** Cancel status flag. */
209 bool mCanceled;
210 KNJobConsumer *c_onsumer;
211 /** An associated KJob. */
212 QPointer<KJob> mJob;
213 /** The progress item representing this job to the user. */
214 KPIM::ProgressItem *mProgressItem;
216 private slots:
217 /** Connected to the progress signal of mJob to update the progress item. */
218 void slotJobPercent( KJob *job, unsigned long percent );
219 /** Connected to the info message signal if mJob to update the progress item. */
220 void slotJobInfoMessage( KJob *job, const QString &msg );
221 /** Emits the finished signal. @see emitFinished() */
222 void slotEmitFinished();
227 #endif