-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainWindow.cpp
191 lines (158 loc) · 5.12 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
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
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
#include "Server.hpp"
#include "ServerDiscovery.hpp"
#include "LocalClient.hpp"
#include <QDebug>
#include <QFileDialog>
MainWindow::MainWindow(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)));
connect(m_client, SIGNAL(tunneledMessageReceived(int,QByteArray)),
this, SLOT(onTunneledMessageReceived(int,QByteArray)));
m_serverDiscovery->discoveryServer();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectToServer()
{
m_client->connectToServer(ui->serverListAddress->text(),
ui->serverListPort->value());
}
void MainWindow::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 MainWindow::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 MainWindow::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 MainWindow::addServerToDiscovery(quint16 port)
{
m_serverDiscovery->addServer(port);
}
void MainWindow::createServer()
{
m_server->startServer(ui->serverPort->value());
ui->createServer->setDisabled(true);
}
void MainWindow::participantsOnReceived(const QList<int> &ids, const QStringList &names)
{
ui->contacts->clear();
//ui->contacts->addItems(names);
int counter = 0;
for (QString name : names) {
QListWidgetItem *newItem = new QListWidgetItem(name);
newItem->setData(Qt::UserRole,ids[counter]);
ui->contacts->addItem(newItem);
counter++;
}
}
void MainWindow::onTunneledMessageReceived(int idFrom, const QByteArray &message)
{
}
void MainWindow::onServerMessageReceived(QString from, QString message)
{
ui->serverLog->insertPlainText(from + ":" + message + "\n");
}
void MainWindow::on_serverListConnectButton_clicked()
{
connectToServer();
}
void MainWindow::on_serverListList_itemDoubleClicked(QListWidgetItem *item)
{
connectToServer();
}
void MainWindow::on_serverListList_itemClicked(QListWidgetItem *item)
{
ui->serverListAddress->setText(item->text().section(':',-2,-2));
ui->serverListPort->setValue(item->text().section(':',-1).toInt());
}
void MainWindow::on_serverListRefreshButton_clicked()
{
ui->serverListList->clear();
m_serverDiscovery->discoveryServer();
}
void MainWindow::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 MainWindow::onClientConnected()
{
ui->tabWidget->setCurrentIndex(1);
ui->messages->append("You connected to server \n");
}
void MainWindow::sendFile()
{
QFileDialog dialog;
if (dialog.exec()) {
QByteArray sep(";");
QFile file(dialog.selectedFiles()[0]);
file.open(QIODevice::ReadOnly);
QByteArray fileBytes(file.readAll());
for(QListWidgetItem *item : ui->contacts->selectedItems()) {
m_client->sendTunneledMessage(item->data(Qt::UserRole).toInt(),
QByteArray("File;")+QByteArray::number(file.size())+sep+file.fileName().section('/',-1).toUtf8()+sep+fileBytes);
}
}
}
void MainWindow::on_contacts_customContextMenuRequested(const QPoint &pos)
{
QMenu menu;
menu.addAction("Send file", this, SLOT(sendFile()));
menu.exec(ui->contacts->mapToGlobal(pos));
}