From 5f56d630ce19dcf2ab4ca68f6c7befbdbd905eb6 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Fri, 3 Aug 2018 21:07:18 +0300 Subject: [PATCH] messenger refactoring --- toxygen/contacts/contacts_manager.py | 14 +++--- toxygen/messenger/messenger.py | 73 ++++++++++------------------ toxygen/ui/menu.py | 34 +++++++------ 3 files changed, 52 insertions(+), 69 deletions(-) diff --git a/toxygen/contacts/contacts_manager.py b/toxygen/contacts/contacts_manager.py index 4a5454b..757bfb0 100644 --- a/toxygen/contacts/contacts_manager.py +++ b/toxygen/contacts/contacts_manager.py @@ -24,7 +24,6 @@ class ContactsManager(ToxSave): self._active_contact_changed = Event() self._sorting = settings['sorting'] self._filter_string = '' - self._friend_item_height = 40 if settings['compact_mode'] else 70 screen.contacts_filter.setCurrentIndex(int(self._sorting)) self._history = history self._load_contacts() @@ -149,6 +148,9 @@ class ContactsManager(ToxSave): def is_active_a_group(self): 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 # ----------------------------------------------------------------------------------------------------------------- @@ -198,11 +200,9 @@ class ContactsManager(ToxSave): 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 friend.visibility = friend.visibility or friend.messages or friend.actions - # TODO: calculate height - if friend.visibility: - self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, self._friend_item_height)) - else: - self._screen.friends_list.item(index).setSizeHint(QtCore.QSize(250, 0)) + item = self._screen.friends_list.item(index) + item_widget = self._screen.friends_list.itemWidget(item) + item.setSizeHint(QtCore.QSize(250, item_widget.height() if friend.visibility else 0)) # save soring results self._sorting, self._filter_string = sorting, filter_str self._settings['sorting'] = self._sorting @@ -436,7 +436,7 @@ class ContactsManager(ToxSave): util.log('Accept friend request failed! ' + str(ex)) 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 diff --git a/toxygen/messenger/messenger.py b/toxygen/messenger/messenger.py index 99d7f2f..cce619d 100644 --- a/toxygen/messenger/messenger.py +++ b/toxygen/messenger/messenger.py @@ -44,14 +44,28 @@ class Messenger(tox_save.ToxSave): def send_message(self): 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 :param text: message text @@ -60,19 +74,9 @@ class Messenger(tox_save.ToxSave): if friend_number is None: 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: 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) messages = self._split_message(text.encode('utf-8')) t = util.get_unix_time() @@ -108,23 +112,13 @@ class Messenger(tox_save.ToxSave): # 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: 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: 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) messages = self._split_message(text.encode('utf-8')) t = util.get_unix_time() @@ -155,27 +149,16 @@ class Messenger(tox_save.ToxSave): # 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: group_peer_contact = self._contacts_manager.get_curr_contact() peer_id = group_peer_contact.number group = self._get_group_by_public_key(group_peer_contact.group_pk) 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: 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 = self._get_group_by_number(group_number) messages = self._split_message(text.encode('utf-8')) @@ -220,12 +203,10 @@ class Messenger(tox_save.ToxSave): """ Send typing notification to a friend """ - if self._contacts_manager.can_send_typing_notification(): - try: - contact = self._contacts_manager.get_curr_contact() - contact.typing_notification_handler.send(self._tox, typing) - except: - pass + if not self._contacts_manager.can_send_typing_notification(): + return + contact = self._contacts_manager.get_curr_contact() + contact.typing_notification_handler.send(self._tox, typing) def friend_typing(self, friend_number, typing): """ diff --git a/toxygen/ui/menu.py b/toxygen/ui/menu.py index 07fbb85..ea49e82 100644 --- a/toxygen/ui/menu.py +++ b/toxygen/ui/menu.py @@ -589,22 +589,24 @@ class InterfaceSettings(CenteredWidget): def import_sm(self): directory = util_ui.directory_dialog(util_ui.tr('Choose folder with smiley pack')) - if directory: - src = directory + '/' - dest = curr_directory() + '/smileys/' + os.path.basename(directory) + '/' - copy(src, dest) + if not directory: + return + src = directory + '/' + dest = get_smileys_directory() + os.path.basename(directory) + '/' + copy(src, dest) def new_font(self): font, ok = QtWidgets.QFontDialog.getFont(QtGui.QFont(self._settings['font'], 10), self) - if ok: - self._settings['font'] = font.family() - self._settings.save() - util_ui.question() - msgBox = QtWidgets.QMessageBox() - text = util_ui.tr('Restart app to apply settings') - msgBox.setWindowTitle(util_ui.tr('Restart required')) - msgBox.setText(text) - msgBox.exec_() + if not ok: + return + self._settings['font'] = font.family() + self._settings.save() + util_ui.question() + msgBox = QtWidgets.QMessageBox() + text = util_ui.tr('Restart app to apply settings') + msgBox.setWindowTitle(util_ui.tr('Restart required')) + msgBox.setText(text) + msgBox.exec_() def select_color(self): col = QtWidgets.QColorDialog.getColor(QtGui.QColor(self._settings['unread_color'])) @@ -616,10 +618,10 @@ class InterfaceSettings(CenteredWidget): def closeEvent(self, event): self._settings['theme'] = str(self.themeSelect.currentText()) + app = QtWidgets.QApplication.instance() try: theme = self._settings['theme'] - app = QtWidgets.QApplication.instance() - with open(curr_directory() + self._settings.built_in_themes()[theme]) as fl: + with open(get_styles_directory() + self._settings.built_in_themes()[theme]) as fl: style = fl.read() app.setStyleSheet(style) except IsADirectoryError: @@ -637,7 +639,7 @@ class InterfaceSettings(CenteredWidget): restart = True self._settings['smiley_pack'] = self.smiley_pack.currentText() 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() if self._settings['language'] != language: self._settings['language'] = language