-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
405 lines (343 loc) · 14.1 KB
/
mainwindow.cpp
File metadata and controls
405 lines (343 loc) · 14.1 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "database.h"
void MainWindow::setupDatabase() {
QString dbPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/user_credentials.db";
QDir().mkpath(QFileInfo(dbPath).path()); // Ensure directory exists
QSqlDatabase db;
if (QSqlDatabase::contains("qt_sql_default_connection")) {
db = QSqlDatabase::database("qt_sql_default_connection");
} else {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbPath);
}
if (!db.open()) {
qDebug() << "Error: Unable to open the database!" << db.lastError().text();
} else {
qDebug() << "Database opened successfully at" << dbPath;
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->msgData->setFocus();
// Implementation of server
TCPServer = new QTcpServer(this);
if (TCPServer->listen(QHostAddress::Any, 8080)) {
connect(TCPServer, &QTcpServer::newConnection, this, &MainWindow::newConnection);
qDebug() << "Server started successfully";
} else {
qDebug() << "Server failed to start";
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers()==Qt::ShiftModifier) {
ui->sendMsgButton->click();
}
else{
QMainWindow::keyPressEvent(event);
}
}
MainWindow::~MainWindow()
{
TCPServer->close();
for (QTcpSocket *client : clientConnectionList) {
client->disconnectFromHost();
}
delete ui;
}
void MainWindow::appendMessage(const QString &message, bool isSentByUser) {
// Define the alignment style based on the sender
QString alignmentStyle = isSentByUser ? "text-align: right;" : "text-align: left;";
// Wrap the message in a <div> tag (instead of <p>) for better block-level formatting
QString html = QString(
"<div style='%1; margin: 10px; padding: 5px; display: block; max-width: 40%; word-wrap: break-word;'>"
"%2"
"<div style='height: 10px;'><br></div>"
"</div>").arg(alignmentStyle).arg(message);
// Append the styled message to QTextEdit using insertHtml to avoid append overwriting
ui->chatHistory->insertHtml(html); // Use insertHtml for better HTML control
}
void MainWindow::appendMessage(const QString &message, const QString &fileName, bool isSentByUser) {
QFileInfo fileInfo(fileName);
QString fileExtension = fileInfo.suffix().toLower();
QString iconPath;
// Determine the icon based on the file extension
if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "png") {
iconPath = ":/image.png";
} else if (fileExtension == "pdf") {
iconPath = ":/pdf.png";
} else if (fileExtension == "txt") {
iconPath = ":/text.png";
} else {
iconPath = ":/unknown.png"; // Fallback icon for unsupported extensions
}
// Define the alignment style based on the sender
QString alignmentStyle = isSentByUser ? "text-align: right;" : "text-align: left;";
// Construct the HTML block for the file attachment inside a <div> tag for proper block-level formatting
QString fileHtml = QString(
"<div style='%1; margin: 10px; padding: 5px; display: block;'>"
" <img src='%2' width='30' height='30'/>"
" <br>" // Add a break to separate the icon and the message text
" %3"
"<div style='height: 10px;'><br></div>"
"</div>").arg(alignmentStyle, iconPath, message);
// Append the message and file attachment to QTextEdit using insertHtml
ui->chatHistory->insertHtml(fileHtml); // Use insertHtml for better formatting
}
void MainWindow::appendMessage(const QString &message) {
ui->chatHistory->append(message);
}
QString MainWindow::formatMessage(const QString &sender, const QString &message) {
return sender + ": " + message;
}
void MainWindow::on_sendMsgButton_clicked()
{
ui->attachFile->show();
QString writeData;
// Check if a file is attached
if (!selectedFilePath.isEmpty()) {
// Send the file to all recipients
if (ui->recipeintType->currentText() == "Everyone") {
foreach (QTcpSocket *socket, clientConnectionList) {
sendFile(socket, selectedFilePath);
}
} else if (ui->recipeintType->currentText() == "Private") {
QString receiverId = ui->clientsList->currentText();
foreach (QTcpSocket *socket, clientConnectionList) {
if (socket->socketDescriptor() == receiverId.toLongLong()) {
sendFile(socket, selectedFilePath);
}
}
}
// Clear the attachment after sending
QFileInfo fileInfo(selectedFilePath);
QString fileName = fileInfo.fileName();
writeData=ui->msgData->toPlainText();
QString fileData=ui->fileHold->toPlainText();
appendMessage(fileData,fileName,true);
if(!writeData.isEmpty()){
appendMessage(writeData,true); // Indicate the file has been sent
}
selectedFilePath.clear(); // Reset the file path
}
else{
if (!TCPSocket || !TCPSocket->isOpen()) {
QMessageBox::information(this, "Error", TCPSocket ? TCPSocket->errorString() : "Socket not initialized.");
return;
}
writeData =ui->msgData->toPlainText(); // Use toPlainText() for QTextEdit
TCPSocket->write(writeData.toUtf8());
appendMessage(writeData, true);
QString messageForClient = ui->msgData->toPlainText(); // Use toPlainText() for QTextEdit
QString receiver = ui->clientsList->currentText();
if (ui->recipeintType->currentText() == "Everyone") {
for (QTcpSocket *socket : clientConnectionList) {
socket->write(messageForClient.toUtf8());
}
} else if (ui->recipeintType->currentText() == "Private") {
for (QTcpSocket *socket : clientConnectionList) {
if (socket->socketDescriptor() == receiver.toLongLong()) {
socket->write(messageForClient.toUtf8());
break;
}
}
}
}
ui->fileHold->clear();
ui->msgData->clear(); // Use clear() for QTextEdit
}
void MainWindow::readDataFromServer()
{
if (TCPSocket && TCPSocket->isOpen()) {
QByteArray Data_From_Server = TCPSocket->readAll();
QString serverMsg = QString::fromUtf8(Data_From_Server);
if (serverMsg.contains("filename")) {
// If the data starts with "FILE:", it's a file transfer, so call readFileSocket
readFileSocket(Data_From_Server);
}
else {
qDebug() << "Received non-file data:" << serverMsg;
appendMessage(serverMsg,false);
}
}
}
void MainWindow::readTextSocket()
{
QTcpSocket *socket = reinterpret_cast<QTcpSocket*>(sender());
QByteArray messageFromServer = socket->readAll();
QString message = "Client " + QString::number(socket->socketDescriptor()) + " :: " + QString::fromUtf8(messageFromServer);
appendMessage(message,false);
}
void MainWindow::readFileSocket(const QByteArray &dataBuffer)
{
QByteArray fileData = dataBuffer;
// Ensure data is at least 5 bytes for "FILE:" prefix removal
if (fileData.size() < 5) {
qDebug() << "Invalid data size received.";
return;
}
fileData.remove(0, 5); // Remove the "FILE:" prefix
// Validate and parse the header data (128 bytes assumed)
if (fileData.size() < 128) {
qDebug() << "Header size is less than expected, aborting.";
return;
}
QString headerData = QString::fromUtf8(fileData.mid(0, 128)); // Read header as UTF-8 string
if (!headerData.contains(",") || !headerData.contains(":")) {
qDebug() << "Header format is invalid, aborting.";
return;
}
QString fileName = headerData.split(",")[0].split(":")[1].trimmed(); // Extract file name
QString fileSizeStr = headerData.split(",")[1].split(":")[1].trimmed(); // Extract file size
bool isFileSizeValid;
qint64 fileSize = fileSizeStr.toLongLong(&isFileSizeValid);
if (!isFileSizeValid || fileSize <= 0 || fileSize > 10 * 1024 * 1024) { // Validate file size
qDebug() << "Invalid file size, aborting.";
return;
}
// Show file save dialog to select custom path
QString selectedDir = QFileDialog::getExistingDirectory(
this,
tr("Select Save Directory"),
QCoreApplication::applicationDirPath(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
qDebug() << "this is where dialog is shown";
if (selectedDir.isEmpty()) {
qDebug() << "No directory selected. File saving aborted.";
return;
}
QString saveFilePath = selectedDir + "/" + fileName;
QFileInfo fileInfo(saveFilePath);
qDebug() << saveFilePath;
// Handle potential file overwriting
if (fileInfo.exists()) {
qDebug() << "File with the same name already exists. Appending unique ID.";
QString uniqueSuffix = QString::number(QDateTime::currentMSecsSinceEpoch());
saveFilePath = selectedDir + "/" + fileInfo.baseName() + "_" + uniqueSuffix + "." + fileInfo.suffix();
}
// Remove header data and handle the actual file content
fileData = fileData.mid(128); // Skip the header
QFile file(saveFilePath);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open file for writing. Path:" << saveFilePath;
return;
}
if (file.write(fileData) != fileData.size()) { // Write data and validate
qDebug() << "Error occurred while writing the file.";
} else {
qDebug() << "File saved successfully at:" << saveFilePath;
}
file.close();
}
void MainWindow::newConnection()
{
while (TCPServer->hasPendingConnections()) {
QTcpSocket *newClient = TCPServer->nextPendingConnection();
connect(newClient, &QTcpSocket::readyRead, this, &MainWindow::readTextSocket);
connect(newClient, &QTcpSocket::disconnected, this, [=] { removeClient(newClient); });
clientConnectionList.append(newClient);
ui->clientsList->addItem(QString::number(newClient->socketDescriptor()));
appendMessage("Client " + QString::number(newClient->socketDescriptor()) + " connected");
}
}
void MainWindow::removeClient(QTcpSocket *socket)
{
clientConnectionList.removeAll(socket);
ui->clientsList->clear(); // Refresh clients list
for (QTcpSocket *client : clientConnectionList) {
ui->clientsList->addItem(QString::number(client->socketDescriptor()));
}
socket->deleteLater();
appendMessage(QString("Client %1 disconnected").arg(socket->socketDescriptor()));
}
void MainWindow::sendFile(QTcpSocket *socket, QString fileName)
{
if(socket){
if(socket->isOpen()){
QFile fileData(fileName);
if(fileData.open(QIODevice::ReadOnly)){
QFileInfo fileInfo(fileData);
QString fileNameExt(fileInfo.fileName());
QDataStream socketStream(socket);
socketStream.setVersion(QDataStream::Qt_5_15);
QByteArray header;
header.prepend(("filename:"+fileNameExt.toUtf8()+",filesize:"+QString::number(fileData.size()).toUtf8()));
header.resize(128);
QByteArray byteFileData=fileData.readAll();
byteFileData.prepend(header);
socketStream<<byteFileData;
}
else{
qDebug()<<"File couldn't be opened!";
}
}
else{
qDebug()<<"Client socket couldn't be opened!";
}
}
else{
qDebug()<<"Client socket invalid!";
}
}
void MainWindow::on_attachFile_clicked()
{
ui->attachFile->hide();
// Browse for a file
QString filePath = QFileDialog::getOpenFileName(this, "Select File", QCoreApplication::applicationDirPath(), "File (*.jpg *.png *.txt *.doc *.pdf)");
// Check if the user selected a file
if (!filePath.isEmpty()) {
selectedFilePath = filePath;
// Extract file name
QFileInfo fileInfo(filePath);
QString fileName = fileInfo.fileName();
//map file to an icon
QString fileExtension = fileInfo.suffix().toLower();
QString iconPath;
if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "png") {
iconPath = ":/image.png";
} else if (fileExtension == "pdf") {
iconPath = ":/pdf.png";
} else if (fileExtension == "txt") {
iconPath = ":/text.png";
}
// HTML to display the icon and text in the message box
QString html = QString(
"<table>"
" <tr>"
" <td><img src='%1' width='25' height='25'></td>"
" <td style='vertical-align:middle;'>%2</td>"
" </tr>"
"</table>"
).arg(iconPath, fileName);
// Display the attachment in the message box
ui->fileHold->insertHtml(html);
//ui->msgData->insertHtml("<br>");
//for scrolling to bottom
/* QTextCursor cursor = ui->msgData->textCursor();
cursor.movePosition(QTextCursor::NextRow);
ui->msgData->setTextCursor(cursor);*/
}
else{
ui->attachFile->show();
}
}
void MainWindow::on_pushButton_clicked()
{
// List of IP addresses of other devices
QStringList deviceIPs = {"127.0.0.1"};
// Loop through each IP and attempt to connect
for (const QString &ip : deviceIPs) {
TCPSocket = new QTcpSocket(this);
TCPSocket->connectToHost(QHostAddress(ip), 8080);
connect(TCPSocket, &QTcpSocket::readyRead, this, &MainWindow::readDataFromServer);
if (TCPSocket->isOpen()) {
qDebug() << "Connected to" << ip;
} else {
qDebug() << "Couldn't connect to" << ip;
}
}
}