Debian Codegraph @

main.cpp

raw symbols SCIP
1 #include <QApplication>
2 #include <QTextCodec>
3 #include <QTranslator>
4 #include <QLocale>
5 #include <QString>
6 #include <QLibraryInfo>
7 #include <QDir>
8 #include <QStandardPaths>
9 #include <QMessageLogger>
10 #include <QProcess>
11 #include <QMessageBox>
12
13 #include "mainwindow.h"
14 #include "config_qnetstatview.h"
15 #include "DialogPasswordPrompt.h"
16
17 #if defined(Q_OS_WIN)
18 bool SetDebugPrivileges(){
19 TOKEN_PRIVILEGES tp;
20 LUID luid;
21 HANDLE hToken;
22
23 if(!OpenProcessToken(GetCurrentProcess(),TOKEN_READ|TOKEN_ADJUST_PRIVILEGES,&hToken))
24 {return false;}
25
26 if(!LookupPrivilegeValueA(NULL,"SeDebugPrivilege",&luid))
27 {CloseHandle(hToken);return false;}
28
29 tp.PrivilegeCount=1;
30 tp.Privileges[0].Luid=luid;
31 tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
32
33 if(!AdjustTokenPrivileges(hToken,FALSE,&tp,0,NULL,NULL))
34 {CloseHandle(hToken);return false;}
35
36 CloseHandle(hToken);
37 return true;
38 }
39 #endif
40
41 void printUsage(){
42 qInfo("%s",qPrintable(QObject::tr("Usage: qnetstatview [options]")));
43 qInfo("%s",qPrintable(QCoreApplication::applicationName() +" - "+QObject::tr("Shows detailed listings of all TCP and UDP endpoints")));
44 qInfo() << "";
45 qInfo() << "Options:";
46 qInfo("%s",qPrintable("-h, --help "+QObject::tr("Displays this help.")));
47 qInfo("%s",qPrintable("-v, --version "+QObject::tr("Displays version information.")));
48 qInfo("%s",qPrintable("--run-as-root "+QObject::tr("Run as root.")));
49 }
50
51 void runAsRoot(){
52 QString appForEnterRoot = "";
53
54 QStringList possibleApps {"kdesu5","kdesu","gksu","pkexec"};
55
56 for (QString app:possibleApps){
57 if (QStandardPaths::findExecutable(app)!=""){
58 appForEnterRoot=app;
59 break;
60 }
61 }
62
63 if (appForEnterRoot=="pkexec"){
64 system(QString("pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY "+QApplication::applicationDirPath()+"/"+qAppName()+" ").toStdString().data());
65 exit(0);
66 }
67
68
69 if (appForEnterRoot=="kdesu5" or appForEnterRoot=="kdesu"){
70 QProcess::startDetached(appForEnterRoot+" "+QApplication::applicationDirPath()+"/"+qAppName()+" 2> /dev/null &");
71 exit(0);
72 }
73
74 if (appForEnterRoot=="gksu"){
75 QProcess::startDetached("gksu -u root "+QApplication::applicationDirPath()+"/"+qAppName()+" 2> /dev/null &");
76 exit(0);
77 }
78
79
80 DialogPasswordPrompt ps;
81 if (ps.exec()){
82 QString password = ps.getPassword();
83
84 int code = system(QString("echo \""+password+"\" | sudo -S -b "+QApplication::applicationDirPath()+"/"+qAppName()+" &> /dev/null").toStdString().c_str());
85 if (code!=0){
86 QMessageBox::critical(nullptr,QObject::tr("Restart as root"),QObject::tr("Application startup failed. The password may have been typed incorrectly or you is not in the sudoers file. Restart as root canceled"));
87 }
88 exit(0);
89 }
90 }
91
92 int main(int argc, char *argv[]) {
93 QApplication app(argc, argv);
94
95 QCoreApplication::setOrganizationName("DanSoft");
96 QCoreApplication::setOrganizationDomain("dansoft.ru");
97 QCoreApplication::setApplicationVersion(QNETSTATVIEW_VERSION);
98 QCoreApplication::setApplicationName("QNetStatView");
99
100
101 QDir dirConfig(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
102 if (dirConfig.exists()==false) dirConfig.mkpath(dirConfig.path());
103
104 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
105
106 QTranslator translator;
107 QString locale = QLocale::system().name();
108
109 translator.load(QString(PATH_USERDATA)+QString("/langs/qnetstatview_") + locale);
110 app.installTranslator(&translator);
111
112 //set translator for default widget's text (for example: QMessageBox's buttons)
113 QTranslator qtTranslator;
114 qtTranslator.load("qt_"+locale,QLibraryInfo::location(QLibraryInfo::TranslationsPath));
115 app.installTranslator(&qtTranslator);
116
117 #if defined(Q_OS_WIN)
118 SetDebugPrivileges();
119 #endif
120
121
122 for (QString arg:QCoreApplication::arguments()){
123 if (arg=="--help" or arg=="-h"){
124 printUsage();
125 return 0;
126 }
127
128 if (arg=="--version" or arg=="-v"){
129 qInfo("QNetStatView: %s",QNETSTATVIEW_VERSION);
130 return 0;
131 }
132
133 if (arg.toLower()=="--run-as-root"){
134 runAsRoot();
135 return 0;
136 }
137 }
138
139
140
141 MainWindow window;
142 window.resize(800,600);
143 window.show();
144
145 return app.exec();
146 }
147