[Telepathy-commits] [telepathy-qt4/master] Improved roster example to support add/auth/remove/deny contacts.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Thu Feb 26 15:32:28 PST 2009


---
 examples/roster/roster-item.cpp   |   27 +++++-
 examples/roster/roster-item.h     |    6 +-
 examples/roster/roster-window.cpp |  178 ++++++++++++++++++++++++++++++++++++-
 examples/roster/roster-window.h   |   16 ++++
 4 files changed, 219 insertions(+), 8 deletions(-)

diff --git a/examples/roster/roster-item.cpp b/examples/roster/roster-item.cpp
index db71965..2758633 100644
--- a/examples/roster/roster-item.cpp
+++ b/examples/roster/roster-item.cpp
@@ -29,17 +29,36 @@ RosterItem::RosterItem(const QSharedPointer<Contact> &contact,
       QListWidgetItem(parent),
       mContact(contact)
 {
-    setText(QString("%1 (%2)").arg(contact->id()).arg(contact->presenceStatus()));
+    onPresenceStateChanged();
+
     connect(contact.data(),
             SIGNAL(simplePresenceChanged(const QString &, uint, const QString &)),
-            SLOT(onSimplePresenceChanged(const QString &, uint, const QString &)));
+            SLOT(onPresenceStateChanged()));
+    connect(contact.data(),
+            SIGNAL(subscriptionStateChanged(PresenceState)),
+            SLOT(onPresenceStateChanged()));
+    connect(contact.data(),
+            SIGNAL(publishStateChanged(PresenceState)),
+            SLOT(onPresenceStateChanged()));
 }
 
 RosterItem::~RosterItem()
 {
 }
 
-void RosterItem::onSimplePresenceChanged(const QString &status, uint type, const QString &presenceMessage)
+void RosterItem::onPresenceStateChanged()
 {
-    setText(QString("%1 (%2)").arg(mContact->id()).arg(status));
+    QString status = mContact->presenceStatus();
+    // I've asked to see the contact presence
+    if (mContact->subscriptionState() == Contact::PresenceStateAsk) {
+        setText(QString("%1 (%2) (awaiting approval)").arg(mContact->id()).arg(status));
+    // The contact asked to see my presence
+    } else if (mContact->publishState() == Contact::PresenceStateAsk) {
+        setText(QString("%1 (%2) (pending approval)").arg(mContact->id()).arg(status));
+    } else if (mContact->subscriptionState() == Contact::PresenceStateNo &&
+               mContact->publishState() == Contact::PresenceStateNo) {
+        setText(QString("%1 (unknown)").arg(mContact->id()));
+    } else {
+        setText(QString("%1 (%2)").arg(mContact->id()).arg(status));
+    }
 }
diff --git a/examples/roster/roster-item.h b/examples/roster/roster-item.h
index 09ad7ab..48e6299 100644
--- a/examples/roster/roster-item.h
+++ b/examples/roster/roster-item.h
@@ -37,10 +37,10 @@ public:
             QListWidget *parent = 0);
     ~RosterItem();
 
+    QSharedPointer<Telepathy::Client::Contact> contact() const { return mContact; }
+
 private Q_SLOTS:
-    void onSimplePresenceChanged(const QString &status,
-            uint type,
-            const QString &presenceMessage);
+    void onPresenceStateChanged();
 
 private:
     QSharedPointer<Telepathy::Client::Contact> mContact;
diff --git a/examples/roster/roster-window.cpp b/examples/roster/roster-window.cpp
index 5f093ea..989e535 100644
--- a/examples/roster/roster-window.cpp
+++ b/examples/roster/roster-window.cpp
@@ -26,16 +26,25 @@
 #include <TelepathyQt4/Types>
 #include <TelepathyQt4/Client/Connection>
 #include <TelepathyQt4/Client/ConnectionManager>
+#include <TelepathyQt4/Client/Contact>
 #include <TelepathyQt4/Client/ContactManager>
 #include <TelepathyQt4/Client/PendingConnection>
+#include <TelepathyQt4/Client/PendingContacts>
 #include <TelepathyQt4/Client/PendingOperation>
 #include <TelepathyQt4/Client/PendingReady>
 #include <TelepathyQt4/Client/PendingReadyConnectionManager>
 
 #include <QDebug>
+#include <QDialog>
+#include <QDialogButtonBox>
 #include <QHBoxLayout>
+#include <QLabel>
+#include <QLineEdit>
 #include <QListWidget>
 #include <QListWidgetItem>
+#include <QMessageBox>
+#include <QPushButton>
+#include <QVBoxLayout>
 
 using namespace Telepathy::Client;
 
@@ -59,8 +68,66 @@ RosterWindow::~RosterWindow()
 
 void RosterWindow::setupGui()
 {
+    QWidget *frame = new QWidget;
+
+    QVBoxLayout *vbox = new QVBoxLayout;
+
     mList = new QListWidget;
-    setCentralWidget(mList);
+    vbox->addWidget(mList);
+
+    QHBoxLayout *hbox = new QHBoxLayout;
+
+    mAddBtn = new QPushButton("Add contact");
+    mAddBtn->setEnabled(false);
+    connect(mAddBtn,
+            SIGNAL(clicked(bool)),
+            SLOT(onAddButtonClicked()));
+    hbox->addWidget(mAddBtn);
+
+    mAuthBtn = new QPushButton("Authorize contact");
+    mAuthBtn->setEnabled(false);
+    connect(mAuthBtn,
+            SIGNAL(clicked(bool)),
+            SLOT(onAuthButtonClicked()));
+    hbox->addWidget(mAuthBtn);
+
+    mRemoveBtn = new QPushButton("Remove contact");
+    mRemoveBtn->setEnabled(false);
+    connect(mRemoveBtn,
+            SIGNAL(clicked(bool)),
+            SLOT(onRemoveButtonClicked()));
+    hbox->addWidget(mRemoveBtn);
+
+    mDenyBtn = new QPushButton("Deny contact");
+    mDenyBtn->setEnabled(false);
+    connect(mDenyBtn,
+            SIGNAL(clicked(bool)),
+            SLOT(onDenyButtonClicked()));
+    hbox->addWidget(mDenyBtn);
+
+    vbox->addLayout(hbox);
+
+    frame->setLayout(vbox);
+
+    setCentralWidget(frame);
+
+    mAddDlg = new QDialog(this);
+    QVBoxLayout *addDlgVBox = new QVBoxLayout;
+
+    QHBoxLayout *addDlgEntryHBox = new QHBoxLayout;
+    QLabel *label = new QLabel("Username");
+    addDlgEntryHBox->addWidget(label);
+    mAddDlgEdt = new QLineEdit();
+    addDlgEntryHBox->addWidget(mAddDlgEdt);
+    addDlgVBox->addLayout(addDlgEntryHBox);
+
+    QDialogButtonBox *addDlgBtnBox = new QDialogButtonBox(
+            QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
+    connect(addDlgBtnBox, SIGNAL(accepted()), mAddDlg, SLOT(accept()));
+    connect(addDlgBtnBox, SIGNAL(rejected()), mAddDlg, SLOT(reject()));
+    addDlgVBox->addWidget(addDlgBtnBox);
+
+    mAddDlg->setLayout(addDlgVBox);
 }
 
 void RosterWindow::onCMReady(Telepathy::Client::PendingOperation *op)
@@ -104,8 +171,117 @@ void RosterWindow::onConnectionReady(Telepathy::Client::PendingOperation *op)
         return;
     }
 
+    connect(mConn->contactManager(),
+            SIGNAL(presencePublicationRequested(const QSet<QSharedPointer<Telepathy::Client::Contact> > &)),
+            SLOT(onPresencePublicationRequested(const QSet<QSharedPointer<Telepathy::Client::Contact> > &)));
+
     qDebug() << "Connection ready";
     foreach (const QSharedPointer<Contact> &contact, mConn->contactManager()->allKnownContacts()) {
         (void) new RosterItem(contact, mList);
     }
+
+    mAddBtn->setEnabled(true);
+    mAuthBtn->setEnabled(true);
+    mRemoveBtn->setEnabled(true);
+    mDenyBtn->setEnabled(true);
+}
+
+void RosterWindow::onPresencePublicationRequested(const QSet<QSharedPointer<Contact> > &contacts)
+{
+    qDebug() << "Presence publication requested";
+    foreach (const QSharedPointer<Contact> &contact, contacts) {
+        (void) new RosterItem(contact, mList);
+    }
+}
+
+void RosterWindow::onAddButtonClicked()
+{
+    mAddDlgEdt->clear();
+    int ret = mAddDlg->exec();
+    if (ret == QDialog::Rejected) {
+        return;
+    }
+
+    QString username = mAddDlgEdt->text();
+    PendingContacts *pcontacts = mConn->contactManager()->contactsForIdentifiers(
+            QStringList() << username);
+    connect(pcontacts,
+            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
+            SLOT(onContactRetrieved(Telepathy::Client::PendingOperation *)));
+}
+
+void RosterWindow::onAuthButtonClicked()
+{
+    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
+    if (selectedItems.isEmpty()) {
+        return;
+    }
+
+    Q_ASSERT(selectedItems.size() == 1);
+    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
+    if (item->contact()->publishState() != Contact::PresenceStateYes) {
+        item->contact()->authorizePresencePublication();
+    }
+}
+
+void RosterWindow::onRemoveButtonClicked()
+{
+    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
+    if (selectedItems.isEmpty()) {
+        return;
+    }
+
+    Q_ASSERT(selectedItems.size() == 1);
+    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
+    if (item->contact()->subscriptionState() != Contact::PresenceStateNo) {
+        // The contact can't see my presence and I can't see his/her presence
+        item->contact()->removePresencePublication();
+        item->contact()->removePresenceSubscription();
+    }
+}
+
+void RosterWindow::onDenyButtonClicked()
+{
+    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
+    if (selectedItems.isEmpty()) {
+        return;
+    }
+
+    Q_ASSERT(selectedItems.size() == 1);
+    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
+    if (item->contact()->publishState() != Contact::PresenceStateNo) {
+        // The contact can't see my presence
+        item->contact()->removePresencePublication();
+    }
+}
+
+void RosterWindow::onContactRetrieved(Telepathy::Client::PendingOperation *op)
+{
+    PendingContacts *pcontacts = qobject_cast<PendingContacts *>(op);
+    QList<QSharedPointer<Contact> > contacts = pcontacts->contacts();
+    Q_ASSERT(pcontacts->identifiers().size() == 1);
+    QString username = pcontacts->identifiers().first();
+    if (contacts.size() != 1 || !contacts.first()) {
+        QMessageBox msgBox;
+        msgBox.setText(QString("Unable to add contact \"%1\"").arg(username));
+        msgBox.exec();
+        return;
+    }
+
+    QSharedPointer<Contact> contact = contacts.first();
+    qDebug() << "Request presence subscription for contact" << username;
+    // TODO should we have a signal on ContactManager to signal that a contact was
+    //      added to subscribe list?
+    //
+    bool found = false;
+    for (int i = 0; i < mList->count(); ++i) {
+        RosterItem *item = dynamic_cast<RosterItem*>(mList->item(i));
+        if (item->contact() == contact) {
+            found = true;
+        }
+    }
+    if (!found) {
+        (void) new RosterItem(contact, mList);
+    }
+    contact->requestPresenceSubscription();
 }
diff --git a/examples/roster/roster-window.h b/examples/roster/roster-window.h
index eb89742..30e71d5 100644
--- a/examples/roster/roster-window.h
+++ b/examples/roster/roster-window.h
@@ -28,11 +28,15 @@ namespace Telepathy {
 namespace Client {
 class Connection;
 class ConnectionManager;
+class Contact;
 class PendingOperation;
 }
 }
 
+class QDialog;
+class QLineEdit;
 class QListWidget;
+class QPushButton;
 
 class RosterWindow : public QMainWindow
 {
@@ -47,6 +51,12 @@ private Q_SLOTS:
     void onCMReady(Telepathy::Client::PendingOperation *);
     void onConnectionCreated(Telepathy::Client::PendingOperation *);
     void onConnectionReady(Telepathy::Client::PendingOperation *);
+    void onPresencePublicationRequested(const QSet<QSharedPointer<Telepathy::Client::Contact> > &);
+    void onAddButtonClicked();
+    void onAuthButtonClicked();
+    void onRemoveButtonClicked();
+    void onDenyButtonClicked();
+    void onContactRetrieved(Telepathy::Client::PendingOperation *op);
 
 private:
     void setupGui();
@@ -56,6 +66,12 @@ private:
     QString mUsername;
     QString mPassword;
     QListWidget *mList;
+    QPushButton *mAddBtn;
+    QPushButton *mAuthBtn;
+    QPushButton *mRemoveBtn;
+    QPushButton *mDenyBtn;
+    QDialog *mAddDlg;
+    QLineEdit *mAddDlgEdt;
 };
 
 #endif
-- 
1.5.6.5




More information about the telepathy-commits mailing list