download_dialog.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SuperTux
  2. // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. // 2022-2023 Vankata453
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "supertux/menu/download_dialog.hpp"
  18. #include <sstream>
  19. #include <fmt/format.h>
  20. #include "addon/downloader.hpp"
  21. DownloadDialog::DownloadDialog(TransferStatusPtr status, bool auto_close,
  22. bool passive, bool no_error_msg) :
  23. DownloadDialog(TransferStatusListPtr(new TransferStatusList({ status })),
  24. auto_close, passive, no_error_msg)
  25. {
  26. }
  27. DownloadDialog::DownloadDialog(TransferStatusListPtr statuses, bool auto_close,
  28. bool passive, bool no_error_msg) :
  29. Dialog(passive),
  30. m_status(statuses),
  31. m_title(),
  32. m_auto_close(auto_close),
  33. m_error_msg(!no_error_msg),
  34. m_download_total(0),
  35. m_complete(false)
  36. {
  37. add_default_button(_("Abort Download"), [this]() {
  38. on_abort();
  39. });
  40. update_text();
  41. m_status->then(
  42. [this](bool success)
  43. {
  44. if (success)
  45. {
  46. on_download_complete();
  47. }
  48. else
  49. {
  50. if (m_error_msg)
  51. {
  52. Dialog::show_message(fmt::format(fmt::runtime(_("Error:\n{}")), m_status->get_error()));
  53. }
  54. else
  55. {
  56. MenuManager::instance().set_dialog({});
  57. }
  58. }
  59. });
  60. }
  61. void
  62. DownloadDialog::update()
  63. {
  64. m_status->update();
  65. update_text();
  66. }
  67. void
  68. DownloadDialog::set_title(const std::string& title)
  69. {
  70. m_title = title;
  71. }
  72. void
  73. DownloadDialog::update_text()
  74. {
  75. std::ostringstream out;
  76. out << m_title << "\n";
  77. int dltotal = m_complete ? m_download_total : m_status->get_download_total();
  78. if (dltotal == 0)
  79. {
  80. out << "---\n---";
  81. }
  82. else
  83. {
  84. int dlnow = m_complete ? m_download_total : m_status->get_download_now();
  85. int percent = 100 * dlnow / dltotal;
  86. out << dlnow / 1000 << "/"
  87. << dltotal / 1000 << " kB\n" << percent << "%";
  88. }
  89. set_text(out.str());
  90. }
  91. void
  92. DownloadDialog::on_abort()
  93. {
  94. m_status->abort();
  95. }
  96. void
  97. DownloadDialog::on_download_complete()
  98. {
  99. if (m_auto_close)
  100. {
  101. MenuManager::instance().set_dialog({});
  102. return;
  103. }
  104. m_download_total = m_status->get_download_total();
  105. m_complete = true;
  106. clear_buttons();
  107. add_button(_("Close"), []() {
  108. MenuManager::instance().set_dialog({});
  109. });
  110. }
  111. /* EOF */