FileOperationProgressWidget.cpp 7.6 KB

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