The Battle for Wesnoth  1.17.0-dev
network_transmission.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
3  by Sergey Popov <loonycyborg@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gettext.hpp"
22 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/label.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "log.hpp"
29 
30 #include <chrono>
31 #include <thread>
32 
33 namespace gui2::dialogs
34 {
35 using namespace std::chrono_literals;
36 
37 REGISTER_DIALOG(network_transmission)
38 
40  : connection_(connection)
41  , window_()
42  , completed_(0)
43  , total_(0)
44  , stop_(false)
45  , poller_(std::async(std::launch::async, [this]() {
46  while(!stop_) {
47  // Check for updates
48  connection_->poll();
49 
50  if(connection_->finished()) {
51  return;
52  }
53 
54  completed_ = connection_->current();
55  total_ = connection_->total();
56 
57  std::this_thread::sleep_for(10ms);
58  }
59  }))
60 {
61 }
62 
64 {
65  if(!window_) {
66  return;
67  }
68 
69  // Check if the thread is complete. If it is, loading is done.
70  if(poller_.wait_for(0ms) == std::future_status::ready) {
71  // The worker returns void, so this is only to handle any exceptions thrown from the worker.
72  // worker_result_.valid() will return false after.
73  if(poller_.valid()) {
74  poller_.get();
75  }
76 
77  window_->set_retval(retval::OK);
78  return;
79  }
80 
81  if(total_) {
82  find_widget<progress_bar>(window_.ptr(), "progress", false)
83  .set_percentage((completed_ * 100.) / total_);
84 
85  std::ostringstream ss;
86  ss
87  << utils::si_string(completed_, true, _("unit_byte^B")) << "/"
88  << utils::si_string(total_, true, _("unit_byte^B"));
89 
90  find_widget<label>(window_.ptr(), "numeric_progress", false)
91  .set_label(ss.str());
92  }
93 }
94 
96  connection_data& connection,
97  const std::string& title,
98  const std::string& subtitle)
99  : connection_(&connection)
100  , pump_monitor_(connection_)
101  , subtitle_(subtitle)
102 {
103  register_label("title", true, title, false);
104  set_restore(true);
105 }
106 
108 {
109  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
110  if(!subtitle_.empty()) {
111  label& subtitle_label = find_widget<label>(&window, "subtitle", false);
112 
113  subtitle_label.set_label(subtitle_);
114  subtitle_label.set_use_markup(true);
115  }
116 
117  // NOTE: needed to avoid explicit calls to invalidate_layout()
118  // in network_transmission::pump_monitor::process()
119  find_widget<label>(&window, "numeric_progress", false).set_label(" ");
120  pump_monitor_.window_ = window;
121 }
122 
124 {
125  pump_monitor_.window_ = std::nullopt;
126 
127  if(get_retval() == retval::CANCEL) {
128  // We need to wait for the current polling loop to conclude before exiting so we don't invalidate
129  // the pointer mid-loop, so signal that here.
130  pump_monitor_.stop_ = true;
131  pump_monitor_.poller_.wait();
132 
133  connection_->cancel();
134  }
135 }
136 
137 } // namespace dialogs
Dialog was closed with the CANCEL button.
Definition: retval.hpp:38
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
This file contains the window object, this object is a top level container which has the event manage...
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
STL namespace.
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:57
static std::string _(const char *str)
Definition: gettext.hpp:93
virtual void process(events::pump_info &) override
virtual void set_label(const t_string &label)
std::string subtitle_
The subtitle for the dialog.
gui2::dialogs::network_transmission::pump_monitor pump_monitor_
This file contains the settings handling of the widget library.
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
virtual void set_use_markup(bool use_markup)
network_transmission(connection_data &connection, const std::string &title, const std::string &subtitle)
A wrapper of either a wesnothd_connection or a network_asio::connection.
field_label * register_label(const std::string &id, const bool mandatory, const std::string &text, const bool use_markup=false)
Registers a new styled_widget as a label.
int get_retval() const
Returns the cached window exit code.
Standard logging facilities (interface).
Dialog that tracks network transmissions.
Dialog was closed with the OK button.
Definition: retval.hpp:35
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
void set_restore(const bool restore)