This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2024_Stanislav_Mykhailenko/Lesson_6/Task_1/Client/client.cpp
Stanislav Mykhailenko 0e6e992e05
Fix Lesson 6 Task 1
2024-07-29 23:48:07 +03:00

82 lines
1.8 KiB
C++

#include "client.h"
#include "ui_client.h"
Client::Client(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Client)
{
ui->setupUi(this);
connect (ui->b_connect, &QPushButton::clicked, this, &Client::connectPressed);
connect (ui->b_selectFile, &QPushButton::clicked, this, &Client::selectFile);
connect (ui->b_send, &QPushButton::clicked, this, &Client::sendFile);
connect (m_socket, &QTcpSocket::connected, this, &Client::connectionEstablished);
}
Client::~Client()
{
delete ui;
}
void Client::sendFile()
{
ui->l_status->setText("Connected");
QFile file(m_fileName);
QFileInfo fi(m_fileName);
if (file.open(QIODevice::ReadOnly)) {
m_socket->write((fi.fileName() + '\n').toUtf8());
QTextStream in(&file);
while (!in.atEnd())
m_socket->write((in.readLine() + '\n').toUtf8());
}
ui->l_status->setText("File sent");
ui->b_send->setEnabled(false);
}
void Client::connectPressed()
{
if (m_socket->isOpen()) {
m_socket->close();
ui->l_status->setText("Not connected");
ui->b_connect->setText("Connect");
ui->b_send->setEnabled(false);
return;
}
QString address = ui->e_address->text();
int port = ui->sb_port->value();
ui->b_connect->setText("Disconnect");
ui->l_status->setText("Connecting");
m_socket->connectToHost(QHostAddress(address), port);
}
void Client::connectionEstablished()
{
ui->l_status->setText("Connected");
checkSendingAllowed();
}
void Client::checkSendingAllowed()
{
if (!m_fileName.isEmpty() && m_socket->isOpen())
ui->b_send->setEnabled(true);
}
void Client::selectFile()
{
m_fileName = QFileDialog::getOpenFileName(this, "Select file", "", "All Files (*)");
if (m_fileName.isEmpty())
return;
checkSendingAllowed();
}