source: SMSSender/common/domain/stask.h @ 315:e6c8bb244ecd

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