Added support for password protected services

This commit is contained in:
Daniel Tartavel 2023-03-01 14:12:38 +01:00
parent e7eafb3117
commit f47160622c
4 changed files with 71 additions and 59 deletions

View File

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

View File

@ -2,6 +2,9 @@
<ui version="4.0">
<class>LoginDialog</class>
<widget class="QDialog" name="LoginDialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
@ -13,6 +16,9 @@
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<widget class="QDialogButtonBox" name="loginBox">
<property name="geometry">
<rect>
@ -54,11 +60,20 @@
</item>
<item>
<widget class="QLineEdit" name="loginEdit">
<property name="mouseTracking">
<bool>false</bool>
</property>
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="text">
<string>Enter login name</string>
<string/>
</property>
<property name="maxLength">
<number>128</number>
<number>32767</number>
</property>
<property name="placeholderText">
<string>Enter login</string>
@ -74,7 +89,16 @@
</item>
<item>
<widget class="QLineEdit" name="passwordEdit">
<property name="text">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
<property name="placeholderText">
<string>Enter password</string>
</property>
</widget>

View File

@ -49,6 +49,7 @@ MainWindow::MainWindow(QWidget *parent)
//init login dialog
loginD.setupUi(&loginDialog);
loginD.loginEdit->setFocus();
// text of About
QString aboutText = tr("<h2>Client for rsync server</h2>") +
@ -290,7 +291,7 @@ void MainWindow::populateTree(QTreeWidgetItem * parent)
{
// server is validated, scanning directory
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);
}
// Restoring cursor
QGuiApplication::restoreOverrideCursor();
@ -401,7 +402,7 @@ void MainWindow::listServices()
}
// connect to rsync server to get list of files
void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QString path)
bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QString path)
{
QString cmd;
QStringList param;
@ -413,23 +414,26 @@ void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
bool flag = false;
bool readOk = false;
int nChild = 0;
QMessageBox::StandardButton reply;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
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();
}
if (nChild == 0)
{
if (!this->connexion.user.isEmpty())
{
server.prepend(this->connexion.user + "@");
env.insert("RSYNC_PASSWORD", this->connexion.password); // Add an environment variable
}else
{
server.prepend("anonymous@");
env.insert("RSYNC_PASSWORD", "anonymous"); // Add an environment variable
}
myProcess->setProcessEnvironment(env);
cmd = "rsync";
param << "--contimeout=10" << "--port=" + QString::number(portN) << server + "::" + path;
@ -439,7 +443,7 @@ void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
// waiting for response of the server with a timeout of 10 seconds
do
{
readOk = myProcess->waitForReadyRead(5000);
readOk = myProcess->waitForReadyRead(10000);
if (readOk)
{
while (!flag)
@ -480,39 +484,12 @@ void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
{
if (myProcess->state() == QProcess::Running)
{
if (myProcess->write("\n") == -1)
if (myProcess->waitForFinished(10000) == 0)
{
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();
}
tr("The processus does'nt respond: ") + myProcess->errorString());
}
}
}
@ -522,6 +499,7 @@ void MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
testRsyncReturn(this, myProcess);
myProcess->close();
}
return 0;
}
// Verify if server address is IP address
@ -665,6 +643,7 @@ void MainWindow::on_listWidget_clicked()
QString str;
QStringList logins;
this->rescan = true;
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;
@ -686,14 +665,23 @@ void MainWindow::on_listWidget_clicked()
populateTree(NULL);
}
bool MainWindow::getUserPassword()
// get password and user login
// if object = false ==> searching from connexion object
// else searching from downloading object
bool MainWindow::getUserPassword(bool object = false)
{
QStringList logins;
bool returnValue;
this->settings.beginGroup("Passwords/" + this->connexion.server + "/" + this->connexion.service);
if (object == false)
{
this->settings.beginGroup("Passwords/" + this->connexion.server + "/" + this->connexion.service);
}else
{
this->settings.beginGroup("Passwords/" + this->downloading.server + "/" + this->downloading.service);
}
logins = this->settings.allKeys();
//TODO choose login in case of multiples logins
//TODO choose login in case of multiples logins
if (logins.count() != 0)
{
this->downloading.user = logins[0];
@ -835,7 +823,7 @@ void MainWindow::startDownloading()
ui->progressBar->setValue(0);
ui->progressBar->show();
getUserPassword();
getUserPassword(false);
//QtConcurrent::run(&this->downloadO, &downloadFile::download, this);
this->download();
@ -877,7 +865,6 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
aborted = tr("stopped by user");
}else if (exitCode == 5) // password asked
{
//TODO
loginDialog.show();
}
@ -895,13 +882,6 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
// Some downloads staying in queue
if (ui->listDownload->count() != 0)
{
// autosave is activated
/*if (config.autosaveCheckbox->checkState() == Qt::Checked)
{
// saving download list
saveDownloadList();
}*/
// initializing download
path = ui->listDownload->item(0)->text();
pos = path.lastIndexOf("/");
@ -911,6 +891,7 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
this->downloading.server = path.midRef(pos+4).toString();
path.resize(pos);
this->downloading.path = path;
getUserPassword(true);
// savepath exists in settings ?
str = "Folder/" + this->downloading.server + "/" + this->downloading.service;
@ -1217,6 +1198,11 @@ void MainWindow::on_loginBox_accepted()
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();
if (this->rescan == true)
{
this->rescan = false;
populateTree(NULL);
}
}
}
}

View File

@ -75,7 +75,7 @@ class Downloading
QString savePath;
QString user;
QString password;
int port;
int port = 873;
QProcess * process = nullptr;
bool quit = false;
void clear();
@ -115,6 +115,8 @@ class MainWindow : public QMainWindow
std::vector <QString> serversList;
QSystemTrayIcon * trayIcon;
QString icon = "/usr/share/icons/RsyncUI.png";
bool rescan = false;
QList<QString> bwUnitText {
"KB",
"MB",
@ -148,7 +150,7 @@ class MainWindow : public QMainWindow
bool isIpAddress(QString server);
QTreeWidgetItem * addTreeRoot(QString name, QString description, bool isDir);
QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size, bool isDir);
void scanDir(QString server, int portN, QTreeWidgetItem *parent = NULL, QString path = "" );
bool scanDir(QString server, int portN, QTreeWidgetItem *parent = NULL, QString path = "" );
void startDownloading();
void loadSettings();
void saveSettings();
@ -160,7 +162,7 @@ class MainWindow : public QMainWindow
void hideWindow();
void showWindow();
void init();
bool getUserPassword();
bool getUserPassword(bool);
private slots: