mirror of
https://github.com/edisionnano/QDiskInfo.git
synced 2026-04-16 00:11:28 +03:00
Move some more functions to utils
This commit is contained in:
@@ -84,7 +84,12 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
scanDevices();
|
QPair<QStringList, QJsonArray> values = Utils.scanDevices(initializing);
|
||||||
|
deviceOutputs = values.first;
|
||||||
|
devices = values.second;
|
||||||
|
if (!deviceOutputs.isEmpty()) {
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
this->setFocus();
|
this->setFocus();
|
||||||
initializing = false;
|
initializing = false;
|
||||||
}
|
}
|
||||||
@@ -116,51 +121,6 @@ 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = Utils.getSmartctlPath();
|
|
||||||
QStringList commandList;
|
|
||||||
|
|
||||||
for (const QJsonValue &value : std::as_const(devices)) {
|
|
||||||
QJsonObject device = value.toObject();
|
|
||||||
QString deviceName = device["name"].toString();
|
|
||||||
commandList.append(QString(smartctlPath + " --all --json=o %1").arg(deviceName));
|
|
||||||
}
|
|
||||||
QString command = commandList.join(" ; ");
|
|
||||||
|
|
||||||
if (smartctlPath.isEmpty()) {
|
|
||||||
QMessageBox::critical(this, tr("KDiskInfo Error"), tr("smartctl was not found, please install it!"));
|
|
||||||
QTimer::singleShot(0, qApp, &QApplication::quit);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString allDevicesOutput = getSmartctlOutput({"sh", "-c", command}, true);
|
|
||||||
|
|
||||||
int startIndex = 0;
|
|
||||||
int endIndex = 0;
|
|
||||||
|
|
||||||
static const QRegularExpression regex("\\}\\n\\{");
|
|
||||||
|
|
||||||
while ((endIndex = allDevicesOutput.indexOf(regex, startIndex)) != -1) {
|
|
||||||
++endIndex;
|
|
||||||
QString jsonFragment = allDevicesOutput.mid(startIndex, endIndex - startIndex);
|
|
||||||
deviceOutputs.append(jsonFragment);
|
|
||||||
startIndex = endIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (startIndex < allDevicesOutput.size()) {
|
|
||||||
QString jsonFragment = allDevicesOutput.mid(startIndex);
|
|
||||||
deviceOutputs.append(jsonFragment);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!allDevicesOutput.isEmpty()) {
|
|
||||||
updateUI();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::updateUI()
|
void MainWindow::updateUI()
|
||||||
{
|
{
|
||||||
bool firstTime = true;
|
bool firstTime = true;
|
||||||
@@ -1003,41 +963,6 @@ void MainWindow::addSmartAttributesTable(const QJsonArray &attributes)
|
|||||||
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MainWindow::getSmartctlOutput(const QStringList &arguments, bool root)
|
|
||||||
{
|
|
||||||
QProcess process;
|
|
||||||
QString command;
|
|
||||||
|
|
||||||
if (root) {
|
|
||||||
command = "pkexec";
|
|
||||||
} else {
|
|
||||||
command = Utils.getSmartctlPath();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Utils.getSmartctlPath().isEmpty()) {
|
|
||||||
process.start(command, arguments);
|
|
||||||
process.waitForFinished(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.exitCode() == 127) {
|
|
||||||
QMessageBox::critical(this, tr("KDiskInfo Error"), tr("KDiskInfo needs root access in order to read S.M.A.R.T. data!"));
|
|
||||||
if (initializing) {
|
|
||||||
QTimer::singleShot(0, qApp, &QApplication::quit);
|
|
||||||
}
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (root && !initializing) {
|
|
||||||
clearButtonGroup();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.isOpen()) {
|
|
||||||
return process.readAllStandardOutput();
|
|
||||||
} else {
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QString MainWindow::toTitleCase(const QString& sentence) {
|
QString MainWindow::toTitleCase(const QString& sentence) {
|
||||||
QString result;
|
QString result;
|
||||||
bool capitalizeNext = true;
|
bool capitalizeNext = true;
|
||||||
@@ -1061,18 +986,6 @@ QString MainWindow::toTitleCase(const QString& sentence) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::clearButtonGroup()
|
|
||||||
{
|
|
||||||
QList<QAbstractButton*> buttons = buttonGroup->buttons();
|
|
||||||
for (QAbstractButton* button : buttons) {
|
|
||||||
buttonGroup->removeButton(button);
|
|
||||||
delete button;
|
|
||||||
}
|
|
||||||
horizontalLayout->removeItem(buttonStretch);
|
|
||||||
delete buttonStretch;
|
|
||||||
menuDisk->clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::on_actionQuit_triggered()
|
void MainWindow::on_actionQuit_triggered()
|
||||||
{
|
{
|
||||||
qApp->quit();
|
qApp->quit();
|
||||||
@@ -1116,7 +1029,13 @@ void MainWindow::on_actionGitHub_triggered()
|
|||||||
|
|
||||||
void MainWindow::on_actionRescan_Refresh_triggered()
|
void MainWindow::on_actionRescan_Refresh_triggered()
|
||||||
{
|
{
|
||||||
scanDevices();
|
QPair<QStringList, QJsonArray> values = Utils.scanDevices(initializing);
|
||||||
|
deviceOutputs = values.first;
|
||||||
|
devices = values.second;
|
||||||
|
if (!deviceOutputs.isEmpty()) {
|
||||||
|
Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk);
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1134,7 +1053,7 @@ void MainWindow::on_actionIgnore_C4_Reallocation_Event_Count_toggled(bool enable
|
|||||||
{
|
{
|
||||||
settings.setValue("IgnoreC4", ui->actionIgnore_C4_Reallocation_Event_Count->isChecked());
|
settings.setValue("IgnoreC4", ui->actionIgnore_C4_Reallocation_Event_Count->isChecked());
|
||||||
if (!initializing) {
|
if (!initializing) {
|
||||||
clearButtonGroup();
|
Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk);
|
||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1144,7 +1063,7 @@ void MainWindow::on_actionHEX_toggled(bool enabled)
|
|||||||
{
|
{
|
||||||
settings.setValue("HEX", ui->actionHEX->isChecked());
|
settings.setValue("HEX", ui->actionHEX->isChecked());
|
||||||
if (!initializing) {
|
if (!initializing) {
|
||||||
clearButtonGroup();
|
Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk);
|
||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1154,7 +1073,7 @@ void MainWindow::on_actionUse_Fahrenheit_toggled(bool enabled)
|
|||||||
{
|
{
|
||||||
settings.setValue("Fahrenheit", ui->actionUse_Fahrenheit->isChecked());
|
settings.setValue("Fahrenheit", ui->actionUse_Fahrenheit->isChecked());
|
||||||
if (!initializing) {
|
if (!initializing) {
|
||||||
clearButtonGroup();
|
Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk);
|
||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1169,7 +1088,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 = Utils.getSmartctlPath();
|
QString command = Utils.getSmartctlPath(initializing);
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
arguments << command << "--json=o" << "-t" << testType << deviceNode;
|
arguments << command << "--json=o" << "-t" << testType << deviceNode;
|
||||||
|
|
||||||
@@ -1186,7 +1105,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 = Utils.getSmartctlPath();
|
QString command = Utils.getSmartctlPath(initializing);
|
||||||
QStringList arguments;
|
QStringList arguments;
|
||||||
arguments << command << "-X" << deviceNode;
|
arguments << command << "-X" << deviceNode;
|
||||||
|
|
||||||
@@ -1215,7 +1134,7 @@ void MainWindow::on_actionUse_GB_instead_of_TB_toggled(bool gigabytes)
|
|||||||
{
|
{
|
||||||
settings.setValue("UseGB", ui->actionUse_GB_instead_of_TB->isChecked());
|
settings.setValue("UseGB", ui->actionUse_GB_instead_of_TB->isChecked());
|
||||||
if (!initializing) {
|
if (!initializing) {
|
||||||
clearButtonGroup();
|
Utils.clearButtonGroup(buttonGroup, horizontalLayout, buttonStretch, menuDisk);
|
||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,7 +94,6 @@ private:
|
|||||||
void onNextButtonClicked();
|
void onNextButtonClicked();
|
||||||
void onPrevButtonClicked();
|
void onPrevButtonClicked();
|
||||||
void updateNavigationButtons(int currentIndex);
|
void updateNavigationButtons(int currentIndex);
|
||||||
void scanDevices();
|
|
||||||
void updateUI();
|
void updateUI();
|
||||||
void selfTestHandler(const QString &mode, const QString &name, const QString &minutes);
|
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 populateWindow(const QJsonObject &tempObj, const QString &health, const QVector<QPair<QString, int>>& nvmeLogOrdered = QVector<QPair<QString, int>>());
|
||||||
@@ -102,7 +101,6 @@ private:
|
|||||||
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);
|
||||||
void clearButtonGroup();
|
|
||||||
QString initiateSelfTest(const QString &testType, const QString &deviceNode);
|
QString initiateSelfTest(const QString &testType, const QString &deviceNode);
|
||||||
void cancelSelfTest(const QString &deviceNode);
|
void cancelSelfTest(const QString &deviceNode);
|
||||||
void mousePressEvent(QMouseEvent*);
|
void mousePressEvent(QMouseEvent*);
|
||||||
|
|||||||
@@ -2,7 +2,19 @@
|
|||||||
|
|
||||||
utils::utils() {}
|
utils::utils() {}
|
||||||
|
|
||||||
QString utils::getSmartctlPath() {
|
void utils::clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalLayout, QSpacerItem* buttonStretch, QMenu* menuDisk)
|
||||||
|
{
|
||||||
|
QList<QAbstractButton*> buttons = buttonGroup->buttons();
|
||||||
|
for (QAbstractButton* button : buttons) {
|
||||||
|
buttonGroup->removeButton(button);
|
||||||
|
delete button;
|
||||||
|
}
|
||||||
|
horizontalLayout->removeItem(buttonStretch);
|
||||||
|
delete buttonStretch;
|
||||||
|
menuDisk->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString utils::getSmartctlPath(bool initializing) {
|
||||||
QStringList paths = QString::fromLocal8Bit(qgetenv("PATH")).split(QDir::listSeparator(), Qt::SkipEmptyParts);
|
QStringList paths = QString::fromLocal8Bit(qgetenv("PATH")).split(QDir::listSeparator(), Qt::SkipEmptyParts);
|
||||||
|
|
||||||
paths << "/usr/sbin" << "/usr/local/sbin";
|
paths << "/usr/sbin" << "/usr/local/sbin";
|
||||||
@@ -16,3 +28,83 @@ QString utils::getSmartctlPath() {
|
|||||||
|
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString utils::getSmartctlOutput(const QStringList &arguments, bool root, bool initializing)
|
||||||
|
{
|
||||||
|
QProcess process;
|
||||||
|
QString command;
|
||||||
|
|
||||||
|
if (root) {
|
||||||
|
command = "pkexec";
|
||||||
|
} else {
|
||||||
|
command = getSmartctlPath(initializing);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!getSmartctlPath(initializing).isEmpty()) {
|
||||||
|
process.start(command, arguments);
|
||||||
|
process.waitForFinished(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.exitCode() == 127) {
|
||||||
|
QMessageBox::critical(nullptr, QObject::tr("KDiskInfo Error"), QObject::tr("KDiskInfo needs root access in order to read S.M.A.R.T. data!"));
|
||||||
|
if (initializing) {
|
||||||
|
QTimer::singleShot(0, qApp, &QApplication::quit);
|
||||||
|
}
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.isOpen()) {
|
||||||
|
return process.readAllStandardOutput();
|
||||||
|
} else {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QPair<QStringList, QJsonArray> utils::scanDevices(bool initializing)
|
||||||
|
{
|
||||||
|
QString output = getSmartctlOutput({"--scan", "--json"}, false, initializing);
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(output.toUtf8());
|
||||||
|
QJsonObject jsonObj = doc.object();
|
||||||
|
QJsonArray devices = jsonObj["devices"].toArray();
|
||||||
|
QString smartctlPath = getSmartctlPath(initializing);
|
||||||
|
QStringList commandList;
|
||||||
|
QStringList deviceOutputs;
|
||||||
|
|
||||||
|
for (const QJsonValue &value : std::as_const(devices)) {
|
||||||
|
QJsonObject device = value.toObject();
|
||||||
|
QString deviceName = device["name"].toString();
|
||||||
|
commandList.append(QString(smartctlPath + " --all --json=o %1").arg(deviceName));
|
||||||
|
}
|
||||||
|
QString command = commandList.join(" ; ");
|
||||||
|
|
||||||
|
if (smartctlPath.isEmpty()) {
|
||||||
|
QMessageBox::critical(nullptr, QObject::tr("KDiskInfo Error"), QObject::tr("smartctl was not found, please install it!"));
|
||||||
|
QTimer::singleShot(0, qApp, &QApplication::quit);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString allDevicesOutput = getSmartctlOutput({"sh", "-c", command}, true, initializing);
|
||||||
|
|
||||||
|
int startIndex = 0;
|
||||||
|
int endIndex = 0;
|
||||||
|
|
||||||
|
static const QRegularExpression regex("\\}\\n\\{");
|
||||||
|
|
||||||
|
while ((endIndex = allDevicesOutput.indexOf(regex, startIndex)) != -1) {
|
||||||
|
++endIndex;
|
||||||
|
QString jsonFragment = allDevicesOutput.mid(startIndex, endIndex - startIndex);
|
||||||
|
deviceOutputs.append(jsonFragment);
|
||||||
|
startIndex = endIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startIndex < allDevicesOutput.size()) {
|
||||||
|
QString jsonFragment = allDevicesOutput.mid(startIndex);
|
||||||
|
deviceOutputs.append(jsonFragment);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!allDevicesOutput.isEmpty()) {
|
||||||
|
return QPair<QStringList, QJsonArray>(deviceOutputs, devices);
|
||||||
|
} else {
|
||||||
|
return QPair<QStringList, QJsonArray>(QStringList(), QJsonArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
28
src/utils.h
28
src/utils.h
@@ -1,17 +1,37 @@
|
|||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
#include <QString>
|
#include <QMainWindow>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QDir>
|
#include <QWidget>
|
||||||
#include <QFile>
|
#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 <QHBoxLayout>
|
||||||
|
#include <QSpacerItem>
|
||||||
|
#include <QAbstractButton>
|
||||||
|
#include <QMenu>
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
class utils
|
class utils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
utils();
|
utils();
|
||||||
|
|
||||||
QString getSmartctlPath();
|
void clearButtonGroup(QButtonGroup* buttonGroup, QHBoxLayout* horizontalLayout, QSpacerItem* buttonStretch, QMenu* menuDisk);
|
||||||
|
QString getSmartctlPath(bool initializing);
|
||||||
|
QString getSmartctlOutput(const QStringList &arguments, bool root, bool initializing);
|
||||||
|
QPair<QStringList, QJsonArray> scanDevices(bool initializing);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // UTILS_H
|
#endif // UTILS_H
|
||||||
|
|||||||
Reference in New Issue
Block a user