messenger refactoring

This commit is contained in:
ingvar1995 2018-08-03 21:07:18 +03:00
parent 25de4fa2ef
commit 5f56d630ce
3 changed files with 52 additions and 69 deletions

View File

@ -24,7 +24,6 @@ class ContactsManager(ToxSave):
self._active_contact_changed = Event() self._active_contact_changed = Event()
self._sorting = settings['sorting'] self._sorting = settings['sorting']
self._filter_string = '' self._filter_string = ''
self._friend_item_height = 40 if settings['compact_mode'] else 70
screen.contacts_filter.setCurrentIndex(int(self._sorting)) screen.contacts_filter.setCurrentIndex(int(self._sorting))
self._history = history self._history = history
self._load_contacts() self._load_contacts()
@ -149,6 +148,9 @@ class ContactsManager(ToxSave):
def is_active_a_group(self): def is_active_a_group(self):
return type(self.get_curr_contact()) is GroupChat return type(self.get_curr_contact()) is GroupChat
def is_active_a_group_chat_peer(self):
return type(self.get_curr_contact()) is GroupPeerContact
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Filtration # Filtration
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
@ -198,11 +200,9 @@ class ContactsManager(ToxSave):
friend.visibility = (friend.status is not None or sorting not in (1, 4)) and filtered_by_name friend.visibility = (friend.status is not None or sorting not in (1, 4)) and filtered_by_name
# show friend even if it's hidden when there any unread messages/actions # show friend even if it's hidden when there any unread messages/actions
friend.visibility = friend.visibility or friend.messages or friend.actions friend.visibility = friend.visibility or friend.messages or friend.actions
# TODO: calculate height item = self._screen.friends_list.item(index)
if friend.visibility: item_widget = self._screen.friends_list.itemWidget(item)
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, self._friend_item_height)) item.setSizeHint(QtCore.QSize(250, item_widget.height() if friend.visibility else 0))
else:
self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0))
# save soring results # save soring results
self._sorting, self._filter_string = sorting, filter_str self._sorting, self._filter_string = sorting, filter_str
self._settings['sorting'] = self._sorting self._settings['sorting'] = self._sorting
@ -436,7 +436,7 @@ class ContactsManager(ToxSave):
util.log('Accept friend request failed! ' + str(ex)) util.log('Accept friend request failed! ' + str(ex))
def can_send_typing_notification(self): def can_send_typing_notification(self):
return self._settings['typing_notifications'] and self._active_contact + 1 return self._settings['typing_notifications'] and not self.is_active_a_group_chat_peer()
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Contacts numbers update # Contacts numbers update

View File

@ -44,14 +44,28 @@ class Messenger(tox_save.ToxSave):
def send_message(self): def send_message(self):
text = self._screen.messageEdit.toPlainText() text = self._screen.messageEdit.toPlainText()
if self._contacts_manager.is_active_a_friend():
self.send_message_to_friend(text)
elif self._contacts_manager.is_active_a_group():
self.send_message_to_group(text)
else:
self.send_message_to_group_peer(text)
def send_message_to_friend(self, text, friend_number=None): plugin_command_prefix = '/plugin '
if text.startswith(plugin_command_prefix):
self._plugin_loader.command(text[len(plugin_command_prefix):])
self._screen.messageEdit.clear()
return
action_message_prefix = '/me '
if text.startswith(action_message_prefix):
message_type = TOX_MESSAGE_TYPE['ACTION']
text = text[len(action_message_prefix):]
else:
message_type = TOX_MESSAGE_TYPE['NORMAL']
if self._contacts_manager.is_active_a_friend():
self.send_message_to_friend(text, message_type)
elif self._contacts_manager.is_active_a_group():
self.send_message_to_group(text, message_type)
elif self._contacts_manager.is_active_a_group_chat_peer():
self.send_message_to_group_peer(text, message_type)
def send_message_to_friend(self, text, message_type, friend_number=None):
""" """
Send message Send message
:param text: message text :param text: message text
@ -60,19 +74,9 @@ class Messenger(tox_save.ToxSave):
if friend_number is None: if friend_number is None:
friend_number = self._contacts_manager.get_active_number() friend_number = self._contacts_manager.get_active_number()
if text.startswith('/plugin '):
self._plugin_loader.command(text[8:])
self._screen.messageEdit.clear()
return
if not text or friend_number < 0: if not text or friend_number < 0:
return return
if text.startswith('/me '):
message_type = TOX_MESSAGE_TYPE['ACTION']
text = text[4:]
else:
message_type = TOX_MESSAGE_TYPE['NORMAL']
friend = self._get_friend_by_number(friend_number) friend = self._get_friend_by_number(friend_number)
messages = self._split_message(text.encode('utf-8')) messages = self._split_message(text.encode('utf-8'))
t = util.get_unix_time() t = util.get_unix_time()
@ -108,23 +112,13 @@ class Messenger(tox_save.ToxSave):
# Messaging - groups # Messaging - groups
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def send_message_to_group(self, text, group_number=None): def send_message_to_group(self, text, message_type, group_number=None):
if group_number is None: if group_number is None:
group_number = self._contacts_manager.get_active_number() group_number = self._contacts_manager.get_active_number()
if text.startswith('/plugin '):
self._plugin_loader.command(text[8:])
self._screen.messageEdit.clear()
return
if not text or group_number < 0: if not text or group_number < 0:
return return
if text.startswith('/me '):
message_type = TOX_MESSAGE_TYPE['ACTION']
text = text[4:]
else:
message_type = TOX_MESSAGE_TYPE['NORMAL']
group = self._get_group_by_number(group_number) group = self._get_group_by_number(group_number)
messages = self._split_message(text.encode('utf-8')) messages = self._split_message(text.encode('utf-8'))
t = util.get_unix_time() t = util.get_unix_time()
@ -155,27 +149,16 @@ class Messenger(tox_save.ToxSave):
# Messaging - group peers # Messaging - group peers
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def send_message_to_group_peer(self, text, group_number=None, peer_id=None): def send_message_to_group_peer(self, text, message_type, group_number=None, peer_id=None):
if group_number is None or peer_id is None: if group_number is None or peer_id is None:
group_peer_contact = self._contacts_manager.get_curr_contact() group_peer_contact = self._contacts_manager.get_curr_contact()
peer_id = group_peer_contact.number peer_id = group_peer_contact.number
group = self._get_group_by_public_key(group_peer_contact.group_pk) group = self._get_group_by_public_key(group_peer_contact.group_pk)
group_number = group.number group_number = group.number
if text.startswith('/plugin '):
self._plugin_loader.command(text[8:])
self._screen.messageEdit.clear()
return
if not text or group_number < 0 or peer_id < 0: if not text or group_number < 0 or peer_id < 0:
return return
if text.startswith('/me '):
message_type = TOX_MESSAGE_TYPE['ACTION']
text = text[4:]
else:
message_type = TOX_MESSAGE_TYPE['NORMAL']
group_peer_contact = self._contacts_manager.get_or_create_group_peer_contact(group_number, peer_id) group_peer_contact = self._contacts_manager.get_or_create_group_peer_contact(group_number, peer_id)
group = self._get_group_by_number(group_number) group = self._get_group_by_number(group_number)
messages = self._split_message(text.encode('utf-8')) messages = self._split_message(text.encode('utf-8'))
@ -220,12 +203,10 @@ class Messenger(tox_save.ToxSave):
""" """
Send typing notification to a friend Send typing notification to a friend
""" """
if self._contacts_manager.can_send_typing_notification(): if not self._contacts_manager.can_send_typing_notification():
try: return
contact = self._contacts_manager.get_curr_contact() contact = self._contacts_manager.get_curr_contact()
contact.typing_notification_handler.send(self._tox, typing) contact.typing_notification_handler.send(self._tox, typing)
except:
pass
def friend_typing(self, friend_number, typing): def friend_typing(self, friend_number, typing):
""" """

View File

@ -589,22 +589,24 @@ class InterfaceSettings(CenteredWidget):
def import_sm(self): def import_sm(self):
directory = util_ui.directory_dialog(util_ui.tr('Choose folder with smiley pack')) directory = util_ui.directory_dialog(util_ui.tr('Choose folder with smiley pack'))
if directory: if not directory:
src = directory + '/' return
dest = curr_directory() + '/smileys/' + os.path.basename(directory) + '/' src = directory + '/'
copy(src, dest) dest = get_smileys_directory() + os.path.basename(directory) + '/'
copy(src, dest)
def new_font(self): def new_font(self):
font, ok = QtWidgets.QFontDialog.getFont(QtGui.QFont(self._settings['font'], 10), self) font, ok = QtWidgets.QFontDialog.getFont(QtGui.QFont(self._settings['font'], 10), self)
if ok: if not ok:
self._settings['font'] = font.family() return
self._settings.save() self._settings['font'] = font.family()
util_ui.question() self._settings.save()
msgBox = QtWidgets.QMessageBox() util_ui.question()
text = util_ui.tr('Restart app to apply settings') msgBox = QtWidgets.QMessageBox()
msgBox.setWindowTitle(util_ui.tr('Restart required')) text = util_ui.tr('Restart app to apply settings')
msgBox.setText(text) msgBox.setWindowTitle(util_ui.tr('Restart required'))
msgBox.exec_() msgBox.setText(text)
msgBox.exec_()
def select_color(self): def select_color(self):
col = QtWidgets.QColorDialog.getColor(QtGui.QColor(self._settings['unread_color'])) col = QtWidgets.QColorDialog.getColor(QtGui.QColor(self._settings['unread_color']))
@ -616,10 +618,10 @@ class InterfaceSettings(CenteredWidget):
def closeEvent(self, event): def closeEvent(self, event):
self._settings['theme'] = str(self.themeSelect.currentText()) self._settings['theme'] = str(self.themeSelect.currentText())
app = QtWidgets.QApplication.instance()
try: try:
theme = self._settings['theme'] theme = self._settings['theme']
app = QtWidgets.QApplication.instance() with open(get_styles_directory() + self._settings.built_in_themes()[theme]) as fl:
with open(curr_directory() + self._settings.built_in_themes()[theme]) as fl:
style = fl.read() style = fl.read()
app.setStyleSheet(style) app.setStyleSheet(style)
except IsADirectoryError: except IsADirectoryError:
@ -637,7 +639,7 @@ class InterfaceSettings(CenteredWidget):
restart = True restart = True
self._settings['smiley_pack'] = self.smiley_pack.currentText() self._settings['smiley_pack'] = self.smiley_pack.currentText()
self._settings['close_to_tray'] = self.close_to_tray.isChecked() self._settings['close_to_tray'] = self.close_to_tray.isChecked()
smileys.SmileyLoader.get_instance().load_pack() self._smiley_loader.load_pack()
language = self.lang_choose.currentText() language = self.lang_choose.currentText()
if self._settings['language'] != language: if self._settings['language'] != language:
self._settings['language'] = language self._settings['language'] = language