rsyncui/downloadfile.cpp

112 lines
3.0 KiB
C++
Raw Normal View History

2023-01-07 12:44:45 +01:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "downloadfile.h"
2023-01-07 12:44:45 +01:00
#include "tools.h"
#include <pstreams/pstream.h>
#include <sstream>
#include <string>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <QMessageBox>
#include <vector>
#include <boost/algorithm/string/replace.hpp>
#include <memory>
#include <stdexcept>
#include <array>
2023-01-12 09:23:45 +01:00
#include <unistd.h>
#include <sys/types.h>
2023-01-07 12:44:45 +01:00
using namespace std;
// Initialization de la class
2023-01-07 12:44:45 +01:00
downloadFile::downloadFile()
{
}
//Slot activated when download is cancelled
2023-02-10 21:32:20 +01:00
void MainWindow::cancelled(QProcess * process)
2023-01-07 12:44:45 +01:00
{
2023-02-10 21:32:20 +01:00
process->terminate();
2023-01-07 12:44:45 +01:00
}
// launch a rsync processus downloading a file
2023-02-10 21:32:20 +01:00
void MainWindow::download()
2023-01-07 12:44:45 +01:00
{
2023-02-10 21:32:20 +01:00
QString cmd;
QStringList param;
2023-01-22 14:33:23 +01:00
// Populating array with command and parameters for popen2
2023-02-10 21:32:20 +01:00
cmd = "rsync";
if (this->connexion.bandwidthLimit != 0)
2023-01-22 14:33:23 +01:00
{
2023-02-10 21:32:20 +01:00
param << "--bwlimit=" + QString::number(this->connexion.bandwidthLimit) + bwUnitChar[this->connexion.bandwidthLimitUnit];
2023-01-22 14:33:23 +01:00
}
2023-02-10 21:32:20 +01:00
param << "--port=" + QString::number(this->connexion.port);
param << "-aXP";
param << this->downloading.server + "::" + this->downloading.service + "/" + this->downloading.path << this->downloading.savePath + "/";
2023-01-07 12:44:45 +01:00
2023-02-10 21:32:20 +01:00
this->downloading.process = new QProcess(this);
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);
}
2023-01-07 12:44:45 +01:00
2023-02-10 21:32:20 +01:00
void MainWindow::readRsyncOutput()
{
QString line;
bool flag = false;
int value;
int pos;
while(!flag)
2023-01-07 12:44:45 +01:00
{
2023-02-10 21:32:20 +01:00
line = QString::fromUtf8(this->downloading.process->readLine());
if (line.isEmpty())
2023-01-07 12:44:45 +01:00
{
2023-02-10 21:32:20 +01:00
flag = true;
break;
2023-01-07 12:44:45 +01:00
}
2023-02-10 21:32:20 +01:00
pos = line.indexOf("%");
2023-01-07 12:44:45 +01:00
if (pos != -1)
{
2023-02-10 21:32:20 +01:00
line.resize(pos);
pos = line.lastIndexOf(' ');
2023-01-07 12:44:45 +01:00
if (pos != -1)
{
2023-02-10 21:32:20 +01:00
line.remove(0, pos);
value = line.toInt();
// sending progress to Main window
2023-01-07 12:44:45 +01:00
emit progressSignal(value);
}
}
}
2023-02-10 21:32:20 +01:00
}
2023-01-22 14:33:23 +01:00
2023-02-10 21:32:20 +01:00
/*void MainWindow::downloadProcessError(QProcess::ProcessError error)
{
QMessageBox::warning(
this,
"RsyncUI",
downloadProcessErrorString[error].toStdString().c_str()
);
}*/
// process raise error
2023-02-10 21:32:20 +01:00
void MainWindow::downloadProcessStderr()
{
QByteArray errorLine;
errorLine = this->downloading.process->readAllStandardError();
QMessageBox::warning(
this,
"RsyncUI",
errorLine
);
2023-01-07 12:44:45 +01:00
}
2023-02-10 21:32:20 +01:00