-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
152 lines (128 loc) · 4.57 KB
/
mainwindow.cpp
File metadata and controls
152 lines (128 loc) · 4.57 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*Author: Yifan Wang
Date: Aug 25th 2021
References:
https://forum.qt.io/topic/91556/how-do-i-solve-the-error-specializing-member-requires-template-syntax/6
https://stackoverflow.com/a/4644922
https://blog.csdn.net/ljheee/article/details/70230016
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "server.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
firstConnect = 1;
QWidget::connect(ui->tabs, SIGNAL( currentChanged(int)),this, SLOT( onTabChange(int)));
QWidget::connect(ui->killButton, SIGNAL(clicked(bool)),this, SLOT(onKillProcess()));
QWidget::connect(ui->refreshButton, SIGNAL(clicked(bool)),this,SLOT(onRefreshProcess()));
showTab(1);
}
void MainWindow::showTab(int currentTab)
{
//if at process page
if(currentTab == 1)
{
ui->processList->clear();
QDir qd("/proc");
QString str = qd.entryList().join("\n"), sPid, text, pName, pState, pMemory;
int head=3, totalProcess=0, nPSleep=0, nPActive=0, nPZombie=0, pid;
bool isInt;
QFile file;
QListWidgetItem *title = new QListWidgetItem("PID\t" + QString::fromUtf8("name") + "\t\t\t" +
QString::fromUtf8("status") + "\t" +
QString::fromUtf8("memory"), ui->processList);
ui->processList->addItem(title);
while(1){
//get pid
int s = str.indexOf("\n", head);
int e = str.indexOf("\n", s+1);
head = e;
sPid = str.mid(s+1, e-s-1);
pid = sPid.toInt(&isInt, 10);
if(!isInt) break;
totalProcess++;
//open the file corressponding to that pid
file.setFileName("/proc/"+sPid+"/stat");
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, tr("warning"), tr("the file is not readable"), QMessageBox::Ok);
return;
}
text = file.readLine();
if(text.length()==0)
{
QMessageBox::warning(this, tr("warning"), tr("no stat"), QMessageBox::Ok);
break;
}
s = text.indexOf("(");
e = text.indexOf(")");
pName = text.mid(s+1, e-s-1);
pName.trimmed();
pState = text.section(" ", 2,2);
pMemory = text.section(" ",22,22);
switch(pState.at(0).toLatin1())
{
case 'S':
nPSleep++;
break;
case 'R':
nPActive++;
break;
case 'Z':
nPZombie++;
break;
default:
break;
}
if(pName.length()>=12)
{
QListWidgetItem *item =
new QListWidgetItem(sPid + "\t" + pName + "\t\t" +
pState + "\t" +
pMemory, ui->processList);
ui->processList->addItem(item);
}
ui->nPLabel->setText(QString::number(totalProcess,10));
ui->nPActiveLabel->setText(QString::number(nPActive, 10));
ui->nPSleepLabel->setText(QString::number(nPSleep,10));
ui->nPZombieLabel->setText(QString::number(nPZombie,10));
}
} else if(currentTab==2){
if(firstConnect==1){
ui->chatRoom->clear();
firstConnect = 0;
Server *server = new Server();
QObject::connect(server, SIGNAL(requestAddingItem(QString)), this, SLOT(addItemToChattingRoom(QString)));
server->startServer();
}
}
}
void MainWindow::onKillProcess()
{
QListWidgetItem *selectedProcess = ui->processList->currentItem();
QString process = selectedProcess->text();
QString pid = process.section("\t", 0,0);
system("kill "+pid.toLatin1());
QMessageBox::warning(this, tr("killed"), QString::fromUtf8("this process has been successfully killed"), QMessageBox::Ok);
showTab(1);
}
void MainWindow::onTabChange(int currentTab)
{
showTab(currentTab);
return;
}
void MainWindow::onRefreshProcess(){
showTab(1);
}
void MainWindow::addItemToChattingRoom(QString text)
{
//QString str = QTextCodec::codecForMib(1015)->toUnicode(text);
QListWidgetItem *chat = new QListWidgetItem(text, ui->chatRoom);
ui->chatRoom->addItem(chat);
}
MainWindow::~MainWindow()
{
delete ui;
}