join group with different credentials

This commit is contained in:
ingvar1995 2018-07-26 00:38:25 +03:00
parent 850c3b1ca3
commit 3272617403
7 changed files with 217 additions and 56 deletions

View file

@ -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)

View file

@ -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

View file

@ -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):

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>639</width>
<height>199</height>
<width>640</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
@ -19,9 +19,9 @@
</property>
<property name="geometry">
<rect>
<x>180</x>
<y>150</y>
<width>271</width>
<x>20</x>
<y>250</y>
<width>601</width>
<height>41</height>
</rect>
</property>
@ -32,28 +32,28 @@
<widget class="QLineEdit" name="groupNameLineEdit">
<property name="geometry">
<rect>
<x>140</x>
<y>40</y>
<width>471</width>
<height>31</height>
<x>150</x>
<y>20</y>
<width>470</width>
<height>35</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="groupTypeComboBox">
<property name="geometry">
<rect>
<x>140</x>
<y>100</y>
<width>471</width>
<height>41</height>
<x>150</x>
<y>80</y>
<width>470</width>
<height>35</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="groupNameLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<x>20</x>
<y>20</y>
<width>121</width>
<height>31</height>
</rect>
@ -65,8 +65,8 @@
<widget class="QLabel" name="groupTypeLabel">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<x>20</x>
<y>80</y>
<width>121</width>
<height>31</height>
</rect>
@ -75,6 +75,52 @@
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="statusLabel">
<property name="geometry">
<rect>
<x>20</x>
<y>200</y>
<width>111</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="nickLabel">
<property name="geometry">
<rect>
<x>20</x>
<y>150</y>
<width>111</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLineEdit" name="nickLineEdit">
<property name="geometry">
<rect>
<x>150</x>
<y>140</y>
<width>470</width>
<height>35</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="statusComboBox">
<property name="geometry">
<rect>
<x>150</x>
<y>190</y>
<width>470</width>
<height>35</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>

View file

@ -6,10 +6,22 @@
<rect>
<x>0</x>
<y>0</y>
<width>739</width>
<height>212</height>
<width>740</width>
<height>320</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>740</width>
<height>320</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>740</width>
<height>320</height>
</size>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
@ -17,7 +29,7 @@
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<y>30</y>
<width>67</width>
<height>17</height>
</rect>
@ -30,7 +42,7 @@
<property name="geometry">
<rect>
<x>30</x>
<y>100</y>
<y>90</y>
<width>67</width>
<height>17</height>
</rect>
@ -45,9 +57,9 @@
</property>
<property name="geometry">
<rect>
<x>258</x>
<y>150</y>
<width>241</width>
<x>30</x>
<y>260</y>
<width>680</width>
<height>51</height>
</rect>
</property>
@ -60,7 +72,7 @@
<rect>
<x>190</x>
<y>20</y>
<width>431</width>
<width>520</width>
<height>41</height>
</rect>
</property>
@ -69,8 +81,54 @@
<property name="geometry">
<rect>
<x>190</x>
<y>90</y>
<width>431</width>
<y>80</y>
<width>520</width>
<height>41</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="nickLabel">
<property name="geometry">
<rect>
<x>30</x>
<y>150</y>
<width>67</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="statusLabel">
<property name="geometry">
<rect>
<x>30</x>
<y>210</y>
<width>67</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLineEdit" name="nickLineEdit">
<property name="geometry">
<rect>
<x>190</x>
<y>140</y>
<width>520</width>
<height>41</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="statusComboBox">
<property name="geometry">
<rect>
<x>190</x>
<y>200</y>
<width>520</width>
<height>41</height>
</rect>
</property>

View file

@ -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())

View file

@ -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)