diff --git a/include/Globals.hpp b/include/Globals.hpp index 3ba9c51dc30f9da1909025d97b9664811b6bf6c4..966fec80322e5921915d1c379eeaea24e0c37e8e 100644 --- a/include/Globals.hpp +++ b/include/Globals.hpp @@ -9,9 +9,8 @@ // enum OS_TYPE {OS_WINDOWS, OS_LINUX, OS_OSX, OS_UNKNOWN}; -#if defined(Q_OS_WIN) +#if defined(Q_OS_WIN) constexpr enum OS_TYPE CURRENT_OS = OS_WINDOWS; - #include <windows.h> #elif defined(Q_OS_LINUX) constexpr enum OS_TYPE CURRENT_OS = OS_LINUX; #elif defined(Q_OS_OSX) @@ -23,33 +22,11 @@ enum OS_TYPE {OS_WINDOWS, OS_LINUX, OS_OSX, OS_UNKNOWN}; // // Config // -QStringList DEF_GATEWAYS; -QStringList OUTER_DEST; -QStringList NS_ADDRS; -QString DNS_ADDR; +extern QStringList DEF_GATEWAYS; +extern QStringList OUTER_DEST; +extern QStringList NS_ADDRS; +extern QString DNS_ADDR; -void INIT_GLOBALS(){ - DEF_GATEWAYS << "152.66.176.254"; - DEF_GATEWAYS << "152.66.178.254"; - DEF_GATEWAYS << "152.66.179.254"; - DEF_GATEWAYS << "152.66.180.254"; - DEF_GATEWAYS << "152.66.181.254"; - DEF_GATEWAYS << "152.66.182.254"; - DEF_GATEWAYS << "152.66.183.254"; - DEF_GATEWAYS << "152.66.210.254"; - DEF_GATEWAYS << "10.66.255.254"; - - OUTER_DEST << "152.66.0.1"; - OUTER_DEST << "152.66.0.15"; - OUTER_DEST << "8.8.8.8"; - OUTER_DEST << "8.8.4.4"; - - NS_ADDRS << "152.66.208.1"; - NS_ADDRS << "152.66.208.7"; - NS_ADDRS << "8.8.8.8"; - NS_ADDRS << "8.8.4.4"; - - DNS_ADDR = "sch.bme.hu"; -} +void INIT_GLOBALS(); #endif // GLOBALS_HPP diff --git a/include/ProcessHandler.hpp b/include/ProcessHandler.hpp index 406d978a23eaca64af3dcf5a4ef442ee2f0f5ee5..4fc5fc4def72c9b766c6a67fe113ec2b3e6c41c4 100644 --- a/include/ProcessHandler.hpp +++ b/include/ProcessHandler.hpp @@ -8,18 +8,9 @@ class ProcessHandler : protected QProcess { Q_OBJECT public: - ProcessHandler(QProcess::OpenMode mode): QProcess() { - setOpenMode(mode); - } + ProcessHandler(QProcess::OpenMode mode); public: - QString exec(QString commands){ - start(commands); - if (!waitForFinished()){ - return tr("Error occured. Command was: %1\n").arg(commands); - } - QByteArray ba = readAllStandardOutput(); - return ba; - } + QString exec(QString commands); }; #endif diff --git a/include/Translation.hpp b/include/Translation.hpp index 5816f539beb6020ff34ffcd8d4dc12c977e9bf40..36d09d6b9ea6f21bdd831e93dcaa9aa27dac7367 100644 --- a/include/Translation.hpp +++ b/include/Translation.hpp @@ -7,7 +7,7 @@ struct LANG { enum {ENG = 0, HUN = 1}; }; QString TXT_APP_TITLE[] = { "NetChecker", "NetChecker" }; -QString README_URL[] = { ":/rsrc/readme_en.html", ":/rsrc/readme_hu.html" }; +QString README_URL[] = { ":/res/readme_en.html", ":/res/readme_hu.html" }; QString TXT_NEXT_BTN[] = { "Next", "Tovább" }; QString TXT_DIAG_GROUP[] = { "Running diagnostics", "Diagnosztika futtatása" }; QString TXT_EST_TIME[] = { "Estimated time", "Várható befejezés" }; diff --git a/include/privileges.hpp b/include/privileges.hpp index a6d838e1afd5ddb94f04eb734062d0a1e245216a..65a5c053bede789e8ac409a0169c1967564632b4 100644 --- a/include/privileges.hpp +++ b/include/privileges.hpp @@ -4,33 +4,12 @@ #ifndef PRIVILEGES_HPP #define PRIVILEGES_HPP -struct Privileges { +class Privileges { - static bool checkPrivileges(QWidget *parent, const QString title, const QString message){ - bool (*isRoot)(); - // TODO: isRoot_linux, isRoot_osx - if (!isRoot_win()){ - QMessageBox::warning(parent, title, message, QMessageBox::Ok, QMessageBox::NoButton); - return false; - } - return true; - } +public: + static void checkPrivileges(QWidget*, const QString, const QString); - static bool isRoot_win(){ - BOOL fRet = FALSE; - HANDLE hToken = NULL; - if( OpenProcessToken( GetCurrentProcess( ),TOKEN_QUERY,&hToken ) ) { - TOKEN_ELEVATION Elevation; - DWORD cbSize = sizeof( TOKEN_ELEVATION ); - if( GetTokenInformation( hToken, TokenElevation, &Elevation, sizeof( Elevation ), &cbSize ) ) { - fRet = Elevation.TokenIsElevated; - } - } - if( hToken ) { - CloseHandle( hToken ); - } - return fRet; - } + static bool isRoot(); }; #endif // PRIVILEGES_HPP diff --git a/main.cpp b/main.cpp index 3db647a8f404c64f59421d781791c25eeee76b9b..3706401bc75855c16695e08f182fb17650acdb63 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ #include <QApplication> -#include "include/NetCheckerWindow.hpp" +#include <include/NetCheckerWindow.hpp> int main(int argc, char *argv[]) { diff --git a/netchecker.pro b/netchecker.pro index b064c813f39073786e1a93dcc693bd908a92e5b4..75b16f16329503f4ee36e2fa8ac03c7e5d168753 100644 --- a/netchecker.pro +++ b/netchecker.pro @@ -1,19 +1,22 @@ QT += widgets network svg -win32:RC_ICONS += rsrc/app.ico -macx:ICON = app.hqx +win32:RC_ICONS += res/app.ico +macx:ICON = res/app.hqx HEADERS = \ include/NetCheckerWindow.hpp \ include/Diagnostic.hpp \ + include/Globals.hpp \ include/Translation.hpp \ include/ProcessHandler.hpp \ - include/Globals.hpp \ include/Privileges.hpp SOURCES = \ main.cpp \ - include/NetCheckerWindow.cpp \ - include/Diagnostic.cpp + src/Diagnostic.cpp \ + src/Globals.cpp \ + src/NetCheckerWindow.cpp \ + src/Privileges.cpp \ + src/ProcessHandler.cpp # install target.path = bin @@ -23,3 +26,6 @@ RESOURCES += \ resource.qrc CONFIG += c++11 + +QMAKE_CFLAGS_WARN_ON +QMAKE_CXXFLAGS_WARN_ON diff --git a/netchecker.pro.user b/netchecker.pro.user index 705875404d6e399bd06da0da1a528e93d7afc7ed..7e03c0d70a16a57299434754579feb856ec688d9 100644 --- a/netchecker.pro.user +++ b/netchecker.pro.user @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE QtCreatorProject> -<!-- Written by QtCreator 3.4.2, 2015-08-11T13:30:51. --> +<!-- Written by QtCreator 3.4.2, 2015-08-12T14:16:22. --> <qtcreator> <data> <variable>EnvironmentId</variable> @@ -61,7 +61,7 @@ <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.5.0 MinGW 32bit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.5.0 MinGW 32bit</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.55.win32_mingw492_kit</value> - <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value> + <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value> <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> diff --git a/rsrc/app.hqx b/res/app.hqx similarity index 100% rename from rsrc/app.hqx rename to res/app.hqx diff --git a/rsrc/app.ico b/res/app.ico similarity index 100% rename from rsrc/app.ico rename to res/app.ico diff --git a/rsrc/flag_eng.gif b/res/flag_eng.gif similarity index 100% rename from rsrc/flag_eng.gif rename to res/flag_eng.gif diff --git a/rsrc/flag_hun.gif b/res/flag_hun.gif similarity index 100% rename from rsrc/flag_hun.gif rename to res/flag_hun.gif diff --git a/rsrc/logo.svg b/res/logo.svg similarity index 100% rename from rsrc/logo.svg rename to res/logo.svg diff --git a/rsrc/readme_en.html b/res/readme_en.html similarity index 100% rename from rsrc/readme_en.html rename to res/readme_en.html diff --git a/rsrc/readme_hu.html b/res/readme_hu.html similarity index 100% rename from rsrc/readme_hu.html rename to res/readme_hu.html diff --git a/resource.qrc b/resource.qrc index 79a68ba0f7214db584803c6fd8e18e1c67f65a75..d9f80107a3242d9777724bdb4e0fad096ace3889 100644 --- a/resource.qrc +++ b/resource.qrc @@ -1,9 +1,11 @@ <RCC> <qresource prefix="/"> - <file>rsrc/logo.svg</file> - <file>rsrc/readme_en.html</file> - <file>rsrc/readme_hu.html</file> - <file>rsrc/flag_eng.gif</file> - <file>rsrc/flag_hun.gif</file> + <file>res/app.hqx</file> + <file>res/app.ico</file> + <file>res/flag_eng.gif</file> + <file>res/flag_hun.gif</file> + <file>res/logo.svg</file> + <file>res/readme_en.html</file> + <file>res/readme_hu.html</file> </qresource> </RCC> diff --git a/include/Diagnostic.cpp b/src/Diagnostic.cpp similarity index 95% rename from include/Diagnostic.cpp rename to src/Diagnostic.cpp index aeb474104df22a3b3ee0e0980b2d9e102521745b..fbf4a564f116bfecc57058ee45cb1634168c528c 100644 --- a/include/Diagnostic.cpp +++ b/src/Diagnostic.cpp @@ -1,5 +1,3 @@ -#include "Diagnostic.hpp" - #include <QtWidgets> #include <QVector> #include <QCoreApplication> @@ -10,9 +8,9 @@ #include <QtNetwork/QHostInfo> #include <QtNetwork/QDnsLookup> -#include "Globals.hpp" -#include "ProcessHandler.hpp" - +#include <include/Diagnostic.hpp> +#include <include/Globals.hpp> +#include <include/ProcessHandler.hpp> void Diagnostic::onStarted(){ double step = 100.0/func_vec.size(); diff --git a/src/Globals.cpp b/src/Globals.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4e1a55a3c983f43b0c9841255f8f422f680974a --- /dev/null +++ b/src/Globals.cpp @@ -0,0 +1,36 @@ +#include <include/Globals.hpp> + +#ifndef GLOBALS_CPP +#define GLOBALS_CPP + +QStringList DEF_GATEWAYS; +QStringList OUTER_DEST; +QStringList NS_ADDRS; +QString DNS_ADDR; + +void INIT_GLOBALS(){ + DEF_GATEWAYS << "152.66.176.254"; + DEF_GATEWAYS << "152.66.178.254"; + DEF_GATEWAYS << "152.66.179.254"; + DEF_GATEWAYS << "152.66.180.254"; + DEF_GATEWAYS << "152.66.181.254"; + DEF_GATEWAYS << "152.66.182.254"; + DEF_GATEWAYS << "152.66.183.254"; + DEF_GATEWAYS << "152.66.210.254"; + DEF_GATEWAYS << "10.66.255.254"; + + OUTER_DEST << "152.66.0.1"; + OUTER_DEST << "152.66.0.15"; + OUTER_DEST << "8.8.8.8"; + OUTER_DEST << "8.8.4.4"; + + NS_ADDRS << "152.66.208.1"; + NS_ADDRS << "152.66.208.7"; + NS_ADDRS << "8.8.8.8"; + NS_ADDRS << "8.8.4.4"; + + DNS_ADDR = "sch.bme.hu"; +} + +#endif // GLOBALS_CPP + diff --git a/include/NetCheckerWindow.cpp b/src/NetCheckerWindow.cpp similarity index 93% rename from include/NetCheckerWindow.cpp rename to src/NetCheckerWindow.cpp index 2c2fa4859ac6bb6b49b3055cbe9488df64bf878a..578325cd6b8e96069fd4e29a7d94460b6308c371 100644 --- a/include/NetCheckerWindow.cpp +++ b/src/NetCheckerWindow.cpp @@ -2,9 +2,9 @@ #include <QPainter> #include <QImage> -#include "NetCheckerWindow.hpp" -#include "Privileges.hpp" -#include "Translation.hpp" +#include <include/NetCheckerWindow.hpp> +#include <include/Privileges.hpp> +#include <include/Translation.hpp> NetCheckerWindow::NetCheckerWindow(){ /* default language is hungarian */ @@ -60,7 +60,7 @@ void NetCheckerWindow::initLogoSide(){ /* logo image at the left */ logo_image_w = new QLabel; - QSvgRenderer renderer(tr(":/rsrc/logo.svg")); + QSvgRenderer renderer(tr(":/res/logo.svg")); int image_width = LOGO_WIDTH - 9; QImage image(image_width, static_cast<int>(1.376888*image_width), QImage::Format_ARGB32); image.fill(Qt::transparent); @@ -75,8 +75,8 @@ void NetCheckerWindow::initLogoSide(){ QHBoxLayout *flag_holder_l = new QHBoxLayout; QPushButton *flag_eng_w = new QPushButton; QPushButton *flag_hun_w = new QPushButton; - QPixmap pix_flag_eng(":/rsrc/flag_eng.gif"); - QPixmap pix_flag_hun(":/rsrc/flag_hun.gif"); + QPixmap pix_flag_eng(":/res/flag_eng.gif"); + QPixmap pix_flag_hun(":/res/flag_hun.gif"); QIcon icon_eng(pix_flag_eng); QIcon icon_hun(pix_flag_hun); QSize icon_size(pix_flag_eng.width(), pix_flag_eng.height()); @@ -236,9 +236,7 @@ void NetCheckerWindow::initText(){ void NetCheckerWindow::nextPage(){ /* check for root privileges */ - if (!Privileges::checkPrivileges(this, TXT_ROOT_MSG_TITLE_WIN[lang], TXT_ROOT_MSG_WIN[lang])){ - close(); - } + Privileges::checkPrivileges(this, TXT_ROOT_MSG_TITLE_WIN[lang], TXT_ROOT_MSG_WIN[lang]); /* turns page */ global_w->setCurrentIndex(1); @@ -271,8 +269,11 @@ void NetCheckerWindow::updateInfos(int progress_value, const QString info){ } void NetCheckerWindow::finishAction(){ - /* if diagnostic is over stop the timer and change text to finished */ + /* if diagnostic is over, stop the timer and change text to finished */ progress_timer->stop(); + if (!progress_info_time->isHidden()){ + progress_info_time->hide(); + } progress_info->setText(TXT_FINISHED[lang]); /* enable save buttons */ @@ -286,8 +287,11 @@ void NetCheckerWindow::copyClipboard(){ } void NetCheckerWindow::saveToFile(){ + QString username = qgetenv("USER"); + if (username.isEmpty()) + username = qgetenv("USERNAME"); QString filename = QFileDialog::getExistingDirectory(0, TXT_SAVE_DIALOG[lang]); - filename.append(tr("/%1.txt").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_h-m-s"))); + filename.append(tr("/%1_%2.txt").arg(username).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_h-m-s"))); if (!filename.isEmpty()){ QFile file(filename); if (file.open(QIODevice::WriteOnly | QIODevice::Text)){ @@ -322,6 +326,8 @@ void NetCheckerWindow::incrementTime(){ } else { progress_info->setText(TXT_ALMOST_END[lang]); - progress_info_time->hide(); + if (!progress_info_time->isHidden()){ + progress_info_time->hide(); + } } } diff --git a/src/Privileges.cpp b/src/Privileges.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b00e4d8b169f911270aab77865e13822fe5dc122 --- /dev/null +++ b/src/Privileges.cpp @@ -0,0 +1,47 @@ +#include <QString> +#include <QMessageBox> + +#include <include/Globals.hpp> +#include <include/ProcessHandler.hpp> +#include <include/Privileges.hpp> + +#ifndef PRIVILEGES_CPP +#define PRIVILEGES_CPP + +void Privileges::checkPrivileges(QWidget *parent, const QString title, const QString message){ + if (!isRoot()){ + QMessageBox::warning(parent, title, message, QMessageBox::Ok, QMessageBox::NoButton); + parent->close(); + } +} + +#if defined(Q_OS_WIN) + #include <windows.h> + bool Privileges::isRoot(){ + BOOL fRet = FALSE; + HANDLE hToken = NULL; + if( OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)){ + TOKEN_ELEVATION Elevation; + DWORD cbSize = sizeof(TOKEN_ELEVATION); + if(GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation),&cbSize)){ + fRet = Elevation.TokenIsElevated; + } + } + if(hToken){ + CloseHandle(hToken); + } + return fRet; + } +#elif defined(Q_OS_LINUX) + #include <unistd.h> + bool Privileges::isRoot(){ + return !(getuid() && geteuid()); + } +#elif defined(Q_OS_OSX) + #error "OSX is not supported yet!" +#else + #error "This IS is not supported yet!" +#endif + +#endif // PRIVILEGES_CPP + diff --git a/src/ProcessHandler.cpp b/src/ProcessHandler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0b210db826701a7fcad689c5a63c64a65fcfdc4 --- /dev/null +++ b/src/ProcessHandler.cpp @@ -0,0 +1,23 @@ +#include <QObject> +#include <QProcess> +#include <QStringList> + +#include <include/ProcessHandler.hpp> + +#ifndef PROCESSHANDLER_CPP +#define PROCESSHANDLER_CPP + +ProcessHandler::ProcessHandler(QProcess::OpenMode mode): QProcess() { + setOpenMode(mode); +} +QString ProcessHandler::exec(QString commands){ + start(commands); + if (!waitForFinished()){ + return tr("Error occured. Command was: %1\n").arg(commands); + } + QByteArray ba = readAllStandardOutput(); + return ba; +} + +#endif +