~larsu/ido/center-user-avatar

« back to all changes in this revision

Viewing changes to src/idolocationmenuitem.c

  • Committer: Tarmac
  • Author(s): Charles Kerr
  • Date: 2013-07-30 22:17:45 UTC
  • mfrom: (142.1.12 add-progress-menu-item)
  • Revision ID: tarmac-20130730221745-i1g9mg16zgu5wmys
Support x-canonical-type: "com.canonical.indicator.progress" and "com.canonical.indicator.alarm".

Approved by Ted Gould, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
enum
32
32
{
33
33
  PROP_0,
34
 
  PROP_NAME,
35
34
  PROP_TIMEZONE,
36
 
  PROP_FORMAT,
37
35
  PROP_LAST
38
36
};
39
37
 
41
39
 
42
40
struct _IdoLocationMenuItemPrivate
43
41
{
44
 
  char * name;
45
42
  char * timezone;
46
 
  char * format;
47
43
 
48
44
  guint timestamp_timer;
49
 
 
50
 
  GtkWidget * name_label;
51
 
  GtkWidget * timestamp_label;
52
45
};
53
46
 
54
47
typedef IdoLocationMenuItemPrivate priv_t;
55
48
 
56
 
G_DEFINE_TYPE (IdoLocationMenuItem, ido_location_menu_item, GTK_TYPE_MENU_ITEM);
 
49
G_DEFINE_TYPE (IdoLocationMenuItem, ido_location_menu_item, IDO_TYPE_TIME_STAMP_MENU_ITEM);
57
50
 
58
51
/***
59
52
****  Timestamp Label
60
53
***/
61
54
 
62
55
static void
63
 
update_timestamp_label (IdoLocationMenuItem * self)
 
56
update_timestamp (IdoLocationMenuItem * self)
64
57
{
65
 
  priv_t * p = self->priv;
66
 
 
67
 
  if (p->format && *p->format)
68
 
    {
69
 
      GTimeZone * tz;
70
 
      GDateTime * now;
71
 
      char * str;
72
 
 
73
 
      tz = g_time_zone_new (p->timezone);
74
 
      if (tz == NULL)
75
 
        tz = g_time_zone_new_local ();
76
 
      now = g_date_time_new_now (tz);
77
 
      str = g_date_time_format (now, p->format);
78
 
 
79
 
      gtk_label_set_text (GTK_LABEL(p->timestamp_label), str);
80
 
 
81
 
      g_free (str);
82
 
      g_date_time_unref (now);
83
 
      g_time_zone_unref (tz);
84
 
    }
85
 
  else
86
 
    {
87
 
      gtk_label_set_text (GTK_LABEL(p->timestamp_label), "");
88
 
    }
 
58
  GTimeZone * tz;
 
59
  GDateTime * date_time;
 
60
 
 
61
  tz = g_time_zone_new (self->priv->timezone);
 
62
  if (tz == NULL)
 
63
    tz = g_time_zone_new_local ();
 
64
  date_time = g_date_time_new_now (tz);
 
65
 
 
66
  ido_time_stamp_menu_item_set_date_time (IDO_TIME_STAMP_MENU_ITEM(self),
 
67
                                          date_time);
 
68
 
 
69
  g_date_time_unref (date_time);
 
70
  g_time_zone_unref (tz);
89
71
}
90
72
 
91
73
static void
100
82
    }
101
83
}
102
84
 
103
 
static void start_timestamp_timer (IdoLocationMenuItem * self);
 
85
static void restart_timestamp_timer (IdoLocationMenuItem * self);
104
86
 
105
87
static gboolean
106
88
on_timestamp_timer (gpointer gself)
107
89
{
108
90
  IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (gself);
109
91
 
110
 
  update_timestamp_label (self);
 
92
  update_timestamp (self);
111
93
 
112
 
  start_timestamp_timer (self);
 
94
  restart_timestamp_timer (self);
113
95
  return G_SOURCE_REMOVE;
114
96
}
115
97
 
143
125
}
144
126
 
145
127
static void
146
 
start_timestamp_timer (IdoLocationMenuItem * self)
 
128
restart_timestamp_timer (IdoLocationMenuItem * self)
147
129
{
 
130
  const char * fmt = ido_time_stamp_menu_item_get_format (IDO_TIME_STAMP_MENU_ITEM (self));
 
131
  gboolean timestamp_shows_seconds;
148
132
  int interval_sec;
149
 
  gboolean timestamp_shows_seconds;
150
 
  priv_t * p = self->priv;
151
 
  const char * const fmt = p->format;
152
133
 
153
134
  stop_timestamp_timer (self);
154
135
 
161
142
  else
162
143
    interval_sec = calculate_seconds_until_next_minute();
163
144
 
164
 
  p->timestamp_timer = g_timeout_add_seconds (interval_sec,
165
 
                                              on_timestamp_timer,
166
 
                                              self);
 
145
  self->priv->timestamp_timer = g_timeout_add_seconds (interval_sec,
 
146
                                                       on_timestamp_timer,
 
147
                                                       self);
167
148
}
168
149
 
169
150
/***
181
162
 
182
163
  switch (property_id)
183
164
    {
184
 
      case PROP_NAME:
185
 
        g_value_set_string (value, p->name);
186
 
        break;
187
 
 
188
165
      case PROP_TIMEZONE:
189
166
        g_value_set_string (value, p->timezone);
190
167
        break;
191
168
 
192
 
      case PROP_FORMAT:
193
 
        g_value_set_string (value, p->format);
194
 
        break;
195
 
 
196
169
      default:
197
170
        G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
198
171
        break;
209
182
 
210
183
  switch (property_id)
211
184
    {
212
 
      case PROP_NAME:
213
 
        ido_location_menu_item_set_name (self, g_value_get_string (value));
214
 
        break;
215
 
 
216
185
      case PROP_TIMEZONE:
217
186
        ido_location_menu_item_set_timezone (self, g_value_get_string (value));
218
187
        break;
219
188
 
220
 
      case PROP_FORMAT:
221
 
        ido_location_menu_item_set_format (self, g_value_get_string (value));
222
 
        break;
223
 
 
224
189
      default:
225
190
        G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
226
191
        break;
239
204
my_finalize (GObject * object)
240
205
{
241
206
  IdoLocationMenuItem * self = IDO_LOCATION_MENU_ITEM (object);
242
 
  priv_t * p = self->priv;
243
207
 
244
 
  g_free (p->format);
245
 
  g_free (p->name);
246
 
  g_free (p->timezone);
 
208
  g_free (self->priv->timezone);
247
209
 
248
210
  G_OBJECT_CLASS (ido_location_menu_item_parent_class)->finalize (object);
249
211
}
255
217
static void
256
218
ido_location_menu_item_class_init (IdoLocationMenuItemClass *klass)
257
219
{
258
 
  GParamFlags prop_flags;
259
220
  GObjectClass * gobject_class = G_OBJECT_CLASS (klass);
260
221
 
261
222
  g_type_class_add_private (klass, sizeof (IdoLocationMenuItemPrivate));
265
226
  gobject_class->dispose = my_dispose;
266
227
  gobject_class->finalize = my_finalize;
267
228
 
268
 
  prop_flags = G_PARAM_CONSTRUCT
269
 
             | G_PARAM_READWRITE
270
 
             | G_PARAM_STATIC_STRINGS;
271
 
 
272
 
  properties[PROP_NAME] = g_param_spec_string (
273
 
    "name",
274
 
    "The location's name",
275
 
    "The name to display; eg, 'Oklahoma City'",
276
 
    "Location",
277
 
    prop_flags);
278
 
 
279
229
  properties[PROP_TIMEZONE] = g_param_spec_string (
280
230
    "timezone",
281
231
    "timezone identifier",
282
232
    "string used to identify a timezone; eg, 'America/Chicago'",
283
233
    NULL,
284
 
    prop_flags);
285
 
 
286
 
  properties[PROP_FORMAT] = g_param_spec_string (
287
 
    "format",
288
 
    "strftime format",
289
 
    "strftime-style format string for the timestamp",
290
 
    "%T",
291
 
    prop_flags);
 
234
    G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
292
235
 
293
236
  g_object_class_install_properties (gobject_class, PROP_LAST, properties);
294
237
}
296
239
static void
297
240
ido_location_menu_item_init (IdoLocationMenuItem *self)
298
241
{
299
 
  priv_t * p;
300
 
  GtkWidget * w;
301
 
  GtkGrid * grid;
302
 
 
303
242
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
304
243
                                            IDO_LOCATION_MENU_ITEM_TYPE,
305
244
                                            IdoLocationMenuItemPrivate);
306
245
 
307
 
  p = self->priv;
308
 
 
309
 
  p->name_label = gtk_label_new (NULL);
310
 
  gtk_misc_set_alignment (GTK_MISC(p->name_label), 0.0, 0.5);
311
 
 
312
 
  p->timestamp_label = gtk_label_new (NULL);
313
 
  gtk_misc_set_alignment (GTK_MISC(p->timestamp_label), 1.0, 0.5);
314
 
 
315
 
  w = gtk_grid_new ();
316
 
  grid = GTK_GRID (w);
317
 
  gtk_grid_attach (grid, p->name_label, 0, 0, 1, 1);
318
 
  gtk_grid_attach (grid, p->timestamp_label, 1, 0, 1, 1);
319
 
  g_object_set (p->name_label,
320
 
                "halign", GTK_ALIGN_START,
321
 
                "hexpand", TRUE,
322
 
                "margin-right", 6,
323
 
                "valign", GTK_ALIGN_CENTER,
324
 
                NULL);
325
 
  g_object_set (p->timestamp_label,
326
 
                "halign", GTK_ALIGN_END,
327
 
                "hexpand", FALSE,
328
 
                "valign", GTK_ALIGN_CENTER,
329
 
                NULL);
330
 
 
331
 
  gtk_widget_show_all (w);
332
 
  gtk_container_add (GTK_CONTAINER (self), w);
 
246
  /* Update the timer whenever the format string changes
 
247
     because it determines whether we update once per second or per minute */
 
248
  g_signal_connect (self, "notify::format",
 
249
                    G_CALLBACK(restart_timestamp_timer), NULL);
333
250
}
334
251
 
335
 
 
336
252
/***
337
253
****  Public API
338
254
***/
345
261
}
346
262
 
347
263
/**
348
 
 * ido_location_menu_item_set_name:
349
 
 * @name: human-readable name, such as a city (eg: "Oklahoma City")
350
 
 *
351
 
 * Sets this location's name, for display in the menuitem's primary label.
352
 
 */
353
 
void
354
 
ido_location_menu_item_set_name (IdoLocationMenuItem * self,
355
 
                                 const char          * name)
356
 
{
357
 
  priv_t * p;
358
 
 
359
 
  g_return_if_fail (IDO_IS_LOCATION_MENU_ITEM (self));
360
 
  p = self->priv;
361
 
 
362
 
  g_free (p->name);
363
 
  p->name = g_strdup (name);
364
 
  gtk_label_set_text (GTK_LABEL(p->name_label), p->name);
365
 
}
366
 
 
367
 
/**
368
264
 * ido_location_menu_item_set_timezone:
369
265
 * @timezone: timezone identifier (eg: "America/Chicago")
370
266
 *
382
278
 
383
279
  g_free (p->timezone);
384
280
  p->timezone = g_strdup (timezone);
385
 
  update_timestamp_label (self);
386
 
}
387
 
 
388
 
/**
389
 
 * ido_location_menu_item_set_format:
390
 
 * @format: the format string used when showing the location's time
391
 
 *
392
 
 * Set the format string for rendering the location's time
393
 
 * in its right-justified secondary label.
394
 
 *
395
 
 * See strfrtime(3) for more information on the format string.
396
 
 */
397
 
void
398
 
ido_location_menu_item_set_format (IdoLocationMenuItem   * self,
399
 
                                   const char            * format)
400
 
{
401
 
  priv_t * p;
402
 
 
403
 
  g_return_if_fail (IDO_IS_LOCATION_MENU_ITEM (self));
404
 
  p = self->priv;
405
 
 
406
 
  g_free (p->format);
407
 
  p->format = g_strdup (format);
408
 
  update_timestamp_label (self);
409
 
  start_timestamp_timer (self);
 
281
  update_timestamp (self);
410
282
}
411
283
 
412
284
/**
436
308
 
437
309
  if (g_menu_item_get_attribute (menu_item, "label", "s", &str))
438
310
    {
439
 
      GParameter p = { "name", G_VALUE_INIT };
 
311
      GParameter p = { "text", G_VALUE_INIT };
440
312
      g_value_init (&p.value, G_TYPE_STRING);
441
313
      g_value_take_string (&p.value, str);
442
314
      parameters[n++] = p;