support of gtk3
[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_sensorpref.h"
31 #include "ui_pref.h"
32
33 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
34 {
35         struct ui_psensor *ui = (struct ui_psensor *)data;
36
37         gtk_window_present(GTK_WINDOW(ui->main_window));
38 }
39
40 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
41 {
42         ui_psensor_quit(data);
43 }
44
45 static void
46 cb_menu_preferences(GtkMenuItem *mi, gpointer data)
47 {
48 #ifdef HAVE_APPINDICATOR_029
49         gdk_threads_enter();
50 #endif
51
52         ui_pref_dialog_run((struct ui_psensor *)data);
53
54 #ifdef HAVE_APPINDICATOR_029
55         gdk_threads_leave();
56 #endif
57 }
58
59 static void
60 cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
61 {
62         struct ui_psensor *ui = data;
63
64         if (ui->sensors && *ui->sensors)
65                 ui_sensorpref_dialog_run(*ui->sensors, ui);
66 }
67
68 static const char *menu_desc =
69 "<ui>"
70 "  <popup name='MainMenu'>"
71 "      <menuitem name='Show' action='ShowAction' />"
72 "      <menuitem name='Preferences' action='PreferencesAction' />"
73 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
74 "      <separator />"
75 "      <menuitem name='Quit' action='QuitAction' />"
76 "  </popup>"
77 "</ui>";
78
79 static GtkActionEntry entries[] = {
80   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
81
82   { "ShowAction", NULL,        /* name, stock id */
83     "_Show", NULL, /* label, accelerator */
84     "Show",                    /* tooltip */
85     G_CALLBACK(cb_menu_show) },
86
87   { "PreferencesAction", GTK_STOCK_PREFERENCES,     /* name, stock id */
88     "_Preferences", NULL,                           /* label, accelerator */
89     "Preferences",                                  /* tooltip */
90     G_CALLBACK(cb_menu_preferences) },
91
92   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
93     "S_ensor Preferences",
94     NULL,
95     "SensorPreferences",
96     G_CALLBACK(cb_sensor_preferences) },
97
98   { "QuitAction",
99     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
100 };
101 static guint n_entries = G_N_ELEMENTS(entries);
102
103 static GtkWidget *get_menu(struct ui_psensor *ui)
104 {
105         GtkActionGroup      *action_group;
106         GtkUIManager        *menu_manager;
107         GError              *error;
108
109         action_group = gtk_action_group_new("PsensorActions");
110         gtk_action_group_set_translation_domain(action_group, PACKAGE);
111         menu_manager = gtk_ui_manager_new();
112
113         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
114         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
115
116         error = NULL;
117         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
118
119         if (error)
120                 g_error(_("building menus failed: %s"), error->message);
121
122         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
123 }
124
125
126 void ui_appindicator_update(struct ui_psensor *ui)
127 {
128         struct psensor **sensor_cur = ui->sensors;
129         AppIndicatorStatus status;
130         int attention = 0;
131
132         if (!ui->indicator)
133                 return;
134
135         while (*sensor_cur) {
136                 struct psensor *s = *sensor_cur;
137
138                 if (s->alarm_enabled && s->alarm_raised) {
139                         attention = 1;
140                         break;
141                 }
142
143                 sensor_cur++;
144         }
145
146         status = app_indicator_get_status(ui->indicator);
147
148         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
149                 app_indicator_set_status
150                     (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
151
152         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
153                 app_indicator_set_status
154                     (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
155 }
156
157 void ui_appindicator_init(struct ui_psensor *ui)
158 {
159         GtkWidget *indicatormenu;
160
161         ui->indicator
162             = app_indicator_new("psensor",
163                                 "psensor",
164                                 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
165
166         app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
167         app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
168
169         indicatormenu = get_menu(ui);
170         app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
171 }