some todo implemented. profile filtering added, support of action messages added

This commit is contained in:
ingvar1995 2016-02-28 18:25:18 +03:00
parent 7329f5f46c
commit d7f1f15b04
3 changed files with 52 additions and 28 deletions

View file

@ -52,11 +52,10 @@ def main():
if not _login.t: if not _login.t:
return return
elif _login.t == 1: # create new profile elif _login.t == 1: # create new profile
# TODO: test
name = _login.name if _login.name else 'toxygen_user' name = _login.name if _login.name else 'toxygen_user'
tox = tox_factory() tox = tox_factory()
tox.self_set_name('Toxygen User') tox.self_set_name(_login.name if _login.name else 'Toxygen User')
tox.self_set_status('Toxing on Toxygen') tox.self_set_status_message('Toxing on Toxygen')
ProfileHelper.save_profile(tox.get_savedata(), name) ProfileHelper.save_profile(tox.get_savedata(), name)
else: # load existing profile else: # load existing profile
path, name = _login.get_data() path, name = _login.get_data()
@ -71,7 +70,6 @@ def main():
tox = tox_factory(data, settings) tox = tox_factory(data, settings)
ms = MainWindow(tox) ms = MainWindow(tox)
#ms.setup_info_from_tox()
ms.show() ms.show()
# bootstrap # bootstrap
for data in node_generator(): for data in node_generator():

View file

@ -10,10 +10,11 @@ from util import curr_time
class MessageItem(QtGui.QListWidget): class MessageItem(QtGui.QListWidget):
def __init__(self, text, time, user='', parent=None): def __init__(self, text, time, user='', message_type=TOX_MESSAGE_TYPE['NORMAL'], parent=None):
QtGui.QListWidget.__init__(self, parent) QtGui.QListWidget.__init__(self, parent)
self.name = QtGui.QLabel(self) self.name = QtGui.QLabel(self)
self.name.setGeometry(QtCore.QRect(0, 0, 50, 25)) self.name.setGeometry(QtCore.QRect(0, 0, 50, 25))
self.name.setTextFormat(QtCore.Qt.PlainText)
font = QtGui.QFont() font = QtGui.QFont()
font.setFamily("Times New Roman") font.setFamily("Times New Roman")
font.setPointSize(12) font.setPointSize(12)
@ -37,6 +38,10 @@ class MessageItem(QtGui.QListWidget):
self.message.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse) self.message.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.LinksAccessibleByMouse)
self.message.setPlainText(text) self.message.setPlainText(text)
if message_type == TOX_MESSAGE_TYPE['ACTION']:
self.name.setStyleSheet("QLabel { color: blue; }")
self.message.setStyleSheet("QPlainTextEdit { color: blue; }")
class ContactItem(QtGui.QListWidget): class ContactItem(QtGui.QListWidget):
@ -323,7 +328,6 @@ class MainWindow(QtGui.QMainWindow):
# self.connection_status.repaint() # self.connection_status.repaint()
def setup_info_from_tox(self): def setup_info_from_tox(self):
# TODO: remove - use Profile()
self.name.setText(self.profile.name) self.name.setText(self.profile.name)
self.status_message.setText(self.profile.status_message) self.status_message.setText(self.profile.status_message)
@ -379,9 +383,9 @@ class MainWindow(QtGui.QMainWindow):
print 'row:', index.row() print 'row:', index.row()
num = index.row() num = index.row()
self.profile.setActive(num) self.profile.setActive(num)
friend = self.profile.friends[num] friend = self.profile.getActiveFriendData()
self.account_name.setText(friend.name) self.account_name.setText(friend[0])
self.account_status.setText(friend.status_message) self.account_status.setText(friend[1])
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -5,7 +5,7 @@ import os
from tox import Tox from tox import Tox
from toxcore_enums_and_consts import * from toxcore_enums_and_consts import *
from ctypes import * from ctypes import *
from util import curr_time from util import curr_time, log
class ProfileHelper(object): class ProfileHelper(object):
@ -56,9 +56,8 @@ class Contact(object):
number - unique number of friend in list, widget - widget for update number - unique number of friend in list, widget - widget for update
""" """
def __init__(self, name, status_message, number, widget): def __init__(self, name, status_message, widget):
# TODO: remove number self._name, self._status_message= name, status_message
self._name, self._status_message, self._number = name, status_message, number
self._status, self._widget = None, widget self._status, self._widget = None, widget
widget.name.setText(name) widget.name.setText(name)
widget.status_message.setText(status_message) widget.status_message.setText(status_message)
@ -96,12 +95,20 @@ class Friend(Contact):
Friend in list of friends. Can be hidden, unread messages added Friend in list of friends. Can be hidden, unread messages added
""" """
def __init__(self, *args): def __init__(self, number, *args):
super(Friend, self).__init__(*args) super(Friend, self).__init__(*args)
self._number = number
self._new_messages = False self._new_messages = False
self._visible = True
def getVisibility(self):
return self._visible
def setVisibility(self, value): def setVisibility(self, value):
self._widget.setVisibility(value) self._widget.setVisibility(value)
self._visible = value
visibility = property(getVisibility, setVisibility)
def setMessages(self, value): def setMessages(self, value):
self._new_messages = value self._new_messages = value
@ -119,8 +126,7 @@ class Profile(Contact):
""" """
Profile of current toxygen user. Contains friends list, tox instance Profile of current toxygen user. Contains friends list, tox instance
""" """
# TODO: add slices # TODO: add unicode support in messages
# TODO: add unicode support
def __init__(self, tox, widgets, widget, messages_list): def __init__(self, tox, widgets, widget, messages_list):
self._widget = widget self._widget = widget
self._messages = messages_list self._messages = messages_list
@ -131,9 +137,9 @@ class Profile(Contact):
data = tox.self_get_friend_list() data = tox.self_get_friend_list()
self.friends, num, self._active_friend = [], 0, -1 self.friends, num, self._active_friend = [], 0, -1
for i in data: for i in data:
name = tox.friend_get_name(i) or 'Tox user' # tox.friend_get_public_key(i) name = tox.friend_get_name(i) or tox.friend_get_public_key(i)
status_message = tox.friend_get_status_message(i) status_message = tox.friend_get_status_message(i)
self.friends.append(Friend(name, status_message, i, widgets[num])) self.friends.append(Friend(i, name, status_message, widgets[num]))
num += 1 num += 1
Profile._instance = self Profile._instance = self
@ -145,16 +151,25 @@ class Profile(Contact):
return self._active_friend return self._active_friend
def setActive(self, value): def setActive(self, value):
if 0 <= value < self.tox.self_get_friend_list_size(): try:
self._active_friend = value visible_friends = filter(lambda num, friend: friend.visibility, enumerate(self.friends))
self._active_friend = visible_friends[value][0]
self._messages.clear()
# TODO: load history
except: # no friend found. ignore
log('Incorrect friend value: ' + str(value))
active_friend = property(getActive, setActive) active_friend = property(getActive, setActive)
def getActiveFriendData(self):
friend = self.friends[self._active_friend]
return friend.name, friend.status_message
def getActiveNumber(self): def getActiveNumber(self):
return self.friends[self._active_friend].getNumber() return self.friends[self._active_friend].number
def getActiveName(self): def getActiveName(self):
return self.friends[self._active_friend].getName() return self.friends[self._active_friend].name
def isActiveOnline(self): def isActiveOnline(self):
if not self._active_friend + 1: # no active friend if not self._active_friend + 1: # no active friend
@ -162,16 +177,18 @@ class Profile(Contact):
else: else:
# TODO: callbacks! # TODO: callbacks!
return True return True
status = self.friends[self._active_friend].getStatus() status = self.friends[self._active_friend].status
return status is not None return status is not None
def filtration(self, show_online=True, filter_str=''):
for friend in self.friends:
friend.visibility = (friend.status is not None or not show_online) and (filter_str in friend.name)
def newMessage(self, id, message_type, message): def newMessage(self, id, message_type, message):
# TODO: add support of action (/me) messages
if id == self._active_friend: # add message to list if id == self._active_friend: # add message to list
user_name = Profile.getInstance().getActiveName() user_name = Profile.getInstance().getActiveName()
item = mainscreen.MessageItem(message, curr_time(), user_name) item = mainscreen.MessageItem(message, curr_time(), user_name, message_type)
elem = QtGui.QListWidgetItem(self._messages) elem = QtGui.QListWidgetItem(self._messages)
print item.sizeHint()
elem.setSizeHint(QtCore.QSize(500, 100)) elem.setSizeHint(QtCore.QSize(500, 100))
self._messages.addItem(elem) self._messages.addItem(elem)
self._messages.setItemWidget(elem, item) self._messages.setItemWidget(elem, item)
@ -183,8 +200,13 @@ class Profile(Contact):
def sendMessage(self, text): def sendMessage(self, text):
if self.isActiveOnline() and text: if self.isActiveOnline() and text:
self.tox.friend_send_message(self._active_friend, TOX_MESSAGE_TYPE['NORMAL'], text) if text.startswith('/me'):
item = mainscreen.MessageItem(text, curr_time(), self._name) message_type = TOX_MESSAGE_TYPE['ACTION']
text = text[3:]
else:
message_type = TOX_MESSAGE_TYPE['NORMAL']
self.tox.friend_send_message(self._active_friend, message_type, text)
item = mainscreen.MessageItem(text, curr_time(), self._name, message_type)
elem = QtGui.QListWidgetItem(self._messages) elem = QtGui.QListWidgetItem(self._messages)
elem.setSizeHint(QtCore.QSize(500, 100)) elem.setSizeHint(QtCore.QSize(500, 100))
self._messages.addItem(elem) self._messages.addItem(elem)