mirror of
https://github.com/edisionnano/QDiskInfo.git
synced 2026-03-16 17:50:53 +03:00
Start moving stuff to utils.cpp
This commit is contained in:
@@ -26,6 +26,8 @@ set(PROJECT_SOURCES
|
||||
src/statusdot.cpp
|
||||
src/jsonparser.h
|
||||
src/jsonparser.cpp
|
||||
src/utils.h
|
||||
src/utils.cpp
|
||||
src/resources.qrc
|
||||
)
|
||||
|
||||
|
||||
@@ -116,28 +116,13 @@ void MainWindow::updateNavigationButtons(int currentIndex)
|
||||
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()
|
||||
{
|
||||
QString output = getSmartctlOutput({"--scan", "--json"}, false);
|
||||
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
||||
QJsonObject jsonObj = doc.object();
|
||||
devices = jsonObj["devices"].toArray();
|
||||
QString smartctlPath = getSmartctlPath();
|
||||
QString smartctlPath = Utils.getSmartctlPath();
|
||||
QStringList commandList;
|
||||
|
||||
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->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
tableWidget->horizontalHeaderItem(1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
tableWidget->horizontalHeaderItem(2)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
||||
for (int i = 0; i < tableWidget->columnCount(); ++i) {
|
||||
QTableWidgetItem *headerItem = tableWidget->horizontalHeaderItem(i);
|
||||
if (headerItem) {
|
||||
if (i == 2) {
|
||||
headerItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
|
||||
} else {
|
||||
headerItem->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
|
||||
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;
|
||||
layout->addWidget(tableWidget);
|
||||
popup->setLayout(layout);
|
||||
|
||||
popup->setWindowTitle(tr("Self Test Log"));
|
||||
popup->resize(500, 400);
|
||||
popup->resize(400, 400);
|
||||
popup->show();
|
||||
};
|
||||
|
||||
@@ -1019,10 +1011,10 @@ QString MainWindow::getSmartctlOutput(const QStringList &arguments, bool root)
|
||||
if (root) {
|
||||
command = "pkexec";
|
||||
} else {
|
||||
command = getSmartctlPath();
|
||||
command = Utils.getSmartctlPath();
|
||||
}
|
||||
|
||||
if (!getSmartctlPath().isEmpty()) {
|
||||
if (!Utils.getSmartctlPath().isEmpty()) {
|
||||
process.start(command, arguments);
|
||||
process.waitForFinished(-1);
|
||||
}
|
||||
@@ -1177,7 +1169,7 @@ void MainWindow::on_actionCyclic_Navigation_toggled(bool cyclicNavigation)
|
||||
QString MainWindow::initiateSelfTest(const QString &testType, const QString &deviceNode)
|
||||
{
|
||||
QProcess process;
|
||||
QString command = getSmartctlPath();
|
||||
QString command = Utils.getSmartctlPath();
|
||||
QStringList arguments;
|
||||
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)
|
||||
{
|
||||
QProcess process;
|
||||
QString command = getSmartctlPath();
|
||||
QString command = Utils.getSmartctlPath();
|
||||
QStringList arguments;
|
||||
arguments << command << "-X" << deviceNode;
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "statusdot.h"
|
||||
#include "custombutton.h"
|
||||
#include "jsonparser.h"
|
||||
#include "utils.h"
|
||||
#include "./ui_mainwindow.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
@@ -63,6 +64,7 @@ private slots:
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QLocale locale;
|
||||
utils Utils;
|
||||
QButtonGroup *buttonGroup;
|
||||
QHBoxLayout *horizontalLayout;
|
||||
QLabel *diskName, *temperatureValue, *healthStatusValue;
|
||||
|
||||
18
src/utils.cpp
Normal file
18
src/utils.cpp
Normal 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
17
src/utils.h
Normal 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
|
||||
Reference in New Issue
Block a user