FileOperationProgressWidget.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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() >= 6);
  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]);
  66. }
  67. };
  68. }
  69. FileOperationProgressWidget::~FileOperationProgressWidget()
  70. {
  71. close_pipe();
  72. }
  73. void FileOperationProgressWidget::did_finish()
  74. {
  75. close_pipe();
  76. window()->close();
  77. }
  78. void FileOperationProgressWidget::did_error()
  79. {
  80. // FIXME: Communicate more with the user about errors.
  81. close_pipe();
  82. GUI::MessageBox::show(window(), "An error occurred", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
  83. window()->close();
  84. }
  85. void FileOperationProgressWidget::did_progress(off_t bytes_done, off_t total_byte_count, size_t files_done, size_t total_file_count, const StringView& current_file_name)
  86. {
  87. auto& current_file_label = *find_descendant_of_type_named<GUI::Label>("current_file_label");
  88. auto& current_file_progress_bar = *find_descendant_of_type_named<GUI::ProgressBar>("current_file_progress_bar");
  89. auto& overall_progress_label = *find_descendant_of_type_named<GUI::Label>("overall_progress_label");
  90. auto& overall_progress_bar = *find_descendant_of_type_named<GUI::ProgressBar>("overall_progress_bar");
  91. current_file_label.set_text(current_file_name);
  92. current_file_progress_bar.set_max(total_byte_count);
  93. current_file_progress_bar.set_value(bytes_done);
  94. overall_progress_label.set_text(String::formatted("{} of {}", files_done, total_file_count));
  95. overall_progress_bar.set_max(total_file_count);
  96. overall_progress_bar.set_value(files_done);
  97. }
  98. void FileOperationProgressWidget::close_pipe()
  99. {
  100. if (!m_helper_pipe)
  101. return;
  102. m_helper_pipe = nullptr;
  103. if (m_notifier)
  104. m_notifier->on_ready_to_read = nullptr;
  105. m_notifier = nullptr;
  106. }
  107. }