From f47160622c4f4b31ccc88ba51558fa1c92689be6 Mon Sep 17 00:00:00 2001 From: Daniel Tartavel Date: Wed, 1 Mar 2023 14:12:38 +0100 Subject: [PATCH] Added support for password protected services --- RsyncUI.pro.user | 2 +- login.ui | 30 ++++++++++++++-- mainwindow.cpp | 90 ++++++++++++++++++++---------------------------- mainwindow.h | 8 +++-- 4 files changed, 71 insertions(+), 59 deletions(-) diff --git a/RsyncUI.pro.user b/RsyncUI.pro.user index 4d9d8cf..0b6d4e2 100644 --- a/RsyncUI.pro.user +++ b/RsyncUI.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/login.ui b/login.ui index 029cfff..247b790 100644 --- a/login.ui +++ b/login.ui @@ -2,6 +2,9 @@ LoginDialog + + Qt::ApplicationModal + 0 @@ -13,6 +16,9 @@ Dialog + + true + @@ -54,11 +60,20 @@ + + false + + + Qt::TabFocus + + + false + - Enter login name + - 128 + 32767 Enter login @@ -74,7 +89,16 @@ - + + Qt::TabFocus + + + false + + + QLineEdit::Password + + Enter password diff --git a/mainwindow.cpp b/mainwindow.cpp index a66a2f9..7a5e293 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -49,6 +49,7 @@ MainWindow::MainWindow(QWidget *parent) //init login dialog loginD.setupUi(&loginDialog); + loginD.loginEdit->setFocus(); // text of About QString aboutText = tr("

Client for rsync server

") + @@ -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); + } } } } diff --git a/mainwindow.h b/mainwindow.h index 34d01ad..94277e5 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -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 serversList; QSystemTrayIcon * trayIcon; QString icon = "/usr/share/icons/RsyncUI.png"; + bool rescan = false; + QList 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: