FileOperationProgressWidget.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "FileOperationProgressWidget.h"
  8. #include "FileUtils.h"
  9. #include <Applications/FileManager/FileOperationProgressGML.h>
  10. #include <LibCore/Notifier.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/ImageWidget.h>
  13. #include <LibGUI/Label.h>
  14. #include <LibGUI/MessageBox.h>
  15. #include <LibGUI/Progressbar.h>
  16. #include <LibGUI/Window.h>
  17. namespace FileManager {
  18. FileOperationProgressWidget::FileOperationProgressWidget(FileOperation operation, NonnullOwnPtr<Core::BufferedFile> helper_pipe, int helper_pipe_fd)
  19. : m_operation(operation)
  20. , m_helper_pipe(move(helper_pipe))
  21. {
  22. load_from_gml(file_operation_progress_gml).release_value_but_fixme_should_propagate_errors();
  23. auto& button = *find_descendant_of_type_named<GUI::Button>("button");
  24. auto& file_copy_animation = *find_descendant_of_type_named<GUI::ImageWidget>("file_copy_animation");
  25. file_copy_animation.load_from_file("/res/graphics/file-flying-animation.gif"sv);
  26. file_copy_animation.animate();
  27. auto& source_folder_icon = *find_descendant_of_type_named<GUI::ImageWidget>("source_folder_icon");
  28. source_folder_icon.load_from_file("/res/icons/32x32/filetype-folder-open.png"sv);
  29. auto& destination_folder_icon = *find_descendant_of_type_named<GUI::ImageWidget>("destination_folder_icon");
  30. switch (m_operation) {
  31. case FileOperation::Delete:
  32. destination_folder_icon.load_from_file("/res/icons/32x32/recycle-bin.png"sv);
  33. break;
  34. default:
  35. destination_folder_icon.load_from_file("/res/icons/32x32/filetype-folder-open.png"sv);
  36. break;
  37. }
  38. button.on_click = [this](auto) {
  39. close_pipe();
  40. window()->close();
  41. };
  42. auto& files_copied_label = *find_descendant_of_type_named<GUI::Label>("files_copied_label");
  43. auto& current_file_action_label = *find_descendant_of_type_named<GUI::Label>("current_file_action_label");
  44. switch (m_operation) {
  45. case FileOperation::Copy:
  46. files_copied_label.set_text("Copying files..."_string.release_value_but_fixme_should_propagate_errors());
  47. current_file_action_label.set_text("Copying: "_string.release_value_but_fixme_should_propagate_errors());
  48. break;
  49. case FileOperation::Move:
  50. files_copied_label.set_text("Moving files..."_string.release_value_but_fixme_should_propagate_errors());
  51. current_file_action_label.set_text("Moving: "_string.release_value_but_fixme_should_propagate_errors());
  52. break;
  53. case FileOperation::Delete:
  54. files_copied_label.set_text("Deleting files..."_string.release_value_but_fixme_should_propagate_errors());
  55. current_file_action_label.set_text("Deleting: "_string.release_value_but_fixme_should_propagate_errors());
  56. break;
  57. default:
  58. VERIFY_NOT_REACHED();
  59. }
  60. m_notifier = Core::Notifier::construct(helper_pipe_fd, Core::Notifier::Type::Read);
  61. m_notifier->on_activation = [this] {
  62. auto line_buffer_or_error = ByteBuffer::create_zeroed(1 * KiB);
  63. if (line_buffer_or_error.is_error()) {
  64. did_error("Failed to allocate ByteBuffer for reading data."sv);
  65. return;
  66. }
  67. auto line_buffer = line_buffer_or_error.release_value();
  68. auto line_or_error = m_helper_pipe->read_line(line_buffer.bytes());
  69. if (line_or_error.is_error() || line_or_error.value().is_empty()) {
  70. did_error("Read from pipe returned null."sv);
  71. return;
  72. }
  73. auto line = line_or_error.release_value();
  74. auto parts = line.split_view(' ');
  75. VERIFY(!parts.is_empty());
  76. if (parts[0] == "ERROR"sv) {
  77. did_error(line.substring_view(6));
  78. return;
  79. }
  80. if (parts[0] == "WARN"sv) {
  81. did_error(line.substring_view(5));
  82. return;
  83. }
  84. if (parts[0] == "FINISH"sv) {
  85. did_finish();
  86. return;
  87. }
  88. if (parts[0] == "PROGRESS"sv) {
  89. VERIFY(parts.size() >= 8);
  90. did_progress(
  91. parts[3].to_uint().value_or(0),
  92. parts[4].to_uint().value_or(0),
  93. parts[1].to_uint().value_or(0),
  94. parts[2].to_uint().value_or(0),
  95. parts[5].to_uint().value_or(0),
  96. parts[6].to_uint().value_or(0),
  97. parts[7]);
  98. }
  99. };
  100. m_elapsed_timer.start();
  101. }
  102. FileOperationProgressWidget::~FileOperationProgressWidget()
  103. {
  104. close_pipe();
  105. }
  106. void FileOperationProgressWidget::did_finish()
  107. {
  108. close_pipe();
  109. window()->close();
  110. }
  111. void FileOperationProgressWidget::did_error(StringView message)
  112. {
  113. // FIXME: Communicate more with the user about errors.
  114. close_pipe();
  115. GUI::MessageBox::show(window(), DeprecatedString::formatted("An error occurred: {}", message), "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  116. window()->close();
  117. }
  118. DeprecatedString FileOperationProgressWidget::estimate_time(off_t bytes_done, off_t total_byte_count)
  119. {
  120. i64 const elapsed_seconds = m_elapsed_timer.elapsed_time().to_seconds();
  121. if (bytes_done == 0 || elapsed_seconds < 3)
  122. return "Estimating...";
  123. off_t bytes_left = total_byte_count - bytes_done;
  124. int seconds_remaining = (bytes_left * elapsed_seconds) / bytes_done;
  125. if (seconds_remaining < 30)
  126. return DeprecatedString::formatted("{} seconds", 5 + seconds_remaining - seconds_remaining % 5);
  127. if (seconds_remaining < 60)
  128. return "About a minute";
  129. if (seconds_remaining < 90)
  130. return "Over a minute";
  131. if (seconds_remaining < 120)
  132. return "Less than two minutes";
  133. time_t minutes_remaining = seconds_remaining / 60;
  134. seconds_remaining %= 60;
  135. if (minutes_remaining < 60) {
  136. if (seconds_remaining < 30)
  137. return DeprecatedString::formatted("About {} minutes", minutes_remaining);
  138. return DeprecatedString::formatted("Over {} minutes", minutes_remaining);
  139. }
  140. time_t hours_remaining = minutes_remaining / 60;
  141. minutes_remaining %= 60;
  142. return DeprecatedString::formatted("{} hours and {} minutes", hours_remaining, minutes_remaining);
  143. }
  144. 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, StringView current_filename)
  145. {
  146. auto& files_copied_label = *find_descendant_of_type_named<GUI::Label>("files_copied_label");
  147. auto& current_file_label = *find_descendant_of_type_named<GUI::Label>("current_file_label");
  148. auto& overall_progressbar = *find_descendant_of_type_named<GUI::Progressbar>("overall_progressbar");
  149. auto& estimated_time_label = *find_descendant_of_type_named<GUI::Label>("estimated_time_label");
  150. current_file_label.set_text(String::from_utf8(current_filename).release_value_but_fixme_should_propagate_errors());
  151. switch (m_operation) {
  152. case FileOperation::Copy:
  153. files_copied_label.set_text(String::formatted("Copying file {} of {}", files_done, total_file_count).release_value_but_fixme_should_propagate_errors());
  154. break;
  155. case FileOperation::Move:
  156. files_copied_label.set_text(String::formatted("Moving file {} of {}", files_done, total_file_count).release_value_but_fixme_should_propagate_errors());
  157. break;
  158. case FileOperation::Delete:
  159. files_copied_label.set_text(String::formatted("Deleting file {} of {}", files_done, total_file_count).release_value_but_fixme_should_propagate_errors());
  160. break;
  161. default:
  162. VERIFY_NOT_REACHED();
  163. }
  164. estimated_time_label.set_text(String::from_deprecated_string(estimate_time(bytes_done, total_byte_count)).release_value_but_fixme_should_propagate_errors());
  165. if (total_byte_count) {
  166. window()->set_progress(100.0f * bytes_done / total_byte_count);
  167. overall_progressbar.set_max(total_byte_count);
  168. overall_progressbar.set_value(bytes_done);
  169. }
  170. }
  171. void FileOperationProgressWidget::close_pipe()
  172. {
  173. if (!m_helper_pipe)
  174. return;
  175. m_helper_pipe = nullptr;
  176. if (m_notifier) {
  177. m_notifier->set_enabled(false);
  178. m_notifier->on_activation = nullptr;
  179. }
  180. m_notifier = nullptr;
  181. }
  182. }