| 1 | #include "ProcessList.h" |
| 2 | |
| 3 | #include <QMessageBox> |
| 4 | #include <QSettings> |
| 5 | #include <QDir> |
| 6 | #include <QStandardPaths> |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
| 13 | #include <signal.h> |
| 14 | #include <sys/wait.h> |
| 15 | #endif |
| 16 | |
| 17 | #if defined(Q_OS_WIN) |
| 18 | #include <windows.h> |
| 19 | #include <psapi.h> |
| 20 | #endif |
| 21 | |
| 22 | #include <QDebug> |
| 23 | |
| 24 | ProcessList::ProcessList(){ |
| 25 | |
| 26 | } |
| 27 | |
| 28 | void ProcessList::killProcess(QString pid){ |
| 29 | |
| 30 | if (pid=="" or pid.toInt()==0){ |
| 31 | QMessageBox::critical(nullptr,"KillProcess",QObject::tr("You cannot kill this process")); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | #if defined(Q_OS_LINUX) || defined(Q_OS_UNIX) |
| 36 | if (kill(pid.toInt(),SIGTERM)){ |
| 37 | QMessageBox::critical(nullptr,"KillProcess",QObject::tr("Killing of process was not successful")); |
| 38 | } |
| 39 | #elif defined(Q_OS_WIN) |
| 40 | HANDLE tmpHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pid.toInt()); |
| 41 | if (NULL != tmpHandle){ |
| 42 | TerminateProcess(tmpHandle, 0); |
| 43 | } |
| 44 | #else |
| 45 | QMessageBox::critical(nullptr,"KillProcess",QObject::tr("Your OS does not support killProcess")); |
| 46 | #endif |
| 47 | |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void ProcessList::closeConnection(QString from, QString to){ |
| 52 | QSettings conf(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)+"/qnetstatview.ini", QSettings::IniFormat); |
| 53 | conf.setPath(QSettings::IniFormat, QSettings::UserScope, QDir::currentPath()); |
| 54 | |
| 55 | QString utiliteProcKill=conf.value("setup/prockill","").toString(); |
| 56 | |
| 57 | if (utiliteProcKill==""){ |
| 58 | QMessageBox::critical(nullptr,"closeConnection",QObject::tr("set utilities for kill connections")); |
| 59 | }else{ |
| 60 | QStringList fromList = from.split(":"); |
| 61 | QStringList toList = to.split(":"); |
| 62 | |
| 63 | utiliteProcKill.replace("%LOCAL_IP%",fromList.at(0)); |
| 64 | utiliteProcKill.replace("%LOCAL_PORT%",fromList.at(1)); |
| 65 | utiliteProcKill.replace("%REMOTE_IP%",toList.at(0)); |
| 66 | utiliteProcKill.replace("%REMOTE_PORT%",toList.at(1)); |
| 67 | |
| 68 | system(utiliteProcKill.toStdString().c_str()); |
| 69 | } |
| 70 | |
| 71 | } |