Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(qchat): handle users registration #200

Merged
merged 8 commits into from
Oct 24, 2024
Merged
53 changes: 53 additions & 0 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def __init__(
self.on_custom_context_menu_requested
)

# list users signal listener
self.btn_list_users.pressed.connect(self.on_list_users_button_clicked)
self.btn_list_users.setIcon(
QIcon(QgsApplication.iconPath("processingResult.svg"))
)

# clear chat signal listener
self.btn_clear_chat.pressed.connect(self.on_clear_chat_button_clicked)
self.btn_clear_chat.setIcon(
Expand Down Expand Up @@ -305,6 +311,7 @@ def on_ws_connected(self, room: str) -> None:
self.btn_connect.setText(self.tr("Disconnect"))
self.lbl_status.setText("Connected")
self.grb_room.setTitle(self.tr("Room: {room}").format(room=room))
self.btn_list_users.setEnabled(True)
self.grb_user.setEnabled(True)
self.current_room = room

Expand All @@ -321,6 +328,14 @@ def on_ws_connected(self, room: str) -> None:
self.tr("Connected to room '{room}'").format(room=room)
)

# send newcomer message to websocket
if not self.settings.qchat_incognito_mode:
message = {
"author": INTERNAL_MESSAGE_AUTHOR,
"newcomer": self.settings.author_nickname,
}
self.ws_client.sendTextMessage(json.dumps(message))

def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None:
"""
Disconnect widget from the current room
Expand All @@ -335,6 +350,7 @@ def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None:
self.lbl_status.setText("Disconnected")
self.grb_room.setTitle(self.tr("Room"))
self.grb_qchat.setTitle(self.tr("QChat"))
self.btn_list_users.setEnabled(False)
self.grb_user.setEnabled(False)
self.connected = False
if close_ws:
Expand Down Expand Up @@ -440,6 +456,24 @@ def handle_internal_message(self, message: dict[str, Any]) -> None:
)
)
self.log(message=f"Internal message received: {nb_users} users in room")
if (
"newcomer" in message
and self.settings.qchat_display_admin_messages
and message["newcomer"] != self.settings.author_nickname
):
newcomer = message["newcomer"]
self.add_admin_message(
self.tr("{newcomer} has joined the room").format(newcomer=newcomer)
)
if (
"exiter" in message
and self.settings.qchat_display_admin_messages
and message["exiter"] != self.settings.author_nickname
):
exiter = message["exiter"]
self.add_admin_message(
self.tr("{newcomer} has left the room").format(newcomer=exiter)
)

def on_message_double_clicked(self, item: QTreeWidgetItem, column: int) -> None:
"""
Expand Down Expand Up @@ -507,6 +541,25 @@ def on_hide_message(self, item: QTreeWidgetItem) -> None:
root = self.twg_chat.invisibleRootItem()
(item.parent() or root).removeChild(item)

def on_list_users_button_clicked(self) -> None:
"""
Action called when the list users button is clicked
"""
try:
users = self.qchat_client.get_registered_users(self.current_room)
QMessageBox.information(
self,
self.tr("Registered users"),
self.tr(
"""Registered users in room ({room}):

{users}"""
).format(room=self.current_room, users=",".join(users)),
)
except Exception as exc:
self.iface.messageBar().pushCritical(self.tr("QChat error"), str(exc))
self.log(message=str(exc), log_level=Qgis.Critical)

def on_clear_chat_button_clicked(self) -> None:
"""
Action called when the clear chat button is clicked
Expand Down
36 changes: 28 additions & 8 deletions qtribu/gui/dck_qchat.ui
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="grb_qchat">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
Expand Down Expand Up @@ -215,14 +218,31 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_clear_chat">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
<layout class="QHBoxLayout" name="hly_qchat_buttons">
<item>
<widget class="QPushButton" name="btn_list_users">
<property name="enabled">
<bool>false</bool>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>List users</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_clear_chat">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down
2 changes: 2 additions & 0 deletions qtribu/gui/dlg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def apply(self):
self.ckb_display_admin_messages.isChecked()
)
settings.qchat_show_avatars = self.ckb_show_avatars.isChecked()
settings.qchat_incognito_mode = self.ckb_incognito_mode.isChecked()
settings.qchat_play_sounds = self.ckb_play_sounds.isChecked()
settings.qchat_sound_volume = self.hsl_sound_volume.value()
settings.qchat_ring_tone = self.cbb_ring_tone.currentText()
Expand Down Expand Up @@ -166,6 +167,7 @@ def load_settings(self) -> None:
settings.qchat_display_admin_messages
)
self.ckb_show_avatars.setChecked(settings.qchat_show_avatars)
self.ckb_incognito_mode.setChecked(settings.qchat_incognito_mode)
self.ckb_play_sounds.setChecked(settings.qchat_play_sounds)
self.hsl_sound_volume.setValue(settings.qchat_sound_volume)
beep_index = self.cbb_ring_tone.findText(
Expand Down
26 changes: 24 additions & 2 deletions qtribu/gui/dlg_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>875</width>
<height>577</height>
<width>877</width>
<height>579</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
Expand Down Expand Up @@ -389,20 +389,42 @@
<layout class="QHBoxLayout" name="hly_misc">
<item>
<widget class="QCheckBox" name="ckb_show_avatars">
<property name="toolTip">
<string extracomment="Show avatr besides a QChat message"/>
</property>
<property name="text">
<string>Show avatars</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckb_incognito_mode">
<property name="toolTip">
<string extracomment="Register to the server so that other know you are connected"/>
</property>
<property name="text">
<string>Incognito mode</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckb_display_admin_messages">
<property name="toolTip">
<string extracomment="Display connections and disconnections messages"/>
</property>
<property name="text">
<string>Display admin messages</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="ckb_cheatcodes">
<property name="toolTip">
<string extracomment=";)"/>
</property>
<property name="text">
<string>Activate cheatcodes</string>
</property>
Expand Down
14 changes: 14 additions & 0 deletions qtribu/logic/qchat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,17 @@ def get_rooms(self) -> list[str]:
)
data = json.loads(str(response, "UTF8"))
return data

def get_registered_users(self, room: str) -> list[str]:
"""
Get registered users in a room with an API HTTP CALL
"""
url = f"{self.instance_uri}/room/{room}/users"
response: QByteArray = self.qntwk.get_from_source(
headers=HEADERS,
url=url,
response_expected_content_type=CONTENT_TYPE_JSON,
use_cache=False,
)
data = json.loads(str(response, "UTF8"))
return data
1 change: 1 addition & 0 deletions qtribu/toolbelt/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PlgSettingsStructure:
qchat_activate_cheatcode: bool = True
qchat_display_admin_messages: bool = False
qchat_show_avatars: bool = True
qchat_incognito_mode: bool = False
qchat_play_sounds: bool = True
qchat_sound_volume: int = 33
qchat_ring_tone: str = "beep_1"
Expand Down
Loading