From c97b04163a581a304dfed526cf08b3db025fc4b8 Mon Sep 17 00:00:00 2001 From: gounux Date: Tue, 8 Oct 2024 14:20:41 +0200 Subject: [PATCH 1/6] feature(qchat): handle users registration --- qtribu/gui/dck_qchat.py | 41 ++++++++++++++++++++++++++++++++++++ qtribu/gui/dck_qchat.ui | 33 ++++++++++++++++++++++------- qtribu/logic/qchat_client.py | 14 ++++++++++++ 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index 6323a0d5..75fc9b21 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -95,6 +95,12 @@ def __init__(self, iface: QgisInterface, parent: QWidget = None): self.on_custom_context_menu_requested ) + # list users signal listener + self.bltn_list_users.pressed.connect(self.on_list_users_button_clicked) + self.bltn_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( @@ -288,6 +294,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.grb_qchat.setEnabled(True) self.grb_user.setEnabled(True) self.current_room = room self.connected = True @@ -296,6 +303,12 @@ def on_ws_connected(self, room: str) -> None: self.add_admin_message( self.tr("Connected to room '{room}'").format(room=room) ) + # send newcomer message to websocket + 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: """ @@ -311,6 +324,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.grb_qchat.setEnabled(False) self.grb_user.setEnabled(False) self.connected = False if close_ws: @@ -419,6 +433,14 @@ 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 message["newcomer"] != self.settings.author_nickname + ): + newcomer = message["newcomer"] + self.add_admin_message( + self.tr("{newcomer} has joined the room").format(newcomer=newcomer) + ) def on_message_double_clicked(self, item: QTreeWidgetItem, column: int) -> None: """ @@ -486,6 +508,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 diff --git a/qtribu/gui/dck_qchat.ui b/qtribu/gui/dck_qchat.ui index 64362264..40c15d6c 100644 --- a/qtribu/gui/dck_qchat.ui +++ b/qtribu/gui/dck_qchat.ui @@ -145,6 +145,9 @@ + + false + 0 @@ -208,14 +211,28 @@ - - - PointingHandCursor - - - Clear - - + + + + + PointingHandCursor + + + List users + + + + + + + PointingHandCursor + + + Clear + + + + diff --git a/qtribu/logic/qchat_client.py b/qtribu/logic/qchat_client.py index d73b14a9..bb7c654f 100644 --- a/qtribu/logic/qchat_client.py +++ b/qtribu/logic/qchat_client.py @@ -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 From 1f4749333dfa8a4d106ff312547645a9a64a20a8 Mon Sep 17 00:00:00 2001 From: gounux Date: Tue, 8 Oct 2024 15:26:20 +0200 Subject: [PATCH 2/6] feature(qchat): set user registration optional --- qtribu/gui/dck_qchat.py | 12 +++++++----- qtribu/gui/dlg_settings.py | 2 ++ qtribu/gui/dlg_settings.ui | 10 ++++++++++ qtribu/toolbelt/preferences.py | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index 75fc9b21..1c7dbf14 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -303,12 +303,14 @@ def on_ws_connected(self, room: str) -> None: self.add_admin_message( self.tr("Connected to room '{room}'").format(room=room) ) + # send newcomer message to websocket - message = { - "author": INTERNAL_MESSAGE_AUTHOR, - "newcomer": self.settings.author_nickname, - } - self.ws_client.sendTextMessage(json.dumps(message)) + if self.settings.qchat_register_user: + 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: """ diff --git a/qtribu/gui/dlg_settings.py b/qtribu/gui/dlg_settings.py index e4051720..8904195e 100644 --- a/qtribu/gui/dlg_settings.py +++ b/qtribu/gui/dlg_settings.py @@ -117,6 +117,7 @@ def apply(self): self.ckb_display_admin_messages.isChecked() ) settings.qchat_show_avatars = self.ckb_show_avatars.isChecked() + settings.qchat_register_user = self.ckb_register_nickname.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() @@ -164,6 +165,7 @@ def load_settings(self) -> None: settings.qchat_display_admin_messages ) self.ckb_show_avatars.setChecked(settings.qchat_show_avatars) + self.ckb_register_nickname.setChecked(settings.qchat_register_user) 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( diff --git a/qtribu/gui/dlg_settings.ui b/qtribu/gui/dlg_settings.ui index 492916c2..a36e78ae 100644 --- a/qtribu/gui/dlg_settings.ui +++ b/qtribu/gui/dlg_settings.ui @@ -384,6 +384,16 @@ + + + + Register nickname + + + false + + + diff --git a/qtribu/toolbelt/preferences.py b/qtribu/toolbelt/preferences.py index 666228e0..c1087565 100644 --- a/qtribu/toolbelt/preferences.py +++ b/qtribu/toolbelt/preferences.py @@ -41,6 +41,7 @@ class PlgSettingsStructure: qchat_activate_cheatcode: bool = True qchat_display_admin_messages: bool = False qchat_show_avatars: bool = True + qchat_register_user: bool = True qchat_play_sounds: bool = True qchat_sound_volume: int = 33 qchat_ring_tone: str = "beep_1" From 87d15e80c109838264a2c3fde7208404cfe0e13d Mon Sep 17 00:00:00 2001 From: gounux Date: Tue, 8 Oct 2024 20:35:42 +0200 Subject: [PATCH 3/6] feature(qchat): notify when a user has left --- qtribu/gui/dck_qchat.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index 1c7dbf14..cc12634b 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -437,12 +437,22 @@ 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: """ From 3ca7845442c39582ec8a65869de28ebb23da32ae Mon Sep 17 00:00:00 2001 From: gounux Date: Wed, 9 Oct 2024 08:14:02 +0200 Subject: [PATCH 4/6] refactor(qchat): rename registration to incognito mode --- qtribu/gui/dck_qchat.py | 2 +- qtribu/gui/dlg_settings.py | 4 ++-- qtribu/gui/dlg_settings.ui | 4 ++-- qtribu/toolbelt/preferences.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index cc12634b..6e6bdcb1 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -305,7 +305,7 @@ def on_ws_connected(self, room: str) -> None: ) # send newcomer message to websocket - if self.settings.qchat_register_user: + if not self.settings.qchat_incognito_mode: message = { "author": INTERNAL_MESSAGE_AUTHOR, "newcomer": self.settings.author_nickname, diff --git a/qtribu/gui/dlg_settings.py b/qtribu/gui/dlg_settings.py index 8904195e..0d9ebe91 100644 --- a/qtribu/gui/dlg_settings.py +++ b/qtribu/gui/dlg_settings.py @@ -117,7 +117,7 @@ def apply(self): self.ckb_display_admin_messages.isChecked() ) settings.qchat_show_avatars = self.ckb_show_avatars.isChecked() - settings.qchat_register_user = self.ckb_register_nickname.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() @@ -165,7 +165,7 @@ def load_settings(self) -> None: settings.qchat_display_admin_messages ) self.ckb_show_avatars.setChecked(settings.qchat_show_avatars) - self.ckb_register_nickname.setChecked(settings.qchat_register_user) + 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( diff --git a/qtribu/gui/dlg_settings.ui b/qtribu/gui/dlg_settings.ui index a36e78ae..75bb34c4 100644 --- a/qtribu/gui/dlg_settings.ui +++ b/qtribu/gui/dlg_settings.ui @@ -385,9 +385,9 @@ - + - Register nickname + Incognito mode false diff --git a/qtribu/toolbelt/preferences.py b/qtribu/toolbelt/preferences.py index c1087565..137873e9 100644 --- a/qtribu/toolbelt/preferences.py +++ b/qtribu/toolbelt/preferences.py @@ -41,7 +41,7 @@ class PlgSettingsStructure: qchat_activate_cheatcode: bool = True qchat_display_admin_messages: bool = False qchat_show_avatars: bool = True - qchat_register_user: bool = True + qchat_incognito_mode: bool = False qchat_play_sounds: bool = True qchat_sound_volume: int = 33 qchat_ring_tone: str = "beep_1" From a27167d17cdbfd75fb306fdbe3df95d7959f35ef Mon Sep 17 00:00:00 2001 From: gounux Date: Sun, 13 Oct 2024 11:15:58 +0200 Subject: [PATCH 5/6] ui(qchat): set settings tooltips --- qtribu/gui/dlg_settings.ui | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/qtribu/gui/dlg_settings.ui b/qtribu/gui/dlg_settings.ui index 30c6c05c..c4aee46a 100644 --- a/qtribu/gui/dlg_settings.ui +++ b/qtribu/gui/dlg_settings.ui @@ -42,8 +42,8 @@ 0 0 - 875 - 577 + 877 + 579 @@ -379,6 +379,9 @@ + + + Show avatars @@ -386,6 +389,9 @@ + + + Incognito mode @@ -396,6 +402,9 @@ + + + Display admin messages @@ -403,6 +412,9 @@ + + + Activate cheatcodes From 7d3f88f6a5be45944d55d2f6fc26dbae1f5359aa Mon Sep 17 00:00:00 2001 From: gounux Date: Sun, 13 Oct 2024 11:16:54 +0200 Subject: [PATCH 6/6] ui(qchat): set qchat groupbox enabled --- qtribu/gui/dck_qchat.py | 8 ++++---- qtribu/gui/dck_qchat.ui | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index 21cfdc54..c0c4a654 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -95,8 +95,8 @@ def __init__(self, iface: QgisInterface, parent: QWidget = None): ) # list users signal listener - self.bltn_list_users.pressed.connect(self.on_list_users_button_clicked) - self.bltn_list_users.setIcon( + self.btn_list_users.pressed.connect(self.on_list_users_button_clicked) + self.btn_list_users.setIcon( QIcon(QgsApplication.iconPath("processingResult.svg")) ) @@ -293,7 +293,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.grb_qchat.setEnabled(True) + self.btn_list_users.setEnabled(True) self.grb_user.setEnabled(True) self.current_room = room self.connected = True @@ -325,7 +325,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.grb_qchat.setEnabled(False) + self.btn_list_users.setEnabled(False) self.grb_user.setEnabled(False) self.connected = False if close_ws: diff --git a/qtribu/gui/dck_qchat.ui b/qtribu/gui/dck_qchat.ui index 18a6de28..bbc29f9e 100644 --- a/qtribu/gui/dck_qchat.ui +++ b/qtribu/gui/dck_qchat.ui @@ -153,7 +153,7 @@ - false + true @@ -220,7 +220,10 @@ - + + + false + PointingHandCursor