From 3272617403699e3b6d1a6f1197047ec046c2bd57 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Thu, 26 Jul 2018 00:38:25 +0300 Subject: [PATCH] join group with different credentials --- toxygen/app.py | 2 +- toxygen/groups/groups_service.py | 17 +++--- toxygen/ui/groups_widgets.py | 60 ++++++++++++++++--- toxygen/ui/views/create_group_screen.ui | 80 +++++++++++++++++++------ toxygen/ui/views/join_group_screen.ui | 78 ++++++++++++++++++++---- toxygen/ui/widgets_factory.py | 4 +- toxygen/wrapper/tox.py | 32 ++++++---- 7 files changed, 217 insertions(+), 56 deletions(-) diff --git a/toxygen/app.py b/toxygen/app.py index 23a465a..77864e7 100644 --- a/toxygen/app.py +++ b/toxygen/app.py @@ -366,7 +366,7 @@ class App: widgets_factory = None widgets_factory_provider = Provider(lambda: widgets_factory) self._groups_service = GroupsService(self._tox, self._contacts_manager, self._contacts_provider, self._ms, - widgets_factory_provider) + widgets_factory_provider, self._profile) widgets_factory = WidgetsFactory(self._settings, self._profile, self._profile_manager, self._contacts_manager, self._file_transfer_handler, self._smiley_loader, self._plugin_loader, self._toxes, self._version, self._groups_service, history) diff --git a/toxygen/groups/groups_service.py b/toxygen/groups/groups_service.py index 94d5653..f9cdfc9 100644 --- a/toxygen/groups/groups_service.py +++ b/toxygen/groups/groups_service.py @@ -6,12 +6,13 @@ import wrapper.toxcore_enums_and_consts as constants class GroupsService(tox_save.ToxSave): - def __init__(self, tox, contacts_manager, contacts_provider, main_screen, widgets_factory_provider): + def __init__(self, tox, contacts_manager, contacts_provider, main_screen, widgets_factory_provider, profile): super().__init__(tox) self._contacts_manager = contacts_manager self._contacts_provider = contacts_provider self._peers_list_widget = main_screen.peers_list self._widgets_factory_provider = widgets_factory_provider + self._profile = profile self._peer_screen = None def set_tox(self, tox): @@ -23,8 +24,8 @@ class GroupsService(tox_save.ToxSave): # Groups creation # ----------------------------------------------------------------------------------------------------------------- - def create_new_gc(self, name, privacy_state): - group_number = self._tox.group_new(privacy_state, name.encode('utf-8')) + def create_new_gc(self, name, privacy_state, nick, status): + group_number = self._tox.group_new(privacy_state, name, nick, status) if group_number == -1: return @@ -32,12 +33,12 @@ class GroupsService(tox_save.ToxSave): group = self._get_group_by_number(group_number) group.status = constants.TOX_USER_STATUS['NONE'] - def join_gc_by_id(self, chat_id, password): - group_number = self._tox.group_join(chat_id, password) + def join_gc_by_id(self, chat_id, password, nick, status): + group_number = self._tox.group_join(chat_id, password, nick, status) self._add_new_group_by_number(group_number) - def join_gc_via_invite(self, invite_data, friend_number, password): - group_number = self._tox.group_invite_accept(invite_data, friend_number, password) + def join_gc_via_invite(self, invite_data, friend_number, nick, status, password): + group_number = self._tox.group_invite_accept(invite_data, friend_number, nick, status, password) self._add_new_group_by_number(group_number) # ----------------------------------------------------------------------------------------------------------------- @@ -72,7 +73,7 @@ class GroupsService(tox_save.ToxSave): friend = self._get_friend_by_number(friend_number) text = util_ui.tr('Friend {} invites you to group "{}". Accept?') if util_ui.question(text.format(friend.name, group_name), util_ui.tr('Group invite')): - self.join_gc_via_invite(invite_data, friend_number, None) + self.join_gc_via_invite(invite_data, friend_number, self._profile.name, self._profile.status or 0, None) # ----------------------------------------------------------------------------------------------------------------- # Group info methods diff --git a/toxygen/ui/groups_widgets.py b/toxygen/ui/groups_widgets.py index aa91935..b9f38a9 100644 --- a/toxygen/ui/groups_widgets.py +++ b/toxygen/ui/groups_widgets.py @@ -3,72 +3,116 @@ import utils.util as util from ui.widgets import * from wrapper.toxcore_enums_and_consts import * +# TODO: move common logic to separate class + class CreateGroupScreen(CenteredWidget): - def __init__(self, groups_service): + def __init__(self, groups_service, profile): super().__init__() self._groups_service = groups_service + self._profile = profile uic.loadUi(util.get_views_path('create_group_screen'), self) self.center() self._update_ui() def _update_ui(self): self._retranslate_ui() + + self.statusComboBox.setCurrentIndex(self._profile.status or 0) + self.nickLineEdit.setText(self._profile.name) + self.addGroupButton.clicked.connect(self._create_group) self.groupNameLineEdit.textChanged.connect(self._group_name_changed) + self.nickLineEdit.textChanged.connect(self._nick_changed) def _retranslate_ui(self): self.setWindowTitle(util_ui.tr('Create new group chat')) self.groupNameLabel.setText(util_ui.tr('Group name:')) self.groupTypeLabel.setText(util_ui.tr('Group type:')) + self.nickLabel.setText(util_ui.tr('Nickname:')) + self.statusLabel.setText(util_ui.tr('Status:')) + self.nickLineEdit.setPlaceholderText(util_ui.tr('Your nick in chat')) self.groupNameLineEdit.setPlaceholderText(util_ui.tr('Group\'s persistent name')) self.addGroupButton.setText(util_ui.tr('Create group')) self.groupTypeComboBox.addItem(util_ui.tr('Public')) self.groupTypeComboBox.addItem(util_ui.tr('Private')) self.groupTypeComboBox.setCurrentIndex(1) + self.statusComboBox.addItem(util_ui.tr('Online')) + self.statusComboBox.addItem(util_ui.tr('Away')) + self.statusComboBox.addItem(util_ui.tr('Busy')) def _create_group(self): - name = self.groupNameLineEdit.text() + group_name = self.groupNameLineEdit.text() privacy_state = self.groupTypeComboBox.currentIndex() - self._groups_service.create_new_gc(name, privacy_state) + nick = self.nickLineEdit.text() + status = self.statusComboBox.currentIndex() + self._groups_service.create_new_gc(group_name, privacy_state, nick, status) self.close() + def _nick_changed(self): + self._update_button_state() + def _group_name_changed(self): - name = self.groupNameLineEdit.text() - self.addGroupButton.setEnabled(bool(name.strip())) + self._update_button_state() + + def _update_button_state(self): + is_nick_set = bool(self.nickLineEdit.text()) + is_group_name_set = bool(self.groupNameLineEdit.text()) + self.addGroupButton.setEnabled(is_nick_set and is_group_name_set) class JoinGroupScreen(CenteredWidget): - def __init__(self, groups_service): + def __init__(self, groups_service, profile): super().__init__() self._groups_service = groups_service + self._profile = profile uic.loadUi(util.get_views_path('join_group_screen'), self) self.center() self._update_ui() def _update_ui(self): self._retranslate_ui() + + self.statusComboBox.setCurrentIndex(self._profile.status or 0) + self.nickLineEdit.setText(self._profile.name) + self.chatIdLineEdit.textChanged.connect(self._chat_id_changed) self.joinGroupButton.clicked.connect(self._join_group) + self.nickLineEdit.textChanged.connect(self._nick_changed) def _retranslate_ui(self): self.setWindowTitle(util_ui.tr('Join public group chat')) self.chatIdLabel.setText(util_ui.tr('Group ID:')) self.passwordLabel.setText(util_ui.tr('Password:')) + self.nickLabel.setText(util_ui.tr('Nickname:')) + self.statusLabel.setText(util_ui.tr('Status:')) self.chatIdLineEdit.setPlaceholderText(util_ui.tr('Group\'s chat ID')) + self.nickLineEdit.setPlaceholderText(util_ui.tr('Your nick in chat')) self.joinGroupButton.setText(util_ui.tr('Join group')) self.passwordLineEdit.setPlaceholderText(util_ui.tr('Optional password')) + self.statusComboBox.addItem(util_ui.tr('Online')) + self.statusComboBox.addItem(util_ui.tr('Away')) + self.statusComboBox.addItem(util_ui.tr('Busy')) def _chat_id_changed(self): + self._update_button_state() + + def _nick_changed(self): + self._update_button_state() + + def _update_button_state(self): chat_id = self._get_chat_id() - self.joinGroupButton.setEnabled(len(chat_id) == TOX_GROUP_CHAT_ID_SIZE * 2) + is_nick_set = bool(self.nickLineEdit.text()) + self.joinGroupButton.setEnabled(len(chat_id) == TOX_GROUP_CHAT_ID_SIZE * 2 and is_nick_set) def _join_group(self): chat_id = self._get_chat_id() password = self.passwordLineEdit.text() - self._groups_service.join_gc_by_id(chat_id, password) + nick = self.nickLineEdit.text() + status = self.statusComboBox.currentIndex() + self._groups_service.join_gc_by_id(chat_id, password, nick, status) self.close() def _get_chat_id(self): diff --git a/toxygen/ui/views/create_group_screen.ui b/toxygen/ui/views/create_group_screen.ui index 08a27ff..3a3358a 100644 --- a/toxygen/ui/views/create_group_screen.ui +++ b/toxygen/ui/views/create_group_screen.ui @@ -6,8 +6,8 @@ 0 0 - 639 - 199 + 640 + 300 @@ -19,9 +19,9 @@ - 180 - 150 - 271 + 20 + 250 + 601 41 @@ -32,28 +32,28 @@ - 140 - 40 - 471 - 31 + 150 + 20 + 470 + 35 - 140 - 100 - 471 - 41 + 150 + 80 + 470 + 35 - 10 - 40 + 20 + 20 121 31 @@ -65,8 +65,8 @@ - 10 - 100 + 20 + 80 121 31 @@ -75,6 +75,52 @@ TextLabel + + + + 20 + 200 + 111 + 17 + + + + TextLabel + + + + + + 20 + 150 + 111 + 17 + + + + TextLabel + + + + + + 150 + 140 + 470 + 35 + + + + + + + 150 + 190 + 470 + 35 + + + diff --git a/toxygen/ui/views/join_group_screen.ui b/toxygen/ui/views/join_group_screen.ui index 66b0420..077a332 100644 --- a/toxygen/ui/views/join_group_screen.ui +++ b/toxygen/ui/views/join_group_screen.ui @@ -6,10 +6,22 @@ 0 0 - 739 - 212 + 740 + 320 + + + 740 + 320 + + + + + 740 + 320 + + Form @@ -17,7 +29,7 @@ 30 - 40 + 30 67 17 @@ -30,7 +42,7 @@ 30 - 100 + 90 67 17 @@ -45,9 +57,9 @@ - 258 - 150 - 241 + 30 + 260 + 680 51 @@ -60,7 +72,7 @@ 190 20 - 431 + 520 41 @@ -69,8 +81,54 @@ 190 - 90 - 431 + 80 + 520 + 41 + + + + + + + 30 + 150 + 67 + 17 + + + + TextLabel + + + + + + 30 + 210 + 67 + 17 + + + + TextLabel + + + + + + 190 + 140 + 520 + 41 + + + + + + + 190 + 200 + 520 41 diff --git a/toxygen/ui/widgets_factory.py b/toxygen/ui/widgets_factory.py index 1ddc6a5..8f341df 100644 --- a/toxygen/ui/widgets_factory.py +++ b/toxygen/ui/widgets_factory.py @@ -64,10 +64,10 @@ class WidgetsFactory: return StickerWindow(self._file_transfer_handler, self._contacts_manager) def create_group_screen_window(self): - return CreateGroupScreen(self._groups_service) + return CreateGroupScreen(self._groups_service, self._profile) def create_join_group_screen_window(self): - return JoinGroupScreen(self._groups_service) + return JoinGroupScreen(self._groups_service, self._profile) def create_search_screen(self, messages): return SearchScreen(self._contacts_manager, self._history, messages, messages.parent()) diff --git a/toxygen/wrapper/tox.py b/toxygen/wrapper/tox.py index 64b55ea..cb2f25e 100644 --- a/toxygen/wrapper/tox.py +++ b/toxygen/wrapper/tox.py @@ -29,7 +29,7 @@ class ToxOptions(Structure): class GroupChatSelfPeerInfo(Structure): _fields_ = [ ('nick', c_char_p), - ('nick_length', c_uint16), + ('nick_length', c_uint8), ('user_status', c_int) ] @@ -1533,7 +1533,7 @@ class Tox: # Group chat instance management # ----------------------------------------------------------------------------------------------------------------- - def group_new(self, privacy_state, group_name): + def group_new(self, privacy_state, group_name, nick, status): """ Creates a new group chat. @@ -1551,12 +1551,16 @@ class Tox: """ error = c_int() - peer_info = self.group_chat_self_peer_info_new() - result = Tox.libtoxcore.tox_group_new(self._tox_pointer, privacy_state, group_name, + peer_info = self.group_self_peer_info_new() + nick = bytes(nick, 'utf-8') + peer_info.contents.nick = c_char_p(nick) + peer_info.contents.nick_length = len(nick) + peer_info.contents.user_status = status + result = Tox.libtoxcore.tox_group_new(self._tox_pointer, privacy_state, group_name.encode('utf-8'), len(group_name), peer_info, byref(error)) return result - def group_join(self, chat_id, password): + def group_join(self, chat_id, password, nick, status): """ Joins a group chat with specified Chat ID. @@ -1571,7 +1575,11 @@ class Tox: """ error = c_int() - peer_info = self.group_chat_self_peer_info_new() + peer_info = self.group_self_peer_info_new() + nick = bytes(nick, 'utf-8') + peer_info.contents.nick = c_char_p(nick) + peer_info.contents.nick_length = len(nick) + peer_info.contents.user_status = status result = Tox.libtoxcore.tox_group_join(self._tox_pointer, string_to_bin(chat_id), password, len(password) if password is not None else 0, @@ -2171,15 +2179,15 @@ class Tox: result = Tox.libtoxcore.tox_group_invite_friend(self._tox_pointer, group_number, friend_number, byref(error)) return result - def group_chat_self_peer_info_new(self): + def group_self_peer_info_new(self): error = c_int() - f = Tox.libtoxcore.group_chat_self_peer_info_new + f = Tox.libtoxcore.tox_group_self_peer_info_new f.restype = POINTER(GroupChatSelfPeerInfo) result = f(self._tox_pointer, byref(error)) return result - def group_invite_accept(self, invite_data, friend_number, password=None): + def group_invite_accept(self, invite_data, friend_number, nick, status, password=None): """ Accept an invite to a group chat that the client previously received from a friend. The invite is only valid while the inviter is present in the group. @@ -2192,7 +2200,11 @@ class Tox: error = c_int() f = Tox.libtoxcore.tox_group_invite_accept f.restype = c_uint32 - peer_info = self.group_chat_self_peer_info_new() + peer_info = self.group_self_peer_info_new() + nick = bytes(nick, 'utf-8') + peer_info.contents.nick = c_char_p(nick) + peer_info.contents.nick_length = len(nick) + peer_info.contents.user_status = status result = f(self._tox_pointer, friend_number, invite_data, len(invite_data), password, len(password) if password is not None else 0, peer_info, byref(error)) print('Invite accept. Result:', result, 'Error:', error.value)