Move selftesthandler to utils

This commit is contained in:
spiros
2024-06-19 02:17:46 +03:00
parent a7c7dda87f
commit f4e18061b0
6 changed files with 147 additions and 173 deletions

View File

@@ -285,59 +285,6 @@ void MainWindow::updateUI()
updateNavigationButtons(buttonGroup->buttons().indexOf(buttonGroup->checkedButton()));
}
void MainWindow::selfTestHandler(const QString &mode, const QString &name, const QString &minutes) {
QString output = Utils.initiateSelfTest(mode, name);
if (output.isEmpty()) {
QMessageBox::critical(this, tr("KDiskInfo Error"), tr("KDiskInfo needs root access in order to request a self-test!"));
} else {
QJsonDocument testDoc = QJsonDocument::fromJson(output.toUtf8());
QJsonObject testObj = testDoc.object();
QJsonObject smartctlObj = testObj.value("smartctl").toObject();
QJsonObject deviceObj = testObj.value("device").toObject();
QString name = deviceObj.value("name").toString();
int exitStatus = smartctlObj.value("exit_status").toInt();
QJsonArray outputArray = smartctlObj["output"].toArray();
static const QRegularExpression regex("\\((\\d+%)\\s*(\\w+)\\)");
QString percentage;
for (const QJsonValue &value : outputArray) {
QString line = value.toString();
QRegularExpressionMatch match = regex.match(line);
if (match.hasMatch()) {
percentage = match.captured(0);
break;
}
}
percentage.replace("remaining", tr("remaining"));
percentage.replace("completed", tr("completed"));
if (exitStatus == 4) {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Test Already Running"));
msgBox.setText(tr("A self-test is already being performed") + " " + percentage + "\n" + tr("You can press the Ok button in order to abort the test that is currently running"));
msgBox.setIcon(QMessageBox::Warning);
msgBox.addButton(QMessageBox::Cancel);
QPushButton *abortButton = msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
if (msgBox.clickedButton() == abortButton) {
Utils.cancelSelfTest(name);
}
} else if (exitStatus == 0) {
QString infoMessage = tr("A self-test has been requested successfully");
if (minutes != "0") {
infoMessage = infoMessage + "\n" + tr("It will be completed after") + " " + minutes + " " + tr("minutes");
}
QMessageBox::information(this, tr("Test Requested"), infoMessage);
} else {
QMessageBox::critical(this, tr("KDiskInfo Error"), tr("Error: Something went wrong"));
}
}
}
void MainWindow::populateWindow(const QJsonObject &localObj, const QString &health, const QVector<QPair<QString, int>>& nvmeLogOrdered)
{
QJsonArray attributes = localObj["ata_smart_attributes"].toObject()["table"].toArray();
@@ -717,7 +664,7 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal
}
connect(action, &QAction::triggered, this, [this, mode, name, minutes]() {
selfTestHandler(mode, name, minutes);
Utils.selfTestHandler(mode, name, minutes);
});
i++;
@@ -746,13 +693,13 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal
QAction *actionShort = new QAction(tr("Short"), this);
selfTestMenu->addAction(actionShort);
connect(actionShort, &QAction::triggered, this, [this, mode = "short", name, minutes = "0"]() {
selfTestHandler(mode, name, minutes);
Utils.selfTestHandler(mode, name, minutes);
});
QAction *actionLong = new QAction(tr("Extended"), this);
selfTestMenu->addAction(actionLong);
connect(actionLong , &QAction::triggered, this, [this, mode = "long", name, minutes = "0"]() {
selfTestHandler(mode, name, minutes);
Utils.selfTestHandler(mode, name, minutes);
});
if (nvmeSelfTestsTable.isEmpty()) {

View File

@@ -2,21 +2,6 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProcess>
#include <QWidget>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QButtonGroup>
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <QTimer>
#include <QSettings>
#include <QMouseEvent>
#include <QActionGroup>
#include <cmath>
#include "statusdot.h"
@@ -95,7 +80,6 @@ private:
void onPrevButtonClicked();
void updateNavigationButtons(int currentIndex);
void updateUI();
void selfTestHandler(const QString &mode, const QString &name, const QString &minutes);
void populateWindow(const QJsonObject &tempObj, const QString &health, const QVector<QPair<QString, int>>& nvmeLogOrdered = QVector<QPair<QString, int>>());
void addNvmeLogTable(const QVector<QPair<QString, int>>& nvmeLogOrdered);
void addSmartAttributesTable(const QJsonArray &attributes);

View File

@@ -145,6 +145,59 @@ void utils::cancelSelfTest(const QString &deviceNode)
}
}
void utils::selfTestHandler(const QString &mode, const QString &name, const QString &minutes) {
QString output = initiateSelfTest(mode, name);
if (output.isEmpty()) {
QMessageBox::critical(nullptr, QObject::tr("KDiskInfo Error"), QObject::tr("KDiskInfo needs root access in order to request a self-test!"));
} else {
QJsonDocument testDoc = QJsonDocument::fromJson(output.toUtf8());
QJsonObject testObj = testDoc.object();
QJsonObject smartctlObj = testObj.value("smartctl").toObject();
QJsonObject deviceObj = testObj.value("device").toObject();
QString name = deviceObj.value("name").toString();
int exitStatus = smartctlObj.value("exit_status").toInt();
QJsonArray outputArray = smartctlObj["output"].toArray();
static const QRegularExpression regex("\\((\\d+%)\\s*(\\w+)\\)");
QString percentage;
for (const QJsonValue &value : outputArray) {
QString line = value.toString();
QRegularExpressionMatch match = regex.match(line);
if (match.hasMatch()) {
percentage = match.captured(0);
break;
}
}
percentage.replace("remaining", QObject::tr("remaining"));
percentage.replace("completed", QObject::tr("completed"));
if (exitStatus == 4) {
QMessageBox msgBox;
msgBox.setWindowTitle(QObject::tr("Test Already Running"));
msgBox.setText(QObject::tr("A self-test is already being performed") + " " + percentage + "\n" + QObject::tr("You can press the Ok button in order to abort the test that is currently running"));
msgBox.setIcon(QMessageBox::Warning);
msgBox.addButton(QMessageBox::Cancel);
QPushButton *abortButton = msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
if (msgBox.clickedButton() == abortButton) {
cancelSelfTest(name);
}
} else if (exitStatus == 0) {
QString infoMessage = QObject::tr("A self-test has been requested successfully");
if (minutes != "0") {
infoMessage = infoMessage + "\n" + QObject::tr("It will be completed after") + " " + minutes + " " + QObject::tr("minutes");
}
QMessageBox::information(nullptr, QObject::tr("Test Requested"), infoMessage);
} else {
QMessageBox::critical(nullptr, QObject::tr("KDiskInfo Error"), QObject::tr("Error: Something went wrong"));
}
}
}
QString utils::toTitleCase(const QString& sentence) {
QString result;
bool capitalizeNext = true;

View File

@@ -19,6 +19,7 @@
#include <QActionGroup>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QPushButton>
#include <QAbstractButton>
#include <QMenu>
#include <QApplication>
@@ -34,6 +35,7 @@ public:
QPair<QStringList, QJsonArray> scanDevices(bool initializing);
QString initiateSelfTest(const QString &testType, const QString &deviceNode);
void cancelSelfTest(const QString &deviceNode);
void selfTestHandler(const QString &mode, const QString &name, const QString &minutes);
QString toTitleCase(const QString& sentence);
};