-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChatWindow.cpp
202 lines (177 loc) · 6.54 KB
/
ChatWindow.cpp
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
#include "ChatWindow.h"
#include "ui_ChatWindow.h"
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QTableWidget>
#include <QMessageBox>
#include <QStringList>
ChatWindow::ChatWindow(QJsonObject object, QTcpSocket *socket, QString name, QWidget *parent, bool isGroupChat) :
ChatWindow(socket, name, parent, isGroupChat)
{
this->ui->textBrowser->append(object["msg"].toString());
if (isGroupChat) {
QString users = "";
for (auto i : object["userlist"].toArray()) {
users += i.toString() + '\n';
}
this->ui->userListTextBrowser->setText(users);
}
}
ChatWindow::ChatWindow(QTcpSocket *socket, QString name, QWidget *parent, bool isGroupChat) :
QMainWindow(parent),
ui(new Ui::ChatWindow),
isGroupChat(isGroupChat),
chatId(name),
socket(socket)
{
this->ui->setupUi(this);
if (!isGroupChat) {
this->ui->userListTextBrowser->hide();
this->ui->exitButton->hide();
this->setWindowTitle(name);
}
else {
this->setWindowTitle("Group chat #" + name);
}
this->ui->enterLineEdit->setFocus();
connect(this->ui->enterButton, SIGNAL(clicked(bool)), this, SLOT(sendMessage()));
connect(this->ui->exitButton, SIGNAL(clicked(bool)), this, SLOT(leaveChatRoom()));
connect(this->ui->addUserToChatButton, SIGNAL(clicked(bool)), this, SLOT(addFriendInChatRoom()));
connect(this->ui->enterLineEdit, SIGNAL(returnPressed()), this, SLOT(sendMessage()));
}
ChatWindow::~ChatWindow() {
delete ui;
}
void ChatWindow::update(QJsonObject object){
if (object["connection"] == "0022") {
QString users = "";
for (auto i : object["userlist"].toArray()) {
users += i.toString() + '\n';
}
this->ui->userListTextBrowser->setText(users);
}
this->ui->textBrowser->append(object["msg"].toString());
if (!this->isActiveWindow()) {
((MainWindow*)this->parent())->snd->setObjectName("message");
}
}
void ChatWindow::update(QString message){
this->ui->textBrowser->append(message);
if (this->isVisible() && !this->isActiveWindow()) {
((MainWindow*)this->parent())->snd->setObjectName("message");
}
}
void ChatWindow::sendMessage(){
if (!this->ui->enterLineEdit->text().isEmpty()) {
QJsonObject object;
QJsonDocument document;
QByteArray packet;
object["connection"] = "0023";
object["chatid"] = this->chatId;
object["isprivate"] = !this->isGroupChat;
object["msg"] = this->ui->enterLineEdit->text();
document.setObject(object);
packet = document.toJson(QJsonDocument::Compact);
this->socket->write(packet);
this->socket->flush();
this->ui->enterLineEdit->clear();
}
}
void ChatWindow::leaveChatRoom() {
QJsonObject object;
QJsonDocument document;
QByteArray packet;
object["connection"] = "0025";
object["chatid"] = this->chatId;
document.setObject(object);
packet = document.toJson(QJsonDocument::Compact);
this->socket->write(packet);
this->socket->flush();
((MainWindow*)this->parent())->groupChatMap.remove(this->chatId);
this->destroy();
}
void ChatWindow::addFriendInChatRoom(){
QString username = this->ui->enterLineEdit->text();
if (username.isEmpty()) {
QMessageBox msgBox;
msgBox.setText("In order to add friend to this chat, you have to enter their username in message field!");
msgBox.exec();
}
else {
QRegularExpression delimiter("\\s*,\\s*|\\s+");
QStringList listOfCurrentUsersInChat;
if (this->isGroupChat) {
listOfCurrentUsersInChat = this->ui->userListTextBrowser->toPlainText().split('\n', QString::SplitBehavior::SkipEmptyParts);
}
else {
listOfCurrentUsersInChat += { this->chatId, ((MainWindow*)this->parent())->username };
}
QStringList usernameList = this->ui->enterLineEdit->text().trimmed().split(delimiter, QString::SplitBehavior::SkipEmptyParts);
QJsonArray usernameJsonArray;
QTableWidget* friendsListTable = ((MainWindow*)this->parent())->ui->tableWidget;
int numRows = friendsListTable->rowCount();
int numOfUsernames = usernameList.length();
for (int i=0; i<numOfUsernames; i++) {
QString username = usernameList.at(i);
bool notInFriendList = true;
bool friendOffline = true;
if (listOfCurrentUsersInChat.contains(username)) {
if (this->isGroupChat) {
QMessageBox msgBox;
msgBox.setText("User(s) you've tried to add is/are already in chatroom");
msgBox.exec();
return;
}
else {
continue;
}
}
for (int i=0; i < numRows; i++) {
if (friendsListTable->item(i, 0)->text() == username) {
notInFriendList = false;
if (friendsListTable->item(i, 1)->text() != "Offline") {
friendOffline = false;
}
break;
}
}
if (notInFriendList) {
QMessageBox msgbox;
msgbox.setText("User(s) you've tried to add is/are not in your friends list!");
msgbox.exec();
return;
}
else if (friendOffline) {
QMessageBox msgbox;
msgbox.setText("User(s) you've tried to add is/are currently offline!");
msgbox.exec();
return;
}
usernameJsonArray.push_back(username);
}
if (usernameJsonArray.size() == 0) {
QMessageBox msgBox;
msgBox.setText("Except you and current interlocutor, there has to be at least one more participant!");
msgBox.exec();
return;
}
QJsonObject object;
QJsonDocument document;
QByteArray packet;
if (this->isGroupChat) {
object["connection"] = "0024";
object["chatid"] = this->chatId;
object["userlist"] = usernameJsonArray;
}
else {
object["connection"] = "0021";
usernameJsonArray.push_back(this->chatId);
object["userlist"] = usernameJsonArray;
}
document.setObject(object);
packet = document.toJson(QJsonDocument::Compact);
this->socket->write(packet);
this->socket->flush();
this->ui->enterLineEdit->clear();
}
}