📝 docs(_exp): function 05

This commit is contained in:
eshanized
2024-12-20 19:41:58 +05:30
parent 7eab2e3f83
commit 531cf5d17e

View File

@@ -139,68 +139,97 @@ void SnigdhaOSBlackbox::doUpdate() {
void SnigdhaOSBlackbox::doApply() { void SnigdhaOSBlackbox::doApply() {
// Declare three QStringLists to hold the packages, setup commands, and prepare commands
QStringList packages; QStringList packages;
QStringList setup_commands; QStringList setup_commands;
QStringList prepare_commands; QStringList prepare_commands;
// Find all QCheckBox widgets within the selectWidget_tabs widget
auto checkBoxList = ui->selectWidget_tabs->findChildren<QCheckBox*>(); auto checkBoxList = ui->selectWidget_tabs->findChildren<QCheckBox*>();
// Iterate through each checkbox and check if it's checked
for (auto checkbox : checkBoxList) { for (auto checkbox : checkBoxList) {
if (checkbox->isChecked()) { if (checkbox->isChecked()) {
packages += checkbox->property("packages").toStringList(); // If the checkbox is checked, retrieve its associated properties and add them to the lists
setup_commands += checkbox->property("setup_commands").toStringList(); packages += checkbox->property("packages").toStringList(); // Add selected package names to 'packages'
prepare_commands += checkbox->property("prepare_commands").toStringList(); setup_commands += checkbox->property("setup_commands").toStringList(); // Add setup commands to 'setup_commands'
prepare_commands += checkbox->property("prepare_commands").toStringList(); // Add preparation commands to 'prepare_commands'
} }
} }
// If no packages were selected, mark the state as 'SUCCESS' and exit early
if (packages.isEmpty()) { if (packages.isEmpty()) {
updateState(State::SUCCESS); updateState(State::SUCCESS);
return; return;
} }
// If 'podman' is selected in packages, add a system setup command for it
if (packages.contains("podman")) { if (packages.contains("podman")) {
setup_commands += "systemctl enable --now podman.socket"; setup_commands += "systemctl enable --now podman.socket";
} }
// If 'docker' is selected in packages, add a system setup command for it
if (packages.contains("docker")) { if (packages.contains("docker")) {
setup_commands += "systemctl enable --now docker.socket"; setup_commands += "systemctl enable --now docker.socket";
} }
// Remove duplicate entries in the 'packages' list to avoid redundant installations
packages.removeDuplicates(); packages.removeDuplicates();
// Create a temporary file to store the preparation commands and automatically delete it when no longer needed
QTemporaryFile* prepareFile = new QTemporaryFile(this); QTemporaryFile* prepareFile = new QTemporaryFile(this);
prepareFile->setAutoRemove(true); prepareFile->setAutoRemove(true); // Ensure this file is removed automatically when it goes out of scope
prepareFile->open(); prepareFile->open(); // Open the file for writing
// Create a QTextStream to write the prepare commands to the temporary file
QTextStream prepareStream(prepareFile); QTextStream prepareStream(prepareFile);
prepareStream << prepare_commands.join('\n'); prepareStream << prepare_commands.join('\n'); // Join the list of prepare commands with newlines and write to the file
prepareFile->close(); prepareFile->close(); // Close the file after writing
// Create another temporary file to store the selected packages
QTemporaryFile* packagesFile = new QTemporaryFile(this); QTemporaryFile* packagesFile = new QTemporaryFile(this);
packagesFile->setAutoRemove(true); packagesFile->setAutoRemove(true);
packagesFile->open(); packagesFile->open(); // Open the file for writing
// Create a QTextStream to write the list of packages to the temporary file
QTextStream packagesStream(packagesFile); QTextStream packagesStream(packagesFile);
packagesStream << packages.join(' '); packagesStream << packages.join(' '); // Join the package names with spaces and write to the file
packagesFile->close(); packagesFile->close(); // Close the file after writing
// Create a third temporary file to store the setup commands
QTemporaryFile* setupFile = new QTemporaryFile(this); QTemporaryFile* setupFile = new QTemporaryFile(this);
setupFile->setAutoRemove(true); setupFile->setAutoRemove(true);
setupFile->open(); setupFile->open(); // Open the file for writing
// Create a QTextStream to write the setup commands to the temporary file
QTextStream setupStream(setupFile); QTextStream setupStream(setupFile);
setupStream << setup_commands.join('\n'); setupStream << setup_commands.join('\n'); // Join the setup commands with newlines and write to the file
setupFile->close(); setupFile->close(); // Close the file after writing
// Create a QProcess to execute the shell script and pass the temporary file paths as arguments
auto process = new QProcess(this); auto process = new QProcess(this);
process->start("/usr/lib/snigdhaos/launch-terminal", QStringList() << QString("/usr/lib/snigdhaos-blackbox/apply.sh \"") + prepareFile->fileName() + "\" \"" + packagesFile->fileName() + "\" \"" + setupFile->fileName() + "\""); process->start("/usr/lib/snigdhaos/launch-terminal",
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, [this, process, prepareFile, packagesFile, setupFile](int exitcode, QProcess::ExitStatus status) { QStringList() << QString("/usr/lib/snigdhaos-blackbox/apply.sh \"") +
prepareFile->fileName() + "\" \"" +
packagesFile->fileName() + "\" \"" +
setupFile->fileName() + "\"");
// When the process finishes, the following lambda function is triggered
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [this, process, prepareFile, packagesFile, setupFile](int exitcode, QProcess::ExitStatus status) {
// Clean up: delete the QProcess and the temporary files after the process finishes
process->deleteLater(); process->deleteLater();
prepareFile->deleteLater(); prepareFile->deleteLater();
packagesFile->deleteLater(); packagesFile->deleteLater();
setupFile->deleteLater(); setupFile->deleteLater();
// If the process was successful (exit code 0) and the temporary packages file no longer exists
if (exitcode == 0 && !packagesFile->exists()) { if (exitcode == 0 && !packagesFile->exists()) {
// Mark the state as 'SELECT' to indicate the operation was successful
updateState(State::SELECT); updateState(State::SELECT);
} }
else { else {
// If there was an error (non-zero exit code or file exists), mark the state as 'APPLY_RETRY'
updateState(State::APPLY_RETRY); updateState(State::APPLY_RETRY);
} }
}); });