Skip to content
Snippets Groups Projects
Commit 62d2d473 authored by Juhász Bálint's avatar Juhász Bálint
Browse files

improved stuff basically

parent 5add3e78
Branches
No related tags found
No related merge requests found
#include <QVector>
#include <QCoreApplication>
#include <QStringBuilder>
#include <QStringList>
#include <QTextStream>
#include <QSysInfo>
#include <QRegExp>
#include <QCoreApplication>
#include <QtNetwork/QNetworkInterface>
#include <QSysInfo>
#include "Globals.hpp"
#include "ProcessHandler.hpp"
#ifndef DIAGNOSTIC_HPP
#define DIAGNOSTIC_HPP
static QString gateway_ip;
class Diagnostic {
typedef QString (*diagFn_t)();
public:
typedef QString (Diagnostic::*function_t)();
QVector<function_t> func_vec;
static const int funcsLen = 4;
diagFn_t funcs[funcsLen];
Diagnostic()
: func_vec({
&Diagnostic::getRoutingInfo,
&Diagnostic::getPingOutput,
&Diagnostic::getInterfaceInfo,
&Diagnostic::getOsInfo
})
{}
Diagnostic(){
funcs[0] = getRoutingInfo;
funcs[1] = getOsInfo;
funcs[2] = getPingGateway;
funcs[3] = getInterfaceInfo;
}
QString getRoutingInfo(){
ProcessHandler proc(QIODevice::ReadOnly);
QStringList out;
static QString getOsInfo()
{
// Windows: http://www.windows-commandline.com/find-windows-os-version-from-command/
// Linux: http://superuser.com/questions/11008/how-do-i-find-out-what-version-of-linux-im-running
// I think the best way on linux is: uname -a
// Mac: http://superuser.com/questions/75166/how-to-find-out-mac-os-x-version-from-terminal
// The best way on Mac: sw_vers
return QString("- - - OS version - - - \nOS version");
}
out << "- - - Routing - - - \n";
static QString getRoutingInfo(){
// Useful commands: https://kb.wisc.edu/ns/page.php?id=12364
FILE *cmdHandle;
QString ipAddr("- - - Routing tables - - - \n");
#if defined( Q_OS_WIN )
cmdHandle = popen(
"netsh interface ipv4 show route", "r"
);
#elif defined ( Q_OS_LINUX )
cmdHandle = popen(
"netstat -A inet -rn", "r"
);
#else
#error "Sorry, we do not support this OS!"
#endif
const int BUFFSIZE = 256;
char buffer[BUFFSIZE];
fgets(buffer, BUFFSIZE-1, cmdHandle);
QRegExp regx("^(?:\\s*(?:0\\.0\\.0\\.0)){2}\\s*([^\\s]+)");
regx.indexIn(buffer);
if (regx.capturedTexts().size() > 1){
gateway_ip = regx.capturedTexts()[1];
ipAddr.append(gateway_ip);
}
else {
ipAddr.append("cannot find default gateway\n");
switch (CURRENT_OS){
case OS_WINDOWS:
out << proc.exec("netsh interface ipv4 show route");
//out << proc.exec("netsh interface ipv6 show route");
break;
case OS_LINUX:
out << proc.exec("netstat -A inet -rn");
//out << proc.exec("netstat -A inet6 -rn");
break;
case OS_OSX:
out << proc.exec("/usr/sbin/netstat -f inet -rn");
//out << proc.exec("/usr/sbin/netstat -f inet6 -rn");
break;
default:
static_assert(true, "This OS is not supported");
break;
}
return ipAddr.append("\n");
QString sep;
return out.join(sep);
}
static QString getInterfaceInfo(){
QString getInterfaceInfo(){
QList<QNetworkInterface> ifList = QNetworkInterface::allInterfaces();
QStringList out;
QString tab(" ");
QString tab(4, ' ');
out << "- - - Interface Info - - - \n";
out << "\n";
foreach (QNetworkInterface iface, ifList){
out << QString("%1\n").arg(iface.humanReadableName());
out << QString("%1Address entries:\n").arg(tab);
{
int address_cntr = 0;
foreach (QNetworkAddressEntry netAddr, iface.addressEntries()){
out << QString("%1%1- broadcast addr: %2\n").arg(tab).arg(netAddr.broadcast().toString());
out << QString("%1%1- ip addr: %2\n").arg(tab).arg(netAddr.ip().toString());
out << QString("%1%1- subnetmask: %2\n").arg(tab).arg(netAddr.netmask().toString());
out << QString("%1%1entry #%2\n").arg(tab).arg(address_cntr++);
out << QString("%1%1%1- broadcast addr: %2\n").arg(tab).arg(netAddr.broadcast().toString());
out << QString("%1%1%1- ip addr: %2\n").arg(tab).arg(netAddr.ip().toString());
out << QString("%1%1%1- subnetmask: %2\n").arg(tab).arg(netAddr.netmask().toString());
}
}
out << QString("%1- can broadcast? %2\n").arg(tab).arg(iface.flags().testFlag(QNetworkInterface::CanBroadcast)? "yes" : "no");
out << QString("%1- can multicast? %2\n").arg(tab).arg(iface.flags().testFlag(QNetworkInterface::CanMulticast)? "yes" : "no");
......@@ -94,34 +85,56 @@ public:
foreach (QHostAddress hostAddr, QNetworkInterface::allAddresses()){
out << QString("%1- %2\n").arg(tab).arg(hostAddr.toString());
}
return QString(out.join(' '));
QString sep;
return out.join(sep);
}
static QString getPingGateway(){
QString ping_stat("- - - Pinging Default Gateway - - -\n");
if (gateway_ip.isEmpty() == false){
QString getPingOutput(){
ProcessHandler proc(QIODevice::ReadOnly);
QStringList out;
QString ip("8.8.8.8");
FILE *cmdHandle;
cmdHandle = popen(
#if defined( Q_OS_WIN )
QString("ping -n 4 -w 1 %1").arg(gateway_ip).toStdString().c_str(), "r"
out << "- - - Pinging IPs - - -\n";
#elif defined ( Q_OS_LINUX )
QString("ping -c 4 -W 1 %1").arg(gateway_ip).toStdString().c_str(), "r"
#else
#error "We do not support ping for this OS."
#endif
);
const int BUFFSIZE = 1024;
char buffer[BUFFSIZE] = {'\0'};
fread(buffer, 1, BUFFSIZE-1, cmdHandle);
pclose(cmdHandle);
ping_stat.append(buffer);
switch (CURRENT_OS){
case OS_WINDOWS:
out << proc.exec(QString("ping -n 4 -w 1 %1").arg(ip));
break;
case OS_LINUX:
out << proc.exec(QString("ping -n -c 4 -W 1 %1").arg(ip));
break;
case OS_OSX:
default:
static_assert(true, "This OS is not supported");
break;
}
return out.join(' ');
}
QString getOsInfo(){
ProcessHandler proc(QIODevice::ReadOnly);
QStringList out;
out << "- - - OS Version - - -\n";
switch (CURRENT_OS) {
case OS_WINDOWS:
{
QRegExp e("(?:^[\r\n]+(?=[\r\n]))|(?:[\r\n]{2}(?=[\r\n]))|(?:[\r\n]+(?=[\r\n])$)");
out << proc.exec("wmic os get Caption,CSDVersion /value").replace(e, "");
}
else {
ping_stat.append("cannot ping gateway\n");
break;
case OS_LINUX:
out << proc.exec("lsb_release -a");
break;
case OS_OSX:
out << proc.exec("sw_vers");
break;
default:
static_assert(true, "This OS is not supported");
break;
}
return ping_stat;
QString sep;
return out.join(sep);
}
};
......
#include <QtGlobal>
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
//
// Compile time OS flags
//
enum OS_TYPE {OS_WINDOWS, OS_LINUX, OS_OSX};
#if defined(Q_OS_WIN)
enum OS_TYPE CURRENT_OS = OS_WINDOWS;
#elif defined(Q_OS_LINUX)
enum OS_TYPE CURRENT_OS = OS_LINUX;
#elif defined(Q_OS_OSX)
enum OS_TYPE CURRENT_OS = OS_OSX;
#endif
#endif // GLOBALS_HPP
......@@ -49,20 +49,21 @@ void NetCheckerWindow::initLogoSide(){
logo_side_w = new QWidget;
logo_side_l = new QVBoxLayout;
logo_side_w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
logo_side_l->setContentsMargins(9, 9, 0, 0);
logo_side_w->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
logo_side_w->setFixedWidth(LOGO_WIDTH);
/* logo image at the left */
logo_image_w = new QLabel;
QSvgRenderer renderer(tr(":/rsrc/logo.svg"));
QImage image(LOGO_WIDTH, static_cast<int>(1.376888*LOGO_WIDTH), QImage::Format_ARGB32);
image.fill(Qt::transparent); // partly transparent red-ish background
int image_width = LOGO_WIDTH - 9;
QImage image(image_width, static_cast<int>(1.376888*image_width), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
renderer.render(&painter);
//QPixmap pix_logo(":/rsrc/logo.png");
QPixmap pix_logo = QPixmap::fromImage(image);
pix_logo = pix_logo.scaledToWidth(LOGO_WIDTH, Qt::SmoothTransformation);
//pix_logo = pix_logo.scaledToWidth(image_width, Qt::SmoothTransformation);
logo_image_w->setPixmap(pix_logo);
logo_image_w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
......@@ -143,6 +144,7 @@ void NetCheckerWindow::initDiagnosticPage(){
log_w->setReadOnly(true);
log_w->setLineWrapMode(QTextEdit::NoWrap);
log_w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
log_w->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
log_holder_w->setLayout(log_holder_l);
log_holder_l->addWidget(log_w);
......@@ -152,7 +154,9 @@ void NetCheckerWindow::initDiagnosticPage(){
clipboard_button_w = new QPushButton;
save_button_w = new QPushButton;
clipboard_button_w->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
clipboard_button_w->setEnabled(false);
save_button_w->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
save_button_w->setEnabled(false);
connect(clipboard_button_w, SIGNAL(released()), this, SLOT(copyClipboard()));
connect(save_button_w, SIGNAL(released()), this, SLOT(saveToFile()));
......@@ -193,14 +197,15 @@ void NetCheckerWindow::initText(){
void NetCheckerWindow::nextPage(){
Diagnostic d;
global_w->setCurrentIndex(1);
double step = 100.0/Diagnostic::funcsLen;
for (int i = 0; i < Diagnostic::funcsLen; i++){
log_w->append((d.funcs[i])());
progressbar_w->setValue(static_cast<int>(ceil(step*(i+1))));
/*QMessageBox msg;
msg.setText(QString("[%1]").arg(d.gateway_ip));
msg.exec();*/
}
double step = 100.0/d.func_vec.size();
double i = 1.0;
foreach (Diagnostic::function_t fn, d.func_vec) {
log_w->append((d.*fn)());
progressbar_w->setValue(static_cast<int>(ceil(step*i)));
i += 1.0;
}
clipboard_button_w->setEnabled(true);
save_button_w->setEnabled(true);
}
void NetCheckerWindow::copyClipboard(){
......
......@@ -55,8 +55,8 @@ private:
void initDiagnosticPage();
void initText();
enum {
WINDOW_WIDTH = 350,
WINDOW_HEIGHT = 400,
WINDOW_WIDTH = 380,
WINDOW_HEIGHT = 420,
LOGO_WIDTH = 120,
README_WIDTH = (WINDOW_WIDTH-LOGO_WIDTH)
};
......
#include <QObject>
#include <QProcess>
#include <QStringList>
#ifndef PROCESSHANDLER_HPP
#define PROCESSHANDLER_HPP
class ProcessHandler : protected QProcess {
Q_OBJECT
public:
ProcessHandler(QProcess::OpenMode mode): QProcess() {
setOpenMode(mode);
}
public:
QString exec(QString commands){
start(commands);
waitForFinished();
QByteArray ba = readAllStandardOutput();
return ba;
}
};
#endif
......@@ -5,7 +5,7 @@
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
NetCheckerWindow dialog;
dialog.show();
NetCheckerWindow ncw;
ncw.show();
return app.exec();
}
......@@ -6,11 +6,15 @@ macx:ICON = app.hqx
HEADERS = \
include/NetCheckerWindow.hpp \
include/Diagnostic.hpp \
include/Translation.hpp
include/Translation.hpp \
include/ProcessHandler.hpp \
include/Globals.hpp
SOURCES = \
main.cpp \
include/NetCheckerWindow.cpp
CONFIG += c++11
# install
target.path = bin
INSTALLS += target
......
This diff is collapsed.
rsrc/logo.png

222 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment