identation
[psensor.git] / src / ui_appindicator.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@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_menu_show(gpointer data, guint cb_action, GtkWidget *item)
33 {
34         struct ui_psensor *ui = (struct ui_psensor *)data;
35
36         gtk_window_present(GTK_WINDOW(ui->main_window));
37 }
38
39 static void cb_menu_quit(gpointer data, guint cb_action, GtkWidget *item)
40 {
41         ui_psensor_quit(data);
42 }
43
44 static void 
45 cb_menu_preferences(gpointer data, guint cb_action, GtkWidget *item)
46 {
47 #ifdef HAVE_APPINDICATOR_029
48         gdk_threads_enter();
49 #endif
50
51         ui_pref_dialog_run((struct ui_psensor *)data);
52
53 #ifdef HAVE_APPINDICATOR_029
54         gdk_threads_leave();
55 #endif
56 }
57
58 static GtkItemFactoryEntry menu_items[] = {
59         {"/Show",
60          NULL, cb_menu_show, 0, "<Item>"},
61         {"/Preferences",
62          NULL, cb_menu_preferences, 0, "<Item>"},
63         {"/sep1",
64          NULL, NULL, 0, "<Separator>"},
65         {"/Quit",
66          "", cb_menu_quit, 0, "<StockItem>", GTK_STOCK_QUIT},
67 };
68
69 static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
70 static GtkWidget *get_menu(struct ui_psensor *ui)
71 {
72         GtkItemFactory *item_factory;
73
74         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "<main>", NULL);
75
76         gtk_item_factory_create_items(item_factory,
77                                       nmenu_items, menu_items, ui);
78         return gtk_item_factory_get_widget(item_factory, "<main>");
79 }
80
81 void ui_appindicator_update(struct ui_psensor *ui)
82 {
83         struct psensor **sensor_cur = ui->sensors;
84         AppIndicatorStatus status;
85         int attention = 0;
86
87         if (!ui->indicator)
88                 return;
89
90         while (*sensor_cur) {
91                 struct psensor *s = *sensor_cur;
92
93                 if (s->alarm_enabled && s->alarm_raised) {
94                         attention = 1;
95                         break;
96                 }
97
98                 sensor_cur++;
99         }
100
101         status = app_indicator_get_status(ui->indicator);
102
103         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
104                 app_indicator_set_status
105                     (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
106
107         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
108                 app_indicator_set_status
109                     (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
110 }
111
112 void ui_appindicator_init(struct ui_psensor *ui)
113 {
114         GtkWidget *indicatormenu;
115
116         ui->indicator
117             = app_indicator_new("psensor",
118                                 "psensor",
119                                 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
120
121         app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
122         app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
123
124         indicatormenu = get_menu(ui);
125         app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
126 }