Skip to content

Commit 66e0890

Browse files
changed the way the json parser worked to allow for all of the languages included in utf-8.
1 parent 914ebd2 commit 66e0890

7 files changed

Lines changed: 485 additions & 45 deletions

File tree

.qtcreator/project.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://download.qt.io/official_releases/qtcreator/latest/installer_source/jsonschemas/project.json",
3+
"files.exclude": [
4+
".qtcreator/project.json.user"
5+
]
6+
}

.qtcreator/project.json.user

Lines changed: 358 additions & 0 deletions
Large diffs are not rendered by default.

filemessage.ui

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>581</width>
9+
<width>594</width>
1010
<height>119</height>
1111
</rect>
1212
</property>
13+
<property name="sizePolicy">
14+
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
15+
<horstretch>0</horstretch>
16+
<verstretch>0</verstretch>
17+
</sizepolicy>
18+
</property>
19+
<property name="minimumSize">
20+
<size>
21+
<width>594</width>
22+
<height>0</height>
23+
</size>
24+
</property>
1325
<property name="maximumSize">
1426
<size>
15-
<width>99999</width>
27+
<width>594</width>
1628
<height>99999</height>
1729
</size>
1830
</property>
@@ -22,6 +34,12 @@
2234
<layout class="QVBoxLayout" name="verticalLayout">
2335
<item>
2436
<widget class="QLabel" name="fileName">
37+
<property name="maximumSize">
38+
<size>
39+
<width>576</width>
40+
<height>16777215</height>
41+
</size>
42+
</property>
2543
<property name="text">
2644
<string>Empty file.txt</string>
2745
</property>
@@ -61,6 +79,9 @@
6179
</item>
6280
<item row="1" column="1">
6381
<widget class="QProgressBar" name="transferProgressBar">
82+
<property name="layoutDirection">
83+
<enum>Qt::LayoutDirection::LeftToRight</enum>
84+
</property>
6485
<property name="value">
6586
<number>50</number>
6687
</property>

mainwindow.cpp

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414
#include <QTimer>
1515
#include <QFileInfo>
1616

17-
QString get_data_string(QTcpSocket* socket)
17+
QByteArray get_data_string(QTcpSocket* socket)
1818
{
1919
int deadEndCounter = 0;
20-
QString output = "";
20+
QByteArray output;
2121

2222
while(deadEndCounter < 4096)
2323
{
2424
QByteArray data = socket->read(1);
2525
char c = reinterpret_cast<char>(data[0]);
2626
if (c == '\n') break;
2727

28-
output += c;
28+
output += data[0];
2929

3030
deadEndCounter++;
3131
}
@@ -49,10 +49,10 @@ QString formatByteSpeed(qint64 bytes)
4949
qint64 megaSize = 1047576;
5050
qint64 kilaSize = 1024;
5151

52-
if (bytes > terraSize) {speed = bytes / terraSize; byteIndex = 4;}
53-
else if (bytes > gigaSize) {speed = bytes / gigaSize; byteIndex = 3;}
54-
else if (bytes > megaSize) {speed = bytes / megaSize; byteIndex = 2;}
55-
else if (bytes > kilaSize) {speed = bytes / kilaSize; byteIndex = 1;}
52+
if (bytes > terraSize) {speed = (double)bytes / (double)terraSize; byteIndex = 4;}
53+
else if (bytes > gigaSize) {speed = (double)bytes / (double)gigaSize; byteIndex = 3;}
54+
else if (bytes > megaSize) {speed = (double)bytes / (double)megaSize; byteIndex = 2;}
55+
else if (bytes > kilaSize) {speed = (double)bytes / (double)kilaSize; byteIndex = 1;}
5656

5757
return QString::number(speed, 'f', 2) + " " + byteFormats[byteIndex];
5858
}
@@ -64,6 +64,10 @@ MainWindow::MainWindow(QWidget *parent)
6464
ui->setupUi(this);
6565
ui->RemoveFileButton->setVisible(false);
6666
socket = new QTcpSocket(this);
67+
connect(ui->MessageInput, &QLineEdit::returnPressed, ui->sendButton, &QPushButton::click);
68+
connect(ui->PairIDInput, &QLineEdit::returnPressed, ui->pairButton, &QPushButton::click);
69+
ui->MessageList->setWordWrap(true); // allow wrapping
70+
ui->MessageList->setResizeMode(QListView::Adjust); // items adjust width
6771

6872
updateTimer = new QTimer(this);
6973
connect(updateTimer, &QTimer::timeout, this, [this]() {
@@ -94,7 +98,7 @@ MainWindow::MainWindow(QWidget *parent)
9498
}
9599
case 1:{
96100
QJsonParseError parseError;
97-
QJsonDocument doc = QJsonDocument::fromJson(get_data_string(socket).toUtf8(), &parseError);
101+
QJsonDocument doc = QJsonDocument::fromJson(get_data_string(socket), &parseError);
98102

99103
if (parseError.error != QJsonParseError::NoError)
100104
{
@@ -189,6 +193,7 @@ MainWindow::MainWindow(QWidget *parent)
189193
newMessage->setText(obj["message"].toString());
190194

191195
item->setSizeHint(newMessage->sizeHint());
196+
192197
ui->MessageList->addItem(item);
193198
ui->MessageList->setItemWidget(item, newMessage);
194199
}
@@ -315,6 +320,8 @@ MainWindow::MainWindow(QWidget *parent)
315320

316321
ui->MessageList->clear();
317322

323+
sendingFile= false;
324+
318325
if (current_file) {
319326
if (current_file->isOpen())
320327
current_file->close();
@@ -417,74 +424,94 @@ void MainWindow::sendFile()
417424
{
418425
QFileInfo info(selectedFilePath);
419426

427+
// 1. Send metadata first
420428
QJsonObject jsonMessage;
421429
jsonMessage["type"] = "file_metadata";
422430
jsonMessage["file_name"] = info.fileName();
423431
jsonMessage["file_size"] = info.size();
424432
jsonMessage["to"] = pairPartnerId;
425433

426434
QJsonDocument doc(jsonMessage);
427-
QString jsonString = doc.toJson(QJsonDocument::Compact) + "\n";
435+
QString jsonString = doc.toJson(QJsonDocument::Compact) + '\n';
428436
socket->write(jsonString.toUtf8());
429437

438+
// 2. Setup state
430439
current_file_size = 0;
431440
current_total_file_size = info.size();
432441

433442
QListWidgetItem *item = new QListWidgetItem(ui->MessageList);
434443
currentFileMessage = new fileMessage;
435-
436444
currentFileMessage->setFileName(info.fileName());
437445
currentFileMessage->setToAndFromClient(QString::number(myId), QString::number(pairPartnerId));
438-
currentFileMessage->setStatus("Transfering");
446+
currentFileMessage->setStatus("Transferring");
439447
currentFileMessage->setProgress(0);
440448

441449
item->setSizeHint(currentFileMessage->sizeHint());
442450
ui->MessageList->addItem(item);
443451
ui->MessageList->setItemWidget(item, currentFileMessage);
444452

445-
current_file = new QFile;
446-
current_file->setFileName(selectedFilePath);
447-
453+
current_file = new QFile(selectedFilePath);
448454
if (!current_file->open(QIODevice::ReadOnly)) {
449-
QMessageBox::warning(this, "Error", "Could not open file for writing!");
455+
QMessageBox::warning(this, "Error", "Could not open file for reading!");
456+
delete current_file;
457+
current_file = nullptr;
450458
return;
451459
}
452460

453461
updateTimer->start(1000);
462+
sendingFile = true;
454463

455-
while (!current_file->atEnd()) {
456-
QByteArray bytes = current_file->read(1024);
457-
current_file_size += bytes.size();
464+
// 3. Connect socket signal → chunk sender
465+
uploadConn = connect(socket, &QTcpSocket::bytesWritten,
466+
this, &MainWindow::sendFileChunk);
458467

459-
qint64 written = socket->write(bytes);
460-
if (written == -1) {
461-
QMessageBox::warning(this, "Error", "Failed to write to socket!");
462-
break;
463-
}
468+
// 4. Kick off first chunk immediately
469+
sendFileChunk();
470+
}
464471

465-
// Block until the OS actually flushes this chunk
466-
if (!socket->waitForBytesWritten(-1)) {
467-
QMessageBox::warning(this, "Error", "Failed to send data chunk!");
468-
break;
469-
}
472+
void MainWindow::sendFileChunk()
473+
{
474+
if (!sendingFile || !current_file) return;
470475

471-
double progress = (double)current_file_size / (double)current_total_file_size * 100.0;
472-
static int lastProgress = -1;
473-
int intProgress = static_cast<int>(progress);
476+
const int chunkSize = 1024;
477+
QByteArray bytes = current_file->read(chunkSize);
474478

475-
if (intProgress != lastProgress) {
476-
currentFileMessage->setProgress(intProgress);
477-
lastProgress = intProgress;
478-
}
479-
}
480-
481-
updateTimer->stop();
479+
if (bytes.isEmpty()) {
480+
// === Finished ===
481+
sendingFile = false;
482+
updateTimer->stop();
482483

483-
if (current_file) {
484484
if (current_file->isOpen())
485485
current_file->close();
486486
delete current_file;
487487
current_file = nullptr;
488+
489+
// Disconnect so no future writes trigger us
490+
disconnect(uploadConn);
491+
492+
currentFileMessage->setStatus("File Transferred");
493+
currentFileMessage->setProgress(100);
494+
return;
495+
}
496+
497+
// Write chunk
498+
qint64 written = socket->write(bytes);
499+
if (written == -1) {
500+
QMessageBox::warning(this, "Error", "Failed to write to socket!");
501+
return;
502+
}
503+
504+
current_file_size += bytes.size();
505+
bytesReceivedThisSecond += bytes.size();
506+
507+
// Update progress
508+
double progress = (double)current_file_size / (double)current_total_file_size * 100.0;
509+
static int lastProgress = -1;
510+
int intProgress = static_cast<int>(progress);
511+
512+
if (intProgress != lastProgress) {
513+
currentFileMessage->setProgress(intProgress);
514+
lastProgress = intProgress;
488515
}
489516
}
490517

@@ -496,7 +523,7 @@ void MainWindow::sendMessage(const QString messageToSend)
496523
jsonMessage["to"] = pairPartnerId;
497524

498525
QJsonDocument doc(jsonMessage);
499-
QString jsonString = doc.toJson(QJsonDocument::Compact) + "\n";
526+
QString jsonString = doc.toJson(QJsonDocument::Compact) + '\n';
500527
socket->write(jsonString.toUtf8());
501528

502529
QListWidgetItem *item = new QListWidgetItem(ui->MessageList);

mainwindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class MainWindow : public QMainWindow
4444

4545
int flushIndex = 0;
4646

47+
bool sendingFile = false;
48+
QMetaObject::Connection uploadConn;
49+
4750
private slots:
4851
void on_fileDialogButton_clicked();
4952
void on_connectButton_clicked();
@@ -52,6 +55,7 @@ private slots:
5255
void on_sendButton_clicked();
5356
void sendFile();
5457
void sendMessage(const QString messageToSend);
58+
void sendFileChunk();
5559

5660
};
5761
#endif // MAINWINDOW_H

mainwindow.ui

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
<property name="text">
8181
<string>Select File</string>
8282
</property>
83+
<property name="icon">
84+
<iconset theme="QIcon::ThemeIcon::DocumentOpen"/>
85+
</property>
8386
</widget>
8487
<widget class="QLabel" name="ClientID">
8588
<property name="geometry">
@@ -155,9 +158,15 @@
155158
<height>31</height>
156159
</rect>
157160
</property>
161+
<property name="locale">
162+
<locale language="English" country="UnitedStates"/>
163+
</property>
158164
<property name="text">
159165
<string>Send</string>
160166
</property>
167+
<property name="icon">
168+
<iconset theme="QIcon::ThemeIcon::DocumentSend"/>
169+
</property>
161170
<property name="autoDefault">
162171
<bool>true</bool>
163172
</property>
@@ -174,9 +183,24 @@
174183
<height>471</height>
175184
</rect>
176185
</property>
186+
<property name="focusPolicy">
187+
<enum>Qt::FocusPolicy::NoFocus</enum>
188+
</property>
189+
<property name="verticalScrollBarPolicy">
190+
<enum>Qt::ScrollBarPolicy::ScrollBarAsNeeded</enum>
191+
</property>
192+
<property name="horizontalScrollBarPolicy">
193+
<enum>Qt::ScrollBarPolicy::ScrollBarAlwaysOff</enum>
194+
</property>
195+
<property name="sizeAdjustPolicy">
196+
<enum>QAbstractScrollArea::SizeAdjustPolicy::AdjustIgnored</enum>
197+
</property>
177198
<property name="selectionMode">
178199
<enum>QAbstractItemView::SelectionMode::NoSelection</enum>
179200
</property>
201+
<property name="resizeMode">
202+
<enum>QListView::ResizeMode::Adjust</enum>
203+
</property>
180204
<property name="wordWrap">
181205
<bool>true</bool>
182206
</property>

message.ui

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<x>0</x>
88
<y>0</y>
99
<width>637</width>
10-
<height>93</height>
10+
<height>60</height>
1111
</rect>
1212
</property>
1313
<property name="minimumSize">
1414
<size>
15-
<width>637</width>
15+
<width>0</width>
1616
<height>0</height>
1717
</size>
1818
</property>
@@ -24,7 +24,7 @@
2424
<widget class="QLabel" name="ClientID">
2525
<property name="minimumSize">
2626
<size>
27-
<width>619</width>
27+
<width>0</width>
2828
<height>0</height>
2929
</size>
3030
</property>

0 commit comments

Comments
 (0)