rsyncui/downloadfile.cpp

104 lines
2.3 KiB
C++
Raw Normal View History

2023-01-07 12:44:45 +01:00
#include "downloadfile.h"
#include "mainwindow.h"
#include "ui_mainwindow.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>
using namespace std;
downloadFile::downloadFile()
{
}
void downloadFile::cancelled()
{
this->canceled = true;
}
void downloadFile::download(QString savePath, MainWindow *mw)
{
string server;
string service;
string path;
string line;
string errorRsync;
char cmd[4096];
int portN;
int pos;
int pid;
QTreeWidgetItem * item;
stringstream output;
// QString savePath = ".";
vector<string> v;
int value;
//char command_out[1024] = {0};
array<char, 2048> buffer;
server.assign(mw->ui->khistorycombobox->currentText().toStdString());
portN = mw->ui->portEdit->text().toInt();
service = mw->ui->listWidget->currentItem()->text().toStdString();
pos = service.find_first_of('\n');
service.resize(pos);
item = mw->ui->treeWidget->currentItem();
path = item->text(0).toStdString();
while(item->parent() != NULL)
{
item = item->parent();
path = item->text(0).toStdString() + "/" + path;
};
sprintf(cmd, "rsync --bwlimit=100K --port %i -P %s::\"%s/%s\" \"%s/\" 2>&1", portN, server.c_str(), service.c_str(), path.c_str(), savePath.toStdString().c_str());
//unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
FILE * fp = popen2(cmd, "r", pid);
if (!fp)
{
throw runtime_error("popen2() failed!");
}
while (fgets(buffer.data(), buffer.size(), fp) != nullptr)
{
buffer.data();
if (this->canceled == true)
{
return;
}
line = buffer.data();
//cout << line << endl;
pos = line.find('%');
if (pos != -1)
{
line.erase(pos);
pos = line.find_last_of(' ');
if (pos != -1)
{
line.erase(0, pos);
value = stoi(line);
cout << value << endl;
emit progressSignal(value);
}
}
buffer.empty();
}
//cout << path << endl;
}