You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

widget.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QShortcut>
  4. Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
  5. {
  6. ui->setupUi(this);
  7. QShortcut* quit = new QShortcut(QKeySequence("Ctrl+Q"), this);
  8. connect(quit, SIGNAL(activated()), qApp, SLOT(quit()));
  9. /*EpiUser usr;
  10. usr.setLogin("azerty_u");
  11. usr.setPwd_ppp("psswd");
  12. Epimafia::saveUser(usr);
  13. usr.setLogin("");
  14. usr.setPwd_ppp("ppp");
  15. Epimafia::saveUser(usr);
  16. usr.setLogin("login_x");
  17. Epimafia::saveUser(usr);*/
  18. m_currentUser = 0;
  19. foreach(QLineEdit* line, findChildren<QLineEdit*>())
  20. connect(line, SIGNAL(returnPressed()), this, SLOT(search()));
  21. m_intra = new IntraBocal(this);
  22. connect(m_intra, SIGNAL(error(IntraBocal::Error)), this, SLOT(intraError(IntraBocal::Error)));
  23. connect(m_intra, SIGNAL(logged()), this, SLOT(intraLogged()));
  24. connect(m_intra, SIGNAL(searchFinished(QList<QList<QString> >)), this, SLOT(intraSearchFinished(QList<QList<QString> >)));
  25. foreach(City city, IntraBocal::getCities())
  26. ui->comboBox->addItem(city.second, city.first);
  27. loadUsers();
  28. QStringList args = qApp->arguments();
  29. for(int i = 1; i < args.size() - 1; ++i)
  30. {
  31. QString arg = args.at(i);
  32. if(arg == "-firstName")
  33. ui->lineFirstName->setText(args.at(++i));
  34. else if(arg == "-lastName")
  35. ui->lineLastName->setText(args.at(++i));
  36. else if(arg == "-login")
  37. ui->lineLogin->setText(args.at(++i));
  38. else if(arg == "-promo")
  39. ui->linePromo->setText(args.at(++i));
  40. else if(arg == "-school")
  41. ui->lineSchool->setText(args.at(++i));
  42. }
  43. if(!ui->lineFirstName->text().isEmpty() || !ui->lineLastName->text().isEmpty() || !ui->lineLogin->text().isEmpty() || !ui->linePromo->text().isEmpty() || !ui->lineSchool->text().isEmpty())
  44. search();
  45. }
  46. Widget::~Widget()
  47. {
  48. delete ui;
  49. }
  50. void Widget::paintEvent(QPaintEvent *)
  51. {
  52. resizeToMin();
  53. }
  54. void Widget::search(bool badLogin)
  55. {
  56. if(m_users.isEmpty())
  57. {
  58. askStartUsrMgr();
  59. return;
  60. }
  61. if(badLogin)
  62. ui->labelStatus->setText("Bad Loggin. Trying Another...");
  63. else
  64. ui->labelStatus->setText("Logging in...");
  65. EpiUser usr = m_users.at(m_currentUser);
  66. m_intra->login(usr.getLogin(), usr.getPwd_ppp());
  67. clearLayout(ui->gridLayout);
  68. }
  69. void Widget::loadUsers()
  70. {
  71. m_users = Epimafia::loadUsers();
  72. for(int i = 0; i < m_users.size(); ++i)
  73. {
  74. EpiUser usr = m_users.at(i);
  75. if(!usr.hasLogin() || usr.isUnknown() || !usr.hasPwd_ppp())
  76. m_users.removeAt(i--);
  77. }
  78. m_currentUser = 0;
  79. }
  80. void Widget::askStartUsrMgr()
  81. {
  82. QMessageBox* msg = new QMessageBox(this);
  83. msg->setIcon(QMessageBox::Question);
  84. msg->setWindowTitle("No logins found");
  85. msg->setText("No logins with ppp are available.\nDo you want to open User Manager to create one?");
  86. connect(msg->addButton(QMessageBox::Yes), SIGNAL(clicked()), this, SLOT(startUsrMgr()));
  87. connect(msg->addButton(QMessageBox::No), SIGNAL(clicked()), this, SLOT(loadUsers()));
  88. msg->show();
  89. }
  90. void Widget::startUsrMgr()
  91. {
  92. QProcess* p = new QProcess(this);
  93. connect(p, SIGNAL(finished(int)), this, SLOT(processFinished(int)));
  94. QString exe;
  95. #ifdef Q_OS_WIN
  96. exe = "usermgr.exe";
  97. #else
  98. #ifdef Q_OS_LINUX
  99. exe = "usermgr";
  100. #else
  101. #error "OS specific"
  102. #endif
  103. #endif
  104. p->start(exe);
  105. }
  106. void Widget::intraError(IntraBocal::Error e)
  107. {
  108. if(e == IntraBocal::BadLogin)
  109. {
  110. ui->labelStatus->setText("#BadLogin#");
  111. ++m_currentUser;
  112. if(m_currentUser < m_users.size())
  113. search(true);
  114. else
  115. askStartUsrMgr();
  116. }
  117. else if(e == IntraBocal::NetworkError)
  118. ui->labelStatus->setText("#NetworkError#");
  119. else if(e == IntraBocal::ParseError)
  120. ui->labelStatus->setText("#ParseError#");
  121. else if(e == IntraBocal::SslError)
  122. ui->labelStatus->setText("#SslError#");
  123. }
  124. void Widget::intraLogged()
  125. {
  126. ui->labelStatus->setText("Logged in. Searching...");
  127. m_intra->searchUser(ui->lineLastName->text(), ui->lineFirstName->text(), ui->lineLogin->text(), ui->linePromo->text(),
  128. ui->lineSchool->text(), ui->comboBox->itemData(ui->comboBox->currentIndex()).toString());
  129. }
  130. void Widget::intraSearchFinished(QList<QList<QString> > users)
  131. {
  132. if(users.isEmpty())
  133. {
  134. ui->labelStatus->setText("No results");
  135. return;
  136. }
  137. QList<QString> header = users.at(0);
  138. for(int c = 0; c < header.size(); ++c)
  139. {
  140. QLabel* lbl = new QLabel();
  141. QFont f = lbl->font();
  142. f.setBold(true);
  143. lbl->setFont(f);
  144. lbl->setText(header.at(c));
  145. ui->gridLayout->addWidget(lbl, 0, c);
  146. }
  147. for(int i = 1; i < users.size(); ++i)
  148. {
  149. QList<QString> user = users.at(i);
  150. for(int c = 0; c < user.size(); ++c)
  151. {
  152. QLabel* lbl = new QLabel();
  153. lbl->setText(user.at(c));
  154. ui->gridLayout->addWidget(lbl, i, c);
  155. }
  156. }
  157. ui->labelStatus->setText("idle");
  158. }
  159. void Widget::resizeToMin()
  160. {
  161. resize(minimumSizeHint());
  162. setFixedSize(size());
  163. }
  164. void Widget::processFinished(int)
  165. {
  166. sender()->deleteLater();
  167. loadUsers();
  168. search();
  169. }
  170. void Widget::clearLayout(QLayout *layout)
  171. {
  172. QLayoutItem *item;
  173. while((item = layout->takeAt(0))) {
  174. if (item->layout()) {
  175. clearLayout(item->layout());
  176. delete item->layout();
  177. }
  178. if (item->widget()) {
  179. delete item->widget();
  180. }
  181. delete item;
  182. }
  183. }