rsyncui/tools.cpp
2025-03-24 00:52:46 +08:00

172 lines
3.6 KiB
C++

#include "mainwindow.h"
#include "tools.h"
#include "password.h"
#include <QTextStream>
#include <QHostAddress>
using namespace std;
#define READ 0
#define WRITE 1
extern QMap<int, QString> rsyncErrorStrings;
extern QApplication a;
// test return code of rsync
// return true in case of error
bool testRsyncReturn(MainWindow * w, QProcess * myProcess)
{
info(DEBUGMACRO, "testRsyncReturn()");
info(DEBUGMACRO, "Exit status: " + QString::number(myProcess->exitStatus()));
info(DEBUGMACRO, "Exit code: " + QString::number(myProcess->exitCode()));
if (myProcess->exitStatus() != 0 and w->stopDlAsked != true)
{
info(DEBUGMACRO, "rsync error " + myProcess->errorString());
QMessageBox::warning(
w,
a.applicationName(),
QTranslator::tr("Rsync error:\n") + rsyncErrorStrings[myProcess->error()],
//myProcess->errorString(),
QMessageBox::Ok,
QMessageBox::Ok);
return true;
}
if(myProcess->exitCode() != 0)
{
info(DEBUGMACRO, "rsync error " + myProcess->errorString());
QMessageBox::warning(
w,
a.applicationName(),
QTranslator::tr("Rsync error:\n") + rsyncErrorStrings[myProcess->exitCode()],
QMessageBox::Ok,
QMessageBox::Ok);
return true;
}
w->stopDlAsked = false;
return false;
}
QString getFileType(QString filename)
{
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFile(filename);
QString returnValue = mime.name().section('/',0 ,0);
return returnValue;
}
QString preparePath(QString path)
{
return path.trimmed().replace(' ', "\ ");
}
int whatIpVersion(QString ipAddress)
{
QStringList fieldList;
// ulong field;
// uint i;
uint ipversion;
QHostAddress serverAddress;
info(DEBUGMACRO, "ipVersion of server at " + ipAddress);
if (!serverAddress.setAddress(ipAddress))
{
error ("Address " + ipAddress + " is bad");
}
ipversion = serverAddress.protocol();
return (ipversion==IPV4?4:6);
/*fieldList = ipAddress.split(":");
if (fieldList.count() == 8)
{
for (i=0;i<8;i++)
{
field = fieldList[i].toUInt();
if (field > 65535)
{
return 0;
}
}
info(DEBUGMACRO, "Address is Ip V6");
return 6;
}
fieldList = ipAddress.split(".");
if(fieldList.count() == 4)
{
for (i=0;i<4;i++)
{
field = fieldList[i].toUInt();
if (field > 255)
{
return 0;
}
}
info(DEBUGMACRO, "Address is ip V4");
return 4;
}*/
}
QHostAddress searchHosts(QString server)
{
QString line = "";
QStringList hosts;
QFile fileHandle("/etc/hosts");
QHostAddress serverAddress;
info(DEBUGMACRO, "reading /etc/hosts");
if (!fileHandle.open(QIODevice::ReadOnly))
{
QMessageBox::information(0,MainWindow::tr("Error"), fileHandle.errorString());
}else
{
QTextStream in(&fileHandle);
while(!in.atEnd())
{
line = in.readLine();
hosts = line.split(QRegularExpression("\\s+"));
if (hosts.contains(server))
{
//if (!serverAddress.isNull()) // and serverAddress.protocol() == QAbstractSocket::IPv4Protocol)
//{
if (!serverAddress.setAddress(hosts.first()))
{
error("Error setting address " + hosts.first());
}
//}
}
}
}
return serverAddress;
}
void warning(QString message)
{
QMessageBox::warning(
NULL,
a.applicationName() + "",
message,
QMessageBox::Ok,
QMessageBox::Ok);
}
void error(QString message)
{
QMessageBox::warning(
NULL,
a.applicationName() + ":" + QTranslator::tr("Error"),
message,
QMessageBox::Ok,
QMessageBox::Ok);
}
void info(QString debugHeader, QString message)
{
qInfo("%s %s", debugHeader.toStdString().c_str(), message.toStdString().c_str());
}
//void error()