1515#include < QFileInfo>
1616#include < QScrollBar>
1717#include < QCoreApplication>
18+ #include " addipwindow.h"
1819
1920QByteArray 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+
6278MainWindow::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 (¤tIpFile);
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+
348430void 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
0 commit comments