File: connection.cpp

package info (click to toggle)
libqinfinity 1.0~beta5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 648 kB
  • ctags: 694
  • sloc: cpp: 4,943; makefile: 3
file content (100 lines) | stat: -rw-r--r-- 2,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "connection.h"

#include "connection.moc"

Connection::Connection( const QString &hostname,
	unsigned int port,
	QObject *parent )
	: QObject( parent )
	, m_hostname( hostname )
	, port( port )
	, tcpConnection( 0 )
	, m_xmppConnection( 0 )
{
}

Connection::~Connection()
{
}

void Connection::open()
{
	QHostInfo::lookupHost( hostname(), this,
		SLOT(slotHostnameLookedUp(QHostInfo)) );
}

QString Connection::hostname() const
{
	return m_hostname;
}

QInfinity::XmppConnection &Connection::xmppConnection() const
{
	return *m_xmppConnection;
}

QString Connection::name() const
{
	QString ret = m_hostname;
	ret += ":";
	ret += QString::number( port );
	return ret;
}

void Connection::slotHostnameLookedUp( const QHostInfo &hostInfo )
{
	if( hostInfo.addresses().size() == 0 )
	{
		qDebug() << "Couldnt find hostname.";
		emit(error( this, "Hostname Not Found" ));
		return;
	}
	qDebug() << "Connecting to " << hostInfo.addresses()[0].toString();
	tcpConnection = new QInfinity::TcpConnection( QInfinity::IpAddress( hostInfo.addresses()[0] ),
		port,
		this );

	m_xmppConnection = new QInfinity::XmppConnection( *tcpConnection,
		QInfinity::XmppConnection::Client,
		"localhost", "localhost",
		QInfinity::XmppConnection::PreferTls,
		0, 0, 0,
		this );

	connect( m_xmppConnection, SIGNAL(statusChanged()),
		this, SLOT(slotXmlConnectionStatusChanged()) );
	connect( m_xmppConnection, SIGNAL(error(const GError*)),
		this, SLOT(slotXmlConnectionError(const GError*)) );

	tcpConnection->open();

}

void Connection::slotXmlConnectionStatusChanged()
{
	switch( m_xmppConnection->status() )
	{
		case QInfinity::XmlConnection::Closing:
			emit(disconnecting( this ));
			break;
		case QInfinity::XmlConnection::Open:
			qDebug() << "Open";
			emit(connected( this ));
			break;
		case QInfinity::XmlConnection::Closed:
			qDebug() << "Closed";
			emit(disconnected( this ));
			break;
		case QInfinity::XmlConnection::Opening:
			emit(connecting( this ));
			qDebug() << "Opening";
			break;
	}
}

void Connection::slotXmlConnectionError( const GError *err )
{
	qDebug() << "connection error: " << err->message;
	emit(error( this, QString(err->message) ));
}