summaryrefslogtreecommitdiffstats
path: root/gui/widgets/ServerStatus.cpp
blob: 1b1bb6f7985ccf835d1d98a0194afc59f67d3cf8 (plain)
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
#include "ServerStatus.h"
#include "logic/status/StatusChecker.h"

#include "MultiMC.h"

#include <QHBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QMap>
#include <QToolButton>

ServerStatus::ServerStatus(QWidget *parent, Qt::WindowFlags f)
	:QWidget(parent, f)
{
	clear();
	goodIcon = QPixmap(":/icons/multimc/48x48/status-good.png");
	goodIcon.setDevicePixelRatio(2.0);
	badIcon = QPixmap(":/icons/multimc/48x48/status-bad.png");
	badIcon.setDevicePixelRatio(2.0);
	addStatus(tr("No status available"), false);
	m_statusRefresh = new QToolButton(this);
	m_statusRefresh->setCheckable(true);
	m_statusRefresh->setToolButtonStyle(Qt::ToolButtonIconOnly);
	m_statusRefresh->setIcon(QIcon::fromTheme("refresh"));
	// Start status checker
	{
		connect(MMC->statusChecker().get(), &StatusChecker::statusLoaded, this,
				&ServerStatus::updateStatusUI);
		connect(MMC->statusChecker().get(), &StatusChecker::statusLoadingFailed, this,
				&ServerStatus::updateStatusFailedUI);

		connect(m_statusRefresh, &QAbstractButton::clicked, this, &ServerStatus::reloadStatus);
		connect(&statusTimer, &QTimer::timeout, this, &ServerStatus::reloadStatus);
		statusTimer.setSingleShot(true);

		reloadStatus();
	}

}

ServerStatus::addLine()
{
	auto line = new QFrame(this);
	line->setFrameShape(QFrame::VLine);
	line->setFrameShadow(QFrame::Sunken);
	layout->addWidget(line);
}

ServerStatus::addStatus(QString name, bool online)
{
	auto label = new QLabel(this);
	label->setText(name);
	if(online)
		label->setPixmap(goodIcon);
	else
		label->setPixmap(badIcon);
	layout->addWidget(label);
}

ServerStatus::clear()
{
	if(layout)
		delete layout;
	layout = new QHBoxLayout(this);
}

void ServerStatus::StatusChanged(QMap<QString, QString> statusEntries)
{
	clear();
	int howmany = statusEntries.size();
	int index = 0;
	auto iter = statusEntries.begin();
	while (iter != statusEntries.end())
	{
		addStatus();
		index++;
	}
}

static QString convertStatus(const QString &status)
{
	QString ret = "?";

	if (status == "green")
		ret = "↑";
	else if (status == "yellow")
		ret = "-";
	else if (status == "red")
		ret = "↓";

	return "<span style=\"font-size:11pt; font-weight:600;\">" + ret + "</span>";
}

void ServerStatus::reloadStatus()
{
	m_statusRefresh->setChecked(true);
	MMC->statusChecker()->reloadStatus();
	// updateStatusUI();
}

static QString makeStatusString(const QMap<QString, QString> statuses)
{
	QString status = "";
	status += "Web: " + convertStatus(statuses["minecraft.net"]);
	status += "  Account: " + convertStatus(statuses["account.mojang.com"]);
	status += "  Skins: " + convertStatus(statuses["skins.minecraft.net"]);
	status += "  Auth: " + convertStatus(statuses["authserver.mojang.com"]);
	status += "  Session: " + convertStatus(statuses["sessionserver.mojang.com"]);

	return status;
}

void ServerStatus::updateStatusUI()
{
	m_statusRefresh->setChecked(false);
	MMC->statusChecker()->getStatusEntries();
	statusTimer.start(60 * 1000);
}

void ServerStatus::updateStatusFailedUI()
{
	m_statusRefresh->setChecked(false);
	StatusChanged();
	statusTimer.start(60 * 1000);
}