~mandel/ubuntu-download-manager/remove-shared-process-factory

« back to all changes in this revision

Viewing changes to libubuntudownloadmanager/downloads/state_machines/download_sm.cpp

  • Committer: Tarmac
  • Author(s): Manuel de la Pena
  • Date: 2013-11-21 12:06:12 UTC
  • mfrom: (148.13.24 keep-track-of-states)
  • Revision ID: tarmac-20131121120612-s3xcba5yvun01q6u
Keep track of the state in the state machine so that tests are easier to perform.

Approved by PS Jenkins bot, Mike McCracken.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <QStateMachine>
23
23
#include <QSslError>
24
24
#include "download_sm.h"
 
25
#include "final_state.h"
 
26
#include "state.h"
25
27
 
26
28
namespace Ubuntu {
27
29
 
176
178
    DownloadSMTransition::onTransition(event);
177
179
}
178
180
 
 
181
QString DownloadSM::IDLE = "IDLE";
 
182
QString DownloadSM::INIT = "INIT";
 
183
QString DownloadSM::DOWNLOADING = "DOWNLOADING";
 
184
QString DownloadSM::DOWNLOADING_NOT_CONNECTED = "DOWNLOADING_NOT_CONNECTED";
 
185
QString DownloadSM::PAUSED = "PAUSED";
 
186
QString DownloadSM::PAUSED_NOT_CONNECTED = "PAUSED_NOT_CONNECTED";
 
187
QString DownloadSM::DOWNLOADED = "DOWNLOADED";
 
188
QString DownloadSM::HASHING = "HASHING";
 
189
QString DownloadSM::POST_PROCESSING = "POST_PROCESSING";
 
190
QString DownloadSM::ERROR = "ERROR";
 
191
QString DownloadSM::CANCELED = "CANCELED";
 
192
QString DownloadSM::FINISHED = "FINISHED";
 
193
 
179
194
/**
180
195
 * PRIVATE IMPLEMENTATION
181
196
 */
184
199
    Q_DECLARE_PUBLIC(DownloadSM)
185
200
 
186
201
 public:
187
 
    explicit DownloadSMPrivate(DownloadSM* parent)
188
 
        : _idleState(),
189
 
          _initState(),
190
 
          _downloadingState(),
191
 
          _downloadingNotConnectedState(),
192
 
          _pausedState(),
193
 
          _pausedNotConnectedState(),
194
 
          _downloadedState(),
195
 
          _hashingState(),
196
 
          _postProcessingState(),
197
 
          _errorState(),
198
 
          _canceledState(),
199
 
          _finishedState(),
 
202
    DownloadSMPrivate(SMFileDownload* down, DownloadSM* parent)
 
203
        : _down(down),
200
204
          q_ptr(parent) {
 
205
        Q_Q(DownloadSM);
 
206
        _idleState = new State(q, "state", DownloadSM::IDLE);
 
207
        _initState = new State(q, "state", DownloadSM::INIT);
 
208
        _downloadingState = new State(q, "state", DownloadSM::DOWNLOADING);
 
209
        _downloadingNotConnectedState = new State(q, "state",
 
210
            DownloadSM::DOWNLOADING_NOT_CONNECTED);
 
211
        _pausedState = new State(q, "state", DownloadSM::PAUSED);
 
212
        _pausedNotConnectedState = new State(q, "state",
 
213
                DownloadSM::PAUSED_NOT_CONNECTED);
 
214
        _downloadedState = new State(q, "state", DownloadSM::DOWNLOADED);
 
215
        _hashingState = new State(q, "state", DownloadSM::HASHING);
 
216
        _postProcessingState = new State(q, "state", DownloadSM::POST_PROCESSING);
 
217
        _errorState = new FinalState(q, "state", DownloadSM::ERROR);
 
218
        _canceledState = new FinalState(q, "state", DownloadSM::CANCELED);
 
219
        _finishedState = new FinalState(q, "state", DownloadSM::FINISHED);
 
220
 
201
221
        // add the idle state transitions
202
222
        _transitions.append(new HeaderTransition(_down, _idleState,
203
223
            _initState));
310
330
            _down, SIGNAL(finished()), _finishedState));
311
331
        _transitions.append(_postProcessingState->addTransition(
312
332
            _down, SIGNAL(postProcessingError()), _errorState));
 
333
 
 
334
        // add states set init state
 
335
        _stateMachine.addState(_idleState);
 
336
        _stateMachine.addState(_initState);
 
337
        _stateMachine.addState(_downloadingState);
 
338
        _stateMachine.addState(_downloadingNotConnectedState);
 
339
        _stateMachine.addState(_pausedState);
 
340
        _stateMachine.addState(_pausedNotConnectedState);
 
341
        _stateMachine.addState(_downloadedState);
 
342
        _stateMachine.addState(_hashingState);
 
343
        _stateMachine.addState(_postProcessingState);
 
344
        _stateMachine.addState(_errorState);
 
345
        _stateMachine.addState(_canceledState);
 
346
        _stateMachine.addState(_finishedState);
 
347
 
 
348
        _stateMachine.setInitialState(_initState);
 
349
    }
 
350
 
 
351
    QString state() {
 
352
        return _state;
 
353
    }
 
354
 
 
355
    void setState(QString state) {
 
356
        _state = state;
 
357
    }
 
358
 
 
359
    void start() {
 
360
        _stateMachine.start();
313
361
    }
314
362
 
315
363
    ~DownloadSMPrivate() {
329
377
    }
330
378
 
331
379
 private:
 
380
    QString _state = DownloadSM::IDLE;
332
381
    QStateMachine _stateMachine;
333
382
    QList<QSignalTransition*> _transitions;
334
383
    // states
350
399
    DownloadSM* q_ptr;
351
400
};
352
401
 
353
 
DownloadSM::DownloadSM(QObject* parent)
 
402
DownloadSM::DownloadSM(SMFileDownload* down, QObject* parent)
354
403
    : QObject(parent),
355
 
      d_ptr(new DownloadSMPrivate(this)){
 
404
      d_ptr(new DownloadSMPrivate(down, this)){
 
405
}
 
406
 
 
407
QString
 
408
DownloadSM::state() {
 
409
    Q_D(DownloadSM);
 
410
    return d->state();
 
411
}
 
412
 
 
413
void
 
414
DownloadSM::setState(QString state) {
 
415
    Q_D(DownloadSM);
 
416
    d->setState(state);
 
417
}
 
418
 
 
419
void
 
420
DownloadSM::start() {
 
421
    Q_D(DownloadSM);
 
422
    d->start();
356
423
}
357
424
 
358
425
DownloadSM::~DownloadSM() {