Thank you for your assistance.
This is my first c++ program so please be patient with me 
Please forgive the long post. The following is a small app that I am using for testing before I add it into the real app.
First up is mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "purge.h"
#include <QtGui>
#include <QProcess>
#include <QTimer>
#include <QtDebug> // don't forget to remove when we are done
//#include <QDialog>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect( timer, SIGNAL(timeout()), SLOT(listContainers()) );
timer->start(5000);
listContainers(); // implicitly call the function to display the containers
// instead of waiting for the timer
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
// List containers registered with LXC. We use stdout from lxc-ls to grab the container name
// and then we use stdout from lxc-info to determine the status of the container.
void MainWindow::listContainers()
{
ui
->treeWidget
->header
()->setResizeMode
(0,
QHeaderView::Stretch);
ui
->treeWidget
->header
()->setResizeMode
(1,
QHeaderView::Stretch);
ui->treeWidget->clear();
QString listProgram
= "/usr/bin/lxc-ls";
listProcess->start(listProgram);
listProcess->waitForFinished(-1);
do
{
listOutput = listProcess->readLine();
listOutput.chop(1);
if (listOutput.isEmpty())
break;
QString infoProgram
= "/usr/bin/lxc-info";
arguements << "-n" << listOutput;
infoProcess->start(infoProgram, arguements);
infoProcess->waitForFinished(-1);
QString infoOutput
= infoProcess
->readLine
();
// The output of lxc-info includes the container name as well as the status,
// but the status is in all caps.
// Grab just the status and then beautify it.
infoOutput.chop(1);
if (infoOutput.contains("RUNNING", Qt::CaseInsensitive))
infoOutput = "Running";
if (infoOutput.contains("STOPPED", Qt::CaseInsensitive))
infoOutput = "Shutoff";
if (infoOutput.contains("FROZEN", Qt::CaseInsensitive))
infoOutput = "Paused";
item->setText(0, listOutput);
item->setText(1, infoOutput);
ui->treeWidget->addTopLevelItem(item);
}
while (listOutput != NULL);
};
// Use a messagebox to confirm we want the cache removed and then
// display a new form until the cache removal process is complete.
void MainWindow::on_actionPurge_Cache_triggered()
{
qDebug() << "Displaying confirmation message";
purgeMsgBox.setWindowTitle("Purge Cache Request");
purgeMsgBox.setText("Are you sure you would like to purge the local cache?");
int purgeReply = purgeMsgBox.exec();
switch (purgeReply)
{
qDebug() << "We Clicked cancel";
break;
qDebug() << "We clicked ok";
purge dialog(this);
dialog.exec();
if (dialog.
exec() != QDialog::Accepted) return;
break;
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "purge.h"
#include <QtGui>
#include <QProcess>
#include <QTimer>
#include <QtDebug> // don't forget to remove when we are done
//#include <QDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect( timer, SIGNAL(timeout()), SLOT(listContainers()) );
timer->start(5000);
listContainers(); // implicitly call the function to display the containers
// instead of waiting for the timer
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
// List containers registered with LXC. We use stdout from lxc-ls to grab the container name
// and then we use stdout from lxc-info to determine the status of the container.
void MainWindow::listContainers()
{
ui->treeWidget->header()->setResizeMode(0, QHeaderView::Stretch);
ui->treeWidget->header()->setResizeMode(1, QHeaderView::Stretch);
ui->treeWidget->clear();
QString listProgram = "/usr/bin/lxc-ls";
QProcess *listProcess = new QProcess( this );
listProcess->start(listProgram);
listProcess->waitForFinished(-1);
QString listOutput;
do
{
item = new QTreeWidgetItem;
listOutput = listProcess->readLine();
listOutput.chop(1);
if (listOutput.isEmpty())
break;
QString infoProgram = "/usr/bin/lxc-info";
QStringList arguements;
arguements << "-n" << listOutput;
QProcess *infoProcess = new QProcess( this );
infoProcess->start(infoProgram, arguements);
infoProcess->waitForFinished(-1);
QString infoOutput = infoProcess->readLine();
// The output of lxc-info includes the container name as well as the status,
// but the status is in all caps.
// Grab just the status and then beautify it.
infoOutput.chop(1);
if (infoOutput.contains("RUNNING", Qt::CaseInsensitive))
infoOutput = "Running";
if (infoOutput.contains("STOPPED", Qt::CaseInsensitive))
infoOutput = "Shutoff";
if (infoOutput.contains("FROZEN", Qt::CaseInsensitive))
infoOutput = "Paused";
item->setText(0, listOutput);
item->setText(1, infoOutput);
ui->treeWidget->addTopLevelItem(item);
}
while (listOutput != NULL);
};
// Use a messagebox to confirm we want the cache removed and then
// display a new form until the cache removal process is complete.
void MainWindow::on_actionPurge_Cache_triggered()
{
qDebug() << "Displaying confirmation message";
QMessageBox purgeMsgBox;
purgeMsgBox.setWindowTitle("Purge Cache Request");
purgeMsgBox.setText("Are you sure you would like to purge the local cache?");
purgeMsgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
purgeMsgBox.setDefaultButton(QMessageBox::Cancel);
int purgeReply = purgeMsgBox.exec();
switch (purgeReply)
{
case QMessageBox::Cancel:
qDebug() << "We Clicked cancel";
break;
case QMessageBox::Ok:
qDebug() << "We clicked ok";
purge dialog(this);
dialog.exec();
if (dialog.exec() != QDialog::Accepted)
return;
break;
}
}
To copy to clipboard, switch view to plain text mode
Next up is mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTreeWidget>
namespace Ui {
class MainWindow;
}
Q_OBJECT
public:
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
private slots:
void on_actionPurge_Cache_triggered();
void listContainers();
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTreeWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
QTreeWidgetItem *item;
private slots:
void on_actionPurge_Cache_triggered();
void listContainers();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
Next we have the window that I can't seem to close.
purge.cpp
#include "purge.h"
#include "ui_purge.h"
#include <QtDebug>
#include <QProcess>
ui(new Ui::purge)
{
ui->setupUi(this);
purgeCache();
qDebug() << "we are no longer in the purgeCache function";
}
purge::~purge()
{
delete ui;
}
void purge
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
// Use QProcess to monitor the removal of the local cache and wait until the cache is removed
// until we exit the function
void purge::purgeCache()
{
qDebug() << "We are in function to purge cache";
QString cacheLocation
= "/var/cache/lxc";
arguements << "-rf" << cacheLocation;
removeProcess->start(removeProgram, arguements);
removeProcess->waitForFinished(-1);
qDebug() << "purge process has completed";
close();
}
#include "purge.h"
#include "ui_purge.h"
#include <QtDebug>
#include <QProcess>
purge::purge(QWidget *parent) :
QDialog(parent),
ui(new Ui::purge)
{
ui->setupUi(this);
purgeCache();
qDebug() << "we are no longer in the purgeCache function";
}
purge::~purge()
{
delete ui;
}
void purge::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
// Use QProcess to monitor the removal of the local cache and wait until the cache is removed
// until we exit the function
void purge::purgeCache()
{
qDebug() << "We are in function to purge cache";
QString cacheLocation = "/var/cache/lxc";
QString removeProgram = "/bin/rm";
QStringList arguements;
arguements << "-rf" << cacheLocation;
QProcess *removeProcess = new QProcess(this);
removeProcess->start(removeProgram, arguements);
removeProcess->waitForFinished(-1);
qDebug() << "purge process has completed";
close();
}
To copy to clipboard, switch view to plain text mode
And the header for purge:
#ifndef PURGE_H
#define PURGE_H
#include <QDialog>
namespace Ui {
class purge;
}
Q_OBJECT
public:
~purge();
protected:
private:
Ui::purge *ui;
void purgeCache();
};
#endif // PURGE_H
#ifndef PURGE_H
#define PURGE_H
#include <QDialog>
namespace Ui {
class purge;
}
class purge : public QDialog {
Q_OBJECT
public:
purge(QWidget *parent = 0);
~purge();
protected:
void changeEvent(QEvent *e);
private:
Ui::purge *ui;
void purgeCache();
};
#endif // PURGE_H
To copy to clipboard, switch view to plain text mode
And lastly, main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks