Commit 6d63fd35 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Fix dependency counting

Only do it for the transactions we need.
parent 898cddd9
......@@ -567,23 +567,6 @@ void PackageKitBackend::performDetailsFetch()
PackageKit::Transaction* transaction = PackageKit::Daemon::getDetails(ids);
connect(transaction, &PackageKit::Transaction::details, this, &PackageKitBackend::packageDetails);
connect(transaction, &PackageKit::Transaction::errorCode, this, &PackageKitBackend::transactionError);
QSharedPointer<QMap<QString, int>> packageDependencies(new QMap<QString, int>);
auto trans = PackageKit::Daemon::dependsOn(ids);
connect(trans, &PackageKit::Transaction::package, this, [packageDependencies](PackageKit::Transaction::Info /*info*/, const QString &packageID, const QString &/*summary*/) {
(*packageDependencies)[packageID] += 1;
});
connect(trans, &PackageKit::Transaction::finished, this, [this, packageDependencies](PackageKit::Transaction::Exit /*status*/) {
auto pkgDeps = (*packageDependencies);
for (auto it = pkgDeps.constBegin(), itEnd = pkgDeps.constEnd(); it != itEnd; ++it) {
const auto resources = resourcesByPackageName(PackageKit::Daemon::packageName(it.key()));
for(auto resource : resources) {
auto pkres = qobject_cast<PackageKitResource*>(resource);
pkres->setDependenciesCount(it.value());
}
}
});
}
void PackageKitBackend::checkDaemonRunning()
......
......@@ -79,6 +79,7 @@ class DISCOVERCOMMON_EXPORT PackageKitBackend : public AbstractResourcesBackend
public Q_SLOTS:
void reloadPackageList();
void refreshDatabase();
void transactionError(PackageKit::Transaction::Error, const QString& message);
private Q_SLOTS:
void getPackagesFinished();
......@@ -86,7 +87,6 @@ class DISCOVERCOMMON_EXPORT PackageKitBackend : public AbstractResourcesBackend
void addPackageArch(PackageKit::Transaction::Info info, const QString &packageId, const QString &summary);
void addPackageNotArch(PackageKit::Transaction::Info info, const QString &packageId, const QString &summary);
void packageDetails(const PackageKit::Details& details);
void transactionError(PackageKit::Transaction::Error, const QString& message);
void addPackageToUpdate(PackageKit::Transaction::Info, const QString& pkgid, const QString& summary);
void getUpdatesFinished(PackageKit::Transaction::Exit,uint);
void getUpdatesDetailsFinished(PackageKit::Transaction::Exit,uint);
......
......@@ -25,15 +25,17 @@
#include <KLocalizedString>
#include <PackageKit/Details>
#include <PackageKit/Daemon>
#include <QJsonArray>
#include <QDebug>
PackageKitResource::PackageKitResource(QString packageName, QString summary, PackageKitBackend* parent)
: AbstractResource(parent)
, m_summary(std::move(summary))
, m_name(std::move(packageName))
, m_dependenciesCount(0)
{
setObjectName(m_name);
connect(this, &PackageKitResource::dependenciesFound, this, [this](const QJsonObject& obj) { setDependenciesCount(obj.size()); });
}
QString PackageKitResource::name() const
......@@ -176,7 +178,7 @@ void PackageKitResource::failedFetchingDetails(PackageKit::Transaction::Error, c
qWarning() << "error fetching details" << msg;
}
void PackageKitResource::setDependenciesCount(uint deps)
void PackageKitResource::setDependenciesCount(int deps)
{
if (deps != m_dependenciesCount) {
m_dependenciesCount = deps;
......@@ -254,8 +256,31 @@ PackageKitBackend* PackageKitResource::backend() const
QString PackageKitResource::sizeDescription()
{
if (m_dependenciesCount == 0)
if (m_dependenciesCount < 0) {
fetchDetails();
fetchDependencies();
}
if (m_dependenciesCount <= 0)
return AbstractResource::sizeDescription();
else
return i18np("%2 (plus %1 dependency)", "%2 (plus %1 dependencies)", m_dependenciesCount, AbstractResource::sizeDescription());
}
void PackageKitResource::fetchDependencies()
{
const auto id = availablePackageId();
if (id.isEmpty())
return;
m_dependenciesCount = 0;
QSharedPointer<QJsonObject> packageDependencies(new QJsonObject);
auto trans = PackageKit::Daemon::installPackage(id, PackageKit::Transaction::TransactionFlagSimulate);
connect(trans, &PackageKit::Transaction::errorCode, backend(), &PackageKitBackend::transactionError);
connect(trans, &PackageKit::Transaction::package, this, [packageDependencies](PackageKit::Transaction::Info info, const QString &packageID, const QString &summary) {
(*packageDependencies)[PackageKit::Daemon::packageName(packageID)] = summary ;
});
connect(trans, &PackageKit::Transaction::finished, this, [this, packageDependencies](PackageKit::Transaction::Exit status) {
Q_EMIT dependenciesFound(*packageDependencies);
});
}
......@@ -67,7 +67,10 @@ class PackageKitResource : public AbstractResource
bool canExecute() const override { return false; }
QString sizeDescription() override;
void setDependenciesCount(uint count);
void setDependenciesCount(int count);
Q_SIGNALS:
void dependenciesFound(const QJsonObject& dependencies);
public Q_SLOTS:
void addPackageId(PackageKit::Transaction::Info info, const QString &packageId, bool arch);
......@@ -90,6 +93,8 @@ class PackageKitResource : public AbstractResource
void failedFetchingDetails(PackageKit::Transaction::Error, const QString& msg);
private:
void fetchDependencies();
void setDependencies(const QStringList &deps);
/** fetches details individually, it's better if done in batch, like for updates */
void fetchDetails();
......@@ -97,7 +102,7 @@ class PackageKitResource : public AbstractResource
const QString m_summary;
const QString m_name;
PackageKit::Details m_details;
uint m_dependenciesCount;
int m_dependenciesCount = -1;
};
#endif // PACKAGEKITRESOURCE_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment