From cb907c189168c0c38e40baf090829d34cb29345e Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 10 Sep 2014 13:57:38 +0200 Subject: [PATCH] GlobalServicesScriptingInterface added, functions and properties functional --- interface/src/Application.cpp | 7 +- .../GlobalServicesScriptingInterface.cpp | 67 +++++++++++++++++++ .../GlobalServicesScriptingInterface.h | 53 +++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 interface/src/scripting/GlobalServicesScriptingInterface.cpp create mode 100644 interface/src/scripting/GlobalServicesScriptingInterface.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7c7c455d1d..6facabb3e2 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -80,6 +80,7 @@ #include "scripting/AccountScriptingInterface.h" #include "scripting/AudioDeviceScriptingInterface.h" #include "scripting/ClipboardScriptingInterface.h" +#include "scripting/GlobalServicesScriptingInterface.h" #include "scripting/MenuScriptingInterface.h" #include "scripting/SettingsScriptingInterface.h" #include "scripting/WindowScriptingInterface.h" @@ -3778,9 +3779,11 @@ ScriptEngine* Application::loadScript(const QString& scriptFilename, bool isUser scriptEngine->registerGlobalObject("AudioReflector", &_audioReflector); scriptEngine->registerGlobalObject("Account", AccountScriptingInterface::getInstance()); scriptEngine->registerGlobalObject("Metavoxels", &_metavoxels); - + + scriptEngine->registerGlobalObject("GlobalServices", GlobalServicesScriptingInterface::getInstance()); + scriptEngine->registerGlobalObject("AvatarManager", &_avatarManager); - + #ifdef HAVE_RTMIDI scriptEngine->registerGlobalObject("MIDI", &MIDIManager::getInstance()); #endif diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.cpp b/interface/src/scripting/GlobalServicesScriptingInterface.cpp new file mode 100644 index 0000000000..d28e96fa45 --- /dev/null +++ b/interface/src/scripting/GlobalServicesScriptingInterface.cpp @@ -0,0 +1,67 @@ +// +// GlobalServicesScriptingInterface.cpp +// interface/src/scripting +// +// Created by Thijs Wenker on 9/10/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "AccountManager.h" +#include "XmppClient.h" + +#include "GlobalServicesScriptingInterface.h" + +GlobalServicesScriptingInterface::GlobalServicesScriptingInterface() { + AccountManager& accountManager = AccountManager::getInstance(); + connect(&accountManager, &AccountManager::usernameChanged, this, + &GlobalServicesScriptingInterface::myUsernameChanged); + +} + +GlobalServicesScriptingInterface* GlobalServicesScriptingInterface::getInstance() { + static GlobalServicesScriptingInterface sharedInstance; + return &sharedInstance; +} + +bool GlobalServicesScriptingInterface::isConnected() { +#ifdef HAVE_QXMPP + return XmppClient::getInstance().getXMPPClient().isConnected(); +#else + return false; +#endif // HAVE_QXMPP +} + +QScriptValue GlobalServicesScriptingInterface::chat(const QString& message) { +#ifdef HAVE_QXMPP + if (XmppClient::getInstance().getXMPPClient().isConnected()) { + const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom(); + QXmppMessage messageObject; + messageObject.setTo(publicChatRoom->jid()); + messageObject.setType(QXmppMessage::GroupChat); + messageObject.setBody(message); + return XmppClient::getInstance().getXMPPClient().sendPacket(messageObject); + } +#endif // HAVE_QXMPP + return false; +} + +QString GlobalServicesScriptingInterface::getMyUsername() { + return AccountManager::getInstance().getAccountInfo().getUsername(); +} + +QStringList GlobalServicesScriptingInterface::getOnlineUsers() { +#ifdef HAVE_QXMPP + if (XmppClient::getInstance().getXMPPClient().isConnected()) { + QStringList usernames; + const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom(); + foreach(const QString& participant, XmppClient::getInstance().getPublicChatRoom()->participants()) { + usernames.append(participant.right(participant.count() - 1 - publicChatRoom->jid().count())); + } + return usernames; + } +#endif // HAVE_QXMPP + return QStringList(); +} \ No newline at end of file diff --git a/interface/src/scripting/GlobalServicesScriptingInterface.h b/interface/src/scripting/GlobalServicesScriptingInterface.h new file mode 100644 index 0000000000..9ab8a26923 --- /dev/null +++ b/interface/src/scripting/GlobalServicesScriptingInterface.h @@ -0,0 +1,53 @@ +// +// GlobalServicesScriptingInterface.h +// interface/src/scripting +// +// Created by Thijs Wenker on 9/10/14. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_GlobalServicesScriptingInterface_h +#define hifi_GlobalServicesScriptingInterface_h + +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_QXMPP + +#include +#include + +#endif + +class GlobalServicesScriptingInterface : public QObject { + Q_OBJECT + Q_PROPERTY(bool isConnected READ isConnected) + Q_PROPERTY(QString myUsername READ getMyUsername) + Q_PROPERTY(QStringList onlineUsers READ getOnlineUsers) + GlobalServicesScriptingInterface(); +public: + static GlobalServicesScriptingInterface* getInstance(); + + bool isConnected(); + QString getMyUsername(); + QStringList getOnlineUsers(); + +public slots: + QScriptValue chat(const QString& message); + +signals: + void connected(); + void disconnected(const QString& reason); + void incomingMessage(const QString& username, const QString& message); + void onlineUsersChanged(const QStringList& usernames); + void myUsernameChanged(const QString& username); +}; + +#endif // hifi_GlobalServicesScriptingInterface_h