Debian Codegraph @

src/dialogs/ProgressDialog.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
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
18 public class DesktopFolder.ProgressDialog : Gtk.Dialog {
19 private Gtk.Widget status;
20 private Gtk.Widget progress_bar;
21 private Gtk.Box window_vbox;
22 private uint timeout_id = 0;
23 private GLib.Cancellable cancellable;
24
25 /**
26 * @constructor
27 * @param FolderManager manager the manager of this window
28 */
29 public ProgressDialog (string title, Gtk.Window ? parent) {
30 this.resizable = false;
31 this.deletable = false;
32 this.title = title;
33
34 this.icon_name = "system-file-manager";
35 this.window_vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
36 this.get_content_area ().set_border_width (10);
37 this.get_content_area ().add (window_vbox);
38 this.window_vbox.show ();
39
40 this.status = new Gtk.Label (title);
41 (this.status as Gtk.Label).set_size_request (500, -1);
42 (this.status as Gtk.Label).set_max_width_chars (50);
43 (this.status as Gtk.Label).set_ellipsize (Pango.EllipsizeMode.MIDDLE);
44 (this.status as Gtk.Label).set_line_wrap (true);
45 (this.status as Gtk.Label).set_line_wrap_mode (Pango.WrapMode.WORD_CHAR);
46 (this.status as Gtk.Misc).set_alignment ((float) 0.0, (float) 0.5);
47 window_vbox.pack_start (status, true, false, 0);
48
49 var window_hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 2);
50 this.progress_bar = new Gtk.ProgressBar ();
51 window_hbox.pack_start (this.progress_bar, true, true, 0);
52 var button = new Gtk.Button.from_icon_name ("process-stop-symbolic", Gtk.IconSize.BUTTON);
53 button.get_style_context ().add_class ("flat");
54 button.clicked.connect (() => {
55 button.sensitive = false;
56 this.cancellable.cancel ();
57 this.stop ();
58 });
59 window_hbox.pack_start (button, false, false, 0);
60 this.window_vbox.pack_start (window_hbox);
61
62 this.set_transient_for (parent);
63 this.show_all ();
64 }
65
66 /**
67 * @name start
68 * @description start the progress dialog
69 */
70 public GLib.Cancellable start () {
71 this.cancellable = new GLib.Cancellable ();
72 this.cancellable.cancelled.connect (() => {
73 this.stop ();
74 });
75
76 this.show ();
77 this.timeout_id = GLib.Timeout.add (100, () => {
78 // this.prueba.set_text("prueba %d".printf(this.index));
79 (this.progress_bar as Gtk.ProgressBar).pulse ();
80 (this.status as Gtk.Label).set_text (this.text);
81 return true;
82 });
83
84 return this.cancellable;
85 }
86
87 /**
88 * @name stop
89 * @description stop the progres bar
90 */
91 public void stop () {
92 if (this.timeout_id > 0) {
93 GLib.Source.remove (this.timeout_id);
94 this.timeout_id = 0;
95 }
96 this.close ();
97 }
98
99 string text = "";
100
101 /**
102 * @name show_action
103 * @description show the action in the progress dialog
104 * @param {string} action the action to be showed
105 */
106 public void show_action (string action) {
107 text = action;
108 debug (action);
109 }
110
111 }