Change XMPP connection to use stored credentials.

This commit is contained in:
John Maguire 2011-02-17 14:10:10 +00:00
parent 7717005415
commit 65264b517c
3 changed files with 40 additions and 16 deletions

View File

@ -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

View File

@ -3,8 +3,11 @@
#include <gloox/connectiontcpclient.h>
#include <gloox/message.h>
#include <QSettings>
#include <QtDebug>
#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<ConnectionTCPClient*>(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();
}

View File

@ -2,6 +2,7 @@
#define XMPP_H
#include <gloox/client.h>
#include <gloox/connectionlistener.h>
#include <gloox/messagehandler.h>
#include <boost/scoped_ptr.hpp>
@ -9,13 +10,14 @@
#include <QSocketNotifier>
#include <QString>
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<gloox::Client> client_;
boost::scoped_ptr<QSocketNotifier> notifier_;
};