summaryrefslogtreecommitdiffstats
path: root/LogReceiver/ndgui.cpp
blob: a839aefafd39b92ab2060f9f6b80938f160916f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "ndgui.h"
#include "chooseinterfacedialog.h"
#include "abortbootdialog.h"

ndgui::ndgui(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);

	connect(&logReceiver, SIGNAL(addNewInterface(QString)), this, SLOT(addNewInterface(QString)));
    connect(&logReceiver, SIGNAL(changeProgressBarValue(QString , int )), this, SLOT(handleProgress(QString, int)));
    connect(&logReceiver, SIGNAL(connectionEstablished(QString)), this, SLOT(handleConnectionEstablished(QString)));
    connect(&logReceiver, SIGNAL(abortBoot(QString)), this, SLOT(handleAbortBoot(QString)));
    connect(&logReceiver, SIGNAL(updateStatusLabel(QString,QString)), this, SLOT(handleUpdateStatusLabel(QString, QString)));
    connect(&logReceiver, SIGNAL(allProcessesFinished()), this, SLOT(handleAllProcessesFinished()));

    buildGui();

    logReceiver.initAndRun("/var/tmp/qt_c_socket_custom");
    numberOfInterfaces = 0;


	setWindowTitle(tr("NetD"));
}

ndgui::~ndgui()
{

}

void ndgui::buildGui() {

	ndStatusLabel = new QLabel(tr("test"));
	ndStatusLabel->setSizePolicy(QSizePolicy::Expanding,
	                                    QSizePolicy::Expanding);
	ndStatusLabel->setAlignment(Qt::AlignCenter);
	ndStatusLabel->setMinimumSize(100, 20);

	// create interface group box
	createInterfaceGroupBox();



    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(ndStatusLabel);
    mainLayout->addWidget(interfaceGroupBox);

    setLayout(mainLayout);
}

void ndgui::createInterfaceGroupBox(){
    interfaceGroupBox = new QGroupBox(tr("Interfaces"));

    interfaceGroupBoxLayout = new QVBoxLayout;
    /* add interfaces via addInterfacesToGroupBox()*/

    interfaceGroupBox->setLayout(interfaceGroupBoxLayout);
}

void ndgui::addNewInterface(QString ifName) {
	qDebug() << "receive interface to add:" << ifName;
	QHBoxLayout *hBoxLayout = new QHBoxLayout;
	QLabel *label = new QLabel(ifName);
	QLabel *labelStatus = new QLabel("waiting");
	QProgressBar *pBar = new QProgressBar(this);
	pBar->setRange(1, 100);
	pBar->setMaximumSize(200, 20);

	statusLabels.insert(ifName, labelStatus);
	progressBars.insert(ifName, pBar);

	hBoxLayout->addWidget(label, Qt::AlignLeft);
	hBoxLayout->addWidget(labelStatus, Qt::AlignCenter);
	hBoxLayout->addWidget(pBar, Qt::AlignRight);

	numberOfInterfaces++;

	interfaceGroupBoxLayout->addLayout(hBoxLayout, 2);
}

void ndgui::handleProgress(QString ifName, int newValue) {
	qDebug() << "<[---]> SLOT handleProgress activated with: " << ifName << newValue;
		QProgressBar * pBar = progressBars.value(ifName);
		if(newValue >= pBar->value()) {
	        pBar->setValue(newValue);
		}
		else {
		    qDebug() << "Error: new value is smaller than the old value!";
		}
}

void ndgui::handleConnectionEstablished(QString ifName) {
    finalUsableInterfaces.append(ifName);
}

void ndgui::handleAbortBoot(QString msg) {
	qDebug() << "abort boot. reason:" << msg;
	showAbortBootDialog();
}

void ndgui::handleUpdateStatusLabel(QString ifName, QString status) {
	QLabel* label = statusLabels.value(ifName);
	label->setText(status);
}

void ndgui::handleAllProcessesFinished() {
	qDebug() << "all Processes finished";

	if (finalUsableInterfaces.size() > 0) {
		showChooseInterfaceDialog();
	} else {
		showAbortBootDialog();
	}
}

void ndgui::showAbortBootDialog() {
	aBD = new AbortBootDialog(this);
	connect(aBD, SIGNAL(showLogSignal()), this, SLOT(showLog()));
	connect(aBD, SIGNAL(restartSignal()), this, SLOT(restartSystem()));
	connect(aBD, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem()));
	aBD->setModal(true);
	aBD->show();
}

void ndgui::showChooseInterfaceDialog() {
	cID = new ChooseInterfaceDialog(finalUsableInterfaces, this);
	connect(cID, SIGNAL(continueSignal(QString)), this,
			SLOT(continueBoot(QString)));
	connect(cID, SIGNAL(restartSignal()), this, SLOT(restartSystem()));
	connect(cID, SIGNAL(shutDownSignal()), this, SLOT(shutDownSystem()));
	cID->setModal(true);
	cID->show();
}

void ndgui::restartSystem()
{

    if(qobject_cast<AbortBootDialog*>(QObject::sender())>0)
    {
    	qDebug() << "received Signal restart abd";
        aBD->closeDialog();
    }
    else if(qobject_cast<ChooseInterfaceDialog*>(QObject::sender())>0)
    {
    	qDebug() << "received Signal restart cid";
        cID->close();
    }
    else
    {
        qDebug() << "unknown sender" << QObject::sender();
    }


}

void ndgui::shutDownSystem()
{
    if(qobject_cast<AbortBootDialog*>(QObject::sender())>0)
    {

        aBD->closeDialog();
    }
    else if(qobject_cast<ChooseInterfaceDialog*>(QObject::sender())>0)
    {

        cID->close();
    }
    else
    {
        qDebug() << "unknown sender" << QObject::sender();
    }

}

void ndgui::continueBoot(QString ifName)
{
    QString text = "continue with interface: " + ifName;
    cID->close();
}

void ndgui::showLog()
{
    qDebug() << "show log";
}