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

« back to all changes in this revision

Viewing changes to installer/ApplicationModel/ApplicationModel.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
 
 *                                                                         *
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.                    *
11
 
 *                                                                         *
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.                          *
16
 
 *                                                                         *
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
 
 ***************************************************************************/
20
 
 
21
 
#include "ApplicationModel.h"
22
 
 
23
 
#include <KIcon>
24
 
#include <KLocale>
25
 
#include <KExtendableItemDelegate>
26
 
 
27
 
#include <LibQApt/Package>
28
 
 
29
 
#include <math.h>
30
 
 
31
 
#include "Application.h"
32
 
#include "ApplicationBackend.h"
33
 
#include "ReviewsBackend/Rating.h"
34
 
#include "ReviewsBackend/ReviewsBackend.h"
35
 
#include "Transaction.h"
36
 
 
37
 
ApplicationModel::ApplicationModel(QObject *parent, ApplicationBackend *backend)
38
 
    : QAbstractListModel(parent)
39
 
    , m_appBackend(backend)
40
 
{
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*)));
47
 
}
48
 
 
49
 
ApplicationModel::~ApplicationModel()
50
 
{
51
 
}
52
 
 
53
 
int ApplicationModel::rowCount(const QModelIndex & /*parent*/) const
54
 
{
55
 
    return m_apps.size();
56
 
}
57
 
 
58
 
int ApplicationModel::columnCount(const QModelIndex & /*parent*/) const
59
 
{
60
 
    return 1;
61
 
}
62
 
 
63
 
QVariant ApplicationModel::data(const QModelIndex &index, int role) const
64
 
{
65
 
    if (!index.isValid()) {
66
 
        return false;
67
 
    }
68
 
    switch (role) {
69
 
        case NameRole:
70
 
            return m_apps.at(index.row())->name();
71
 
        case IconRole:
72
 
            return m_apps.at(index.row())->icon();
73
 
        case CommentRole:
74
 
            return m_apps.at(index.row())->comment();
75
 
        case StatusRole:
76
 
            return m_apps.at(index.row())->package()->state();
77
 
        case ActionRole: {
78
 
            Transaction *transaction = transactionAt(index);
79
 
 
80
 
            if (!transaction) {
81
 
                return 0;
82
 
            }
83
 
 
84
 
            return transaction->action();
85
 
        }
86
 
        case RatingRole: {
87
 
            Rating *rating = m_appBackend->reviewsBackend()->ratingForApplication(m_apps.at(index.row()));
88
 
 
89
 
            if (rating) {
90
 
                return rating->rating();
91
 
            } else {
92
 
                return -1;
93
 
            }
94
 
        }
95
 
        case ActiveRole: {
96
 
            Transaction *transaction = transactionAt(index);
97
 
 
98
 
            if (!transaction) {
99
 
                return 0;
100
 
            }
101
 
 
102
 
            if (transaction->state() == (DoneState | InvalidState)) {
103
 
                return false;
104
 
            }
105
 
            return true;
106
 
        }
107
 
        case ProgressRole: {
108
 
            Transaction *transaction = transactionAt(index);
109
 
 
110
 
            if (!transaction) {
111
 
                return 0;
112
 
            }
113
 
 
114
 
            if (transaction->state() == RunningState) {
115
 
                return m_runningTransactions.value(transaction);
116
 
            }
117
 
            return 0;
118
 
        }
119
 
        case ProgressTextRole: {
120
 
            Transaction *transaction = transactionAt(index);
121
 
 
122
 
            if (!transaction) {
123
 
                return QVariant();
124
 
            }
125
 
 
126
 
            switch(transaction->state()) {
127
 
            case QueuedState:
128
 
                return i18nc("@info:status Progress text when waiting", "Waiting");
129
 
            case DoneState:
130
 
                return i18nc("@info:status Progress text when done", "Done");
131
 
            case RunningState:
132
 
            default:
133
 
                break;
134
 
            }
135
 
 
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()) {
141
 
                case InstallApp:
142
 
                    return i18nc("@info:status", "Installing");
143
 
                case ChangeAddons:
144
 
                    return i18nc("@info:status", "Changing Addons");
145
 
                case RemoveApp:
146
 
                    return i18nc("@info:status", "Removing");
147
 
                default:
148
 
                    return QVariant();
149
 
                }
150
 
            default:
151
 
                return QVariant();
152
 
            }
153
 
        }
154
 
        break;
155
 
        case InstalledRole:
156
 
            return m_apps.at(index.row())->package()->isInstalled();
157
 
        case Qt::ToolTipRole:
158
 
            return QVariant();
159
 
    }
160
 
 
161
 
    return QVariant();
162
 
}
163
 
 
164
 
void ApplicationModel::setApplications(const QList<Application*> &list)
165
 
{
166
 
    m_apps.reserve(list.size());
167
 
    beginInsertRows(QModelIndex(), m_apps.count(), m_apps.count());
168
 
    m_apps = list;
169
 
    endInsertRows();
170
 
}
171
 
 
172
 
void ApplicationModel::clear()
173
 
{
174
 
    beginRemoveRows(QModelIndex(), 0, m_apps.size());
175
 
    m_apps.clear();
176
 
    m_runningTransactions.clear();
177
 
    endRemoveRows();
178
 
}
179
 
 
180
 
void ApplicationModel::updateTransactionProgress(Transaction *trans, int progress)
181
 
{
182
 
    if (!m_appBackend->transactions().contains(trans)) {
183
 
        return;
184
 
    }
185
 
    m_runningTransactions[trans] = progress;
186
 
 
187
 
    emit dataChanged(index(m_apps.indexOf(trans->application()), 0),
188
 
                         index(m_apps.indexOf(trans->application()), 0));
189
 
}
190
 
 
191
 
void ApplicationModel::workerEvent(QApt::WorkerEvent event, Transaction *trans)
192
 
{
193
 
    Q_UNUSED(event);
194
 
 
195
 
    if (!m_appBackend->transactions().contains(trans)) {
196
 
        return;
197
 
    }
198
 
 
199
 
    if (trans != 0) {
200
 
        emit dataChanged(index(m_apps.indexOf(trans->application()), 0),
201
 
                         index(m_apps.indexOf(trans->application()), 0));
202
 
    }
203
 
}
204
 
 
205
 
void ApplicationModel::transactionCancelled(Application *application)
206
 
{
207
 
    emit dataChanged(index(m_apps.indexOf(application), 0),
208
 
                     index(m_apps.indexOf(application), 0));
209
 
}
210
 
 
211
 
Application *ApplicationModel::applicationAt(const QModelIndex &index) const
212
 
{
213
 
    return m_apps.at(index.row());
214
 
}
215
 
 
216
 
Transaction *ApplicationModel::transactionAt(const QModelIndex &index) const
217
 
{
218
 
    Transaction *transaction = 0;
219
 
 
220
 
    Application *app = applicationAt(index);
221
 
    foreach (Transaction *trns, m_appBackend->transactions()) {
222
 
        if (trns->application() == app) {
223
 
            transaction = trns;
224
 
        }
225
 
    }
226
 
 
227
 
    return transaction;
228
 
}
229
 
 
230
 
QList<Application*> ApplicationModel::applications() const
231
 
{
232
 
    return m_apps;
233
 
}
234
 
 
235
 
#include "ApplicationModel.moc"