[Telepathy-commits] [telepathy-qt4/master] Add naive ContactManager::contactsForHandles
Olli Salli
olli.salli at collabora.co.uk
Wed Jan 28 06:30:19 PST 2009
---
TelepathyQt4/Client/contact-manager.cpp | 43 ++++++++++++++++++++++++++++++
TelepathyQt4/Client/contact-manager.h | 8 +++++
TelepathyQt4/Client/contact.cpp | 20 ++++++++++++-
TelepathyQt4/Client/contact.h | 8 +++++-
TelepathyQt4/Client/pending-contacts.cpp | 43 ++++++++++++++++++++++++++++++
TelepathyQt4/Client/pending-contacts.h | 9 +++++-
6 files changed, 127 insertions(+), 4 deletions(-)
diff --git a/TelepathyQt4/Client/contact-manager.cpp b/TelepathyQt4/Client/contact-manager.cpp
index a446c46..178d9b5 100644
--- a/TelepathyQt4/Client/contact-manager.cpp
+++ b/TelepathyQt4/Client/contact-manager.cpp
@@ -21,7 +21,12 @@
#include <TelepathyQt4/Client/ContactManager>
+#include <QMap>
+#include <QString>
+
#include <TelepathyQt4/Client/Connection>
+#include <TelepathyQt4/Client/PendingContactAttributes>
+#include <TelepathyQt4/Client/PendingContacts>
#include "TelepathyQt4/debug-internal.h"
/**
@@ -63,6 +68,44 @@ Connection *ContactManager::connection() const
return mPriv->conn;
}
+namespace
+{
+QString featureToInterface(Contact::Feature feature)
+{
+ switch (feature) {
+ // TODO: when more features are added, add the translation here too
+ default:
+ warning() << "ContactManager doesn't know which interface corresponds to feature"
+ << feature;
+ return QString();
+ }
+}
+}
+
+PendingContacts *ContactManager::contactsForHandles(const UIntList &handles,
+ const QSet<Contact::Feature> &features)
+{
+ debug() << "Building contacts for" << handles.size() << "handles" << "with" << features.size()
+ << "features";
+
+ QSet<QString> interfaces;
+ foreach (Contact::Feature feature, features) {
+ if (true) { // TODO when not naive: if one or more of the contacts doesn't have the feature
+ interfaces.insert(featureToInterface(feature));
+ }
+ }
+
+ PendingContactAttributes *attributes =
+ mPriv->conn->getContactAttributes(handles, interfaces.toList(), true);
+
+ PendingContacts *contacts = new PendingContacts(mPriv->conn, handles, features);
+ contacts->connect(attributes,
+ SIGNAL(finished(Telepathy::Client::PendingOperation*)),
+ SLOT(onAttributesFinished(Telepathy::Client::PendingOperation*)));
+
+ return contacts;
+}
+
ContactManager::ContactManager(Connection *parent)
: mPriv(new Private)
{
diff --git a/TelepathyQt4/Client/contact-manager.h b/TelepathyQt4/Client/contact-manager.h
index 94a6f89..2d631f1 100644
--- a/TelepathyQt4/Client/contact-manager.h
+++ b/TelepathyQt4/Client/contact-manager.h
@@ -26,6 +26,11 @@
#error IN_TELEPATHY_QT4_HEADER
#endif
+#include <QSet>
+
+#include <TelepathyQt4/Types>
+#include <TelepathyQt4/Client/Contact>
+
namespace Telepathy
{
namespace Client
@@ -41,6 +46,9 @@ class ContactManager
Connection *connection() const;
+ PendingContacts *contactsForHandles(const UIntList &handles,
+ const QSet<Contact::Feature> &features = QSet<Contact::Feature>());
+
private:
ContactManager(Connection *parent);
~ContactManager();
diff --git a/TelepathyQt4/Client/contact.cpp b/TelepathyQt4/Client/contact.cpp
index a4ca657..eb978b2 100644
--- a/TelepathyQt4/Client/contact.cpp
+++ b/TelepathyQt4/Client/contact.cpp
@@ -20,9 +20,11 @@
*/
#include <TelepathyQt4/Client/Contact>
+#include "TelepathyQt4/Client/_gen/contact.moc.hpp"
#include <TelepathyQt4/Client/Connection>
#include <TelepathyQt4/Client/ContactManager>
+#include <TelepathyQt4/Client/ReferencedHandles>
#include "TelepathyQt4/debug-internal.h"
namespace Telepathy
@@ -32,11 +34,25 @@ namespace Client
struct Contact::Private
{
+ Private(Connection *connection, const ReferencedHandles &handle)
+ : connection(connection), handle(handle)
+ {
+ }
+
+ Connection *connection;
+ ReferencedHandles handle;
};
-Contact::Contact(Connection *connection)
- : QObject(connection), mPriv(new Private)
+Contact::~Contact()
+{
+ delete mPriv;
+}
+
+Contact::Contact(Connection *connection, const ReferencedHandles &handle,
+ const QVariantMap &attributes)
+ : QObject(connection), mPriv(new Private(connection, handle))
{
+ debug() << this << "initialized with" << attributes.size() << "attributes";
}
} // Telepathy::Client
diff --git a/TelepathyQt4/Client/contact.h b/TelepathyQt4/Client/contact.h
index efea57c..f73397c 100644
--- a/TelepathyQt4/Client/contact.h
+++ b/TelepathyQt4/Client/contact.h
@@ -27,6 +27,7 @@
#endif
#include <QObject>
+#include <QVariantMap>
namespace Telepathy
{
@@ -34,9 +35,13 @@ namespace Client
{
class Connection;
class ContactManager;
+class ReferencedHandles;
class Contact : public QObject
{
+ Q_OBJECT;
+ Q_DISABLE_COPY(Contact);
+
public:
enum Feature {
_Padding = 0xFFFFFFFF
@@ -46,9 +51,10 @@ public:
~Contact();
private:
- Contact(Connection *parent);
+ Contact(Connection *parent, const ReferencedHandles &handle, const QVariantMap &attributes);
struct Private;
+ friend class PendingContacts;
friend struct Private;
Private *mPriv;
};
diff --git a/TelepathyQt4/Client/pending-contacts.cpp b/TelepathyQt4/Client/pending-contacts.cpp
index 1e0a2ec..b455397 100644
--- a/TelepathyQt4/Client/pending-contacts.cpp
+++ b/TelepathyQt4/Client/pending-contacts.cpp
@@ -23,6 +23,8 @@
#include "TelepathyQt4/Client/_gen/pending-contacts.moc.hpp"
#include <TelepathyQt4/Client/Connection>
+#include <TelepathyQt4/Client/PendingContactAttributes>
+#include <TelepathyQt4/Client/ReferencedHandles>
#include "TelepathyQt4/debug-internal.h"
namespace Telepathy
@@ -40,6 +42,8 @@ struct PendingContacts::Private
Connection *connection;
UIntList handles;
QSet<Contact::Feature> features;
+
+ QList<QSharedPointer<Contact> > contacts;
};
PendingContacts::PendingContacts(Connection *connection,
@@ -72,5 +76,44 @@ QSet<Contact::Feature> PendingContacts::features() const
return mPriv->features;
}
+QList<QSharedPointer<Contact> > PendingContacts::contacts() const
+{
+ if (!isFinished()) {
+ warning() << "PendingContacts::contacts() called before finished";
+ } else if (isError()) {
+ warning() << "PendingContacts::contacts() called when errored";
+ }
+
+ return mPriv->contacts;
+}
+
+void PendingContacts::onAttributesFinished(PendingOperation *operation)
+{
+ PendingContactAttributes *pendingAttributes =
+ qobject_cast<PendingContactAttributes *>(operation);
+
+ debug() << "Attributes finished for" << this;
+
+ if (pendingAttributes->isError()) {
+ debug() << " error" << pendingAttributes->errorName()
+ << "message" << pendingAttributes->errorMessage();
+ setFinishedWithError(pendingAttributes->errorName(), pendingAttributes->errorMessage());
+ return;
+ }
+
+ ReferencedHandles validHandles = pendingAttributes->validHandles();
+ ContactAttributesMap attributes = pendingAttributes->attributes();
+ debug() << " Success:" << validHandles.size() << "valid handles";
+
+ for (int i = 0; i < validHandles.size(); i++) {
+ uint handle = validHandles[i];
+ ReferencedHandles referenced = validHandles.mid(i, 1);
+ mPriv->contacts.push_back(QSharedPointer<Contact>(new Contact(mPriv->connection, referenced,
+ attributes[handle])));
+ }
+
+ setFinished();
+}
+
} // Telepathy::Client
} // Telepathy
diff --git a/TelepathyQt4/Client/pending-contacts.h b/TelepathyQt4/Client/pending-contacts.h
index 92cf412..86f38bb 100644
--- a/TelepathyQt4/Client/pending-contacts.h
+++ b/TelepathyQt4/Client/pending-contacts.h
@@ -26,9 +26,14 @@
#error IN_TELEPATHY_QT4_HEADER
#endif
+#include <TelepathyQt4/Client/PendingOperation>
+
+#include <QList>
+#include <QSet>
+#include <QSharedPointer>
+
#include <TelepathyQt4/Types>
#include <TelepathyQt4/Client/Contact>
-#include <TelepathyQt4/Client/PendingOperation>
namespace Telepathy
{
@@ -49,6 +54,8 @@ public:
UIntList handles() const;
QSet<Contact::Feature> features() const;
+ QList<QSharedPointer<Contact> > contacts() const;
+
private Q_SLOTS:
void onAttributesFinished(PendingOperation *);
--
1.5.6.5
More information about the telepathy-commits
mailing list