1
/***************************************************************************
2
* Copyright © 2010 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 "ApplicationModel.h"
25
#include <KExtendableItemDelegate>
27
#include <LibQApt/Package>
31
#include "Application.h"
32
#include "ApplicationBackend.h"
33
#include "ReviewsBackend/Rating.h"
34
#include "ReviewsBackend/ReviewsBackend.h"
35
#include "Transaction.h"
37
ApplicationModel::ApplicationModel(QObject *parent, ApplicationBackend *backend)
38
: QAbstractListModel(parent)
39
, m_appBackend(backend)
41
connect(m_appBackend, SIGNAL(progress(Transaction*,int)),
42
this, SLOT(updateTransactionProgress(Transaction*,int)));
43
connect(m_appBackend, SIGNAL(workerEvent(QApt::WorkerEvent,Transaction*)),
44
this, SLOT(workerEvent(QApt::WorkerEvent,Transaction*)));
45
connect(m_appBackend, SIGNAL(transactionCancelled(Application*)),
46
this, SLOT(transactionCancelled(Application*)));
49
ApplicationModel::~ApplicationModel()
53
int ApplicationModel::rowCount(const QModelIndex & /*parent*/) const
58
int ApplicationModel::columnCount(const QModelIndex & /*parent*/) const
63
QVariant ApplicationModel::data(const QModelIndex &index, int role) const
65
if (!index.isValid()) {
70
return m_apps.at(index.row())->name();
72
return m_apps.at(index.row())->icon();
74
return m_apps.at(index.row())->comment();
76
return m_apps.at(index.row())->package()->state();
78
Transaction *transaction = transactionAt(index);
84
return transaction->action();
87
Rating *rating = m_appBackend->reviewsBackend()->ratingForApplication(m_apps.at(index.row()));
90
return rating->rating();
96
Transaction *transaction = transactionAt(index);
102
if (transaction->state() == (DoneState | InvalidState)) {
108
Transaction *transaction = transactionAt(index);
114
if (transaction->state() == RunningState) {
115
return m_runningTransactions.value(transaction);
119
case ProgressTextRole: {
120
Transaction *transaction = transactionAt(index);
126
switch(transaction->state()) {
128
return i18nc("@info:status Progress text when waiting", "Waiting");
130
return i18nc("@info:status Progress text when done", "Done");
136
switch (m_appBackend->workerState().first) {
137
case QApt::PackageDownloadStarted:
138
return i18nc("@info:status", "Downloading");
139
case QApt::CommitChangesStarted:
140
switch (index.data(ApplicationModel::ActionRole).toInt()) {
142
return i18nc("@info:status", "Installing");
144
return i18nc("@info:status", "Changing Addons");
146
return i18nc("@info:status", "Removing");
156
return m_apps.at(index.row())->package()->isInstalled();
157
case Qt::ToolTipRole:
164
void ApplicationModel::setApplications(const QList<Application*> &list)
166
m_apps.reserve(list.size());
167
beginInsertRows(QModelIndex(), m_apps.count(), m_apps.count());
172
void ApplicationModel::clear()
174
beginRemoveRows(QModelIndex(), 0, m_apps.size());
176
m_runningTransactions.clear();
180
void ApplicationModel::updateTransactionProgress(Transaction *trans, int progress)
182
if (!m_appBackend->transactions().contains(trans)) {
185
m_runningTransactions[trans] = progress;
187
emit dataChanged(index(m_apps.indexOf(trans->application()), 0),
188
index(m_apps.indexOf(trans->application()), 0));
191
void ApplicationModel::workerEvent(QApt::WorkerEvent event, Transaction *trans)
195
if (!m_appBackend->transactions().contains(trans)) {
200
emit dataChanged(index(m_apps.indexOf(trans->application()), 0),
201
index(m_apps.indexOf(trans->application()), 0));
205
void ApplicationModel::transactionCancelled(Application *application)
207
emit dataChanged(index(m_apps.indexOf(application), 0),
208
index(m_apps.indexOf(application), 0));
211
Application *ApplicationModel::applicationAt(const QModelIndex &index) const
213
return m_apps.at(index.row());
216
Transaction *ApplicationModel::transactionAt(const QModelIndex &index) const
218
Transaction *transaction = 0;
220
Application *app = applicationAt(index);
221
foreach (Transaction *trns, m_appBackend->transactions()) {
222
if (trns->application() == app) {
230
QList<Application*> ApplicationModel::applications() const
235
#include "ApplicationModel.moc"