-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.cpp
More file actions
431 lines (373 loc) · 12.6 KB
/
session.cpp
File metadata and controls
431 lines (373 loc) · 12.6 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "session.h"
#include "ui_session.h"
#include "setting.h"
#include <QDebug>
QWebSocket *socket = new QWebSocket();
Session::Session(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Session)
{
ui->setupUi(this);
/* 创建信号槽连接 */
connect(socket,&QWebSocket::disconnected,this,&Session::socketDisconnected,Qt::AutoConnection);
connect(socket,&QWebSocket::connected,this,&Session::socketConnected,Qt::AutoConnection);
connect(socket,&QWebSocket::textMessageReceived,this,&Session::socketTextMessageReceived,Qt::AutoConnection);
/* 加载Web框架资源 */
QFileInfo *index = new QFileInfo("./html/index.html");
ui->WebDisplay->setUrl(QUrl("file:///"+index->absoluteFilePath()));
ui->WebDisplay->page()->settings()->setAttribute(QWebEngineSettings::ShowScrollBars,false);
// qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "9223");
// qDebug()<<"file:///"+index->absoluteFilePath();
/* 创建Web连接桥 */
bridge = new class bridge(this);
QWebChannel *channel = new QWebChannel(this);
channel->registerObject("bridge", (QObject*)bridge);
ui->WebDisplay->page()->setWebChannel(channel);
}
Session::~Session()
{
delete ui;
}
void Session::file()
{
ui->WebDisplay->page()->runJavaScript("newMessage(unescape('%5B%u6587%u4EF6%u4F20%u8F93%5D%20%u529F%u80FD%u5DF2%u9501%u5B9A'),0)");
}
void Session::init(QString server,QString nick,QString avatar,QString room,QString styleColor,QString serverType,QString loginMessage)
{
if(!server.isNull()) {
this->server = server;
}
if(!nick.isNull()) {
if(nick.contains("#")){
this->nick = nick.mid(0,nick.indexOf("#"));
qDebug()<<this->nick;
}else {
this->nick = nick;
}
}
this->avatar = avatar;
if(!room.isNull()){
this->room = room;
}
this->styleColor = styleColor;
this->serverType = serverType;
this->setWindowTitle(tr("SChat") + " | "+ tr("频道:")+room);
ui->main_title->setText(room);
ui->info_widget->hide();
ui->tabWidget->tabBar()->close();
//登陆成功,解析登陆成功信息
Q_UNUSED(loginMessage);
if(!loginMessage.isNull()){
onlineSet(loginMessage);
}
this->updateUserSizeShow();
on_lineEdit_textChanged("");
this->resize(900,600);
loadStyleColor(this->styleColor);
}
void Session::at(QString nick, QString msg)
{
Q_UNUSED(msg);
QString message = ui->message_edit->text();
if(message.contains("@"+nick)){
return;
}
ui->message_edit->setText("@"+nick+" "+message);
}
void Session::initWeb()
{
if(avatar.isNull() || avatar.isEmpty()) {
ui->WebDisplay->page()->runJavaScript("init('"+server+"','"+nick+"','"+this->serverType+"','"+this->styleColor+"',\"14\",\"sans\")");
}else {
ui->WebDisplay->page()->runJavaScript("init('"+server+"','"+nick+"','"+this->serverType+"','"+this->styleColor+"','"+avatar+"')");
}
ui->WebDisplay->page()->runJavaScript("systemDate()");
// ui->WebDisplay->page()->runJavaScript("userMessage(messageType.Other,unescape('%u770B%u89C1%u597D%u5904%u5C31%u8981%u62A2'),unescape('%u96F7%u72EE'),'../icon/leishi.jpg');");
// QString user = "雷狮";
// QString message = "测试消息";
// ui->WebDisplay->page()->runJavaScript("userMessage(messageType.Other,'"+message+"','"+user+"','../icon/leishi.jpg');");
}
void Session::sendMessage(QString message)
{
QJsonObject *sendmessage = new QJsonObject();
sendmessage->insert("cmd","chat");
sendmessage->insert("text",message);
QJsonDocument jsonDoc;
jsonDoc.setObject(*sendmessage);
socket->sendTextMessage(jsonDoc.toJson());
}
void Session::closeEvent(QCloseEvent *event)
{
socket->abort();
ui->WebDisplay->close();
event->accept();
}
void Session::paintEvent(QPaintEvent *event)
{
int webDisplayMinWidth = 600;
//隐藏用户栏
if(ui->WebDisplay->width() < webDisplayMinWidth && !ui->user_widget->isHidden()){
ui->user_widget->hide();
}
if(ui->WebDisplay->width() < webDisplayMinWidth && !ui->info_widget->isHidden()) {
ui->info_widget->hide();
}
if(ui->WebDisplay->width() > (webDisplayMinWidth+ui->user_widget->width()) && ui->user_widget->isHidden()){
ui->user_widget->show();
}
event->accept();
}
void Session::loadStyleColor(QString color)
{
if(color == "default"){
color = "63, 188, 242";
}else if (color == "darkblue") {
color = "0, 85, 127";
}else if (color == "black") {
color = "0, 0, 0";
}else if (color == "pink") {
color = "251, 114, 153";
}else {
return;
}
if(color != "63, 188, 242"){
ui->listWidget->setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba("+color+", 255), stop:1 rgba("+color+", 200));\nborder:0;\ncolor: rgb(255, 255, 255);");
ui->lineEdit->setStyleSheet("QLineEdit{\nborder-radius: 4px;\nbackground-color: rgb(247, 247, 247);\ncolor: rgb(0, 0, 0);\noutline: none;\nborder: none;\nselection-background-color: rgb("+color+");\n}\nQLineEdit:focus{\nbackground-color: rgb(255, 255, 255);\nborder: 2px solid rgb("+color+");\n}");
}
ui->message_edit->setStyleSheet("QLineEdit{\nbackground-color: rgb(255, 255, 255,0);\ncolor: rgb(0, 0, 0);\noutline: none;\nborder: none;\nborder-bottom: 2px solid rgb(100, 100, 100);\nselection-background-color: rgb("+color+");\n}\nQLineEdit:hover{\nborder-bottom: 2px solid rgb(20, 20, 20);\n}\nQLineEdit:focus{\nborder-bottom: 2px solid rgb("+color+");\n}");
}
void Session::systemMessage(QString message)
{
ui->WebDisplay->page()->runJavaScript("systemMessage("+message+")");
}
void Session::myMessage(QString message)
{
QString js = "userMessage(1,\""+message.replace("\"",""")+"\",'"+nick+"'";
if(!avatar.isNull() && !avatar.isEmpty()){
js += ",'../icon/"+avatar+"'";
}
js += ");";
ui->WebDisplay->page()->runJavaScript(js);
}
void Session::otherMessage(QString message, QString otherNick, QString otherAvatar)
{
QString js = "userMessage(2,'"+message.replace("\"",""")+"','"+otherNick+"'";
if(!otherAvatar.isNull() && !otherAvatar.isEmpty()){
js += ",'../icon/"+otherAvatar+"'";
}
js += ");";
ui->WebDisplay->page()->runJavaScript(js);
}
void Session::onlineSet(QString json)
{
QJsonDocument jsonDoc = QJsonDocument::fromJson(json.toUtf8());
QJsonObject response = jsonDoc.object();
//验证数据合法性
if(response["cmd"].toString() != "onlineSet") {
return;
}
//开始解析全部信息
QJsonArray users = response["users"].toArray();
for(QJsonValueRef userRef:users) {
QJsonObject user = userRef.toObject();
qDebug()<<user;
User *thisUser = new User();
thisUser->id = QString::number(user.take("userid").toDouble(),'f',0);
thisUser->nick = user["nick"].toString();
thisUser->trip = user["trip"].toString();
thisUser->utype = user["utype"].toString();
thisUser->hash = user["hash"].toString();
thisUser->isMe = user["isme"].toBool(false);
this->users.append(*thisUser);
}
//刷新联系人页面
on_lineEdit_textChanged("");
}
void Session::addUserToList(QJsonObject json)
{
User *user = new User();
user->nick = json["nick"].toString();
user->trip = json["trip"].toString();
user->utype = json["utype"].toString();
user->hash = json["hash"].toString();
user->id = json["userid"].toString();
user->isMe = false;
users.append(*user);
on_lineEdit_textChanged("");
}
void Session::removeUserByNick(QString nick)
{
for(int i = 0; i < users.length() ;i++){
if(users.at(i).nick == nick){
users.removeAt(i);
on_lineEdit_textChanged("");
return;
}
}
}
User Session::getUserByNick(QString nick)
{
if(nick == nullptr) return User();
for(int i = 0; i < users.length() ;i++){
if(users.at(i).nick == nick){
return users.at(i);
}
}
return User();
}
void Session::updateUserSizeShow()
{
ui->main_label->setText(QString::number(users.size())+" Users");
}
void Session::userInfoWidget(QString nick)
{
UserInfo *info = new UserInfo();
User user = getUserByNick(nick);
info->init(avatar,server,room,users.size());
info->initUser(user.nick,user.hash,user.id,user.utype);
info->show();
}
void Session::socketConnected()
{
systemMessage("unescape('%u6210%u529F%u4E0E%u670D%u52A1%u5668%u5EFA%u7ACB%u8FDE%u63A5')");
}
void Session::socketDisconnected()
{
systemMessage("unescape('%u4E22%u5931%u4E0E%u670D%u52A1%u5668%u7684%u8FDE%u63A5')");
}
void Session::socketTextMessageReceived(QString message)
{
QStringList cmdList;
cmdList<<"chat"
<<"info"
<<"onlineSet"
<<"onlineAdd"
<<"onlineRemove";
QJsonDocument jsonDoc = QJsonDocument::fromJson(message.toUtf8());
QJsonObject response = jsonDoc.object();
switch (cmdList.indexOf(response["cmd"].toString())) {
case 0:
/*Chat*/
if(response["nick"].toString() == nick) {
myMessage(response["text"].toString());
}else {
otherMessage(response["text"].toString(),response["nick"].toString(),NULL);
}
return;
case 1:
/*Info*/
systemMessage("'"+response["text"].toString()+"'");
return;
case 2:
/*OnlineSet*/
onlineSet(message);
return;
case 3:
/*onlineAdd*/
systemMessage(tr("'用户[")+response["nick"].toString()+tr("]加入了聊天室'"));
addUserToList(response);
return;
case 4:
/*onlineRemove*/
systemMessage(tr("'用户[")+response["nick"].toString()+tr("]离开了聊天室'"));
removeUserByNick(response["nick"].toString());
return;
default:
qDebug()<<message;
}
}
void Session::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
//** 将被设计为双击打开个人详情页 **//
userInfoWidget(item->text());
}
void Session::on_lineEdit_textChanged(const QString &arg1)
{
ui->listWidget->clear();
for(User user:users) {
if(user.nick.contains(arg1)){
QListWidgetItem *item = new QListWidgetItem();
item->setText(user.nick);
if(user.nick == this->nick){
if(this->avatar.isEmpty()){
item->setIcon(QIcon(":/ico/unknown"));
}else {
item->setIcon(QIcon("./icon/"+this->avatar));
}
}else if(user.icon != NULL) {
item->setIcon(QIcon("./icon/"+user.icon));
}else {
item->setIcon(QIcon(":/ico/unknown"));
}
ui->listWidget->addItem(item);
}
}
}
void Session::on_actionempty_triggered()
{
QMessageBox::StandardButton button;
button=QMessageBox::question(this,tr("提示"),QString(tr("是否 [清空] 当前页面上的记录? \n未保存的记录无法找回")),QMessageBox::Yes|QMessageBox::No);
if(button==QMessageBox::Yes){
ui->WebDisplay->page()->runJavaScript("empty()");
}
}
void Session::on_actionabout_triggered()
{
//关于 "SChat"
About *h=new About();
//阻塞其他页面,防止多个关于界面
h->setWindowModality(Qt::ApplicationModal);
h->show();
}
void Session::on_send_btn_clicked()
{
QString message = ui->message_edit->text();
if(message.isEmpty()) {
return;
}
sendMessage(message);
ui->message_edit->clear();
}
void Session::on_message_edit_returnPressed()
{
on_send_btn_clicked();
}
void Session::on_actionsystem_triggered()
{
Setting *s = new Setting();
s->show();
}
void Session::on_actionreset_triggered()
{
Join *j = new Join();
this->close();
j->show();
}
void Session::on_actionaccount_triggered()
{
if(serverType=="hack.chat"){
QMessageBox::information(this,"Information","Hack.Chat Server no account control function.");
return;
}
}
void Session::on_enclosure_btn_clicked()
{
if(serverType=="hack.chat"){
QMessageBox::information(this,"Information","Hack.Chat Server no Document service function.");
return;
}
}
void Session::on_pushButton_clicked()
{
// if(serverType=="hack.chat"){
// QMessageBox::information(this,"Information","Hack.Chat Server no Stickers service function.");
// return;
// }
if(ui->info_widget->isHidden()){
this->resize(this->width()+ui->info_widget->width(),this->height());
ui->info_widget->show();
}else{
ui->info_widget->hide();
this->resize(this->width()-ui->info_widget->width(),this->height());
}
}