Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

widget.cpp 4.7KB

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