DownloadWidget.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DownloadWidget.h"
  7. #include <AK/NumberFormat.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibCore/File.h>
  10. #include <LibCore/FileStream.h>
  11. #include <LibCore/StandardPaths.h>
  12. #include <LibDesktop/Launcher.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/Button.h>
  15. #include <LibGUI/ImageWidget.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/MessageBox.h>
  18. #include <LibGUI/Progressbar.h>
  19. #include <LibGUI/Window.h>
  20. #include <LibProtocol/RequestClient.h>
  21. #include <LibWeb/Loader/ResourceLoader.h>
  22. #include <math.h>
  23. namespace Browser {
  24. DownloadWidget::DownloadWidget(const URL& url)
  25. : m_url(url)
  26. {
  27. {
  28. StringBuilder builder;
  29. builder.append(Core::StandardPaths::downloads_directory());
  30. builder.append('/');
  31. builder.append(m_url.basename());
  32. m_destination_path = builder.to_string();
  33. }
  34. m_elapsed_timer.start();
  35. m_download = Web::ResourceLoader::the().protocol_client().start_request("GET", url);
  36. VERIFY(m_download);
  37. m_download->on_progress = [this](Optional<u32> total_size, u32 downloaded_size) {
  38. did_progress(total_size.value(), downloaded_size);
  39. };
  40. {
  41. auto file_or_error = Core::File::open(m_destination_path, Core::OpenMode::WriteOnly);
  42. if (file_or_error.is_error()) {
  43. GUI::MessageBox::show(window(), String::formatted("Cannot open {} for writing", m_destination_path), "Download failed", GUI::MessageBox::Type::Error);
  44. window()->close();
  45. return;
  46. }
  47. m_output_file_stream = make<Core::OutputFileStream>(*file_or_error.value());
  48. }
  49. m_download->on_finish = [this](bool success, auto) { did_finish(success); };
  50. m_download->stream_into(*m_output_file_stream);
  51. set_fill_with_background_color(true);
  52. auto& layout = set_layout<GUI::VerticalBoxLayout>();
  53. layout.set_margins({ 4, 4, 4, 4 });
  54. auto& animation_container = add<GUI::Widget>();
  55. animation_container.set_fixed_height(32);
  56. auto& animation_layout = animation_container.set_layout<GUI::HorizontalBoxLayout>();
  57. auto& browser_image = animation_container.add<GUI::ImageWidget>();
  58. browser_image.load_from_file("/res/graphics/download-animation.gif");
  59. animation_layout.add_spacer();
  60. auto& source_label = add<GUI::Label>(String::formatted("From: {}", url));
  61. source_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  62. source_label.set_fixed_height(16);
  63. m_progressbar = add<GUI::Progressbar>();
  64. m_progressbar->set_fixed_height(20);
  65. m_progress_label = add<GUI::Label>();
  66. m_progress_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  67. m_progress_label->set_fixed_height(16);
  68. auto& destination_label = add<GUI::Label>(String::formatted("To: {}", m_destination_path));
  69. destination_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  70. destination_label.set_fixed_height(16);
  71. auto& button_container = add<GUI::Widget>();
  72. auto& button_container_layout = button_container.set_layout<GUI::HorizontalBoxLayout>();
  73. button_container_layout.add_spacer();
  74. m_cancel_button = button_container.add<GUI::Button>("Cancel");
  75. m_cancel_button->set_fixed_size(100, 22);
  76. m_cancel_button->on_click = [this](auto) {
  77. bool success = m_download->stop();
  78. VERIFY(success);
  79. window()->close();
  80. };
  81. m_close_button = button_container.add<GUI::Button>("OK");
  82. m_close_button->set_enabled(false);
  83. m_close_button->set_fixed_size(100, 22);
  84. m_close_button->on_click = [this](auto) {
  85. window()->close();
  86. };
  87. }
  88. DownloadWidget::~DownloadWidget()
  89. {
  90. }
  91. void DownloadWidget::did_progress(Optional<u32> total_size, u32 downloaded_size)
  92. {
  93. m_progressbar->set_min(0);
  94. if (total_size.has_value()) {
  95. int percent = roundf(((float)downloaded_size / (float)total_size.value()) * 100.0f);
  96. window()->set_progress(percent);
  97. m_progressbar->set_max(total_size.value());
  98. } else {
  99. m_progressbar->set_max(0);
  100. }
  101. m_progressbar->set_value(downloaded_size);
  102. {
  103. StringBuilder builder;
  104. builder.append("Downloaded ");
  105. builder.append(human_readable_size(downloaded_size));
  106. builder.appendff(" in {} sec", m_elapsed_timer.elapsed() / 1000);
  107. m_progress_label->set_text(builder.to_string());
  108. }
  109. {
  110. StringBuilder builder;
  111. if (total_size.has_value()) {
  112. int percent = roundf(((float)downloaded_size / (float)total_size.value()) * 100);
  113. builder.appendff("{}%", percent);
  114. } else {
  115. builder.append(human_readable_size(downloaded_size));
  116. }
  117. builder.append(" of ");
  118. builder.append(m_url.basename());
  119. window()->set_title(builder.to_string());
  120. }
  121. }
  122. void DownloadWidget::did_finish(bool success)
  123. {
  124. dbgln("did_finish, success={}", success);
  125. m_close_button->set_enabled(true);
  126. m_cancel_button->set_text("Open in Folder");
  127. m_cancel_button->on_click = [this](auto) {
  128. Desktop::Launcher::open(URL::create_with_file_protocol(Core::StandardPaths::downloads_directory(), m_url.basename()));
  129. window()->close();
  130. };
  131. m_cancel_button->update();
  132. if (!success) {
  133. GUI::MessageBox::show(window(), String::formatted("Download failed for some reason"), "Download failed", GUI::MessageBox::Type::Error);
  134. window()->close();
  135. return;
  136. }
  137. }
  138. }