Files
qt5-legatus-legacy/Lts/Tools/LicenceEditor/mainwindow.cpp
T
2026-08-13 18:33:10 +02:00

425 lines
12 KiB
C++
Executable File

/*
* @RDE_HEADER@
* <short description>
* $Id$
* $HeadURL$
* $Author$
* Created by Malte Schröder, SLSE, 2016-06-08
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "sortfilter.h"
#include <QCloseEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QModelIndex>
#include <LtsLicence/aes_crypt.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_ui(new Ui::MainWindow)
, m_licenceEdit(new LicenceEdit(this))
, m_licencesModel(new QStandardItemModel(this))
, m_cryptState(CryptState::Unknown)
{
m_ui->setupUi(this);
m_licenceEdit->setModal(true);
enableDisableButtons();
setTVHeaders();
m_filterProxyModel = new SortFilter(this);
m_filterProxyModel->setSourceModel(m_licencesModel);
m_ui->tvContent->setModel(m_filterProxyModel);
m_ui->tvContent->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_ui->leEncKey->setText(LtsLicence::passPhrase());
connect(m_ui->pbResetKey, &QPushButton::clicked, this, [this]() {m_ui->leEncKey->setText(LtsLicence::passPhrase());});
setWindowTitle(tr("Licence Editor"));
}
MainWindow::~MainWindow()
{
delete m_ui;
}
bool MainWindow::init()
{
connect(m_ui->actionOpen, &QAction::triggered, this, &MainWindow::onActionOpen);
connect(m_ui->actionSave, &QAction::triggered, this, &MainWindow::onActionSave);
connect(m_ui->actionSave_As, &QAction::triggered, this, &MainWindow::onActionSaveAs);
connect(m_ui->actionExit, &QAction::triggered, this, &MainWindow::close);
connect(m_ui->tvContent, &QTableView::doubleClicked, this, &MainWindow::onActionEdit);
connect(m_ui->nglDialogButtonBox, &NGLDialogButtonBox::onReset, this, &MainWindow::onActionReset);
connect(m_ui->nglDialogButtonBox, &NGLDialogButtonBox::onRemove, this, &MainWindow::onActionRemove);
connect(m_ui->nglDialogButtonBox, &NGLDialogButtonBox::onEdit, this, &MainWindow::onActionEdit);
connect(m_ui->nglDialogButtonBox, &NGLDialogButtonBox::onAdd, this, &MainWindow::onActionAdd);
connect(m_licenceEdit, &LicenceEdit::newLicence, this, &MainWindow::onNewLicence);
QPushButton *pb = m_ui->nglDialogButtonBox->button(NGLStandardButton::Add);
if (pb)
{
pb->setShortcut(tr("+"));
pb->setToolTip(tr("+"));
}
pb = m_ui->nglDialogButtonBox->button(NGLStandardButton::Remove);
if (pb)
{
pb->setShortcut(tr("-"));
pb->setToolTip(tr("-"));
}
pb = m_ui->nglDialogButtonBox->button(NGLStandardButton::Edit);
if (pb)
{
pb->setShortcut(tr("F2"));
pb->setToolTip(tr("F2"));
}
pb = m_ui->nglDialogButtonBox->button(NGLStandardButton::Reset);
if (pb)
{
pb->setShortcut(tr("Del"));
pb->setToolTip(tr("Del"));
}
return true;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (!isWindowModified())
{
QMainWindow::closeEvent(event);
return;
}
int ret = QMessageBox::warning(this, tr("Warning"),
tr("Do you want to save the changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
QMessageBox::Save);
switch (ret)
{
case QMessageBox::Save:
m_fileName.isEmpty() ? onActionSaveAs() : onActionSave();
break;
case QMessageBox::Discard:
event->accept();
break;
case QMessageBox::Cancel:
return;
}
QMainWindow::closeEvent(event);
}
void MainWindow::insertRow(const LtsLicence::Licence &licence, int currentRowLicModel)
{
if (currentRowLicModel >= m_licencesModel->rowCount())
{
m_licencesModel->setRowCount(m_licencesModel->rowCount() + 1);
currentRowLicModel = m_licencesModel->rowCount() - 1;
}
// -----APPEND NAME-----
QStandardItem *name = new QStandardItem(licence.name);
m_licencesModel->setItem(currentRowLicModel, 0, name);
// -----APPEND MAXCOUNT-----
QStandardItem *maxCount = new QStandardItem();
maxCount->setData(licence.maxCount);
if (licence.ignoreMaxCount)
maxCount->setText(tr("(%1 unused)").arg(QString::number(licence.maxCount)));
else
maxCount->setText(QString::number(licence.maxCount));
m_licencesModel->setItem(currentRowLicModel, 1, maxCount);
// -----APPEND EXPIRY-----
QStandardItem *expiry = new QStandardItem();
expiry->setData(licence.expiration);
if (licence.ignoreExpiration)
expiry->setText(tr("(%1 unused)").arg(licence.expiration.toString(tr("ddd dd.MM.yyyy HH:mm"))));
else
expiry->setText(licence.expiration.toString(tr("ddd dd.MM.yyyy HH:mm")));
m_licencesModel->setItem(currentRowLicModel, 2, expiry);
setTVHeaders();
enableDisableButtons();
}
void MainWindow::onNewLicence(const LtsLicence::Licence &licence)
{
if (m_licences.contains(licence.name))
{
m_licences[licence.name] = licence;
for (int i = 0; i < m_licencesModel->rowCount(); ++i)
{
if (m_licencesModel->item(i, 0)->text() == licence.name)
{
insertRow(licence, i);
setWindowModified(true);
return;
}
}
}
m_licences.insert(licence.name, licence);
insertRow(licence, m_licences.count());
setWindowModified(true);
m_ui->tvContent->setFocus();
}
void MainWindow::enableDisableButtons()
{
bool enable = m_licencesModel->rowCount() > 0;
m_ui->actionSave_As->setEnabled(enable);
m_ui->nglDialogButtonBox->setEnabled(NGLStandardButton::Remove, enable);
m_ui->nglDialogButtonBox->setEnabled(NGLStandardButton::Reset, enable);
m_ui->nglDialogButtonBox->setEnabled(NGLStandardButton::Edit, enable);
}
void MainWindow::setCurrentIndex()
{
QModelIndex index = m_ui->tvContent->currentIndex();
m_ui->tvContent->selectRow(index.row() + 1);
}
void MainWindow::setTVHeaders()
{
m_licencesModel->setHorizontalHeaderItem(0, new QStandardItem(tr("Name")));
m_licencesModel->setHorizontalHeaderItem(1, new QStandardItem(tr("Count")));
m_licencesModel->setHorizontalHeaderItem(2, new QStandardItem(tr("Expiry")));
m_ui->tvContent->resizeColumnsToContents();
m_ui->tvContent->horizontalHeader()->setStretchLastSection(true);
}
/**
* @brief this method is called by clicking the item "Open" in the Main-Menu
*
* The function opens a XML-file and loads each element of it into the QListWidget "lwContent".
* Furthermore, all items are going to append to the QVector<Licence> "m_licences"
*
* @return void
*/
void MainWindow::onActionOpen()
{
if (!onActionReset())
return;
const QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Licence File"),
QString(),
tr("Encrypted Licence Files (*.dat *.crl) (*.dat *.crl);;Unencrypted Licence Files (*.xml *.lic) (*.xml *.lic);;"));
if (fileName.isEmpty())
return;
m_fileName = fileName;
m_ui->actionSave->setEnabled(true);
QFile file(fileName);
if (!file.exists() ||
!file.open(QIODevice::ReadOnly))
{
file.close();
return;
}
QByteArray ba = file.readAll();
file.close();
if (ba.isEmpty())
return;
m_cryptState = CryptState::Decrypted;
QVector<LtsLicence::Licence> licVec = LtsLicence::readLicences(ba);
if (licVec.isEmpty())
{
ba = LtsLicence::decryptData(ba, m_ui->leEncKey->text());
licVec = LtsLicence::readLicences(ba);
if (licVec.isEmpty())
{
m_cryptState = CryptState::Unknown;
m_fileName.clear();
return;
}
m_cryptState = CryptState::Encrypted;
}
for (const LtsLicence::Licence &lic : qAsConst(licVec))
{
m_licences.insert(lic.name, lic);
insertRow(lic, m_licencesModel->rowCount());
}
QFileInfo fi(fileName);
setWindowTitle(fi.fileName() + QLatin1String("[*] - Licence Editor"));
m_ui->tvContent->resizeColumnsToContents();
m_ui->tvContent->sortByColumn(0, Qt::AscendingOrder);
setCurrentIndex();
}
/**
* @brief function adds an entry to the QListWidget
*
* The function onActionAddEntry() adds a new QListWidgetItem to lwContent.
* It will append to the QVector<Licence> "m_licences" in a smiliar way.
*
* @return void
*/
void MainWindow::onActionAdd()
{
m_licenceEdit->clear();
m_licenceEdit->show();
}
/**
* @brief writes the content of the QListWidget to a file
*
* This function writes all content of the QVector<Licence> "m_licences" which represents the content of the QListWidget to a XML-file
*
* @return void
*/
void MainWindow::onActionSaveAs()
{
if (m_licences.isEmpty())
{
QMessageBox::warning(this, tr("Error"), tr("There are no elements to save!"), QMessageBox::Ok);
return;
}
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Licence file"), QString()
, tr("Encrypted Licence Files (*.crl) (*.crl);;Decrypted Licence Files (*.lic) (*.lic)")
, &selectedFilter);
if (fileName.isEmpty())
return;
if (selectedFilter.contains(QLatin1String("lic")))
{
m_cryptState = CryptState::Decrypted;
m_ui->actionSave->setEnabled(true);
}
else if (selectedFilter.contains(QLatin1String("crl")))
{
m_cryptState = CryptState::Encrypted;
m_ui->actionSave->setEnabled(true);
}
else
{
m_cryptState = CryptState::Unknown;
m_ui->actionSave->setEnabled(false);
}
QFileInfo fi(fileName);
setWindowTitle(fi.fileName() + QLatin1String("[*] - Licence Editor"));
m_fileName = fileName;
onActionSave();
}
void MainWindow::onActionSave()
{
if (m_fileName.isEmpty())
return;
QVector<LtsLicence::Licence> licVec;
licVec.reserve(m_licences.size());
for (const LtsLicence::Licence &lic : qAsConst(m_licences))
licVec += lic;
QByteArray licenceBa = LtsLicence::writeUnencryptedLicences(licVec);
if (m_cryptState == CryptState::Encrypted)
licenceBa = LtsLicence::encryptData(licenceBa, m_ui->leEncKey->text());
QFile file(m_fileName);
if (!file.open(QIODevice::WriteOnly))
return;
file.write(licenceBa);
file.close();
setWindowModified(false);
}
void MainWindow::onActionEdit()
{
const QModelIndex mi = m_filterProxyModel->mapToSource(m_ui->tvContent->currentIndex());
if (!mi.isValid())
return;
QStandardItem *si = m_licencesModel->item(mi.row(), 0);
if (!si)
return;
QString licenceName = si->text();
m_licenceEdit->setLicence(m_licences[licenceName]);
m_licenceEdit->show();
}
/**
* @brief slot for Button pbClear clears both the QListWidget and the QVector<Licence> m_licences
*
* This functions resets the Application to its intial state. In doing so, the QListWidget m_lwContent and
* the QVector<Licence> "m_licences" gets resetted.
*
* @return void
*/
bool MainWindow::onActionReset()
{
if (!m_licences.isEmpty())
{
int ret = QMessageBox::warning(this, tr("Reset"), tr("You sure to reset the current list?"), QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No)
return false;
}
m_licencesModel->clear();
m_licencesModel->setRowCount(0);
setTVHeaders();
m_licences.clear();
m_fileName.clear();
m_ui->actionSave->setEnabled(false);
enableDisableButtons();
return true;
}
/**
* @brief function will be called by the Button pbDelete
*
* The function deletes the current QListWidgetItem of the QListWidget "lwContent"
*
* @return void
*/
void MainWindow::onActionRemove()
{
int ret = QMessageBox::question(this, tr("Remove"), tr("You sure to remove the selected item from the list?"), QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No)
return;
const QModelIndex mi = m_filterProxyModel->mapToSource(m_ui->tvContent->currentIndex());
if (!mi.isValid())
return;
int currentRow = mi.row();
QStandardItem *si = m_licencesModel->item(currentRow, 0);
if (!si)
return;
QString licenceName = si->text();
m_licencesModel->removeRow(currentRow);
m_licences.remove(licenceName);
enableDisableButtons();
setCurrentIndex();
}