version 1.9.2
This commit is contained in:
+59
-58
@@ -25,85 +25,86 @@ downloadFile::downloadFile()
|
||||
}
|
||||
|
||||
//Slot activated when download is cancelled
|
||||
void downloadFile::cancelled(int pid)
|
||||
void MainWindow::cancelled(QProcess * process)
|
||||
{
|
||||
if (pid == 0)
|
||||
{
|
||||
perror("Pid = 0 : I do not kill"); // Error rsync process not launched so it can't be killed
|
||||
}else if (kill(pid, SIGTERM) == -1)
|
||||
{
|
||||
//TODO managing error of kill
|
||||
}
|
||||
process->terminate();
|
||||
}
|
||||
|
||||
// launch a rsync processus downloading a file
|
||||
void downloadFile::download(MainWindow *mw)
|
||||
void MainWindow::download()
|
||||
{
|
||||
string line;
|
||||
string errorRsync;
|
||||
int pos;
|
||||
array<string,8> argv;
|
||||
stringstream output;
|
||||
vector<string> v;
|
||||
int value;
|
||||
char buffer[4096];
|
||||
Downloading d;
|
||||
|
||||
d = mw->downloading;
|
||||
QString cmd;
|
||||
QStringList param;
|
||||
|
||||
// Populating array with command and parameters for popen2
|
||||
argv[0] = "/usr/bin/rsync";
|
||||
if (mw->connexion.bandwidthLimit == 0)
|
||||
cmd = "rsync";
|
||||
if (this->connexion.bandwidthLimit != 0)
|
||||
{
|
||||
argv[1] = "--bwlimit=1000P";
|
||||
}else
|
||||
{
|
||||
argv[1] = "--bwlimit=" + to_string(mw->connexion.bandwidthLimit) + mw->connexion.bandwidthLimitUnit;
|
||||
param << "--bwlimit=" + QString::number(this->connexion.bandwidthLimit) + bwUnitChar[this->connexion.bandwidthLimitUnit];
|
||||
}
|
||||
argv[2] = "--port=" + to_string(mw->connexion.port);
|
||||
argv[3] = "-aP";
|
||||
argv[4] = d.server + "::" + d.service + "/" + d.path;
|
||||
argv[5] = d.savePath + "/";
|
||||
argv[6].clear();
|
||||
param << "--port=" + QString::number(this->connexion.port);
|
||||
param << "-aXP";
|
||||
param << this->downloading.server + "::" + this->downloading.service + "/" + this->downloading.path << this->downloading.savePath + "/";
|
||||
|
||||
//launching downloading thread
|
||||
FILE * fp = popen2(argv, "r", mw->pid);
|
||||
if (fp <= (FILE *) 0)
|
||||
{
|
||||
sprintf(buffer, "popen2() failed!: returning code:%d", fileno(fp));
|
||||
throw runtime_error(buffer);
|
||||
return;
|
||||
}
|
||||
this->downloading.process = new QProcess(this);
|
||||
|
||||
// waiting rsync output
|
||||
while (fgets(buffer, 4096, fp) != nullptr)
|
||||
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(readyReadStandardError()), this, SLOT(downloadProcessStderr()));
|
||||
connect(this->downloading.process, SIGNAL(readyReadStandardOutput()), this, SLOT(readRsyncOutput()));
|
||||
|
||||
this->downloading.process->start(cmd, param);
|
||||
}
|
||||
|
||||
void MainWindow::readRsyncOutput()
|
||||
{
|
||||
QString line;
|
||||
bool flag = false;
|
||||
int value;
|
||||
int pos;
|
||||
|
||||
while(!flag)
|
||||
{
|
||||
// Downloading is cancelled, we return
|
||||
if (this->canceled == true)
|
||||
line = QString::fromUtf8(this->downloading.process->readLine());
|
||||
if (line.isEmpty())
|
||||
{
|
||||
return;
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
line = buffer;
|
||||
|
||||
// extracting percentage of completion
|
||||
pos = line.find('%');
|
||||
pos = line.indexOf("%");
|
||||
if (pos != -1)
|
||||
{
|
||||
line.erase(pos);
|
||||
pos = line.find_last_of(' ');
|
||||
line.resize(pos);
|
||||
pos = line.lastIndexOf(' ');
|
||||
if (pos != -1)
|
||||
{
|
||||
line.erase(0, pos);
|
||||
value = stoi(line);
|
||||
line.remove(0, pos);
|
||||
value = line.toInt();
|
||||
// sending progress to Main window
|
||||
emit progressSignal(value);
|
||||
}
|
||||
}
|
||||
// download ended
|
||||
}
|
||||
pclose2(fp, mw->pid);
|
||||
|
||||
// ProgressBar to 100% and emit signal finished to main application
|
||||
emit progressSignal(100);
|
||||
emit finishedSignal(true);
|
||||
}
|
||||
|
||||
/*void MainWindow::downloadProcessError(QProcess::ProcessError error)
|
||||
{
|
||||
QMessageBox::warning(
|
||||
this,
|
||||
"RsyncUI",
|
||||
downloadProcessErrorString[error].toStdString().c_str()
|
||||
);
|
||||
}*/
|
||||
|
||||
void MainWindow::downloadProcessStderr()
|
||||
{
|
||||
QByteArray errorLine;
|
||||
|
||||
errorLine = this->downloading.process->readAllStandardError();
|
||||
QMessageBox::warning(
|
||||
this,
|
||||
"RsyncUI",
|
||||
errorLine
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user