[Telepathy-commits] [telepathy-qt4/master] Added roster example.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Wed Feb 25 09:21:54 PST 2009


This example will show all the contacts of a given account using gabble
and track their presence status.
---
 configure.ac                      |    1 +
 examples/Makefile.am              |    2 +-
 examples/roster/Makefile.am       |   34 +++++++++++
 examples/roster/main.cpp          |   27 +++++++++
 examples/roster/roster-item.cpp   |   45 +++++++++++++++
 examples/roster/roster-item.h     |   49 ++++++++++++++++
 examples/roster/roster-window.cpp |  111 +++++++++++++++++++++++++++++++++++++
 examples/roster/roster-window.h   |   61 ++++++++++++++++++++
 8 files changed, 329 insertions(+), 1 deletions(-)
 create mode 100644 examples/roster/Makefile.am
 create mode 100644 examples/roster/main.cpp
 create mode 100644 examples/roster/roster-item.cpp
 create mode 100644 examples/roster/roster-item.h
 create mode 100644 examples/roster/roster-window.cpp
 create mode 100644 examples/roster/roster-window.h

diff --git a/configure.ac b/configure.ac
index 4ee29bc..a2a710a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -242,6 +242,7 @@ AC_OUTPUT([
     examples/Makefile
     examples/extensions/Makefile
     examples/accounts/Makefile
+    examples/roster/Makefile
     m4/Makefile
     spec/Makefile
     tests/Makefile
diff --git a/examples/Makefile.am b/examples/Makefile.am
index e399b2e..4523c79 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -1 +1 @@
-SUBDIRS = extensions accounts
+SUBDIRS = extensions accounts roster
diff --git a/examples/roster/Makefile.am b/examples/roster/Makefile.am
new file mode 100644
index 0000000..4c5bf3e
--- /dev/null
+++ b/examples/roster/Makefile.am
@@ -0,0 +1,34 @@
+AM_CXXFLAGS = \
+	$(ERROR_CXXFLAGS) \
+	$(QTCORE_CFLAGS) \
+	$(QTGUI_CFLAGS) \
+	$(QTDBUS_CFLAGS) \
+	$(TP_QT4_CFLAGS)
+
+noinst_PROGRAMS = roster
+
+roster_LDADD = \
+	$(QTGUI_LIBS) \
+	$(QTDBUS_LIBS) \
+	$(top_builddir)/TelepathyQt4/libtelepathy-qt4.la
+
+roster_SOURCES = \
+	main.cpp \
+	roster-item.cpp \
+	roster-item.h \
+	roster-window.cpp \
+	roster-window.h
+
+nodist_roster_SOURCES = \
+	_gen/roster-item.moc.hpp \
+	_gen/roster-window.moc.hpp
+
+BUILT_SOURCES = \
+	$(nodist_roster_SOURCES)
+
+CLEANFILES = \
+	$(BUILT_SOURCES)
+
+_gen/%.moc.hpp: %.h
+	$(mkdir_p) _gen
+	$(MOC) @QTCORE_CFLAGS@ @QTDBUS_CFLAGS@ -I$(top_builddir) -I$(top_srcdir) -i $< -o $@
diff --git a/examples/roster/main.cpp b/examples/roster/main.cpp
new file mode 100644
index 0000000..5d39880
--- /dev/null
+++ b/examples/roster/main.cpp
@@ -0,0 +1,27 @@
+#include <TelepathyQt4/Debug>
+#include <TelepathyQt4/Constants>
+#include <TelepathyQt4/Types>
+
+#include <QDebug>
+#include <QtGui>
+
+#include "roster-window.h"
+
+int main(int argc, char **argv)
+{
+    QApplication app(argc, argv);
+
+    if (argc < 3) {
+        qDebug() << "usage: roster username password";
+        return 1;
+    }
+
+    Telepathy::registerTypes();
+    Telepathy::enableDebug(true);
+    Telepathy::enableWarnings(true);
+
+    RosterWindow w(argv[1], argv[2]);
+    w.show();
+
+    return app.exec();
+}
diff --git a/examples/roster/roster-item.cpp b/examples/roster/roster-item.cpp
new file mode 100644
index 0000000..db71965
--- /dev/null
+++ b/examples/roster/roster-item.cpp
@@ -0,0 +1,45 @@
+/*
+ * 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-item.h"
+#include "_gen/roster-item.moc.hpp"
+
+using namespace Telepathy::Client;
+
+RosterItem::RosterItem(const QSharedPointer<Contact> &contact,
+        QListWidget *parent)
+    : QObject(parent),
+      QListWidgetItem(parent),
+      mContact(contact)
+{
+    setText(QString("%1 (%2)").arg(contact->id()).arg(contact->presenceStatus()));
+    connect(contact.data(),
+            SIGNAL(simplePresenceChanged(const QString &, uint, const QString &)),
+            SLOT(onSimplePresenceChanged(const QString &, uint, const QString &)));
+}
+
+RosterItem::~RosterItem()
+{
+}
+
+void RosterItem::onSimplePresenceChanged(const QString &status, uint type, const QString &presenceMessage)
+{
+    setText(QString("%1 (%2)").arg(mContact->id()).arg(status));
+}
diff --git a/examples/roster/roster-item.h b/examples/roster/roster-item.h
new file mode 100644
index 0000000..09ad7ab
--- /dev/null
+++ b/examples/roster/roster-item.h
@@ -0,0 +1,49 @@
+/*
+ * 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_item_h_HEADER_GUARD_
+#define _TelepathyQt4_examples_roster_roster_item_h_HEADER_GUARD_
+
+#include <TelepathyQt4/Types>
+#include <TelepathyQt4/Client/Contact>
+
+#include <QListWidgetItem>
+#include <QSharedPointer>
+#include <QString>
+
+class RosterItem : public QObject, public QListWidgetItem
+{
+    Q_OBJECT
+
+public:
+    RosterItem(const QSharedPointer<Telepathy::Client::Contact> &contact,
+            QListWidget *parent = 0);
+    ~RosterItem();
+
+private Q_SLOTS:
+    void onSimplePresenceChanged(const QString &status,
+            uint type,
+            const QString &presenceMessage);
+
+private:
+    QSharedPointer<Telepathy::Client::Contact> mContact;
+};
+
+#endif
diff --git a/examples/roster/roster-window.cpp b/examples/roster/roster-window.cpp
new file mode 100644
index 0000000..5f093ea
--- /dev/null
+++ b/examples/roster/roster-window.cpp
@@ -0,0 +1,111 @@
+/*
+ * 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/ContactManager>
+#include <TelepathyQt4/Client/PendingConnection>
+#include <TelepathyQt4/Client/PendingOperation>
+#include <TelepathyQt4/Client/PendingReady>
+#include <TelepathyQt4/Client/PendingReadyConnectionManager>
+
+#include <QDebug>
+#include <QHBoxLayout>
+#include <QListWidget>
+#include <QListWidgetItem>
+
+using namespace Telepathy::Client;
+
+RosterWindow::RosterWindow(const QString &username, const QString &password,
+        QWidget *parent)
+    : QMainWindow(parent),
+      mUsername(username),
+      mPassword(password)
+{
+    setupGui();
+
+    mCM = new ConnectionManager("gabble", this);
+    connect(mCM->becomeReady(),
+            SIGNAL(finished(Telepathy::Client::PendingOperation *)),
+            SLOT(onCMReady(Telepathy::Client::PendingOperation *)));
+}
+
+RosterWindow::~RosterWindow()
+{
+}
+
+void RosterWindow::setupGui()
+{
+    mList = new QListWidget;
+    setCentralWidget(mList);
+}
+
+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();
+    QSet<uint> features = QSet<uint>() << 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;
+    }
+
+    qDebug() << "Connection ready";
+    foreach (const QSharedPointer<Contact> &contact, mConn->contactManager()->allKnownContacts()) {
+        (void) new RosterItem(contact, mList);
+    }
+}
diff --git a/examples/roster/roster-window.h b/examples/roster/roster-window.h
new file mode 100644
index 0000000..eb89742
--- /dev/null
+++ b/examples/roster/roster-window.h
@@ -0,0 +1,61 @@
+/*
+ * 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 <QSharedPointer>
+
+namespace Telepathy {
+namespace Client {
+class Connection;
+class ConnectionManager;
+class PendingOperation;
+}
+}
+
+class QListWidget;
+
+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 *);
+
+private:
+    void setupGui();
+
+    Telepathy::Client::ConnectionManager *mCM;
+    QSharedPointer<Telepathy::Client::Connection> mConn;
+    QString mUsername;
+    QString mPassword;
+    QListWidget *mList;
+};
+
+#endif
-- 
1.5.6.5




More information about the telepathy-commits mailing list