From 5b8a9633480bb8fab9789bb611889d538e3354c2 Mon Sep 17 00:00:00 2001
From: Romain DERRE <romain.derre@pc227-11.pedago.ensiie.fr>
Date: Mon, 25 Mar 2019 10:09:05 +0100
Subject: [PATCH] ajout de service.[h/cpp] fonction pour obtenir le user
 principal dans generalstatus et le user d'une task

---
 app/app.pro           |  6 ++++--
 app/element.cpp       |  2 +-
 app/element.h         |  2 +-
 app/elements.cpp      | 16 +++++++++++++++-
 app/elements.h        |  2 ++
 app/generalstatus.cpp |  6 ++++++
 app/generalstatus.h   |  2 ++
 app/service.cpp       | 23 +++++++++++++++++++++++
 app/service.h         | 17 +++++++++++++++++
 app/task.cpp          | 35 ++++++++++++++++++++++++++++++++++-
 app/task.h            |  6 +++++-
 11 files changed, 110 insertions(+), 7 deletions(-)
 create mode 100644 app/service.cpp
 create mode 100644 app/service.h

diff --git a/app/app.pro b/app/app.pro
index 2623b02..ada7bd9 100644
--- a/app/app.pro
+++ b/app/app.pro
@@ -31,7 +31,8 @@ SOURCES += \
     updatable.cpp \
     task.cpp \
     controller.cpp \
-    generalstatus.cpp
+    generalstatus.cpp \
+    service.cpp
 
 HEADERS += \
         mainwindow.h \
@@ -40,7 +41,8 @@ HEADERS += \
     updatable.h \
     task.h \
     controller.h \
-    generalstatus.h
+    generalstatus.h \
+    service.h
 
 FORMS += \
         mainwindow.ui
diff --git a/app/element.cpp b/app/element.cpp
index 30107d1..a84ea4a 100644
--- a/app/element.cpp
+++ b/app/element.cpp
@@ -1,6 +1,6 @@
 #include "element.h"
 
-Element::Element(QString & name) : name(name)
+Element::Element()
 {
 
 }
diff --git a/app/element.h b/app/element.h
index e89a88f..be0978d 100644
--- a/app/element.h
+++ b/app/element.h
@@ -9,7 +9,7 @@
 class Element : public Updatable
 {
 public:
-    Element(QString & name);
+    Element();
     QString get_name();
     QString get_status();
 protected:
diff --git a/app/elements.cpp b/app/elements.cpp
index 52a9e5d..23ce03f 100644
--- a/app/elements.cpp
+++ b/app/elements.cpp
@@ -1,6 +1,6 @@
 #include "elements.h"
 
-Elements::Elements()
+Elements::Elements() : proc("/proc/"), task_list(".listT")
 {
 
 }
@@ -13,6 +13,20 @@ QStandardItemModel & Elements::getModel()
 
 void Elements::update()
 {
+    system("ls /proc | grep '[0-9]' > .listT");
+    if(!task_list.open(QIODevice::ReadOnly)) {
+        QMessageBox::information(0, "error", task_list.errorString());
+    }
+    QTextStream in(&task_list);
 
+    while(!in.atEnd()) {
+        int pid = in.readLine().toInt();
+
+
+        //model->appendRow(line);
+    }
+
+
+    task_list.close();
 }
 
diff --git a/app/elements.h b/app/elements.h
index 48eac02..234b0c2 100644
--- a/app/elements.h
+++ b/app/elements.h
@@ -16,6 +16,8 @@ public:
 private:
     QStandardItemModel model;
     QStandardItem *parent0 = model.invisibleRootItem();
+    QFile proc;
+    QFile task_list;
 };
 
 #endif // ELEMENTS_H
diff --git a/app/generalstatus.cpp b/app/generalstatus.cpp
index 429056b..f1c29d4 100644
--- a/app/generalstatus.cpp
+++ b/app/generalstatus.cpp
@@ -14,6 +14,8 @@ GeneralStatus::GeneralStatus() : user_time(0), uptime("/proc/uptime"), meminfo("
 
     meminfo.close();
 
+    user = getenv("USER");
+
     GeneralStatus::update();
 }
 
@@ -95,3 +97,7 @@ int GeneralStatus::getNbTask()
 {
     return nbTask;
 }
+QString GeneralStatus::getUser()
+{
+    return user;
+}
diff --git a/app/generalstatus.h b/app/generalstatus.h
index cfa60a3..ed6303e 100644
--- a/app/generalstatus.h
+++ b/app/generalstatus.h
@@ -12,6 +12,7 @@ public:
     int getMemTotal();
     double getCPU();
     int getNbTask();
+    QString getUser();
     void update();
 private:
     int time;
@@ -20,6 +21,7 @@ private:
     double CPU;
     int nbTask;
     int user_time;
+    QString user;
     QFile uptime;
     QFile meminfo;
     QFile stat;
diff --git a/app/service.cpp b/app/service.cpp
new file mode 100644
index 0000000..d90acf1
--- /dev/null
+++ b/app/service.cpp
@@ -0,0 +1,23 @@
+#include "service.h"
+
+Service::Service()
+{
+
+}
+
+Service::update()
+{
+
+}
+
+
+
+int Service::get_nbTask()
+{
+    return nbTask;
+}
+
+QString Service::get_details()
+{
+    return details;
+}
diff --git a/app/service.h b/app/service.h
new file mode 100644
index 0000000..e7d989f
--- /dev/null
+++ b/app/service.h
@@ -0,0 +1,17 @@
+#ifndef SERVICE_H
+#define SERVICE_H
+
+#include "element.h"
+
+class Service : public Element
+{
+public:
+    Service();
+    int get_nbTask();
+    QString get_details();
+private:
+    int nbTask;
+    QString details;
+};
+
+#endif // SERVICE_H
diff --git a/app/task.cpp b/app/task.cpp
index 8480da3..a10be23 100644
--- a/app/task.cpp
+++ b/app/task.cpp
@@ -1,8 +1,32 @@
 #include "task.h"
 
 
-Task::Task(QString & name, int PID) : Element(name), PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/stat")
+Task::Task(int PID) : PID(PID), utime(0), stat("/proc/"+QString::number(PID)+"/stat")
 {
+    QFile cmd("/proc"+QString::number(PID)+"/comm");
+    if(!cmd.open(QIODevice::ReadOnly)) {
+        QMessageBox::information(0, "error", cmd.errorString());
+    }
+    QTextStream in(&cmd);
+
+    name = in.readLine();
+
+    cmd.close();
+
+    QString command("ls -la /proc/"+QString::number(PID)+" | grep ' \.' > .userT");
+    system(command.toStdString().c_str());
+
+    QFile task_user(".userT");
+    if(!task_user.open(QIODevice::ReadOnly)) {
+        QMessageBox::information(0, "error", task_user.errorString());
+    }
+    QTextStream in2(&task_user);
+    QStringList fields = in2.readLine().split(" ");
+
+    user = fields.at(fields.length()-8);
+
+    task_user.close();
+
     Task::update();
 }
 
@@ -22,6 +46,7 @@ void Task::update()
     int new_utime = fields.at(13).toInt();
     CPU = static_cast<double>(new_utime-utime);
     utime = new_utime;
+    PPID = fields.at(3).toInt();
 
     stat.close();
 }
@@ -47,3 +72,11 @@ double Task::getMem()
 {
     return mem;
 }
+int Task::getPPID()
+{
+    return PPID;
+}
+QString Task::getUser()
+{
+    return user;
+}
diff --git a/app/task.h b/app/task.h
index 250435c..6687f2e 100644
--- a/app/task.h
+++ b/app/task.h
@@ -10,19 +10,23 @@
 class Task : Element
 {
 public:
-    Task(QString & name, int PID);
+    Task(int PID);
     void update();
     void sendSignal(int sig);
     int getPID();
     double getCPU();
     int getUtime();
     double getMem();
+    int getPPID();
+    QString getUser();
 private:
     int PID;
     double CPU;
     int utime;
     double mem;
+    int PPID;
     QFile stat;
+    QString user;
 };
 
 #endif // TASK_H
-- 
GitLab