[Telepathy-commits] [telepathy-qt4/master] roster example: renamed RosterWindow to RosterWidget.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Thu Mar 12 11:09:19 PDT 2009


---
 examples/roster/Makefile.am       |    6 +-
 examples/roster/main.cpp          |    4 +-
 examples/roster/roster-widget.cpp |  384 ++++++++++++++++++++++++++++++++++++
 examples/roster/roster-widget.h   |   86 ++++++++
 examples/roster/roster-window.cpp |  388 -------------------------------------
 examples/roster/roster-window.h   |   86 --------
 6 files changed, 475 insertions(+), 479 deletions(-)
 create mode 100644 examples/roster/roster-widget.cpp
 create mode 100644 examples/roster/roster-widget.h
 delete mode 100644 examples/roster/roster-window.cpp
 delete mode 100644 examples/roster/roster-window.h

diff --git a/examples/roster/Makefile.am b/examples/roster/Makefile.am
index 4c5bf3e..6553f3c 100644
--- a/examples/roster/Makefile.am
+++ b/examples/roster/Makefile.am
@@ -16,12 +16,12 @@ roster_SOURCES = \
 	main.cpp \
 	roster-item.cpp \
 	roster-item.h \
-	roster-window.cpp \
-	roster-window.h
+	roster-widget.cpp \
+	roster-widget.h
 
 nodist_roster_SOURCES = \
 	_gen/roster-item.moc.hpp \
-	_gen/roster-window.moc.hpp
+	_gen/roster-widget.moc.hpp
 
 BUILT_SOURCES = \
 	$(nodist_roster_SOURCES)
diff --git a/examples/roster/main.cpp b/examples/roster/main.cpp
index 5d39880..d886baa 100644
--- a/examples/roster/main.cpp
+++ b/examples/roster/main.cpp
@@ -5,7 +5,7 @@
 #include <QDebug>
 #include <QtGui>
 
-#include "roster-window.h"
+#include "roster-widget.h"
 
 int main(int argc, char **argv)
 {
@@ -20,7 +20,7 @@ int main(int argc, char **argv)
     Telepathy::enableDebug(true);
     Telepathy::enableWarnings(true);
 
-    RosterWindow w(argv[1], argv[2]);
+    RosterWidget w(argv[1], argv[2]);
     w.show();
 
     return app.exec();
diff --git a/examples/roster/roster-widget.cpp b/examples/roster/roster-widget.cpp
new file mode 100644
index 0000000..5820c7d
--- /dev/null
+++ b/examples/roster/roster-widget.cpp
@@ -0,0 +1,384 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "roster-widget.h"
+#include "_gen/roster-widget.moc.hpp"
+
+#include "roster-item.h"
+
+#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 <QAction>
+#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;
+
+RosterWidget::RosterWidget(const QString &username, const QString &password,
+        QWidget *parent)
+    : QWidget(parent),
+      mUsername(username),
+      mPassword(password)
+{
+    setWindowTitle("Roster");
+
+    createActions();
+    setupGui();
+
+    mCM = new ConnectionManager("gabble", this);
+    connect(mCM->becomeReady(),
+            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
+            SLOT(onCMReady(Telepathy::Client::PendingOperation *)));
+
+    resize(240, 320);
+}
+
+RosterWidget::~RosterWidget()
+{
+}
+
+void RosterWidget::createActions()
+{
+    mAuthAction = new QAction(QLatin1String("Authorize Contact"), this);
+    mAuthAction->setEnabled(false);
+    connect(mAuthAction,
+            SIGNAL(triggered(bool)),
+            SLOT(onAuthActionTriggered(bool)));
+    mDenyAction = new QAction(QLatin1String("Deny Contact"), this);
+    mDenyAction->setEnabled(false);
+    connect(mDenyAction,
+            SIGNAL(triggered(bool)),
+            SLOT(onDenyActionTriggered(bool)));
+    mRemoveAction = new QAction(QLatin1String("Remove Contact"), this);
+    mRemoveAction->setEnabled(false);
+    connect(mRemoveAction,
+            SIGNAL(triggered(bool)),
+            SLOT(onRemoveActionTriggered(bool)));
+    mBlockAction = new QAction(QLatin1String("Block Contact"), this);
+    mBlockAction->setEnabled(false);
+    mBlockAction->setCheckable(true);
+    connect(mBlockAction,
+            SIGNAL(triggered(bool)),
+            SLOT(onBlockActionTriggered(bool)));
+}
+
+void RosterWidget::setupGui()
+{
+    QVBoxLayout *vbox = new QVBoxLayout;
+
+    mList = new QListWidget;
+    connect(mList,
+            SIGNAL(itemSelectionChanged()),
+            SLOT(onItemSelectionChanged()));
+    vbox->addWidget(mList);
+
+    mList->setContextMenuPolicy(Qt::ActionsContextMenu);
+    mList->addAction(mAuthAction);
+    mList->addAction(mDenyAction);
+    mList->addAction(mRemoveAction);
+    mList->addAction(mBlockAction);
+
+    QHBoxLayout *hbox = new QHBoxLayout;
+
+    mAddBtn = new QPushButton("+");
+    mAddBtn->setEnabled(false);
+    connect(mAddBtn,
+            SIGNAL(clicked(bool)),
+            SLOT(onAddButtonClicked()));
+    hbox->addWidget(mAddBtn);
+    hbox->addStretch(1);
+
+    vbox->addLayout(hbox);
+
+    setLayout(vbox);
+
+    mAddDlg = new QDialog(this);
+    mAddDlg->setWindowTitle("Add Contact");
+    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::createItemForContact(const ContactPtr &contact,
+        bool checkExists)
+{
+    bool found = false;
+    if (checkExists) {
+        for (int i = 0; i < mList->count(); ++i) {
+            RosterItem *item = dynamic_cast<RosterItem*>(mList->item(i));
+            if (item->contact() == contact) {
+                found = true;
+            }
+        }
+    }
+
+    if (!found) {
+        RosterItem *item = new RosterItem(contact, mList);
+        connect(item, SIGNAL(changed()), SLOT(updateActions()));
+    }
+}
+
+void RosterWidget::onCMReady(Telepathy::Client::PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "CM cannot become ready";
+        return;
+    }
+
+    qDebug() << "CM ready";
+    QVariantMap params;
+    params.insert("account", QVariant(mUsername));
+    params.insert("password", QVariant(mPassword));
+    PendingConnection *pconn = mCM->requestConnection("jabber", params);
+    connect(pconn,
+            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
+            SLOT(onConnectionCreated(Telepathy::Client::PendingOperation *)));
+}
+
+void RosterWidget::onConnectionCreated(Telepathy::Client::PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "Unable to create connection";
+        return;
+    }
+
+    qDebug() << "Connection created";
+    PendingConnection *pconn =
+        qobject_cast<PendingConnection *>(op);
+    mConn = pconn->connection();
+    Features features = Features() << Connection::FeatureRoster;
+    connect(mConn->requestConnect(features),
+            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
+            SLOT(onConnectionReady(Telepathy::Client::PendingOperation *)));
+}
+
+void RosterWidget::onConnectionReady(Telepathy::Client::PendingOperation *op)
+{
+    if (op->isError()) {
+        qWarning() << "Connection cannot become ready";
+        return;
+    }
+
+    connect(mConn->contactManager(),
+            SIGNAL(presencePublicationRequested(const Telepathy::Client::Contacts &)),
+            SLOT(onPresencePublicationRequested(const Telepathy::Client::Contacts &)));
+
+    qDebug() << "Connection ready";
+    foreach (const ContactPtr &contact, mConn->contactManager()->allKnownContacts()) {
+        createItemForContact(contact);
+    }
+
+    mAddBtn->setEnabled(true);
+}
+
+void RosterWidget::onPresencePublicationRequested(const Contacts &contacts)
+{
+    qDebug() << "Presence publication requested";
+    foreach (const ContactPtr &contact, contacts) {
+        createItemForContact(contact, true);
+    }
+}
+
+void RosterWidget::onItemSelectionChanged()
+{
+    updateActions();
+}
+
+void RosterWidget::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 RosterWidget::onAuthActionTriggered(bool checked)
+{
+    Q_UNUSED(checked);
+
+    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 RosterWidget::onDenyActionTriggered(bool checked)
+{
+    Q_UNUSED(checked);
+
+    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 RosterWidget::onRemoveActionTriggered(bool checked)
+{
+    Q_UNUSED(checked);
+
+    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 RosterWidget::onBlockActionTriggered(bool checked)
+{
+    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
+    if (selectedItems.isEmpty()) {
+        return;
+    }
+
+    Q_ASSERT(selectedItems.size() == 1);
+    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
+    item->contact()->block(checked);
+}
+
+void RosterWidget::onContactRetrieved(Telepathy::Client::PendingOperation *op)
+{
+    PendingContacts *pcontacts = qobject_cast<PendingContacts *>(op);
+    QList<ContactPtr> 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;
+    }
+
+    ContactPtr 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?
+    createItemForContact(contact, true);
+    contact->requestPresenceSubscription();
+}
+
+void RosterWidget::updateActions()
+{
+    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
+    if (selectedItems.isEmpty()) {
+        mAuthAction->setEnabled(false);
+        mDenyAction->setEnabled(false);
+        mRemoveAction->setEnabled(false);
+        mBlockAction->setEnabled(false);
+        return;
+    }
+    Q_ASSERT(selectedItems.size() == 1);
+
+    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
+    ContactPtr contact = item->contact();
+
+    ContactManager *manager = contact->manager();
+    qDebug() << "Contact" << contact->id() << "selected";
+    qDebug() << " subscription state:" << contact->subscriptionState();
+    qDebug() << " publish state     :" << contact->publishState();
+    qDebug() << " blocked           :" << contact->isBlocked();
+
+    if (manager->canAuthorizePresencePublication() &&
+        contact->publishState() == Contact::PresenceStateAsk) {
+        mAuthAction->setEnabled(true);
+    } else {
+        mAuthAction->setEnabled(false);
+    }
+
+    if (manager->canRemovePresencePublication() &&
+        contact->publishState() != Contact::PresenceStateNo) {
+        mDenyAction->setEnabled(true);
+    } else {
+        mDenyAction->setEnabled(false);
+    }
+
+    if (manager->canRemovePresenceSubscription() &&
+        contact->subscriptionState() != Contact::PresenceStateNo) {
+        mRemoveAction->setEnabled(true);
+    } else {
+        mRemoveAction->setEnabled(false);
+    }
+
+    if (manager->canBlockContacts() &&
+        contact->publishState() == Contact::PresenceStateYes) {
+        mBlockAction->setEnabled(true);
+    } else {
+        mBlockAction->setEnabled(false);
+    }
+
+    mBlockAction->setChecked(contact->isBlocked());
+}
diff --git a/examples/roster/roster-widget.h b/examples/roster/roster-widget.h
new file mode 100644
index 0000000..67b24c6
--- /dev/null
+++ b/examples/roster/roster-widget.h
@@ -0,0 +1,86 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _TelepathyQt4_examples_roster_roster_widget_h_HEADER_GUARD_
+#define _TelepathyQt4_examples_roster_roster_widget_h_HEADER_GUARD_
+
+#include <QWidget>
+
+#include <TelepathyQt4/Client/Contact>
+#include <TelepathyQt4/Client/Connection>
+
+namespace Telepathy {
+namespace Client {
+class ConnectionManager;
+class PendingOperation;
+}
+}
+
+class QAction;
+class QDialog;
+class QLineEdit;
+class QListWidget;
+class QListWidgetItem;
+class QPushButton;
+
+class RosterWidget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    RosterWidget(const QString &username, const QString &password,
+            QWidget *parent = 0);
+    virtual ~RosterWidget();
+
+private Q_SLOTS:
+    void onCMReady(Telepathy::Client::PendingOperation *);
+    void onConnectionCreated(Telepathy::Client::PendingOperation *);
+    void onConnectionReady(Telepathy::Client::PendingOperation *);
+    void onPresencePublicationRequested(const Telepathy::Client::Contacts &);
+    void onItemSelectionChanged();
+    void onAddButtonClicked();
+    void onAuthActionTriggered(bool);
+    void onDenyActionTriggered(bool);
+    void onRemoveActionTriggered(bool);
+    void onBlockActionTriggered(bool);
+    void onContactRetrieved(Telepathy::Client::PendingOperation *op);
+    void updateActions();
+
+private:
+    void createActions();
+    void setupGui();
+    void createItemForContact(const Telepathy::Client::ContactPtr &contact,
+            bool checkExists = false);
+
+    Telepathy::Client::ConnectionManager *mCM;
+    Telepathy::Client::ConnectionPtr mConn;
+    QString mUsername;
+    QString mPassword;
+    QAction *mAuthAction;
+    QAction *mRemoveAction;
+    QAction *mDenyAction;
+    QAction *mBlockAction;
+    QListWidget *mList;
+    QPushButton *mAddBtn;
+    QDialog *mAddDlg;
+    QLineEdit *mAddDlgEdt;
+};
+
+#endif
diff --git a/examples/roster/roster-window.cpp b/examples/roster/roster-window.cpp
deleted file mode 100644
index 24fd258..0000000
--- a/examples/roster/roster-window.cpp
+++ /dev/null
@@ -1,388 +0,0 @@
-/*
- * This file is part of TelepathyQt4
- *
- * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#include "roster-window.h"
-#include "_gen/roster-window.moc.hpp"
-
-#include "roster-item.h"
-
-#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 <QAction>
-#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;
-
-RosterWindow::RosterWindow(const QString &username, const QString &password,
-        QWidget *parent)
-    : QMainWindow(parent),
-      mUsername(username),
-      mPassword(password)
-{
-    setWindowTitle("Roster");
-
-    createActions();
-    setupGui();
-
-    mCM = new ConnectionManager("gabble", this);
-    connect(mCM->becomeReady(),
-            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
-            SLOT(onCMReady(Telepathy::Client::PendingOperation *)));
-
-    resize(240, 320);
-}
-
-RosterWindow::~RosterWindow()
-{
-}
-
-void RosterWindow::createActions()
-{
-    mAuthAction = new QAction(QLatin1String("Authorize Contact"), this);
-    mAuthAction->setEnabled(false);
-    connect(mAuthAction,
-            SIGNAL(triggered(bool)),
-            SLOT(onAuthActionTriggered(bool)));
-    mDenyAction = new QAction(QLatin1String("Deny Contact"), this);
-    mDenyAction->setEnabled(false);
-    connect(mDenyAction,
-            SIGNAL(triggered(bool)),
-            SLOT(onDenyActionTriggered(bool)));
-    mRemoveAction = new QAction(QLatin1String("Remove Contact"), this);
-    mRemoveAction->setEnabled(false);
-    connect(mRemoveAction,
-            SIGNAL(triggered(bool)),
-            SLOT(onRemoveActionTriggered(bool)));
-    mBlockAction = new QAction(QLatin1String("Block Contact"), this);
-    mBlockAction->setEnabled(false);
-    mBlockAction->setCheckable(true);
-    connect(mBlockAction,
-            SIGNAL(triggered(bool)),
-            SLOT(onBlockActionTriggered(bool)));
-}
-
-void RosterWindow::setupGui()
-{
-    QWidget *frame = new QWidget;
-
-    QVBoxLayout *vbox = new QVBoxLayout;
-
-    mList = new QListWidget;
-    connect(mList,
-            SIGNAL(itemSelectionChanged()),
-            SLOT(onItemSelectionChanged()));
-    vbox->addWidget(mList);
-
-    mList->setContextMenuPolicy(Qt::ActionsContextMenu);
-    mList->addAction(mAuthAction);
-    mList->addAction(mDenyAction);
-    mList->addAction(mRemoveAction);
-    mList->addAction(mBlockAction);
-
-    QHBoxLayout *hbox = new QHBoxLayout;
-
-    mAddBtn = new QPushButton("+");
-    mAddBtn->setEnabled(false);
-    connect(mAddBtn,
-            SIGNAL(clicked(bool)),
-            SLOT(onAddButtonClicked()));
-    hbox->addWidget(mAddBtn);
-    hbox->addStretch(1);
-
-    vbox->addLayout(hbox);
-
-    frame->setLayout(vbox);
-
-    setCentralWidget(frame);
-
-    mAddDlg = new QDialog(this);
-    mAddDlg->setWindowTitle("Add Contact");
-    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::createItemForContact(const ContactPtr &contact,
-        bool checkExists)
-{
-    bool found = false;
-    if (checkExists) {
-        for (int i = 0; i < mList->count(); ++i) {
-            RosterItem *item = dynamic_cast<RosterItem*>(mList->item(i));
-            if (item->contact() == contact) {
-                found = true;
-            }
-        }
-    }
-
-    if (!found) {
-        RosterItem *item = new RosterItem(contact, mList);
-        connect(item, SIGNAL(changed()), SLOT(updateActions()));
-    }
-}
-
-void RosterWindow::onCMReady(Telepathy::Client::PendingOperation *op)
-{
-    if (op->isError()) {
-        qWarning() << "CM cannot become ready";
-        return;
-    }
-
-    qDebug() << "CM ready";
-    QVariantMap params;
-    params.insert("account", QVariant(mUsername));
-    params.insert("password", QVariant(mPassword));
-    PendingConnection *pconn = mCM->requestConnection("jabber", params);
-    connect(pconn,
-            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
-            SLOT(onConnectionCreated(Telepathy::Client::PendingOperation *)));
-}
-
-void RosterWindow::onConnectionCreated(Telepathy::Client::PendingOperation *op)
-{
-    if (op->isError()) {
-        qWarning() << "Unable to create connection";
-        return;
-    }
-
-    qDebug() << "Connection created";
-    PendingConnection *pconn =
-        qobject_cast<PendingConnection *>(op);
-    mConn = pconn->connection();
-    Features features = Features() << Connection::FeatureRoster;
-    connect(mConn->requestConnect(features),
-            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
-            SLOT(onConnectionReady(Telepathy::Client::PendingOperation *)));
-}
-
-void RosterWindow::onConnectionReady(Telepathy::Client::PendingOperation *op)
-{
-    if (op->isError()) {
-        qWarning() << "Connection cannot become ready";
-        return;
-    }
-
-    connect(mConn->contactManager(),
-            SIGNAL(presencePublicationRequested(const Telepathy::Client::Contacts &)),
-            SLOT(onPresencePublicationRequested(const Telepathy::Client::Contacts &)));
-
-    qDebug() << "Connection ready";
-    foreach (const ContactPtr &contact, mConn->contactManager()->allKnownContacts()) {
-        createItemForContact(contact);
-    }
-
-    mAddBtn->setEnabled(true);
-}
-
-void RosterWindow::onPresencePublicationRequested(const Contacts &contacts)
-{
-    qDebug() << "Presence publication requested";
-    foreach (const ContactPtr &contact, contacts) {
-        createItemForContact(contact, true);
-    }
-}
-
-void RosterWindow::onItemSelectionChanged()
-{
-    updateActions();
-}
-
-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::onAuthActionTriggered(bool checked)
-{
-    Q_UNUSED(checked);
-
-    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::onDenyActionTriggered(bool checked)
-{
-    Q_UNUSED(checked);
-
-    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::onRemoveActionTriggered(bool checked)
-{
-    Q_UNUSED(checked);
-
-    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::onBlockActionTriggered(bool checked)
-{
-    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
-    if (selectedItems.isEmpty()) {
-        return;
-    }
-
-    Q_ASSERT(selectedItems.size() == 1);
-    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
-    item->contact()->block(checked);
-}
-
-void RosterWindow::onContactRetrieved(Telepathy::Client::PendingOperation *op)
-{
-    PendingContacts *pcontacts = qobject_cast<PendingContacts *>(op);
-    QList<ContactPtr> 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;
-    }
-
-    ContactPtr 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?
-    createItemForContact(contact, true);
-    contact->requestPresenceSubscription();
-}
-
-void RosterWindow::updateActions()
-{
-    QList<QListWidgetItem *> selectedItems = mList->selectedItems();
-    if (selectedItems.isEmpty()) {
-        mAuthAction->setEnabled(false);
-        mDenyAction->setEnabled(false);
-        mRemoveAction->setEnabled(false);
-        mBlockAction->setEnabled(false);
-        return;
-    }
-    Q_ASSERT(selectedItems.size() == 1);
-
-    RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
-    ContactPtr contact = item->contact();
-
-    ContactManager *manager = contact->manager();
-    qDebug() << "Contact" << contact->id() << "selected";
-    qDebug() << " subscription state:" << contact->subscriptionState();
-    qDebug() << " publish state     :" << contact->publishState();
-    qDebug() << " blocked           :" << contact->isBlocked();
-
-    if (manager->canAuthorizePresencePublication() &&
-        contact->publishState() == Contact::PresenceStateAsk) {
-        mAuthAction->setEnabled(true);
-    } else {
-        mAuthAction->setEnabled(false);
-    }
-
-    if (manager->canRemovePresencePublication() &&
-        contact->publishState() != Contact::PresenceStateNo) {
-        mDenyAction->setEnabled(true);
-    } else {
-        mDenyAction->setEnabled(false);
-    }
-
-    if (manager->canRemovePresenceSubscription() &&
-        contact->subscriptionState() != Contact::PresenceStateNo) {
-        mRemoveAction->setEnabled(true);
-    } else {
-        mRemoveAction->setEnabled(false);
-    }
-
-    if (manager->canBlockContacts() &&
-        contact->publishState() == Contact::PresenceStateYes) {
-        mBlockAction->setEnabled(true);
-    } else {
-        mBlockAction->setEnabled(false);
-    }
-
-    mBlockAction->setChecked(contact->isBlocked());
-}
diff --git a/examples/roster/roster-window.h b/examples/roster/roster-window.h
deleted file mode 100644
index 485f292..0000000
--- a/examples/roster/roster-window.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * This file is part of TelepathyQt4
- *
- * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#ifndef _TelepathyQt4_examples_roster_roster_window_h_HEADER_GUARD_
-#define _TelepathyQt4_examples_roster_roster_window_h_HEADER_GUARD_
-
-#include <QMainWindow>
-
-#include <TelepathyQt4/Client/Contact>
-#include <TelepathyQt4/Client/Connection>
-
-namespace Telepathy {
-namespace Client {
-class ConnectionManager;
-class PendingOperation;
-}
-}
-
-class QAction;
-class QDialog;
-class QLineEdit;
-class QListWidget;
-class QListWidgetItem;
-class QPushButton;
-
-class RosterWindow : public QMainWindow
-{
-    Q_OBJECT
-
-public:
-    RosterWindow(const QString &username, const QString &password,
-            QWidget *parent = 0);
-    virtual ~RosterWindow();
-
-private Q_SLOTS:
-    void onCMReady(Telepathy::Client::PendingOperation *);
-    void onConnectionCreated(Telepathy::Client::PendingOperation *);
-    void onConnectionReady(Telepathy::Client::PendingOperation *);
-    void onPresencePublicationRequested(const Telepathy::Client::Contacts &);
-    void onItemSelectionChanged();
-    void onAddButtonClicked();
-    void onAuthActionTriggered(bool);
-    void onDenyActionTriggered(bool);
-    void onRemoveActionTriggered(bool);
-    void onBlockActionTriggered(bool);
-    void onContactRetrieved(Telepathy::Client::PendingOperation *op);
-    void updateActions();
-
-private:
-    void createActions();
-    void setupGui();
-    void createItemForContact(const Telepathy::Client::ContactPtr &contact,
-            bool checkExists = false);
-
-    Telepathy::Client::ConnectionManager *mCM;
-    Telepathy::Client::ConnectionPtr mConn;
-    QString mUsername;
-    QString mPassword;
-    QAction *mAuthAction;
-    QAction *mRemoveAction;
-    QAction *mDenyAction;
-    QAction *mBlockAction;
-    QListWidget *mList;
-    QPushButton *mAddBtn;
-    QDialog *mAddDlg;
-    QLineEdit *mAddDlgEdt;
-};
-
-#endif
-- 
1.5.6.5




More information about the telepathy-commits mailing list