repoindex/alpm/updatelookup.h

146 lines
3.7 KiB
C++

#ifndef PACKAGEMANAGEMENT_UPDATELOOKUP_H
#define PACKAGEMANAGEMENT_UPDATELOOKUP_H
#include "package.h"
#include <QObject>
#include <QJsonObject>
#include <QJsonArray>
#include <QFutureWatcher>
#include <functional>
QT_FORWARD_DECLARE_CLASS(QNetworkReply)
namespace PackageManagement {
class Manager;
class AlpmDataBase;
typedef std::function<void (const QJsonObject &&results)> UpdateLookupCallback;
template<class Package, class VersionType = QString>
class UpdateResult
{
public:
UpdateResult(const Package &package, const VersionType &previousVersion);
Package package;
VersionType previousVersion;
QJsonObject json() const;
};
template<class Package, class VersionType>
inline UpdateResult<Package, VersionType>::UpdateResult(const Package &package, const VersionType &previousVersion) :
package(package),
previousVersion(previousVersion)
{}
template<class Package, class VersionType>
QJsonObject UpdateResult<Package, VersionType>::json() const
{
QJsonObject obj;
obj.insert(QStringLiteral("pkg"), package.basicInfo(true));
obj.insert(QStringLiteral("prevVersion"), previousVersion);
return obj;
}
template<class Package>
inline UpdateResult<Package> makeUpdateResult(const Package &package, const QString &previousVersion)
{
return UpdateResult<Package>(package, previousVersion);
}
template<class Package>
inline UpdateResult<Package> makeUpdateResult(const Package &package, const char *previousVersion)
{
return UpdateResult<Package>(package, QString::fromLocal8Bit(previousVersion));
}
template<class Package>
class UpdateLookupResults
{
public:
UpdateLookupResults();
/*!
* \brief Indicates that there are no upgrade sources available.
*/
bool noSources;
/*!
* \brief Packages providing a software upgrade (new version).
*/
QList<UpdateResult<Package> > newVersions;
/*!
* \brief Package upgrades only (new release).
*/
QList<UpdateResult<Package> > newReleases;
/*!
* \brief Downgrades (older version in sync db).
*/
QList<UpdateResult<Package> > downgrades;
/*!
* \brief Orphaned packages (could not be found in any of the sync dbs).
*/
QSet<AlpmPackage> orphaned;
/*!
* \brief Warnings occured when checking for updates.
*/
QStringList warnings;
/*!
* \brief Errors occured when checking for updates.
*/
QStringList errors;
};
/*!
* \brief Constructs new update lookup results.
*/
template<class Package>
inline UpdateLookupResults<Package>::UpdateLookupResults() :
noSources(false)
{}
class UpdateLookup : public QObject
{
Q_OBJECT
public:
explicit UpdateLookup(const Manager &manager, const QJsonObject &request, const UpdateLookupCallback callback, QObject *parent = nullptr);
private slots:
UpdateLookupResults<AlpmPackage> checkSyncDbs(const QJsonValue &syncDbsValue);
void aurPackageInfoAvailable(const QNetworkReply *reply);
UpdateLookupResults<AurPackage> checkAur();
void lookupDone();
private:
const Manager &m_manager;
const QJsonObject m_request;
const UpdateLookupCallback m_callback;
const AlpmDataBase *m_db;
QFutureWatcher<UpdateLookupResults<AlpmPackage> > *m_syncDbsWatcher;
QFutureWatcher<UpdateLookupResults<AurPackage> > *m_aurWatcher;
UpdateLookupResults<AlpmPackage> m_syncDbsResults;
UpdateLookupResults<AurPackage> m_aurResults;
QNetworkReply *m_aurReply;
bool m_sourcesAvailable;
QJsonArray m_warnings;
QJsonArray m_errors;
QJsonArray m_softwareUpdates;
QJsonArray m_packageOnlyUpdates;
QJsonArray m_downgrades;
QJsonArray m_orphanedPackages;
};
} // namespace PackageManagement
#endif // PACKAGEMANAGEMENT_UPDATELOOKUP_H