rsyncui/downloadfile.cpp

128 lines
3.8 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "downloadfile.h"
#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>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
// Initialization de la class
downloadFile::downloadFile()
{
}
//Slot activated when download is cancelled
void MainWindow::cancelled(QProcess * process)
{
bool n = 0;
process->terminate();
n = process->waitForFinished(30000);
if (n == false)
{
process->close();
}
}
// launch a rsync processus downloading a file
void MainWindow::download()
{
QString cmd;
QStringList param;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
this->downloading.process = new QProcess(this);
if (!this->downloading.user.isEmpty())
{
this->downloading.server.prepend(this->connexion.user + "@");
env.insert("RSYNC_PASSWORD", this->downloading.password); // Add an environment variable
this->downloading.process->setProcessEnvironment(env);
}
// Populating array with command and parameters for popen2
cmd = "rsync";
if (this->connexion.bandwidthLimit != 0)
{
param << "--bwlimit=" + QString::number(this->connexion.bandwidthLimit) + bwUnitChar[this->connexion.bandwidthLimitUnit];
}
param << "--port=" + QString::number(this->downloading.port);
param << "-aXP";
param << this->downloading.server + "::" + this->downloading.service + "/" + this->downloading.path << this->downloading.savePath + "/";
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;
static QString dlSpeed;
QStringList list;
static QString filename;
int i;
int listSize;
while(!flag)
{
list.clear();
line = QString::fromUtf8(this->downloading.process->readLine());
if (line.isEmpty())
{
flag = true;
break;
}else
{
pos = line.indexOf("%");
if (pos != -1)
{
line = line.simplified();
list = line.split(" ");
listSize = list.count() / 4;
for (i = 0; i < listSize; i++)
{
value = list.at(i *4 + 1).chopped(1).toInt();
dlSpeed = list.at(i * 4 + 2);
/*line.resize(pos);
pos = line.lastIndexOf(' ');
if (pos != -1)
{
line.remove(0, pos);
value = line.toInt();*/
// sending progress to Main window
emit progressSignal(value);
emit fileName(filename + " %p%" + "\t " + dlSpeed);
}
}else
{
if (!line.contains("receiving"))
{
filename = line.remove(QChar('\n'), Qt::CaseInsensitive);
emit fileName(filename + " %p%\t " + dlSpeed);
}
}
}
}
}