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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
An indicator to show information that is in messaging applications
that the user is using.
Copyright 2009 Canonical Ltd.
Authors:
Ted Gould <ted@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "im-menu-item.h"
enum {
TIME_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
typedef struct _ImMenuItemPrivate ImMenuItemPrivate;
struct _ImMenuItemPrivate
{
IndicateListener * listener;
IndicateListenerServer * server;
IndicateListenerIndicator * indicator;
glong seconds;
gboolean show_time;
gulong indicator_changed;
guint time_update_min;
GtkHBox * hbox;
GtkLabel * user;
GtkLabel * time;
GtkImage * icon;
};
#define IM_MENU_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), IM_MENU_ITEM_TYPE, ImMenuItemPrivate))
/* Prototypes */
static void im_menu_item_class_init (ImMenuItemClass *klass);
static void im_menu_item_init (ImMenuItem *self);
static void im_menu_item_dispose (GObject *object);
static void im_menu_item_finalize (GObject *object);
static void sender_cb (IndicateListener * listener,
IndicateListenerServer * server,
IndicateListenerIndicator * indicator,
gchar * property,
gchar * propertydata,
gpointer data);
static void time_cb (IndicateListener * listener,
IndicateListenerServer * server,
IndicateListenerIndicator * indicator,
gchar * property,
GTimeVal * propertydata,
gpointer data);
static void icon_cb (IndicateListener * listener,
IndicateListenerServer * server,
IndicateListenerIndicator * indicator,
gchar * property,
GdkPixbuf * propertydata,
gpointer data);
static void activate_cb (ImMenuItem * self,
gpointer data);
static void indicator_modified_cb (IndicateListener * listener,
IndicateListenerServer * server,
IndicateListenerIndicator * indicator,
gchar * type,
gchar * property,
ImMenuItem * self);
static GtkSizeGroup * icon_group = NULL;
static GtkSizeGroup * user_group = NULL;
static GtkSizeGroup * time_group = NULL;
G_DEFINE_TYPE (ImMenuItem, im_menu_item, GTK_TYPE_MENU_ITEM);
static void
im_menu_item_class_init (ImMenuItemClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (ImMenuItemPrivate));
object_class->dispose = im_menu_item_dispose;
object_class->finalize = im_menu_item_finalize;
signals[TIME_CHANGED] = g_signal_new(IM_MENU_ITEM_SIGNAL_TIME_CHANGED,
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ImMenuItemClass, time_changed),
NULL, NULL,
g_cclosure_marshal_VOID__LONG,
G_TYPE_NONE, 1, G_TYPE_LONG);
return;
}
static void
im_menu_item_init (ImMenuItem *self)
{
g_debug("Building new IM Menu Item");
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
priv->listener = NULL;
priv->server = NULL;
priv->indicator = NULL;
priv->seconds = 0;
/* build widgets first */
priv->icon = GTK_IMAGE(gtk_image_new());
priv->user = GTK_LABEL(gtk_label_new(""));
priv->time = GTK_LABEL(gtk_label_new(""));
gtk_misc_set_alignment(GTK_MISC(priv->user), 0.0, 0.5);
gtk_misc_set_alignment(GTK_MISC(priv->time), 0.0, 0.5);
if (icon_group == NULL) {
icon_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
}
if (user_group == NULL) {
user_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
}
if (time_group == NULL) {
time_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
}
gtk_size_group_add_widget(icon_group, GTK_WIDGET(priv->icon));
gtk_size_group_add_widget(user_group, GTK_WIDGET(priv->user));
gtk_size_group_add_widget(time_group, GTK_WIDGET(priv->time));
priv->hbox = GTK_HBOX(gtk_hbox_new(FALSE, 3));
gtk_box_pack_start(GTK_BOX(priv->hbox), GTK_WIDGET(priv->icon), FALSE, TRUE, 3);
gtk_box_pack_start(GTK_BOX(priv->hbox), GTK_WIDGET(priv->user), TRUE, TRUE, 3);
gtk_box_pack_start(GTK_BOX(priv->hbox), GTK_WIDGET(priv->time), FALSE, TRUE, 3);
gtk_widget_show(GTK_WIDGET(priv->hbox));
gtk_container_add(GTK_CONTAINER(self), GTK_WIDGET(priv->hbox));
return;
}
static void
im_menu_item_dispose (GObject *object)
{
G_OBJECT_CLASS (im_menu_item_parent_class)->dispose (object);
ImMenuItem * self = IM_MENU_ITEM(object);
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
if (priv->time_update_min != 0) {
g_source_remove(priv->time_update_min);
}
g_signal_handler_disconnect(priv->listener, priv->indicator_changed);
priv->indicator_changed = 0;
return;
}
static void
im_menu_item_finalize (GObject *object)
{
G_OBJECT_CLASS (im_menu_item_parent_class)->finalize (object);
}
static void
icon_cb (IndicateListener * listener, IndicateListenerServer * server, IndicateListenerIndicator * indicator, gchar * property, GdkPixbuf * propertydata, gpointer data)
{
ImMenuItem * self = IM_MENU_ITEM(data);
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
gint height, width;
gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height);
GdkPixbuf * scaled = gdk_pixbuf_scale_simple(propertydata, width, height, GDK_INTERP_BILINEAR);
g_object_unref(propertydata);
gtk_image_set_from_pixbuf(priv->icon, scaled);
g_object_unref(scaled);
gtk_widget_show(GTK_WIDGET(priv->icon));
return;
}
static void
update_time (ImMenuItem * self)
{
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
if (!priv->show_time) {
gtk_label_set_label(priv->time, "");
return;
}
gchar * timestring = NULL;
GTimeVal current_time;
g_get_current_time(¤t_time);
guint elapsed_seconds = current_time.tv_sec - priv->seconds;
guint elapsed_minutes = elapsed_seconds / 60;
if (elapsed_seconds % 60 > 55) {
/* We're using fuzzy timers, so we need fuzzy comparisons */
elapsed_minutes += 1;
}
if (elapsed_minutes < 60) {
timestring = g_strdup_printf(ngettext("%d m", "%d m", elapsed_minutes), elapsed_minutes);
} else {
guint elapsed_hours = elapsed_minutes / 60;
if (elapsed_minutes % 60 > 55) {
/* We're using fuzzy timers, so we need fuzzy comparisons */
elapsed_hours += 1;
}
timestring = g_strdup_printf(ngettext("%d h", "%d h", elapsed_hours), elapsed_hours);
}
if (timestring != NULL) {
gtk_label_set_label(priv->time, timestring);
gtk_widget_show(GTK_WIDGET(priv->time));
g_free(timestring);
}
return;
}
static gboolean
time_update_cb (gpointer data)
{
ImMenuItem * self = IM_MENU_ITEM(data);
update_time(self);
return TRUE;
}
static void
time_cb (IndicateListener * listener, IndicateListenerServer * server, IndicateListenerIndicator * indicator, gchar * property, GTimeVal * propertydata, gpointer data)
{
g_debug("Got Time info");
ImMenuItem * self = IM_MENU_ITEM(data);
if (self == NULL) {
g_error("Menu Item callback called without a menu item");
return;
}
if (property == NULL || g_strcmp0(property, "time")) {
g_warning("Time callback called without being sent the time.");
return;
}
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
priv->seconds = propertydata->tv_sec;
update_time(self);
if (priv->time_update_min == 0) {
g_timeout_add_seconds(60, time_update_cb, self);
}
g_signal_emit(G_OBJECT(self), signals[TIME_CHANGED], 0, priv->seconds, TRUE);
return;
}
static void
sender_cb (IndicateListener * listener, IndicateListenerServer * server, IndicateListenerIndicator * indicator, gchar * property, gchar * propertydata, gpointer data)
{
g_debug("Got Sender Information");
ImMenuItem * self = IM_MENU_ITEM(data);
if (self == NULL) {
g_error("Menu Item callback called without a menu item");
return;
}
if (property == NULL || g_strcmp0(property, "sender")) {
g_warning("Sender callback called without being sent the sender. We got '%s' with value '%s'.", property, propertydata);
return;
}
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
gtk_label_set_label(priv->user, propertydata);
gtk_widget_show(GTK_WIDGET(priv->user));
/* Once we have the user we'll show the menu item */
gtk_widget_show(GTK_WIDGET(self));
return;
}
static void
activate_cb (ImMenuItem * self, gpointer data)
{
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
indicate_listener_display(priv->listener, priv->server, priv->indicator);
}
void
indicator_modified_cb (IndicateListener * listener, IndicateListenerServer * server, IndicateListenerIndicator * indicator, gchar * type, gchar * property, ImMenuItem * self)
{
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
/* Not meant for us */
if (INDICATE_LISTENER_INDICATOR_ID(indicator) != INDICATE_LISTENER_INDICATOR_ID(priv->indicator)) return;
if (server != priv->server) return;
if (!g_strcmp0(property, "sender")) {
indicate_listener_get_property(listener, server, indicator, "sender", sender_cb, self);
} else if (!g_strcmp0(property, "time")) {
indicate_listener_get_property_time(listener, server, indicator, "time", time_cb, self);
} else if (!g_strcmp0(property, "icon")) {
indicate_listener_get_property_icon(listener, server, indicator, "icon", icon_cb, self);
}
return;
}
ImMenuItem *
im_menu_item_new (IndicateListener * listener, IndicateListenerServer * server, IndicateListenerIndicator * indicator, gboolean show_time)
{
g_debug("Building a new IM Menu Item");
ImMenuItem * self = g_object_new(IM_MENU_ITEM_TYPE, NULL);
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(self);
priv->listener = listener;
priv->server = server;
priv->indicator = indicator;
priv->show_time = show_time;
priv->time_update_min = 0;
indicate_listener_get_property(listener, server, indicator, "sender", sender_cb, self);
indicate_listener_get_property_time(listener, server, indicator, "time", time_cb, self);
indicate_listener_get_property_icon(listener, server, indicator, "icon", icon_cb, self);
g_signal_connect(G_OBJECT(self), "activate", G_CALLBACK(activate_cb), NULL);
priv->indicator_changed = g_signal_connect(G_OBJECT(listener), INDICATE_LISTENER_SIGNAL_INDICATOR_MODIFIED, G_CALLBACK(indicator_modified_cb), self);
return self;
}
glong
im_menu_item_get_seconds (ImMenuItem * menuitem)
{
ImMenuItemPrivate * priv = IM_MENU_ITEM_GET_PRIVATE(menuitem);
return priv->seconds;
}
|