File indexing completed on 2024-04-21 05:46:51

0001 /*
0002     Copyright © 2017 Harald Sitter <sitter@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), which shall
0010     act as a proxy defined in Section 6 of version 3 of the license.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include "TransactionModel.h"
0022 
0023 #include <QMetaEnum>
0024 
0025 #include <PackageKit/Daemon>
0026 
0027 #include "Debug.h"
0028 #include "Transaction.h"
0029 
0030 TransactionModel::TransactionModel(QObject *parent)
0031     : AbstractModel(&m_objMap, parent)
0032 {
0033     initRoleNames(Transaction::staticMetaObject);
0034 
0035     connect(PackageKit::Daemon::global(), &PackageKit::Daemon::transactionListChanged,
0036             this, &TransactionModel::onTransactionsChanged);
0037 
0038     // Fetch what is available right now.
0039     auto reply = PackageKit::Daemon::getTransactionList();
0040     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
0041     QObject::connect(watcher, &QDBusPendingCallWatcher::finished,
0042                      [&](QDBusPendingCallWatcher *call) {
0043         QDBusPendingReply<QList<QDBusObjectPath>> reply = *call;
0044         onTransactionObjectPathsChanged(reply.argumentAt<0>());
0045         call->deleteLater();
0046     });
0047 }
0048 
0049 void TransactionModel::onTransactionObjectPathsChanged(const QList<QDBusObjectPath> &newTids)
0050 {
0051     // Convert to StringList. Slight waste of time, but probably not slower as this is only used
0052     // For the initial list, whereas the StringList based code is used for everything else.
0053     QStringList strList;
0054     for (auto objectPath : newTids) {
0055         strList << objectPath.path();
0056     }
0057 
0058     onTransactionsChanged(strList);
0059 
0060     if (m_objMap.isEmpty()) {
0061         // On start suggest that the daemon quits again if it has no.
0062         // It ought to be noted that later we should no longer suggest this as it can cause quits
0063         // clients do not expect.
0064         PackageKit::Daemon::suggestDaemonQuit();
0065     }
0066 }
0067 
0068 void TransactionModel::onTransactionsChanged(const QStringList &newTids)
0069 {
0070     qDebug() << "transactions changed";
0071     qDebug() << newTids;
0072     for (auto it = m_objMap.begin(); it != m_objMap.end();) {
0073         if (newTids.contains(it.key())) {
0074             // Keep
0075             ++it;
0076         } else {
0077             qDebug() << "dropping transaction" << it.key();
0078             // Delete
0079             auto index = m_objMap.keys().indexOf(it.key());
0080             auto transaction = it.value();
0081             it = m_objMap.erase(it);
0082             onDataRemoved(index);
0083             transaction->deleteLater();
0084         }
0085     }
0086     auto knownTids = m_objMap.keys();
0087     for (const auto &tid : newTids) {
0088         if (knownTids.contains(tid)) {
0089             continue;
0090         }
0091 
0092         qDebug() << "adding transaction" << tid;
0093         auto it = m_objMap.insert(tid, new Transaction(QDBusObjectPath(tid)));
0094         onDataAdded(m_objMap.keys().indexOf(it.key()));
0095 
0096     }
0097 }