2021-04-12 15:32:40 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-12 15:32:40 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "FileOperationProgressWidget.h"
|
2021-06-22 08:56:40 -03:00
|
|
|
#include "FileUtils.h"
|
2021-04-12 15:32:40 -03:00
|
|
|
#include <Applications/FileManager/FileOperationProgressGML.h>
|
2021-04-13 04:58:09 -03:00
|
|
|
#include <LibCore/File.h>
|
2021-04-12 15:32:40 -03:00
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
|
#include <LibGUI/Button.h>
|
2021-04-16 22:39:35 -03:00
|
|
|
#include <LibGUI/ImageWidget.h>
|
2021-04-12 15:32:40 -03:00
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
|
#include <LibGUI/MessageBox.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
#include <LibGUI/Progressbar.h>
|
2021-04-12 15:32:40 -03:00
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
|
|
|
|
|
|
namespace FileManager {
|
|
|
|
|
|
2022-03-12 10:45:03 -03:00
|
|
|
FileOperationProgressWidget::FileOperationProgressWidget(FileOperation operation, NonnullOwnPtr<Core::Stream::BufferedFile> helper_pipe, int helper_pipe_fd)
|
2021-06-22 08:56:40 -03:00
|
|
|
: m_operation(operation)
|
|
|
|
|
, m_helper_pipe(move(helper_pipe))
|
2021-04-12 15:32:40 -03:00
|
|
|
{
|
|
|
|
|
load_from_gml(file_operation_progress_gml);
|
|
|
|
|
|
|
|
|
|
auto& button = *find_descendant_of_type_named<GUI::Button>("button");
|
|
|
|
|
|
2021-04-16 22:39:35 -03:00
|
|
|
auto& file_copy_animation = *find_descendant_of_type_named<GUI::ImageWidget>("file_copy_animation");
|
2022-07-11 14:32:29 -03:00
|
|
|
file_copy_animation.load_from_file("/res/graphics/file-flying-animation.gif"sv);
|
2021-04-16 22:39:35 -03:00
|
|
|
file_copy_animation.animate();
|
|
|
|
|
|
|
|
|
|
auto& source_folder_icon = *find_descendant_of_type_named<GUI::ImageWidget>("source_folder_icon");
|
2022-07-11 14:32:29 -03:00
|
|
|
source_folder_icon.load_from_file("/res/icons/32x32/filetype-folder-open.png"sv);
|
2021-04-16 22:39:35 -03:00
|
|
|
|
|
|
|
|
auto& destination_folder_icon = *find_descendant_of_type_named<GUI::ImageWidget>("destination_folder_icon");
|
2022-02-05 16:19:10 -03:00
|
|
|
|
|
|
|
|
switch (m_operation) {
|
|
|
|
|
case FileOperation::Delete:
|
2022-07-11 14:32:29 -03:00
|
|
|
destination_folder_icon.load_from_file("/res/icons/32x32/recycle-bin.png"sv);
|
2022-02-05 16:19:10 -03:00
|
|
|
break;
|
|
|
|
|
default:
|
2022-07-11 14:32:29 -03:00
|
|
|
destination_folder_icon.load_from_file("/res/icons/32x32/filetype-folder-open.png"sv);
|
2022-02-05 16:19:10 -03:00
|
|
|
break;
|
|
|
|
|
}
|
2021-04-16 22:39:35 -03:00
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 15:34:31 -03:00
|
|
|
button.on_click = [this](auto) {
|
2021-04-12 15:32:40 -03:00
|
|
|
close_pipe();
|
|
|
|
|
window()->close();
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-22 08:56:40 -03:00
|
|
|
auto& files_copied_label = *find_descendant_of_type_named<GUI::Label>("files_copied_label");
|
|
|
|
|
auto& current_file_action_label = *find_descendant_of_type_named<GUI::Label>("current_file_action_label");
|
|
|
|
|
|
|
|
|
|
switch (m_operation) {
|
|
|
|
|
case FileOperation::Copy:
|
|
|
|
|
files_copied_label.set_text("Copying files...");
|
|
|
|
|
current_file_action_label.set_text("Copying: ");
|
|
|
|
|
break;
|
2021-06-22 09:56:53 -03:00
|
|
|
case FileOperation::Move:
|
2021-06-22 08:56:40 -03:00
|
|
|
files_copied_label.set_text("Moving files...");
|
|
|
|
|
current_file_action_label.set_text("Moving: ");
|
|
|
|
|
break;
|
2021-06-22 11:56:55 -03:00
|
|
|
case FileOperation::Delete:
|
|
|
|
|
files_copied_label.set_text("Deleting files...");
|
|
|
|
|
current_file_action_label.set_text("Deleting: ");
|
|
|
|
|
break;
|
2021-06-22 08:56:40 -03:00
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 10:45:03 -03:00
|
|
|
m_notifier = Core::Notifier::construct(helper_pipe_fd, Core::Notifier::Read);
|
2021-04-12 15:32:40 -03:00
|
|
|
m_notifier->on_ready_to_read = [this] {
|
2022-03-12 10:45:03 -03:00
|
|
|
auto line_buffer = ByteBuffer::create_zeroed(1 * KiB).release_value_but_fixme_should_propagate_errors();
|
2022-04-15 10:52:33 -03:00
|
|
|
auto line_or_error = m_helper_pipe->read_line(line_buffer.bytes());
|
|
|
|
|
if (line_or_error.is_error() || line_or_error.value().is_empty()) {
|
2021-06-24 12:28:24 -03:00
|
|
|
did_error("Read from pipe returned null."sv);
|
2021-04-12 15:32:40 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2021-04-14 15:15:28 -03:00
|
|
|
|
2022-04-15 10:52:33 -03:00
|
|
|
auto line = line_or_error.release_value();
|
2022-03-12 10:45:03 -03:00
|
|
|
|
2021-04-13 04:58:09 -03:00
|
|
|
auto parts = line.split_view(' ');
|
2021-04-12 15:32:40 -03:00
|
|
|
VERIFY(!parts.is_empty());
|
|
|
|
|
|
2021-04-14 15:15:28 -03:00
|
|
|
if (parts[0] == "ERROR"sv) {
|
2021-06-24 12:28:24 -03:00
|
|
|
did_error(line.substring_view(6));
|
2021-04-14 15:15:28 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parts[0] == "WARN"sv) {
|
2021-06-24 12:28:24 -03:00
|
|
|
did_error(line.substring_view(5));
|
2021-04-14 15:15:28 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 04:58:09 -03:00
|
|
|
if (parts[0] == "FINISH"sv) {
|
2021-04-12 15:32:40 -03:00
|
|
|
did_finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parts[0] == "PROGRESS"sv) {
|
2021-04-13 05:08:59 -03:00
|
|
|
VERIFY(parts.size() >= 8);
|
2021-04-12 15:32:40 -03:00
|
|
|
did_progress(
|
|
|
|
|
parts[3].to_uint().value_or(0),
|
|
|
|
|
parts[4].to_uint().value_or(0),
|
|
|
|
|
parts[1].to_uint().value_or(0),
|
|
|
|
|
parts[2].to_uint().value_or(0),
|
2021-04-13 05:08:59 -03:00
|
|
|
parts[5].to_uint().value_or(0),
|
|
|
|
|
parts[6].to_uint().value_or(0),
|
|
|
|
|
parts[7]);
|
2021-04-12 15:32:40 -03:00
|
|
|
}
|
|
|
|
|
};
|
2021-04-16 22:41:44 -03:00
|
|
|
|
|
|
|
|
m_elapsed_timer.start();
|
2021-04-12 15:32:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileOperationProgressWidget::~FileOperationProgressWidget()
|
|
|
|
|
{
|
|
|
|
|
close_pipe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileOperationProgressWidget::did_finish()
|
|
|
|
|
{
|
|
|
|
|
close_pipe();
|
|
|
|
|
window()->close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 20:55:02 -03:00
|
|
|
void FileOperationProgressWidget::did_error(StringView message)
|
2021-04-12 15:32:40 -03:00
|
|
|
{
|
|
|
|
|
// FIXME: Communicate more with the user about errors.
|
|
|
|
|
close_pipe();
|
2022-07-11 14:32:29 -03:00
|
|
|
GUI::MessageBox::show(window(), String::formatted("An error occurred: {}", message), "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
|
2021-04-12 15:32:40 -03:00
|
|
|
window()->close();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 22:41:44 -03:00
|
|
|
String FileOperationProgressWidget::estimate_time(off_t bytes_done, off_t total_byte_count)
|
|
|
|
|
{
|
|
|
|
|
int elapsed = m_elapsed_timer.elapsed() / 1000;
|
|
|
|
|
|
|
|
|
|
if (bytes_done == 0 || elapsed < 3)
|
|
|
|
|
return "Estimating...";
|
|
|
|
|
|
|
|
|
|
off_t bytes_left = total_byte_count - bytes_done;
|
|
|
|
|
int seconds_remaining = (bytes_left * elapsed) / bytes_done;
|
|
|
|
|
|
|
|
|
|
if (seconds_remaining < 30)
|
|
|
|
|
return String::formatted("{} seconds", 5 + seconds_remaining - seconds_remaining % 5);
|
|
|
|
|
if (seconds_remaining < 60)
|
|
|
|
|
return "About a minute";
|
|
|
|
|
if (seconds_remaining < 90)
|
|
|
|
|
return "Over a minute";
|
|
|
|
|
if (seconds_remaining < 120)
|
|
|
|
|
return "Less than two minutes";
|
|
|
|
|
|
|
|
|
|
time_t minutes_remaining = seconds_remaining / 60;
|
|
|
|
|
seconds_remaining %= 60;
|
|
|
|
|
|
|
|
|
|
if (minutes_remaining < 60) {
|
|
|
|
|
if (seconds_remaining < 30)
|
|
|
|
|
return String::formatted("About {} minutes", minutes_remaining);
|
|
|
|
|
return String::formatted("Over {} minutes", minutes_remaining);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time_t hours_remaining = minutes_remaining / 60;
|
|
|
|
|
minutes_remaining %= 60;
|
|
|
|
|
|
|
|
|
|
return String::formatted("{} hours and {} minutes", hours_remaining, minutes_remaining);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 20:55:02 -03:00
|
|
|
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)
|
2021-04-12 15:32:40 -03:00
|
|
|
{
|
2021-04-16 22:41:44 -03:00
|
|
|
auto& files_copied_label = *find_descendant_of_type_named<GUI::Label>("files_copied_label");
|
2021-04-12 15:32:40 -03:00
|
|
|
auto& current_file_label = *find_descendant_of_type_named<GUI::Label>("current_file_label");
|
2021-04-13 11:18:20 -03:00
|
|
|
auto& overall_progressbar = *find_descendant_of_type_named<GUI::Progressbar>("overall_progressbar");
|
2021-04-16 22:41:44 -03:00
|
|
|
auto& estimated_time_label = *find_descendant_of_type_named<GUI::Label>("estimated_time_label");
|
2021-04-12 15:32:40 -03:00
|
|
|
|
2021-04-29 16:46:15 -03:00
|
|
|
current_file_label.set_text(current_filename);
|
2021-04-12 15:32:40 -03:00
|
|
|
|
2021-06-22 08:56:40 -03:00
|
|
|
switch (m_operation) {
|
|
|
|
|
case FileOperation::Copy:
|
|
|
|
|
files_copied_label.set_text(String::formatted("Copying file {} of {}", files_done, total_file_count));
|
|
|
|
|
break;
|
2021-06-22 09:56:53 -03:00
|
|
|
case FileOperation::Move:
|
2021-06-22 08:56:40 -03:00
|
|
|
files_copied_label.set_text(String::formatted("Moving file {} of {}", files_done, total_file_count));
|
|
|
|
|
break;
|
2021-06-22 11:56:55 -03:00
|
|
|
case FileOperation::Delete:
|
|
|
|
|
files_copied_label.set_text(String::formatted("Deleting file {} of {}", files_done, total_file_count));
|
|
|
|
|
break;
|
2021-06-22 08:56:40 -03:00
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 22:41:44 -03:00
|
|
|
estimated_time_label.set_text(estimate_time(bytes_done, total_byte_count));
|
|
|
|
|
|
|
|
|
|
if (total_byte_count) {
|
|
|
|
|
window()->set_progress(100.0f * bytes_done / total_byte_count);
|
|
|
|
|
overall_progressbar.set_max(total_byte_count);
|
|
|
|
|
overall_progressbar.set_value(bytes_done);
|
|
|
|
|
}
|
2021-04-12 15:32:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileOperationProgressWidget::close_pipe()
|
|
|
|
|
{
|
|
|
|
|
if (!m_helper_pipe)
|
|
|
|
|
return;
|
|
|
|
|
m_helper_pipe = nullptr;
|
2021-04-14 15:05:23 -03:00
|
|
|
if (m_notifier) {
|
|
|
|
|
m_notifier->set_enabled(false);
|
2021-04-12 15:32:40 -03:00
|
|
|
m_notifier->on_ready_to_read = nullptr;
|
2021-04-14 15:05:23 -03:00
|
|
|
}
|
2021-04-12 15:32:40 -03:00
|
|
|
m_notifier = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|