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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
* Copyright 2013 Canonical Ltd.
*
* Authors:
* Charles Kerr <charles.kerr@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 <string.h> /* strstr() */
#include <gtk/gtk.h>
#include "idoactionhelper.h"
#include "idolocationmenuitem.h"
enum
{
PROP_0,
PROP_NAME,
PROP_TIMEZONE,
PROP_FORMAT,
PROP_LAST
};
static GParamSpec *properties[PROP_LAST];
struct _IdoLocationMenuItemPrivate
{
char * name;
char * timezone;
char * format;
guint timestamp_timer;
GtkWidget * name_label;
GtkWidget * timestamp_label;
};
typedef IdoLocationMenuItemPrivate priv_t;
G_DEFINE_TYPE (IdoLocationMenuItem, ido_location_menu_item, GTK_TYPE_MENU_ITEM);
/***
**** Timestamp Label
***/
static void
update_timestamp_label (IdoLocationMenuItem * self)
{
priv_t * p = self->priv;
if (p->format && *p->format)
{
GTimeZone * tz;
GDateTime * now;
char * str;
tz = g_time_zone_new (p->timezone);
if (tz == NULL)
tz = g_time_zone_new_local ();
now = g_date_time_new_now (tz);
str = g_date_time_format (now, p->format);
gtk_label_set_text (GTK_LABEL(p->timestamp_label), str);
g_free (str);
g_date_time_unref (now);
g_time_zone_unref (tz);
}
else
{
gtk_label_set_text (GTK_LABEL(p->timestamp_label), "");
}
}
static void
stop_timestamp_timer (IdoLocationMenuItem * self)
{
priv_t * p = self->priv;
if (p->timestamp_timer != 0)
{
g_source_remove (p->timestamp_timer);
p->timestamp_timer = 0;
}
}
static void start_timestamp_timer (IdoLocationMenuItem * self);
static gboolean
on_timestamp_timer (gpointer gself)
{
IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (gself);
update_timestamp_label (self);
start_timestamp_timer (self);
return G_SOURCE_REMOVE;
}
static guint
calculate_seconds_until_next_minute (void)
{
guint seconds;
GTimeSpan diff;
GDateTime * now;
GDateTime * next;
GDateTime * start_of_next;
now = g_date_time_new_now_local ();
next = g_date_time_add_minutes (now, 1);
start_of_next = g_date_time_new_local (g_date_time_get_year (next),
g_date_time_get_month (next),
g_date_time_get_day_of_month (next),
g_date_time_get_hour (next),
g_date_time_get_minute (next),
1);
diff = g_date_time_difference (start_of_next, now);
seconds = (diff + (G_TIME_SPAN_SECOND - 1)) / G_TIME_SPAN_SECOND;
/* cleanup */
g_date_time_unref (start_of_next);
g_date_time_unref (next);
g_date_time_unref (now);
return seconds;
}
static void
start_timestamp_timer (IdoLocationMenuItem * self)
{
int interval_sec;
gboolean timestamp_shows_seconds;
priv_t * p = self->priv;
const char * const fmt = p->format;
stop_timestamp_timer (self);
timestamp_shows_seconds = fmt && (strstr(fmt,"%s") || strstr(fmt,"%S") ||
strstr(fmt,"%T") || strstr(fmt,"%X") ||
strstr(fmt,"%c"));
if (timestamp_shows_seconds)
interval_sec = 1;
else
interval_sec = calculate_seconds_until_next_minute();
p->timestamp_timer = g_timeout_add_seconds (interval_sec,
on_timestamp_timer,
self);
}
/***
**** GObject Virtual Functions
***/
static void
my_get_property (GObject * o,
guint property_id,
GValue * value,
GParamSpec * pspec)
{
IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (o);
priv_t * p = self->priv;
switch (property_id)
{
case PROP_NAME:
g_value_set_string (value, p->name);
break;
case PROP_TIMEZONE:
g_value_set_string (value, p->timezone);
break;
case PROP_FORMAT:
g_value_set_string (value, p->format);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
break;
}
}
static void
my_set_property (GObject * o,
guint property_id,
const GValue * value,
GParamSpec * pspec)
{
IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (o);
switch (property_id)
{
case PROP_NAME:
ido_location_menu_item_set_name (self, g_value_get_string (value));
break;
case PROP_TIMEZONE:
ido_location_menu_item_set_timezone (self, g_value_get_string (value));
break;
case PROP_FORMAT:
ido_location_menu_item_set_format (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
break;
}
}
static void
my_dispose (GObject * object)
{
stop_timestamp_timer (IDO_LOCATION_MENU_ITEM (object));
G_OBJECT_CLASS (ido_location_menu_item_parent_class)->dispose (object);
}
static void
my_finalize (GObject * object)
{
IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (object);
priv_t * p = self->priv;
g_free (p->format);
g_free (p->name);
g_free (p->timezone);
G_OBJECT_CLASS (ido_location_menu_item_parent_class)->finalize (object);
}
/***
**** Instantiation
***/
static void
ido_location_menu_item_class_init (IdoLocationMenuItemClass *klass)
{
GParamFlags prop_flags;
GObjectClass * gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (IdoLocationMenuItemPrivate));
gobject_class->get_property = my_get_property;
gobject_class->set_property = my_set_property;
gobject_class->dispose = my_dispose;
gobject_class->finalize = my_finalize;
prop_flags = G_PARAM_CONSTRUCT
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS;
properties[PROP_NAME] = g_param_spec_string (
"name",
"The location's name",
"The name to display; eg, 'Oklahoma City'",
"Location",
prop_flags);
properties[PROP_TIMEZONE] = g_param_spec_string (
"timezone",
"timezone identifier",
"string used to identify a timezone; eg, 'America/Chicago'",
NULL,
prop_flags);
properties[PROP_FORMAT] = g_param_spec_string (
"format",
"strftime format",
"strftime-style format string for the timestamp",
"%T",
prop_flags);
g_object_class_install_properties (gobject_class, PROP_LAST, properties);
}
static void
ido_location_menu_item_init (IdoLocationMenuItem *self)
{
priv_t * p;
GtkBox * box;
GtkWidget * w;
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
IDO_LOCATION_MENU_ITEM_TYPE,
IdoLocationMenuItemPrivate);
p = self->priv;
p->name_label = gtk_label_new (NULL);
p->timestamp_label = gtk_label_new (NULL);
w = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 3);
gtk_misc_set_alignment (GTK_MISC(p->timestamp_label), 1.0, 0.5);
box = GTK_BOX (w);
gtk_box_pack_start (box, p->name_label, FALSE, FALSE, 3);
gtk_box_pack_end (box, p->timestamp_label, FALSE, FALSE, 5);
gtk_widget_show_all (w);
gtk_container_add (GTK_CONTAINER (self), w);
}
/***
**** Public API
***/
/* create a new IdoLocationMenuItemType */
GtkWidget *
ido_location_menu_item_new (void)
{
return GTK_WIDGET (g_object_new (IDO_LOCATION_MENU_ITEM_TYPE, NULL));
}
/**
* ido_location_menu_item_set_name:
* @name: human-readable name, such as a city (eg: "Oklahoma City")
*
* Sets this location's name, for display in the menuitem's primary label.
*/
void
ido_location_menu_item_set_name (IdoLocationMenuItem * self,
const char * name)
{
priv_t * p;
g_return_if_fail (IDO_IS_LOCATION_MENU_ITEM (self));
p = self->priv;
g_free (p->name);
p->name = g_strdup (name);
gtk_label_set_text (GTK_LABEL(p->name_label), p->name);
}
/**
* ido_location_menu_item_set_timezone:
* @timezone: timezone identifier (eg: "America/Chicago")
*
* Set this location's timezone. This will be used to show the location's
* current time in menuitem's right-justified secondary label.
*/
void
ido_location_menu_item_set_timezone (IdoLocationMenuItem * self,
const char * timezone)
{
priv_t * p;
g_return_if_fail (IDO_IS_LOCATION_MENU_ITEM (self));
p = self->priv;
g_free (p->timezone);
p->timezone = g_strdup (timezone);
update_timestamp_label (self);
}
/**
* ido_location_menu_item_set_format:
* @format: the format string used when showing the location's time
*
* Set the format string for rendering the location's time
* in its right-justified secondary label.
*
* See strfrtime(3) for more information on the format string.
*/
void
ido_location_menu_item_set_format (IdoLocationMenuItem * self,
const char * format)
{
priv_t * p;
g_return_if_fail (IDO_IS_LOCATION_MENU_ITEM (self));
p = self->priv;
g_free (p->format);
p->format = g_strdup (format);
update_timestamp_label (self);
start_timestamp_timer (self);
}
/**
* ido_location_menu_item_new_from_model:
* @menu_item: the corresponding menuitem
* @actions: action group to tell when this GtkMenuItem is activated
*
* Creates a new IdoLocationMenuItem with properties initialized from
* the menuitem's attributes.
*
* If the menuitem's 'action' attribute is set, trigger that action
* in @actions when this IdoLocationMenuItem is activated.
*/
GtkMenuItem *
ido_location_menu_item_new_from_model (GMenuItem * menu_item,
GActionGroup * actions)
{
guint i;
guint n;
gchar * str;
IdoLocationMenuItem * ido_location;
GParameter parameters[4];
/* create the ido_location */
n = 0;
if (g_menu_item_get_attribute (menu_item, "label", "s", &str))
{
GParameter p = { "name", G_VALUE_INIT };
g_value_init (&p.value, G_TYPE_STRING);
g_value_take_string (&p.value, str);
parameters[n++] = p;
}
if (g_menu_item_get_attribute (menu_item, "x-canonical-timezone", "s", &str))
{
GParameter p = { "timezone", G_VALUE_INIT };
g_value_init (&p.value, G_TYPE_STRING);
g_value_take_string (&p.value, str);
parameters[n++] = p;
}
if (g_menu_item_get_attribute (menu_item, "x-canonical-time-format", "s", &str))
{
GParameter p = { "format", G_VALUE_INIT };
g_value_init (&p.value, G_TYPE_STRING);
g_value_take_string (&p.value, str);
parameters[n++] = p;
}
g_assert (n <= G_N_ELEMENTS (parameters));
ido_location = g_object_newv (IDO_LOCATION_MENU_ITEM_TYPE, n, parameters);
for (i=0; i<n; i++)
g_value_unset (¶meters[i].value);
/* give it an ActionHelper */
if (g_menu_item_get_attribute (menu_item, "action", "s", &str))
{
GVariant * target;
IdoActionHelper * helper;
target = g_menu_item_get_attribute_value (menu_item, "target",
G_VARIANT_TYPE_ANY);
helper = ido_action_helper_new (GTK_WIDGET(ido_location), actions,
str, target);
g_signal_connect_swapped (ido_location, "activate",
G_CALLBACK (ido_action_helper_activate), helper);
g_signal_connect_swapped (ido_location, "destroy",
G_CALLBACK (g_object_unref), helper);
if (target)
g_variant_unref (target);
g_free (str);
}
return GTK_MENU_ITEM (ido_location);
}
|