#include "widget.h" #include "ui_widget.h" #include Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); QShortcut* quit = new QShortcut(QKeySequence("Ctrl+Q"), this); connect(quit, SIGNAL(activated()), qApp, SLOT(quit())); /*EpiUser usr; usr.setLogin("azerty_u"); usr.setPwd_ppp("psswd"); Epimafia::saveUser(usr); usr.setLogin(""); usr.setPwd_ppp("ppp"); Epimafia::saveUser(usr); usr.setLogin("login_x"); Epimafia::saveUser(usr);*/ m_currentUser = 0; foreach(QLineEdit* line, findChildren()) connect(line, SIGNAL(returnPressed()), this, SLOT(search())); m_intra = new IntraBocal(this); connect(m_intra, SIGNAL(error(IntraBocal::Error)), this, SLOT(intraError(IntraBocal::Error))); connect(m_intra, SIGNAL(logged()), this, SLOT(intraLogged())); connect(m_intra, SIGNAL(searchFinished(QList >)), this, SLOT(intraSearchFinished(QList >))); foreach(City city, IntraBocal::getCities()) ui->comboBox->addItem(city.second, city.first); loadUsers(); QStringList args = qApp->arguments(); for(int i = 1; i < args.size() - 1; ++i) { QString arg = args.at(i); if(arg == "-firstName") ui->lineFirstName->setText(args.at(++i)); else if(arg == "-lastName") ui->lineLastName->setText(args.at(++i)); else if(arg == "-login") ui->lineLogin->setText(args.at(++i)); else if(arg == "-promo") ui->linePromo->setText(args.at(++i)); else if(arg == "-school") ui->lineSchool->setText(args.at(++i)); } if(!ui->lineFirstName->text().isEmpty() || !ui->lineLastName->text().isEmpty() || !ui->lineLogin->text().isEmpty() || !ui->linePromo->text().isEmpty() || !ui->lineSchool->text().isEmpty()) search(); } Widget::~Widget() { delete ui; } void Widget::paintEvent(QPaintEvent *) { resizeToMin(); } void Widget::search(bool badLogin) { if(m_users.isEmpty()) { askStartUsrMgr(); return; } if(badLogin) ui->labelStatus->setText("Bad Loggin. Trying Another..."); else ui->labelStatus->setText("Logging in..."); EpiUser usr = m_users.at(m_currentUser); m_intra->login(usr.getLogin(), usr.getPwd_ppp()); clearLayout(ui->gridLayout); } void Widget::loadUsers() { m_users = Epimafia::loadUsers(); for(int i = 0; i < m_users.size(); ++i) { EpiUser usr = m_users.at(i); if(!usr.hasLogin() || usr.isUnknown() || !usr.hasPwd_ppp()) m_users.removeAt(i--); } m_currentUser = 0; } void Widget::askStartUsrMgr() { QMessageBox* msg = new QMessageBox(this); msg->setIcon(QMessageBox::Question); msg->setWindowTitle("No logins found"); msg->setText("No logins with ppp are available.\nDo you want to open User Manager to create one?"); connect(msg->addButton(QMessageBox::Yes), SIGNAL(clicked()), this, SLOT(startUsrMgr())); connect(msg->addButton(QMessageBox::No), SIGNAL(clicked()), this, SLOT(loadUsers())); msg->show(); } void Widget::startUsrMgr() { QProcess* p = new QProcess(this); connect(p, SIGNAL(finished(int)), this, SLOT(processFinished(int))); QString exe; #ifdef Q_OS_WIN exe = "usermgr.exe"; #else #ifdef Q_OS_LINUX exe = "usermgr"; #else #error "OS specific" #endif #endif p->start(exe); } void Widget::intraError(IntraBocal::Error e) { if(e == IntraBocal::BadLogin) { ui->labelStatus->setText("#BadLogin#"); ++m_currentUser; if(m_currentUser < m_users.size()) search(true); else askStartUsrMgr(); } else if(e == IntraBocal::NetworkError) ui->labelStatus->setText("#NetworkError#"); else if(e == IntraBocal::ParseError) ui->labelStatus->setText("#ParseError#"); else if(e == IntraBocal::SslError) ui->labelStatus->setText("#SslError#"); } void Widget::intraLogged() { ui->labelStatus->setText("Logged in. Searching..."); m_intra->searchUser(ui->lineLastName->text(), ui->lineFirstName->text(), ui->lineLogin->text(), ui->linePromo->text(), ui->lineSchool->text(), ui->comboBox->itemData(ui->comboBox->currentIndex()).toString()); } void Widget::intraSearchFinished(QList > users) { if(users.isEmpty()) { ui->labelStatus->setText("No results"); return; } QList header = users.at(0); for(int c = 0; c < header.size(); ++c) { QLabel* lbl = new QLabel(); QFont f = lbl->font(); f.setBold(true); lbl->setFont(f); lbl->setText(header.at(c)); ui->gridLayout->addWidget(lbl, 0, c); } for(int i = 1; i < users.size(); ++i) { QList user = users.at(i); for(int c = 0; c < user.size(); ++c) { QLabel* lbl = new QLabel(); lbl->setText(user.at(c)); ui->gridLayout->addWidget(lbl, i, c); } } ui->labelStatus->setText("idle"); } void Widget::resizeToMin() { resize(minimumSizeHint()); setFixedSize(size()); } void Widget::processFinished(int) { sender()->deleteLater(); search(); } void Widget::clearLayout(QLayout *layout) { QLayoutItem *item; while((item = layout->takeAt(0))) { if (item->layout()) { clearLayout(item->layout()); delete item->layout(); } if (item->widget()) { delete item->widget(); } delete item; } }