~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

« back to all changes in this revision

Viewing changes to libmuon/Transaction/TransactionListener.cpp

Tags: upstream-1.3.65
Import upstream version 1.3.65

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright © 2010 Jonathan Thomas <echidnaman@kubuntu.org>             *
 
3
 *   Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>       *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or         *
 
6
 *   modify it under the terms of the GNU General Public License as        *
 
7
 *   published by the Free Software Foundation; either version 2 of        *
 
8
 *   the License or (at your option) version 3 or any later version        *
 
9
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 
10
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 
11
 *   defined in Section 14 of version 3 of the license.                    *
 
12
 *                                                                         *
 
13
 *   This program is distributed in the hope that it will be useful,       *
 
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
16
 *   GNU General Public License for more details.                          *
 
17
 *                                                                         *
 
18
 *   You should have received a copy of the GNU General Public License     *
 
19
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 
20
 ***************************************************************************/
 
21
 
 
22
#include "TransactionListener.h"
 
23
#include "ApplicationBackend.h"
 
24
#include "Transaction.h"
 
25
#include "Application.h"
 
26
 
 
27
#include <KLocalizedString>
 
28
#include <KDebug>
 
29
 
 
30
TransactionListener::TransactionListener(QObject* parent)
 
31
    : QObject(parent)
 
32
    , m_appBackend(0)
 
33
    , m_app(0)
 
34
    , m_progress(0)
 
35
    , m_downloading(false)
 
36
{}
 
37
 
 
38
TransactionListener::~TransactionListener()
 
39
{}
 
40
 
 
41
void TransactionListener::setBackend(ApplicationBackend* appBackend)
 
42
{
 
43
    if(m_appBackend) {
 
44
        disconnect(m_appBackend, SIGNAL(workerEvent(QApt::WorkerEvent,Transaction*)),
 
45
                this, SLOT(workerEvent(QApt::WorkerEvent,Transaction*)));
 
46
        disconnect(m_appBackend, SIGNAL(transactionCancelled(Application*)),
 
47
                this, SLOT(transactionCancelled(Application*)));
 
48
        disconnect(m_appBackend, SIGNAL(transactionRemoved(Transaction*)),
 
49
                this, SLOT(transactionRemoved(Transaction*)));
 
50
    }
 
51
    
 
52
    m_appBackend = appBackend;
 
53
    // Catch already-begun downloads. If the state is something else, we won't
 
54
    // care because we won't handle it
 
55
    init();
 
56
    
 
57
    connect(m_appBackend, SIGNAL(workerEvent(QApt::WorkerEvent,Transaction*)),
 
58
            this, SLOT(workerEvent(QApt::WorkerEvent,Transaction*)));
 
59
    connect(m_appBackend, SIGNAL(transactionCancelled(Application*)),
 
60
            this, SLOT(transactionCancelled(Application*)));
 
61
    connect(m_appBackend, SIGNAL(transactionRemoved(Transaction*)),
 
62
                this, SLOT(transactionRemoved(Transaction*)));
 
63
}
 
64
 
 
65
void TransactionListener::init()
 
66
{
 
67
    QPair<QApt::WorkerEvent, Transaction *> workerState = m_appBackend->workerState();
 
68
    workerEvent(workerState.first, workerState.second);
 
69
 
 
70
    foreach (Transaction *transaction, m_appBackend->transactions()) {
 
71
        if (transaction->application() == m_app) {
 
72
            emit running(true);
 
73
            kDebug() << transaction->state();
 
74
            showTransactionState(transaction);
 
75
        }
 
76
    }
 
77
}
 
78
 
 
79
bool TransactionListener::isActive() const
 
80
{
 
81
    if(!m_appBackend)
 
82
        return false;
 
83
 
 
84
    foreach (Transaction *transaction, m_appBackend->transactions()) {
 
85
        if (transaction->application() == m_app) {
 
86
            return transaction->state()!=TransactionState::DoneState;
 
87
        }
 
88
    }
 
89
    return false;
 
90
}
 
91
 
 
92
bool TransactionListener::isDownloading() const
 
93
{
 
94
    return m_appBackend && m_downloading;
 
95
}
 
96
 
 
97
void TransactionListener::workerEvent(QApt::WorkerEvent event, Transaction *transaction)
 
98
{
 
99
    if (!transaction || !m_appBackend->transactions().contains(transaction) ||
 
100
        m_app != transaction->application()) {
 
101
        return;
 
102
    }
 
103
 
 
104
    switch (event) {
 
105
    case QApt::PackageDownloadStarted:
 
106
        m_comment = i18nc("@info:status", "Downloading");
 
107
        m_progress = 0;
 
108
        connect(m_appBackend, SIGNAL(progress(Transaction*,int)),
 
109
                this, SLOT(updateProgress(Transaction*,int)));
 
110
        emit running(true);
 
111
        emit commentChanged();
 
112
        emit progressChanged();
 
113
        setDownloading(true);
 
114
        break;
 
115
    case QApt::PackageDownloadFinished:
 
116
        disconnect(m_appBackend, SIGNAL(progress(Transaction*,int)),
 
117
                   this, SLOT(updateProgress(Transaction*,int)));
 
118
        setDownloading(false);
 
119
        break;
 
120
    case QApt::CommitChangesStarted:
 
121
        setStateComment(transaction);
 
122
        connect(m_appBackend, SIGNAL(progress(Transaction*,int)),
 
123
                this, SLOT(updateProgress(Transaction*,int)));
 
124
        break;
 
125
    case QApt::CommitChangesFinished:
 
126
        emit running(false);
 
127
        disconnect(m_appBackend, SIGNAL(progress(Transaction*,int)),
 
128
                   this, SLOT(updateProgress(Transaction*,int)));
 
129
        break;
 
130
    default:
 
131
        break;
 
132
    }
 
133
}
 
134
 
 
135
void TransactionListener::updateProgress(Transaction *transaction, int percentage)
 
136
{
 
137
    if (m_app == transaction->application()) {
 
138
        m_progress = percentage;
 
139
        emit progressChanged();
 
140
 
 
141
        if (percentage == 100) {
 
142
            m_comment = i18nc("@info:status Progress text when done", "Done");
 
143
            emit commentChanged();
 
144
        }
 
145
    }
 
146
}
 
147
 
 
148
void TransactionListener::showTransactionState(Transaction *transaction)
 
149
{
 
150
    switch (transaction->state()) {
 
151
        case QueuedState:
 
152
            m_comment = i18nc("@info:status Progress text when waiting", "Waiting");
 
153
            emit commentChanged();
 
154
            break;
 
155
        case RunningState:
 
156
            setStateComment(transaction);
 
157
            break;
 
158
        case DoneState:
 
159
            m_comment = i18nc("@info:status Progress text when done", "Done");
 
160
            m_progress = 100;
 
161
            emit progressChanged();
 
162
            emit commentChanged();
 
163
            break;
 
164
        default:
 
165
            break;
 
166
    }
 
167
}
 
168
 
 
169
void TransactionListener::setStateComment(Transaction* transaction)
 
170
{
 
171
    switch(transaction->action()) {
 
172
        case InstallApp:
 
173
            m_comment = i18nc("@info:status", "Installing");
 
174
            emit commentChanged();
 
175
            emit running(true);
 
176
            break;
 
177
        case ChangeAddons:
 
178
            m_comment = i18nc("@info:status", "Changing Addons");
 
179
            emit commentChanged();
 
180
            break;
 
181
        case RemoveApp:
 
182
            m_comment = i18nc("@info:status", "Removing");
 
183
            emit commentChanged();
 
184
            break;
 
185
        default:
 
186
            break;
 
187
    }
 
188
}
 
189
 
 
190
QString TransactionListener::comment() const
 
191
{
 
192
    return m_comment;
 
193
}
 
194
 
 
195
int TransactionListener::progress() const
 
196
{
 
197
    return m_progress;
 
198
}
 
199
 
 
200
void TransactionListener::transactionCancelled(Application* app)
 
201
{
 
202
    if(app!=m_app)
 
203
        return;
 
204
    emit running(false);
 
205
    setDownloading(false);
 
206
    emit cancelled();
 
207
}
 
208
 
 
209
void TransactionListener::setApplication(Application* app)
 
210
{
 
211
    m_app = app;
 
212
    init();
 
213
    emit applicationChanged();
 
214
}
 
215
 
 
216
Application* TransactionListener::application() const
 
217
{
 
218
    return m_app;
 
219
}
 
220
 
 
221
ApplicationBackend* TransactionListener::backend() const
 
222
{
 
223
    return m_appBackend;
 
224
}
 
225
 
 
226
void TransactionListener::setDownloading(bool b)
 
227
{
 
228
    m_downloading = b;
 
229
    emit downloading(b);
 
230
}
 
231
 
 
232
void TransactionListener::transactionRemoved(Transaction* t)
 
233
{
 
234
    if(t && t->application()==m_app) {
 
235
        emit running(false);
 
236
    }
 
237
}