From 65264b517c4666d345f029613266eed06a47ee67 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Thu, 17 Feb 2011 14:10:10 +0000 Subject: [PATCH] Change XMPP connection to use stored credentials. --- src/main.cpp | 6 +----- src/remote/xmpp.cpp | 39 ++++++++++++++++++++++++++++++--------- src/remote/xmpp.h | 11 +++++++++-- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0f6a1c4d7..daf1be173 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -331,12 +331,8 @@ int main(int argc, char *argv[]) { #endif #ifdef HAVE_REMOTE - Zeroconf* zeroconf = Zeroconf::GetZeroconf(); - if (zeroconf) { - zeroconf->Publish("local", "_clementine._tcp", "Clementine", 12345); - } XMPP xmpp; - xmpp.Connect("timetabletest2@googlemail.com", "timetabletestpassword"); + xmpp.Connect(); #endif // Window diff --git a/src/remote/xmpp.cpp b/src/remote/xmpp.cpp index c70452b2e..9ab8a27f0 100644 --- a/src/remote/xmpp.cpp +++ b/src/remote/xmpp.cpp @@ -3,8 +3,11 @@ #include #include +#include #include +#include "keychain.h" + using gloox::Client; using gloox::ConnectionTCPClient; using gloox::JID; @@ -19,34 +22,52 @@ XMPP::~XMPP() { } -bool XMPP::Connect(const QString& jid, const QString& password) { - qDebug() << Q_FUNC_INFO; +void XMPP::Connect() { + QSettings s; + s.beginGroup("remote"); + QVariant username = s.value("username"); + if (username.isValid()) { + Keychain* keychain = Keychain::getDefault(); + QString password = keychain->getPassword(username.toString()); + Connect(username.toString() + "/clementine", password); + } else { + qWarning() << "No username or password set."; + } +} + +void XMPP::Connect(const QString& jid, const QString& password) { // TODO: Generate <256 char resource. JID j(jid.toUtf8().constData()); - qDebug() << "Resource:" << j.resource().c_str(); client_.reset(new Client(j, password.toUtf8().constData())); + client_->registerConnectionListener(this); client_->registerMessageHandler(this); client_->setServer("talk.google.com"); client_->connect(false); - qDebug() << Q_FUNC_INFO; int fd = static_cast(client_->connectionImpl())->socket(); notifier_.reset(new QSocketNotifier(fd, QSocketNotifier::Read)); connect(notifier_.get(), SIGNAL(activated(int)), SLOT(Receive())); - - qDebug() << Q_FUNC_INFO; - return true; } void XMPP::handleMessage(const Message& stanza, MessageSession* session) { qDebug() << Q_FUNC_INFO; qDebug() << stanza.tag()->xml().c_str(); - qDebug() << "resource:" << client_->resource().c_str(); Message reply(Message::Chat, stanza.from(), "Hello World!"); client_->send(reply); } +void XMPP::onConnect() { + qDebug() << "Connected with resource:" << client_->resource().c_str(); +} + +void XMPP::onDisconnect(gloox::ConnectionError e) { + qDebug() << "Disconnected:" << e; +} + +bool XMPP::onTLSConnect(const gloox::CertInfo& info) { + return true; +} + void XMPP::Receive() { - qDebug() << Q_FUNC_INFO; client_->recv(); } diff --git a/src/remote/xmpp.h b/src/remote/xmpp.h index 02bc87202..03ecdca86 100644 --- a/src/remote/xmpp.h +++ b/src/remote/xmpp.h @@ -2,6 +2,7 @@ #define XMPP_H #include +#include #include #include @@ -9,13 +10,14 @@ #include #include -class XMPP : public QObject, public gloox::MessageHandler { +class XMPP : public QObject, public gloox::ConnectionListener, public gloox::MessageHandler { Q_OBJECT public: XMPP(); virtual ~XMPP(); - bool Connect(const QString& jid, const QString& password); + void Connect(); + void Connect(const QString& jid, const QString& password); private slots: void Receive(); @@ -25,6 +27,11 @@ class XMPP : public QObject, public gloox::MessageHandler { virtual void handleMessage(const gloox::Message& stanza, gloox::MessageSession* session = 0); + // gloox::ConnectionListener + virtual void onConnect(); + virtual void onDisconnect(gloox::ConnectionError e); + virtual bool onTLSConnect(const gloox::CertInfo& info); + boost::scoped_ptr client_; boost::scoped_ptr notifier_; };