Skip to content

Commit

Permalink
Merge pull request gamenet#6 from n-gorbunov/master
Browse files Browse the repository at this point in the history
Client presence issues
  • Loading branch information
Ilyatk committed May 22, 2014
2 parents 35d504a + 0cd8d3d commit de23e40
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 46 deletions.
69 changes: 64 additions & 5 deletions QmlClient/plugins/QXmpp/plugins.qmltypes
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ Module {
prototype: "QObject"
exports: ["QXmpp/QXmppClient 1.0"]
exportMetaObjectRevisions: [0]
Enum {
name: "StatusType"
values: {
"Online": 0,
"Away": 1,
"XA": 2,
"DND": 3,
"Chat": 4,
"Invisible": 5
}
}
Property {
name: "configuration"
type: "QmlQXmppConfiguration"
Expand All @@ -97,8 +108,8 @@ Module {
isPointer: true
}
Property { name: "vcardManager"; type: "QmlQXmppVCardManager"; isReadonly: true; isPointer: true }
Property { name: "statusType"; type: "string" }
Property { name: "statusText"; type: "string" }
Property { name: "clientStatusType"; type: "StatusType"; isReadonly: true }
Property { name: "clientStatusText"; type: "string"; isReadonly: true }
Method { name: "connectUsingConfiguration" }
Method {
name: "connectToServer"
Expand All @@ -109,7 +120,11 @@ Module {
Method {
name: "sendMessage"
Parameter { name: "bareJid"; type: "string" }
Parameter { name: "message"; type: "string" }
Parameter { name: "map"; type: "QVariantMap" }
}
Method {
name: "setClientPresence"
Parameter { name: "map"; type: "QVariantMap" }
}
}
Component {
Expand All @@ -129,6 +144,50 @@ Module {
Property { name: "keepAliveInterval"; type: "int" }
Property { name: "keepAliveTimeout"; type: "int" }
}
Component {
name: "QmlQXmppMessage"
prototype: "QObject"
exports: ["QXmpp/QXmppMessage 1.0"]
exportMetaObjectRevisions: [0]
Enum {
name: "Type"
values: {
"Error": 0,
"Normal": 1,
"Chat": 2,
"GroupChat": 3,
"Headline": 4
}
}
Enum {
name: "State"
values: {
"None": 0,
"Active": 1,
"Inactive": 2,
"Gone": 3,
"Composing": 4,
"Paused": 5
}
}
Property { name: "body"; type: "string"; isReadonly: true }
Property { name: "isAttentionRequested"; type: "bool"; isReadonly: true }
Property { name: "isReceiptRequested"; type: "bool"; isReadonly: true }
Property { name: "mucInvitationJid"; type: "string"; isReadonly: true }
Property { name: "mucInvitationPassword"; type: "string"; isReadonly: true }
Property { name: "mucInvitationReason"; type: "string"; isReadonly: true }
Property { name: "receiptId"; type: "string"; isReadonly: true }
Property { name: "stamp"; type: "QDateTime"; isReadonly: true }
Property { name: "subject"; type: "string"; isReadonly: true }
Property { name: "thread"; type: "string"; isReadonly: true }
Property { name: "xhtml"; type: "string"; isReadonly: true }
Property { name: "state"; type: "QmlQXmppMessage::State"; isReadonly: true }
Property { name: "type"; type: "QmlQXmppMessage::Type"; isReadonly: true }
Property { name: "to"; type: "string"; isReadonly: true }
Property { name: "from"; type: "string"; isReadonly: true }
Property { name: "id"; type: "string"; isReadonly: true }
Property { name: "lang"; type: "string"; isReadonly: true }
}
Component {
name: "QmlQXmppPresence"
prototype: "QObject"
Expand Down Expand Up @@ -261,8 +320,8 @@ Module {
Component {
name: "QmlQXmppVCard"
prototype: "QObject"
exports: ["QXmpp/QXmppVCard 1.0", "QXmpp/QXmppVCardEx 1.0"]
exportMetaObjectRevisions: [0, 0]
exports: ["QXmpp/QXmppVCard 1.0"]
exportMetaObjectRevisions: [0]
Property { name: "from"; type: "string" }
Property { name: "birthday"; type: "QDate" }
Property { name: "description"; type: "string" }
Expand Down
Binary file modified QmlClient/plugins/QXmpp/qxmpp-declarative.dll
Binary file not shown.
28 changes: 20 additions & 8 deletions qxmpp-declarative/include/QmlQXmppClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,33 @@ class QXmppArchiveManager;
class QmlQXmppClient : public QObject
{
Q_OBJECT
Q_ENUMS(StatusType)
Q_PROPERTY(QmlQXmppConfiguration *configuration READ configuration CONSTANT)
Q_PROPERTY(QmlQXmppArchiveManager* archiveManager READ archiveManager CONSTANT)
Q_PROPERTY(QmlQXmppRosterManager* rosterManager READ rosterManager CONSTANT)
Q_PROPERTY(QmlQXmppVCardManager* vcardManager READ vcardManager CONSTANT)
Q_PROPERTY(QString statusType READ clientStatusType WRITE setClientStatusType NOTIFY statusTypeChanged)
Q_PROPERTY(QString statusText READ clientStatusText WRITE setClientStatusText NOTIFY statusTextChanged)
Q_PROPERTY(StatusType clientStatusType READ clientStatusType NOTIFY clientStatusTypeChanged)
Q_PROPERTY(QString clientStatusText READ clientStatusText NOTIFY clientStatusTextChanged)

public:
enum StatusType {
Online = QXmppPresence::Online,
Away,
XA,
DND,
Chat,
Invisible
};

QmlQXmppClient(QObject *parent = 0);
~QmlQXmppClient();

QmlQXmppConfiguration *configuration();
QmlQXmppArchiveManager *archiveManager();
QmlQXmppRosterManager *rosterManager();
QmlQXmppVCardManager* vcardManager();

QString clientStatusType();
void setClientStatusType(const QString &value);

StatusType clientStatusType();

QString clientStatusText();
void setClientStatusText(const QString &value);
Expand Down Expand Up @@ -94,28 +103,31 @@ class QmlQXmppClient : public QObject

void messageReceived(QmlQXmppMessage* message);

// This signal is emitted when client presence type changes.
// This signal is emitted when presence stanza received
void presenceReceived(QmlQXmppPresence *presence);

// This signal is emitted when client status type changes.
void statusTypeChanged(const QString &type);
void clientStatusTypeChanged();

// This signal is emitted when client status text changes.
void statusTextChanged(const QString &text);
void clientStatusTextChanged();

public slots:
// connect using QXmppConfiguration params
void connectUsingConfiguration();
void connectToServer(const QString &jid, const QString &password);
void disconnectFromServer();
void sendMessage(const QString& bareJid, QVariantMap map);
void setClientPresence(QVariantMap map);

private slots:
void onMessageReceived(const QXmppMessage& message);
void onPresenceReceived(const QXmppPresence &presence);

private:
void connectSignals();
QXmppPresence::AvailableStatusType intToAvailableStatusType(int value);
StatusType availableStatusTypeToStatusType(QXmppPresence::AvailableStatusType type);

private:
QXmppClient _client;
Expand Down
105 changes: 72 additions & 33 deletions qxmpp-declarative/src/QmlQXmppClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <QtDeclarative/QDeclarativeEngine>

#include <QXmppUtils.h>
#include <QXmppMessage.h>
#include <QXmppPresence.h>
#include <QXmppArchiveManager.h>
Expand Down Expand Up @@ -96,46 +97,16 @@ QmlQXmppVCardManager *QmlQXmppClient::vcardManager()
return this->_vcardManagerWrapper;
}

QString QmlQXmppClient::clientStatusType()
QmlQXmppClient::StatusType QmlQXmppClient::clientStatusType()
{
if (this->_client.clientPresence().type() == QXmppPresence::Available)
return QmlQXmppPresence::statusToString(this->_client.clientPresence().availableStatusType());

return "offline";
}

void QmlQXmppClient::setClientStatusType(const QString &value)
{
if (value != this->clientStatusType()) {
QXmppPresence presence = this->_client.clientPresence();
if (value == "offline") {
presence.setType(QXmppPresence::Unavailable);
} else {
presence.setType(QXmppPresence::Available);
presence.setAvailableStatusType(QmlQXmppPresence::stringToStatus(value));
}
this->_client.setClientPresence(presence);

emit this->statusTypeChanged(value);
}
return this->availableStatusTypeToStatusType(this->_client.clientPresence().availableStatusType());
}

QString QmlQXmppClient::clientStatusText()
{
return this->_client.clientPresence().statusText();
}

void QmlQXmppClient::setClientStatusText(const QString &value)
{
if (value != this->clientStatusText()) {
QXmppPresence presence = this->_client.clientPresence();
presence.setStatusText(value);
this->_client.setClientPresence(presence);

emit this->statusTextChanged(value);
}
}

void QmlQXmppClient::connectUsingConfiguration()
{
this->_client.connectToServer(this->_configuration);
Expand Down Expand Up @@ -179,6 +150,31 @@ void QmlQXmppClient::sendMessage(const QString& bareJid, QVariantMap map)
}
}

void QmlQXmppClient::setClientPresence(QVariantMap map)
{
bool anythingChanged = false;
QXmppPresence presence = this->_client.clientPresence();

if (map.contains(QString("type"))) {
QXmppPresence::AvailableStatusType newType = intToAvailableStatusType(map["type"].toInt());
if (newType != presence.availableStatusType()) {
presence.setAvailableStatusType(newType);
anythingChanged = true;
}
}

if (map.contains(QString("text"))) {
QString newStatus = map["text"].toString();
if (newStatus != presence.statusText()) {
presence.setStatusText(newStatus);
anythingChanged = true;
}
}

if (anythingChanged)
this->_client.setClientPresence(presence);
}

void QmlQXmppClient::onMessageReceived(const QXmppMessage& message)
{
QmlQXmppMessage qmlmessage(message);
Expand All @@ -187,6 +183,11 @@ void QmlQXmppClient::onMessageReceived(const QXmppMessage& message)

void QmlQXmppClient::onPresenceReceived(const QXmppPresence &presence)
{
if (QXmppUtils::jidToBareJid(presence.from()) == this->_client.configuration().jidBare()) {
emit this->clientStatusTypeChanged();
emit this->clientStatusTextChanged();
}

QmlQXmppPresence presenceWrapper(presence);
emit this->presenceReceived(&presenceWrapper);
}
Expand All @@ -198,4 +199,42 @@ void QmlQXmppClient::connectSignals()
SIGNAL_CONNECT_CHECK(connect(&this->_client, SIGNAL(disconnected()), this, SIGNAL(disconnected())));
SIGNAL_CONNECT_CHECK(connect(&this->_client, SIGNAL(messageReceived(const QXmppMessage &)), this, SLOT(onMessageReceived(const QXmppMessage &))));
SIGNAL_CONNECT_CHECK(connect(&this->_client, SIGNAL(presenceReceived(const QXmppPresence &)), this, SLOT(onPresenceReceived(const QXmppPresence &))));
}
}

QXmppPresence::AvailableStatusType QmlQXmppClient::intToAvailableStatusType(int value)
{
switch (value) {
case QmlQXmppClient::Online:
return QXmppPresence::Online;
case QmlQXmppClient::Away:
return QXmppPresence::Away;
case QmlQXmppClient::XA:
return QXmppPresence::XA;
case QmlQXmppClient::DND:
return QXmppPresence::DND;
case QmlQXmppClient::Chat:
return QXmppPresence::Chat;
case QmlQXmppClient::Invisible:
return QXmppPresence::Invisible;
}
return QXmppPresence::Online;
}

QmlQXmppClient::StatusType QmlQXmppClient::availableStatusTypeToStatusType(QXmppPresence::AvailableStatusType type)
{
switch (type) {
case QXmppPresence::Online:
return QmlQXmppClient::Online;
case QXmppPresence::Away:
return QmlQXmppClient::Away;
case QXmppPresence::XA:
return QmlQXmppClient::XA;
case QXmppPresence::DND:
return QmlQXmppClient::DND;
case QXmppPresence::Chat:
return QmlQXmppClient::Chat;
case QXmppPresence::Invisible:
return QmlQXmppClient::Invisible;
}
return QmlQXmppClient::Online;
}

0 comments on commit de23e40

Please sign in to comment.