removed Widgets from service part
This commit is contained in:
parent
f7926847ac
commit
23ad006187
11 changed files with 178 additions and 162 deletions
|
|
@ -1,6 +1,5 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QDesktopServices>
|
||||
#include <QDir>
|
||||
#include <QHostAddress>
|
||||
#include <QHostInfo>
|
||||
|
|
@ -10,15 +9,15 @@
|
|||
#include <QStandardPaths>
|
||||
#include <QUrl>
|
||||
|
||||
#include "version.h"
|
||||
#include "utilities.h"
|
||||
#include "version.h"
|
||||
|
||||
QString Utils::getRandomString(int len)
|
||||
{
|
||||
const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
||||
|
||||
QString randomString;
|
||||
for(int i=0; i<len; ++i) {
|
||||
for (int i = 0; i < len; ++i) {
|
||||
quint32 index = QRandomGenerator::global()->generate() % possibleCharacters.length();
|
||||
QChar nextChar = possibleCharacters.at(index);
|
||||
randomString.append(nextChar);
|
||||
|
|
@ -31,7 +30,7 @@ QString Utils::systemLogPath()
|
|||
#ifdef Q_OS_WIN
|
||||
QStringList locationList = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
|
||||
QString primaryLocation = "ProgramData";
|
||||
foreach (const QString& location, locationList) {
|
||||
foreach (const QString &location, locationList) {
|
||||
if (location.contains(primaryLocation)) {
|
||||
return QString("%1/%2/log").arg(location).arg(APPLICATION_NAME);
|
||||
}
|
||||
|
|
@ -42,7 +41,7 @@ QString Utils::systemLogPath()
|
|||
#endif
|
||||
}
|
||||
|
||||
bool Utils::initializePath(const QString& path)
|
||||
bool Utils::initializePath(const QString &path)
|
||||
{
|
||||
QDir dir;
|
||||
if (!dir.mkpath(path)) {
|
||||
|
|
@ -52,13 +51,13 @@ bool Utils::initializePath(const QString& path)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Utils::createEmptyFile(const QString& path)
|
||||
bool Utils::createEmptyFile(const QString &path)
|
||||
{
|
||||
QFile f(path);
|
||||
return f.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||
}
|
||||
|
||||
QString Utils::executable(const QString& baseName, bool absPath)
|
||||
QString Utils::executable(const QString &baseName, bool absPath)
|
||||
{
|
||||
QString ext;
|
||||
#ifdef Q_OS_WIN
|
||||
|
|
@ -71,7 +70,7 @@ QString Utils::executable(const QString& baseName, bool absPath)
|
|||
return QCoreApplication::applicationDirPath() + "/" + fileName;
|
||||
}
|
||||
|
||||
QString Utils::usrExecutable(const QString& baseName)
|
||||
QString Utils::usrExecutable(const QString &baseName)
|
||||
{
|
||||
if (QFileInfo::exists("/usr/sbin/" + baseName))
|
||||
return ("/usr/sbin/" + baseName);
|
||||
|
|
@ -79,18 +78,22 @@ QString Utils::usrExecutable(const QString& baseName)
|
|||
return ("/usr/bin/" + baseName);
|
||||
}
|
||||
|
||||
bool Utils::processIsRunning(const QString& fileName)
|
||||
bool Utils::processIsRunning(const QString &fileName)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
QProcess process;
|
||||
process.setReadChannel(QProcess::StandardOutput);
|
||||
process.setProcessChannelMode(QProcess::MergedChannels);
|
||||
process.start("wmic.exe", QStringList() << "/OUTPUT:STDOUT" << "PROCESS" << "get" << "Caption");
|
||||
process.start("wmic.exe",
|
||||
QStringList() << "/OUTPUT:STDOUT"
|
||||
<< "PROCESS"
|
||||
<< "get"
|
||||
<< "Caption");
|
||||
process.waitForStarted();
|
||||
process.waitForFinished();
|
||||
QString processData(process.readAll());
|
||||
QStringList processList = processData.split(QRegularExpression("[\r\n]"),Qt::SkipEmptyParts);
|
||||
foreach (const QString& rawLine, processList) {
|
||||
QStringList processList = processData.split(QRegularExpression("[\r\n]"), Qt::SkipEmptyParts);
|
||||
foreach (const QString &rawLine, processList) {
|
||||
const QString line = rawLine.simplified();
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
|
|
@ -99,7 +102,6 @@ bool Utils::processIsRunning(const QString& fileName)
|
|||
if (line == fileName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
#elif defined(Q_OS_IOS)
|
||||
|
|
@ -107,7 +109,7 @@ bool Utils::processIsRunning(const QString& fileName)
|
|||
#else
|
||||
QProcess process;
|
||||
process.setProcessChannelMode(QProcess::MergedChannels);
|
||||
process.start("pgrep", QStringList({fileName}));
|
||||
process.start("pgrep", QStringList({ fileName }));
|
||||
process.waitForFinished();
|
||||
if (process.exitStatus() == QProcess::NormalExit) {
|
||||
return (process.readAll().toUInt() > 0);
|
||||
|
|
@ -116,7 +118,7 @@ bool Utils::processIsRunning(const QString& fileName)
|
|||
#endif
|
||||
}
|
||||
|
||||
QString Utils::getIPAddress(const QString& host)
|
||||
QString Utils::getIPAddress(const QString &host)
|
||||
{
|
||||
if (ipAddressRegExp().match(host).hasMatch()) {
|
||||
return host;
|
||||
|
|
@ -130,42 +132,48 @@ QString Utils::getIPAddress(const QString& host)
|
|||
return "";
|
||||
}
|
||||
|
||||
QString Utils::getStringBetween(const QString& s, const QString& a, const QString& b)
|
||||
QString Utils::getStringBetween(const QString &s, const QString &a, const QString &b)
|
||||
{
|
||||
int ap = s.indexOf(a), bp = s.indexOf(b, ap + a.length());
|
||||
if(ap < 0 || bp < 0)
|
||||
if (ap < 0 || bp < 0)
|
||||
return QString();
|
||||
ap += a.length();
|
||||
if(bp - ap <= 0)
|
||||
if (bp - ap <= 0)
|
||||
return QString();
|
||||
return s.mid(ap, bp - ap).trimmed();
|
||||
}
|
||||
|
||||
bool Utils::checkIPv4Format(const QString& ip)
|
||||
bool Utils::checkIPv4Format(const QString &ip)
|
||||
{
|
||||
if (ip.isEmpty()) return false;
|
||||
if (ip.isEmpty())
|
||||
return false;
|
||||
int count = ip.count(".");
|
||||
if(count != 3) return false;
|
||||
if (count != 3)
|
||||
return false;
|
||||
|
||||
QHostAddress addr(ip);
|
||||
return (addr.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv4Protocol);
|
||||
return (addr.protocol() == QAbstractSocket::NetworkLayerProtocol::IPv4Protocol);
|
||||
}
|
||||
|
||||
bool Utils::checkIpSubnetFormat(const QString &ip)
|
||||
{
|
||||
if (!ip.contains("/")) return checkIPv4Format(ip);
|
||||
if (!ip.contains("/"))
|
||||
return checkIPv4Format(ip);
|
||||
|
||||
QStringList parts = ip.split("/");
|
||||
if (parts.size() != 2) return false;
|
||||
if (parts.size() != 2)
|
||||
return false;
|
||||
|
||||
bool ok;
|
||||
int subnet = parts.at(1).toInt(&ok);
|
||||
if (subnet >= 0 && subnet <= 32 && ok) return checkIPv4Format(parts.at(0));
|
||||
else return false;
|
||||
if (subnet >= 0 && subnet <= 32 && ok)
|
||||
return checkIPv4Format(parts.at(0));
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void Utils::killProcessByName(const QString &name)
|
||||
{
|
||||
{
|
||||
qDebug().noquote() << "Kill process" << name;
|
||||
#ifdef Q_OS_WIN
|
||||
QProcess::execute("taskkill", QStringList() << "/IM" << name << "/F");
|
||||
|
|
@ -178,40 +186,39 @@ void Utils::killProcessByName(const QString &name)
|
|||
|
||||
QString Utils::netMaskFromIpWithSubnet(const QString ip)
|
||||
{
|
||||
if (!ip.contains("/")) return "255.255.255.255";
|
||||
if (!ip.contains("/"))
|
||||
return "255.255.255.255";
|
||||
|
||||
bool ok;
|
||||
int prefix = ip.split("/").at(1).toInt(&ok);
|
||||
if (!ok) return "255.255.255.255";
|
||||
if (!ok)
|
||||
return "255.255.255.255";
|
||||
|
||||
unsigned long mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF;
|
||||
|
||||
return QString("%1.%2.%3.%4")
|
||||
.arg(mask >> 24)
|
||||
.arg((mask >> 16) & 0xFF)
|
||||
.arg((mask >> 8) & 0xFF)
|
||||
.arg( mask & 0xFF);
|
||||
return QString("%1.%2.%3.%4").arg(mask >> 24).arg((mask >> 16) & 0xFF).arg((mask >> 8) & 0xFF).arg(mask & 0xFF);
|
||||
}
|
||||
|
||||
QString Utils::ipAddressFromIpWithSubnet(const QString ip)
|
||||
{
|
||||
if (ip.count(".") != 3) return "";
|
||||
if (ip.count(".") != 3)
|
||||
return "";
|
||||
return ip.split("/").first();
|
||||
}
|
||||
|
||||
QStringList Utils::summarizeRoutes(const QStringList &ips, const QString cidr)
|
||||
{
|
||||
// QMap<int, int>
|
||||
// QHostAddress
|
||||
// QMap<int, int>
|
||||
// QHostAddress
|
||||
|
||||
// QMap<QString, QStringList> subnets; // <"a.b", <list subnets>>
|
||||
// QMap<QString, QStringList> subnets; // <"a.b", <list subnets>>
|
||||
|
||||
// for (const QString &ip : ips) {
|
||||
// if (ip.count(".") != 3) continue;
|
||||
// for (const QString &ip : ips) {
|
||||
// if (ip.count(".") != 3) continue;
|
||||
|
||||
// const QStringList &parts = ip.split(".");
|
||||
// subnets[parts.at(0) + "." + parts.at(1)].append(ip);
|
||||
// }
|
||||
// const QStringList &parts = ip.split(".");
|
||||
// subnets[parts.at(0) + "." + parts.at(1)].append(ip);
|
||||
// }
|
||||
|
||||
return QStringList();
|
||||
}
|
||||
|
|
@ -252,58 +259,6 @@ QString Utils::certUtilPath()
|
|||
#endif
|
||||
}
|
||||
|
||||
void Utils::saveFile(const QString &fileExtension,
|
||||
const QString &caption,
|
||||
const QString &fileName,
|
||||
const QString &data)
|
||||
{
|
||||
QString docDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
|
||||
QUrl fileUrl = QFileDialog::getSaveFileUrl(nullptr,
|
||||
caption,
|
||||
QUrl::fromLocalFile(docDir + "/" + fileName),
|
||||
"*" + fileExtension);
|
||||
if (fileUrl.isEmpty())
|
||||
return;
|
||||
if (!fileUrl.toString().endsWith(fileExtension)) {
|
||||
fileUrl = QUrl(fileUrl.toString() + fileExtension);
|
||||
}
|
||||
if (fileUrl.isEmpty())
|
||||
return;
|
||||
|
||||
QFile save(fileUrl.toLocalFile());
|
||||
|
||||
//todo check if save successful
|
||||
save.open(QIODevice::WriteOnly);
|
||||
save.write(data.toUtf8());
|
||||
save.close();
|
||||
|
||||
QFileInfo fi(fileUrl.toLocalFile());
|
||||
QDesktopServices::openUrl(fi.absoluteDir().absolutePath());
|
||||
}
|
||||
|
||||
QString Utils::getFileName(QWidget *parent,
|
||||
const QString &caption,
|
||||
const QString &dir,
|
||||
const QString &filter,
|
||||
QString *selectedFilter,
|
||||
QFileDialog::Options options)
|
||||
{
|
||||
QString fileName
|
||||
= QFileDialog::getOpenFileName(parent, caption, dir, filter, selectedFilter, options);
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
// patch for files containing spaces etc
|
||||
const QString sep{"raw%3A%2F"};
|
||||
if (fileName.startsWith("content://") && fileName.contains(sep)) {
|
||||
QString contentUrl = fileName.split(sep).at(0);
|
||||
QString rawUrl = fileName.split(sep).at(1);
|
||||
rawUrl.replace(" ", "%20");
|
||||
fileName = contentUrl + sep + rawUrl;
|
||||
}
|
||||
#endif
|
||||
return fileName;
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
// Inspired from http://stackoverflow.com/a/15281070/1529139
|
||||
// and http://stackoverflow.com/q/40059902/1529139
|
||||
|
|
@ -315,8 +270,7 @@ bool Utils::signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent)
|
|||
// (otherwise AttachConsole will return ERROR_ACCESS_DENIED)
|
||||
bool consoleDetached = (FreeConsole() != FALSE);
|
||||
|
||||
if (AttachConsole(dwProcessId) != FALSE)
|
||||
{
|
||||
if (AttachConsole(dwProcessId) != FALSE) {
|
||||
// Add a fake Ctrl-C handler for avoid instant kill is this console
|
||||
// WARNING: do not revert it or current program will be also killed
|
||||
SetConsoleCtrlHandler(nullptr, true);
|
||||
|
|
@ -324,11 +278,9 @@ bool Utils::signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent)
|
|||
FreeConsole();
|
||||
}
|
||||
|
||||
if (consoleDetached)
|
||||
{
|
||||
if (consoleDetached) {
|
||||
// Create a new console if previous was deleted by OS
|
||||
if (AttachConsole(thisConsoleId) == FALSE)
|
||||
{
|
||||
if (AttachConsole(thisConsoleId) == FALSE) {
|
||||
int errorCode = GetLastError();
|
||||
if (errorCode == 31) // 31=ERROR_GEN_FAILURE
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue