|
|
|
|
@ -226,6 +226,16 @@ class P2PChatGUI(QObject):
|
|
|
|
|
self._contact_list_widget = ContactListWidget()
|
|
|
|
|
self.main_window.set_contact_list_widget(self._contact_list_widget)
|
|
|
|
|
|
|
|
|
|
# 创建并设置聊天组件
|
|
|
|
|
self._chat_widget = ChatWidget()
|
|
|
|
|
self.main_window.set_chat_widget(self._chat_widget)
|
|
|
|
|
|
|
|
|
|
# 连接聊天组件信号
|
|
|
|
|
self._chat_widget.message_sent.connect(self._on_send_message)
|
|
|
|
|
self._chat_widget.file_send_requested.connect(lambda peer_id: self._on_send_file())
|
|
|
|
|
self._chat_widget.image_send_requested.connect(lambda peer_id: self._on_send_image())
|
|
|
|
|
self._chat_widget.voice_call_requested.connect(lambda peer_id: self._on_voice_call())
|
|
|
|
|
|
|
|
|
|
# 连接联系人列表信号
|
|
|
|
|
self._contact_list_widget.contact_selected.connect(self._on_contact_selected)
|
|
|
|
|
self._contact_list_widget.contact_double_clicked.connect(self._on_contact_double_clicked)
|
|
|
|
|
@ -404,17 +414,55 @@ class P2PChatGUI(QObject):
|
|
|
|
|
# TODO: 实现语音通话
|
|
|
|
|
|
|
|
|
|
def _on_contact_selected(self, user_id: str):
|
|
|
|
|
"""联系人选中回调"""
|
|
|
|
|
"""联系人选中回调 - 打开聊天窗口"""
|
|
|
|
|
self._current_chat_peer = user_id
|
|
|
|
|
logger.debug(f"Contact selected: {user_id}")
|
|
|
|
|
|
|
|
|
|
# 获取联系人信息并打开聊天窗口
|
|
|
|
|
if hasattr(self, '_contact_list_widget') and self._contact_list_widget:
|
|
|
|
|
peer_info = self._contact_list_widget.get_contact(user_id)
|
|
|
|
|
if hasattr(self, '_chat_widget') and self._chat_widget:
|
|
|
|
|
self._chat_widget.set_peer(user_id, peer_info)
|
|
|
|
|
self.main_window.set_current_chat_peer(user_id)
|
|
|
|
|
|
|
|
|
|
def _on_contact_double_clicked(self, user_id: str):
|
|
|
|
|
"""联系人双击回调 - 打开聊天"""
|
|
|
|
|
self._current_chat_peer = user_id
|
|
|
|
|
logger.info(f"Opening chat with: {user_id}")
|
|
|
|
|
# TODO: 打开聊天窗口
|
|
|
|
|
|
|
|
|
|
# 获取联系人信息并打开聊天窗口
|
|
|
|
|
if hasattr(self, '_contact_list_widget') and self._contact_list_widget:
|
|
|
|
|
peer_info = self._contact_list_widget.get_contact(user_id)
|
|
|
|
|
if hasattr(self, '_chat_widget') and self._chat_widget:
|
|
|
|
|
self._chat_widget.set_peer(user_id, peer_info)
|
|
|
|
|
self.main_window.set_current_chat_peer(user_id)
|
|
|
|
|
|
|
|
|
|
self.main_window._statusbar.showMessage(f"与 {user_id} 聊天", 3000)
|
|
|
|
|
|
|
|
|
|
def _on_send_message(self, peer_id: str, content: str):
|
|
|
|
|
"""发送消息回调"""
|
|
|
|
|
if self.worker:
|
|
|
|
|
success = self.worker.send_message(peer_id, content)
|
|
|
|
|
if success:
|
|
|
|
|
# 添加消息到聊天窗口显示
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from shared.models import ChatMessage
|
|
|
|
|
|
|
|
|
|
msg = ChatMessage(
|
|
|
|
|
message_id=f"msg_{datetime.now().timestamp()}",
|
|
|
|
|
sender_id=self._current_user.user_id if self._current_user else "",
|
|
|
|
|
receiver_id=peer_id,
|
|
|
|
|
content=content,
|
|
|
|
|
timestamp=datetime.now(),
|
|
|
|
|
is_sent=True
|
|
|
|
|
)
|
|
|
|
|
if hasattr(self, '_chat_widget') and self._chat_widget:
|
|
|
|
|
self._chat_widget.add_message(msg, is_self=True)
|
|
|
|
|
|
|
|
|
|
logger.info(f"Message sent to {peer_id}")
|
|
|
|
|
else:
|
|
|
|
|
self.main_window._statusbar.showMessage("消息发送失败", 3000)
|
|
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
|
"""清理资源"""
|
|
|
|
|
logger.info("Cleaning up resources...")
|
|
|
|
|
|