[Telepathy-commits] [telepathy-qt4/master] Improved roster example to support blocking contacts.
Andre Moreira Magalhaes (andrunko)
andre.magalhaes at collabora.co.uk
Fri Feb 27 14:48:22 PST 2009
Changed pushbuttons to actions triggered by context menu, to make the UI less
cluttered.
Added blocking contacts support.
Fixed some bugs.
---
examples/roster/roster-item.cpp | 21 +++-
examples/roster/roster-item.h | 5 +-
examples/roster/roster-window.cpp | 190 ++++++++++++++++++++++++++++---------
examples/roster/roster-window.h | 21 +++-
4 files changed, 180 insertions(+), 57 deletions(-)
diff --git a/examples/roster/roster-item.cpp b/examples/roster/roster-item.cpp
index 2758633..0007d7b 100644
--- a/examples/roster/roster-item.cpp
+++ b/examples/roster/roster-item.cpp
@@ -29,24 +29,27 @@ RosterItem::RosterItem(const QSharedPointer<Contact> &contact,
QListWidgetItem(parent),
mContact(contact)
{
- onPresenceStateChanged();
+ onContactChanged();
connect(contact.data(),
SIGNAL(simplePresenceChanged(const QString &, uint, const QString &)),
- SLOT(onPresenceStateChanged()));
+ SLOT(onContactChanged()));
connect(contact.data(),
SIGNAL(subscriptionStateChanged(PresenceState)),
- SLOT(onPresenceStateChanged()));
+ SLOT(onContactChanged()));
connect(contact.data(),
SIGNAL(publishStateChanged(PresenceState)),
- SLOT(onPresenceStateChanged()));
+ SLOT(onContactChanged()));
+ connect(contact.data(),
+ SIGNAL(blockStatusChanged(bool)),
+ SLOT(onContactChanged()));
}
RosterItem::~RosterItem()
{
}
-void RosterItem::onPresenceStateChanged()
+void RosterItem::onContactChanged()
{
QString status = mContact->presenceStatus();
// I've asked to see the contact presence
@@ -59,6 +62,12 @@ void RosterItem::onPresenceStateChanged()
mContact->publishState() == Contact::PresenceStateNo) {
setText(QString("%1 (unknown)").arg(mContact->id()));
} else {
- setText(QString("%1 (%2)").arg(mContact->id()).arg(status));
+ if (mContact->isBlocked()) {
+ setText(QString("%1 (%2) (blocked)").arg(mContact->id()).arg(status));
+ } else {
+ setText(QString("%1 (%2)").arg(mContact->id()).arg(status));
+ }
}
+
+ emit changed();
}
diff --git a/examples/roster/roster-item.h b/examples/roster/roster-item.h
index 48e6299..b6b14af 100644
--- a/examples/roster/roster-item.h
+++ b/examples/roster/roster-item.h
@@ -39,8 +39,11 @@ public:
QSharedPointer<Telepathy::Client::Contact> contact() const { return mContact; }
+Q_SIGNALS:
+ void changed();
+
private Q_SLOTS:
- void onPresenceStateChanged();
+ void onContactChanged();
private:
QSharedPointer<Telepathy::Client::Contact> mContact;
diff --git a/examples/roster/roster-window.cpp b/examples/roster/roster-window.cpp
index 989e535..8fe4bc4 100644
--- a/examples/roster/roster-window.cpp
+++ b/examples/roster/roster-window.cpp
@@ -34,6 +34,7 @@
#include <TelepathyQt4/Client/PendingReady>
#include <TelepathyQt4/Client/PendingReadyConnectionManager>
+#include <QAction>
#include <QDebug>
#include <QDialog>
#include <QDialogButtonBox>
@@ -54,18 +55,48 @@ RosterWindow::RosterWindow(const QString &username, const QString &password,
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;
@@ -73,37 +104,26 @@ void RosterWindow::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("Add contact");
+ mAddBtn = new QPushButton("+");
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);
+ hbox->addStretch(1);
vbox->addLayout(hbox);
@@ -112,6 +132,7 @@ void RosterWindow::setupGui()
setCentralWidget(frame);
mAddDlg = new QDialog(this);
+ mAddDlg->setWindowTitle("Add Contact");
QVBoxLayout *addDlgVBox = new QVBoxLayout;
QHBoxLayout *addDlgEntryHBox = new QHBoxLayout;
@@ -130,6 +151,25 @@ void RosterWindow::setupGui()
mAddDlg->setLayout(addDlgVBox);
}
+void RosterWindow::createItemForContact(const QSharedPointer<Contact> &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()) {
@@ -177,23 +217,25 @@ void RosterWindow::onConnectionReady(Telepathy::Client::PendingOperation *op)
qDebug() << "Connection ready";
foreach (const QSharedPointer<Contact> &contact, mConn->contactManager()->allKnownContacts()) {
- (void) new RosterItem(contact, mList);
+ createItemForContact(contact);
}
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);
+ createItemForContact(contact, true);
}
}
+void RosterWindow::onItemSelectionChanged()
+{
+ updateActions();
+}
+
void RosterWindow::onAddButtonClicked()
{
mAddDlgEdt->clear();
@@ -210,8 +252,10 @@ void RosterWindow::onAddButtonClicked()
SLOT(onContactRetrieved(Telepathy::Client::PendingOperation *)));
}
-void RosterWindow::onAuthButtonClicked()
+void RosterWindow::onAuthActionTriggered(bool checked)
{
+ Q_UNUSED(checked);
+
QList<QListWidgetItem *> selectedItems = mList->selectedItems();
if (selectedItems.isEmpty()) {
return;
@@ -224,8 +268,27 @@ void RosterWindow::onAuthButtonClicked()
}
}
-void RosterWindow::onRemoveButtonClicked()
+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;
@@ -240,7 +303,7 @@ void RosterWindow::onRemoveButtonClicked()
}
}
-void RosterWindow::onDenyButtonClicked()
+void RosterWindow::onBlockActionTriggered(bool checked)
{
QList<QListWidgetItem *> selectedItems = mList->selectedItems();
if (selectedItems.isEmpty()) {
@@ -249,10 +312,7 @@ void RosterWindow::onDenyButtonClicked()
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();
- }
+ item->contact()->block(checked);
}
void RosterWindow::onContactRetrieved(Telepathy::Client::PendingOperation *op)
@@ -272,16 +332,58 @@ void RosterWindow::onContactRetrieved(Telepathy::Client::PendingOperation *op)
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;
- }
+ 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;
}
- if (!found) {
- (void) new RosterItem(contact, mList);
+ Q_ASSERT(selectedItems.size() == 1);
+
+ RosterItem *item = dynamic_cast<RosterItem*>(selectedItems.first());
+ QSharedPointer<Contact> 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->canAuthorizeContactsPresencePublication() &&
+ contact->publishState() == Contact::PresenceStateAsk) {
+ mAuthAction->setEnabled(true);
+ } else {
+ mAuthAction->setEnabled(false);
}
- contact->requestPresenceSubscription();
+
+ if (manager->canRemoveContactsPresencePublication() &&
+ contact->publishState() != Contact::PresenceStateNo) {
+ mDenyAction->setEnabled(true);
+ } else {
+ mDenyAction->setEnabled(false);
+ }
+
+ if (manager->canRemoveContactsPresenceSubscription() &&
+ 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
index 30e71d5..a57e4d6 100644
--- a/examples/roster/roster-window.h
+++ b/examples/roster/roster-window.h
@@ -33,9 +33,11 @@ class PendingOperation;
}
}
+class QAction;
class QDialog;
class QLineEdit;
class QListWidget;
+class QListWidgetItem;
class QPushButton;
class RosterWindow : public QMainWindow
@@ -52,24 +54,31 @@ private Q_SLOTS:
void onConnectionCreated(Telepathy::Client::PendingOperation *);
void onConnectionReady(Telepathy::Client::PendingOperation *);
void onPresencePublicationRequested(const QSet<QSharedPointer<Telepathy::Client::Contact> > &);
+ void onItemSelectionChanged();
void onAddButtonClicked();
- void onAuthButtonClicked();
- void onRemoveButtonClicked();
- void onDenyButtonClicked();
+ 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 QSharedPointer<Telepathy::Client::Contact> &contact,
+ bool checkExists = false);
Telepathy::Client::ConnectionManager *mCM;
QSharedPointer<Telepathy::Client::Connection> mConn;
QString mUsername;
QString mPassword;
+ QAction *mAuthAction;
+ QAction *mRemoveAction;
+ QAction *mDenyAction;
+ QAction *mBlockAction;
QListWidget *mList;
QPushButton *mAddBtn;
- QPushButton *mAuthBtn;
- QPushButton *mRemoveBtn;
- QPushButton *mDenyBtn;
QDialog *mAddDlg;
QLineEdit *mAddDlgEdt;
};
--
1.5.6.5
More information about the telepathy-commits
mailing list