source: SMSSender/common/domain/stask.h @ 319:1ec456878bcb

separation-frontend-backend
Last change on this file since 319:1ec456878bcb was 319:1ec456878bcb, checked in by Sämy Zehnder <saemy.zehnder@…>, 8 years ago
  • Adjusts header guards to follow common pattern based on the filename.
File size: 6.7 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2014, gorrión. See http://smssender.gorrion.ch
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18#ifndef COMMON_DOMAIN_STASK_H
19#define COMMON_DOMAIN_STASK_H
20
21#include <QMutex>
22#include <QObject>
23#include <QPointer>
24#include <QString>
25#include <QThread>
26#include <QWaitCondition>
27
28#include "common/exceptions/eexception.h"
29#include "common/global.h"
30
31typedef struct {
32    uint    uid;
33    QString nmespace;
34} SUIdNamespaceIdent;
35inline bool operator==(const SUIdNamespaceIdent& a, const SUIdNamespaceIdent& b) { return (a.uid == b.uid) && (a.nmespace == b.nmespace); }
36inline bool operator<(const SUIdNamespaceIdent& a, const SUIdNamespaceIdent& b) { return (a.nmespace + QString::number(a.uid)) < (b.nmespace + QString::number(b.uid)); }
37
38class COMMON_EXPORT STask : public QObject {
39    Q_OBJECT
40
41    friend class STaskManager; // sigDoStart()
42
43public:
44    enum Property {
45        pTitle,
46        pDetails,
47        pProgress,
48        pStatus
49    };
50
51    enum Status {
52        sWaiting  = 0x00,
53        sRunning  = 0x01,
54        sFinished = 0x02,
55
56        LastStatus = sFinished
57    };
58
59    enum Result {
60        rSuccess,
61        rError,
62        rCancelled
63    };
64
65public:
66    STask(uint UID, const QString& nmespace);
67    ~STask();
68
69    uint    uid() const;
70    QString nmespace() const;
71
72    virtual QString title() const;
73    virtual QString details() const;
74    virtual uint    progress() const;
75    virtual uint    status() const;
76    virtual QString statusStr() const;
77    virtual STask::Result taskResult() const;
78    virtual uint    specialResult() const;
79
80    STask*   parent() const;
81    bool     hasParent() const;
82    bool     isSubtaskOf(STask* parent) const;
83
84    void     lockRessource(int ressourceId, QString nmespace = "", QMutex::RecursionMode mode = QMutex::Recursive);
85    void     unlockRessource(int ressourceId, QString nmespace = "");
86
87    void     addBlockingTask(uint uid, QString nmespace = "");
88    void     addBlockingTask(const SUIdNamespaceIdent& taskIdent);
89    void     setBlockingTasks(const QList<SUIdNamespaceIdent>& tasks);
90    void     addBlockingTask(STask* task);
91    void     setSelfBlocking(bool selfBlocking);
92    void     removeBlockingTask(uint uid, QString nmespace = "");
93    void     removeBlockingTask(const SUIdNamespaceIdent& taskIdent);
94    void     removeBlockingTask(STask* task);
95
96    QList<SUIdNamespaceIdent> blockingTaskIdents() const;
97    QList<QPointer<STask> >   blockingTaskInstances() const;
98
99    void    enqueueAfter(STask* other, bool onlyIfSuccessful = false);
100
101    void    setTitle(const QString& title);
102    void    setDetails(const QString& details);
103    void    setProgress(uint progress);
104    void    setTaskResult(STask::Result taskResult);
105    void    setSpecialResult(uint result);
106
107    void    setParentTask(STask* parent);
108
109    bool    isStarted() const;
110    bool    isRunning() const;
111    bool    isCancelled() const;
112    bool    isFinished() const;
113
114    bool              hadException() const;
115    const EException& exception() const;
116
117public slots:
118    void start();
119    void cancel();
120
121signals:
122    void changed(STask* task, STask::Property property); // Always use Qt::DirectConnection when connecting!
123
124    void started(STask* task); // Always use Qt::DirectConnection when connecting!
125    void cancelled(STask* task); // Always use Qt::DirectConnection when connecting!
126    void exceptionOccured(STask* task); // Always use Qt::DirectConnection when connecting!
127    void finished(STask* task); // Always use Qt::DirectConnection when connecting!
128
129protected:
130    QMutex&      dataMutex() const;
131
132    void         setStatus(uint status);
133    virtual void setException(const EException& e);
134    virtual void setException(const QSharedPointer<EException>& e);
135
136    void         startSubTask(STask* task, bool waitFor);
137
138    void         waitFor(STask* other);
139    void         waitFor(QList<STask*> others);
140    void         waitFor(QList<QPointer<STask> > others);
141
142    void         setStarted();
143
144    virtual void doStart();
145    virtual void doTheWork() =0;
146    void         safeDoTheWork();
147
148    void         haltIfCancelRequested();
149
150    QString      taskIdent();
151    QDebug       sDebug();
152
153    virtual EException* inheritSubtaskException(const EException& exception);
154
155protected slots:
156    void         setFinished();
157
158private slots:
159    void         onSigStart();
160    void         onSubTaskFinished(STask* subTask);
161    void         onSubTaskExceptionOccured(STask* subTask);
162
163signals: // private
164    void _sigDoStart();
165
166private slots:
167    void waitForTaskFinished(STask* task);
168    void startIfSuccessful(STask* task);
169
170private:
171    mutable QMutex  dataMutex_;
172
173    uint    uid_;
174    QString nmespace_;
175
176    QList<SUIdNamespaceIdent> blockingTaskIdents_;
177    QList<QPointer<STask> >   blockingTaskInstances_;
178    QString         title_;
179    QString         details_;
180    uint            progress_;
181    uint            status_;
182    STask::Result   taskResult_;
183    uint            specialResult_;
184
185    QPointer<STask> parent_;
186
187    QList<QPointer<STask> > waitingFor_;
188    mutable QMutex waitForMutex_;
189    QWaitCondition waitForWaitCondition_;
190
191    QList<QPointer<STask> > subTasks_;
192
193    bool started_;
194    bool cancelled_;
195    bool finished_;
196    bool autoHaltOnCancelRequested_;
197
198    QSharedPointer<EException> exception_;
199};
200
201
202class COMMON_EXPORT SThreadedTask : public STask {
203    Q_OBJECT
204
205public:
206    SThreadedTask(uint UID, const QString& nmespace);
207    ~SThreadedTask();
208
209protected:
210    void doStart();
211
212private slots:
213    void threadStarted();
214
215private:
216    QThread* thread_;
217};
218
219
220class COMMON_EXPORT SAsynchroneousTask : public STask {
221    Q_OBJECT
222
223public:
224    SAsynchroneousTask(uint UID, const QString& nmespace);
225
226protected:
227    void doStart();
228
229    void doTheWork();
230    virtual void startTheWork() =0;
231};
232
233/**
234 * Exception which gets thrown when calling SThread::haltIfCancelRequested().
235 */
236class COMMON_EXPORT EAbortException : public EException {
237public:
238    EAbortException();
239
240public: /* EException */
241    virtual void raise();
242    EAbortException* createClonedInstance() const throw();
243};
244
245#endif /* COMMON_DOMAIN_STASK_H */
Note: See TracBrowser for help on using the repository browser.