aboutsummaryrefslogtreecommitdiff
path: root/source/QXmppClient.cpp
blob: 5c6f892b01b8f12f74df1d897efd5dc0811b567d (plain) (blame)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (C) 2008-2009 Manjeet Dahiya
 *
 * Author:
 *	Manjeet Dahiya
 *
 * Source:
 *	http://code.google.com/p/qxmpp
 *
 * This file is a part of QXmpp library.
 *
 * 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.
 *
 */


#include "QXmppClient.h"
#include "QXmppStream.h"
#include "QXmppRoster.h"
#include "QXmppMessage.h"
#include "QXmppReconnectionManager.h"

/// Creates a QXmppClient object.
/// \param parent is passed to the QObject's contructor.
/// The default value is 0.

QXmppClient::QXmppClient(QObject *parent)
    : QObject(parent), m_stream(0), m_clientPrecence(QXmppPresence::Available),
    m_reconnectionManager(0)
{
    m_stream = new QXmppStream(this);

    bool check = connect(m_stream, SIGNAL(messageReceived(const QXmppMessage&)),
                         this, SIGNAL(messageReceived(const QXmppMessage&)));
    Q_ASSERT(check);

    check = connect(m_stream, SIGNAL(presenceReceived(const QXmppPresence&)),
                    this, SIGNAL(presenceReceived(const QXmppPresence&)));
    Q_ASSERT(check);

    check = connect(m_stream, SIGNAL(iqReceived(const QXmppIq&)), this,
        SIGNAL(iqReceived(const QXmppIq&)));

    check = connect(m_stream, SIGNAL(disconnected()), this,
        SIGNAL(disconnected()));
    Q_ASSERT(check);

    check = connect(m_stream, SIGNAL(xmppConnected()), this,
        SIGNAL(connected()));
    Q_ASSERT(check);

    check = connect(m_stream, SIGNAL(error(QXmppClient::Error)), this,
        SIGNAL(error(QXmppClient::Error)));
    Q_ASSERT(check);

    check = setReconnectionManager(new QXmppReconnectionManager(this));
    Q_ASSERT(check);
}

/// Destroys the QXmppClient object.
///

QXmppClient::~QXmppClient()
{
}

/// Returns a modifiable reference to the current configuration of QXmppClient.
/// \return Reference to the QXmppClient's configuration for the connection.

QXmppConfiguration& QXmppClient::getConfiguration()
{
    return m_config;
}

/// Overloaded function. It returns a const reference to the current configuration
/// of QXmppClient.
/// \return Constant reference to the QXmppClient's configuration for the connection.

const QXmppConfiguration& QXmppClient::getConfiguration() const
{
    return m_config;
}

/// Attempts to connect to the XMPP server. Server deatils and other configurations
/// are specified using the config parameter. Use signals connected(), error(QXmppClient::Error)
/// and disconnected() to know the status of the connection.
/// \param config Specifies the configuration object for connecting the XMPP server.
/// This contains the host name, user, passwd etc. See QXmppConfiguration for details.
/// \param initialPresence The initial presence which will be set for this user
/// after establishing the session. The default value is QXmppPresence::Available

void QXmppClient::connectToServer(const QXmppConfiguration& config,
                                  const QXmppPresence& initialPresence)
{
    m_config = config;

    if(!m_config.getAutoReconnectionEnabled())
    {
        delete m_reconnectionManager;
        m_reconnectionManager = 0;
    }

    m_clientPrecence = initialPresence;

    m_stream->connect();
}

/// Overloaded function.
/// \param host host name of the XMPP server where connection has to be made
/// (e.g. "jabber.org" and "talk.google.com"). It can also be an IP address in
/// the form of a string (e.g. "192.168.1.25").
/// \param user Username of the account at the specified XMPP server. It should
/// be the name without the domain name. E.g. "qxmpp.test1" and not
/// "qxmpp.test1@gmail.com"
/// \param passwd Password for the specified username
/// \param domain Domain name e.g. "gmail.com" and "jabber.org".
/// \param port Port number at which the XMPP server is listening. The default
/// value is 5222.
/// \param initialPresence The initial presence which will be set for this user
/// after establishing the session. The default value is QXmppPresence::Available

void QXmppClient::connectToServer(const QString& host, const QString& user,
                                  const QString& passwd, const QString& domain,
                                  int port,
                                  const QXmppPresence& initialPresence)
{
    m_config.setHost(host);
    m_config.setUser(user);
    m_config.setPasswd(passwd);
    m_config.setDomain(domain);
    m_config.setPort(port);

    m_clientPrecence = initialPresence;

    m_stream->connect();
}

/// After successfully connecting to the server use this function to send
/// stanzas to the server. This function can solely be used to send various kind
/// of stanzas to the server. QXmppPacket is a parent class of all the stanzas
/// QXmppMessage, QXmppPresence, QXmppIq, QXmppBind, QXmppRosterIq, QXmppSession
/// and QXmppVCard.
///
/// Following code snippet illustrates how to send a message using this function:
/// \code
/// QXmppMessage message(from, to, message);
/// client.sendPacket(message);
/// \endcode
///
/// \param packet A valid XMPP stanza. It can an iq, a message or a presence stanza.
///

void QXmppClient::sendPacket(const QXmppPacket& packet)
{
    if(m_stream)
    {
        m_stream->sendPacket(packet);
    }
}

/// Disconnects the client and the current presence of client changes to
/// QXmppPresence::Unavailable and statatus text changes to "Logged out".
///
/// \note Make sure that the clientPresence is changed to
/// QXmppPresence::Available, if you are again calling connectToServer() after
/// calling the disconnect() function.
///

void QXmppClient::disconnect()
{
    m_clientPrecence.setType(QXmppPresence::Unavailable);
    m_clientPrecence.getStatus().setType(QXmppPresence::Status::Online);
    m_clientPrecence.getStatus().setStatusText("Logged out");
    sendPacket(m_clientPrecence);
    if(m_stream)
        m_stream->disconnect();
}

/// Returns the reference to QXmppRoster object of the client.
/// \return Reference to the roster object of the connected client. Use this to
/// get the list of friends in the roster and there presence information.
///

QXmppRoster& QXmppClient::getRoster()
{
    if(m_stream)
        return m_stream->getRoster();
}

/// Utility function to send message to all the resources associated with the
/// specified bareJid.
///
/// \param bareJid bareJid of the receiving entity
/// \param message Message string to be sent.

void QXmppClient::sendMessage(const QString& bareJid, const QString& message)
{
    QStringList resources = getRoster().getResources(bareJid);
    for(int i = 0; i < resources.size(); ++i)
    {
        sendPacket(QXmppMessage("", bareJid + "/" + resources.at(i), message));
    }
}

/// Changes the presence of the connected client.
///
/// \param presence QXmppPresence object
///

void QXmppClient::setClientPresence(const QXmppPresence& presence)
{
    m_clientPrecence = presence;
    sendPacket(m_clientPrecence);
}

/// Overloaded function.
///
/// It only changes the status text.
///
/// \param statusText New status message string
///

void QXmppClient::setClientPresence(const QString& statusText)
{
    m_clientPrecence.getStatus().setStatusText(statusText);
    sendPacket(m_clientPrecence);
}

/// Overloaded function.
///
/// It only changes the QXmppPresence::Type.
///
/// \param presenceType New QXmppPresence::Type
///

void QXmppClient::setClientPresence(QXmppPresence::Type presenceType)
{
    if(presenceType == QXmppPresence::Unavailable)
    {
        disconnect();
    }
    else
    {
        m_clientPrecence.setType(presenceType);
        sendPacket(m_clientPrecence);
    }
}

/// Overloaded function.
///
/// It only changes the QXmppPresence::Status::Type.
///
/// \param statusType New QXmppPresence::Status::Type
///

void QXmppClient::setClientPresence(QXmppPresence::Status::Type statusType)
{
    m_clientPrecence.getStatus().setType(statusType);
    sendPacket(m_clientPrecence);
}

/// Function to get the client's current presence.
///
/// \return Constant reference to the client's presence object
///

const QXmppPresence& QXmppClient::getClientPresence() const
{
    return m_clientPrecence;
}

/// Function to get reconnection manager. By default there exists a reconnection
/// manager. See QXmppReconnectionManager for more details of the reconnection
/// mechanism.
///
/// \return Pointer to QXmppReconnectionManager
///

QXmppReconnectionManager* QXmppClient::getReconnectionManager()
{
    return m_reconnectionManager;
}

/// Sets the user defined reconnection manager.
///
/// \return true if all the signal-slot connections are made correctly.
///

bool QXmppClient::setReconnectionManager(QXmppReconnectionManager*
                                         reconnectionManager)
{
	if(!reconnectionManager)
		return false;
		
    if(m_reconnectionManager)
        delete m_reconnectionManager;

    m_reconnectionManager = reconnectionManager;

    bool check = connect(this, SIGNAL(connected()), m_reconnectionManager,
                         SLOT(connected()));
    Q_ASSERT(check);
	if(!check)
		return false;

    check = connect(this, SIGNAL(error(QXmppClient::Error)),
                    m_reconnectionManager, SLOT(error(QXmppClient::Error)));
    Q_ASSERT(check);
	if(!check)
		return false;
	
    return true;
}

/// Returns the socket error if QXmppClient::Error is QXmppClient::SocketError.
///
/// \return QAbstractSocket::SocketError
///

QAbstractSocket::SocketError QXmppClient::getSocketError()
{
    return m_stream->getSocketError();
}

/// Returns the reference to QXmppVCardManager, implimentation of XEP-0054.
/// http://xmpp.org/extensions/xep-0054.html
///

QXmppVCardManager& QXmppClient::getVCardManager()
{
    return m_stream->getVCardManager();
}