Blob Blame History Raw
From ab3ee74c7ec4b74b4c9d1ab6298f4c32da03073b Mon Sep 17 00:00:00 2001
From: Daniel Nicoletti <dantti12@gmail.com>
Date: Tue, 2 Oct 2012 09:04:19 -0300
Subject: [PATCH 4/4] packagekit-qt: Break ABI to make pk-qt more closer to
 what pk-glib is and allow for searchGroup() to work

---
 configure.ac                              |  2 +-
 lib/packagekit-qt2/Makefile.am            |  4 ++
 lib/packagekit-qt2/bitfield.cpp           | 70 +++++++++++++++++++++++++++
 lib/packagekit-qt2/bitfield.h             | 33 +++++++++++++
 lib/packagekit-qt2/daemon.cpp             | 15 +-----
 lib/packagekit-qt2/daemon.h               | 77 ++++++++++++++++++++++++++++++
 lib/packagekit-qt2/package.cpp            | 23 ++++-----
 lib/packagekit-qt2/package.h              |  4 +-
 lib/packagekit-qt2/packagedetails.cpp     |  8 +---
 lib/packagekit-qt2/packagedetails.h       | 79 ++++++++++++++++---------------
 lib/packagekit-qt2/transaction.cpp        | 18 ++++---
 lib/packagekit-qt2/transaction.h          | 74 ++++++++++++++++-------------
 lib/packagekit-qt2/transactionprivate.cpp |  2 +-
 13 files changed, 295 insertions(+), 114 deletions(-)
 create mode 100644 lib/packagekit-qt2/bitfield.cpp
 create mode 100644 lib/packagekit-qt2/bitfield.h

diff --git a/configure.ac b/configure.ac
index 1a4f123..a7530c3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -74,7 +74,7 @@ AC_SUBST(LT_AGE)
 # - If the interface is the same as the previous version, but bugs are
 #   fixed, change:
 #      REVISION += 1
-LT_QT_CURRENT=4
+LT_QT_CURRENT=5
 LT_QT_REVISION=0
 LT_QT_AGE=0
 AC_SUBST(LT_QT_CURRENT)
diff --git a/lib/packagekit-qt2/Makefile.am b/lib/packagekit-qt2/Makefile.am
index 6e5e777..959e618 100644
--- a/lib/packagekit-qt2/Makefile.am
+++ b/lib/packagekit-qt2/Makefile.am
@@ -21,6 +21,8 @@ MOCFILES =							\
 	daemonproxy.moc						\
 	transactionproxy.moc					\
 	transaction.moc						\
+	package.moc						\
+	packagedetails.moc					\
 	$(NULL)
 
 lib_LTLIBRARIES =						\
@@ -30,6 +32,7 @@ lib_LTLIBRARIES =						\
 libpackagekit_qt2_includedir = $(includedir)/PackageKit/packagekit-qt2
 
 libpackagekit_qt2_include_HEADERS =				\
+	bitfield.h						\
 	Signature						\
 	signature.h						\
 	Eula							\
@@ -47,6 +50,7 @@ libpackagekit_qt2_include_HEADERS =				\
 	$(NULL)
 
 libpackagekit_qt2_la_SOURCES =					\
+	bitfield.cpp						\
 	signature.h						\
 	signature.cpp						\
 	eula.h							\
diff --git a/lib/packagekit-qt2/bitfield.cpp b/lib/packagekit-qt2/bitfield.cpp
new file mode 100644
index 0000000..2fa6e82
--- /dev/null
+++ b/lib/packagekit-qt2/bitfield.cpp
@@ -0,0 +1,70 @@
+#include "bitfield.h"
+
+using namespace PackageKit;
+
+Bitfield::Bitfield () : m_val (0)
+{
+}
+
+Bitfield::Bitfield (qulonglong val) : m_val (val)
+{
+}
+
+Bitfield::~Bitfield ()
+{
+}
+
+qulonglong Bitfield::operator& (qulonglong mask) const
+{
+	return m_val & (1ULL << mask);
+}
+
+qulonglong Bitfield::operator&= (qulonglong mask)
+{
+	m_val &= (1ULL << mask);
+	return m_val;
+}
+
+qulonglong Bitfield::operator| (qulonglong mask) const
+{
+	return m_val | (1ULL << mask);
+}
+
+qulonglong Bitfield::operator|= (qulonglong mask)
+{
+	m_val |= (1ULL << mask);
+	return m_val;
+}
+
+Bitfield Bitfield::operator& (Bitfield mask) const
+{
+	return m_val & mask.m_val;
+}
+
+Bitfield Bitfield::operator&= (Bitfield mask)
+{
+	m_val &= mask.m_val;
+	return m_val;
+}
+
+Bitfield Bitfield::operator| (Bitfield mask) const
+{
+	return m_val | mask.m_val;
+}
+
+Bitfield Bitfield::operator|= (Bitfield mask)
+{
+	m_val |= mask.m_val;
+	return m_val;
+}
+
+
+Bitfield& Bitfield::operator= (const Bitfield& other)
+{
+	if (this == &other)
+		return *this;
+
+	m_val = other.m_val;
+
+	return *this;
+}
diff --git a/lib/packagekit-qt2/bitfield.h b/lib/packagekit-qt2/bitfield.h
new file mode 100644
index 0000000..d7dd481
--- /dev/null
+++ b/lib/packagekit-qt2/bitfield.h
@@ -0,0 +1,33 @@
+#ifndef PACKAGEKIT_BITFIELD_H
+#define PACKAGEKIT_BITFIELD_H
+
+#include <QtGlobal>
+
+namespace PackageKit {
+
+class Bitfield
+{
+public:
+    Bitfield ();
+    Bitfield (qulonglong val);
+    ~Bitfield ();
+
+    qulonglong operator& (qulonglong mask) const;
+    qulonglong operator&= (qulonglong mask);
+    qulonglong operator| (qulonglong mask) const;
+    qulonglong operator|= (qulonglong mask);
+
+    Bitfield operator& (Bitfield mask) const;
+    Bitfield operator&= (Bitfield mask);
+    Bitfield operator| (Bitfield mask) const;
+    Bitfield operator|= (Bitfield mask);
+
+    Bitfield& operator= (const Bitfield& other);
+
+private:
+    qulonglong m_val;
+};
+
+} // End namespace PackageKit
+
+#endif
diff --git a/lib/packagekit-qt2/daemon.cpp b/lib/packagekit-qt2/daemon.cpp
index 7b0cee1..8984946 100644
--- a/lib/packagekit-qt2/daemon.cpp
+++ b/lib/packagekit-qt2/daemon.cpp
@@ -78,11 +78,7 @@ Daemon::~Daemon()
 
 Transaction::Roles Daemon::actions()
 {
-    qulonglong roles = global()->d_ptr->daemon->roles();
-
-    Transaction::Roles flags;
-    flags |= static_cast<Transaction::Role>(roles);
-    return flags;
+    return global()->d_ptr->daemon->roles();
 }
 
 QString Daemon::backendName()
@@ -144,14 +140,7 @@ QDBusObjectPath Daemon::getTid()
 
 uint Daemon::getTimeSinceAction(Transaction::Role role)
 {
-    // We need to find the Role enum position
-    // since this method does not support bitwised enum.
-    for (uint i = 0; i < 64; ++i) {
-        if (role == (1 << i)) {
-            return global()->d_ptr->daemon->GetTimeSinceAction(i);
-        }
-    }
-    return UINT_MAX;
+    return global()->d_ptr->daemon->GetTimeSinceAction(role);
 }
 
 QList<QDBusObjectPath> Daemon::getTransactionList()
diff --git a/lib/packagekit-qt2/daemon.h b/lib/packagekit-qt2/daemon.h
index 8ef525f..d8bb976 100644
--- a/lib/packagekit-qt2/daemon.h
+++ b/lib/packagekit-qt2/daemon.h
@@ -23,6 +23,7 @@
 #define PACKAGEKIT_DAEMON_H
 
 #include <QtCore/QObject>
+#include <QtCore/QMetaEnum>
 
 #include "package.h"
 #include "transaction.h"
@@ -242,6 +243,82 @@ public:
      * The micro version number.
      */
     static uint versionMicro();
+    
+    /**
+     * Returns the string representing the enum
+     * Useful for PackageDetails::Group
+     */
+    template<class T> static QString enumToString(int value, const char *enumName)
+    {
+        QString prefix = enumName;
+        int id = T::staticMetaObject.indexOfEnumerator(enumName);
+        QMetaEnum e = T::staticMetaObject.enumerator(id);
+        if (!e.isValid ()) {
+//             qDebug() << "Invalid enum " << prefix;
+            return QString();
+        }
+        QString enumString = e.valueToKey(value);
+        if (enumString.isNull()) {
+//             qDebug() << "Enum key not found while searching for value" << QString::number(value) << "in enum" << prefix;
+            return QString();
+        }
+
+        // Remove the prefix
+        if(!prefix.isNull() && enumString.indexOf(prefix) == 0) {
+            enumString.remove(0, prefix.length());
+        }
+
+        QString pkName;
+        for(int i = 0 ; i < enumString.length() - 1 ; ++i) {
+            pkName += enumString[i];
+            if(enumString[i+1].isUpper())
+                pkName += QChar('-');
+        }
+        pkName += enumString[enumString.length() - 1];
+
+        return pkName.toLower();
+    }
+    
+    template<class T> static int enumFromString(const QString &str, const char *enumName)
+    {
+        QString prefix = enumName;
+        QString realName;
+        bool lastWasDash = false;
+        QChar buf;
+
+        for(int i = 0 ; i < str.length() ; ++i) {
+            buf = str[i].toLower();
+            if(i == 0 || lastWasDash) {
+                buf = buf.toUpper();
+            }
+
+            lastWasDash = false;
+            if(buf == QLatin1Char('-')) {
+                lastWasDash = true;
+            } else if(buf == QLatin1Char('~')) {
+                lastWasDash = true;
+                realName += "Not";
+            } else {
+                realName += buf;
+            }
+        };
+
+        if (!prefix.isNull()) {
+            realName = prefix + realName;
+        }
+
+        int id = T::staticMetaObject.indexOfEnumerator(enumName);
+        QMetaEnum e = T::staticMetaObject.enumerator(id);
+        int enumValue = e.keyToValue(realName.toAscii().data());
+
+        if (enumValue == -1) {
+            enumValue = e.keyToValue(prefix.append("Unknown").toAscii().data());
+            if (!QString(enumName).isEmpty()) {
+//                 qDebug() << "enumFromString (" << enumName << ") : converted" << str << "to" << QString("Unknown").append(enumName) << ", enum id" << id;
+            }
+        }
+        return enumValue;
+    }
 
 Q_SIGNALS:
     /**
diff --git a/lib/packagekit-qt2/package.cpp b/lib/packagekit-qt2/package.cpp
index 2e1a91a..384c32e 100644
--- a/lib/packagekit-qt2/package.cpp
+++ b/lib/packagekit-qt2/package.cpp
@@ -28,9 +28,9 @@
 using namespace PackageKit;
 
 Package::Package(const QString &packageId, Info info, const QString &summary) :
-    QString(packageId),
     d(new PackagePrivate)
 {
+    d->id = packageId;
     d->info = info;
     d->summary = summary;
 }
@@ -42,11 +42,8 @@ Package::Package() :
 }
 
 Package::Package(const Package &other) :
-    d(new PackagePrivate)
+    d(other.d)
 {
-    d->info = InfoUnknown;
-
-    *this = other;
 }
 
 Package::~Package()
@@ -55,40 +52,40 @@ Package::~Package()
 
 bool Package::isValid() const
 {
-    int sepCount = count(QLatin1Char(';'));
+    int sepCount = d->id.count(QLatin1Char(';'));
     if (sepCount == 3) {
         // A valid pk-id "name;version;arch;data"
         return true;
     } else if (sepCount == 0) {
         // its also valid just "name"
-        return !isEmpty();
+        return !d->id.isEmpty();
     }
     return false;
 }
 
 QString Package::id() const
 {
-    return *this;
+    return d->id;
 }
 
 QString Package::name() const
 {
-    return section(QLatin1Char(';'), 0, 0);
+    return d->id.section(QLatin1Char(';'), 0, 0);
 }
 
 QString Package::version() const
 {
-    return section(QLatin1Char(';'), 1, 1);
+    return d->id.section(QLatin1Char(';'), 1, 1);
 }
 
 QString Package::arch() const
 {
-    return section(QLatin1Char(';'), 2, 2);
+    return d->id.section(QLatin1Char(';'), 2, 2);
 }
 
 QString Package::data() const
 {
-    return section(QLatin1Char(';'), 3, 3);
+    return d->id.section(QLatin1Char(';'), 3, 3);
 }
 
 QString Package::summary() const
@@ -144,6 +141,6 @@ bool Package::operator==(const Package &package) const
 Package& Package::operator=(const Package &package)
 {
     d = package.d;
-    QString::operator=(package);
 }
 
+#include "package.moc"
diff --git a/lib/packagekit-qt2/package.h b/lib/packagekit-qt2/package.h
index 57fb153..81eca8d 100644
--- a/lib/packagekit-qt2/package.h
+++ b/lib/packagekit-qt2/package.h
@@ -37,6 +37,7 @@ public:
         info(other.info),
         summary(other.summary) { }
     ~PackagePrivate() { }
+    QString id;
     uint info;
     QString summary;
 };
@@ -51,8 +52,9 @@ public:
  *
  * \note All Package objects should be deleted by the user.
  */
-class Package : public QString
+class Package
 {
+    Q_GADGET
 public:
     /**
      * Describes the state of a package
diff --git a/lib/packagekit-qt2/packagedetails.cpp b/lib/packagekit-qt2/packagedetails.cpp
index 3be7de1..c273e5d 100644
--- a/lib/packagekit-qt2/packagedetails.cpp
+++ b/lib/packagekit-qt2/packagedetails.cpp
@@ -22,12 +22,6 @@
 
 #include <QtCore/QDebug>
 
-namespace PackageKit {
-
-
-
-}
-
 using namespace PackageKit;
 
 PackageDetails::PackageDetails(const QString &package_id, const QString &license, uint group, const QString &detail, const QString &url, qulonglong size) :
@@ -82,3 +76,5 @@ qulonglong PackageDetails::size() const
 {
     return d->size;
 }
+
+#include "packagedetails.moc"
diff --git a/lib/packagekit-qt2/packagedetails.h b/lib/packagekit-qt2/packagedetails.h
index c4751db..f188e18 100644
--- a/lib/packagekit-qt2/packagedetails.h
+++ b/lib/packagekit-qt2/packagedetails.h
@@ -22,6 +22,7 @@
 #define PACKAGEKIT_PACKAGE_DETAILS_H
 
 #include "package.h"
+#include "bitfield.h"
 
 #include <QtCore/QDateTime>
 
@@ -57,48 +58,50 @@ namespace PackageKit {
  */
 class PackageDetails : public Package
 {
+    Q_GADGET
+    Q_ENUMS(Group)
 public:
     /**
      * Describes the different package groups
      */
-    enum Group {
-        GroupUnknown         = 1ULL << 0,
-        GroupAccessibility   = 1ULL << 1,
-        GroupAccessories     = 1ULL << 2,
-        GroupAdminTools      = 1ULL << 3,
-        GroupCommunication   = 1ULL << 4,
-        GroupDesktopGnome    = 1ULL << 5,
-        GroupDesktopKde      = 1ULL << 6,
-        GroupDesktopOther    = 1ULL << 7,
-        GroupDesktopXfce     = 1ULL << 8,
-        GroupEducation       = 1ULL << 9,
-        GroupFonts           = 1ULL << 10,
-        GroupGames           = 1ULL << 11,
-        GroupGraphics        = 1ULL << 12,
-        GroupInternet        = 1ULL << 13,
-        GroupLegacy          = 1ULL << 14,
-        GroupLocalization    = 1ULL << 15,
-        GroupMaps            = 1ULL << 16,
-        GroupMultimedia      = 1ULL << 17,
-        GroupNetwork         = 1ULL << 18,
-        GroupOffice          = 1ULL << 19,
-        GroupOther           = 1ULL << 20,
-        GroupPowerManagement = 1ULL << 21,
-        GroupProgramming     = 1ULL << 22,
-        GroupPublishing      = 1ULL << 23,
-        GroupRepos           = 1ULL << 24,
-        GroupSecurity        = 1ULL << 25,
-        GroupServers         = 1ULL << 26,
-        GroupSystem          = 1ULL << 27,
-        GroupVirtualization  = 1ULL << 28,
-        GroupScience         = 1ULL << 29,
-        GroupDocumentation   = 1ULL << 30,
-        GroupElectronics     = 1ULL << 31,
-        GroupCollections     = 1ULL << 32,
-        GroupVendor          = 1ULL << 33,
-        GroupNewest          = 1ULL << 34
-    };
-    typedef qulonglong Groups;
+    typedef enum {
+        GroupUnknown,
+        GroupAccessibility,
+        GroupAccessories,
+        GroupAdminTools,
+        GroupCommunication,
+        GroupDesktopGnome,
+        GroupDesktopKde,
+        GroupDesktopOther,
+        GroupDesktopXfce,
+        GroupEducation,
+        GroupFonts,
+        GroupGames,
+        GroupGraphics,
+        GroupInternet,
+        GroupLegacy,
+        GroupLocalization,
+        GroupMaps,
+        GroupMultimedia,
+        GroupNetwork,
+        GroupOffice,
+        GroupOther,
+        GroupPowerManagement,
+        GroupProgramming,
+        GroupPublishing,
+        GroupRepos,
+        GroupSecurity,
+        GroupServers,
+        GroupSystem,
+        GroupVirtualization,
+        GroupScience,
+        GroupDocumentation,
+        GroupElectronics,
+        GroupCollections,
+        GroupVendor,
+        GroupNewest
+    } Group;
+    typedef Bitfield Groups;
 
     /**
      * Constructs package
diff --git a/lib/packagekit-qt2/transaction.cpp b/lib/packagekit-qt2/transaction.cpp
index f605b1b..ee7c59b 100644
--- a/lib/packagekit-qt2/transaction.cpp
+++ b/lib/packagekit-qt2/transaction.cpp
@@ -59,7 +59,6 @@ void Transaction::init(const QDBusObjectPath &tid)
 {
     Q_D(Transaction);
 
-    d->tid = tid;
     d->oldtrans = false;
     d->p = 0;
 
@@ -67,7 +66,8 @@ void Transaction::init(const QDBusObjectPath &tid)
     // he want us to get it
     if (tid.path().isNull()) {
         d->tid = Daemon::global()->getTid();
-        qDebug() << "packagekit-qt:" << d->tid.path();
+    } else {
+        d->tid = tid;
     }
 
     int retry = 0;
@@ -78,7 +78,7 @@ void Transaction::init(const QDBusObjectPath &tid)
                                     QDBusConnection::systemBus(),
                                     this);
         if (!d->p->isValid()) {
-            qDebug() << "Error, cannot create transaction proxy" << d->p->lastError();
+            qWarning() << "Error, cannot create transaction proxy" << d->p->lastError();
             QDBusMessage message;
             message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.DBus"),
                                                      QLatin1String("/"),
@@ -278,16 +278,14 @@ Transaction::Role Transaction::role() const
 {
     Q_D(const Transaction);
     if(d->oldtrans) {
-        uint role = 1 << d->role;
-        return static_cast<Transaction::Role>(role);
+        return d->role;
     }
 
     if (d->destroyed) {
         return Transaction::RoleUnknown;
     }
 
-    uint role = 1 << d->p->role();
-    return static_cast<Transaction::Role>(role);
+    return static_cast<Transaction::Role>(d->p->role());
 }
 
 void Transaction::setHints(const QStringList &hints)
@@ -543,6 +541,12 @@ void Transaction::searchGroup(const QString &group, Transaction::Filters filters
     searchGroups(QStringList() << group, filters);
 }
 
+void Transaction::searchGroup(PackageDetails::Group group, Filters filters)
+{
+    QString groupString = Daemon::enumToString<PackageDetails>(group, "Group");
+    searchGroup(groupString, filters);
+}
+
 void Transaction::searchGroups(PackageDetails::Groups groups, Transaction::Filters filters)
 {
     searchGroups(groups, filters);
diff --git a/lib/packagekit-qt2/transaction.h b/lib/packagekit-qt2/transaction.h
index 9e3416d..c599957 100644
--- a/lib/packagekit-qt2/transaction.h
+++ b/lib/packagekit-qt2/transaction.h
@@ -88,40 +88,40 @@ public:
     /**
      * Describes the role of the transaction
      */
-    enum Role {
-        RoleUnknown            = 1 << 0,
-        RoleCancel             = 1 << 1,
-        RoleGetDepends         = 1 << 2,
-        RoleGetDetails         = 1 << 3,
-        RoleGetFiles           = 1 << 4,
-        RoleGetPackages        = 1 << 5,
-        RoleGetRepoList        = 1 << 6,
-        RoleGetRequires        = 1 << 7,
-        RoleGetUpdateDetail    = 1 << 8,
-        RoleGetUpdates         = 1 << 9,
-        RoleInstallFiles       = 1 << 10,
-        RoleInstallPackages    = 1 << 11,
-        RoleInstallSignature   = 1 << 12,
-        RoleRefreshCache       = 1 << 13,
-        RoleRemovePackages     = 1 << 14,
-        RoleRepoEnable         = 1 << 15,
-        RoleRepoSetData        = 1 << 16,
-        RoleResolve            = 1 << 17,
-        RoleSearchDetails      = 1 << 18,
-        RoleSearchFile         = 1 << 19,
-        RoleSearchGroup        = 1 << 20,
-        RoleSearchName         = 1 << 21,
-        RoleUpdatePackages     = 1 << 22,
-        RoleWhatProvides       = 1 << 23,
-        RoleAcceptEula         = 1 << 24,
-        RoleDownloadPackages   = 1 << 25,
-        RoleGetDistroUpgrades  = 1 << 26,
-        RoleGetCategories      = 1 << 27,
-        RoleGetOldTransactions = 1 << 28,
-        RoleUpgradeSystem      = 1 << 29, // Since 0.6.11
-        RoleRepairSystem       = 1 << 30 // Since 0.7.2
-    };
-    Q_DECLARE_FLAGS(Roles, Role)
+    typedef enum {
+        RoleUnknown,
+        RoleCancel,
+        RoleGetDepends,
+        RoleGetDetails,
+        RoleGetFiles,
+        RoleGetPackages,
+        RoleGetRepoList,
+        RoleGetRequires,
+        RoleGetUpdateDetail,
+        RoleGetUpdates,
+        RoleInstallFiles,
+        RoleInstallPackages,
+        RoleInstallSignature,
+        RoleRefreshCache,
+        RoleRemovePackages,
+        RoleRepoEnable,
+        RoleRepoSetData,
+        RoleResolve,
+        RoleSearchDetails,
+        RoleSearchFile,
+        RoleSearchGroup,
+        RoleSearchName,
+        RoleUpdatePackages,
+        RoleWhatProvides,
+        RoleAcceptEula,
+        RoleDownloadPackages,
+        RoleGetDistroUpgrades,
+        RoleGetCategories,
+        RoleGetOldTransactions,
+        RoleUpgradeSystem, // Since 0.6.11
+        RoleRepairSystem   // Since 0.7.2
+    } Role;
+    typedef Bitfield Roles;
 
     /**
      * Describes the different types of error
@@ -844,6 +844,12 @@ public:
      * \sa searchGroups(const QStringList &groups, Filters filters = FilterNone)
      */
     void searchGroup(const QString &group, Filters filters = FilterNone);
+    
+    /**
+     * Convenience function to search by group enum
+     * \sa searchGroups(const QStringList &groups, Filters filters = FilterNone)
+     */
+    void searchGroup(PackageDetails::Group group, Filters filters = FilterNone);
 
     /**
      * \brief Lists all the packages in the given \p group
diff --git a/lib/packagekit-qt2/transactionprivate.cpp b/lib/packagekit-qt2/transactionprivate.cpp
index 94b2832..564bd58 100644
--- a/lib/packagekit-qt2/transactionprivate.cpp
+++ b/lib/packagekit-qt2/transactionprivate.cpp
@@ -202,7 +202,7 @@ QStringList TransactionPrivate::packageListToPids(const PackageList &packages) c
 {
     QStringList pids;
     foreach (const Package &package, packages) {
-        pids << package;
+        pids << package.id();
     }
     return pids;
 }
-- 
1.7.12.1