#include #include #include #include #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 ; } /* generic proxy panel */ topLabel = new QLabel("Top Panel"); topLabel->setAlignment(Qt::AlignHCenter); countryBox = new QComboBox; stateBox = new QComboBox; stateBox->setVisible(false); cityBox = new QComboBox; cityBox->setVisible(false); vector 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))); topPanelLayout = new QGridLayout; topPanelLayout->setSpacing(5); topPanelLayout->addWidget(countryBox, 0, 0); topPanelLayout->addWidget(stateBox, 1, 0); topPanelLayout->addWidget(cityBox, 2, 0); genericProxyGroup = new QButtonGroup; /* static proxy panel */ bottomLabel = new QLabel("Bottom Panel"); bottomLabel->setAlignment(Qt::AlignHCenter); QGridLayout *bottomPanelLayout = new QGridLayout; unsigned nlines = cfg->GetStaticProxyGuiLines(); for (unsigned i = 0; i < nlines; i++) { vector staticProxyLine = cfg->GetStaticProxyGuiLine(i+1); for (unsigned j = 0; j < staticProxyLine.size(); j++) { stringstream ss; if (staticProxyLine[j].speed > cfg->StaticProxySpeedLow) { ss << "" << staticProxyLine[j].speed << ""; } else { ss << "" << staticProxyLine[j].speed << ""; } QLabel *speedLabel = new QLabel(ss.str().c_str()); QString btnStr = QString::fromUtf8(staticProxyLine[j]. name.c_str()); QLabel *btnLabel = new QLabel(btnStr); QHBoxLayout *btnLayout = new QHBoxLayout; btnLayout->setSizeConstraint(QLayout::SetMinimumSize); btnLayout->addWidget(speedLabel); btnLayout->addWidget(btnLabel); QPushButton *btn = new QPushButton(); btn->setLayout(btnLayout); btn->setCheckable(true); 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::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 states = cfg->GetStates(country); if (states.empty()) { Logger::Info("There are no states in %s\n",country.c_str()); vector 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 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 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); } /* 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::fromUtf8(proxies[i].c_str()); QPushButton *btn = new QPushButton(btnStr); btn->setCheckable(true); genericProxyGroup->addButton(btn); topPanelLayout->addWidget(btn, i / 3, i % 3 + 1); } }