~charlesk/indicator-datetime/1060263

« back to all changes in this revision

Viewing changes to src/datetime-service.c

  • Committer: Charles Kerr
  • Date: 2012-07-12 17:59:44 UTC
  • Revision ID: charles.kerr@canonical.com-20120712175944-o601fe43jpkajvlz
cyphermox patch to support EDS 3.6's API

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include <geoclue/geoclue-master-client.h>
41
41
 
42
42
#include <time.h>
43
 
#include <libecal/e-cal.h>
 
43
#include <libecal/libecal.h>
44
44
#include <libical/ical.h>
45
 
#include <libecal/e-cal-time-util.h>
46
 
#include <libedataserver/e-source.h>
47
 
#include <libedataserverui/e-passwords.h>
 
45
#include <libedataserver/libedataserver.h>
48
46
// Other users of ecal seem to also include these, not sure why they should be included by the above
49
47
#include <libical/icaltime.h>
50
48
#include <cairo/cairo.h>
600
598
        return FALSE;
601
599
}
602
600
 
603
 
// Authentication function
604
 
static gchar *
605
 
auth_func (ECal *ecal, 
606
 
           const gchar *prompt, 
607
 
           const gchar *key, 
608
 
           gpointer user_data)
609
 
{
610
 
        ESource *source = e_cal_get_source (ecal);
611
 
        gchar *auth_domain = e_source_get_duped_property (source, "auth-domain");
612
 
 
613
 
        const gchar *component_name;
614
 
        if (auth_domain) component_name = auth_domain;
615
 
        else component_name = "Calendar";
616
 
        
617
 
        gchar *password = e_passwords_get_password (component_name, key);
618
 
        
619
 
        g_free (auth_domain);
620
 
 
621
 
        return password;
622
 
}
623
 
 
624
601
static gint
625
602
compare_comp_instances (gconstpointer ga, gconstpointer gb)
626
603
{
655
632
}
656
633
 
657
634
static gboolean
658
 
populate_appointment_instances (ECalComponent * comp,
 
635
populate_appointment_instances (ECalClient * client,
659
636
                                time_t          start,
660
637
                                time_t          end,
661
638
                                gpointer        data)
662
639
{
663
 
        g_debug("Appending item %p", comp);
664
 
        
665
 
        ECalComponentVType vtype = e_cal_component_get_vtype (comp);
666
 
        if (vtype != E_CAL_COMPONENT_EVENT && vtype != E_CAL_COMPONENT_TODO) return FALSE;
667
 
        
668
 
        icalproperty_status status;
669
 
        e_cal_component_get_status (comp, &status);
670
 
        if (status == ICAL_STATUS_COMPLETED || status == ICAL_STATUS_CANCELLED) return FALSE;
671
 
 
672
 
        struct comp_instance *ci = comp_instance_new (comp, start, end, E_SOURCE(data));
673
 
        comp_instances = g_list_append (comp_instances, ci);
674
 
        return TRUE;
 
640
       GSList *ecalcomps, *comp_item;
 
641
 
 
642
       if (e_cal_client_get_object_list_as_comps_sync (client,
 
643
                                                       NULL,
 
644
                                                       &ecalcomps,
 
645
                                                       NULL, NULL)) {
 
646
 
 
647
               for (comp_item = ecalcomps; comp_item; comp_item = g_slist_next(comp_item)) {
 
648
                       ECalComponent *comp = comp_item->data;
 
649
 
 
650
                       g_debug("Appending item %p", e_cal_component_get_as_string(comp));
 
651
 
 
652
                       ECalComponentVType vtype = e_cal_component_get_vtype (comp);
 
653
                       if (vtype != E_CAL_COMPONENT_EVENT && vtype != E_CAL_COMPONENT_TODO) return FALSE;
 
654
 
 
655
                       icalproperty_status status;
 
656
                       e_cal_component_get_status (comp, &status);
 
657
                       if (status == ICAL_STATUS_COMPLETED || status == ICAL_STATUS_CANCELLED) return FALSE;
 
658
 
 
659
                       g_object_ref(comp);
 
660
 
 
661
                       ECalComponentDateTime datetime;
 
662
                       icaltimezone *appointment_zone = NULL;
 
663
                       icaltimezone *current_zone = NULL;
 
664
 
 
665
                       if (vtype == E_CAL_COMPONENT_EVENT)
 
666
                               e_cal_component_get_dtstart (comp, &datetime);
 
667
                       else
 
668
                               e_cal_component_get_due (comp, &datetime);
 
669
 
 
670
                       appointment_zone = icaltimezone_get_builtin_timezone_from_tzid(datetime.tzid);
 
671
                       current_zone = icaltimezone_get_builtin_timezone_from_tzid(current_timezone);
 
672
                       if (!appointment_zone || datetime.value->is_date) { // If it's today put in the current timezone?
 
673
                               appointment_zone = current_zone;
 
674
                       }
 
675
 
 
676
                       struct comp_instance *ci = comp_instance_new (comp, start, end, E_SOURCE(data));
 
677
                       comp_instances = g_list_append (comp_instances, ci);
 
678
               }
 
679
               return TRUE;
 
680
       }
 
681
       return FALSE;
675
682
}
676
683
 
677
684
/* Populate the menu with todays, next 5 appointments. 
691
698
        updating_appointments = TRUE;
692
699
        
693
700
        time_t curtime = 0, t1 = 0, t2 = 0;
694
 
        GList *l;
695
 
        GSList *g;
 
701
        GList *l, *s;
696
702
        GError *gerror = NULL;
697
703
        gint i;
698
704
        gint width = 0, height = 0;
699
 
        ESourceList * sources = NULL;
 
705
        ESourceRegistry * src_registry = NULL;
 
706
        GList * sources = NULL;
700
707
 
701
708
        // Get today & work out query times
702
709
        time(&curtime);
736
743
        highlightdays = highlightdays + 7; // Minimum of 7 days ahead 
737
744
        t2 = t1 + (time_t) (highlightdays * 24 * 60 * 60);
738
745
        
739
 
        if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, &gerror)) {
740
 
                g_debug("Failed to get ecal sources\n");
741
 
                g_clear_error (&gerror);
742
 
                return FALSE;
743
 
        }
744
 
        
745
746
        // clear any previous comp_instances
746
747
        g_list_free_full (comp_instances, (GDestroyNotify)comp_instance_free);
747
748
        comp_instances = NULL;
748
749
 
749
 
        GSList *cal_list = gconf_client_get_list(gconf, "/apps/evolution/calendar/display/selected_calendars", GCONF_VALUE_STRING, &gerror);
750
 
        if (gerror) {
751
 
          g_debug("Failed to get evolution preference for enabled calendars");
752
 
          g_clear_error (&gerror);
753
 
          cal_list = NULL;
754
 
        }
755
 
        
 
750
       src_registry = e_source_registry_new_sync (NULL, &gerror);
 
751
       if (!src_registry) {
 
752
               g_debug("Failed to get access to source registry: %s\n", gerror->message);
 
753
               return FALSE;
 
754
       }
 
755
 
 
756
       sources = e_source_registry_list_sources(src_registry, E_SOURCE_EXTENSION_CALENDAR);
 
757
 
756
758
        // Generate instances for all sources
757
 
        for (g = e_source_list_peek_groups (sources); g; g = g->next) {
758
 
                ESourceGroup *group = E_SOURCE_GROUP (g->data);
759
 
                GSList *s;
760
 
                
761
 
                for (s = e_source_group_peek_sources (group); s; s = s->next) {
762
 
                        ESource *source = E_SOURCE (s->data);
763
 
                        g_signal_connect (G_OBJECT(source), "changed", G_CALLBACK (update_appointment_menu_items), NULL);
764
 
                        ECal *ecal = e_cal_new(source, E_CAL_SOURCE_TYPE_EVENT);
765
 
                        e_cal_set_auth_func (ecal, (ECalAuthFunc) auth_func, NULL);
766
 
                        
767
 
                        icaltimezone* current_zone = icaltimezone_get_builtin_timezone(current_timezone);
768
 
                        if (!current_zone) {
769
 
                                // current_timezone may be a TZID?
770
 
                                current_zone = icaltimezone_get_builtin_timezone_from_tzid(current_timezone);
771
 
                        }
772
 
                        if (current_zone && !e_cal_set_default_timezone(ecal, current_zone, &gerror)) {
773
 
                                g_debug("Failed to set ecal default timezone %s", gerror->message);
774
 
                                g_clear_error (&gerror);
775
 
                                g_object_unref(ecal);
776
 
                                continue;
777
 
                        }
778
 
                        
779
 
                        if (!e_cal_open(ecal, FALSE, &gerror)) {
780
 
                                g_debug("Failed to get ecal sources %s", gerror->message);
781
 
                                g_clear_error (&gerror);
782
 
                                g_object_unref(ecal);
783
 
                                continue;
784
 
                        }
785
 
 
786
 
                        const gchar *ecal_uid = e_source_peek_uid(source);
787
 
                        g_debug("Checking ecal_uid is enabled: %s", ecal_uid);
788
 
                        const gboolean in_list = g_slist_find_custom (cal_list, ecal_uid, (GCompareFunc)g_strcmp0) != NULL;
789
 
                        if (!in_list) {
790
 
                                g_object_unref(ecal);
791
 
                                continue;
792
 
                        }
793
 
 
794
 
                        g_debug("ecal_uid is enabled, generating instances");
795
 
                        e_cal_generate_instances (ecal, t1, t2, (ECalRecurInstanceFn) populate_appointment_instances, source);
796
 
                        g_object_unref(ecal);
797
 
                }
798
 
        }
799
 
        g_slist_free_full (cal_list, g_free);
 
759
       for (s = g_list_first (sources); s; s = g_list_next (s)) {
 
760
 
 
761
               ESource *source = E_SOURCE (s->data);
 
762
               g_signal_connect (G_OBJECT(source), "changed", G_CALLBACK (update_appointment_menu_items), NULL);
 
763
               ECalClient *ecal = e_cal_client_new(source, E_CAL_CLIENT_SOURCE_TYPE_EVENTS, &gerror);
 
764
 
 
765
               icaltimezone* current_zone = icaltimezone_get_builtin_timezone(current_timezone);
 
766
               if (!current_zone) {
 
767
                       // current_timezone may be a TZID?
 
768
                       current_zone = icaltimezone_get_builtin_timezone_from_tzid(current_timezone);
 
769
               }
 
770
 
 
771
               e_cal_client_set_default_timezone (ecal, current_zone);
 
772
 
 
773
               g_debug("Checking if source %s is enabled", e_source_get_uid(source));
 
774
               if (e_source_get_enabled (source)) {
 
775
                       g_debug("source is enabled, generating instances");
 
776
 
 
777
                       if (!e_client_open_sync (E_CLIENT (ecal), TRUE, NULL, &gerror)) {
 
778
                               g_debug("Failed to open source: %s", gerror->message);
 
779
                                g_clear_error (&gerror);
 
780
                                g_object_unref(ecal);
 
781
                                continue;
 
782
                        }
 
783
 
 
784
                       e_cal_client_generate_instances (ecal, t1, t2, NULL,
 
785
                                                        (ECalRecurInstanceFn) populate_appointment_instances,
 
786
                                                        (gpointer) source,
 
787
                                                        NULL);
 
788
               }
 
789
               g_object_unref(ecal);
 
790
       }
 
791
       g_list_free_full (sources, g_object_unref);
800
792
 
801
793
        g_debug("Number of ECalComponents returned: %d", g_list_length(comp_instances));
802
794
        GList *sorted_comp_instances = g_list_sort(comp_instances, compare_comp_instances);
948
940
                                       G_CALLBACK(activate_cb), cmd, (GClosureNotify)g_free, 0);
949
941
                g_free (ad);
950
942
 
951
 
        const gchar *color_spec = e_source_peek_color_spec(ci->source);
 
943
        const gchar *color_spec = e_source_selectable_get_color (e_source_get_extension (ci->source, E_SOURCE_EXTENSION_CALENDAR));
952
944
        g_debug("Colour to use: %s", color_spec);
953
945
                        
954
946
                // Draw the correct icon for the appointment type and then tint it using mask fill.