Merge tag 'upstream/1.1.2'
[psensor-pkg-debian.git] / src / ui_status.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <plog.h>
20 #include "ui_status.h"
21
22 static const char *ICON = "psensor_normal";
23 static const char *ATTENTION_ICON = "psensor_hot";
24
25 static GtkStatusIcon *status;
26 static unsigned status_attention;
27
28 static void cb_activate(GtkStatusIcon *icon,
29                         gpointer data)
30 {
31         log_debug("cb_activate()");
32         ui_window_show((struct ui_psensor *)data);
33 }
34
35 static void cb_popup_menu(GtkStatusIcon *icon,
36                           guint button,
37                           guint activate_time,
38                           gpointer data)
39 {
40         log_debug("cb_popup_menu()");
41 }
42
43 void ui_status_init(struct ui_psensor *ui)
44 {
45         if (status)
46                 return;
47
48         log_debug("ui_status_create()");
49
50         status = gtk_status_icon_new();
51         gtk_status_icon_set_from_icon_name(status, ICON);
52         ui_status_set_visible(0);
53
54         g_signal_connect(G_OBJECT(status),
55                          "popup-menu",
56                          G_CALLBACK(cb_popup_menu),
57                          NULL);
58
59         g_signal_connect(G_OBJECT(status),
60                          "activate",
61                          G_CALLBACK(cb_activate),
62                          ui);
63 }
64
65 int is_status_supported()
66 {
67         return status && gtk_status_icon_is_embedded(status);
68 }
69
70 void ui_status_cleanup()
71 {
72         log_debug("ui_status_cleanup()");
73
74         if (status) {
75                 g_object_unref(G_OBJECT(status));
76                 status = NULL;
77         }
78 }
79
80 void ui_status_update(struct ui_psensor *ui, unsigned int attention)
81 {
82         log_debug("ui_status_update()");
83
84         if (status_attention && !attention)
85                 gtk_status_icon_set_from_icon_name(status, ICON);
86         else if (!status_attention && attention)
87                 gtk_status_icon_set_from_icon_name(status, ATTENTION_ICON);
88
89         status_attention = attention;
90 }
91
92 GtkStatusIcon *ui_status_get_icon(struct ui_psensor *ui)
93 {
94         if (!status)
95                 ui_status_init(ui);
96
97         return status;
98 }
99
100 void ui_status_set_visible(unsigned int visible)
101 {
102         log_debug("ui_status_set_visible(%d)", visible);
103
104         if (status) {
105                 if (visible)
106                         gtk_status_icon_set_visible(status, TRUE);
107                 else
108                         gtk_status_icon_set_visible(status, FALSE);
109         }
110 }