1
/***************************************************************************
2
* Copyright © 2012 Jonathan Thomas <echidnaman@kubuntu.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License as *
6
* published by the Free Software Foundation; either version 2 of *
7
* the License or (at your option) version 3 or any later version *
8
* accepted by the membership of KDE e.V. (or its successor approved *
9
* by the membership of KDE e.V.), which shall act as a proxy *
10
* defined in Section 14 of version 3 of the license. *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
19
***************************************************************************/
21
#include "TransactionModel.h"
26
#include "Application.h"
27
#include "ApplicationBackend.h"
28
#include "ApplicationModel/ApplicationModel.h"
29
#include "TransactionListener.h"
30
#include "Transaction.h"
32
TransactionModel::TransactionModel(QObject *parent)
33
: QAbstractListModel(parent)
34
, m_appBackend(nullptr)
38
QVariant TransactionModel::data(const QModelIndex& index, int role) const
43
TransactionListener *trans = m_transactions.at(index.row());
46
case ApplicationModel::NameRole:
47
return trans->application()->name();
48
case ApplicationModel::IconRole:
49
return trans->application()->icon();
50
case ApplicationModel::CommentRole:
51
return trans->application()->comment();
52
case ApplicationModel::StatusRole:
53
return trans->application()->package()->state();
54
case ApplicationModel::RatingRole:
56
case ApplicationModel::ActiveRole:
58
case ApplicationModel::ProgressRole:
59
return trans->progress();
60
case ApplicationModel::ProgressTextRole:
61
return trans->comment();
62
case ApplicationModel::InstalledRole:
66
case ApplicationModel::ApplicationRole:
67
return qVariantFromValue<QObject *>(trans->application());
75
int TransactionModel::rowCount(const QModelIndex &parent) const
79
return m_transactions.size();
82
void TransactionModel::setBackend(ApplicationBackend* appBackend)
84
m_appBackend = appBackend;
85
connect(m_appBackend, SIGNAL(progress(Transaction*,int)), this, SLOT(externalUpdate()));
86
connect(m_appBackend, SIGNAL(transactionAdded(Transaction*)), this, SLOT(addTransaction(Transaction*)));
87
connect(m_appBackend, SIGNAL(transactionCancelled(Application*)),
88
this, SLOT(removeTransaction(Application*)));
91
void TransactionModel::addTransactions(const QList<Transaction *> &transList)
93
for (Transaction *trans : transList) {
94
addTransaction(trans);
98
void TransactionModel::addTransaction(Transaction *trans)
100
if (!trans || !trans->application())
103
beginInsertRows(QModelIndex(), 0, 0);
104
TransactionListener *listener = new TransactionListener(this);
105
listener->setBackend(m_appBackend);
106
listener->setApplication(trans->application());
107
m_transactions.append(listener);
111
void TransactionModel::removeTransaction(Application *app)
113
TransactionListener *toRemove = nullptr;
114
for (TransactionListener *listener : m_transactions) {
115
if(listener->application() == app) {
122
removeTransaction(toRemove);
125
void TransactionModel::removeTransaction(TransactionListener *listener)
128
int row = m_transactions.indexOf(listener);
129
beginRemoveRows(QModelIndex(), row, row);
130
m_transactions.removeAll(listener);
135
if (!m_transactions.size())
136
emit lastTransactionCancelled();
139
void TransactionModel::clear()
142
qDeleteAll(m_transactions);
143
m_transactions.clear();
147
void TransactionModel::externalUpdate()
149
emit dataChanged(QModelIndex(), QModelIndex());