FileOperationProgressWidget.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "FileOperationProgressWidget.h"
  27. #include <Applications/FileManager/FileOperationProgressGML.h>
  28. #include <LibCore/File.h>
  29. #include <LibCore/Notifier.h>
  30. #include <LibGUI/Button.h>
  31. #include <LibGUI/ImageWidget.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/MessageBox.h>
  34. #include <LibGUI/Progressbar.h>
  35. #include <LibGUI/Window.h>
  36. namespace FileManager {
  37. FileOperationProgressWidget::FileOperationProgressWidget(NonnullRefPtr<Core::File> helper_pipe)
  38. : m_helper_pipe(move(helper_pipe))
  39. {
  40. load_from_gml(file_operation_progress_gml);
  41. auto& button = *find_descendant_of_type_named<GUI::Button>("button");
  42. auto& file_copy_animation = *find_descendant_of_type_named<GUI::ImageWidget>("file_copy_animation");
  43. file_copy_animation.load_from_file("/res/graphics/file-flying-animation.gif");
  44. file_copy_animation.animate();
  45. auto& source_folder_icon = *find_descendant_of_type_named<GUI::ImageWidget>("source_folder_icon");
  46. source_folder_icon.load_from_file("/res/icons/32x32/filetype-folder-open.png");
  47. auto& destination_folder_icon = *find_descendant_of_type_named<GUI::ImageWidget>("destination_folder_icon");
  48. destination_folder_icon.load_from_file("/res/icons/32x32/filetype-folder-open.png");
  49. button.on_click = [this] {
  50. close_pipe();
  51. window()->close();
  52. };
  53. m_notifier = Core::Notifier::construct(m_helper_pipe->fd(), Core::Notifier::Read);
  54. m_notifier->on_ready_to_read = [this] {
  55. auto line = m_helper_pipe->read_line();
  56. if (line.is_null()) {
  57. did_error();
  58. return;
  59. }
  60. auto parts = line.split_view(' ');
  61. VERIFY(!parts.is_empty());
  62. if (parts[0] == "FINISH"sv) {
  63. did_finish();
  64. return;
  65. }
  66. if (parts[0] == "PROGRESS"sv) {
  67. VERIFY(parts.size() >= 8);
  68. did_progress(
  69. parts[3].to_uint().value_or(0),
  70. parts[4].to_uint().value_or(0),
  71. parts[1].to_uint().value_or(0),
  72. parts[2].to_uint().value_or(0),
  73. parts[5].to_uint().value_or(0),
  74. parts[6].to_uint().value_or(0),
  75. parts[7]);
  76. }
  77. };
  78. m_elapsed_timer.start();
  79. }
  80. FileOperationProgressWidget::~FileOperationProgressWidget()
  81. {
  82. close_pipe();
  83. }
  84. void FileOperationProgressWidget::did_finish()
  85. {
  86. close_pipe();
  87. window()->close();
  88. }
  89. void FileOperationProgressWidget::did_error()
  90. {
  91. // FIXME: Communicate more with the user about errors.
  92. close_pipe();
  93. GUI::MessageBox::show(window(), "An error occurred", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  94. window()->close();
  95. }
  96. String FileOperationProgressWidget::estimate_time(off_t bytes_done, off_t total_byte_count)
  97. {
  98. int elapsed = m_elapsed_timer.elapsed() / 1000;
  99. if (bytes_done == 0 || elapsed < 3)
  100. return "Estimating...";
  101. off_t bytes_left = total_byte_count - bytes_done;
  102. int seconds_remaining = (bytes_left * elapsed) / bytes_done;
  103. if (seconds_remaining < 30)
  104. return String::formatted("{} seconds", 5 + seconds_remaining - seconds_remaining % 5);
  105. if (seconds_remaining < 60)
  106. return "About a minute";
  107. if (seconds_remaining < 90)
  108. return "Over a minute";
  109. if (seconds_remaining < 120)
  110. return "Less than two minutes";
  111. time_t minutes_remaining = seconds_remaining / 60;
  112. seconds_remaining %= 60;
  113. if (minutes_remaining < 60) {
  114. if (seconds_remaining < 30)
  115. return String::formatted("About {} minutes", minutes_remaining);
  116. return String::formatted("Over {} minutes", minutes_remaining);
  117. }
  118. time_t hours_remaining = minutes_remaining / 60;
  119. minutes_remaining %= 60;
  120. return String::formatted("{} hours and {} minutes", hours_remaining, minutes_remaining);
  121. }
  122. void FileOperationProgressWidget::did_progress(off_t bytes_done, off_t total_byte_count, size_t files_done, size_t total_file_count, [[maybe_unused]] off_t current_file_done, [[maybe_unused]] off_t current_file_size, const StringView& current_file_name)
  123. {
  124. auto& files_copied_label = *find_descendant_of_type_named<GUI::Label>("files_copied_label");
  125. auto& current_file_label = *find_descendant_of_type_named<GUI::Label>("current_file_label");
  126. auto& overall_progressbar = *find_descendant_of_type_named<GUI::Progressbar>("overall_progressbar");
  127. auto& estimated_time_label = *find_descendant_of_type_named<GUI::Label>("estimated_time_label");
  128. current_file_label.set_text(current_file_name);
  129. files_copied_label.set_text(String::formatted("Copying file {} of {}", files_done, total_file_count));
  130. estimated_time_label.set_text(estimate_time(bytes_done, total_byte_count));
  131. if (total_byte_count) {
  132. window()->set_progress(100.0f * bytes_done / total_byte_count);
  133. overall_progressbar.set_max(total_byte_count);
  134. overall_progressbar.set_value(bytes_done);
  135. }
  136. }
  137. void FileOperationProgressWidget::close_pipe()
  138. {
  139. if (!m_helper_pipe)
  140. return;
  141. m_helper_pipe = nullptr;
  142. if (m_notifier) {
  143. m_notifier->set_enabled(false);
  144. m_notifier->on_ready_to_read = nullptr;
  145. }
  146. m_notifier = nullptr;
  147. }
  148. }