rsyncui/downloadfile.cpp

110 lines
2.6 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-01-12 09:23:45 +01:00
void downloadFile::cancelled(int pid)
2023-01-07 12:44:45 +01:00
{
if (pid == 0)
2023-01-12 09:23:45 +01:00
{
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
2023-01-12 09:23:45 +01:00
}
2023-01-07 12:44:45 +01:00
}
// launch a rsync processus downloading a file
2023-01-12 09:23:45 +01:00
void downloadFile::download(MainWindow *mw)
2023-01-07 12:44:45 +01:00
{
string line;
string errorRsync;
int pos;
array<string,8> argv;
2023-01-07 12:44:45 +01:00
stringstream output;
vector<string> v;
int value;
2023-01-12 09:23:45 +01:00
char buffer[4096];
2023-02-02 16:10:51 +01:00
Downloading d;
d = mw->downloading;
2023-01-22 14:33:23 +01:00
// Populating array with command and parameters for popen2
2023-01-12 09:23:45 +01:00
argv[0] = "/usr/bin/rsync";
2023-01-22 14:33:23 +01:00
if (mw->connexion.bandwidthLimit == 0)
{
argv[1] = "--bwlimit=1000P";
}else
{
argv[1] = "--bwlimit=" + to_string(mw->connexion.bandwidthLimit) + mw->connexion.bandwidthLimitUnit;
2023-01-22 14:33:23 +01:00
}
2023-01-12 09:23:45 +01:00
argv[2] = "--port=" + to_string(mw->connexion.port);
2023-02-02 17:14:04 +01:00
argv[3] = "-aP";
2023-02-02 16:10:51 +01:00
argv[4] = d.server + "::" + d.service + "/" + d.path;
argv[5] = d.savePath + "/";
argv[6].clear();
2023-01-07 12:44:45 +01:00
2023-01-22 14:33:23 +01:00
//launching downloading thread
2023-02-02 16:10:51 +01:00
FILE * fp = popen2(argv, "r", mw->pid);
if (fp <= (FILE *) 0)
2023-01-07 12:44:45 +01:00
{
sprintf(buffer, "popen2() failed!: returning code:%d", fileno(fp));
throw runtime_error(buffer);
2023-01-12 09:23:45 +01:00
return;
2023-01-07 12:44:45 +01:00
}
// waiting rsync output
2023-01-12 09:23:45 +01:00
while (fgets(buffer, 4096, fp) != nullptr)
2023-01-07 12:44:45 +01:00
{
// Downloading is cancelled, we return
2023-01-07 12:44:45 +01:00
if (this->canceled == true)
{
return;
}
2023-01-12 09:23:45 +01:00
line = buffer;
// extracting percentage of completion
2023-01-07 12:44:45 +01:00
pos = line.find('%');
if (pos != -1)
{
line.erase(pos);
pos = line.find_last_of(' ');
if (pos != -1)
{
line.erase(0, pos);
value = stoi(line);
// sending progress to Main window
2023-01-07 12:44:45 +01:00
emit progressSignal(value);
}
}
// download ended
2023-01-07 12:44:45 +01:00
}
2023-02-02 16:10:51 +01:00
pclose2(fp, mw->pid);
2023-01-22 14:33:23 +01:00
// ProgressBar to 100% and emit signal finished to main application
2023-01-12 09:23:45 +01:00
emit progressSignal(100);
emit finishedSignal(true);
2023-01-07 12:44:45 +01:00
}