本篇主要讲述,如何调用PackageKit c++库的接口,只举例个别接口,基本上通用。
//检测更新
auto refresher = PackageKit::Daemon::refreshCache(false);connect(refresher, &PackageKit::Transaction::errorCode, this, &MainWindow::errorMessage);auto success = connect(refresher, &PackageKit::Transaction::finished,this, &MainWindow::setupGetUpdateTransaction);if (!success) {qDebug() << "Failed to connect signal and slot";} else {qDebug() << "true";}
}void MainWindow::setupGetUpdateTransaction()
{qDebug() << "detection System Updates";PackageKit::Daemon *daemon = PackageKit::Daemon::global();PackageKit::Transaction *tx = daemon->getUpdates();connect(tx, &PackageKit::Transaction::errorCode, this, &MainWindow::errorMessage);connect(tx, &PackageKit::Transaction::package, this, &MainWindow::saveUpgradePackage);connect(tx, &PackageKit::Transaction::finished, this, &MainWindow::upgradeSystem);connect(tx, &PackageKit::Transaction::percentageChanged, this, [=](){qDebug() << "getUpdates percentageChanged" << tx->percentage();});
}//更新包if (m_updateSecurityList.empty() && m_updateSystemList.empty()) return;PackageKit::Transaction *tx = PackageKit::Daemon::updatePackages(m_updateSecurityList.values());connect(tx, &PackageKit::Transaction::errorCode, this, &MainWindow::errorMessage);connect(tx, &PackageKit::Transaction::package, this, &MainWindow::deleteUpgradedPackage);connect(tx, &PackageKit::Transaction::finished, this, [=](){qDebug() << "update finish";});connect(tx, &PackageKit::Transaction::percentageChanged, this, [=](){qDebug() << "Updates percentageChanged" << tx->percentage();});//区分包的种类void MainWindow::saveUpgradePackage(PackageKit::Transaction::Info info,const QString &packageID,const QString &summary)
{qDebug() << "info is " << info << PackageKit::Daemon::packageName(packageID);if (info == PackageKit::Transaction::InfoSecurity) {m_updateSecurityList.insert(packageID);} else {m_updateSystemList.insert(packageID);}
}void MainWindow::deleteUpgradedPackage(PackageKit::Transaction::Info info, const QString &packageID, const QString &summary)
{if (info == PackageKit::Transaction::InfoSecurity && m_updateSecurityList.contains(packageID)) {m_updateSecurityList.remove(packageID);} else if (m_updateSystemList.contains(packageID)){m_updateSystemList.remove(packageID);}
}
PackageKit 有很多封装好的接口,并且有相应的信号传送,可以下载源码,查看头文件,注释写的很详细。