Save to file

This commit is contained in:
Spiros
2024-06-05 22:42:10 +03:00
parent 3a7786ee12
commit 697823a3f2
2 changed files with 55 additions and 10 deletions

View File

@@ -136,22 +136,22 @@ void MainWindow::scanDevices()
} }
QJsonDocument localDoc = QJsonDocument::fromJson(allOutput.toUtf8()); QJsonDocument localDoc = QJsonDocument::fromJson(allOutput.toUtf8());
QJsonObject localObj = localDoc.object(); deviceJson = localDoc.object();
QString modelName = localObj["model_name"].toString(); QString modelName = deviceJson["model_name"].toString();
QJsonArray attributes = localObj["ata_smart_attributes"].toObject()["table"].toArray(); QJsonArray attributes = deviceJson["ata_smart_attributes"].toObject()["table"].toArray();
QJsonObject nvmeLog = localObj["nvme_smart_health_information_log"].toObject(); QJsonObject nvmeLog = deviceJson["nvme_smart_health_information_log"].toObject();
QString temperature = "-- °C"; QString temperature = "-- °C";
bool healthPassed = localObj["smart_status"].toObject()["passed"].toBool(); bool healthPassed = deviceJson["smart_status"].toObject()["passed"].toBool();
bool caution = false; bool caution = false;
bool bad = false; bool bad = false;
QString health; QString health;
QColor healthColor; QColor healthColor;
QString protocol = localObj["device"].toObject()["protocol"].toString(); QString protocol = deviceJson["device"].toObject()["protocol"].toString();
bool isNvme = (protocol == "NVMe"); bool isNvme = (protocol == "NVMe");
int temperatureInt = localObj["temperature"].toObject()["current"].toInt(); int temperatureInt = deviceJson["temperature"].toObject()["current"].toInt();
if (temperatureInt > 0) { if (temperatureInt > 0) {
temperature = QString::number(temperatureInt) + " °C"; temperature = QString::number(temperatureInt) + " °C";
} }
@@ -219,15 +219,15 @@ void MainWindow::scanDevices()
connect(button, &QPushButton::clicked, this, [=]() { connect(button, &QPushButton::clicked, this, [=]() {
if (isNvme) { if (isNvme) {
populateWindow(localObj, health, nvmeSmartOrdered); populateWindow(deviceJson, health, nvmeSmartOrdered);
} else { } else {
populateWindow(localObj, health); populateWindow(deviceJson, health);
} }
updateNavigationButtons(buttonGroup->buttons().indexOf(button)); updateNavigationButtons(buttonGroup->buttons().indexOf(button));
}); });
if (firstTime) { if (firstTime) {
globalObj = localObj; globalObj = deviceJson;
globalHealth = health; globalHealth = health;
button->setChecked(true); button->setChecked(true);
firstTime = false; firstTime = false;
@@ -673,3 +673,38 @@ void MainWindow::on_actionExit_triggered()
qApp->quit(); qApp->quit();
} }
void MainWindow::on_actionSave_JSON_triggered()
{
if (deviceJson.isEmpty()) {
QMessageBox::information(this, tr("Empty JSON"),
tr("The JSON is empty"));
return;
}
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save JSON"), "",
tr("JSON (*.json);;All Files (*)"));
if (fileName.isEmpty())
return;
else {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("Unable to open file for writing"),
file.errorString());
return;
}
QJsonDocument doc(deviceJson);
QByteArray jsonData = doc.toJson();
file.write(jsonData);
file.close();
}
}
void MainWindow::on_actionGitHub_triggered()
{
QDesktopServices::openUrl(QUrl("https://github.com/edisionnano/KDiskInfo"));
}

View File

@@ -9,6 +9,10 @@
#include <QJsonArray> #include <QJsonArray>
#include <QButtonGroup> #include <QButtonGroup>
#include <QAction> #include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>
#include <cmath> #include <cmath>
#include "statusdot.h" #include "statusdot.h"
@@ -33,6 +37,10 @@ public:
private slots: private slots:
void on_actionExit_triggered(); void on_actionExit_triggered();
void on_actionSave_JSON_triggered();
void on_actionGitHub_triggered();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QButtonGroup *buttonGroup; QButtonGroup *buttonGroup;
@@ -44,6 +52,8 @@ private:
QPushButton *prevButton, *nextButton; QPushButton *prevButton, *nextButton;
QColor goodColor, cautionColor, badColor, naColor; QColor goodColor, cautionColor, badColor, naColor;
QJsonObject deviceJson;
void onNextButtonClicked(); void onNextButtonClicked();
void onPrevButtonClicked(); void onPrevButtonClicked();
void updateNavigationButtons(int currentIndex); void updateNavigationButtons(int currentIndex);