FileOperationProgressWidget.cpp 6.8 KB

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