Added default and cancel button to the Dialog
[supertux.git] / src / supertux / menu / download_dialog.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/menu/download_dialog.hpp"
18
19 #include <sstream>
20
21 #include "gui/menu_manager.hpp"
22 #include "util/gettext.hpp"
23
24 DownloadDialog::DownloadDialog(TransferStatusPtr status) :
25   m_status(status),
26   m_title()
27 {
28   add_default_button(_("Abort Download"), [this]{
29       on_abort();
30     });
31
32   update_text();
33
34   status->then(
35     [this](bool success)
36     {
37       if (success)
38       {
39         on_download_complete();
40       }
41       else
42       {
43         std::unique_ptr<Dialog> dialog(new Dialog);
44         dialog->set_text(_("Error:\n") + m_status->error_msg);
45         dialog->add_button(_("Ok"));
46         MenuManager::instance().set_dialog(std::move(dialog));
47       }
48     });
49 }
50
51 void
52 DownloadDialog::update()
53 {
54   m_status->update();
55
56   update_text();
57 }
58
59 void
60 DownloadDialog::set_title(const std::string& title)
61 {
62   m_title = title;
63 }
64
65 void
66 DownloadDialog::update_text()
67 {
68   std::ostringstream out;
69   out << m_title << "\n";
70
71   if (m_status->dltotal == 0)
72   {
73     out << "---\n---";
74   }
75   else
76   {
77     int percent = 100 * m_status->dlnow / m_status->dltotal;
78     out << m_status->dlnow/1000 << "/" << m_status->dltotal/1000 << " kB\n" << percent << "%";
79   }
80
81   set_text(out.str());
82 }
83
84 void
85 DownloadDialog::on_abort()
86 {
87   m_status->abort();
88 }
89
90 void
91 DownloadDialog::on_download_complete()
92 {
93   clear_buttons();
94   add_button(_("Close"), [this]{
95       MenuManager::instance().set_dialog({});
96     });
97 }
98
99 /* EOF */