Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

widget.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #error "OS specific"
  96. #endif
  97. p->start(exe);
  98. }
  99. void Widget::intraError(IntraBocal::Error e)
  100. {
  101. if(e == IntraBocal::BadLogin)
  102. {
  103. ui->labelStatus->setText("#BadLogin#");
  104. ++m_currentUser;
  105. if(m_currentUser < m_users.size())
  106. search(true);
  107. else
  108. askStartUsrMgr();
  109. }
  110. else if(e == IntraBocal::NetworkError)
  111. ui->labelStatus->setText("#NetworkError#");
  112. else if(e == IntraBocal::ParseError)
  113. ui->labelStatus->setText("#ParseError#");
  114. else if(e == IntraBocal::SslError)
  115. ui->labelStatus->setText("#SslError#");
  116. }
  117. void Widget::intraLogged()
  118. {
  119. ui->labelStatus->setText("Logged in. Searching...");
  120. m_intra->searchUser(ui->lineLastName->text(), ui->lineFirstName->text(), ui->lineLogin->text(), ui->linePromo->text(),
  121. ui->lineSchool->text(), ui->comboBox->itemData(ui->comboBox->currentIndex()).toString());
  122. }
  123. void Widget::intraSearchFinished(QList<QList<QString> > users)
  124. {
  125. if(users.isEmpty())
  126. {
  127. ui->labelStatus->setText("No results");
  128. return;
  129. }
  130. QList<QString> header = users.at(0);
  131. for(int c = 0; c < header.size(); ++c)
  132. {
  133. QLabel* lbl = new QLabel();
  134. QFont f = lbl->font();
  135. f.setBold(true);
  136. lbl->setFont(f);
  137. lbl->setText(header.at(c));
  138. ui->gridLayout->addWidget(lbl, 0, c);
  139. }
  140. for(int i = 1; i < users.size(); ++i)
  141. {
  142. QList<QString> user = users.at(i);
  143. for(int c = 0; c < user.size(); ++c)
  144. {
  145. QLabel* lbl = new QLabel();
  146. lbl->setText(user.at(c));
  147. ui->gridLayout->addWidget(lbl, i, c);
  148. }
  149. }
  150. ui->labelStatus->setText("idle");
  151. }
  152. void Widget::resizeToMin()
  153. {
  154. resize(minimumSizeHint());
  155. setFixedSize(size());
  156. }
  157. void Widget::processFinished(int)
  158. {
  159. sender()->deleteLater();
  160. search();
  161. }
  162. void Widget::clearLayout(QLayout *layout)
  163. {
  164. QLayoutItem *item;
  165. while((item = layout->takeAt(0))) {
  166. if (item->layout()) {
  167. clearLayout(item->layout());
  168. delete item->layout();
  169. }
  170. if (item->widget()) {
  171. delete item->widget();
  172. }
  173. delete item;
  174. }
  175. }