modified algo filling treeview to speed

This commit is contained in:
Daniel Tartavel 2023-01-26 11:31:56 +01:00
parent eb7795c791
commit 012573f89d
4 changed files with 115 additions and 47 deletions

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-01-11T01:16:59. --> <!-- Written by QtCreator 4.14.2, 2023-01-25T23:35:55. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -32,7 +32,7 @@ MainWindow::MainWindow(QWidget *parent)
// init of widgets // init of widgets
ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget); ui->ktreewidgetsearchline->setTreeWidget(ui->treeWidget);
ui->ktreewidgetsearchline->setCaseSensitivity(Qt::CaseInsensitive); 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")) if (this->settings.contains("connexion/lastServer"))
{ {
ui->portEdit->setText(this->settings.value("connexion/port").toString()); ui->portEdit->setText(this->settings.value("connexion/port").toString());
@ -43,7 +43,6 @@ MainWindow::MainWindow(QWidget *parent)
ui->khistorycombobox->clear(); ui->khistorycombobox->clear();
} }
ui->progressBar->hide(); ui->progressBar->hide();
populateList(); populateList();
@ -87,7 +86,7 @@ void MainWindow::closeEvent (QCloseEvent *event)
} }
// Populate treeview with list of files // Populate treeview with list of files
void MainWindow::populateTree() void MainWindow::populateTree(QTreeWidgetItem * parent)
{ {
stringstream ss; stringstream ss;
vector<string> path; vector<string> path;
@ -103,7 +102,7 @@ void MainWindow::populateTree()
{ {
// server is validated // server is validated
path = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2); 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 // Restoring cursor
QGuiApplication::restoreOverrideCursor(); QGuiApplication::restoreOverrideCursor();
@ -193,37 +192,41 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri
string errorRsync; string errorRsync;
vector<string> v; vector<string> v;
QTreeWidgetItem * item; 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 ); 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); redi::ipstream in(cmd, redi::pstreams::pstdout | redi::pstreams::pstderr);
while (getline(in.out(), line)) while (getline(in.out(), line))
{ {
v = explode(line, ' ', 5); v = explode(line, ' ', 5);
if (v.size() == 5) if (v.size() == 5)
{ {
if (v[4].at(0) != '.' and (v[0].at(0) == '-' or v[0].at(0) == 'd')) 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') if (v[0].at(0) == 'd')
{ {
sprintf(npath, "%s%s/", path.c_str(), v[4].c_str()); isDir = true;
scanDir(server, portN, item, npath); }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 reading stdout stopped at EOF then reset the state:
if (in.eof() && in.fail()) if (in.eof() && in.fail())
in.clear(); in.clear();
// read child's stderr // read child's stderr
while (getline(in.err(), line)) while (getline(in.err(), line))
{ {
@ -231,6 +234,7 @@ void MainWindow::scanDir(string server, int portN, QTreeWidgetItem *parent, stri
errorRsync.append(line); errorRsync.append(line);
errorRsync.append("\n"); errorRsync.append("\n");
} }
if ( !errorRsync.empty()) if ( !errorRsync.empty())
{ {
QMessageBox::warning( QMessageBox::warning(
@ -332,26 +336,41 @@ void MainWindow::on_connectButton_clicked()
} }
// add a dir in treeview // 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(QTreeWidget * parent, int type = Type)
QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget); QTreeWidgetItem *treeItem = new QTreeWidgetItem(ui->treeWidget);
// QTreeWidgetItem::setText(int column, const QString & text) // QTreeWidgetItem::setText(int column, const QString & text)
treeItem->setText(0, name); if (isDir == true)
treeItem->setText(1, fileSize); {
treeItem->setText(0, "Dir");
}else
{
treeItem->setText(0,"File");
}
treeItem->setText(1, name);
treeItem->setText(2, fileSize);
return treeItem; return treeItem;
} }
// add a file in treeview // 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(QTreeWidget * parent, int type = Type)
QTreeWidgetItem *treeItem = new QTreeWidgetItem(); QTreeWidgetItem *treeItem = new QTreeWidgetItem();
// QTreeWidgetItem::setText(int column, const QString & text) // QTreeWidgetItem::setText(int column, const QString & text)
treeItem->setText(0, name); if (isDir == true)
treeItem->setText(1, fileSize); {
treeItem->setText(0, "Dir");
}else
{
treeItem->setText(0,"File");
}
treeItem->setText(1, name);
treeItem->setText(2, fileSize);
// QTreeWidgetItem::addChild(QTreeWidgetItem * child) // QTreeWidgetItem::addChild(QTreeWidgetItem * child)
parent->addChild(treeItem); parent->addChild(treeItem);
@ -364,7 +383,7 @@ void MainWindow::on_listWidget_clicked()
vector<string> v; vector<string> v;
v = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2); v = explode(ui->listWidget->currentItem()->text().toStdString(), '\n', 2);
this->downloading.service = v[0]; this->downloading.service = v[0];
populateTree(); populateTree(NULL);
} }
//Slot activated when a file is clicked in the treeview //Slot activated when a file is clicked in the treeview
@ -372,24 +391,40 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item)
{ {
QFuture<void> future; QFuture<void> future;
QFileDialog dialog; QFileDialog dialog;
QTreeWidgetItem * itemR;
string path;
item = ui->treeWidget->currentItem(); //item = ui->treeWidget->currentItem();
this->downloading.path = item->text(0).toStdString(); itemR = item;
while(item->parent() != NULL)
path = item->text(1).toStdString();
while(itemR->parent() != NULL)
{ {
item = item->parent(); itemR = itemR->parent();
this->downloading.path = item->text(0).toStdString() + "/" + this->downloading.path; path = itemR->text(1).toStdString() + "/" + path;
}; };
if (item->text(0) == "File")
{
// Item is a file
this->downloading.path = path;
if (this->downloading.savePath.empty()) if (this->downloading.savePath.empty())
{ {
on_DefaultSaveFolder_triggered(); on_DefaultSaveFolder_triggered();
} }else if (this->downloading.pid == 0)
if (!this->downloading.savePath.empty() && this->downloading.pid == 0)
{ {
startDownloading(); startDownloading();
sleep(1);
} }
ui->listDownload->addItem(QString::fromStdString(this->downloading.path)); ui->listDownload->addItem(QString::fromStdString(this->downloading.path));
}else
{
//Item is a Directory
scanDir(this->connexion.server, this->connexion.port, item, this->downloading.service + "/" + path +"/");
item->setExpanded(true);
}
} }
// Launch the thread which download the file // Launch the thread which download the file
@ -459,10 +494,10 @@ void MainWindow::on_listDownload_itemClicked(QListWidgetItem *item)
// load settings // load settings
void MainWindow::loadSettings() 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->restoreGeometry(settings.value("window/geometry").toByteArray());
this->restoreState(settings.value("window/state").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->restoreState(settings.value("splitter/state").toByteArray());
ui->splitter_2->restoreState(settings.value("splitter2/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/geometry", saveGeometry());
this->settings.setValue("window/state", saveState()); 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("splitter/state", ui->splitter->saveState());
this->settings.setValue("splitter2/state", ui->splitter_2->saveState()); this->settings.setValue("splitter2/state", ui->splitter_2->saveState());
this->settings.setValue("connexion/lastServer", QString::fromStdString(this->connexion.server)); this->settings.setValue("connexion/lastServer", QString::fromStdString(this->connexion.server));
@ -520,8 +555,7 @@ void MainWindow::on_DefaultSaveFolder_triggered()
{ {
QFileDialog dialog; QFileDialog dialog;
this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), this->downloading.dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString(); this->downloading.savePath = dialog.getExistingDirectory(this, tr("Choose directory to save file"), QString::fromStdString(this->downloading.savePath), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks).toStdString();
// this->downloading.dirPath = this->downloading.savePath.c_str();
this->settings.setValue("Folder/", this->downloading.savePath.c_str()); this->settings.setValue("Folder/", this->downloading.savePath.c_str());
this->settings.sync(); this->settings.sync();
} }
@ -529,7 +563,7 @@ void MainWindow::on_DefaultSaveFolder_triggered()
// Activated when menu "settings" is clicked // Activated when menu "settings" is clicked
void MainWindow::on_action_Settings_triggered() 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); config.spinBox->setValue(this->connexion.bandwidthLimit);
Configuration.show(); Configuration.show();
} }
@ -548,7 +582,7 @@ void MainWindow::on_buttonBox_accepted()
}else }else
{ {
this->connexion.bandwidthLimit = config.spinBox->value(); 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("bandwidthlimit", this->connexion.bandwidthLimit);
this->settings.setValue("bandwidthlimitunit", this->connexion.bandwidthLimitUnit.c_str()); this->settings.setValue("bandwidthlimitunit", this->connexion.bandwidthLimitUnit.c_str());

View File

@ -31,6 +31,7 @@
#include <QShortcut> #include <QShortcut>
#include <QCloseEvent> #include <QCloseEvent>
#include <unistd.h> #include <unistd.h>
#include <magic.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
@ -52,7 +53,6 @@ class Downloading
std::string path; std::string path;
std::string defaultSavePath; std::string defaultSavePath;
std::string savePath; std::string savePath;
QString dirPath;
int pid = 0; int pid = 0;
}; };
@ -78,13 +78,13 @@ class MainWindow : public QMainWindow
~MainWindow(); ~MainWindow();
QProgressDialog *progress; QProgressDialog *progress;
void displayTree(); void displayTree();
void populateTree(); void populateTree(QTreeWidgetItem * parent);
void populateList(); void populateList();
void listServices(); void listServices();
bool validateServer(std::string server); bool validateServer(std::string server);
bool isIpAddress(std::string server); bool isIpAddress(std::string server);
QTreeWidgetItem * addTreeRoot(QString name, QString description); QTreeWidgetItem * addTreeRoot(QString name, QString description, bool isDir);
QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size); QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size, bool isDir);
void scanDir(std::string server, int portN, QTreeWidgetItem *parent = NULL, std::string path = "" ); void scanDir(std::string server, int portN, QTreeWidgetItem *parent = NULL, std::string path = "" );
void startDownloading(); void startDownloading();
void loadSettings(); void loadSettings();
@ -99,6 +99,13 @@ class MainWindow : public QMainWindow
QDialog Configuration; QDialog Configuration;
Ui::Configuration config; Ui::Configuration config;
std::vector <QString> serversList; std::vector <QString> serversList;
map<char, int> bwUnixIndex {
{'B', 0},
{'K', 1},
{'M', 2},
{'G', 3},
{'P', 4}
};
private slots: private slots:

View File

@ -57,7 +57,7 @@
<cursorShape>IBeamCursor</cursorShape> <cursorShape>IBeamCursor</cursorShape>
</property> </property>
<property name="focusPolicy"> <property name="focusPolicy">
<enum>Qt::TabFocus</enum> <enum>Qt::WheelFocus</enum>
</property> </property>
<property name="acceptDrops"> <property name="acceptDrops">
<bool>true</bool> <bool>true</bool>
@ -139,6 +139,9 @@
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip"> <property name="toolTip">
<string>Enter rsync port on server</string> <string>Enter rsync port on server</string>
</property> </property>
@ -173,6 +176,9 @@
</item> </item>
<item> <item>
<widget class="QPushButton" name="connectButton"> <widget class="QPushButton" name="connectButton">
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip"> <property name="toolTip">
<string extracomment="Connect to server">Press button to connect to rsync server</string> <string extracomment="Connect to server">Press button to connect to rsync server</string>
</property> </property>
@ -205,6 +211,9 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip"> <property name="toolTip">
<string>Click to view the list of files of this folder</string> <string>Click to view the list of files of this folder</string>
</property> </property>
@ -214,6 +223,9 @@
<property name="editTriggers"> <property name="editTriggers">
<set>QAbstractItemView::SelectedClicked</set> <set>QAbstractItemView::SelectedClicked</set>
</property> </property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="resizeMode"> <property name="resizeMode">
<enum>QListView::Adjust</enum> <enum>QListView::Adjust</enum>
</property> </property>
@ -231,6 +243,9 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="contextMenuPolicy"> <property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum> <enum>Qt::NoContextMenu</enum>
</property> </property>
@ -241,11 +256,14 @@
<number>5000</number> <number>5000</number>
</property> </property>
<property name="whatsThis"> <property name="whatsThis">
<string extracomment="Click to add to download queue"/> <string/>
</property> </property>
<property name="editTriggers"> <property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set> <set>QAbstractItemView::NoEditTriggers</set>
</property> </property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="showDropIndicator" stdset="0"> <property name="showDropIndicator" stdset="0">
<bool>false</bool> <bool>false</bool>
</property> </property>
@ -270,6 +288,9 @@
<property name="sortingEnabled"> <property name="sortingEnabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="animated">
<bool>true</bool>
</property>
<property name="columnCount"> <property name="columnCount">
<number>1</number> <number>1</number>
</property> </property>
@ -303,9 +324,15 @@
</widget> </widget>
</widget> </widget>
<widget class="QListWidget" name="listDownload"> <widget class="QListWidget" name="listDownload">
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="toolTip"> <property name="toolTip">
<string>Click on file to stop downloading</string> <string>Click on file to stop downloading</string>
</property> </property>
<property name="toolTipDuration">
<number>5000</number>
</property>
<property name="dragEnabled"> <property name="dragEnabled">
<bool>true</bool> <bool>true</bool>
</property> </property>