summaryrefslogtreecommitdiff
path: root/Userland/Applications/Browser/DownloadWidget.cpp
blob: 05c548721f17796d0a5a8887e82362f6ecdb8e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "DownloadWidget.h"
#include <AK/NumberFormat.h>
#include <AK/StringBuilder.h>
#include <LibCore/Proxy.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/Stream.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Label.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Progressbar.h>
#include <LibGUI/Window.h>
#include <LibWeb/Loader/ResourceLoader.h>

#include <LibConfig/Client.h>

namespace Browser {

DownloadWidget::DownloadWidget(const URL& url)
    : m_url(url)
{
    {
        StringBuilder builder;
        builder.append(Core::StandardPaths::downloads_directory());
        builder.append('/');
        builder.append(m_url.basename());
        m_destination_path = builder.to_deprecated_string();
    }

    auto close_on_finish = Config::read_bool("Browser"sv, "Preferences"sv, "CloseDownloadWidgetOnFinish"sv, false);

    m_elapsed_timer.start();
    m_download = Web::ResourceLoader::the().connector().start_request("GET", url);
    VERIFY(m_download);
    m_download->on_progress = [this](Optional<u32> total_size, u32 downloaded_size) {
        did_progress(total_size.value(), downloaded_size);
    };

    {
        auto file_or_error = Core::Stream::File::open(m_destination_path, Core::Stream::OpenMode::Write);
        if (file_or_error.is_error()) {
            GUI::MessageBox::show(window(), DeprecatedString::formatted("Cannot open {} for writing", m_destination_path), "Download failed"sv, GUI::MessageBox::Type::Error);
            window()->close();
            return;
        }
        m_output_file_stream = file_or_error.release_value();
    }

    m_download->on_finish = [this](bool success, auto) { did_finish(success); };
    m_download->stream_into(*m_output_file_stream);

    set_fill_with_background_color(true);
    auto& layout = set_layout<GUI::VerticalBoxLayout>();
    layout.set_margins(4);

    auto& animation_container = add<GUI::Widget>();
    animation_container.set_fixed_height(32);
    auto& animation_layout = animation_container.set_layout<GUI::HorizontalBoxLayout>();

    m_browser_image = animation_container.add<GUI::ImageWidget>();
    m_browser_image->load_from_file("/res/graphics/download-animation.gif"sv);
    animation_layout.add_spacer();

    auto& source_label = add<GUI::Label>(DeprecatedString::formatted("From: {}", url));
    source_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
    source_label.set_fixed_height(16);

    m_progressbar = add<GUI::Progressbar>();
    m_progressbar->set_fixed_height(20);

    m_progress_label = add<GUI::Label>();
    m_progress_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
    m_progress_label->set_fixed_height(16);

    auto& destination_label = add<GUI::Label>(DeprecatedString::formatted("To: {}", m_destination_path));
    destination_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
    destination_label.set_fixed_height(16);

    m_close_on_finish_checkbox = add<GUI::CheckBox>("Close when finished");
    m_close_on_finish_checkbox->set_checked(close_on_finish);

    m_close_on_finish_checkbox->on_checked = [&](bool checked) {
        Config::write_bool("Browser"sv, "Preferences"sv, "CloseDownloadWidgetOnFinish"sv, checked);
    };

    auto& button_container = add<GUI::Widget>();
    auto& button_container_layout = button_container.set_layout<GUI::HorizontalBoxLayout>();
    button_container_layout.add_spacer();
    m_cancel_button = button_container.add<GUI::Button>("Cancel");
    m_cancel_button->set_fixed_size(100, 22);
    m_cancel_button->on_click = [this](auto) {
        bool success = m_download->stop();
        VERIFY(success);
        window()->close();
    };

    m_close_button = button_container.add<GUI::Button>("OK");
    m_close_button->set_enabled(false);
    m_close_button->set_fixed_size(100, 22);
    m_close_button->on_click = [this](auto) {
        window()->close();
    };
}

void DownloadWidget::did_progress(Optional<u32> total_size, u32 downloaded_size)
{
    m_progressbar->set_min(0);
    if (total_size.has_value()) {
        int percent = roundf(((float)downloaded_size / (float)total_size.value()) * 100.0f);
        window()->set_progress(percent);
        m_progressbar->set_max(total_size.value());
    } else {
        m_progressbar->set_max(0);
    }
    m_progressbar->set_value(downloaded_size);

    {
        StringBuilder builder;
        builder.append("Downloaded "sv);
        builder.append(human_readable_size(downloaded_size));
        builder.appendff(" in {} sec", m_elapsed_timer.elapsed() / 1000);
        m_progress_label->set_text(builder.to_deprecated_string());
    }

    {
        StringBuilder builder;
        if (total_size.has_value()) {
            int percent = roundf(((float)downloaded_size / (float)total_size.value()) * 100);
            builder.appendff("{}%", percent);
        } else {
            builder.append(human_readable_size(downloaded_size));
        }
        builder.append(" of "sv);
        builder.append(m_url.basename());
        window()->set_title(builder.to_deprecated_string());
    }
}

void DownloadWidget::did_finish(bool success)
{
    dbgln("did_finish, success={}", success);

    m_browser_image->load_from_file("/res/graphics/download-finished.gif"sv);
    window()->set_title("Download finished!");
    m_close_button->set_enabled(true);
    m_cancel_button->set_text("Open in Folder");
    m_cancel_button->on_click = [this](auto) {
        Desktop::Launcher::open(URL::create_with_file_scheme(Core::StandardPaths::downloads_directory(), m_url.basename()));
        window()->close();
    };
    m_cancel_button->update();

    if (!success) {
        GUI::MessageBox::show(window(), DeprecatedString::formatted("Download failed for some reason"), "Download failed"sv, GUI::MessageBox::Type::Error);
        window()->close();
        return;
    }

    if (m_close_on_finish_checkbox->is_checked())
        window()->close();
}

}