-
Notifications
You must be signed in to change notification settings - Fork 0
/
pill-applet.c
179 lines (142 loc) · 5.27 KB
/
pill-applet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "pill-applet.h"
static void do_reset_from_menu(BonoboUIComponent* uic, gpointer data, const char* cname);
static const char context_menu_xml[] =
"<popup name=\"button3\">\n"
" <menuitem name=\"Reset Item\" "
" verb=\"ResetStatus\" "
" _label=\"Reset Pill Status\" />\n"
"</popup>\n";
static const BonoboUIVerb pill_menu_verbs[] = {
BONOBO_UI_VERB("ResetStatus",do_reset_from_menu),
BONOBO_UI_VERB_END
};
static const gchar *PILL_TAKEN_MSG = "PILL TAKEN";
static const gchar *PILL_NOT_TAKEN_MSG = "PILL NOT TAKEN";
static const gchar *INTERVAL_KEY = "/apps/pill-applet/reset_interval";
static const gchar *APP_CONFIG = "/apps/pill-applet";
static const gchar *APP_IID = "OAFIID:GNOME_PillApplet";
static const gchar *APP_FACTORY_IID = "OAFIID:GNOME_PillApplet_Factory";
static const gchar *APP_NAME = "Pill Applet";
static const gchar *APP_VERSION = "0.6";
// (60 seconds/minute, 30 minutes) - poll wallclock every half an hour :(
static const guint INTERVAL = 60 * 30;
// reset after 22 hours (60 seconds/minute * 60 minutes/hour * 22 hours)
static void cleanup(GtkWidget *applet, gpointer data) {
ApplicationState *app = NULL;
GConfClient *client = NULL;
app = (ApplicationState*)data;
client = GCONF_CLIENT(app->client);
gconf_client_notify_remove(client, app->notification_id);
gconf_client_remove_dir(client, APP_CONFIG, NULL);
g_object_unref(client);
g_free(app);
}
static void reset_app_state(ApplicationState *app) {
timerclear(&(app->pill_taken_time));
app->event_fired = FALSE;
gtk_label_set_text(GTK_LABEL(LABEL_FROM_APPLET(app->applet)), PILL_NOT_TAKEN_MSG);
}
static void do_reset_from_menu(BonoboUIComponent* uic, gpointer data, const char* cname) {
reset_app_state(data);
}
static gboolean try_reset_indicator(gpointer data) {
struct timeval now = { 0,0 };
struct timeval elapsed_time = { 0,0 };
ApplicationState *app = NULL;
gboolean rv = TRUE;
app = (ApplicationState*)data;
gettimeofday(&now,NULL);
timeradd(&(app->pill_taken_time), &(app->reset_interval), &elapsed_time);
if(timercmp(&now, &elapsed_time, >)) {
reset_app_state(app);
rv = FALSE;
}
return rv;
}
static void interval_changed(GConfClient *client,
guint cnxn_id,
GConfEntry *entry,
gpointer user_data) {
GConfValue *value = NULL;
int new_interval = 0;
GSource *old_timer = NULL;
ApplicationState *app = NULL;
app = (ApplicationState*)user_data;
value = gconf_entry_get_value(entry);
new_interval = gconf_value_get_int(value);
// disable timeout event
if (app->timeout_event_id) {
old_timer = g_main_context_find_source_by_id(NULL,app->timeout_event_id);
if(old_timer) {
g_source_destroy(old_timer);
app->timeout_event_id = 0;
}
}
reset_app_state(app);
app->reset_interval.tv_sec = new_interval;
}
static gboolean pill_taken(GtkWidget *event_box, GdkEventButton *event, gpointer data) {
GtkWidget *label = NULL;
ApplicationState *app = NULL;
app = (ApplicationState*)data;
label = LABEL_FROM_APPLET(app->applet);
if(!(app->event_fired) && (event->button == 1)) {
app->event_fired = TRUE;
gettimeofday(&(app->pill_taken_time),NULL);
gtk_label_set_text(GTK_LABEL(label), PILL_TAKEN_MSG);
app->timeout_event_id = g_timeout_add_seconds(INTERVAL, try_reset_indicator, app);
}
return FALSE;
}
gboolean pill_applet_fill(PanelApplet *applet, const gchar *iid, gpointer data) {
GtkWidget *label = NULL;
GtkWidget *event_box = NULL;
ApplicationState *app = NULL;
GConfValue* initial_interval = NULL;
int interval = 0;
if (strcmp(iid,APP_IID) != 0) {
return FALSE;
}
app = g_new0(ApplicationState, 1);
app->applet = applet;
if(!(app->client = gconf_client_get_default())) {
return FALSE;
}
gconf_client_set_error_handling(app->client,GCONF_CLIENT_HANDLE_ALL);
if(!(initial_interval = gconf_client_get(app->client,INTERVAL_KEY,NULL))) {
g_object_unref(app->client);
g_free(app);
return FALSE;
}
if(!(interval = gconf_value_get_int(initial_interval))) {
gconf_value_free(initial_interval);
g_object_unref(app->client);
g_free(app);
return FALSE;
}
app->reset_interval.tv_sec = interval;
app->reset_interval.tv_usec = 0;
gconf_value_free(initial_interval);
label = gtk_label_new(PILL_NOT_TAKEN_MSG);
event_box = gtk_event_box_new();
gtk_event_box_set_visible_window(GTK_EVENT_BOX(event_box),FALSE);
gtk_container_add(GTK_CONTAINER(event_box), label);
g_signal_connect(GTK_OBJECT(event_box), "button-press-event", G_CALLBACK(pill_taken),app);
gtk_container_add(GTK_CONTAINER(applet),event_box);
gtk_widget_show_all(GTK_WIDGET(applet));
gconf_client_add_dir(app->client, APP_CONFIG,
GCONF_CLIENT_PRELOAD_NONE, NULL);
app->notification_id = gconf_client_notify_add(app->client,
INTERVAL_KEY,
interval_changed, app,
NULL, NULL);
g_signal_connect(GTK_OBJECT(applet), "destroy", G_CALLBACK(cleanup), app);
panel_applet_setup_menu(PANEL_APPLET(applet),context_menu_xml,pill_menu_verbs,app);
return TRUE;
}
PANEL_APPLET_BONOBO_FACTORY (APP_FACTORY_IID,
PANEL_TYPE_APPLET,
APP_NAME,
APP_VERSION,
pill_applet_fill,
NULL)