Add chat mention notifications

This commit is contained in:
Stojce Slavkovski 2014-04-26 22:19:33 +02:00
parent 91d7bd252d
commit 5c7fb3eca5
8 changed files with 45 additions and 27 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -209,6 +209,9 @@ Menu::Menu() :
toggleChat(); toggleChat();
connect(&xmppClient, SIGNAL(connected()), this, SLOT(toggleChat())); connect(&xmppClient, SIGNAL(connected()), this, SLOT(toggleChat()));
connect(&xmppClient, SIGNAL(disconnected()), this, SLOT(toggleChat())); connect(&xmppClient, SIGNAL(disconnected()), this, SLOT(toggleChat()));
// init chat window to listen chat
_chatWindow = new ChatWindow(Application::getInstance()->getWindow());
#endif #endif
QMenu* viewMenu = addMenu("View"); QMenu* viewMenu = addMenu("View");

View file

@ -16,19 +16,15 @@
#include "XmppClient.h" #include "XmppClient.h"
const QString DEFAULT_XMPP_SERVER = "chat.highfidelity.io"; const QString DEFAULT_XMPP_SERVER = "chat.highfidelity.io";
const QString DEFAULT_CHAT_ROOM = "public@public-chat.highfidelity.io"; const QString DEFAULT_CHAT_ROOM = "test@public-chat.highfidelity.io";
XmppClient::XmppClient() : XmppClient::XmppClient() :
_xmppClient(), _xmppClient(),
_xmppMUCManager(), _xmppMUCManager()
_archiveManager()
{ {
AccountManager& accountManager = AccountManager::getInstance(); AccountManager& accountManager = AccountManager::getInstance();
connect(&accountManager, SIGNAL(accessTokenChanged()), this, SLOT(connectToServer())); connect(&accountManager, SIGNAL(accessTokenChanged()), this, SLOT(connectToServer()));
connect(&accountManager, SIGNAL(logoutComplete()), this, SLOT(disconnectFromServer())); connect(&accountManager, SIGNAL(logoutComplete()), this, SLOT(disconnectFromServer()));
_archiveManager = new QXmppArchiveManager;
_xmppClient.addExtension(_archiveManager);
} }
XmppClient& XmppClient::getInstance() { XmppClient& XmppClient::getInstance() {

View file

@ -17,7 +17,6 @@
#include <QObject> #include <QObject>
#include <QXmppClient.h> #include <QXmppClient.h>
#include <QXmppMucManager.h> #include <QXmppMucManager.h>
#include "QXmppArchiveManager.h"
/// Generalized threaded processor for handling received inbound packets. /// Generalized threaded processor for handling received inbound packets.
class XmppClient : public QObject { class XmppClient : public QObject {
@ -28,7 +27,6 @@ public:
QXmppClient& getXMPPClient() { return _xmppClient; } QXmppClient& getXMPPClient() { return _xmppClient; }
const QXmppMucRoom* getPublicChatRoom() const { return _publicChatRoom; } const QXmppMucRoom* getPublicChatRoom() const { return _publicChatRoom; }
QXmppArchiveManager* getArchiveManager() const { return _archiveManager; }
private slots: private slots:
void xmppConnected(); void xmppConnected();
@ -45,7 +43,6 @@ private:
QXmppClient _xmppClient; QXmppClient _xmppClient;
QXmppMucManager _xmppMUCManager; QXmppMucManager _xmppMUCManager;
QXmppMucRoom* _publicChatRoom; QXmppMucRoom* _publicChatRoom;
QXmppArchiveManager* _archiveManager;
}; };
#endif // __interface__XmppClient__ #endif // __interface__XmppClient__

View file

@ -26,6 +26,8 @@
#include "ChatWindow.h" #include "ChatWindow.h"
#include <QMessageBox>
const int NUM_MESSAGES_TO_TIME_STAMP = 20; const int NUM_MESSAGES_TO_TIME_STAMP = 20;
const QRegularExpression regexLinks("((?:(?:ftp)|(?:https?))://\\S+)"); const QRegularExpression regexLinks("((?:(?:ftp)|(?:https?))://\\S+)");
@ -36,7 +38,9 @@ ChatWindow::ChatWindow(QWidget* parent) :
ui(new Ui::ChatWindow), ui(new Ui::ChatWindow),
numMessagesAfterLastTimeStamp(0), numMessagesAfterLastTimeStamp(0),
_mousePressed(false), _mousePressed(false),
_mouseStartPosition() _mouseStartPosition(),
_trayIcon(parent),
_effectPlayer()
{ {
setAttribute(Qt::WA_DeleteOnClose, false); setAttribute(Qt::WA_DeleteOnClose, false);
@ -80,8 +84,21 @@ ChatWindow::ChatWindow(QWidget* parent) :
connect(&xmppClient, SIGNAL(connected()), this, SLOT(connected())); connect(&xmppClient, SIGNAL(connected()), this, SLOT(connected()));
} }
connect(&xmppClient, SIGNAL(messageReceived(QXmppMessage)), this, SLOT(messageReceived(QXmppMessage))); connect(&xmppClient, SIGNAL(messageReceived(QXmppMessage)), this, SLOT(messageReceived(QXmppMessage)));
connect(&_trayIcon, SIGNAL(messageClicked()), this, SLOT(notificationClicked()));
#endif #endif
QDir mentionSoundsDir(Application::resourcesPath() + "/sounds/mention/");
_mentionSounds = mentionSoundsDir.entryList(QDir::Files);
}
void ChatWindow::notificationClicked() {
if (parentWidget()->isMinimized()) {
parentWidget()->showNormal();
}
if (isHidden()) {
show();
}
scrollToBottom();
} }
ChatWindow::~ChatWindow() { ChatWindow::~ChatWindow() {
@ -205,17 +222,6 @@ void ChatWindow::connected() {
const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom(); const QXmppMucRoom* publicChatRoom = XmppClient::getInstance().getPublicChatRoom();
connect(publicChatRoom, SIGNAL(participantsChanged()), this, SLOT(participantsChanged())); connect(publicChatRoom, SIGNAL(participantsChanged()), this, SLOT(participantsChanged()));
// set limits
QDateTime m_startDate = QDateTime::currentDateTime().addDays(-2);
QDateTime m_endDate = QDateTime::currentDateTime();
QXmppResultSetQuery rsmQuery;
rsmQuery.setMax(100);
QXmppArchiveManager* archiveManager = XmppClient::getInstance().getArchiveManager();
archiveManager->listCollections("", m_startDate, m_endDate, rsmQuery);
#endif #endif
startTimerForTimeStamps(); startTimerForTimeStamps();
} }
@ -317,6 +323,21 @@ void ChatWindow::messageReceived(const QXmppMessage& message) {
} else { } else {
lastMessageStamp = QDateTime::currentDateTime(); lastMessageStamp = QDateTime::currentDateTime();
} }
QRegularExpression usernameMention("@(\\b" + AccountManager::getInstance().getUsername() + "\\b)");
qDebug() << "message: " << message.body();
if (isHidden() && message.body().contains(usernameMention)) {
if (_effectPlayer.state() != QMediaPlayer::PlayingState) {
// get random sound
QFileInfo inf = QFileInfo(Application::resourcesPath() + "/sounds/mention/" + _mentionSounds.at(rand() % _mentionSounds.size()));
_effectPlayer.setMedia(QUrl::fromLocalFile(inf.absoluteFilePath()));
_effectPlayer.play();
}
_trayIcon.show();
_trayIcon.showMessage(windowTitle(), message.body());
}
} }
#endif #endif

View file

@ -14,6 +14,8 @@
#include <QDateTime> #include <QDateTime>
#include <QDockWidget> #include <QDockWidget>
#include <QMediaPlayer>
#include <QSystemTrayIcon.h>
#include <QTimer> #include <QTimer>
#include <Application.h> #include <Application.h>
@ -24,10 +26,6 @@
#include <QXmppClient.h> #include <QXmppClient.h>
#include <QXmppMessage.h> #include <QXmppMessage.h>
class QXmppArchiveChat;
class QXmppArchiveManager;
class QXmppResultSetReply;
#endif #endif
namespace Ui { namespace Ui {
@ -67,6 +65,9 @@ private:
QDateTime lastMessageStamp; QDateTime lastMessageStamp;
bool _mousePressed; bool _mousePressed;
QPoint _mouseStartPosition; QPoint _mouseStartPosition;
QSystemTrayIcon _trayIcon;
QStringList _mentionSounds;
QMediaPlayer _effectPlayer;
private slots: private slots:
void connected(); void connected();
@ -75,7 +76,7 @@ private slots:
void error(QXmppClient::Error error); void error(QXmppClient::Error error);
void participantsChanged(); void participantsChanged();
void messageReceived(const QXmppMessage& message); void messageReceived(const QXmppMessage& message);
void notificationClicked();
#endif #endif
}; };