FileOperationProgressWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/Label.h>
  32. #include <LibGUI/MessageBox.h>
  33. #include <LibGUI/Progressbar.h>
  34. #include <LibGUI/Window.h>
  35. namespace FileManager {
  36. FileOperationProgressWidget::FileOperationProgressWidget(NonnullRefPtr<Core::File> helper_pipe)
  37. : m_helper_pipe(move(helper_pipe))
  38. {
  39. load_from_gml(file_operation_progress_gml);
  40. auto& button = *find_descendant_of_type_named<GUI::Button>("button");
  41. button.on_click = [this] {
  42. close_pipe();
  43. window()->close();
  44. };
  45. m_notifier = Core::Notifier::construct(m_helper_pipe->fd(), Core::Notifier::Read);
  46. m_notifier->on_ready_to_read = [this] {
  47. auto line = m_helper_pipe->read_line();
  48. if (line.is_null()) {
  49. did_error();
  50. return;
  51. }
  52. auto parts = line.split_view(' ');
  53. VERIFY(!parts.is_empty());
  54. if (parts[0] == "FINISH"sv) {
  55. did_finish();
  56. return;
  57. }
  58. if (parts[0] == "PROGRESS"sv) {
  59. VERIFY(parts.size() >= 8);
  60. did_progress(
  61. parts[3].to_uint().value_or(0),
  62. parts[4].to_uint().value_or(0),
  63. parts[1].to_uint().value_or(0),
  64. parts[2].to_uint().value_or(0),
  65. parts[5].to_uint().value_or(0),
  66. parts[6].to_uint().value_or(0),
  67. parts[7]);
  68. }
  69. };
  70. }
  71. FileOperationProgressWidget::~FileOperationProgressWidget()
  72. {
  73. close_pipe();
  74. }
  75. void FileOperationProgressWidget::did_finish()
  76. {
  77. close_pipe();
  78. window()->close();
  79. }
  80. void FileOperationProgressWidget::did_error()
  81. {
  82. // FIXME: Communicate more with the user about errors.
  83. close_pipe();
  84. GUI::MessageBox::show(window(), "An error occurred", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  85. window()->close();
  86. }
  87. void FileOperationProgressWidget::did_progress([[maybe_unused]] off_t bytes_done, [[maybe_unused]] off_t total_byte_count, size_t files_done, size_t total_file_count, off_t current_file_done, off_t current_file_size, const StringView& current_file_name)
  88. {
  89. auto& current_file_label = *find_descendant_of_type_named<GUI::Label>("current_file_label");
  90. auto& current_file_progressbar = *find_descendant_of_type_named<GUI::Progressbar>("current_file_progressbar");
  91. auto& overall_progress_label = *find_descendant_of_type_named<GUI::Label>("overall_progress_label");
  92. auto& overall_progressbar = *find_descendant_of_type_named<GUI::Progressbar>("overall_progressbar");
  93. current_file_label.set_text(current_file_name);
  94. current_file_progressbar.set_max(current_file_size);
  95. current_file_progressbar.set_value(current_file_done);
  96. overall_progress_label.set_text(String::formatted("{} of {}", files_done, total_file_count));
  97. overall_progressbar.set_max(total_file_count);
  98. overall_progressbar.set_value(files_done);
  99. }
  100. void FileOperationProgressWidget::close_pipe()
  101. {
  102. if (!m_helper_pipe)
  103. return;
  104. m_helper_pipe = nullptr;
  105. if (m_notifier)
  106. m_notifier->on_ready_to_read = nullptr;
  107. m_notifier = nullptr;
  108. }
  109. }