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

« back to all changes in this revision

Viewing changes to src/datetime-service.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:
276
276
}
277
277
 
278
278
static gboolean
 
279
update_appointment_menu_items_idle (gpointer user_data)
 
280
{
 
281
        update_appointment_menu_items(user_data);
 
282
        return FALSE;
 
283
}
 
284
 
 
285
static gboolean
279
286
month_changed_cb (DbusmenuMenuitem * menuitem, gchar *name, GVariant *variant, guint timestamp)
280
287
{
281
288
        start_time_appointments = (time_t)g_variant_get_uint32(variant);
282
289
        
283
290
        g_debug("Received month changed with timestamp: %d -> %s",(int)start_time_appointments, ctime(&start_time_appointments));       
284
 
        update_appointment_menu_items(NULL);
 
291
        /* By default one of the first things we do is
 
292
           clear the marks as we don't know the correct
 
293
           ones yet and we don't want to confuse the
 
294
           user. */
 
295
        dbusmenu_menuitem_property_remove(menuitem, CALENDAR_MENUITEM_PROP_MARKS);
 
296
 
 
297
        GList * appointment;
 
298
        for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
 
299
                DbusmenuMenuitem * mi = DBUSMENU_MENUITEM(appointment->data);
 
300
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
 
301
        }
 
302
 
 
303
        g_idle_add(update_appointment_menu_items_idle, NULL);
285
304
        return TRUE;
286
305
}
287
306
 
288
307
static gboolean
289
308
day_selected_cb (DbusmenuMenuitem * menuitem, gchar *name, GVariant *variant, guint timestamp)
290
309
{
291
 
        start_time_appointments = (time_t)g_variant_get_uint32(variant);
292
 
        
 
310
        time_t new_time = (time_t)g_variant_get_uint32(variant);
 
311
        g_warn_if_fail(new_time != 0);
 
312
 
 
313
        if (start_time_appointments == 0 || new_time == 0) {
 
314
                /* If we've got nothing, assume everyhting is going to
 
315
                   get repopulated, let's start with a clean slate */
 
316
                dbusmenu_menuitem_property_remove(menuitem, CALENDAR_MENUITEM_PROP_MARKS);
 
317
        } else {
 
318
                /* No check to see if we changed months.  If we did we'll
 
319
                   want to clear the marks.  Otherwise we're cool keeping
 
320
                   them around. */
 
321
                struct tm start_tm;
 
322
                struct tm new_tm;
 
323
 
 
324
                localtime_r(&start_time_appointments, &start_tm);
 
325
                localtime_r(&new_time, &new_tm);
 
326
 
 
327
                if (start_tm.tm_mon != new_tm.tm_mon) {
 
328
                        dbusmenu_menuitem_property_remove(menuitem, CALENDAR_MENUITEM_PROP_MARKS);
 
329
                }
 
330
        }
 
331
 
 
332
        GList * appointment;
 
333
        for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
 
334
                DbusmenuMenuitem * mi = DBUSMENU_MENUITEM(appointment->data);
 
335
                dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
 
336
        }
 
337
 
 
338
        start_time_appointments = new_time;
 
339
 
293
340
        g_debug("Received day-selected with timestamp: %d -> %s",(int)start_time_appointments, ctime(&start_time_appointments));        
294
 
        update_appointment_menu_items(NULL);
 
341
        g_idle_add(update_appointment_menu_items_idle, NULL);
 
342
 
295
343
        return TRUE;
296
344
}
297
345
 
316
364
        return TRUE;
317
365
}
318
366
 
 
367
static gboolean
 
368
close_menu_cb (DbusmenuMenuitem * menuitem, gchar *name, GVariant *variant) 
 
369
{
 
370
        if (calendar == NULL) return FALSE;
 
371
        g_debug("Resetting date on menu close");
 
372
        start_time_appointments = 0;
 
373
        // TODO create a variant which will be an array of 3 ints {y,m,d}
 
374
        GVariant *date_variant;
 
375
        time_t curtime;
 
376
        struct tm *t1;
 
377
        time(&curtime);
 
378
        t1 = localtime(&curtime);
 
379
        GVariant *date[3];
 
380
        date[0] = g_variant_new_uint32(t1->tm_year + 1900);
 
381
        date[1] = g_variant_new_uint32(t1->tm_mon);
 
382
        date[2] = g_variant_new_uint32(t1->tm_mday);
 
383
        date_variant = g_variant_new_array(NULL, date, 3);
 
384
        
 
385
        dbusmenu_menuitem_property_set_variant (calendar, CALENDAR_MENUITEM_PROP_SET_DATE, date_variant);
 
386
        return TRUE;
 
387
}
 
388
 
319
389
static guint ecaltimer = 0;
320
390
 
321
391
static void
331
401
{
332
402
        if (ecaltimer != 0) g_source_remove(ecaltimer);
333
403
}
 
404
static gboolean
 
405
idle_start_ecal_timer (gpointer data)
 
406
{
 
407
        start_ecal_timer();
 
408
        return FALSE;
 
409
}
334
410
 
335
411
static void
336
412
show_events_changed (void)
344
420
                dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
345
421
                /* Remove all of the previous appointments */
346
422
                if (appointments != NULL) {
347
 
                        g_debug("Freeing old appointments");
348
 
                        while (appointments != NULL) {
349
 
                                DbusmenuMenuitem * litem =  DBUSMENU_MENUITEM(appointments->data);
350
 
                                g_debug("Freeing old appointment: %p", litem);
 
423
                        g_debug("Hiding old appointments");
 
424
                        GList * appointment;
 
425
                        for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
 
426
                                DbusmenuMenuitem * litem =  DBUSMENU_MENUITEM(appointment->data);
 
427
                                g_debug("Hiding old appointment: %p", litem);
351
428
                                // Remove all the existing menu items which are in appointments.
352
 
                                appointments = g_list_remove(appointments, litem);
353
 
                                dbusmenu_menuitem_child_delete(root, DBUSMENU_MENUITEM(litem));
354
 
                                g_object_unref(G_OBJECT(litem));
 
429
                                dbusmenu_menuitem_property_set_bool(DBUSMENU_MENUITEM(litem), DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
355
430
                        }
356
431
                }
357
432
                stop_ecal_timer();
389
464
                if (g_settings_get_boolean(conf, SETTINGS_SHOW_EVENTS_S)) {
390
465
                        dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
391
466
                        dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
392
 
                        start_ecal_timer();
 
467
                        g_idle_add((GSourceFunc)idle_start_ecal_timer, NULL);
393
468
                } else {
394
469
                        dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
395
470
                        dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
580
655
        const int mday = today->tm_mday;
581
656
        const int mon = today->tm_mon;
582
657
        const int year = today->tm_year;
583
 
        
 
658
 
584
659
        struct tm *start_tm = NULL;
585
660
        int this_year = today->tm_year + 1900;
586
661
        int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
600
675
                        month_start.tm_mon = start_tm->tm_mon;
601
676
                        month_start.tm_mday = 1;
602
677
                        t1 = mktime(&month_start);
603
 
                        highlightdays = days[mon];
 
678
                        highlightdays = days[start_month];
604
679
                }
605
680
        }
606
681
        
607
682
        g_debug("Will highlight %d days from %s", highlightdays, ctime(&t1));
608
683
 
609
 
        t2 = t1 + (time_t) (highlightdays * 24 * 60 * 60); 
610
 
        
611
 
        // Remove all highlights from the calendar widget
612
 
        dbusmenu_menuitem_property_set (calendar, CALENDAR_MENUITEM_PROP_CLEAR_MARKS, NULL);
 
684
        t2 = t1 + (time_t) (highlightdays * 24 * 60 * 60);
613
685
        
614
686
        if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, &gerror)) {
615
687
                g_debug("Failed to get ecal sources\n");
653
725
        comp_instances = NULL;
654
726
        g_debug("Components sorted");
655
727
        
656
 
        /* Remove all of the previous appointments */
 
728
        /* Hiding all of the previous appointments */
657
729
        if (appointments != NULL) {
658
 
                g_debug("Freeing old appointments");
659
 
                while (appointments != NULL) {
660
 
                        DbusmenuMenuitem * litem =  DBUSMENU_MENUITEM(appointments->data);
661
 
                        g_debug("Freeing old appointment: %p", litem);
 
730
                g_debug("Hiding old appointments");
 
731
                GList * appointment;
 
732
                for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
 
733
                        DbusmenuMenuitem * litem =  DBUSMENU_MENUITEM(appointment->data);
 
734
                        g_debug("Hiding old appointment: %p", litem);
662
735
                        // Remove all the existing menu items which are in appointments.
663
 
                        appointments = g_list_remove(appointments, litem);
664
 
                        dbusmenu_menuitem_child_delete(root, DBUSMENU_MENUITEM(litem));
665
 
                        g_object_unref(G_OBJECT(litem));
 
736
                        dbusmenu_menuitem_property_set_bool(DBUSMENU_MENUITEM(litem), DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
666
737
                }
667
738
        }
668
739
 
679
750
        } else if (g_strcmp0(time_format_str, "24-hour") == 0) {
680
751
                apt_output = SETTINGS_TIME_24_HOUR;
681
752
        } else {
682
 
                apt_output = SETTINGS_TIME_LOCALE;
 
753
                if (is_locale_12h()) {
 
754
                        apt_output = SETTINGS_TIME_12_HOUR;
 
755
                } else {
 
756
                        apt_output = SETTINGS_TIME_24_HOUR;
 
757
                }
683
758
        }
684
759
        
 
760
        GVariantBuilder markeddays;
 
761
        g_variant_builder_init (&markeddays, G_VARIANT_TYPE_ARRAY);
 
762
        
685
763
        i = 0;
 
764
        GList * cached_appointment = appointments;
686
765
        for (l = sorted_comp_instances; l; l = l->next) {
687
766
                struct comp_instance *ci = l->data;
688
767
                ECalComponent *ecalcomp = ci->comp;
703
782
                const int dyear = due->tm_year;
704
783
                
705
784
                // Mark day
706
 
                g_debug("Marking date %s", ctime(&ci->start));
707
 
                dbusmenu_menuitem_property_set_int (calendar, CALENDAR_MENUITEM_PROP_MARK, due->tm_mday);
708
 
 
 
785
                g_debug("Adding marked date %s, %d", ctime(&ci->start), dmday);
 
786
                g_variant_builder_add (&markeddays, "i", dmday);
709
787
 
710
788
                // If the appointment time is less than the selected date, 
711
789
                // don't create an appointment item for it.
718
796
                if (i >= 5) continue;
719
797
                i++;
720
798
 
721
 
                g_debug("Create menu item");
722
 
                
723
 
                item = dbusmenu_menuitem_new();
724
 
                dbusmenu_menuitem_property_set       (item, DBUSMENU_MENUITEM_PROP_TYPE, APPOINTMENT_MENUITEM_TYPE);
 
799
                if (cached_appointment == NULL) {
 
800
                        g_debug("Create menu item");
 
801
                        
 
802
                        item = dbusmenu_menuitem_new();
 
803
                        dbusmenu_menuitem_property_set       (item, DBUSMENU_MENUITEM_PROP_TYPE, APPOINTMENT_MENUITEM_TYPE);
 
804
 
 
805
                        dbusmenu_menuitem_child_add_position (root, item, 2+i);
 
806
                        appointments = g_list_append (appointments, item); // Keep track of the items here to make them easy to remove
 
807
                } else {
 
808
                        item = DBUSMENU_MENUITEM(cached_appointment->data);
 
809
                        cached_appointment = g_list_next(cached_appointment);
 
810
 
 
811
                        /* Remove the icon as we might not replace it on error */
 
812
                        dbusmenu_menuitem_property_remove(item, APPOINTMENT_MENUITEM_PROP_ICON);
 
813
 
 
814
                        /* Remove the activate handler */
 
815
                        g_signal_handlers_disconnect_matched(G_OBJECT(item), G_SIGNAL_MATCH_FUNC, 0, 0, NULL, G_CALLBACK(activate_cb), NULL);
 
816
                }
 
817
 
725
818
                dbusmenu_menuitem_property_set_bool  (item, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
726
819
                dbusmenu_menuitem_property_set_bool  (item, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
 
820
 
727
821
        
728
822
        // Label text        
729
823
                e_cal_component_get_summary (ecalcomp, &valuetext);
744
838
                                strftime(right, 20, _(DEFAULT_TIME_24_FORMAT), due);
745
839
                        else
746
840
                                strftime(right, 20, _(DEFAULT_TIME_24_FORMAT_WITH_DAY), due);
747
 
                } else {
748
 
                        if ((mday == dmday) && (mon == dmon) && (year == dyear))
749
 
                                strftime(right, 20, _(DEFAULT_TIME_FORMAT), due);
750
 
                        else
751
 
                                strftime(right, 20, _(DEFAULT_TIME_FORMAT_WITH_DAY), due);
752
841
                }
753
842
                g_debug("Appointment time: %s, for date %s", right, asctime(due));
754
843
                dbusmenu_menuitem_property_set (item, APPOINTMENT_MENUITEM_PROP_RIGHT, right);
821
910
                        cairo_surface_destroy (surface);
822
911
                        cairo_destroy(cr);
823
912
                }
824
 
                dbusmenu_menuitem_child_add_position (root, item, 2+i);
825
 
                appointments = g_list_append         (appointments, item); // Keep track of the items here to make them easy to remove
826
913
                g_debug("Adding appointment: %p", item);
827
914
        }
828
915
        
833
920
        }
834
921
        g_list_free(sorted_comp_instances);
835
922
        
 
923
        GVariant * marks = g_variant_builder_end (&markeddays);
 
924
        dbusmenu_menuitem_property_set_variant (calendar, CALENDAR_MENUITEM_PROP_MARKS, marks);
 
925
        
836
926
        updating_appointments = FALSE;
837
927
        g_debug("End of objects");
838
928
        return TRUE;
1219
1309
        
1220
1310
        build_menus(root);
1221
1311
        
 
1312
        // Connect to the close signal to reset the calendar date 
 
1313
        g_signal_connect(root, "event::closed", G_CALLBACK(close_menu_cb), NULL);
 
1314
        
1222
1315
        /* Cache the timezone */
1223
1316
        update_current_timezone();
1224
1317