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

« back to all changes in this revision

Viewing changes to libmuon/Transaction/TransactionModel.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 © 2012 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 "TransactionModel.h"
 
22
 
 
23
#include <KDebug>
 
24
 
 
25
// Own includes
 
26
#include "Application.h"
 
27
#include "ApplicationBackend.h"
 
28
#include "ApplicationModel/ApplicationModel.h"
 
29
#include "TransactionListener.h"
 
30
#include "Transaction.h"
 
31
 
 
32
TransactionModel::TransactionModel(QObject *parent)
 
33
    : QAbstractListModel(parent)
 
34
    , m_appBackend(nullptr)
 
35
{
 
36
}
 
37
 
 
38
QVariant TransactionModel::data(const QModelIndex& index, int role) const
 
39
{
 
40
    if(!index.isValid())
 
41
        return QVariant();
 
42
 
 
43
    TransactionListener *trans = m_transactions.at(index.row());
 
44
 
 
45
    switch (role) {
 
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:
 
55
            return -1;
 
56
        case ApplicationModel::ActiveRole:
 
57
            return true;
 
58
        case ApplicationModel::ProgressRole:
 
59
            return trans->progress();
 
60
        case ApplicationModel::ProgressTextRole:
 
61
            return trans->comment();
 
62
        case ApplicationModel::InstalledRole:
 
63
            return false;
 
64
        case Qt::ToolTipRole:
 
65
            return QVariant();
 
66
        case ApplicationModel::ApplicationRole:
 
67
            return qVariantFromValue<QObject *>(trans->application());
 
68
        default:
 
69
            return QVariant();
 
70
    }
 
71
 
 
72
    return QVariant();
 
73
}
 
74
 
 
75
int TransactionModel::rowCount(const QModelIndex &parent) const
 
76
{
 
77
    if(parent.isValid())
 
78
        return 0;
 
79
    return m_transactions.size();
 
80
}
 
81
 
 
82
void TransactionModel::setBackend(ApplicationBackend* appBackend)
 
83
{
 
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*)));
 
89
}
 
90
 
 
91
void TransactionModel::addTransactions(const QList<Transaction *> &transList)
 
92
{
 
93
    for (Transaction *trans : transList) {
 
94
        addTransaction(trans);
 
95
    }
 
96
}
 
97
 
 
98
void TransactionModel::addTransaction(Transaction *trans)
 
99
{
 
100
    if (!trans || !trans->application())
 
101
        return;
 
102
 
 
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);
 
108
    endInsertRows();
 
109
}
 
110
 
 
111
void TransactionModel::removeTransaction(Application *app)
 
112
{
 
113
    TransactionListener *toRemove = nullptr;
 
114
    for (TransactionListener *listener : m_transactions) {
 
115
        if(listener->application() == app) {
 
116
            toRemove = listener;
 
117
            break;
 
118
        }
 
119
    }
 
120
 
 
121
    if (toRemove)
 
122
        removeTransaction(toRemove);
 
123
}
 
124
 
 
125
void TransactionModel::removeTransaction(TransactionListener *listener)
 
126
{
 
127
    if (listener) {
 
128
        int row = m_transactions.indexOf(listener);
 
129
        beginRemoveRows(QModelIndex(), row, row);
 
130
        m_transactions.removeAll(listener);
 
131
        delete listener;
 
132
        endRemoveRows();
 
133
    }
 
134
 
 
135
    if (!m_transactions.size())
 
136
        emit lastTransactionCancelled();
 
137
}
 
138
 
 
139
void TransactionModel::clear()
 
140
{
 
141
    beginResetModel();
 
142
    qDeleteAll(m_transactions);
 
143
    m_transactions.clear();
 
144
    endResetModel();
 
145
}
 
146
 
 
147
void TransactionModel::externalUpdate()
 
148
{
 
149
    emit dataChanged(QModelIndex(), QModelIndex());
 
150
}