mirror of
https://github.com/edisionnano/QDiskInfo.git
synced 2026-04-20 08:33:19 +03:00
Order NVMe values
This commit is contained in:
@@ -40,6 +40,7 @@ else()
|
|||||||
${PROJECT_SOURCES}
|
${PROJECT_SOURCES}
|
||||||
custombutton.h custombutton.cpp
|
custombutton.h custombutton.cpp
|
||||||
statusdot.h statusdot.cpp
|
statusdot.h statusdot.cpp
|
||||||
|
jsonparser.h jsonparser.cpp
|
||||||
|
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
55
jsonparser.cpp
Normal file
55
jsonparser.cpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#include "jsonparser.h"
|
||||||
|
|
||||||
|
JsonParser::JsonParser()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QVector<QPair<QString, int>> JsonParser::parse(const QString &json)
|
||||||
|
{
|
||||||
|
QVector<QPair<QString, int>> data;
|
||||||
|
QStringList lines = json.split('\n');
|
||||||
|
bool found = false;
|
||||||
|
bool skip = false;
|
||||||
|
|
||||||
|
for (const QString &line : lines) {
|
||||||
|
QString trimmedLine = line.trimmed();
|
||||||
|
if (trimmedLine.contains("[")) {
|
||||||
|
skip = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (trimmedLine.contains("]")) {
|
||||||
|
skip = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (skip) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (trimmedLine.contains("nvme_smart_health_information_log")) {
|
||||||
|
found = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (found) {
|
||||||
|
if (trimmedLine.contains("}")) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int colon_pos = trimmedLine.indexOf(":");
|
||||||
|
if (colon_pos != -1) {
|
||||||
|
QString key = removeQuotes(trimmedLine.left(colon_pos));
|
||||||
|
QString valueString = trimmedLine.mid(colon_pos + 1).trimmed();
|
||||||
|
valueString.chop(1);
|
||||||
|
int value = valueString.toInt();
|
||||||
|
data.append(qMakePair(key, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString JsonParser::removeQuotes(const QString &s)
|
||||||
|
{
|
||||||
|
QString result = s.trimmed();
|
||||||
|
if (result.length() >= 2 && result.front() == '"' && result.back() == '"') {
|
||||||
|
return result.mid(1, result.size() - 2);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
20
jsonparser.h
Normal file
20
jsonparser.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef JSONPARSER_H
|
||||||
|
#define JSONPARSER_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QVector>
|
||||||
|
#include <QStringList>
|
||||||
|
#include <QPair>
|
||||||
|
|
||||||
|
class JsonParser
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
JsonParser();
|
||||||
|
|
||||||
|
QVector<QPair<QString, int>> parse(const QString &json);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString removeQuotes(const QString &s);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -57,7 +57,10 @@ void MainWindow::scanDevices()
|
|||||||
QJsonArray devices = jsonObj["devices"].toArray();
|
QJsonArray devices = jsonObj["devices"].toArray();
|
||||||
QJsonObject globalObj;
|
QJsonObject globalObj;
|
||||||
QString globalHealth;
|
QString globalHealth;
|
||||||
|
QVector<QPair<QString, int>> globalNvmeSmartOrdered;
|
||||||
bool firstTime = true;
|
bool firstTime = true;
|
||||||
|
bool globalIsNvme = false;
|
||||||
|
|
||||||
|
|
||||||
for (const QJsonValue &value : devices) {
|
for (const QJsonValue &value : devices) {
|
||||||
QJsonObject device = value.toObject();
|
QJsonObject device = value.toObject();
|
||||||
@@ -87,6 +90,7 @@ void MainWindow::scanDevices()
|
|||||||
temperature = QString::number(temperatureInt) + " °C";
|
temperature = QString::number(temperatureInt) + " °C";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVector<QPair<QString, int>> nvmeSmartOrdered;
|
||||||
if (!isNvme) {
|
if (!isNvme) {
|
||||||
for (const QJsonValue &attr : attributes) {
|
for (const QJsonValue &attr : attributes) {
|
||||||
QJsonObject attrObj = attr.toObject();
|
QJsonObject attrObj = attr.toObject();
|
||||||
@@ -95,6 +99,9 @@ void MainWindow::scanDevices()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
JsonParser parser;
|
||||||
|
nvmeSmartOrdered = parser.parse(allOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (healthPassed && !caution) {
|
if (healthPassed && !caution) {
|
||||||
@@ -117,7 +124,11 @@ void MainWindow::scanDevices()
|
|||||||
button->setAutoExclusive(true);
|
button->setAutoExclusive(true);
|
||||||
|
|
||||||
connect(button, &QPushButton::clicked, this, [=]() {
|
connect(button, &QPushButton::clicked, this, [=]() {
|
||||||
|
if (isNvme) {
|
||||||
|
populateWindow(localObj, health, nvmeSmartOrdered);
|
||||||
|
} else {
|
||||||
populateWindow(localObj, health);
|
populateWindow(localObj, health);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (firstTime) {
|
if (firstTime) {
|
||||||
@@ -125,13 +136,22 @@ void MainWindow::scanDevices()
|
|||||||
globalHealth = health;
|
globalHealth = health;
|
||||||
button->setChecked(true);
|
button->setChecked(true);
|
||||||
firstTime = false;
|
firstTime = false;
|
||||||
|
globalIsNvme = isNvme;
|
||||||
|
if (isNvme) {
|
||||||
|
globalNvmeSmartOrdered = nvmeSmartOrdered;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
horizontalLayout->addStretch();
|
horizontalLayout->addStretch();
|
||||||
|
|
||||||
|
if (globalIsNvme) {
|
||||||
|
populateWindow(globalObj, globalHealth, globalNvmeSmartOrdered);
|
||||||
|
} else {
|
||||||
populateWindow(globalObj, globalHealth);
|
populateWindow(globalObj, globalHealth);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::populateWindow(const QJsonObject &localObj, const QString &health)
|
void MainWindow::populateWindow(const QJsonObject &localObj, const QString &health, const QVector<QPair<QString, int>>& nvmeLogOrdered)
|
||||||
{
|
{
|
||||||
QJsonArray attributes = localObj["ata_smart_attributes"].toObject()["table"].toArray();
|
QJsonArray attributes = localObj["ata_smart_attributes"].toObject()["table"].toArray();
|
||||||
QJsonObject nvmeLog = localObj["nvme_smart_health_information_log"].toObject();
|
QJsonObject nvmeLog = localObj["nvme_smart_health_information_log"].toObject();
|
||||||
@@ -290,17 +310,17 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal
|
|||||||
if (protocol != "NVMe") {
|
if (protocol != "NVMe") {
|
||||||
addSmartAttributesTable(attributes);
|
addSmartAttributesTable(attributes);
|
||||||
} else {
|
} else {
|
||||||
addNvmeLogTable(nvmeLog);
|
addNvmeLogTable(nvmeLogOrdered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::addNvmeLogTable(const QJsonObject &nvmeLog)
|
void MainWindow::addNvmeLogTable(const QVector<QPair<QString, int>>& nvmeLogOrdered)
|
||||||
{
|
{
|
||||||
tableWidget->setColumnCount(4);
|
tableWidget->setColumnCount(4);
|
||||||
tableWidget->setHorizontalHeaderLabels({"", "ID", "Attribute Name", "Raw Values"});
|
tableWidget->setHorizontalHeaderLabels({"", "ID", "Attribute Name", "Raw Values"});
|
||||||
tableWidget->verticalHeader()->setVisible(false);
|
tableWidget->verticalHeader()->setVisible(false);
|
||||||
tableWidget->setItemDelegateForColumn(0, new StatusDot(tableWidget));
|
tableWidget->setItemDelegateForColumn(0, new StatusDot(tableWidget));
|
||||||
tableWidget->setRowCount(nvmeLog.size());
|
tableWidget->setRowCount(nvmeLogOrdered.size());
|
||||||
|
|
||||||
for (int i = 0; i < tableWidget->columnCount(); ++i) {
|
for (int i = 0; i < tableWidget->columnCount(); ++i) {
|
||||||
QTableWidgetItem *headerItem = tableWidget->horizontalHeaderItem(i);
|
QTableWidgetItem *headerItem = tableWidget->horizontalHeaderItem(i);
|
||||||
@@ -314,16 +334,16 @@ void MainWindow::addNvmeLogTable(const QJsonObject &nvmeLog)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int row = 0;
|
int row = 0;
|
||||||
for (auto smartItem = nvmeLog.constBegin(); smartItem != nvmeLog.constEnd(); ++smartItem) {
|
for (const QPair<QString, int> &pair : nvmeLogOrdered) {
|
||||||
QString id = QString("%1").arg(row, 2, 16, QChar('0')).toUpper();
|
QString id = QString("%1").arg(row + 1, 2, 16, QChar('0')).toUpper();
|
||||||
|
|
||||||
QString name = smartItem.key().replace("_", " ");
|
QString key = pair.first;
|
||||||
|
QString name = key.replace("_", " ");
|
||||||
name = toTitleCase(name);
|
name = toTitleCase(name);
|
||||||
|
|
||||||
QString raw = QString::number(smartItem.value().toInt());
|
QString raw = QString::number(pair.second);
|
||||||
raw = QString("%1").arg(raw.toUInt(nullptr), 14, 16, QChar('0')).toUpper();
|
raw = QString("%1").arg(raw.toUInt(nullptr), 14, 16, QChar('0')).toUpper();
|
||||||
|
|
||||||
|
|
||||||
QColor statusColor;
|
QColor statusColor;
|
||||||
statusColor = Qt::green; // For now leave it all green
|
statusColor = Qt::green; // For now leave it all green
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
#include "statusdot.h"
|
#include "statusdot.h"
|
||||||
#include "custombutton.h"
|
#include "custombutton.h"
|
||||||
|
#include "jsonparser.h"
|
||||||
#include "./ui_mainwindow.h"
|
#include "./ui_mainwindow.h"
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@@ -39,8 +40,8 @@ private:
|
|||||||
QTableWidget *tableWidget;
|
QTableWidget *tableWidget;
|
||||||
|
|
||||||
void scanDevices();
|
void scanDevices();
|
||||||
void populateWindow(const QJsonObject &tempObj, const QString &health);
|
void populateWindow(const QJsonObject &tempObj, const QString &health, const QVector<QPair<QString, int>>& nvmeLogOrdered = QVector<QPair<QString, int>>());
|
||||||
void addNvmeLogTable(const QJsonObject &nvmeLog);
|
void addNvmeLogTable(const QVector<QPair<QString, int>>& nvmeLogOrdered);
|
||||||
void addSmartAttributesTable(const QJsonArray &attributes);
|
void addSmartAttributesTable(const QJsonArray &attributes);
|
||||||
QString getSmartctlOutput(const QStringList &arguments, bool root);
|
QString getSmartctlOutput(const QStringList &arguments, bool root);
|
||||||
QString toTitleCase(const QString& sentence);
|
QString toTitleCase(const QString& sentence);
|
||||||
|
|||||||
Reference in New Issue
Block a user