-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
222 lines (173 loc) · 7.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import sys
from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QHBoxLayout, QTextEdit, QLineEdit, QWidget, QLabel, QMainWindow
from client.client import sio, User, reg_callback
from PySide6.QtCore import QObject, QThread, Signal
username = None
user = User(None)
target_user = None
# Create a class for handling Socket.IO client operations
class SocketIOClient(QObject):
message_received = Signal(str)
def __init__(self):
super().__init__()
def run(self):
def on_message(data):
print("pingi")
self.message_received.emit(data)
reg_callback(user, on_message)
sio.wait()
# Create a worker thread to run the Socket.IO client
class Worker(QThread):
def __init__(self):
super().__init__()
def run(self):
self.client = SocketIOClient()
self.client.message_received.connect(self.on_message_received)
self.client.run()
def on_message_received(self, message):
print("Received message:", message) # Do something with the received message, like updating the UI
if target_user != None:
mw.update_chat(user.messages[target_user])
# def send_message():
# message = input_field.text()
# if message:
# chat_history.append(f'<font color="red">{message}</font>')
# input_field.clear()
# Create the Qt Application
app = QApplication(sys.argv)
class LoginScreen(QWidget):
def __init__(self, parent=None):
super(LoginScreen, self).__init__(parent)
self.setWindowTitle("Login")
self.username_label = QLabel("Username:")
self.password_label = QLabel("Password:")
self.username_edit = QLineEdit()
self.password_edit = QLineEdit()
self.password_edit.setEchoMode(QLineEdit.Password)
self.login_button = QPushButton("Login")
self.login_button.clicked.connect(self.handle_login)
layout = QVBoxLayout()
layout.addWidget(self.username_label)
layout.addWidget(self.username_edit)
layout.addWidget(self.login_button)
self.setLayout(layout)
def handle_login(self):
global username
# Check username and password, just a simple check for demo
# if self.username_edit.text() == "user" and self.password_edit.text() == "password":
# self.parent().switch_to_chat_screen()
# else:
# print("Invalid username or password")
global user
username = self.username_edit.text()
user.username = username
if(user.register_user()):
self.parent().switch_to_select_screen()
class SelectScreen(QWidget):
def __init__(self, parent=None):
super(SelectScreen, self).__init__(parent)
self.setWindowTitle("Select")
# Create main layout
self.main_layout = QVBoxLayout()
self.main_layout.addWidget(QLabel(f"Hi {username}!"))
self.main_layout.addWidget(QLabel("<h2>Connect to:</h2>"))
# Refresh button
self.refresh_button = QPushButton("Refresh")
self.refresh_button.clicked.connect(self.refresh)
self.main_layout.addWidget(self.refresh_button)
# User buttons layout
self.user_layout = QVBoxLayout()
self.main_layout.addLayout(self.user_layout)
# Populate initial user list
self.refresh()
self.setLayout(self.main_layout)
def refresh(self):
# Clear previous user buttons
for i in reversed(range(self.user_layout.count())):
widget = self.user_layout.itemAt(i).widget()
if widget is not None:
widget.deleteLater()
# Retrieve users
users = sio.call('request_users')
print(users)
# Add buttons for each user
for u in users:
if u != username:
button = QPushButton(u)
button.clicked.connect(lambda _, name=u: self.user_clicked(name))
self.user_layout.addWidget(button)
def user_clicked(self, name):
global target_user
target_user = name
print(target_user)
if not user.is_connected(target_user):
user.request_user_prekey_bundle(target_user)
user.perform_x3dh(target_user)
print("connected")
self.parent().switch_to_chat_screen()
class ChatScreen(QWidget):
def __init__(self, parent=None):
super(ChatScreen, self).__init__(parent)
self.setWindowTitle("Chat")
# Create main layout
xlayout = QVBoxLayout()
# Create chat layout
chat_layout = QHBoxLayout()
# Add back button - assuming you want a back button to return to the login screen
back_button = QPushButton("Back")
back_button.clicked.connect(self.back_message)
toolbar_layout = QHBoxLayout()
xlayout.addLayout(toolbar_layout)
toolbar_layout.addWidget(back_button)
toolbar_layout.addWidget(QLabel(f"{username} -> {target_user}"))
# Add text area for chat history
self.chat_history = QTextEdit()
self.chat_history.setReadOnly(True)
xlayout.addWidget(self.chat_history)
# Add input field and send button for new message
self.input_field = QLineEdit()
send_button = QPushButton("Send")
send_button.clicked.connect(self.send_message)
chat_layout.addWidget(self.input_field)
chat_layout.addWidget(send_button)
xlayout.addLayout(chat_layout)
self.setLayout(xlayout)
self.update_messages(user.messages[target_user])
def send_message(self):
message = self.input_field.text()
if message:
self.input_field.clear()
print("sending")
user.send_message(target_user, message)
self.update_messages(user.messages[target_user])
def update_messages(self, messages):
self.chat_history.clear()
for sender, msg in messages:
# Set color based on sender
color = "green" if sender == username else "red"
# Append message with color
self.append_colored_text(sender, msg, color)
def back_message(self):
self.parent().switch_to_select_screen()
def append_colored_text(self, sender, msg, color):
self.chat_history.append(f"<font color='{color}'> <strong>{sender}:</strong> {msg}</font><br>")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.login_screen = LoginScreen(self)
self.setCentralWidget(self.login_screen)
def switch_to_chat_screen(self):
self.chat_screen = ChatScreen(self)
self.setCentralWidget(self.chat_screen)
def switch_to_select_screen(self):
self.select_screen = SelectScreen(self)
self.setCentralWidget(self.select_screen)
def update_chat(self, messages):
self.chat_screen.update_messages(messages)
# Create main window
mw = MainWindow()
mw.show()
worker_thread = Worker()
worker_thread.start()
# Run the main Qt loop
sys.exit(app.exec())