serverstatuswidget.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "serverstatuswidget.h"
  2. #include "ui_serverstatuswidget.h"
  3. #include <QDebug>
  4. #include "constants.h"
  5. ServerStatusWidget::ServerStatusWidget(QWidget *parent) :
  6. QWidget(parent),
  7. ui(new Ui::ServerStatusWidget)
  8. {
  9. servers_disabled = false;
  10. setMouseTracking(true);
  11. ui->setupUi(this);
  12. connect(&update_timer, &QTimer::timeout, &status_downloader, &Downloader::start);
  13. connect(&status_downloader, &Downloader::downloadFinished, this, &ServerStatusWidget::updateStatus);
  14. // updateStatus will start tech_work_message_downloader
  15. connect(&tech_work_message_downloader, &Downloader::downloadFinished, this, &ServerStatusWidget::updateTechWorksMessage);
  16. }
  17. ServerStatusWidget::~ServerStatusWidget()
  18. {
  19. delete ui;
  20. }
  21. void ServerStatusWidget::setupUpdater(QUrl status_url, QUrl tech_works_message_url)
  22. {
  23. status_downloader.targetBytearray = &status_data;
  24. status_downloader.setUrl(status_url);
  25. tech_work_message_downloader.targetBytearray = &tech_work_message_data;
  26. tech_work_message_downloader.setUrl(tech_works_message_url);
  27. update_timer.setInterval(1000 * 60); // 1 minute;
  28. update_timer.start();
  29. // Perform update on startup
  30. status_downloader.start();
  31. }
  32. void ServerStatusWidget::updateFontsSizes()
  33. {
  34. foreach (QString server_name, servers_list) {
  35. QLabel* label = ui->servers_list->findChild<QLabel*>(server_name.toLower());
  36. if (!label) {
  37. qDebug() << "Error! Cannot find server widget " << server_name;
  38. continue;
  39. }
  40. label->setFont(trajan_9pt);
  41. }
  42. }
  43. void ServerStatusWidget::resizeEvent(QResizeEvent *event)
  44. {
  45. updateFontsSizes();
  46. }
  47. void ServerStatusWidget::leaveEvent(QEvent *event)
  48. {
  49. event->ignore();
  50. emit showNoTooltip();
  51. }
  52. void ServerStatusWidget::enterEvent(QEvent *event)
  53. {
  54. event->ignore();
  55. emit showServersTooltip();
  56. }
  57. void ServerStatusWidget::updateStatus()
  58. {
  59. if (QString(status_data).contains("profilaktika")) {
  60. servers_disabled = true;
  61. qDebug() << "All servers are closed!";
  62. foreach (QString server_name, servers_list) {
  63. QLabel* label = ui->servers_list->findChild<QLabel*>(server_name.toLower());
  64. if (!label) {
  65. qDebug() << "Error! Cannot find server widget " << server_name;
  66. continue;
  67. }
  68. label->setStyleSheet(closed_color);
  69. }
  70. status_data = "";
  71. tech_work_message_downloader.start();
  72. return;
  73. }
  74. servers_disabled = false;
  75. QStringList servers = QString(status_data).split("|||");
  76. foreach (QString server, servers) {
  77. QStringList data = server.split(":::");
  78. QWidget* widget = findChild<QWidget*>(QString(data[0]).toLower());
  79. if (!widget)
  80. continue;
  81. if (data.size() > 1 && data[1] == "on")
  82. widget->setStyleSheet(open_color);
  83. else
  84. widget->setStyleSheet(closed_color);
  85. }
  86. status_data = "";
  87. tech_work_message_downloader.start();
  88. }
  89. void ServerStatusWidget::updateTechWorksMessage()
  90. {
  91. if (tech_work_message_data.isEmpty()) {
  92. QString result;
  93. if (!servers_disabled) {
  94. result = "ИГРОВЫЕ СЕРВЕРЫ LOTRO РАБОТАЮТ В ШТАТНОМ РЕЖИМЕ";
  95. } else {
  96. result = "<p style =\"color:#ff0000\">СЕРВЕРЫ ИГРЫ ОТКЛЮЧЕНЫ</p>";
  97. }
  98. emit updateServersTooltip(result);
  99. tech_work_message_data = "";
  100. return;
  101. }
  102. QStringList list = QString(tech_work_message_data).split(":::");
  103. if (list.size() < 4) {
  104. tech_work_message_data = "";
  105. return;
  106. }
  107. QString time_beginning = "<span style =\"font-family:Trajan Pro 3\">" + list[0] + "</span>";
  108. QString time_end = "<span style =\"font-family:Trajan Pro 3\">" + list[1] + "</span>";
  109. QString day = "<span style =\"font-family:Trajan Pro 3\">" + list[2] + "</span>";
  110. QString month = QString(" " + list[3]).toUpper();
  111. QString result;
  112. if (!servers_disabled) {
  113. result = "<p style=\"color:#ffff7f\">ПЛАНИРУЕТСЯ ОТКЛЮЧЕНИЕ СЕРВЕРОВ ИГРЫ " + day + month + " С " + time_beginning + " ДО " + time_end+ " (МСК).</p>";
  114. } else {
  115. result = "<p style=\"color:#ff0000\">СЕРВЕРЫ ИГРЫ ОТКЛЮЧЕНЫ<br>ВРЕМЯ ПРОФ.РАБОТ: " + day + month + " С " + time_beginning + " ДО " + time_end + " (МСК).</p>";
  116. }
  117. tech_work_message_data = "";
  118. emit updateServersTooltip(result);
  119. }