summaryrefslogtreecommitdiff
path: root/client/Dialog.cpp
blob: 03b9db75313b9e0fd4b1cda51777261e48c54458 (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

#include <QtGui>
#include <iostream>
#include "client.h"
#include "Config.h"
#include "Dialog.h"
#include "Proxy.h"


using namespace std;

ProxyDialog::ProxyDialog(QWidget *parent): QDialog(parent)
{
	//WIPE it out!!! COnfig shoud be reread by timer event!
	Config *cfg = Config::CurrentConfig();
	cfg->AcquireConfig();
	if (! cfg->IsConfigValid())
	{
		Logger::Fatal("No valid configuration found! Can't proceed.");
		return ;
	}

	topLabel = new QLabel(tr("Top Panel"));
	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::fromUtf8(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)));
	
	/* proxy buttons */
	QPushButton *topProxy1 = new QPushButton("proxy 1");
	QPushButton *topProxy2 = new QPushButton("proxy 2");
	
	QPushButton *bottomProxy1 = new QPushButton("proxy 3");
	QPushButton *bottomProxy2 = new QPushButton("proxy 4");
	
	bottomLabel = new QLabel(tr("Bottom Panel"));
	
	/* setup layouting */
	QVBoxLayout *comboBoxLayout = new QVBoxLayout;
	comboBoxLayout->addWidget(countryBox);
	comboBoxLayout->addWidget(stateBox);
	comboBoxLayout->addWidget(cityBox);
	
	QVBoxLayout *proxyButtonLayout = new QVBoxLayout;
	proxyButtonLayout->addWidget(topProxy1);
	proxyButtonLayout->addWidget(topProxy2);
	
	QHBoxLayout *topPanelLayout = new QHBoxLayout;
	topPanelLayout->addLayout(comboBoxLayout);
	topPanelLayout->addLayout(proxyButtonLayout);
	
	QHBoxLayout *bottomPanelLayout = new QHBoxLayout;
	bottomPanelLayout->addWidget(bottomProxy1);
	bottomPanelLayout->addWidget(bottomProxy2);
	
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(topLabel);
	mainLayout->addLayout(topPanelLayout);
	mainLayout->addWidget(bottomLabel);
	mainLayout->addLayout(bottomPanelLayout);
	
	setLayout(mainLayout);
}

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().toUtf8().constData());
	Logger::Info("Country %s was selected\n", country.c_str());
	
	Config *cfg = Config::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::fromUtf8(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::fromUtf8(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().toUtf8().constData());
	string state(stateBox->currentText().toUtf8().constData());
	Logger::Info("State %s was selected\n", state.c_str());
	
	Config *cfg = Config::CurrentConfig();
	vector<string> cities = cfg->GetCities(country, state);
	cityBox->clear();
	for (unsigned i = 0; i < cities.size(); i++)
	{
		QString city = QString::fromUtf8(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;
	Config *cfg = Config::CurrentConfig();	
	string country(countryBox->currentText().toUtf8().constData());
	string city(cityBox->currentText().toUtf8().constData());
	if (stateBox->isVisible())
	{
		string state(stateBox->currentText().toUtf8().constData());
		proxies = cfg->GetProxies(country, state, city);
	}
	else
	{
		proxies = cfg->GetProxies(country, city);
	}
	
	
}