forked from UrFU-Programming/IsitMessenger
/
MainWindow.cpp
157 lines (129 loc) · 4.06 KB
/
MainWindow.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
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
#include "Server.hpp"
#include "ServerDiscovery.hpp"
#include "LocalClient.hpp"
GomokuWindow::GomokuWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_client(new LocalClient(this))
{
ui->setupUi(this);
connect(ui->createServer, SIGNAL(clicked(bool)),
this, SLOT(createServer()));
connect(ui->message, SIGNAL(returnPressed()),
this, SLOT(sendMessage()));
connect(ui->sendButton, SIGNAL(clicked(bool)),
this, SLOT(sendMessage()));
m_serverDiscovery = new ServerDiscovery(this);
m_server = new Server(this);
connect(m_server, SIGNAL(serverStarted(quint16)),
this, SLOT(addServerToDiscovery(quint16)));
connect(m_server, SIGNAL(messageReceived(QString,QString)),
this, SLOT(onServerMessageReceived(QString,QString)));
connect(m_serverDiscovery,SIGNAL(serverFound(QString,quint16)),
this, SLOT(addServer(QString,quint16)));
connect(m_client, SIGNAL(messageReceived(QString,QString)),
this, SLOT(addMessage(QString,QString)));
connect(m_client, SIGNAL(connected()),
this, SLOT(onClientConnected()));
connect(m_client, SIGNAL(participantsReceived(QList<int>,QStringList)),
this, SLOT(participantsOnReceived(QList<int>,QStringList)));
m_serverDiscovery->discoveryServer();
}
GomokuWindow::~GomokuWindow()
{
delete ui;
}
void GomokuWindow::connectToServer()
{
m_client->connectToServer(ui->serverListAddress->text(),
ui->serverListPort->value());
}
void GomokuWindow::addContact(QString name)
{
for(int i=0;i<ui->contacts->count();i++){
QListWidgetItem *item = ui->contacts->item(i);
if(item->text() == name){
return;
}
}
ui->contacts->addItem(name);
}
void GomokuWindow::addMessage(QString name, QString text)
{
if (text.startsWith("/me ")){
ui->messages->setFontItalic(true);
ui->messages->append ('*' + name + ' ' + text.section(' ', 1, -1));
return;
}
ui->messages->setFontItalic(false);
ui->messages->append(name+":"+text+"\n");
}
void GomokuWindow::addServer(QString address, quint16 port)
{
QString text = address+":"+QString::number(port);
for(int i=0;i<ui->serverListList->count();i++){
QListWidgetItem *item = ui->serverListList->item(i);
if(item->text() == text){
return;
}
}
ui->serverListList->addItem(text);
}
void GomokuWindow::addServerToDiscovery(quint16 port)
{
m_serverDiscovery->addServer(port);
}
void GomokuWindow::createServer()
{
m_server->startServer(ui->serverPort->value());
ui->createServer->setDisabled(true);
}
void GomokuWindow::participantsOnReceived(const QList<int> &ids, const QStringList &names)
{
ui->contacts->clear();
ui->contacts->addItems(names);
}
void GomokuWindow::onServerMessageReceived(QString from, QString message)
{
ui->serverLog->insertPlainText(from + ":" + message + "\n");
}
void GomokuWindow::on_serverListConnectButton_clicked()
{
connectToServer();
}
void GomokuWindow::on_serverListList_itemDoubleClicked(QListWidgetItem *item)
{
connectToServer();
}
void GomokuWindow::on_serverListList_itemClicked(QListWidgetItem *item)
{
ui->serverListAddress->setText(item->text().section(':',-2,-2));
ui->serverListPort->setValue(item->text().section(':',-1).toInt());
}
void GomokuWindow::on_serverListRefreshButton_clicked()
{
ui->serverListList->clear();
m_serverDiscovery->discoveryServer();
}
void GomokuWindow::sendMessage()
{
QString text = ui->message->text();
ui->message->clear();
if(text.startsWith("/nick ")){
m_client->setNickname(text.mid(6, -1));
} else {
m_client->sendMessage(text);
}
}
void GomokuWindow::onClientConnected()
{
ui->tabWidget->setCurrentIndex(1);
ui->messages->append("You connected to server \n");
}
void GomokuWindow::on_contacts_customContextMenuRequested(const QPoint &pos)
{
QMenu menu;
menu.exec(ui->contacts->mapToGlobal(pos));
}