forked from mansoor-s/Linux-Memory-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessList.cpp
134 lines (120 loc) · 3.52 KB
/
ProcessList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <QDebug>
#include <iostream>
#include <QMainWindow>
#include "ProcessList.h"
#include "ui_ProcessList.h"
#include "mainwindow.h"
ProcessList::ProcessList(QWidget *parent) :
QDialog(parent),
ui(new Ui::ProcessList)
{
ui->setupUi(this);
// ui->tableWidget->setColumnCount(2);
// QTableWidgetItem *header0 = new QTableWidgetItem(QString("PID"));
// QTableWidgetItem *header1 = new QTableWidgetItem(QString("Process name"));
// ui->tableWidget->setHorizontalHeaderItem(0,header0);
// ui->tableWidget->setHorizontalHeaderItem(1,header1);
QHeaderView *headerLayout = new QHeaderView(Qt::Horizontal,ui->tableWidget);
headerLayout->setResizeMode(QHeaderView::Stretch);
ui->tableWidget->setHorizontalHeader(headerLayout);
try
{
QMap<int, QString> processList;
processList = getCurrentProcesses();
if(processList.count() < 1)
{
throw(QString("Process list is empty!"));
}
ui->tableWidget->setRowCount(processList.count());
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableWidget->setColumnWidth(0, ui->tableWidget->width() / 2);
QMapIterator<int, QString> i(processList);
int j = 0;
while(i.hasNext())
{
i.next();
QTableWidgetItem *processPID = new QTableWidgetItem(QString::number(i.key(), 10));
QTableWidgetItem *processName = new QTableWidgetItem(QString(i.value()));
ui->tableWidget->setItem(j, 0, processPID);
ui->tableWidget->setItem(j, 1, processName);
j++;
}
}
catch(QString e)
{
qDebug() << e;
}
ui->label_user->setText(currUser);
}
/*
*
* read and parse ps -u $CURRENTUSER$ f
* and return the PID and proess name
* also sets (private) currUser (used in UI)
*/
QMap<int, QString> ProcessList::getCurrentProcesses()
{
QMap<int, QString> *processList= new QMap<int, QString>;
int PID;
char buffer[256] = {0};
// get current user
FILE *proc = popen("whoami", "r");
fgets(buffer, sizeof(buffer), proc);
pclose(proc);
char buff2[256] = {0};
strncpy(buff2, buffer, strlen(buffer) - 1); // get rid of the new line char
currUser = QString(buff2);
sprintf(buffer, "ps -u %s -f", buff2);
proc= popen(buffer, "r");
for(;fgets(buff2, sizeof(buff2), proc);)
{
strncpy(buffer, "\0",1);
// we only need PID and process name
sscanf(buff2,"%*s %d %*d %*s %*s %*s %*s %s", &PID, buffer);
if(strlen(buffer) != 0)
{
processList->insert(PID, QString(buffer));
}
}
return *processList;
}
ProcessList::~ProcessList()
{
delete ui;
}
void ProcessList::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type())
{
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void ProcessList::exit()
{
this->close();
}
/*
* Gives the MainWindow class back the PID and the Name of the application that the user has selected
*
*/
void ProcessList::selectProc()
{
QMap<int, QString> targetProcess;
int row = ui->tableWidget->currentItem()->row();
QTableWidgetItem *cell = ui->tableWidget->item(row,0);
int pid = cell->text().toInt();
cell = ui->tableWidget->item(row,1);
QString procName = cell->text();
targetProcess[pid] = procName;
((MainWindow *) parentWidget())->setTarget(targetProcess);
((MainWindow *) parentWidget())->setWindowTitle(((MainWindow *) parentWidget())->windowTitle() + " - " + procName);
// enable the first scan button which will the enable the rest of the controls
((MainWindow *) parentWidget())->enableScan();
this->close();
}