1ère version fonctionnelle

This commit is contained in:
2023-01-12 09:23:45 +01:00
parent a76f32ea30
commit 789317ea12
9 changed files with 273 additions and 150 deletions
+150 -67
View File
@@ -10,6 +10,7 @@
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <QMessageBox>
#include <vector>
#include <boost/algorithm/string/replace.hpp>
@@ -19,6 +20,9 @@
#include <QFileDialog>
#include <QThread>
#include <QProgressDialog>
#include <QCheckBox>
#include <sys/wait.h>
#include <QGuiApplication>
using namespace std;
@@ -29,67 +33,85 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&downloadO, &downloadFile::progressSignal, ui->progressBar, &QProgressBar::setValue);
connect(&downloadO, &downloadFile::finishedSignal, this, &MainWindow::downloadFinished);
connect(this, &MainWindow::stopDownloading, &downloadO, &downloadFile::cancelled);
ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget);
ui->ktreewidgetsearchline->setCaseSensitivity(Qt::CaseInsensitive);
ui->treeWidget->setHeaderLabels({tr("Path"), tr("Size")} );
ui->progressBar->hide();
loadSettings();
populateList();
}
MainWindow::~MainWindow()
{
QMessageBox::StandardButton reply;
if (ui->listDownload->count() != 0)
{
reply = QMessageBox::question(
this,
"RsyncUI",
tr("Exiting will stop downloading, and will clear the download queue.\n Do you want to exit ?"),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if (reply == QMessageBox::Yes)
{
emit (stopDownloading(this->downloading.pid));
}
}
if (this->downloading.pid != 0)
{
waitpid(this->downloading.pid, NULL, WUNTRACED);
}
delete ui;
}
void MainWindow::populateTree()
{
string server;
string port;
int portN;
stringstream ss;
vector<string> path;
server.assign(ui->khistorycombobox->currentText().toStdString());
port.assign(ui->portEdit->text().toStdString());
ss << port;
ss >> portN;
if (!server.empty() and !port.empty() and portN < 65536)
if (!this->connexion.server.empty() and this->connexion.port > 0 and this->connexion.port < 65536)
{
if (validateServer(server))
if (validateServer(this->connexion.server))
{
ui->treeWidget->cursor().setShape(Qt::WaitCursor);
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
path = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2);
scanDir(server, portN, NULL, path[0].append("/") );
ui->treeWidget->cursor().setShape(Qt::ArrowCursor);
scanDir(this->connexion.server, this->connexion.port, NULL, path[0].append("/") );
this->unsetCursor();
QGuiApplication::setOverrideCursor(Qt::ArrowCursor);
}
}
}
void MainWindow::populateList()
{
string server;
string port;
int portN;
stringstream ss;
server.assign(ui->khistorycombobox->currentText().toStdString());
port.assign(ui->portEdit->text().toStdString());
ss << port;
ss >> portN;
if (!server.empty() and !port.empty() and portN < 65536)
this->connexion.server.assign(ui->khistorycombobox->currentText().toStdString());
ss << ui->portEdit->text().toStdString();
ss >> this->connexion.port;
if (!this->connexion.server.empty() and this->connexion.port > 0 and this->connexion.port < 65536)
{
if (validateServer(server))
if (validateServer(this->connexion.server))
{
ui->khistorycombobox->addItem(this->connexion.server.c_str());
ui->centralwidget->cursor().setShape(Qt::WaitCursor);
listServices(server, portN);
listServices();
ui->centralwidget->cursor().setShape(Qt::ArrowCursor);
QStringList test = ui->khistorycombobox->historyItems();
sleep(1);
}
}
}
void MainWindow::listServices(string server, int portN)
void MainWindow::listServices()
{
char cmd[4096];
string line;
@@ -97,7 +119,7 @@ void MainWindow::listServices(string server, int portN)
vector<string> v;
char service[4096];
sprintf(cmd, "rsync --contimeout=10 -P \"%s::\" --port %d ", server.c_str(), portN );
sprintf(cmd, "rsync --contimeout=10 -P \"%s::\" --port %d ", this->connexion.server.c_str(), this->connexion.port );
redi::ipstream in(cmd, redi::pstreams::pstdout | redi::pstreams::pstderr);
while (getline(in.out(), line))
{
@@ -167,25 +189,24 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri
bool MainWindow::isIpAddress(string server)
{
bool returnCode = false;
vector<string> r;
stringstream ss;
int elementN;
QString qr;
bool ok;
r = explode(server, '.');
r = explode(server, '.', 5);
if (r.size() == 4)
{
for (auto element : r)
{
ss << element;
ss >> elementN;
if (elementN >0 and elementN < 256)
elementN = QString::fromStdString(element).toInt(&ok);
if (elementN < 0 or elementN > 255 or ok == false)
{
returnCode &= true;
return false;
}
}
}
return returnCode;
return true;
}
bool MainWindow::validateServer(string server)
@@ -227,6 +248,9 @@ bool MainWindow::validateServer(string server)
if ( flag == false)
{
flag = isIpAddress(server);
}
if ( flag == false)
{
QMessageBox::warning(
this,
"RsyncUI",
@@ -247,21 +271,11 @@ void MainWindow::on_khistorycombobox_returnPressed()
populateList();
}
/*void MainWindow::on_portEdit_userTextChanged()
{
populateTree();
}*/
void MainWindow::on_portEdit_returnPressed()
{
populateList();
}
void MainWindow::on_khistorycombobox_textActivated()
{
populateList();
}
void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
@@ -304,41 +318,110 @@ QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name
void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{
vector<string> v;
v = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2);
this->downloading.service = v[0];
populateTree();
}
void MainWindow::on_listDownload_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
cout << current->text().toStdString() << endl;
}
void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
string path;
QFuture<void> future;
QFutureWatcher<void> watcher;
QString savePath;
QFileDialog dialog;
QString dirPath;
/*int p[2];
QCheckBox *cb = new QCheckBox("Okay I understand");
if (pipe(p) < 0)
item = ui->treeWidget->currentItem();
this->downloading.path = item->text(0).toStdString();
while(item->parent() != NULL)
{
return;
}*/
dirPath = getenv("HOME");
dirPath.append("/Vidéos/");
savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
downloadFile downloadO;
item = item->parent();
this->downloading.path = item->text(0).toStdString() + "/" + this->downloading.path;
};
//QProgressDialog progress("Downloading file ...", "Abort Download", 0, 100, this);
//dirPath.append("/Vidéos/");
if (this->downloading.savePath.empty())
{
if (this->downloading.dirPath.toStdString().empty())
{
this->downloading.dirPath = getenv("HOME");
}
ui->progressBar->setWindowModality(Qt::WindowModal);
this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), this->downloading.dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString();
if (!this->downloading.savePath.empty() && this->downloading.pid == 0)
{
startDownloading();
}
}
ui->listDownload->addItem(QString::fromStdString(this->downloading.path));
}
void MainWindow::startDownloading()
{
ui->progressBar->setValue(0);
ui->progressBar->show();
connect(&watcher, &QFutureWatcherBase::finished, ui->progressBar, &QProgressBar::hide);
connect(&downloadO, &downloadFile::progressSignal, ui->progressBar, &QProgressBar::setValue);
future = QtConcurrent::run(&this->MyObject, &downloadFile::download, savePath, this);
watcher.setFuture(future);
QtConcurrent::run(&this->downloadO, &downloadFile::download, this);
}
void MainWindow::stoppingDownload()
{
emit (stopDownloading(this->downloading.pid));
}
void MainWindow::downloadFinished()
{
ui->progressBar->hide();
delete ui->listDownload->takeItem(0);
this->downloading.pid = 0;
if (ui->listDownload->count() != 0)
{
this->downloading.path = ui->listDownload->item(0)->text().toStdString();
startDownloading();
}
}
void MainWindow::on_listDownload_itemClicked(QListWidgetItem *item)
{
QFileDialog dialog;
QMessageBox::StandardButton reply;
cout << item->text().toStdString() << endl;
if (item->listWidget()->row(item) == 0)
{
reply = QMessageBox::question(
this,
"RsyncUI",
tr("Do you want to stop downloading and delete this file from download queue ?"),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if (reply == QMessageBox::Yes)
{
emit (stopDownloading(this->downloading.pid));
}
}else
{
reply = QMessageBox::question(
this,
"RsyncUI",
tr("Do you want to delete this file from download queue ?"),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if (reply == QMessageBox::Yes)
{
ui->listDownload->removeItemWidget(item);
delete item;
}
}
}
void MainWindow::loadSettings()
{
this->settings.value("serverlist");
}
void MainWindow::saveSettings()
{
//ui->khistorycombobox->
}