~mterry/indicator-datetime/update-after-resume

« back to all changes in this revision

Viewing changes to src/indicator-datetime.c

  • Committer: Ted Gould
  • Date: 2011-03-30 21:55:14 UTC
  • mfrom: (49.3.52 resetdate)
  • Revision ID: ted@gould.cx-20110330215514-0ml7ap46mzwcbgob
Fix marking and clean up event handling in the indicator and service

Show diffs side-by-side

added added

removed removed

Lines of Context:
101
101
        GList * timezone_items;
102
102
 
103
103
        GSettings * settings;
 
104
 
 
105
        GtkSizeGroup * indicator_right_group;
104
106
};
105
107
 
106
108
/* Enum for the properties so that they can be quickly
169
171
static void guess_label_size              (IndicatorDatetime * self);
170
172
static void setup_timer                   (IndicatorDatetime * self, GDateTime * datetime);
171
173
static void update_time                   (IndicatorDatetime * self);
 
174
static void session_active_change_cb      (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name, GVariant * parameters, gpointer user_data);
172
175
static void receive_signal                (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name, GVariant * parameters, gpointer user_data);
 
176
static void system_proxy_cb  (GObject * object, GAsyncResult * res, gpointer user_data);
173
177
static void service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data);
174
178
static gint generate_strftime_bitmask     (const char *time_str);
175
179
static void timezone_update_labels        (indicator_item_t * mi_data);
 
180
static gboolean new_calendar_item         (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient   * client, gpointer user_data);
 
181
static gboolean new_appointment_item      (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient * client, gpointer user_data);
 
182
static gboolean new_timezone_item         (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient   * client, gpointer user_data);
176
183
 
177
184
/* Indicator Module Config */
178
185
INDICATOR_SET_VERSION
180
187
 
181
188
G_DEFINE_TYPE (IndicatorDatetime, indicator_datetime, INDICATOR_OBJECT_TYPE);
182
189
 
183
 
static GtkSizeGroup * indicator_right_group = NULL;
184
 
 
185
190
static void
186
191
indicator_datetime_class_init (IndicatorDatetimeClass *klass)
187
192
{
339
344
        }
340
345
 
341
346
        self->priv->sm = indicator_service_manager_new_version(SERVICE_NAME, SERVICE_VERSION);
 
347
        self->priv->indicator_right_group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
 
348
 
 
349
        self->priv->menu = dbusmenu_gtkmenu_new(SERVICE_NAME, MENU_OBJ);
 
350
 
 
351
        DbusmenuGtkClient *client = dbusmenu_gtkmenu_get_client(self->priv->menu);
 
352
        dbusmenu_client_add_type_handler_full(DBUSMENU_CLIENT(client), DBUSMENU_CALENDAR_MENUITEM_TYPE, new_calendar_item, self, NULL);
 
353
        dbusmenu_client_add_type_handler_full(DBUSMENU_CLIENT(client), APPOINTMENT_MENUITEM_TYPE, new_appointment_item, self, NULL);
 
354
        dbusmenu_client_add_type_handler_full(DBUSMENU_CLIENT(client), TIMEZONE_MENUITEM_TYPE, new_timezone_item, self, NULL);
342
355
 
343
356
        self->priv->service_proxy_cancel = g_cancellable_new();
344
357
 
352
365
                                  service_proxy_cb,
353
366
                                  self);
354
367
 
 
368
        g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
 
369
                                  G_DBUS_PROXY_FLAGS_NONE,
 
370
                                  NULL,
 
371
                                  "org.freedesktop.ConsoleKit",
 
372
                                  "/org/freedesktop/ConsoleKit/Manager",
 
373
                                  "org.freedesktop.ConsoleKit.Manager",
 
374
                                  NULL, system_proxy_cb, self);
355
375
        return;
356
376
}
 
377
/* for hooking into console kit signal on wake from suspend */
 
378
static void
 
379
system_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data)
 
380
{
 
381
        GError * error = NULL;
 
382
        
 
383
        IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
 
384
        g_return_if_fail(self != NULL);
 
385
        
 
386
        GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);
 
387
 
 
388
        if (error != NULL) {
 
389
                g_warning("Could not grab DBus proxy for %s: %s", SERVICE_NAME, error->message);
 
390
                g_error_free(error);
 
391
                return;
 
392
        }
 
393
        g_signal_connect(proxy, "g-signal", G_CALLBACK(session_active_change_cb), self);
 
394
 
 
395
}
357
396
 
358
397
/* Callback from trying to create the proxy for the serivce, this
359
398
   could include starting the service.  Sometime it'll fail and
430
469
                self->priv->service_proxy = NULL;
431
470
        }
432
471
 
 
472
        if (self->priv->indicator_right_group != NULL) {
 
473
                g_object_unref(G_OBJECT(self->priv->indicator_right_group));
 
474
                self->priv->indicator_right_group = NULL;
 
475
        }
 
476
 
433
477
        G_OBJECT_CLASS (indicator_datetime_parent_class)->dispose (object);
434
478
        return;
435
479
}
512
556
        case PROP_SHOW_CLOCK: {
513
557
                if (g_value_get_boolean(value) != self->priv->show_clock) {
514
558
                        self->priv->show_clock = g_value_get_boolean(value);
515
 
                        gtk_widget_set_visible (GTK_WIDGET (self->priv->label), self->priv->show_clock);
 
559
                        if (self->priv->label != NULL) {
 
560
                                gtk_widget_set_visible (GTK_WIDGET (self->priv->label), self->priv->show_clock);
 
561
                        }
516
562
                }
517
563
                break;
518
564
        }
585
631
        case PROP_SHOW_CALENDAR: {
586
632
                if (g_value_get_boolean(value) != self->priv->show_calendar) {
587
633
                        self->priv->show_calendar = g_value_get_boolean(value);
588
 
                        gtk_widget_set_visible (GTK_WIDGET (self->priv->ido_calendar), self->priv->show_calendar);
 
634
                        if (self->priv->ido_calendar != NULL) {
 
635
                                gtk_widget_set_visible (GTK_WIDGET (self->priv->ido_calendar), self->priv->show_calendar);
 
636
                        }
589
637
                }
590
638
                break;
591
639
        } 
776
824
        return;
777
825
}
778
826
 
 
827
static void
 
828
session_active_change_cb (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name,
 
829
                GVariant * parameters, gpointer user_data)
 
830
{
 
831
        // Just returned from suspend
 
832
        IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
 
833
        if (g_strcmp0(signal_name, "ActiveChanged") == 0) {
 
834
                update_time(self);
 
835
        }
 
836
        return;
 
837
}
 
838
 
779
839
/* Receives all signals from the service, routed to the appropriate functions */
780
840
static void
781
841
receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name,
1148
1208
                timezone_update_labels(mi_data);
1149
1209
        } else if (!g_strcmp0(prop, TIMEZONE_MENUITEM_PROP_RADIO)) {
1150
1210
                gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mi_data->gmi), g_variant_get_boolean(value));
1151
 
                
1152
 
        // Properties for marking and unmarking the calendar
1153
 
        
1154
 
        } else if (!g_strcmp0(prop, CALENDAR_MENUITEM_PROP_MARK)) {
1155
 
                ido_calendar_menu_item_mark_day (IDO_CALENDAR_MENU_ITEM (mi_data), g_variant_get_int16(value));
1156
 
        } else if (!g_strcmp0(prop, CALENDAR_MENUITEM_PROP_UNMARK)) {
1157
 
                ido_calendar_menu_item_unmark_day (IDO_CALENDAR_MENU_ITEM (mi_data), g_variant_get_int16(value));
1158
 
        } else if (!g_strcmp0(prop, CALENDAR_MENUITEM_PROP_CLEAR_MARKS)) {
 
1211
        }
 
1212
        return;
 
1213
}
 
1214
// Properties for marking and unmarking the calendar
 
1215
static void
 
1216
calendar_prop_change_cb (DbusmenuMenuitem * mi, gchar * prop, GVariant *value, IdoCalendarMenuItem * mi_data)
 
1217
{
 
1218
        g_debug("Changing calendar property: %s", prop);
 
1219
        if (!g_strcmp0(prop, CALENDAR_MENUITEM_PROP_MARKS)) {
1159
1220
                ido_calendar_menu_item_clear_marks (IDO_CALENDAR_MENU_ITEM (mi_data));
 
1221
 
 
1222
                if (value != NULL) {
 
1223
                        GVariantIter *iter;
 
1224
                        gint day;
 
1225
 
 
1226
                        g_debug("\tMarks: %s", g_variant_print(value, FALSE));
 
1227
 
 
1228
                        g_variant_get (value, "ai", &iter);
 
1229
                        while (g_variant_iter_loop (iter, "i", &day)) {
 
1230
                                ido_calendar_menu_item_mark_day (IDO_CALENDAR_MENU_ITEM (mi_data), day);
 
1231
                        }
 
1232
                        g_variant_iter_free (iter);
 
1233
                } else {
 
1234
                        g_debug("\tMarks: <cleared>");
 
1235
                }
1160
1236
        } else if (!g_strcmp0(prop, CALENDAR_MENUITEM_PROP_SET_DATE)) {
1161
 
                gsize size = 3;
1162
 
                const gint * array = g_variant_get_fixed_array(value, &size, sizeof(gint));
1163
 
                ido_calendar_menu_item_set_date (IDO_CALENDAR_MENU_ITEM (mi_data), array[0], array[1], array[2]);
1164
 
        } else {
1165
 
                g_warning("Indicator Item property '%s' unknown", prop);
 
1237
                if (value != NULL) {
 
1238
                        gsize size = 3;
 
1239
                        const gint * array = g_variant_get_fixed_array(value, &size, sizeof(gint));
 
1240
                        g_debug("Setting date y-m-d: %d-%d-%d", array[0], array[1], array[2]);
 
1241
                        ido_calendar_menu_item_set_date (IDO_CALENDAR_MENU_ITEM (mi_data), array[0], array[1], array[2]);
 
1242
                }
1166
1243
        }
1167
1244
        return;
1168
1245
}
1178
1255
{
1179
1256
        g_return_val_if_fail(DBUSMENU_IS_MENUITEM(newitem), FALSE);
1180
1257
        g_return_val_if_fail(DBUSMENU_IS_GTKCLIENT(client), FALSE);
 
1258
        g_return_val_if_fail(IS_INDICATOR_DATETIME(user_data), FALSE);
1181
1259
        /* Note: not checking parent, it's reasonable for it to be NULL */
 
1260
        IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
1182
1261
 
1183
1262
        indicator_item_t * mi_data = g_new0(indicator_item_t, 1);
1184
1263
 
1228
1307
        /* Usually either the time or the count on the individual
1229
1308
           item. */
1230
1309
        mi_data->right = gtk_label_new(dbusmenu_menuitem_property_get(newitem, APPOINTMENT_MENUITEM_PROP_RIGHT));
1231
 
        gtk_size_group_add_widget(indicator_right_group, mi_data->right);
 
1310
        gtk_size_group_add_widget(self->priv->indicator_right_group, mi_data->right);
1232
1311
        gtk_misc_set_alignment(GTK_MISC(mi_data->right), 1.0, 0.5);
1233
1312
        gtk_box_pack_start(GTK_BOX(hbox), mi_data->right, FALSE, FALSE, 0);
1234
1313
        gtk_widget_show(mi_data->right);
1239
1318
        dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client), newitem, GTK_MENU_ITEM(mi_data->gmi), parent);
1240
1319
 
1241
1320
        g_signal_connect(G_OBJECT(newitem), DBUSMENU_MENUITEM_SIGNAL_PROPERTY_CHANGED, G_CALLBACK(indicator_prop_change_cb), mi_data);
1242
 
        g_signal_connect_swapped(G_OBJECT(newitem), "destroyed", G_CALLBACK(g_free), mi_data);
1243
 
 
1244
1321
        return TRUE;
1245
1322
}
1246
1323
 
1304
1381
                                   DbusmenuClient   * client,
1305
1382
                                   gpointer           user_data)
1306
1383
{
 
1384
        g_debug("New calendar item");
1307
1385
        g_return_val_if_fail(DBUSMENU_IS_MENUITEM(newitem), FALSE);
1308
1386
        g_return_val_if_fail(DBUSMENU_IS_GTKCLIENT(client), FALSE);
 
1387
        g_return_val_if_fail(IS_INDICATOR_DATETIME(user_data), FALSE);
1309
1388
        /* Note: not checking parent, it's reasonable for it to be NULL */
1310
1389
 
1311
 
        IndicatorObject *io = g_object_get_data (G_OBJECT (client), "indicator");
1312
 
        if (io == NULL) {
1313
 
                g_warning ("found no indicator to attach the caledar to");
1314
 
                return FALSE;
1315
 
        }
1316
 
 
1317
 
        IndicatorDatetime *self = INDICATOR_DATETIME(io);
 
1390
        IndicatorDatetime *self = INDICATOR_DATETIME(user_data);
1318
1391
        self->priv = INDICATOR_DATETIME_GET_PRIVATE(self);
1319
1392
        
1320
1393
        IdoCalendarMenuItem *ido = IDO_CALENDAR_MENU_ITEM (ido_calendar_menu_item_new ());
1330
1403
        gtk_widget_set_visible (GTK_WIDGET (self->priv->ido_calendar), self->priv->show_calendar);
1331
1404
 
1332
1405
        dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client), newitem, GTK_MENU_ITEM(ido), parent);
 
1406
 
1333
1407
        g_signal_connect_after(ido, "month-changed", G_CALLBACK(month_changed_cb), (gpointer)newitem);
1334
1408
        g_signal_connect_after(ido, "day-selected", G_CALLBACK(day_selected_cb), (gpointer)newitem);
1335
1409
        g_signal_connect_after(ido, "day-selected-double-click", G_CALLBACK(day_selected_double_click_cb), (gpointer)newitem);
1336
1410
 
 
1411
        g_signal_connect(G_OBJECT(newitem), DBUSMENU_MENUITEM_SIGNAL_PROPERTY_CHANGED, G_CALLBACK(calendar_prop_change_cb), ido);
 
1412
 
 
1413
        /* Run the current values through prop changed */
 
1414
        GVariant * propval = NULL;
 
1415
 
 
1416
        propval = dbusmenu_menuitem_property_get_variant(newitem, CALENDAR_MENUITEM_PROP_MARKS);
 
1417
        if (propval != NULL) {
 
1418
                calendar_prop_change_cb(newitem, CALENDAR_MENUITEM_PROP_MARKS, propval, ido);
 
1419
        }
 
1420
 
 
1421
        propval = dbusmenu_menuitem_property_get_variant(newitem, CALENDAR_MENUITEM_PROP_SET_DATE);
 
1422
        if (propval != NULL) {
 
1423
                calendar_prop_change_cb(newitem, CALENDAR_MENUITEM_PROP_SET_DATE, propval, ido);
 
1424
        }
 
1425
 
1337
1426
        return TRUE;
1338
1427
}
1339
1428
 
1363
1452
{
1364
1453
        g_return_val_if_fail(DBUSMENU_IS_MENUITEM(newitem), FALSE);
1365
1454
        g_return_val_if_fail(DBUSMENU_IS_GTKCLIENT(client), FALSE);
 
1455
        g_return_val_if_fail(IS_INDICATOR_DATETIME(user_data), FALSE);
1366
1456
        /* Note: not checking parent, it's reasonable for it to be NULL */
1367
 
        
1368
 
        IndicatorObject *io = g_object_get_data (G_OBJECT (client), "indicator");
1369
 
        if (io == NULL) {
1370
 
                g_warning ("found no indicator to attach the timezone to");
1371
 
                return FALSE;
1372
 
        }
1373
1457
 
1374
 
        IndicatorDatetime *self = INDICATOR_DATETIME(io);
 
1458
        IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
1375
1459
        IndicatorDatetimePrivate *priv = INDICATOR_DATETIME_GET_PRIVATE(self);
1376
1460
 
1377
1461
        // Menu item with a radio button and a right aligned time
1398
1482
        /* Usually either the time or the count on the individual
1399
1483
           item. */
1400
1484
        mi_data->right = gtk_label_new("");
1401
 
        gtk_size_group_add_widget(indicator_right_group, mi_data->right);
 
1485
        gtk_size_group_add_widget(self->priv->indicator_right_group, mi_data->right);
1402
1486
        gtk_misc_set_alignment(GTK_MISC(mi_data->right), 1.0, 0.5);
1403
1487
        gtk_box_pack_start(GTK_BOX(hbox), mi_data->right, FALSE, FALSE, 0);
1404
1488
        gtk_widget_show(mi_data->right);
1448
1532
{
1449
1533
        IndicatorDatetime * self = INDICATOR_DATETIME(io);
1450
1534
 
1451
 
        if (self->priv->menu == NULL) {
1452
 
                self->priv->menu = dbusmenu_gtkmenu_new(SERVICE_NAME, MENU_OBJ);
1453
 
        }
1454
 
 
1455
 
        DbusmenuGtkClient *client = dbusmenu_gtkmenu_get_client(self->priv->menu);
1456
 
        g_object_set_data (G_OBJECT (client), "indicator", io);
1457
 
 
1458
 
        dbusmenu_client_add_type_handler(DBUSMENU_CLIENT(client), DBUSMENU_CALENDAR_MENUITEM_TYPE, new_calendar_item);
1459
 
        dbusmenu_client_add_type_handler(DBUSMENU_CLIENT(client), APPOINTMENT_MENUITEM_TYPE, new_appointment_item);
1460
 
        dbusmenu_client_add_type_handler(DBUSMENU_CLIENT(client), TIMEZONE_MENUITEM_TYPE, new_timezone_item);
1461
 
 
1462
1535
        return GTK_MENU(self->priv->menu);
1463
1536
}
1464
1537