mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 06:44:06 +02:00
commit
e69e90b5b4
6 changed files with 255 additions and 2 deletions
57
examples/globalServicesExample.js
Normal file
57
examples/globalServicesExample.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
//
|
||||
// globalServicesExample.js
|
||||
// examples
|
||||
//
|
||||
// Created by Thijs Wenker on 9/12/14.
|
||||
// Copyright 2014 High Fidelity, Inc.
|
||||
//
|
||||
// Example usage of the GlobalServices object. You could use it to make your own chatbox.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
function onConnected() {
|
||||
if (GlobalServices.onlineUsers.length > 0) {
|
||||
sendMessageForm()
|
||||
return;
|
||||
}
|
||||
Script.setTimeout(function() { sendMessageForm(); }, 5000);
|
||||
}
|
||||
|
||||
function onDisconnected(reason) {
|
||||
switch(reason) {
|
||||
case "logout":
|
||||
Window.alert("logged out!");
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function onOnlineUsersChanged(users) {
|
||||
print(users);
|
||||
}
|
||||
|
||||
function onIncommingMessage(user, message) {
|
||||
print(user + ": " + message);
|
||||
if (message === "hello") {
|
||||
GlobalServices.chat("hello, @" + user + "!");
|
||||
}
|
||||
}
|
||||
|
||||
function sendMessageForm() {
|
||||
var form =
|
||||
[
|
||||
{ label: "To:", options: ["(noone)"].concat(GlobalServices.onlineUsers) },
|
||||
{ label: "Message:", value: "Enter message here" }
|
||||
];
|
||||
if (Window.form("Send message on public chat", form)) {
|
||||
GlobalServices.chat(form[0].value == "(noone)" ? form[1].value : "@" + form[0].value + ", " + form[1].value);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
GlobalServices.connected.connect(onConnected);
|
||||
GlobalServices.disconnected.connect(onDisconnected);
|
||||
GlobalServices.onlineUsersChanged.connect(onOnlineUsersChanged);
|
||||
GlobalServices.incomingMessage.connect(onIncommingMessage);
|
|
@ -81,6 +81,7 @@
|
|||
#include "scripting/AccountScriptingInterface.h"
|
||||
#include "scripting/AudioDeviceScriptingInterface.h"
|
||||
#include "scripting/ClipboardScriptingInterface.h"
|
||||
#include "scripting/GlobalServicesScriptingInterface.h"
|
||||
#include "scripting/LocationScriptingInterface.h"
|
||||
#include "scripting/MenuScriptingInterface.h"
|
||||
#include "scripting/SettingsScriptingInterface.h"
|
||||
|
@ -3827,9 +3828,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
|
||||
|
|
113
interface/src/scripting/GlobalServicesScriptingInterface.cpp
Normal file
113
interface/src/scripting/GlobalServicesScriptingInterface.cpp
Normal file
|
@ -0,0 +1,113 @@
|
|||
//
|
||||
// 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);
|
||||
connect(&accountManager, &AccountManager::logoutComplete, this, &GlobalServicesScriptingInterface::loggedOut);
|
||||
#ifdef HAVE_QXMPP
|
||||
const XmppClient& xmppClient = XmppClient::getInstance();
|
||||
connect(&xmppClient, &XmppClient::joinedPublicChatRoom, this, &GlobalServicesScriptingInterface::connected);
|
||||
connect(&xmppClient, &XmppClient::joinedPublicChatRoom, this, &GlobalServicesScriptingInterface::onConnected);
|
||||
const QXmppClient& qxmppClient = XmppClient::getInstance().getXMPPClient();
|
||||
connect(&qxmppClient, &QXmppClient::messageReceived, this, &GlobalServicesScriptingInterface::messageReceived);
|
||||
#endif // HAVE_QXMPP
|
||||
}
|
||||
|
||||
GlobalServicesScriptingInterface::~GlobalServicesScriptingInterface() {
|
||||
AccountManager& accountManager = AccountManager::getInstance();
|
||||
disconnect(&accountManager, &AccountManager::usernameChanged, this, &GlobalServicesScriptingInterface::myUsernameChanged);
|
||||
disconnect(&accountManager, &AccountManager::logoutComplete, this, &GlobalServicesScriptingInterface::loggedOut);
|
||||
#ifdef HAVE_QXMPP
|
||||
const XmppClient& xmppClient = XmppClient::getInstance();
|
||||
disconnect(&xmppClient, &XmppClient::joinedPublicChatRoom, this, &GlobalServicesScriptingInterface::connected);
|
||||
disconnect(&xmppClient, &XmppClient::joinedPublicChatRoom, this, &GlobalServicesScriptingInterface::onConnected);
|
||||
const QXmppClient& qxmppClient = XmppClient::getInstance().getXMPPClient();
|
||||
disconnect(&qxmppClient, &QXmppClient::messageReceived, this, &GlobalServicesScriptingInterface::messageReceived);
|
||||
const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom();
|
||||
disconnect(publicChatRoom, &QXmppMucRoom::participantsChanged, this, &GlobalServicesScriptingInterface::participantsChanged);
|
||||
#endif // HAVE_QXMPP
|
||||
}
|
||||
|
||||
void GlobalServicesScriptingInterface::onConnected() {
|
||||
#ifdef HAVE_QXMPP
|
||||
const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom();
|
||||
connect(publicChatRoom, &QXmppMucRoom::participantsChanged, this, &GlobalServicesScriptingInterface::participantsChanged, Qt::UniqueConnection);
|
||||
#endif // HAVE_QXMPP
|
||||
}
|
||||
|
||||
void GlobalServicesScriptingInterface::participantsChanged() {
|
||||
#ifdef HAVE_QXMPP
|
||||
emit GlobalServicesScriptingInterface::onlineUsersChanged(this->getOnlineUsers());
|
||||
#endif // HAVE_QXMPP
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void GlobalServicesScriptingInterface::loggedOut() {
|
||||
emit GlobalServicesScriptingInterface::disconnected(QString("logout"));
|
||||
}
|
||||
|
||||
void GlobalServicesScriptingInterface::messageReceived(const QXmppMessage& message) {
|
||||
if (message.type() != QXmppMessage::GroupChat) {
|
||||
return;
|
||||
}
|
||||
const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom();
|
||||
emit GlobalServicesScriptingInterface::incomingMessage(message.from().right(message.from().count() - 1 - publicChatRoom->jid().count()), message.body());
|
||||
}
|
60
interface/src/scripting/GlobalServicesScriptingInterface.h
Normal file
60
interface/src/scripting/GlobalServicesScriptingInterface.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
//
|
||||
// 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 <QObject>
|
||||
#include <QScriptContext>
|
||||
#include <QScriptEngine>
|
||||
#include <QScriptValue>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
#ifdef HAVE_QXMPP
|
||||
|
||||
#include <QXmppClient.h>
|
||||
#include <QXmppMessage.h>
|
||||
|
||||
#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();
|
||||
~GlobalServicesScriptingInterface();
|
||||
public:
|
||||
static GlobalServicesScriptingInterface* getInstance();
|
||||
|
||||
bool isConnected();
|
||||
QString getMyUsername();
|
||||
QStringList getOnlineUsers();
|
||||
|
||||
public slots:
|
||||
QScriptValue chat(const QString& message);
|
||||
|
||||
private slots:
|
||||
void loggedOut();
|
||||
void onConnected();
|
||||
void participantsChanged();
|
||||
void messageReceived(const QXmppMessage& 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
|
|
@ -240,6 +240,7 @@ QScriptValue WindowScriptingInterface::doGetNonBlockingFormResult(QScriptValue a
|
|||
if (_formResult == QDialog::Accepted) {
|
||||
int e = -1;
|
||||
int d = -1;
|
||||
int c = -1;
|
||||
for (int i = 0; i < _form.property("length").toInt32(); ++i) {
|
||||
QScriptValue item = _form.property(i);
|
||||
QScriptValue value = item.property("value");
|
||||
|
@ -255,6 +256,10 @@ QScriptValue WindowScriptingInterface::doGetNonBlockingFormResult(QScriptValue a
|
|||
value = _directories.at(d)->property("path").toString();
|
||||
item.setProperty("directory", value);
|
||||
_form.setProperty(i, item);
|
||||
} else if (item.property("options").isArray()) {
|
||||
c += 1;
|
||||
item.setProperty("value", _combos.at(c)->currentText());
|
||||
_form.setProperty(i, item);
|
||||
} else {
|
||||
e += 1;
|
||||
bool ok = true;
|
||||
|
@ -308,6 +313,7 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal
|
|||
if (result == QDialog::Accepted) {
|
||||
int e = -1;
|
||||
int d = -1;
|
||||
int c = -1;
|
||||
for (int i = 0; i < form.property("length").toInt32(); ++i) {
|
||||
QScriptValue item = form.property(i);
|
||||
QScriptValue value = item.property("value");
|
||||
|
@ -323,6 +329,10 @@ QScriptValue WindowScriptingInterface::showForm(const QString& title, QScriptVal
|
|||
value = _directories.at(d)->property("path").toString();
|
||||
item.setProperty("directory", value);
|
||||
form.setProperty(i, item);
|
||||
} else if (item.property("options").isArray()) {
|
||||
c += 1;
|
||||
item.setProperty("value", _combos.at(c)->currentText());
|
||||
_form.setProperty(i, item);
|
||||
} else {
|
||||
e += 1;
|
||||
bool ok = true;
|
||||
|
@ -425,6 +435,15 @@ QDialog* WindowScriptingInterface::createForm(const QString& title, QScriptValue
|
|||
|
||||
} else if (item.property("type").toString() == "header") {
|
||||
formLayout->addRow(new QLabel(item.property("label").toString()));
|
||||
} else if (item.property("options").isArray()) {
|
||||
QComboBox* combo = new QComboBox();
|
||||
combo->setMinimumWidth(200);
|
||||
QStringList options = item.property("options").toVariant().toStringList();
|
||||
for (QStringList::const_iterator it = options.begin(); it != options.end(); it += 1) {
|
||||
combo->addItem(*it);
|
||||
}
|
||||
_combos.push_back(combo);
|
||||
formLayout->addRow(new QLabel(item.property("label").toString()), combo);
|
||||
} else {
|
||||
QLineEdit* edit = new QLineEdit(item.property("value").toString());
|
||||
edit->setMinimumWidth(200);
|
||||
|
|
|
@ -80,6 +80,7 @@ private:
|
|||
QScriptValue _form;
|
||||
bool _nonBlockingFormActive;
|
||||
int _formResult;
|
||||
QVector<QComboBox*> _combos;
|
||||
QVector<QLineEdit*> _edits;
|
||||
QVector<QPushButton*> _directories;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue