Skip to content

Commit

Permalink
send typing status to client
Browse files Browse the repository at this point in the history
  • Loading branch information
shanet committed Aug 25, 2013
1 parent 1c2f0be commit db77e5b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
4 changes: 4 additions & 0 deletions cryptully/network/client.py
Expand Up @@ -41,6 +41,10 @@ def sendChatMessage(self, text):
self.sendMessage(constants.COMMAND_MSG, text)


def sendTypingMessage(self, status):
self.sendMessage(constants.COMMAND_TYPING, str(status))


def sendMessage(self, command, payload=None):
message = Message(clientCommand=command, destNick=self.remoteNick)

Expand Down
23 changes: 23 additions & 0 deletions cryptully/qt/qChatWidget.py
@@ -1,6 +1,7 @@
import re

from PyQt4.QtCore import Qt
from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QFontMetrics
from PyQt4.QtGui import QHBoxLayout
from PyQt4.QtGui import QLabel
Expand Down Expand Up @@ -58,16 +59,34 @@ def __init__(self, connectionManager, parent=None):
hbox.addWidget(splitter)
self.setLayout(hbox)

self.typingTimer = QTimer()
self.typingTimer.setSingleShot(True)
self.typingTimer.timeout.connect(self.stoppedTyping)


def chatInputTextChanged(self):
if str(self.chatInput.toPlainText())[-1:] == '\n':
self.sendMessage()
else:
# Start a timer to check for the user stopping typing
self.typingTimer.start(constants.TYPING_TIMEOUT)
self.sendTypingStatus(constants.TYPING_START)


def stoppedTyping(self):
self.typingTimer.stop()
if str(self.chatInput.toPlainText()) == '':
self.sendTypingStatus(constants.TYPING_STOP_WITHOUT_TEXT)
else:
self.sendTypingStatus(constants.TYPING_STOP_WITH_TEXT)


def sendMessage(self):
if self.isDisabled:
return

self.typingTimer.stop()

text = str(self.chatInput.toPlainText())[:-1]

# Don't send empty messages
Expand All @@ -86,6 +105,10 @@ def sendMessage(self):
self.appendMessage(text, constants.SENDER)


def sendTypingStatus(self, status):
self.connectionManager.getClient(self.nick).sendTypingMessage(status)


def showNowChattingMessage(self, nick):
self.nick = nick
self.appendMessage("You are now securely chatting with " + self.nick + " :)",
Expand Down
40 changes: 27 additions & 13 deletions cryptully/qt/qChatWindow.py
Expand Up @@ -56,6 +56,7 @@ def __init__(self, restartCallback, connectionManager=None, messageQueue=None):
self.chatTabs.tabCloseRequested.connect(self.closeTab)
self.chatTabs.currentChanged.connect(self.tabChanged)

self.statusBar = self.statusBar()
self.systemTrayIcon = QSystemTrayIcon(self)
self.systemTrayIcon.setVisible(True)

Expand Down Expand Up @@ -209,20 +210,33 @@ def postMessage(self, command, sourceNick, payload):

@pyqtSlot(str, str, str)
def sendMessageToTab(self, command, sourceNick, payload):
self.getTabByNick(sourceNick)[0].appendMessage(payload, constants.RECEIVER)

# Update the unread message count if the message is not intended for the currently selected tab
# If a typing command, update the typing status in the tab, otherwise
# show the message in the tab
tab, tabIndex = self.getTabByNick(sourceNick)
if tabIndex != self.chatTabs.currentIndex():
tab.unreadCount += 1
self.chatTabs.setTabText(tabIndex, tab.nick + (" (%d)" % tab.unreadCount))

# Show a system notifcation of the new message if not the current window or tab or the
# scrollbar of the tab isn't at the bottom
chatLogScrollbar = tab.widgetStack.widget(2).chatLog.verticalScrollBar()
if not self.isActiveWindow() or tabIndex != self.chatTabs.currentIndex() or \
chatLogScrollbar.value() != chatLogScrollbar.maximum():
qtUtils.showDesktopNotification(self.systemTrayIcon, sourceNick, payload)
if command == constants.COMMAND_TYPING:
# Show the typing status in the status bar if the tab is the selected tab
if tabIndex == self.chatTabs.currentIndex():
payload = int(payload)
if payload == constants.TYPING_START:
self.statusBar.showMessage("%s is typing" % sourceNick)
elif payload == constants.TYPING_STOP_WITHOUT_TEXT:
self.statusBar.showMessage('')
elif payload == constants.TYPING_STOP_WITH_TEXT:
self.statusBar.showMessage("%s has entered text" % sourceNick)
else:
tab.appendMessage(payload, constants.RECEIVER)

# Update the unread message count if the message is not intended for the currently selected tab
if tabIndex != self.chatTabs.currentIndex():
tab.unreadCount += 1
self.chatTabs.setTabText(tabIndex, tab.nick + (" (%d)" % tab.unreadCount))

# Show a system notifcation of the new message if not the current window or tab or the
# scrollbar of the tab isn't at the bottom
chatLogScrollbar = tab.widgetStack.widget(2).chatLog.verticalScrollBar()
if not self.isActiveWindow() or tabIndex != self.chatTabs.currentIndex() or \
chatLogScrollbar.value() != chatLogScrollbar.maximum():
qtUtils.showDesktopNotification(self.systemTrayIcon, sourceNick, payload)


@pyqtSlot(int)
Expand Down
10 changes: 9 additions & 1 deletion cryptully/utils/constants.py
Expand Up @@ -5,6 +5,8 @@

DEFAULT_AES_MODE = 'aes_256_cbc'

TYPING_TIMEOUT = 1500

# Protocol commands

# Server commands
Expand All @@ -25,15 +27,21 @@

# Loop commands
COMMAND_MSG = "MSG"
COMMAND_TYPING = "TYPING"
COMMAND_END = "END"
COMMAND_ERR = "ERR"
LOOP_COMMANDS = [COMMAND_MSG, COMMAND_END, COMMAND_ERR]
LOOP_COMMANDS = [COMMAND_MSG, COMMAND_TYPING, COMMAND_END, COMMAND_ERR]

# Message sources
SENDER = 0
RECEIVER = 1
SERVICE = 2

# Typing statuses
TYPING_START = 0
TYPING_STOP_WITHOUT_TEXT = 1
TYPING_STOP_WITH_TEXT = 2

# QT UI custom button codes
BUTTON_OKAY = 0
BUTTON_CANCEL = 1
Expand Down

0 comments on commit db77e5b

Please sign in to comment.