Start moving stuff to utils.cpp

This commit is contained in:
spiros
2024-06-19 00:35:50 +03:00
parent f57e8ac8ec
commit d21ad393fd
5 changed files with 56 additions and 25 deletions

View File

@@ -26,6 +26,8 @@ set(PROJECT_SOURCES
src/statusdot.cpp src/statusdot.cpp
src/jsonparser.h src/jsonparser.h
src/jsonparser.cpp src/jsonparser.cpp
src/utils.h
src/utils.cpp
src/resources.qrc src/resources.qrc
) )

View File

@@ -116,28 +116,13 @@ void MainWindow::updateNavigationButtons(int currentIndex)
nextButton->setEnabled(currentIndex < buttonGroup->buttons().size() - 1||ui->actionCyclic_Navigation->isChecked()); nextButton->setEnabled(currentIndex < buttonGroup->buttons().size() - 1||ui->actionCyclic_Navigation->isChecked());
} }
QString getSmartctlPath() {
QStringList paths = QString::fromLocal8Bit(qgetenv("PATH")).split(QDir::listSeparator(), Qt::SkipEmptyParts);
paths << "/usr/sbin" << "/usr/local/sbin";
for (const QString &path : paths) {
QString absolutePath = QDir(path).absoluteFilePath("smartctl");
if (QFile::exists(absolutePath) && QFileInfo(absolutePath).isExecutable()) {
return absolutePath;
}
}
return QString();
}
void MainWindow::scanDevices() void MainWindow::scanDevices()
{ {
QString output = getSmartctlOutput({"--scan", "--json"}, false); QString output = getSmartctlOutput({"--scan", "--json"}, false);
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8()); QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
QJsonObject jsonObj = doc.object(); QJsonObject jsonObj = doc.object();
devices = jsonObj["devices"].toArray(); devices = jsonObj["devices"].toArray();
QString smartctlPath = getSmartctlPath(); QString smartctlPath = Utils.getSmartctlPath();
QStringList commandList; QStringList commandList;
for (const QJsonValue &value : std::as_const(devices)) { for (const QJsonValue &value : std::as_const(devices)) {
@@ -458,9 +443,17 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal
tableWidget->setItem(i, 2, item); tableWidget->setItem(i, 2, item);
} }
tableWidget->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); for (int i = 0; i < tableWidget->columnCount(); ++i) {
tableWidget->horizontalHeaderItem(1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); QTableWidgetItem *headerItem = tableWidget->horizontalHeaderItem(i);
tableWidget->horizontalHeaderItem(2)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); if (headerItem) {
if (i == 2) {
headerItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
} else {
headerItem->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
}
}
}
tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
for (int i = 0; i < tableWidget->columnCount(); ++i) { for (int i = 0; i < tableWidget->columnCount(); ++i) {
@@ -476,9 +469,8 @@ void MainWindow::populateWindow(const QJsonObject &localObj, const QString &heal
QVBoxLayout *layout = new QVBoxLayout; QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tableWidget); layout->addWidget(tableWidget);
popup->setLayout(layout); popup->setLayout(layout);
popup->setWindowTitle(tr("Self Test Log")); popup->setWindowTitle(tr("Self Test Log"));
popup->resize(500, 400); popup->resize(400, 400);
popup->show(); popup->show();
}; };
@@ -1019,10 +1011,10 @@ QString MainWindow::getSmartctlOutput(const QStringList &arguments, bool root)
if (root) { if (root) {
command = "pkexec"; command = "pkexec";
} else { } else {
command = getSmartctlPath(); command = Utils.getSmartctlPath();
} }
if (!getSmartctlPath().isEmpty()) { if (!Utils.getSmartctlPath().isEmpty()) {
process.start(command, arguments); process.start(command, arguments);
process.waitForFinished(-1); process.waitForFinished(-1);
} }
@@ -1177,7 +1169,7 @@ void MainWindow::on_actionCyclic_Navigation_toggled(bool cyclicNavigation)
QString MainWindow::initiateSelfTest(const QString &testType, const QString &deviceNode) QString MainWindow::initiateSelfTest(const QString &testType, const QString &deviceNode)
{ {
QProcess process; QProcess process;
QString command = getSmartctlPath(); QString command = Utils.getSmartctlPath();
QStringList arguments; QStringList arguments;
arguments << command << "--json=o" << "-t" << testType << deviceNode; arguments << command << "--json=o" << "-t" << testType << deviceNode;
@@ -1194,7 +1186,7 @@ QString MainWindow::initiateSelfTest(const QString &testType, const QString &dev
void MainWindow::cancelSelfTest(const QString &deviceNode) void MainWindow::cancelSelfTest(const QString &deviceNode)
{ {
QProcess process; QProcess process;
QString command = getSmartctlPath(); QString command = Utils.getSmartctlPath();
QStringList arguments; QStringList arguments;
arguments << command << "-X" << deviceNode; arguments << command << "-X" << deviceNode;

View File

@@ -22,6 +22,7 @@
#include "statusdot.h" #include "statusdot.h"
#include "custombutton.h" #include "custombutton.h"
#include "jsonparser.h" #include "jsonparser.h"
#include "utils.h"
#include "./ui_mainwindow.h" #include "./ui_mainwindow.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@@ -63,6 +64,7 @@ private slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QLocale locale; QLocale locale;
utils Utils;
QButtonGroup *buttonGroup; QButtonGroup *buttonGroup;
QHBoxLayout *horizontalLayout; QHBoxLayout *horizontalLayout;
QLabel *diskName, *temperatureValue, *healthStatusValue; QLabel *diskName, *temperatureValue, *healthStatusValue;

18
src/utils.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "utils.h"
utils::utils() {}
QString utils::getSmartctlPath() {
QStringList paths = QString::fromLocal8Bit(qgetenv("PATH")).split(QDir::listSeparator(), Qt::SkipEmptyParts);
paths << "/usr/sbin" << "/usr/local/sbin";
for (const QString &path : paths) {
QString absolutePath = QDir(path).absoluteFilePath("smartctl");
if (QFile::exists(absolutePath) && QFileInfo(absolutePath).isExecutable()) {
return absolutePath;
}
}
return QString();
}

17
src/utils.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef UTILS_H
#define UTILS_H
#include <QString>
#include <QProcess>
#include <QDir>
#include <QFile>
class utils
{
public:
utils();
QString getSmartctlPath();
};
#endif // UTILS_H