~ubuntu-branches/ubuntu/oneiric/partitionmanager/oneiric

« back to all changes in this revision

Viewing changes to src/core/operationrunner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-01-23 17:57:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090123175736-2ltrhgg3m55dokbm
Tags: upstream-1.0.0~beta1a
ImportĀ upstreamĀ versionĀ 1.0.0~beta1a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2008 by Volker Lanz <vl@fidra.de>                       *
 
3
 *                                                                         *
 
4
 *   This program is free software; you can redistribute it and/or modify  *
 
5
 *   it under the terms of the GNU General Public License as published by  *
 
6
 *   the Free Software Foundation; either version 2 of the License, or     *
 
7
 *   (at your option) any later version.                                   *
 
8
 *                                                                         *
 
9
 *   This program is distributed in the hope that it will be useful,       *
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
12
 *   GNU General Public License for more details.                          *
 
13
 *                                                                         *
 
14
 *   You should have received a copy of the GNU General Public License     *
 
15
 *   along with this program; if not, write to the                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 
18
 ***************************************************************************/
 
19
 
 
20
#include "core/operationrunner.h"
 
21
 
 
22
#include "core/operationstack.h"
 
23
 
 
24
#include "ops/operation.h"
 
25
 
 
26
#include "util/report.h"
 
27
 
 
28
#include <QMutex>
 
29
 
 
30
#include <kdebug.h>
 
31
#include <klocale.h>
 
32
 
 
33
/** Constructs an OperationRunner.
 
34
        @param ostack the OperationStack to act on
 
35
*/
 
36
OperationRunner::OperationRunner(OperationStack& ostack) :
 
37
        QThread(),
 
38
        m_OperationStack(ostack),
 
39
        m_Report(NULL),
 
40
        m_SuspendMutex(),
 
41
        m_Cancelling(false)
 
42
{
 
43
}
 
44
 
 
45
/** Runs the operations in the OperationStack. */
 
46
void OperationRunner::run()
 
47
{
 
48
        Q_ASSERT(m_Report);
 
49
 
 
50
        setCancelling(false);
 
51
 
 
52
        bool status = true;
 
53
 
 
54
        for (int i = 0; i < numOperations(); i++)
 
55
        {
 
56
                suspendMutex().lock();
 
57
 
 
58
                if (!status || isCancelling())
 
59
                {
 
60
                        suspendMutex().unlock();
 
61
                        break;
 
62
                }
 
63
 
 
64
                Operation* op = operationStack().operations()[i];
 
65
                op->setStatus(Operation::StatusRunning);
 
66
 
 
67
                emit opStarted(i + 1, op);
 
68
 
 
69
                connect(op, SIGNAL(progressChanged(int)), this, SIGNAL(progressSub(int)));
 
70
 
 
71
                status = op->execute(report());
 
72
                op->preview();
 
73
 
 
74
                disconnect(op, SIGNAL(progressChanged(int)), this, SIGNAL(progressSub(int)));
 
75
 
 
76
                emit opFinished(i + 1, op);
 
77
 
 
78
                suspendMutex().unlock();
 
79
 
 
80
                /** @todo Sleep a little to give others a chance to suspend us.
 
81
                        ProgressDialog::slotButtonClicked() blocks if we don't sleep here... Why?
 
82
                        It should block until we unlock above, but it blocks until run() ends...
 
83
                */
 
84
                msleep(100);
 
85
        }
 
86
 
 
87
        if (!status)
 
88
                emit error();
 
89
        else if (isCancelling())
 
90
                emit cancelled();
 
91
        else
 
92
                emit finished();
 
93
}
 
94
 
 
95
/** @return the number of Operations to run */
 
96
qint32 OperationRunner::numOperations() const
 
97
{
 
98
        return operationStack().operations().size();
 
99
}
 
100
 
 
101
/** @return the number of Jobs to run */
 
102
qint32 OperationRunner::numJobs() const
 
103
{
 
104
        qint32 result = 0;
 
105
        
 
106
        foreach (const Operation* op,  operationStack().operations())
 
107
                result += op->jobs().size();
 
108
 
 
109
        return result;
 
110
}
 
111
 
 
112
/** @param op the number of the Operation to get a description for
 
113
        @return the Operation's description
 
114
*/
 
115
QString OperationRunner::description(qint32 op) const
 
116
{
 
117
        Q_ASSERT(op >= 0);
 
118
        Q_ASSERT(op < operationStack().size());
 
119
 
 
120
        return operationStack().operations()[op]->description();
 
121
}