~jfi/ubuntu/quantal/psensor/new-upstream

« back to all changes in this revision

Viewing changes to src/ui_appindicator.c

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2012-01-21 22:41:34 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120121224134-mty23d3917ng5ujd
Tags: 0.6.2.16-1ubuntu1
* Merge from Debian testing.  Remaining changes:
  + Modifed build-dep to add NVidia and Application Indicator support
  + Only add an nvidia-settings build-dep on i386 and amd64

* Add a patch to drop the deprecated g_thread_init() call
  - add debian/patches/drop_g_thread_init.patch
  - add debian/patches/series

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
 
 
2
 * Copyright (C) 2010-2011 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
 */
20
19
#include <stdio.h>
21
20
#include <stdlib.h>
22
21
#include <string.h>
27
26
#include "psensor.h"
28
27
#include "ui.h"
29
28
#include "ui_appindicator.h"
 
29
#include "ui_sensorpref.h"
 
30
#include "ui_status.h"
30
31
#include "ui_pref.h"
31
32
 
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)
 
33
static const char *ICON = "psensor_normal";
 
34
static const char *ATTENTION_ICON = "psensor_hot";
 
35
 
 
36
static GtkMenuItem **sensor_menu_items;
 
37
static GtkWidget *main_window;
 
38
static int appindicator_supported = 1;
 
39
static AppIndicator *indicator;
 
40
static struct ui_psensor *ui_psensor;
 
41
 
 
42
static void cb_menu_show(GtkMenuItem *mi, gpointer data)
 
43
{
 
44
        ui_window_show((struct ui_psensor *)data);
 
45
}
 
46
 
 
47
static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
 
48
{
 
49
        ui_psensor_quit(data);
 
50
}
 
51
 
 
52
static void cb_menu_preferences(GtkMenuItem *mi, gpointer data)
46
53
{
47
54
#ifdef HAVE_APPINDICATOR_029
48
55
        gdk_threads_enter();
55
62
#endif
56
63
}
57
64
 
58
 
GtkWidget *ui_appindicator_get_menu(struct ui_psensor *ui)
59
 
{
60
 
        GtkWidget *menu, *item;
61
 
 
62
 
        menu = gtk_menu_new();
63
 
 
64
 
        item = gtk_menu_item_new_with_label(_("Show"));
65
 
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
66
 
        g_signal_connect(item,
67
 
                         "activate", G_CALLBACK(cb_appindicator_show), ui);
68
 
 
69
 
        item = gtk_menu_item_new_with_label(_("Preferences"));
70
 
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
71
 
        g_signal_connect(item,
72
 
                         "activate", G_CALLBACK(cb_appindicator_preferences),
73
 
                         ui);
74
 
 
75
 
        item = gtk_separator_menu_item_new();
76
 
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
77
 
 
78
 
        item = gtk_menu_item_new_with_label(_("Quit"));
79
 
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
80
 
        g_signal_connect(item,
81
 
                         "activate", G_CALLBACK(cb_appindicator_quit), ui);
82
 
 
83
 
        gtk_widget_show_all(menu);
84
 
 
85
 
        return menu;
86
 
}
87
 
 
88
 
void ui_appindicator_update(struct ui_psensor *ui)
89
 
{
90
 
        struct psensor **sensor_cur = ui->sensors;
 
65
static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
 
66
{
 
67
        struct ui_psensor *ui = data;
 
68
 
 
69
#ifdef HAVE_APPINDICATOR_029
 
70
        gdk_threads_enter();
 
71
#endif
 
72
 
 
73
        if (ui->sensors && *ui->sensors)
 
74
                ui_sensorpref_dialog_run(*ui->sensors, ui);
 
75
 
 
76
#ifdef HAVE_APPINDICATOR_029
 
77
        gdk_threads_leave();
 
78
#endif
 
79
}
 
80
 
 
81
static void cb_about(GtkMenuItem *mi, gpointer data)
 
82
{
 
83
        ui_show_about_dialog();
 
84
}
 
85
 
 
86
static const char *menu_desc =
 
87
"<ui>"
 
88
"  <popup name='MainMenu'>"
 
89
"      <menuitem name='Show' action='ShowAction' />"
 
90
"      <separator />"
 
91
"      <separator />"
 
92
"      <menuitem name='Preferences' action='PreferencesAction' />"
 
93
"      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
 
94
"      <separator />"
 
95
"      <menuitem name='About' action='AboutAction' />"
 
96
"      <separator />"
 
97
"      <menuitem name='Quit' action='QuitAction' />"
 
98
"  </popup>"
 
99
"</ui>";
 
100
 
 
101
static GtkActionEntry entries[] = {
 
102
        { "PsensorMenuAction", NULL, "_Psensor" },
 
103
 
 
104
        { "ShowAction", NULL,
 
105
          "_Show", NULL,
 
106
          "Show",
 
107
          G_CALLBACK(cb_menu_show) },
 
108
 
 
109
        { "PreferencesAction", GTK_STOCK_PREFERENCES,
 
110
          "_Preferences", NULL,
 
111
          "Preferences",
 
112
          G_CALLBACK(cb_menu_preferences) },
 
113
 
 
114
        { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
 
115
          "S_ensor Preferences",
 
116
          NULL,
 
117
          "SensorPreferences",
 
118
          G_CALLBACK(cb_sensor_preferences) },
 
119
 
 
120
        { "AboutAction", NULL,
 
121
          "_About",
 
122
          NULL,
 
123
          "About",
 
124
          G_CALLBACK(cb_about) },
 
125
 
 
126
        { "QuitAction",
 
127
          GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
 
128
};
 
129
static guint n_entries = G_N_ELEMENTS(entries);
 
130
 
 
131
static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
 
132
{
 
133
        gchar *str;
 
134
 
 
135
        str = g_strdup_printf("%s: %2.f %s",
 
136
                              s->name,
 
137
                              psensor_get_current_value(s),
 
138
                              psensor_type_to_unit_str(s->type));
 
139
 
 
140
        gtk_menu_item_set_label(item, str);
 
141
 
 
142
        g_free(str);
 
143
}
 
144
 
 
145
static void update_sensor_menu_items(struct psensor **sensors)
 
146
{
 
147
        int n, i;
 
148
 
 
149
        n = psensor_list_size(sensors);
 
150
        for (i = 0; i < n; i++)
 
151
                update_sensor_menu_item(sensor_menu_items[i], sensors[i]);
 
152
}
 
153
 
 
154
static GtkWidget *get_menu(struct ui_psensor *ui)
 
155
{
 
156
        GtkActionGroup *action_group;
 
157
        GtkUIManager *menu_manager;
 
158
        GError *error;
 
159
        GtkMenu *menu;
 
160
        int i;
 
161
        int n = psensor_list_size(ui->sensors);
 
162
        struct psensor **sensors = ui->sensors;
 
163
 
 
164
 
 
165
        action_group = gtk_action_group_new("PsensorActions");
 
166
        gtk_action_group_set_translation_domain(action_group, PACKAGE);
 
167
        menu_manager = gtk_ui_manager_new();
 
168
 
 
169
        gtk_action_group_add_actions(action_group, entries, n_entries, ui);
 
170
        gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
 
171
 
 
172
        error = NULL;
 
173
        gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
 
174
 
 
175
        if (error)
 
176
                g_error(_("building menus failed: %s"), error->message);
 
177
 
 
178
        menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu"));
 
179
 
 
180
        sensor_menu_items = malloc(sizeof(GtkWidget *)*n);
 
181
        for (i = 0; i < n; i++) {
 
182
                struct psensor *s = sensors[i];
 
183
 
 
184
                sensor_menu_items[i]
 
185
                        = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name));
 
186
 
 
187
                gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
 
188
                                      GTK_WIDGET(sensor_menu_items[i]),
 
189
                                      i+2);
 
190
 
 
191
                update_sensor_menu_item(sensor_menu_items[i],
 
192
                                        s);
 
193
        }
 
194
 
 
195
 
 
196
        return GTK_WIDGET(menu);
 
197
}
 
198
 
 
199
void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
 
200
{
91
201
        AppIndicatorStatus status;
92
 
        int attention = 0;
93
202
 
94
 
        if (!ui->indicator)
 
203
        if (!indicator)
95
204
                return;
96
205
 
97
 
        while (*sensor_cur) {
98
 
                struct psensor *s = *sensor_cur;
99
 
 
100
 
                if (s->alarm_enabled && s->alarm_raised) {
101
 
                        attention = 1;
102
 
                        break;
103
 
                }
104
 
 
105
 
                sensor_cur++;
106
 
        }
107
 
 
108
 
        status = app_indicator_get_status(ui->indicator);
 
206
        status = app_indicator_get_status(indicator);
109
207
 
110
208
        if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
111
209
                app_indicator_set_status
112
 
                    (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
 
210
                        (indicator, APP_INDICATOR_STATUS_ACTIVE);
113
211
 
114
212
        if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
115
213
                app_indicator_set_status
116
 
                    (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
 
214
                    (indicator, APP_INDICATOR_STATUS_ATTENTION);
 
215
 
 
216
        update_sensor_menu_items(ui->sensors);
 
217
}
 
218
 
 
219
static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
 
220
{
 
221
        log_debug("ui_appindicator#unity_fallback");
 
222
 
 
223
        appindicator_supported = 0;
 
224
 
 
225
        return ui_status_get_icon(ui_psensor);
 
226
}
 
227
 
 
228
static void
 
229
unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
 
230
{
 
231
        log_debug("ui_appindicator#unity_unfallback");
 
232
 
 
233
        appindicator_supported = 1;
117
234
}
118
235
 
119
236
void ui_appindicator_init(struct ui_psensor *ui)
120
237
{
121
 
        GtkWidget *indicatormenu;
122
 
 
123
 
        ui->indicator
124
 
            = app_indicator_new("psensor",
125
 
                                "psensor",
126
 
                                APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
127
 
 
128
 
        indicatormenu = ui_appindicator_get_menu(ui);
129
 
 
130
 
        app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
131
 
        app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
132
 
 
133
 
        app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
 
238
        GtkWidget *menu;
 
239
 
 
240
        ui_psensor = ui;
 
241
        main_window = ui->main_window;
 
242
 
 
243
        indicator = app_indicator_new
 
244
                ("psensor",
 
245
                 ICON,
 
246
                 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
 
247
 
 
248
        APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
 
249
        APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
 
250
 
 
251
        app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
 
252
        app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
 
253
 
 
254
        menu = get_menu(ui);
 
255
        app_indicator_set_menu(indicator, GTK_MENU(menu));
 
256
 
 
257
        gtk_widget_show_all(menu);
 
258
}
 
259
 
 
260
int is_appindicator_supported()
 
261
{
 
262
        return appindicator_supported;
 
263
}
 
264
 
 
265
void ui_appindicator_cleanup()
 
266
{
 
267
        /* TODO: cleanup menu items. */
134
268
}