The Battle for Wesnoth  1.15.1+dev
network_transmission.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 Sergey Popov <loonycyborg@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "gettext.hpp"
21 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "log.hpp"
28 
29 #include <chrono>
30 
31 namespace gui2
32 {
33 namespace 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  window_.get().set_retval(retval::OK);
72  return;
73  }
74 
75  if(total_) {
76  find_widget<progress_bar>(&(window_.get()), "progress", false)
77  .set_percentage((completed_ * 100.) / total_);
78 
79  std::ostringstream ss;
80  ss
81  << utils::si_string(completed_, true, _("unit_byte^B")) << "/"
82  << utils::si_string(total_, true, _("unit_byte^B"));
83 
84  find_widget<label>(&(window_.get()), "numeric_progress", false)
85  .set_label(ss.str());
86 
87  window_->invalidate_layout();
88  }
89 }
90 
92  connection_data& connection,
93  const std::string& title,
94  const std::string& subtitle)
95  : connection_(&connection)
96  , pump_monitor_(connection_)
97  , subtitle_(subtitle)
98 {
99  register_label("title", true, title, false);
100  set_restore(true);
101 }
102 
104 {
105  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
106  if(!subtitle_.empty()) {
107  label& subtitle_label = find_widget<label>(&window, "subtitle", false);
108 
109  subtitle_label.set_label(subtitle_);
110  subtitle_label.set_use_markup(true);
111  }
112 
113  pump_monitor_.window_ = window;
114 }
115 
117 {
118  pump_monitor_.window_.reset();
119 
120  if(get_retval() == retval::CANCEL) {
121  // We need to wait for the current polling loop to conclude before exiting so we don't invalidate
122  // the pointer mid-loop, so signal that here.
123  pump_monitor_.stop_ = true;
124  pump_monitor_.poller_.wait();
125 
126  connection_->cancel();
127  }
128 }
129 
130 } // namespace dialogs
131 } // namespace gui2
Dialog was closed with the CANCEL button.
Definition: retval.hpp:37
virtual void post_show(window &window) override
Inherited from modal_dialog.
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
Inherited from modal_dialog.
STL namespace.
Label showing a text.
Definition: label.hpp:32
virtual void process(events::pump_info &) override
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void set_label(const t_string &label)
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
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)
Various uncategorised dialogs.
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.
Standard logging facilities (interface).
Dialog that tracks network transmissions.
Dialog was closed with the OK button.
Definition: retval.hpp:34
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
void set_restore(const bool restore)