File: TransactionModel.cpp

package info (click to toggle)
apper 1.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,244 kB
  • sloc: cpp: 14,383; xml: 970; makefile: 13; sh: 4
file content (157 lines) | stat: -rw-r--r-- 5,516 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
 *   Copyright (C) 2009-2010 by Daniel Nicoletti                           *
 *   dantti12@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; see the file COPYING. If not, write to       *
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 *   Boston, MA 02110-1301, USA.                                           *
 ***************************************************************************/

#include "TransactionModel.h"

#include <KUser>
#include <KFormat>
#include <KLocalizedString>
#include <QLoggingCategory>

#include <PkIcons.h>
#include <PkStrings.h>

using namespace PackageKit;

TransactionModel::TransactionModel(QObject *parent)
: QStandardItemModel(parent)
{
    setSortRole(Qt::DisplayRole);
    clear();
}

void TransactionModel::clear()
{
    QStandardItemModel::clear();
    setHorizontalHeaderItem(0, new QStandardItem(i18n("Date")));
    setHorizontalHeaderItem(1, new QStandardItem(i18n("Action")));
    setHorizontalHeaderItem(2, new QStandardItem(i18n("Details")));
    setHorizontalHeaderItem(3, new QStandardItem(i18nc("Machine user who issued the transaction", "Username")));
    setHorizontalHeaderItem(4, new QStandardItem(i18n("Application")));
}

void TransactionModel::addTransaction(PackageKit::Transaction *trans)
{
    auto dateI    = new QStandardItem;
    auto roleI    = new QStandardItem;
    auto detailsI = new QStandardItem;
    auto userI    = new QStandardItem;
    auto appI     = new QStandardItem;

    dateI->setText(QLocale::system().toString(trans->timespec().date()));
    // this is for the filterSort model
    dateI->setData(trans->timespec(), Qt::UserRole);
    dateI->setEditable(false);

    roleI->setText(PkStrings::actionPast(trans->role()));
    roleI->setIcon(PkIcons::actionIcon(trans->role()));
    roleI->setEditable(false);

    detailsI->setText(getDetailsLocalized(trans->data()));
    detailsI->setEditable(false);

    KUser user(trans->uid());
    QString display;
    if (!user.property(KUser::FullName).toString().isEmpty()) {
        display = user.property(KUser::FullName).toString() + QLatin1String(" (") + user.loginName() + QLatin1Char(')');
    } else {
        display = user.loginName();
    }
    userI->setText(display);
    userI->setEditable(false);

    appI->setText(trans->cmdline());
    appI->setEditable(false);

    appendRow({
                  dateI,
                  roleI,
                  detailsI,
                  userI,
                  appI
              });
    delete trans;
}

QString TransactionModel::getDetailsLocalized(const QString &data) const
{
    QStringList lines = data.split(QLatin1Char('\n'));
    QStringList ret;

    QString text;
    text = getTypeLine(lines, Transaction::StatusInstall);
    if (!text.isNull()) {
        ret << text;
    }

    text = getTypeLine(lines, Transaction::StatusRemove);
    if (!text.isNull()) {
        ret << text;
    }

    text = getTypeLine(lines, Transaction::StatusUpdate);
    if (!text.isNull()) {
        ret << text;
    }

    return ret.join(QLatin1Char('\n'));
}

QString TransactionModel::getTypeLine(const QStringList &lines, Transaction::Status status) const
{
    QStringList text;
    for (const QString &line : lines) {
        const QStringList sections = line.split(QLatin1Char('\t'));
        if (sections.size() > 1) {
            switch (status) {
                case Transaction::StatusInstall:
                    if (sections.at(0) != QLatin1String("installing")) {
                        continue;
                    }
                    break;
                case Transaction::StatusRemove:
                    if (sections.at(0) != QLatin1String("removing")) {
                        continue;
                    }
                    break;
                case Transaction::StatusUpdate:
                    if (sections.at(0) != QLatin1String("updating")) {
                        continue;
                    }
                    break;
                default:
                    continue;
            }
            QStringList packageData = sections.at(1).split(QLatin1Char(';'));
            if (packageData.size()) {
                text << packageData.at(0);
            }
        }
    }

    if (text.size()) {
        // TODO make the status BOLD
        return PkStrings::statusPast(status) + QLatin1String(": ") + text.join(QLatin1String(", "));
    } else {
        return QString();
    }
}

#include "moc_TransactionModel.cpp"