added password protected connexion

correction bug on detection of already downloaded file
This commit is contained in:
Daniel Tartavel 2023-02-27 23:48:05 +01:00
parent 10060ee2c4
commit e7eafb3117
9 changed files with 314 additions and 50 deletions

View File

@ -29,6 +29,7 @@ HEADERS += \
FORMS += \ FORMS += \
about.ui \ about.ui \
configuration.ui \ configuration.ui \
login.ui \
mainwindow.ui mainwindow.ui
TRANSLATIONS += \ TRANSLATIONS += \

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.2, 2023-02-22T00:23:04. --> <!-- Written by QtCreator 4.14.2, 2023-02-26T22:25:49. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -33,7 +33,7 @@ void MainWindow::cancelled(QProcess * process)
n = process->waitForFinished(30000); n = process->waitForFinished(30000);
if (n == false) if (n == false)
{ {
process->kill(); process->close();
} }
} }
@ -42,7 +42,15 @@ void MainWindow::download()
{ {
QString cmd; QString cmd;
QStringList param; QStringList param;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
this->downloading.process = new QProcess(this);
if (!this->downloading.user.isEmpty())
{
this->downloading.server.prepend(this->connexion.user + "@");
env.insert("RSYNC_PASSWORD", this->downloading.password); // Add an environment variable
this->downloading.process->setProcessEnvironment(env);
}
// Populating array with command and parameters for popen2 // Populating array with command and parameters for popen2
cmd = "rsync"; cmd = "rsync";
if (this->connexion.bandwidthLimit != 0) if (this->connexion.bandwidthLimit != 0)
@ -53,7 +61,7 @@ void MainWindow::download()
param << "-aXP"; param << "-aXP";
param << this->downloading.server + "::" + this->downloading.service + "/" + this->downloading.path << this->downloading.savePath + "/"; param << this->downloading.server + "::" + this->downloading.service + "/" + this->downloading.path << this->downloading.savePath + "/";
this->downloading.process = new QProcess(this);
connect(this->downloading.process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(downloadFinished(int, QProcess::ExitStatus))); connect(this->downloading.process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(downloadFinished(int, QProcess::ExitStatus)));
//connect(this->downloading.process, SIGNAL(errorOccurred(QProcess::ProcessError error)), this, SLOT(downloadProcessError(QProcess::ProcessError error))); //connect(this->downloading.process, SIGNAL(errorOccurred(QProcess::ProcessError error)), this, SLOT(downloadProcessError(QProcess::ProcessError error)));

120
login.ui Normal file
View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LoginDialog</class>
<widget class="QDialog" name="LoginDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>194</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="loginBox">
<property name="geometry">
<rect>
<x>50</x>
<y>150</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>381</width>
<height>121</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Login name</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="loginEdit">
<property name="text">
<string>Enter login name</string>
</property>
<property name="maxLength">
<number>128</number>
</property>
<property name="placeholderText">
<string>Enter login</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="passwordEdit">
<property name="text">
<string>Enter password</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>loginBox</sender>
<signal>accepted()</signal>
<receiver>LoginDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>loginBox</sender>
<signal>rejected()</signal>
<receiver>LoginDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -47,6 +47,9 @@ MainWindow::MainWindow(QWidget *parent)
// init about window // init about window
AboutW.setupUi(&aboutDialog); AboutW.setupUi(&aboutDialog);
//init login dialog
loginD.setupUi(&loginDialog);
// text of About // text of About
QString aboutText = tr("<h2>Client for rsync server</h2>") + QString aboutText = tr("<h2>Client for rsync server</h2>") +
"<b>" + tr("Version") + ": " + this->about.version + "</b><br>" + "<b>" + tr("Version") + ": " + this->about.version + "</b><br>" +
@ -66,6 +69,7 @@ MainWindow::MainWindow(QWidget *parent)
connect(this, &MainWindow::stopDownloading, this, &MainWindow::cancelled); connect(this, &MainWindow::stopDownloading, this, &MainWindow::cancelled);
connect(config.buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted())); connect(config.buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
connect(config.comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::on_comboBox_currentIndexChanged); connect(config.comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::on_comboBox_currentIndexChanged);
connect(loginD.loginBox, SIGNAL(accepted()), this, SLOT(on_loginBox_accepted()));
// init of widgets // init of widgets
ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget); // attach search widget to treewidget ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget); // attach search widget to treewidget
@ -284,7 +288,7 @@ void MainWindow::populateTree(QTreeWidgetItem * parent)
// validating server's address // validating server's address
if (validateServer(this->connexion.server)) if (validateServer(this->connexion.server))
{ {
// server is validated // server is validated, scanning directory
path = ui->listWidget->currentItem()->text().section('\n', 0, 0) + "/"; path = ui->listWidget->currentItem()->text().section('\n', 0, 0) + "/";
scanDir(this->connexion.server, this->connexion.port, parent, path); scanDir(this->connexion.server, this->connexion.port, parent, path);
} }
@ -392,7 +396,8 @@ void MainWindow::listServices()
} }
// verifying error code // verifying error code
testRsyncReturn(myProcess); testRsyncReturn(this, myProcess);
myProcess->close();
} }
// connect to rsync server to get list of files // connect to rsync server to get list of files
@ -406,22 +411,36 @@ void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
QProcess * myProcess; QProcess * myProcess;
bool isDir = false; bool isDir = false;
bool flag = false; bool flag = false;
bool readOk = false;
int nChild = 0; int nChild = 0;
QMessageBox::StandardButton reply;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
if (parent != NULL) myProcess = new QProcess(this);
if (!this->connexion.user.isEmpty())
{
server.prepend(this->connexion.user + "@");
env.insert("RSYNC_PASSWORD", "Test"); // Add an environment variable
myProcess->setProcessEnvironment(env);
}
if (parent != nullptr)
{ {
nChild = parent->childCount(); nChild = parent->childCount();
} }
if (nChild == 0) if (nChild == 0)
{ {
cmd = "rsync"; cmd = "rsync";
param << "--contimeout=10" << "--port=" + QString::number(portN) << server + "::" + path; param << "--contimeout=10" << "--port=" + QString::number(portN) << server + "::" + path;
myProcess = new QProcess(this);
myProcess->start(cmd, param); myProcess->start(cmd, param);
// waiting for responsiteme of the server with a timeout of 10 seconds // waiting for response of the server with a timeout of 10 seconds
while(myProcess->waitForReadyRead(100000)) do
{
readOk = myProcess->waitForReadyRead(5000);
if (readOk)
{ {
while (!flag) while (!flag)
{ {
@ -456,11 +475,52 @@ void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
} }
} }
} }
flag = false; flag = false;
}else
{
if (myProcess->state() == QProcess::Running)
{
if (myProcess->write("\n") == -1)
{
QMessageBox::warning(
this,
"RsyncUI",
tr("Can't write to processus: ") + myProcess->errorString());
} }
if (myProcess->waitForBytesWritten(5000) == 0)
{
QMessageBox::warning(
this,
"RsyncUI",
tr("writing to processus did not respond: ") + myProcess->errorString());
}
if (myProcess->state() == QProcess::Running)
{
/* if (myProcess->waitForFinished(10000) == 0)
{
QMessageBox::warning(
this,
"RsyncUI",
tr("The processus does'nt respond: do you need a password ? ") + myProcess->errorString());
}*/
reply = QMessageBox::question(
this,
"RsyncUI",
tr("The processus does'nt respond: do you need a password ? "),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No);
if (reply == QMessageBox::Yes)
{
loginDialog.show();
}
}
}
}
}while(readOk);
// buffer empty go to waiting new datas // buffer empty go to waiting new datas
testRsyncReturn(myProcess);
testRsyncReturn(this, myProcess);
myProcess->close();
} }
} }
@ -540,6 +600,7 @@ bool MainWindow::validateServer(QString server)
tr("server does not exists" ) tr("server does not exists" )
); );
} }
myProcess->close();
return flag; return flag;
} }
@ -602,8 +663,10 @@ QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name
void MainWindow::on_listWidget_clicked() void MainWindow::on_listWidget_clicked()
{ {
QString str; QString str;
QStringList logins;
this->connexion.service = ui->listWidget->currentItem()->text().section("\n", 0 ,0); this->connexion.user = nullptr;
this->connexion.password = nullptr; this->connexion.service = ui->listWidget->currentItem()->text().section("\n", 0 ,0);
str = "Folder/" + this->connexion.server + "/" + this->connexion.service; str = "Folder/" + this->connexion.server + "/" + this->connexion.service;
// if service exists in settings for this server // if service exists in settings for this server
if (this->settings.contains(str)) if (this->settings.contains(str))
@ -611,9 +674,41 @@ void MainWindow::on_listWidget_clicked()
// setting savePath from settings // setting savePath from settings
this->downloading.savePath = this->settings.value(str).toString(); this->downloading.savePath = this->settings.value(str).toString();
} }
this->settings.beginGroup("Passwords/" + this->connexion.server + "/" + this->connexion.service);
logins = this->settings.allKeys();
//TODO choose login
if (logins.count() != 0)
{
this->connexion.user = logins[0];
this->connexion.password = this->settings.value(logins[0]).toString();
}
this->settings.endGroup();
populateTree(NULL); populateTree(NULL);
} }
bool MainWindow::getUserPassword()
{
QStringList logins;
bool returnValue;
this->settings.beginGroup("Passwords/" + this->connexion.server + "/" + this->connexion.service);
logins = this->settings.allKeys();
//TODO choose login in case of multiples logins
if (logins.count() != 0)
{
this->downloading.user = logins[0];
this->downloading.password = this->settings.value(logins[0]).toString();
returnValue = true;
}else
{
this->downloading.user = nullptr;
this->downloading.password = nullptr;
returnValue = false;
}
this->settings.endGroup();
return returnValue;
}
//Slot activated when a file is clicked in the treeview //Slot activated when a file is clicked in the treeview
void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, bool downloadDir) void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, bool downloadDir)
{ {
@ -629,6 +724,7 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, bool downloadD
// assembling path from treewidget // assembling path from treewidget
path = item->text(0); path = item->text(0);
sizeFromRsync = item->text(2).remove(',').toUInt();
// exists saving path in settings ? // exists saving path in settings ?
str = "Folder/" + this->connexion.server + "/" + this->connexion.service; str = "Folder/" + this->connexion.server + "/" + this->connexion.service;
@ -651,16 +747,15 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, bool downloadD
itemR = itemR->parent(); itemR = itemR->parent();
// concatening parent to path // concatening parent to path
path.prepend(itemR->text(0) + "/"); path.prepend(itemR->text(0) + "/");
sizeFromRsync = itemR->text(1).toUInt();
}; };
QFileInfo info(path); QFileInfo info(this->downloading.savePath + "/" + path);
if (item->text(1) == tr("File") or downloadDir == true) if (item->text(1) == tr("File") or downloadDir == true)
{ {
// Item is a file // Item is a file
// searching if file exists in savepath // searching if file exists in savepath
if (QFile::exists(this->downloading.savePath + "/" + path)) if (QFile::exists(this->downloading.savePath + "/" + path))
{ {
if (info.size() >= sizeFromRsync) if (info.size() < sizeFromRsync)
{ {
reply = QMessageBox::question( reply = QMessageBox::question(
this, this,
@ -737,8 +832,10 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, bool downloadD
// Launch the thread which download the file // Launch the thread which download the file
void MainWindow::startDownloading() void MainWindow::startDownloading()
{ {
ui->progressBar->setValue(0); ui->progressBar->setValue(0);
ui->progressBar->show(); ui->progressBar->show();
getUserPassword();
//QtConcurrent::run(&this->downloadO, &downloadFile::download, this); //QtConcurrent::run(&this->downloadO, &downloadFile::download, this);
this->download(); this->download();
@ -778,16 +875,19 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
}else if (exitCode == 20) }else if (exitCode == 20)
{ {
aborted = tr("stopped by user"); aborted = tr("stopped by user");
}else if (exitCode == 5) // password asked
{
//TODO
loginDialog.show();
} }
this->trayIcon->showMessage("RsyncUI", tr("Download ") + aborted + "\n" + this->downloading.path, QSystemTrayIcon::Information); this->trayIcon->showMessage("RsyncUI", tr("Download ") + aborted + "\n" + this->downloading.path, QSystemTrayIcon::Information);
// disconnecting signals to slots // disconnecting signals to slots
disconnect(this->downloading.process, 0, 0, 0); disconnect(this->downloading.process, 0, 0, 0);
// reset variables and window // reset variables and window
this->downloading.process = nullptr; this->downloading.process->close();
ui->progressBar->hide(); ui->progressBar->hide();
delete ui->listDownload->takeItem(0); delete ui->listDownload->takeItem(0);
this->downloading.clear(); this->downloading.clear();
@ -1069,6 +1169,11 @@ void Downloading::clear()
this->server.clear(); this->server.clear();
this->savePath.clear(); this->savePath.clear();
this->service.clear(); this->service.clear();
this->user.clear();
this->password.clear();
this->port = 0;
this->process = nullptr;
this->quit = false;
} }
// Context menu of file list clicked // Context menu of file list clicked
@ -1102,7 +1207,22 @@ void MainWindow::on_actionExit_triggered()
quitApp(); quitApp();
} }
void MainWindow::on_loginBox_accepted()
{
if (!loginD.loginEdit->text().isEmpty())
{
this->connexion.user = loginD.loginEdit->text();
if (!loginD.passwordEdit->text().isEmpty())
{
this->connexion.password = loginD.passwordEdit->text();
this->settings.setValue("Passwords/" + this->connexion.server + "/" + this->connexion.service + "/" + this->connexion.user, this->connexion.password);
this->settings.sync();
}
}
}
void MainWindow::setDlSpeed(QString speed) void MainWindow::setDlSpeed(QString speed)
{ {
speed.squeeze(); speed.squeeze();
} }

View File

@ -5,6 +5,7 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "ui_configuration.h" #include "ui_configuration.h"
#include "ui_about.h" #include "ui_about.h"
#include "ui_login.h"
#include "downloadfile.h" #include "downloadfile.h"
#include "tools.h" #include "tools.h"
#include <QMainWindow> #include <QMainWindow>
@ -44,6 +45,7 @@
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QMenu> #include <QMenu>
#include <QTranslator> #include <QTranslator>
#include <QInputDialog>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
@ -58,6 +60,8 @@ class Connexion
int bandwidthLimitUnit; int bandwidthLimitUnit;
QString server; QString server;
QString service; QString service;
QString user;
QString password;
int port = 873; int port = 873;
bool comboboxChanged; bool comboboxChanged;
}; };
@ -69,6 +73,8 @@ class Downloading
QString service; QString service;
QString path; QString path;
QString savePath; QString savePath;
QString user;
QString password;
int port; int port;
QProcess * process = nullptr; QProcess * process = nullptr;
bool quit = false; bool quit = false;
@ -98,13 +104,14 @@ class MainWindow : public QMainWindow
QProgressDialog *progress; QProgressDialog *progress;
Connexion connexion; Connexion connexion;
Downloading downloading; Downloading downloading;
//downloadFile downloadO;
QSettings settings; QSettings settings;
About about; About about;
QDialog Configuration; QDialog Configuration;
Ui::Configuration config; Ui::Configuration config;
QDialog aboutDialog; QDialog aboutDialog;
Ui::windowAbout AboutW; Ui::windowAbout AboutW;
QDialog loginDialog;
Ui::LoginDialog loginD;
std::vector <QString> serversList; std::vector <QString> serversList;
QSystemTrayIcon * trayIcon; QSystemTrayIcon * trayIcon;
QString icon = "/usr/share/icons/RsyncUI.png"; QString icon = "/usr/share/icons/RsyncUI.png";
@ -153,6 +160,7 @@ class MainWindow : public QMainWindow
void hideWindow(); void hideWindow();
void showWindow(); void showWindow();
void init(); void init();
bool getUserPassword();
private slots: private slots:
@ -202,6 +210,8 @@ class MainWindow : public QMainWindow
void setDlSpeed(QString speed); void setDlSpeed(QString speed);
void on_loginBox_accepted();
signals: signals:
void stopDownloading(QProcess *); void stopDownloading(QProcess *);
void progressSignal(int); void progressSignal(int);

View File

@ -275,10 +275,10 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="dragEnabled"> <property name="dragEnabled">
<bool>true</bool> <bool>false</bool>
</property> </property>
<property name="dragDropMode"> <property name="dragDropMode">
<enum>QAbstractItemView::DragOnly</enum> <enum>QAbstractItemView::NoDragDrop</enum>
</property> </property>
<property name="alternatingRowColors"> <property name="alternatingRowColors">
<bool>true</bool> <bool>true</bool>

View File

@ -44,7 +44,7 @@ const vector<string> explode(const string& s, const char& c, int n = 0)
} }
// test return code of rsync // test return code of rsync
bool testRsyncReturn(QProcess * myProcess) bool testRsyncReturn(MainWindow * w, QProcess * myProcess)
{ {
if (myProcess->exitStatus() != 0) if (myProcess->exitStatus() != 0)
{ {
@ -55,6 +55,9 @@ bool testRsyncReturn(QProcess * myProcess)
QMessageBox::Ok, QMessageBox::Ok,
QMessageBox::Ok); QMessageBox::Ok);
return true; return true;
}else if (myProcess->exitCode() == 5)
{
w->loginDialog.show();
}else if (myProcess->exitCode() != 0) }else if (myProcess->exitCode() != 0)
{ {
QMessageBox::warning( QMessageBox::warning(
@ -67,3 +70,5 @@ bool testRsyncReturn(QProcess * myProcess)
} }
return false; return false;
} }

View File

@ -1,6 +1,7 @@
#ifndef TOOLS_H #ifndef TOOLS_H
#define TOOLS_H #define TOOLS_H
#include "mainwindow.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <array> #include <array>
@ -17,7 +18,6 @@ FILE * popen2(array<string,8> argv, string type, int & pid);
int pclose2(FILE * fp, pid_t pid); int pclose2(FILE * fp, pid_t pid);
bool testRsyncReturn(QProcess *); bool testRsyncReturn(MainWindow *, QProcess *);
#endif // TOOLS_H #endif // TOOLS_H