Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

widget.cpp 5.4KB

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