diff --git a/RsyncUI.pro.user b/RsyncUI.pro.user index 2021afe..d120c4b 100644 --- a/RsyncUI.pro.user +++ b/RsyncUI.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/mainwindow.cpp b/mainwindow.cpp index f1660e0..16b5f12 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -32,7 +32,7 @@ MainWindow::MainWindow(QWidget *parent) // init of widgets ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget); ui->ktreewidgetsearchline->setCaseSensitivity(Qt::CaseInsensitive); - ui->treeWidget->setHeaderLabels({tr("Path"), tr("Size")} ); + ui->treeWidget->setHeaderLabels({tr("Type"), tr("Path"), tr("Size")} ); if (this->settings.contains("connexion/lastServer")) { ui->portEdit->setText(this->settings.value("connexion/port").toString()); @@ -43,7 +43,6 @@ MainWindow::MainWindow(QWidget *parent) ui->khistorycombobox->clear(); } - ui->progressBar->hide(); populateList(); @@ -87,7 +86,7 @@ void MainWindow::closeEvent (QCloseEvent *event) } // Populate treeview with list of files -void MainWindow::populateTree() +void MainWindow::populateTree(QTreeWidgetItem * parent) { stringstream ss; vector path; @@ -103,7 +102,7 @@ void MainWindow::populateTree() { // server is validated path = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2); - scanDir(this->connexion.server, this->connexion.port, NULL, path[0].append("/") ); + scanDir(this->connexion.server, this->connexion.port, parent, path[0].append("/") ); } // Restoring cursor QGuiApplication::restoreOverrideCursor(); @@ -193,37 +192,41 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri string errorRsync; vector v; QTreeWidgetItem * item; - char npath[4096]; + bool isDir = false; sprintf(cmd, "rsync --contimeout=10 -P \"%s::%s\" --port %d ", server.c_str(), path.c_str(), portN ); redi::ipstream in(cmd, redi::pstreams::pstdout | redi::pstreams::pstderr); while (getline(in.out(), line)) { - v = explode(line, ' ', 5); if (v.size() == 5) { if (v[4].at(0) != '.' and (v[0].at(0) == '-' or v[0].at(0) == 'd')) { - if (parent != NULL) - { - item = addTreeChild(parent,QString::fromStdString(v[4]), QString::fromStdString(v[1])); - }else - { - item = addTreeRoot(QString::fromStdString(v[4]), QString::fromStdString(v[1])); - } if (v[0].at(0) == 'd') { - sprintf(npath, "%s%s/", path.c_str(), v[4].c_str()); - scanDir(server, portN, item, npath); + isDir = true; + }else + { + isDir = false; } + if (parent != NULL) + { + item = addTreeChild(parent,QString::fromStdString(v[4]), QString::fromStdString(v[1]), isDir); + }else + { + item = addTreeRoot(QString::fromStdString(v[4]), QString::fromStdString(v[1]), isDir); + } + } } } + // if reading stdout stopped at EOF then reset the state: if (in.eof() && in.fail()) in.clear(); + // read child's stderr while (getline(in.err(), line)) { @@ -231,6 +234,7 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri errorRsync.append(line); errorRsync.append("\n"); } + if ( !errorRsync.empty()) { QMessageBox::warning( @@ -332,26 +336,41 @@ void MainWindow::on_connectButton_clicked() } // add a dir in treeview -QTreeWidgetItem * MainWindow::addTreeRoot(QString name, QString fileSize) +QTreeWidgetItem * MainWindow::addTreeRoot(QString name, QString fileSize, bool isDir) { // QTreeWidgetItem(QTreeWidget * parent, int type = Type) QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget); // QTreeWidgetItem::setText(int column, const QString & text) - treeItem->setText(0, name); - treeItem->setText(1, fileSize); + if (isDir == true) + { + treeItem->setText(0, "Dir"); + }else + { + treeItem->setText(0,"File"); + } + treeItem->setText(1, name); + treeItem->setText(2, fileSize); + return treeItem; } // add a file in treeview -QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name, QString fileSize) +QTreeWidgetItem * MainWindow::addTreeChild(QTreeWidgetItem *parent, QString name, QString fileSize, bool isDir) { // QTreeWidgetItem(QTreeWidget * parent, int type = Type) QTreeWidgetItem *treeItem = new QTreeWidgetItem(); // QTreeWidgetItem::setText(int column, const QString & text) - treeItem->setText(0, name); - treeItem->setText(1, fileSize); + if (isDir == true) + { + treeItem->setText(0, "Dir"); + }else + { + treeItem->setText(0,"File"); + } + treeItem->setText(1, name); + treeItem->setText(2, fileSize); // QTreeWidgetItem::addChild(QTreeWidgetItem * child) parent->addChild(treeItem); @@ -364,7 +383,7 @@ void MainWindow::on_listWidget_clicked() vector v; v = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2); this->downloading.service = v[0]; - populateTree(); + populateTree(NULL); } //Slot activated when a file is clicked in the treeview @@ -372,24 +391,40 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item) { QFuture future; QFileDialog dialog; + QTreeWidgetItem * itemR; + string path; - item = ui->treeWidget->currentItem(); - this->downloading.path = item->text(0).toStdString(); - while(item->parent() != NULL) + //item = ui->treeWidget->currentItem(); + itemR = item; + + path = item->text(1).toStdString(); + while(itemR->parent() != NULL) { - item = item->parent(); - this->downloading.path = item->text(0).toStdString() + "/" + this->downloading.path; + itemR = itemR->parent(); + path = itemR->text(1).toStdString() + "/" + path; }; - if (this->downloading.savePath.empty()) + if (item->text(0) == "File") { - on_DefaultSaveFolder_triggered(); - } - if (!this->downloading.savePath.empty() && this->downloading.pid == 0) + // Item is a file + this->downloading.path = path; + if (this->downloading.savePath.empty()) + { + on_DefaultSaveFolder_triggered(); + }else if (this->downloading.pid == 0) + { + startDownloading(); + sleep(1); + + } + ui->listDownload->addItem(QString::fromStdString(this->downloading.path)); + }else { - startDownloading(); + //Item is a Directory + scanDir(this->connexion.server, this->connexion.port, item, this->downloading.service + "/" + path +"/"); + item->setExpanded(true); } - ui->listDownload->addItem(QString::fromStdString(this->downloading.path)); + } // Launch the thread which download the file @@ -459,10 +494,10 @@ void MainWindow::on_listDownload_itemClicked(QListWidgetItem *item) // load settings void MainWindow::loadSettings() { - // restoring geometry and state of wondow and widgets + // restoring geometry and state of window and widgets this->restoreGeometry(settings.value("window/geometry").toByteArray()); this->restoreState(settings.value("window/state").toByteArray()); - ui->treeWidget->header()->restoreState(settings.value("treeView/state").toByteArray()); + ui->treeWidget->header()->restoreState(settings.value("treeWidget/state").toByteArray()); ui->splitter->restoreState(settings.value("splitter/state").toByteArray()); ui->splitter_2->restoreState(settings.value("splitter2/state").toByteArray()); @@ -489,7 +524,7 @@ void MainWindow::saveSettings() { this->settings.setValue("window/geometry", saveGeometry()); this->settings.setValue("window/state", saveState()); - this->settings.setValue("treeView/state", ui->treeWidget->header()->saveState()); + this->settings.setValue("treeWidget/state", ui->treeWidget->header()->saveState()); this->settings.setValue("splitter/state", ui->splitter->saveState()); this->settings.setValue("splitter2/state", ui->splitter_2->saveState()); this->settings.setValue("connexion/lastServer", QString::fromStdString(this->connexion.server)); @@ -520,8 +555,7 @@ void MainWindow::on_DefaultSaveFolder_triggered() { QFileDialog dialog; - this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), this->downloading.dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString(); - // this->downloading.dirPath = this->downloading.savePath.c_str(); + this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), QString::fromStdString(this->downloading.savePath), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString(); this->settings.setValue("Folder/", this->downloading.savePath.c_str()); this->settings.sync(); } @@ -529,7 +563,7 @@ void MainWindow::on_DefaultSaveFolder_triggered() // Activated when menu "settings" is clicked void MainWindow::on_action_Settings_triggered() { - config.UnitCombobox->setCurrentText(QString::fromStdString(this->connexion.bandwidthLimitUnit)); + config.UnitCombobox->setCurrentIndex(bwUnixIndex[this->connexion.bandwidthLimitUnit[0]]); config.spinBox->setValue(this->connexion.bandwidthLimit); Configuration.show(); } @@ -548,7 +582,7 @@ void MainWindow::on_buttonBox_accepted() }else { this->connexion.bandwidthLimit = config.spinBox->value(); - this->connexion.bandwidthLimitUnit = config.UnitCombobox->currentText().toStdString(); + this->connexion.bandwidthLimitUnit = config.UnitCombobox->currentText().toStdString()[0]; } this->settings.setValue("bandwidthlimit", this->connexion.bandwidthLimit); this->settings.setValue("bandwidthlimitunit", this->connexion.bandwidthLimitUnit.c_str()); diff --git a/mainwindow.h b/mainwindow.h index 431ac22..2021135 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -31,6 +31,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -52,7 +53,6 @@ class Downloading std::string path; std::string defaultSavePath; std::string savePath; - QString dirPath; int pid = 0; }; @@ -78,13 +78,13 @@ class MainWindow : public QMainWindow ~MainWindow(); QProgressDialog *progress; void displayTree(); - void populateTree(); + void populateTree(QTreeWidgetItem * parent); void populateList(); void listServices(); bool validateServer(std::string server); bool isIpAddress(std::string server); - QTreeWidgetItem * addTreeRoot(QString name, QString description); - QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size); + QTreeWidgetItem * addTreeRoot(QString name, QString description, bool isDir); + QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size, bool isDir); void scanDir(std::string server, int portN, QTreeWidgetItem *parent = NULL, std::string path = "" ); void startDownloading(); void loadSettings(); @@ -99,6 +99,13 @@ class MainWindow : public QMainWindow QDialog Configuration; Ui::Configuration config; std::vector serversList; + map bwUnixIndex { + {'B', 0}, + {'K', 1}, + {'M', 2}, + {'G', 3}, + {'P', 4} + }; private slots: diff --git a/mainwindow.ui b/mainwindow.ui index 2a6d695..95d80d3 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -57,7 +57,7 @@ IBeamCursor - Qt::TabFocus + Qt::WheelFocus true @@ -139,6 +139,9 @@ 16777215 + + Qt::WheelFocus + Enter rsync port on server @@ -173,6 +176,9 @@ + + Qt::WheelFocus + Press button to connect to rsync server @@ -205,6 +211,9 @@ 0 + + Qt::WheelFocus + Click to view the list of files of this folder @@ -214,6 +223,9 @@ QAbstractItemView::SelectedClicked + + true + QListView::Adjust @@ -231,6 +243,9 @@ 0 + + Qt::WheelFocus + Qt::NoContextMenu @@ -241,11 +256,14 @@ 5000 - + QAbstractItemView::NoEditTriggers + + true + false @@ -270,6 +288,9 @@ true + + true + 1 @@ -303,9 +324,15 @@ + + Qt::WheelFocus + Click on file to stop downloading + + 5000 + true