Debian Codegraph @

src/notifications.vala

raw
1 /* notifications.vala
2 *
3 * Copyright (C) Red Hat, Inc
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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Felipe Borges <[email protected]>
19 *
20 */
21
22 namespace Connections {
23 private class NotificationsBar : Gtk.Bin {
24 public const int DEFAULT_TIMEOUT = 6;
25 private const int MAX_NOTIFICATIONS = 5;
26
27 private Gtk.Widget? _active_notification;
28 public Gtk.Widget? active_notification {
29 get {
30 return _active_notification;
31 }
32
33 set {
34 if (_active_notification != null) {
35 remove (_active_notification);
36 }
37
38 _active_notification = value;
39
40 if (_active_notification != null) {
41 add (_active_notification);
42 }
43 }
44 }
45
46 construct {
47 valign = Gtk.Align.START;
48 halign = Gtk.Align.CENTER;
49 get_style_context ().add_class ("transparent-bg");
50 }
51
52 public void display_for_error (string message) {
53 var notification = new Notification (message, null, null, null);
54
55 active_notification = notification;
56 }
57
58 public void display_for_action (string message,
59 string? ok_label,
60 owned Notification.OKFunc? ok_func,
61 owned Notification.DismissFunc? dismiss_func) {
62 var notification = new Notification (message, ok_label, (owned) ok_func, (owned) dismiss_func);
63 notification.dismissed.connect (() => {
64 active_notification = null;
65 });
66
67 active_notification = notification;
68 }
69
70 public void dismiss () {
71 if (active_notification != null) {
72 if (active_notification is Notification) {
73 Notification? notification = active_notification as Notification;
74
75 notification.dismiss ();
76 }
77
78 active_notification = null;
79 }
80 }
81 }
82
83 [GtkTemplate (ui = "/org/gnome/Connections/ui/notification.ui")]
84 private class Notification : Gtk.Revealer {
85 public signal void dismissed ();
86
87 public delegate void OKFunc ();
88 public delegate void DismissFunc ();
89
90 private DismissFunc? dismiss_func;
91
92 [GtkChild]
93 private unowned Gtk.Label message_label;
94 [GtkChild]
95 private unowned Gtk.Button ok_button;
96
97 private uint notification_timeout_id = 0;
98
99 public Notification (string message,
100 string? ok_label,
101 owned OKFunc? ok_func,
102 owned DismissFunc? dismiss_func,
103 int timeout = NotificationsBar.DEFAULT_TIMEOUT) {
104 this.dismiss_func = (owned)dismiss_func;
105 set_reveal_child (true);
106
107 message_label.label = message;
108
109 notification_timeout_id = Timeout.add_seconds (timeout, () => {
110 notification_timeout_id = 0;
111 dismiss ();
112 return Source.REMOVE;
113 });
114
115 if (ok_label != null) {
116 ok_button.label = ok_label;
117
118 ok_button.clicked.connect (() => {
119 if (ok_func != null)
120 ok_func ();
121
122 set_reveal_child (false);
123 dismissed ();
124
125 if (notification_timeout_id != 0) {
126 Source.remove (notification_timeout_id);
127 notification_timeout_id = 0;
128 }
129 });
130
131 ok_button.show_all ();
132 }
133 }
134
135 [GtkCallback]
136 public void dismiss () {
137 dismissed ();
138 set_reveal_child (false);
139 if (dismiss_func != null)
140 dismiss_func ();
141
142 if (notification_timeout_id != 0) {
143 Source.remove (notification_timeout_id);
144 notification_timeout_id = 0;
145 }
146 }
147 }
148 }