FileOperationProgressWidget.cpp 6.5 KB

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