Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / ui_status.c
1 /*
2  * Copyright (C) 2010-2012 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 "log.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
33         ui_window_show((struct ui_psensor *)data);
34 }
35
36 static void cb_popup_menu(GtkStatusIcon *icon,
37                           guint button,
38                           guint activate_time,
39                           gpointer data)
40 {
41         log_debug("cb_popup_menu()");
42 }
43
44 void ui_status_init(struct ui_psensor *ui)
45 {
46         if (status)
47                 return ;
48
49         log_debug("ui_status_create()");
50
51         status = gtk_status_icon_new();
52         gtk_status_icon_set_from_icon_name(status, ICON);
53         gtk_status_icon_set_visible(status, TRUE);
54
55         g_signal_connect(G_OBJECT(status),
56                          "popup-menu",
57                          G_CALLBACK(cb_popup_menu),
58                          NULL);
59
60         g_signal_connect(G_OBJECT(status),
61                          "activate",
62                          G_CALLBACK(cb_activate),
63                          ui);
64 }
65
66 int is_status_supported()
67 {
68         return status && gtk_status_icon_is_embedded(status);
69 }
70
71 void ui_status_cleanup()
72 {
73         log_debug("ui_status_cleanup()");
74
75         if (status) {
76                 g_object_unref(G_OBJECT(status));
77                 status = NULL;
78         }
79 }
80
81 void ui_status_update(struct ui_psensor *ui, unsigned int attention)
82 {
83         log_debug("ui_status_update()");
84
85         if (status_attention && !attention)
86                 gtk_status_icon_set_from_icon_name(status, ICON);
87         else if (!status_attention && attention)
88                 gtk_status_icon_set_from_icon_name(status, ATTENTION_ICON);
89
90         status_attention = attention;
91 }
92
93 GtkStatusIcon *ui_status_get_icon(struct ui_psensor *ui)
94 {
95         if (!status)
96                 ui_status_init(ui);
97
98         return status;
99 }