1
/***************************************************************************
2
* Copyright © 2010 Jonathan Thomas <echidnaman@kubuntu.org> *
3
* Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com> *
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. *
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. *
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
***************************************************************************/
22
#include "TransactionListener.h"
23
#include "ApplicationBackend.h"
24
#include "Transaction.h"
25
#include "Application.h"
27
#include <KLocalizedString>
30
TransactionListener::TransactionListener(QObject* parent)
35
, m_downloading(false)
38
TransactionListener::~TransactionListener()
41
void TransactionListener::setBackend(ApplicationBackend* 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*)));
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
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*)));
65
void TransactionListener::init()
67
QPair<QApt::WorkerEvent, Transaction *> workerState = m_appBackend->workerState();
68
workerEvent(workerState.first, workerState.second);
70
foreach (Transaction *transaction, m_appBackend->transactions()) {
71
if (transaction->application() == m_app) {
73
kDebug() << transaction->state();
74
showTransactionState(transaction);
79
bool TransactionListener::isActive() const
84
foreach (Transaction *transaction, m_appBackend->transactions()) {
85
if (transaction->application() == m_app) {
86
return transaction->state()!=TransactionState::DoneState;
92
bool TransactionListener::isDownloading() const
94
return m_appBackend && m_downloading;
97
void TransactionListener::workerEvent(QApt::WorkerEvent event, Transaction *transaction)
99
if (!transaction || !m_appBackend->transactions().contains(transaction) ||
100
m_app != transaction->application()) {
105
case QApt::PackageDownloadStarted:
106
m_comment = i18nc("@info:status", "Downloading");
108
connect(m_appBackend, SIGNAL(progress(Transaction*,int)),
109
this, SLOT(updateProgress(Transaction*,int)));
111
emit commentChanged();
112
emit progressChanged();
113
setDownloading(true);
115
case QApt::PackageDownloadFinished:
116
disconnect(m_appBackend, SIGNAL(progress(Transaction*,int)),
117
this, SLOT(updateProgress(Transaction*,int)));
118
setDownloading(false);
120
case QApt::CommitChangesStarted:
121
setStateComment(transaction);
122
connect(m_appBackend, SIGNAL(progress(Transaction*,int)),
123
this, SLOT(updateProgress(Transaction*,int)));
125
case QApt::CommitChangesFinished:
127
disconnect(m_appBackend, SIGNAL(progress(Transaction*,int)),
128
this, SLOT(updateProgress(Transaction*,int)));
135
void TransactionListener::updateProgress(Transaction *transaction, int percentage)
137
if (m_app == transaction->application()) {
138
m_progress = percentage;
139
emit progressChanged();
141
if (percentage == 100) {
142
m_comment = i18nc("@info:status Progress text when done", "Done");
143
emit commentChanged();
148
void TransactionListener::showTransactionState(Transaction *transaction)
150
switch (transaction->state()) {
152
m_comment = i18nc("@info:status Progress text when waiting", "Waiting");
153
emit commentChanged();
156
setStateComment(transaction);
159
m_comment = i18nc("@info:status Progress text when done", "Done");
161
emit progressChanged();
162
emit commentChanged();
169
void TransactionListener::setStateComment(Transaction* transaction)
171
switch(transaction->action()) {
173
m_comment = i18nc("@info:status", "Installing");
174
emit commentChanged();
178
m_comment = i18nc("@info:status", "Changing Addons");
179
emit commentChanged();
182
m_comment = i18nc("@info:status", "Removing");
183
emit commentChanged();
190
QString TransactionListener::comment() const
195
int TransactionListener::progress() const
200
void TransactionListener::transactionCancelled(Application* app)
205
setDownloading(false);
209
void TransactionListener::setApplication(Application* app)
213
emit applicationChanged();
216
Application* TransactionListener::application() const
221
ApplicationBackend* TransactionListener::backend() const
226
void TransactionListener::setDownloading(bool b)
232
void TransactionListener::transactionRemoved(Transaction* t)
234
if(t && t->application()==m_app) {