Skip to content

Commit fa5a848

Browse files
Changed the ip input from a simpel input to a combobox selection, as well as added a feature where the .cache folder would be created at start up that will hold the list of ips added as well as the last selected ip address. These features also come with the proper input methods to add ip address to the list with a button.
1 parent a39bbd0 commit fa5a848

File tree

7 files changed

+314
-24
lines changed

7 files changed

+314
-24
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ qt_add_executable(DirectFilePushClient
1414
mainwindow.ui
1515
message.h message.cpp message.ui
1616
filemessage.h filemessage.cpp filemessage.ui
17+
addipwindow.h addipwindow.cpp addipwindow.ui
1718
)
1819

1920
target_link_libraries(DirectFilePushClient

addipwindow.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "addipwindow.h"
2+
#include "ui_addipwindow.h"
3+
#include <QFile>
4+
#include "mainwindow.h"
5+
6+
addIpWindow::addIpWindow(MainWindow *parent)
7+
: QDialog(parent)
8+
, ui(new Ui::addIpWindow)
9+
{
10+
ui->setupUi(this);
11+
}
12+
13+
addIpWindow::~addIpWindow()
14+
{
15+
delete ui;
16+
}
17+
18+
void addIpWindow::on_AddButton_clicked()
19+
{
20+
accept();
21+
}
22+
23+
QString addIpWindow::getIpAddress() const {
24+
return ui->IpEnter->text();
25+
}
26+
27+
QString addIpWindow::getPort() const {
28+
return ui->PortEnter->text();
29+
}
30+
31+
void addIpWindow::on_CancelButton_clicked()
32+
{
33+
reject();
34+
}

addipwindow.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef ADDIPWINDOW_H
2+
#define ADDIPWINDOW_H
3+
4+
#include <QDialog>
5+
#include "mainwindow.h"
6+
7+
namespace Ui {
8+
class addIpWindow;
9+
}
10+
11+
class addIpWindow : public QDialog
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
explicit addIpWindow(MainWindow *parent = nullptr);
17+
~addIpWindow();
18+
19+
QString getIpAddress() const;
20+
QString getPort() const;
21+
22+
private:
23+
Ui::addIpWindow *ui;
24+
25+
private slots:
26+
void on_AddButton_clicked();
27+
void on_CancelButton_clicked();
28+
};
29+
30+
#endif // ADDIPWINDOW_H

addipwindow.ui

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>addIpWindow</class>
4+
<widget class="QWidget" name="addIpWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>317</width>
10+
<height>207</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Form</string>
15+
</property>
16+
<widget class="QLabel" name="Title">
17+
<property name="geometry">
18+
<rect>
19+
<x>0</x>
20+
<y>0</y>
21+
<width>311</width>
22+
<height>41</height>
23+
</rect>
24+
</property>
25+
<property name="font">
26+
<font>
27+
<pointsize>20</pointsize>
28+
<bold>true</bold>
29+
</font>
30+
</property>
31+
<property name="text">
32+
<string>Enter Ip and Port</string>
33+
</property>
34+
<property name="scaledContents">
35+
<bool>false</bool>
36+
</property>
37+
<property name="alignment">
38+
<set>Qt::AlignmentFlag::AlignCenter</set>
39+
</property>
40+
</widget>
41+
<widget class="QLineEdit" name="IpEnter">
42+
<property name="geometry">
43+
<rect>
44+
<x>112</x>
45+
<y>50</y>
46+
<width>191</width>
47+
<height>41</height>
48+
</rect>
49+
</property>
50+
<property name="text">
51+
<string>localhost</string>
52+
</property>
53+
</widget>
54+
<widget class="QLineEdit" name="PortEnter">
55+
<property name="geometry">
56+
<rect>
57+
<x>110</x>
58+
<y>100</y>
59+
<width>191</width>
60+
<height>41</height>
61+
</rect>
62+
</property>
63+
<property name="text">
64+
<string>3000</string>
65+
</property>
66+
</widget>
67+
<widget class="QPushButton" name="AddButton">
68+
<property name="geometry">
69+
<rect>
70+
<x>160</x>
71+
<y>160</y>
72+
<width>141</width>
73+
<height>41</height>
74+
</rect>
75+
</property>
76+
<property name="text">
77+
<string>Add</string>
78+
</property>
79+
</widget>
80+
<widget class="QPushButton" name="CancelButton">
81+
<property name="geometry">
82+
<rect>
83+
<x>10</x>
84+
<y>160</y>
85+
<width>131</width>
86+
<height>41</height>
87+
</rect>
88+
</property>
89+
<property name="text">
90+
<string>Cancel</string>
91+
</property>
92+
</widget>
93+
<widget class="QLabel" name="IpDisplay">
94+
<property name="geometry">
95+
<rect>
96+
<x>10</x>
97+
<y>60</y>
98+
<width>91</width>
99+
<height>21</height>
100+
</rect>
101+
</property>
102+
<property name="text">
103+
<string>Ip Address</string>
104+
</property>
105+
<property name="alignment">
106+
<set>Qt::AlignmentFlag::AlignCenter</set>
107+
</property>
108+
</widget>
109+
<widget class="QLabel" name="PortDisplay">
110+
<property name="geometry">
111+
<rect>
112+
<x>10</x>
113+
<y>110</y>
114+
<width>91</width>
115+
<height>21</height>
116+
</rect>
117+
</property>
118+
<property name="text">
119+
<string>Port</string>
120+
</property>
121+
<property name="alignment">
122+
<set>Qt::AlignmentFlag::AlignCenter</set>
123+
</property>
124+
</widget>
125+
</widget>
126+
<resources/>
127+
<connections/>
128+
</ui>

mainwindow.cpp

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <QFileInfo>
1616
#include <QScrollBar>
1717
#include <QCoreApplication>
18+
#include "addipwindow.h"
1819

1920
QByteArray get_data_string(QTcpSocket* socket)
2021
{
@@ -59,6 +60,21 @@ QString formatByteSpeed(qint64 bytes)
5960
return QString::number(speed, 'f', 2) + " " + byteFormats[byteIndex];
6061
}
6162

63+
void MainWindow::UpdateLastIp()
64+
{
65+
QDir dir;
66+
if (!dir.exists(".cache"))
67+
dir.mkpath(".cache"); // make sure the folder exists
68+
69+
QFile file(".cache/lastIp.txt");
70+
if (file.open(QFile::WriteOnly | QFile::Truncate | QFile::Text))
71+
{
72+
QTextStream out(&file);
73+
out << ui->ipSelector->currentText();
74+
file.close();
75+
}
76+
}
77+
6278
MainWindow::MainWindow(QWidget *parent)
6379
: QMainWindow(parent)
6480
, ui(new Ui::MainWindow)
@@ -71,6 +87,45 @@ MainWindow::MainWindow(QWidget *parent)
7187
ui->MessageList->setWordWrap(true); // allow wrapping
7288
ui->MessageList->setResizeMode(QListView::Adjust); // items adjust width
7389

90+
QDir dir;
91+
dir.mkpath(".cache"); // no need to check; mkpath returns true even if folder already exists
92+
93+
QFile currentIpFile(".cache/lastIp.txt");
94+
QString lastIp;
95+
96+
if (currentIpFile.open(QIODevice::ReadOnly | QIODevice::Text))
97+
{
98+
QTextStream in(&currentIpFile);
99+
lastIp = in.readLine().trimmed(); // trim removes spaces, \n, and \r
100+
currentIpFile.close();
101+
}
102+
103+
// Load IP list
104+
QFile file(".cache/iplist.txt");
105+
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
106+
QTextStream in(&file);
107+
while (!in.atEnd()) {
108+
QString line = in.readLine().trimmed();
109+
if (!line.isEmpty())
110+
ui->ipSelector->addItem(line);
111+
}
112+
file.close();
113+
}
114+
115+
// Apply last IP
116+
if (!lastIp.isEmpty()) {
117+
int index = ui->ipSelector->findText(lastIp, Qt::MatchExactly);
118+
if (index != -1)
119+
ui->ipSelector->setCurrentIndex(index);
120+
else {
121+
ui->ipSelector->addItem(lastIp);
122+
ui->ipSelector->setCurrentText(lastIp);
123+
}
124+
}
125+
126+
connect(ui->ipSelector, &QComboBox::currentTextChanged,
127+
this, &MainWindow::UpdateLastIp);
128+
74129
updateTimer = new QTimer(this);
75130
connect(updateTimer, &QTimer::timeout, this, [this]() {
76131
currentFileMessage->setStatus(
@@ -83,7 +138,7 @@ MainWindow::MainWindow(QWidget *parent)
83138

84139
connect(socket, &QTcpSocket::connected, this, [this]
85140
{
86-
ui->IpEnter->setVisible(false);
141+
ui->ipSelector->setVisible(false);
87142
ui->connectButton->setText("Disconnect");
88143
});
89144

@@ -315,7 +370,7 @@ MainWindow::MainWindow(QWidget *parent)
315370
connect(socket, &QTcpSocket::disconnected, this, [this]() {
316371
qDebug() << "Disconnected from server.";
317372
ui->connectButton->setText("Connect");
318-
ui->IpEnter->setVisible(true);
373+
ui->ipSelector->setVisible(true);
319374
ui->PairIDInput->setReadOnly(false);
320375
ui->PairIDInput->setText("");
321376
ui->pairButton->setText("Pair");
@@ -345,6 +400,33 @@ MainWindow::~MainWindow()
345400
delete ui;
346401
}
347402

403+
void MainWindow::on_AddIpButton_clicked()
404+
{
405+
addIpWindow dialog(this);
406+
if (dialog.exec() == QDialog::Accepted)
407+
{
408+
QString ip = dialog.getIpAddress();
409+
QString port = dialog.getPort();
410+
QString entry = QString("%1:%2").arg(ip, port);
411+
412+
ui->ipSelector->addItem(entry);
413+
ui->ipSelector->setCurrentIndex(ui->ipSelector->count() - 1);
414+
415+
// Make sure cache directory exists
416+
QDir dir;
417+
if (!dir.exists(".cache"))
418+
dir.mkpath(".cache");
419+
420+
// Open the file for appending
421+
QFile file(".cache/iplist.txt");
422+
if (file.open(QIODevice::Append | QIODevice::Text)) {
423+
QTextStream out(&file);
424+
out << entry << "\n";
425+
file.close();
426+
}
427+
}
428+
}
429+
348430
void MainWindow::on_fileDialogButton_clicked()
349431
{
350432
selectedFilePath = QFileDialog::getOpenFileName(
@@ -375,7 +457,7 @@ void MainWindow::on_connectButton_clicked()
375457
return;
376458
}
377459

378-
QStringList serverInfo = ui->IpEnter->text().split(":");
460+
QStringList serverInfo = ui->ipSelector->currentText().split(":");
379461
QString ipAddress = serverInfo[0];
380462
int port = serverInfo[1].toInt();
381463

mainwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ private slots:
5959
void sendMessage(const QString messageToSend);
6060
void sendFileChunk();
6161
void scrollToBottom();
62+
void on_AddIpButton_clicked();
63+
void UpdateLastIp();
6264

6365
};
6466
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)