summaryrefslogtreecommitdiffstats
path: root/src/speedcheck.cpp
blob: 344be46eba81703ec7f4e6de9280b08c4b1433c3 (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
#include "speedcheck.h"
#include "ui_speedcheck.h"
#include "copythread.h"
#include "datasource/cpuload.h"
#include "datasource/networkspeed.h"

#include <QCoreApplication>
#include <QMessageBox>
#include <QNetworkInterface>
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <QTimer>
#include "addserver.h"

SpeedCheck::SpeedCheck(QString fileName, bool cml)
	: _ui(new Ui::MainWindow),
	  _thread(NULL),
	  _doQuit(false),
	  _fileName(fileName)
{
	_ui->setupUi(this);
	connect(_ui->btnStart, SIGNAL(clicked(bool)), this, SLOT(startClicked(bool)));
	connect(_ui->btnQuit, SIGNAL(clicked(bool)), this, SLOT(quitClicked(bool)));
    connect(_ui->btnselect, SIGNAL(clicked(bool)), this, SLOT(selectClicked(bool)));
    connect(_ui->btnrefreshNetwork, SIGNAL(clicked(bool)), this, SLOT(networkrefreshClicked(bool)));
    connect(_ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateNetworkInfos(int)));
    connect(_ui-> actionAdd_Server, SIGNAL(triggered(bool)), this, SLOT(addServerClicked(bool)));
    _ui->filelabel->setText("Selected File: " + _fileName);
	_timer.setInterval(200);
	connect(&_timer, SIGNAL(timeout()), this, SLOT(updateTimer()));
	qsrand((uint)QCoreApplication::applicationPid());
    networkrefreshClicked(true);
    if (cml) startClicked(true);
}

SpeedCheck::~SpeedCheck()
{

}

void SpeedCheck::logMessage(QString message)
{
    _ui->statusBar->showMessage(message);
}
void SpeedCheck::selectClicked(bool) {
    _fileName = QFileDialog::getOpenFileName(this, tr("Select benchmark file"));
    _ui->filelabel->setText("Selected File: " + _fileName);

}
void SpeedCheck::startClicked(bool)
{
    QFile *file = new QFile(_fileName);
	if (!file->open(QIODevice::ReadOnly)) {
		QMessageBox::critical(this, trUtf8("Error"), trUtf8("Could not open %1 for reading.").arg(_fileName));
		return;
	}
	_ui->picCpu->setDataSource(new CpuLoad());
    _ui->picSpeed->setDataSource(new NetworkSpeed(_ui->comboBox->currentText()));
	_ui->btnStart->setDisabled(true);
    _ui->btnselect->setDisabled(true);
    _ui->comboBox->setDisabled(true);
    _ui->btnrefreshNetwork->setDisabled(true);
	delete _thread;
	_thread = new CopyThread(file, this);
    connect(_thread, SIGNAL(logMessage(QString)), this, SLOT(logMessage(QString)), Qt::QueuedConnection);
	connect(_thread, SIGNAL(finished()), this, SLOT(testFinished()), Qt::QueuedConnection);
	_timer.start();
	_thread->start();
}
void SpeedCheck::quitClicked(bool)
{
	if (_thread != NULL && _thread->isRunning()) {
		_doQuit = true;
		_thread->stop();
	}
	if (_thread == NULL || !_thread->isRunning()) {
		QCoreApplication::instance()->quit();
	}
}

void SpeedCheck::testFinished()
{
	_timer.stop();
	delete _thread;
	_thread = NULL;
	if (_doQuit) {
		QCoreApplication::instance()->quit();
	}
	_ui->btnStart->setEnabled(true);
    _ui->btnselect->setEnabled(true);
    _ui->comboBox->setEnabled(true);
    _ui->btnrefreshNetwork->setEnabled(true);
    quitClicked(true);
    // TODO start next test if available
}
void SpeedCheck::networkrefreshClicked(bool)
{
    while (_ui->comboBox->count() > 0) {
        _ui->comboBox->removeItem(0);
    }
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
        foreach (QNetworkInterface iface, interfaces) {
            if ((iface.flags().testFlag(QNetworkInterface::IsUp)) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
                _ui->comboBox->addItem(iface.name());
            }
        }
}
void SpeedCheck::updateNetworkInfos(int) {
    QString path = "/sys/class/net/" + _ui->comboBox->currentText();
    QFile file(path + "/speed");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;
    QByteArray speed = file.readLine();
    QDir wireless(path + "/wireless");
    // Remove new line at End of the line
    speed.remove(speed.length() - 1, 1);
    if (!wireless.exists()) {
        if (speed.toInt() < 1000) {
            _ui->networkSpeed->setStyleSheet("QLabel {color:orange;}");
            _ui->networkSpeed->setToolTip("Networkspeed slow!");
        }
        else {
            _ui->networkSpeed->setStyleSheet("QLabel {color:black;}");
            _ui->networkSpeed->setToolTip("");
        }
        _ui->connType->setText("wired");
        _ui->networkSpeed->setEnabled(true);
        _ui->link->setEnabled(true);
        _ui->link->setText("Speed: ");
        _ui->networkSpeed->setText(speed + " MB/s");
    }
    else {
        _ui->networkSpeed->setDisabled(true);
        _ui->networkSpeed->setText("");
        _ui->link->setText("");
        _ui->connType->setText("wireless");
    }
}
void SpeedCheck::updateTimer()
{
    _ui->picCpu->readNextValue();
	_ui->picSpeed->readNextValue();
}
void SpeedCheck::addServerClicked(bool) {
    QWidget *wdg = new AddServer();
    wdg->show();
    logMessage("Hi");
}