Skip to content
API Documentation
BastionGuard™
OSDNotify.vala
Go to the documentation of this file.
1
2/*
3 * OSDNotify.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25using TeeJee.Logging;
28
29// dep: notify-send
30public class OSDNotify : GLib.Object {
31
32 private static DateTime dt_last_notification = null;
33 public const int NOTIFICATION_INTERVAL = 3;
34
35 public static int notify_send (
36 string title, string message, int durationMillis,
37 string urgency = "low", // low, normal, critical
38 string dialog_type = "info" //error, info, warning
39 ){
40
41 /* Displays notification bubble on the desktop */
42
43 int retVal = 0;
44
45 switch (dialog_type){
46 case "error":
47 case "info":
48 case "warning":
49 //ok
50 break;
51 default:
52 dialog_type = "info";
53 break;
54 }
55
56 long seconds = 9999;
57
58 if (dt_last_notification != null){
59 DateTime dt_end = new DateTime.now_local();
60 TimeSpan elapsed = dt_end.difference(dt_last_notification);
61 seconds = (long)(elapsed * 1.0 / TimeSpan.SECOND);
62 }
63
64 if (seconds > NOTIFICATION_INTERVAL){
65
66 if (is_supported()){
67
68 string desktop_entry = "bastionguard-backup-gtk";
69 string hint = "string:desktop-entry:%s".printf(desktop_entry);
70
71 string s = "notify-send -t %d -u %s -i %s \"%s\" \"%s\" -h %s".printf(
72 durationMillis, urgency, "gtk-dialog-" + dialog_type, title, message, hint);
73
74 retVal = TeeJee.ProcessHelper.exec_user_async(s);
75
76 dt_last_notification = new DateTime.now_local();
77 }
78 }
79
80 return retVal;
81 }
82
83 public static bool is_supported(){
84 return cmd_exists("notify-send");
85 }
86}
static int notify_send(string title, string message, int durationMillis, string urgency="low", string dialog_type="info")
const int NOTIFICATION_INTERVAL
static DateTime dt_last_notification
static bool is_supported()