summaryrefslogtreecommitdiff
path: root/client/Dialog.cpp
blob: 279bfc8c8621e77c67f9b8dabdfc6412fca2af4f (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

#include <QtGui>
#include <iostream>
#include <sstream>
#include "client.h"
#include "UpdatedConfig.h"
#include "Dialog.h"
#include "Proxifier.h"
#include "Proxy.h"

using namespace std;

ProxyDialog::ProxyDialog(QWidget *parent): QDialog(parent)
{
	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	
	Proxifier *proxifier = Proxifier::GetInstance();
	if (!proxifier->IsValid())
	{
        Logger::Fatal("No valid proxifier configuration file found!\n");
	}
	
	/* generic proxy panel */
	topLabel = new QLabel(QString::fromLocal8Bit(cfg->TopPanelText.c_str()));
	topLabel->setObjectName("topLabel");
	topLabel->setAlignment(Qt::AlignHCenter);
	countryBox = new QComboBox;
	stateBox = new QComboBox;
	stateBox->setVisible(false);
	cityBox = new QComboBox;
	cityBox->setVisible(false);
	vector<string> countries = cfg->GetCountries();
	for (unsigned i = 0; i < countries.size(); i++)
	{
		QString country = QString::fromLocal8Bit(countries[i].c_str());
		countryBox->addItem(country);
	}
	countryBox->setCurrentIndex(-1);

	connect(countryBox, SIGNAL(activated(int)),
			this, SLOT(CountryActivated(int)));
	connect(stateBox, SIGNAL(activated(int)),
			this, SLOT(StateActivated(int)));
	connect(cityBox, SIGNAL(activated(int)),
			this, SLOT(CityActivated(int)));

	topPanelLayout = new QGridLayout;
	topPanelLayout->setSpacing(5);
	topPanelLayout->addWidget(countryBox, 0, 0);
	topPanelLayout->addWidget(stateBox, 1, 0);
	topPanelLayout->addWidget(cityBox, 2, 0);
	genericProxyGroup = new QButtonGroup;
	staticProxyGroup = new QButtonGroup;

	// static proxy panel
	bottomLabel = new QLabel(QString::fromLocal8Bit(cfg->BottomPanelText.c_str()));
	bottomLabel->setObjectName("bottomLabel");
	bottomLabel->setAlignment(Qt::AlignHCenter);
	QGridLayout *bottomPanelLayout = new QGridLayout;
	unsigned nlines = cfg->GetStaticProxyGuiLines();
	for (unsigned i = 0; i < nlines; i++)
	{
		vector<ProxyEntryStatic> staticProxyLine = cfg->GetStaticProxyGuiLine(i+1);
		for (unsigned j = 0; j < staticProxyLine.size(); j++)
		{
			stringstream ss;
			if (cfg->IsSpeedVisible)
			{
				if (staticProxyLine[j].speed > cfg->StaticProxySpeedLow)
				{
					ss << "<font color=\"green\"><b>" << staticProxyLine[j].speed << "</b></font>";
				}
				else
				{
					ss << "<font color=\"red\"><b>" << staticProxyLine[j].speed << "</b></font>";
				}
				ss << " ";
			}
			ss << staticProxyLine[j].name;

			QString btnStr = QString::fromLocal8Bit(ss.str().c_str());
			QLabel *btnLabel = new QLabel(btnStr);
			btnLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
			QHBoxLayout *btnLayout = new QHBoxLayout;
			// insert spacers at the beginning and
			// the end to center-align button content
			btnLayout->addStretch();
			btnLayout->addWidget(btnLabel);
			btnLayout->setSizeConstraint(QLayout::SetMinimumSize);
			btnLayout->addStretch();
			QPushButton *btn = new QPushButton();
			btn->setObjectName("bottomBtn");
			btn->setCheckable(true);
			btn->setChecked(proxifier->IsOn(staticProxyLine[j].host, staticProxyLine[j].port));
			btn->setProperty("proxyName", QString::fromLocal8Bit(staticProxyLine[j].name.c_str()));
			btn->setLayout(btnLayout);
			connect(btn, SIGNAL(toggled(bool)), this, SLOT(StaticToggled(bool)));
			staticProxyGroup->addButton(btn);
			bottomPanelLayout->addWidget(btn, i, j);
		}
	}

	/* setup layouting */
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(topLabel);
	mainLayout->addLayout(topPanelLayout);
	mainLayout->addWidget(bottomLabel);
	mainLayout->addLayout(bottomPanelLayout);
	setLayout(mainLayout);
}

void ProxyDialog::StaticToggled(bool on)
{
	QPushButton* btn = (QPushButton*) sender();
	QVariant propVal = btn->property("proxyName");
	QString proxyName = propVal.toString();
	Logger::Debug("Static Proxy button toggled\n");
	Logger::Debug("State '%s', associated proxy name: %s\n", on ? "down" : "up", proxyName.toStdString().c_str());
	
	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	string name = proxyName.toStdString();
	ProxyEntryStatic* proxy = cfg->GetStaticProxy(name);
	if (proxy == NULL)
	{
		Logger::Error("Static proxy with name '%s' was not found\n", proxyName.toStdString().c_str());
		return;
	}
	Proxifier *proxifier = Proxifier::GetInstance();
	if (!proxifier->IsValid())
	{
        Logger::Fatal("No valid proxifier configuration file found!\n");
	}
	if (on)
	{
		proxifier->TurnProxyOn(*proxy);
	}
	else
	{
		proxifier->TurnProxyOff(*proxy);
	}
}

void ProxyDialog::CountryActivated(int index)
{
	Logger::Trace("Country activated. Activated index=%d\n", index);
	stateBox->setVisible(false);
	cityBox->setVisible(false);
	if (index == -1)
	{
		return;
	}

	string country(countryBox->currentText().toLocal8Bit().constData());
	Logger::Info("Country %s was selected\n", country.c_str());

	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	vector<string> states = cfg->GetStates(country);
	if (states.empty())
	{
		Logger::Info("There are no states in %s\n",country.c_str());
		vector<string> cities = cfg->GetCities(country);
		cityBox->clear();
		for (unsigned i = 0; i < cities.size(); i++)
		{
			QString city = QString::fromLocal8Bit(cities[i].c_str());
			cityBox->addItem(city);
		}
		cityBox->setCurrentIndex(-1);
		cityBox->adjustSize();
		cityBox->setVisible(true);
	}
	else
	{
		stateBox->clear();
		for (unsigned i = 0; i < states.size(); i++)
		{
			QString state = QString::fromLocal8Bit(states[i].c_str());
			stateBox->addItem(state);
		}
		stateBox->setCurrentIndex(-1);
		stateBox->adjustSize();
		stateBox->setVisible(true);
	}
}

void ProxyDialog::StateActivated(int index)
{
	Logger::Trace("State activated. Activated index=%d\n", index);
	cityBox->setVisible(false);
	if (index == -1)
	{
		return;
	}

	string country(countryBox->currentText().toLocal8Bit().constData());
	string state(stateBox->currentText().toLocal8Bit().constData());
	Logger::Info("State %s was selected\n", state.c_str());

	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	vector<string> cities = cfg->GetCities(country, state);
	cityBox->clear();
	for (unsigned i = 0; i < cities.size(); i++)
	{
		QString city = QString::fromLocal8Bit(cities[i].c_str());
		cityBox->addItem(city);
	}
	cityBox->setCurrentIndex(-1);
	cityBox->adjustSize();
	cityBox->setVisible(true);
}

void ProxyDialog::CityActivated(int index)
{
	Logger::Trace("City activated. Activated index=%d\n", index);
	if (index == -1)
	{
		return;
	}

	vector<string> proxies;
	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	string country(countryBox->currentText().toLocal8Bit().constData());
	string city(cityBox->currentText().toLocal8Bit().constData());
	if (stateBox->isVisible())
	{
		string state(stateBox->currentText().toLocal8Bit().constData());
		proxies = cfg->GetProxies(country, state, city);
	}
	else
	{
		proxies = cfg->GetProxies(country, city);
	}

	/* delete existing buttons */
	if (topPanelLayout->count() != 0)
	{
		Logger::Trace("Going to delete existing buttons\n");
		QLayoutItem *child;
		while ((child = topPanelLayout->takeAt(3)) != 0)
		{
			delete child->widget();
		}
	}

	Logger::Trace("Adding new buttons\n");
	for (unsigned i = 0; i < proxies.size(); i++)
	{
		QString btnStr = QString::fromLocal8Bit(proxies[i].c_str());
		QPushButton *btn = new QPushButton(btnStr);
		btn->setCheckable(true);
		genericProxyGroup->addButton(btn);
		topPanelLayout->addWidget(btn, i / 3, i % 3 + 1);
	}
}