#include "mainwindow.h" #include "../common/diality_secure_code.h" #include "../common/diality_keys.h" #include "../common/diality_payload.h" #include #include #include #include #include #include static QString formatFields(const diality::ParsedFields& f) { QString out; out += "ID:" + f.partId + "\n"; out += "Lt:" + f.lot + "\n"; out += "Ex:" + f.exp + "\n"; out += "SN:" + f.serial + "\n"; return out; } MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { diality::cryptoInit(); auto* central = new QWidget(this); auto* root = new QVBoxLayout(central); auto* keysRow = new QHBoxLayout(); m_keysStatus = new QLabel("Keys: not loaded", central); m_reloadKeysBtn = new QPushButton("Reload keys", central); keysRow->addWidget(m_keysStatus, 1); keysRow->addWidget(m_reloadKeysBtn, 0); m_scanEdit = new QLineEdit(central); m_scanEdit->setPlaceholderText("Scan Data Matrix here (focus stays here)"); m_output = new QTextEdit(central); m_output->setReadOnly(true); root->addLayout(keysRow); root->addWidget(m_scanEdit); root->addWidget(m_output); setCentralWidget(central); connect(m_reloadKeysBtn , &QPushButton ::clicked , this, &MainWindow::onReloadKeysClicked); connect(m_scanEdit , &QLineEdit ::returnPressed , this, &MainWindow::onScanEntered ); onReloadKeysClicked(); m_scanEdit->setFocus(); } void MainWindow::onReloadKeysClicked() { QString err; QString path; diality::Keys k; if (!diality::loadDeviceKeys(&k, &path, &err)) { m_keysLoaded = false; m_keys = diality::Keys{}; m_keysPath.clear(); m_keysStatus->setText("Keys: NOT LOADED (" + err + "). Expected /etc/diality/keys.json"); return; } m_keysLoaded = true; m_keys = k; m_keysPath = path; m_keysStatus->setText("Keys: loaded from " + m_keysPath); } bool MainWindow::ensureKeysLoaded() { if (m_keysLoaded) return true; m_output->setPlainText("ERROR: keys not loaded. Place /etc/diality/keys.json and press Reload."); return false; } void MainWindow::onScanEntered() { if (!ensureKeysLoaded()) return; const QString scanned = m_scanEdit->text().trimmed(); m_scanEdit->clear(); if (scanned.isEmpty()) return; const auto res = diality::openSecureDatamatrixText(scanned, m_keys.encKey32, m_keys.signPk32); if (!res.ok) { m_output->setPlainText("ERROR: " + res.error); return; } diality::ParsedFields fields; QString perr; if (!diality::parseTlvPayload(res.plaintext, &fields, &perr)) { m_output->setPlainText("ERROR: Payload parse failed: " + perr); return; } m_output->setPlainText(formatFields(fields)); }