123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #include "widget.h"
- #include "ui_widget.h"
- #include <QShortcut>
-
- 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<QLineEdit*>())
- 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<QList<QString> >)), this, SLOT(intraSearchFinished(QList<QList<QString> >)));
- 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<QList<QString> > users)
- {
- if(users.isEmpty())
- {
- ui->labelStatus->setText("No results");
- return;
- }
- QList<QString> 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<QString> 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();
- loadUsers();
- 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;
- }
- }
|