File indexing completed on 2022-09-27 16:24:30

0001 /***************************************************************************
0002  *   Copyright (C) 2009-2010 by Daniel Nicoletti                           *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "TransactionModel.h"
0022 
0023 #include <KUser>
0024 #include <KFormat>
0025 #include <KLocalizedString>
0026 #include <QLoggingCategory>
0027 
0028 #include <PkIcons.h>
0029 #include <PkStrings.h>
0030 
0031 using namespace PackageKit;
0032 
0033 TransactionModel::TransactionModel(QObject *parent)
0034 : QStandardItemModel(parent)
0035 {
0036     setSortRole(Qt::DisplayRole);
0037     clear();
0038 }
0039 
0040 void TransactionModel::clear()
0041 {
0042     QStandardItemModel::clear();
0043     setHorizontalHeaderItem(0, new QStandardItem(i18n("Date")));
0044     setHorizontalHeaderItem(1, new QStandardItem(i18n("Action")));
0045     setHorizontalHeaderItem(2, new QStandardItem(i18n("Details")));
0046     setHorizontalHeaderItem(3, new QStandardItem(i18nc("Machine user who issued the transaction", "Username")));
0047     setHorizontalHeaderItem(4, new QStandardItem(i18n("Application")));
0048 }
0049 
0050 void TransactionModel::addTransaction(PackageKit::Transaction *trans)
0051 {
0052     auto dateI    = new QStandardItem;
0053     auto roleI    = new QStandardItem;
0054     auto detailsI = new QStandardItem;
0055     auto userI    = new QStandardItem;
0056     auto appI     = new QStandardItem;
0057 
0058     dateI->setText(QLocale::system().toString(trans->timespec().date()));
0059     // this is for the filterSort model
0060     dateI->setData(trans->timespec(), Qt::UserRole);
0061     dateI->setEditable(false);
0062 
0063     roleI->setText(PkStrings::actionPast(trans->role()));
0064     roleI->setIcon(PkIcons::actionIcon(trans->role()));
0065     roleI->setEditable(false);
0066 
0067     detailsI->setText(getDetailsLocalized(trans->data()));
0068     detailsI->setEditable(false);
0069 
0070     KUser user(trans->uid());
0071     QString display;
0072     if (!user.property(KUser::FullName).toString().isEmpty()) {
0073         display = user.property(KUser::FullName).toString() + QLatin1String(" (") + user.loginName() + QLatin1Char(')');
0074     } else {
0075         display = user.loginName();
0076     }
0077     userI->setText(display);
0078     userI->setEditable(false);
0079 
0080     appI->setText(trans->cmdline());
0081     appI->setEditable(false);
0082 
0083     appendRow({
0084                   dateI,
0085                   roleI,
0086                   detailsI,
0087                   userI,
0088                   appI
0089               });
0090     delete trans;
0091 }
0092 
0093 QString TransactionModel::getDetailsLocalized(const QString &data) const
0094 {
0095     QStringList lines = data.split(QLatin1Char('\n'));
0096     QStringList ret;
0097 
0098     QString text;
0099     text = getTypeLine(lines, Transaction::StatusInstall);
0100     if (!text.isNull()) {
0101         ret << text;
0102     }
0103 
0104     text = getTypeLine(lines, Transaction::StatusRemove);
0105     if (!text.isNull()) {
0106         ret << text;
0107     }
0108 
0109     text = getTypeLine(lines, Transaction::StatusUpdate);
0110     if (!text.isNull()) {
0111         ret << text;
0112     }
0113 
0114     return ret.join(QLatin1Char('\n'));
0115 }
0116 
0117 QString TransactionModel::getTypeLine(const QStringList &lines, Transaction::Status status) const
0118 {
0119     QStringList text;
0120     for (const QString &line : lines) {
0121         const QStringList sections = line.split(QLatin1Char('\t'));
0122         if (sections.size() > 1) {
0123             switch (status) {
0124                 case Transaction::StatusInstall:
0125                     if (sections.at(0) != QLatin1String("installing")) {
0126                         continue;
0127                     }
0128                     break;
0129                 case Transaction::StatusRemove:
0130                     if (sections.at(0) != QLatin1String("removing")) {
0131                         continue;
0132                     }
0133                     break;
0134                 case Transaction::StatusUpdate:
0135                     if (sections.at(0) != QLatin1String("updating")) {
0136                         continue;
0137                     }
0138                     break;
0139                 default:
0140                     continue;
0141             }
0142             QStringList packageData = sections.at(1).split(QLatin1Char(';'));
0143             if (packageData.size()) {
0144                 text << packageData.at(0);
0145             }
0146         }
0147     }
0148 
0149     if (text.size()) {
0150         // TODO make the status BOLD
0151         return PkStrings::statusPast(status) + QLatin1String(": ") + text.join(QLatin1String(", "));
0152     } else {
0153         return QString();
0154     }
0155 }
0156 
0157 #include "moc_TransactionModel.cpp"