(no commit message)
[psensor.git] / src / ui_appindicator.c
1 /*
2     Copyright (C) 2010-2011 wpitchoune@gmail.com
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 2 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, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <gtk/gtk.h>
25 #include <libappindicator/app-indicator.h>
26
27 #include "psensor.h"
28 #include "ui.h"
29 #include "ui_appindicator.h"
30 #include "ui_pref.h"
31
32 static void cb_appindicator_show(GtkWidget *widget, gpointer data)
33 {
34         struct ui_psensor *ui = (struct ui_psensor *)data;
35
36         gtk_window_present(GTK_WINDOW(ui->main_window));
37
38 }
39
40 static void cb_appindicator_quit(GtkWidget *widget, gpointer data)
41 {
42         ui_psensor_exit(data);
43 }
44
45 static void cb_appindicator_preferences(GtkWidget *widget, gpointer data)
46 {
47         gdk_threads_enter();
48         ui_pref_dialog_run((struct ui_psensor *)data);
49         gdk_threads_leave();
50 }
51
52 GtkWidget *ui_appindicator_get_menu(struct ui_psensor *ui)
53 {
54         GtkWidget *menu, *item;
55
56         menu = gtk_menu_new();
57
58         item = gtk_menu_item_new_with_label(_("Show"));
59         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
60         g_signal_connect(item,
61                          "activate", G_CALLBACK(cb_appindicator_show), ui);
62
63         item = gtk_menu_item_new_with_label(_("Preferences"));
64         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
65         g_signal_connect(item,
66                          "activate", G_CALLBACK(cb_appindicator_preferences),
67                          ui);
68
69         item = gtk_separator_menu_item_new();
70         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
71
72         item = gtk_menu_item_new_with_label(_("Quit"));
73         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
74         g_signal_connect(item,
75                          "activate", G_CALLBACK(cb_appindicator_quit), ui);
76
77         gtk_widget_show_all(menu);
78
79         return menu;
80 }
81
82 void ui_appindicator_update(struct ui_psensor *ui)
83 {
84         struct psensor **sensor_cur = ui->sensors;
85         AppIndicatorStatus status;
86         int attention = 0;
87
88         if (!ui->indicator)
89                 return;
90
91         while (*sensor_cur) {
92                 struct psensor *s = *sensor_cur;
93
94                 if (s->alarm_enabled && s->alarm_raised) {
95                         attention = 1;
96                         break;
97                 }
98
99                 sensor_cur++;
100         }
101
102         status = app_indicator_get_status(ui->indicator);
103
104         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
105                 app_indicator_set_status
106                     (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
107
108         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
109                 app_indicator_set_status
110                     (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
111 }
112
113 void ui_appindicator_init(struct ui_psensor *ui)
114 {
115         GtkWidget *indicatormenu;
116
117         ui->indicator
118             = app_indicator_new("psensor",
119                                 "psensor",
120                                 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
121
122         indicatormenu = ui_appindicator_get_menu(ui);
123
124         app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
125         app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
126
127         app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
128 }