repoindex/alpm/package.h

497 lines
11 KiB
C
Raw Normal View History

2015-08-10 22:46:01 +02:00
#ifndef ALPM_PACKAGE_H
#define ALPM_PACKAGE_H
#include "list.h"
#include <c++utilities/chrono/datetime.h>
#include <alpm.h>
#include <list>
#include <string>
#include <QString>
QT_BEGIN_NAMESPACE
class QJsonObject;
class QJsonValue;
QT_END_NAMESPACE
namespace PackageManagement {
/*!
* \brief The PackageVersionComparsion enum defines possible results of packages version comparison.
*/
enum class PackageVersionComparsion
{
Equal, /*!< The version of this package is the same as the version of the package from the sync db. */
SoftwareUpgrade, /*!< The software version of the package from the sync db is newer. */
PackageUpgradeOnly, /*!< The package release number of the package from the sync db is newer. */
NewerThenSyncVersion /*!< The version of this package is NEWER then the version of the package from the sync db. */
};
/*!
* \brief The ComparsionResult enum defines possible results of package version part comparsion.
*/
enum class PackageVersionPartComparsion
{
Equal, /*!< Both parts are equal. */
Newer, /*!< Part 1 is newer then part 2. */
Older /*!< Part 2 is newer then part 1. */
};
class PackageVersion
{
public:
PackageVersion(const char *versionStr);
static PackageVersionPartComparsion compareParts(const QString &part1, const QString &part2);
PackageVersionComparsion compare(const PackageVersion &other) const;
QString epoch;
QString version;
QString release;
};
class AurPackage
{
public:
AurPackage();
AurPackage(const QJsonValue &aurJsonValue);
bool isValid() const;
int id() const;
int categoryId() const;
const QString &name() const;
const QString &description() const;
const QString &upstreamUrl() const;
int votes() const;
bool isOutOfDate() const;
const QString &maintainer() const;
ChronoUtilities::DateTime firstSubmitted() const;
ChronoUtilities::DateTime lastModified() const;
const QString &license() const;
const QString &tarUrl() const;
private:
int m_id;
int m_categoryId;
QString m_name;
QString m_description;
QString m_upstreamUrl;
int m_votes;
bool m_outOfDate;
QString m_maintainer;
ChronoUtilities::DateTime m_firstSubmitted;
ChronoUtilities::DateTime m_lastModified;
QString m_license;
QString m_tarUrl;
};
/*!
* \brief Constructs a empty, invalid AUR package.
*/
inline AurPackage::AurPackage() :
m_id(-1),
m_categoryId(-1),
m_votes(-1),
m_outOfDate(false)
{}
/*!
* \brief Returns an indication whether the package is valid.
*/
inline bool AurPackage::isValid() const
{
return m_id >= 0;
}
/*!
* \brief Returns the ID of the package in the AUR.
*/
inline int AurPackage::id() const
{
return m_id;
}
/*!
* \brief Returns the category ID of the package in the AUR.
*/
inline int AurPackage::categoryId() const
{
return m_categoryId;
}
/*!
* \brief Returns the name of the package.
*/
inline const QString &AurPackage::name() const
{
return m_name;
}
/*!
* \brief Returns the description of the package.
*/
inline const QString &AurPackage::description() const
{
return m_description;
}
/*!
* \brief Returns the upstream URL of the package.
*/
inline const QString &AurPackage::upstreamUrl() const
{
return m_upstreamUrl;
}
/*!
* \brief Returns the votes of the package in the AUR.
*/
inline int AurPackage::votes() const
{
return m_votes;
}
/*!
* \brief Returns wheter the package is flagged as out-of-date.
*/
inline bool AurPackage::isOutOfDate() const
{
return m_outOfDate;
}
/*!
* \brief Returns the maintainer of the package.
*/
inline const QString &AurPackage::maintainer() const
{
return m_maintainer;
}
/*!
* \brief Returns the time the package was first submitted.
*/
inline ChronoUtilities::DateTime AurPackage::firstSubmitted() const
{
return m_firstSubmitted;
}
/*!
* \brief Returns the time the package was last modified.
*/
inline ChronoUtilities::DateTime AurPackage::lastModified() const
{
return m_lastModified;
}
/*!
* \brief Returns the license.
*/
inline const QString &AurPackage::license() const
{
return m_license;
}
/*!
* \brief Returns a URL to the tar file from AUR.
*/
inline const QString &AurPackage::tarUrl() const
{
return m_tarUrl;
}
class AlpmPackage
{
public:
AlpmPackage(alpm_pkg_t *package = nullptr);
// package properties
alpm_pkg_t *ptr();
bool hasInstallScript() const;
const char *fileName() const;
const char *name() const;
const char *version() const;
PackageVersionComparsion compareVersion(const AlpmPackage &syncPackage) const;
alpm_pkgfrom_t origin() const;
const char *description() const;
const char *upstreamUrl() const;
ChronoUtilities::DateTime buildDate() const;
ChronoUtilities::DateTime installDate() const;
const char *packager() const;
const char *md5() const;
const char *sha256() const;
const char *architecture() const;
off_t packageSize() const;
off_t installedSize() const;
alpm_pkgreason_t installReason() const;
void setInstallReason(alpm_pkgreason_t reason);
StringList licenses() const;
StringList groups() const;
DependencyList dependencies() const;
DependencyList optionalDependencies() const;
DependencyList conflicts() const;
DependencyList provides() const;
StringList deltas() const;
DependencyList replaces() const;
alpm_filelist_t *files() const;
AlpmList<alpm_backup_t *> backupFiles() const;
alpm_db_t *associatedDatabase() const;
const char *base64Signature() const;
alpm_pkgvalidation_t validation() const;
StringList requiredBy() const;
StringList optionalFor() const;
void *openChangelog() const;
size_t readChangelog(void *changelog, void *buffer, size_t bufferSize);
bool closeChangelog(void *changelog);
operator bool() const;
// JSON serialization
QJsonObject basicInfo(bool includeRepoAndName = false) const;
QJsonObject fullInfo(bool includeRepoAndName = false) const;
protected:
alpm_pkg_t *m_ptr;
};
/*!
* \brief Constructs a new package instance for the specified ALPM \a package.
*/
inline AlpmPackage::AlpmPackage(alpm_pkg_t *package) :
m_ptr(package)
{}
inline alpm_pkg_t *AlpmPackage::ptr()
{
return m_ptr;
}
inline bool AlpmPackage::hasInstallScript() const
{
return alpm_pkg_has_scriptlet(m_ptr);
}
inline const char *AlpmPackage::fileName() const
{
return alpm_pkg_get_filename(m_ptr);
}
inline const char *AlpmPackage::name() const
{
return alpm_pkg_get_name(m_ptr);
}
inline const char *AlpmPackage::version() const
{
return alpm_pkg_get_version(m_ptr);
}
/*!
* \brief Compares the version of this package with the version of the specified package.
*
* This method distinguishes between software upgrades and package releases. See Alpm::PackageVersionComparsion enum.
*/
inline PackageVersionComparsion AlpmPackage::compareVersion(const AlpmPackage &syncPackage) const
{
return PackageVersion(version()).compare(PackageVersion(syncPackage.version()));
}
inline alpm_pkgfrom_t AlpmPackage::origin() const
{
return alpm_pkg_get_origin(m_ptr);
}
inline const char *AlpmPackage::description() const
{
return alpm_pkg_get_desc(m_ptr);
}
inline const char *AlpmPackage::upstreamUrl() const
{
return alpm_pkg_get_url(m_ptr);
}
inline ChronoUtilities::DateTime AlpmPackage::buildDate() const
{
return ChronoUtilities::DateTime::fromTimeStamp(static_cast<time_t>(alpm_pkg_get_builddate(m_ptr)));
}
inline ChronoUtilities::DateTime AlpmPackage::installDate() const
{
return ChronoUtilities::DateTime::fromTimeStamp(static_cast<time_t>(alpm_pkg_get_installdate(m_ptr)));
}
inline const char *AlpmPackage::packager() const
{
return alpm_pkg_get_packager(m_ptr);
}
inline const char *AlpmPackage::md5() const
{
return alpm_pkg_get_md5sum(m_ptr);
}
inline const char *AlpmPackage::sha256() const
{
return alpm_pkg_get_sha256sum(m_ptr);
}
inline const char *AlpmPackage::architecture() const
{
return alpm_pkg_get_arch(m_ptr);
}
inline off_t AlpmPackage::packageSize() const
{
return alpm_pkg_get_size(m_ptr);
}
inline off_t AlpmPackage::installedSize() const
{
return alpm_pkg_get_isize(m_ptr);
}
inline alpm_pkgreason_t AlpmPackage::installReason() const
{
return alpm_pkg_get_reason(m_ptr);
}
inline StringList AlpmPackage::licenses() const
{
return alpm_pkg_get_licenses(m_ptr);
}
inline StringList AlpmPackage::groups() const
{
return alpm_pkg_get_groups(m_ptr);
}
inline DependencyList AlpmPackage::dependencies() const
{
return alpm_pkg_get_depends(m_ptr);
}
inline DependencyList AlpmPackage::optionalDependencies() const
{
return alpm_pkg_get_optdepends(m_ptr);
}
inline DependencyList AlpmPackage::conflicts() const
{
return alpm_pkg_get_conflicts(m_ptr);
}
inline DependencyList AlpmPackage::provides() const
{
return alpm_pkg_get_provides(m_ptr);
}
inline StringList AlpmPackage::deltas() const
{
return alpm_pkg_get_deltas(m_ptr);
}
inline DependencyList AlpmPackage::replaces() const
{
return alpm_pkg_get_replaces(m_ptr);
}
inline alpm_filelist_t *AlpmPackage::files() const
{
return alpm_pkg_get_files(m_ptr);
}
inline AlpmList<alpm_backup_t *> AlpmPackage::backupFiles() const
{
return alpm_pkg_get_backup(m_ptr);
}
inline alpm_db_t *AlpmPackage::associatedDatabase() const
{
return alpm_pkg_get_db(m_ptr);
}
inline const char *AlpmPackage::base64Signature() const
{
return alpm_pkg_get_base64_sig(m_ptr);
}
inline alpm_pkgvalidation_t AlpmPackage::validation() const
{
return alpm_pkg_get_validation(m_ptr);
}
inline StringList AlpmPackage::requiredBy() const
{
return alpm_pkg_compute_requiredby(m_ptr);
}
inline StringList AlpmPackage::optionalFor() const
{
return alpm_pkg_compute_optionalfor(m_ptr);
}
inline void *AlpmPackage::openChangelog() const
{
return alpm_pkg_changelog_open(m_ptr);
}
inline size_t AlpmPackage::readChangelog(void *changelog, void *buffer, size_t bufferSize)
{
return alpm_pkg_changelog_read(buffer, bufferSize, m_ptr, changelog);
}
inline bool AlpmPackage::closeChangelog(void *changelog)
{
return alpm_pkg_changelog_close(m_ptr, changelog);
}
inline PackageManagement::AlpmPackage::operator bool() const
{
return m_ptr != nullptr;
}
class AlpmOwnershipPackage : public AlpmPackage
{
public:
// constructor, destructor
AlpmOwnershipPackage(alpm_pkg_t *package);
AlpmOwnershipPackage(const AlpmOwnershipPackage &other) = delete;
AlpmOwnershipPackage(AlpmOwnershipPackage &&other);
~AlpmOwnershipPackage();
// assignment operator
AlpmOwnershipPackage &operator =(const AlpmOwnershipPackage &other) = delete;
AlpmOwnershipPackage &operator =(AlpmOwnershipPackage &&other);
};
inline AlpmOwnershipPackage::AlpmOwnershipPackage(alpm_pkg_t *package) :
AlpmPackage(package)
{}
inline AlpmOwnershipPackage::AlpmOwnershipPackage(AlpmOwnershipPackage &&other) :
AlpmPackage(other.m_ptr)
{
other.m_ptr = nullptr;
}
inline AlpmOwnershipPackage &AlpmOwnershipPackage::operator =(AlpmOwnershipPackage &&other)
{
if(this != &other) {
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
}
return *this;
}
inline AlpmOwnershipPackage::~AlpmOwnershipPackage()
{
alpm_pkg_free(ptr());
}
}
#endif // ALPM_PACKAGE_H