File indexing completed on 2024-04-21 16:29:38

0001 /***************************************************************************
0002  *   Copyright (C) 2010-2011 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 "PkTransactionProgressModel.h"
0022 
0023 #include <QLoggingCategory>
0024 
0025 #include <PkStrings.h>
0026 
0027 #include "PkTransaction.h"
0028 
0029 Q_DECLARE_LOGGING_CATEGORY(APP_LIB)
0030 
0031 using namespace PackageKit;
0032 
0033 PkTransactionProgressModel::PkTransactionProgressModel(QObject *parent) :
0034     QStandardItemModel(parent)
0035 {
0036 
0037 }
0038 
0039 PkTransactionProgressModel::~PkTransactionProgressModel()
0040 {
0041 }
0042 
0043 void PkTransactionProgressModel::currentRepo(const QString &repoId, const QString &description, bool enabled)
0044 {
0045     Q_UNUSED(enabled)
0046 
0047     auto transaction = qobject_cast<PkTransaction *>(sender());
0048     if (transaction && transaction->flags() & Transaction::TransactionFlagSimulate) {
0049         return;
0050     }
0051 
0052     auto stdItem = new QStandardItem(description);
0053     stdItem->setData(repoId, RoleId);
0054     stdItem->setData(true,   RoleRepo);
0055     appendRow(stdItem);
0056 }
0057 
0058 void PkTransactionProgressModel::itemProgress(const QString &id, Transaction::Status status, uint percentage)
0059 {
0060     Q_UNUSED(status)
0061 
0062     auto transaction = qobject_cast<PkTransaction *>(sender());
0063     if (transaction && transaction->flags() & Transaction::TransactionFlagSimulate) {
0064         return;
0065     }
0066 
0067     QStandardItem *stdItem = findLastItem(id);
0068     if (stdItem && !stdItem->data(RoleFinished).toBool()) {
0069         // if the progress is unknown (101), make it empty
0070         if (percentage == 101) {
0071             percentage = 0;
0072         }
0073         if (stdItem->data(RoleProgress).toUInt() != percentage) {
0074             stdItem->setData(percentage, RoleProgress);
0075         }
0076     }
0077 }
0078 
0079 void PkTransactionProgressModel::clear()
0080 {
0081     removeRows(0, rowCount());
0082 }
0083 
0084 QHash<int, QByteArray> PkTransactionProgressModel::roleNames() const
0085 {
0086     QHash<int, QByteArray> roles;
0087     roles[RoleInfo] = "rInfo";
0088     roles[RolePkgName] = "rPkgName";
0089     roles[RolePkgSummary] = "rPkgSummary";
0090     roles[RoleFinished] = "rFinished";
0091     roles[RoleProgress] = "rProgress";
0092     roles[RoleId] = "rId";
0093     roles[RoleRepo] = "rRepo";
0094     return roles;
0095 }
0096 
0097 void PkTransactionProgressModel::currentPackage(PackageKit::Transaction::Info info, const QString &packageID, const QString &summary)
0098 {
0099     auto transaction = qobject_cast<PkTransaction *>(sender());
0100     if (transaction &&
0101             (transaction->flags() & Transaction::TransactionFlagSimulate ||
0102              transaction->cachedRole() == Transaction::RoleResolve ||
0103              transaction->cachedRole() == Transaction::RoleWhatProvides)) {
0104         return;
0105     }
0106 
0107     if (!packageID.isEmpty()) {
0108         QStandardItem *stdItem = findLastItem(packageID);
0109         // If there is alread some packages check to see if it has
0110         // finished, if the progress is 100 create a new item for the next task
0111         if (stdItem && !stdItem->data(RoleFinished).toBool()) {
0112             // if the item status (info) changed update it
0113             if (stdItem->data(RoleInfo).value<Transaction::Info>() != info) {
0114                 // If the package task has finished set progress to 100
0115                 if (info == Transaction::InfoFinished) {
0116                     itemFinished(stdItem);
0117                 } else {
0118                     stdItem->setData(qVariantFromValue(info), RoleInfo);
0119                     stdItem->setText(PkStrings::infoPresent(info));
0120                 }
0121             }
0122         } else if (info != Transaction::InfoFinished) {
0123             QList<QStandardItem *> items;
0124             // It's a new package create it and append it
0125             stdItem = new QStandardItem;
0126             stdItem->setText(PkStrings::infoPresent(info));
0127             stdItem->setData(Transaction::packageName(packageID), RolePkgName);
0128             stdItem->setData(summary, RolePkgSummary);
0129             stdItem->setData(qVariantFromValue(info), RoleInfo);
0130             stdItem->setData(0,         RoleProgress);
0131             stdItem->setData(false,     RoleFinished);
0132             stdItem->setData(packageID, RoleId);
0133             stdItem->setData(false,     RoleRepo);
0134             items << stdItem;
0135 
0136             stdItem = new QStandardItem(Transaction::packageName(packageID));
0137             stdItem->setToolTip(Transaction::packageVersion(packageID));
0138             items << stdItem;
0139 
0140             stdItem = new QStandardItem(summary);
0141             stdItem->setToolTip(summary);
0142             items << stdItem;
0143 
0144             appendRow(items);
0145         }
0146     }
0147 }
0148 
0149 void PkTransactionProgressModel::itemFinished(QStandardItem *stdItem)
0150 {
0151     // Point to the item before it
0152     int count = stdItem->row() - 1;
0153 
0154     // Find the last finished item
0155     bool found = false;
0156     while (count >= 0) {
0157         // Put it after the finished item
0158         // so that running items can be kept
0159         // at the bottom
0160         if (item(count)->data(RoleFinished).toBool()) {
0161             // make sure it won't end in the same position
0162             if (count + 1 != stdItem->row()) {
0163                 QList<QStandardItem*> items;
0164                 items = takeRow(stdItem->row());
0165                 insertRow(count + 1, items);
0166             }
0167             found = true;
0168             break;
0169         }
0170         --count;
0171     }
0172 
0173     // If it's not at the top of the list
0174     // and no FINISHED Item was found move it there
0175     if (!found && stdItem->row() != 0) {
0176         insertRow(0, takeRow(stdItem->row()));
0177     }
0178 
0179     Transaction::Info info = stdItem->data(RoleInfo).value<Transaction::Info>();
0180     stdItem->setText(PkStrings::infoPast(info));
0181     stdItem->setData(100,  RoleProgress);
0182     stdItem->setData(true, RoleFinished);
0183 }
0184 
0185 QStandardItem* PkTransactionProgressModel::findLastItem(const QString &packageID)
0186 {
0187     int rows = rowCount() - 1;
0188     for (int i = rows; i >= 0; --i) {
0189         QStandardItem *stdItem = item(i);
0190         if (stdItem->data(RoleId).toString() == packageID) {
0191             return stdItem;
0192         }
0193     }
0194     return nullptr;
0195 }
0196 
0197 #include "moc_PkTransactionProgressModel.cpp"