]> code.citadel.org Git - citadel.git/blobdiff - webcit/calendar_view.c
* persistantly handle the bufferlengths for dates; snprintf doesn't terminate strings...
[citadel.git] / webcit / calendar_view.c
index 69bf1bf4393ac63f03a15bd82eef2e2c8554d27d..6d71049a9b406e8120eb57017ad6220daa9a5a24 100644 (file)
@@ -163,6 +163,7 @@ void calendar_month_view_display_events(int year, int month, int day)
        struct icaltimetype end_t;
        struct icaltimetype today_start_t;
        struct icaltimetype today_end_t;
+       struct icaltimetype today_t;
        struct tm starting_tm;
        struct tm ending_tm;
        int all_day_event = 0;
@@ -199,6 +200,14 @@ void calendar_month_view_display_events(int year, int month, int day)
        today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
        today_end_t.is_utc = 1;
 
+       /*
+        * Create another one without caring about the timezone for all day events.
+        */
+       today_t = icaltime_null_date();
+       today_t.year = year;
+       today_t.month = month;
+       today_t.day = day;
+
        /*
         * Now loop through our list of events to see which ones occur today.
         */
@@ -224,7 +233,7 @@ void calendar_month_view_display_events(int year, int month, int day)
 
                if (all_day_event)
                {
-                       show_event = ((t.year == year) && (t.month == month) && (t.day == day));
+                       show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
                }
                else
                {
@@ -236,6 +245,10 @@ void calendar_month_view_display_events(int year, int month, int day)
                 */
                if (show_event) {
                        p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
+                       if (p == NULL) {
+                               p = icalproperty_new_summary(_("Untitled Event"));
+                               icalcomponent_add_property(Cal->cal, p);
+                       }
                        if (p != NULL) {
 
                                if (all_day_event) {
@@ -275,34 +288,63 @@ void calendar_month_view_display_events(int year, int month, int day)
                                        
                                        q = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
                                        if (q != NULL) {
+                                               int no_end = 0;
+
                                                t = icalproperty_get_dtstart(q);
-                                               
+                                               q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
+                                               if (q != NULL) {
+                                                       end_t = icalproperty_get_dtend(q);
+                                               }
+                                               else {
+                                                       /*
+                                                        * events with starting date/time equal
+                                                        * ending date/time might get only
+                                                        * DTSTART but no DTEND
+                                                        */
+                                                       no_end = 1;
+                                               }
+
                                                if (t.is_date) {
+                                                       /* all day event */
                                                        struct tm d_tm;
-                                                       char d_str[32];
+
+                                                       if (!no_end) {
+                                                               /* end given, have to adjust it */
+                                                               icaltime_adjust(&end_t, -1, 0, 0, 0);
+                                                       }
                                                        memset(&d_tm, 0, sizeof d_tm);
                                                        d_tm.tm_year = t.year - 1900;
                                                        d_tm.tm_mon = t.month - 1;
                                                        d_tm.tm_mday = t.day;
-                                                       wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
-                                                       wprintf("<i>%s</i> %s<br>",
-                                                               _("Date:"), d_str);
+                                                       wc_strftime(buf, sizeof buf, "%x", &d_tm);
+
+                                                       if (no_end || !icaltime_compare(t, end_t)) {
+                                                               wprintf("<i>%s</i> %s<br>",
+                                                                       _("Date:"), buf);
+                                                       }
+                                                       else {
+                                                               wprintf("<i>%s</i> %s<br>",
+                                                                       _("Starting date:"), buf);
+                                                               d_tm.tm_year = end_t.year - 1900;
+                                                               d_tm.tm_mon = end_t.month - 1;
+                                                               d_tm.tm_mday = end_t.day;
+                                                               wc_strftime(buf, sizeof buf, "%x", &d_tm);
+                                                               wprintf("<i>%s</i> %s<br>",
+                                                                       _("Ending date:"), buf);
+                                                       }
                                                }
                                                else {
                                                        tt = icaltime_as_timet(t);
-                                                       webcit_fmt_date(buf, tt, 1);
-                                                       wprintf("<i>%s</i> %s<br>",
-                                                               _("Starting date/time:"), buf);
-                                                       
-                                                       /*
-                                                        * Embed the 'show end date/time' loop inside here so it
-                                                        * only executes if this is NOT an all day event.
-                                                        */
-                                                       q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
-                                                       if (q != NULL) {
-                                                               t = icalproperty_get_dtend(q);
-                                                               tt = icaltime_as_timet(t);
-                                                               webcit_fmt_date(buf, tt, 1);
+                                                       webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF);
+                                                       if (no_end || !icaltime_compare(t, end_t)) {
+                                                               wprintf("<i>%s</i> %s<br>",
+                                                                       _("Date/time:"), buf);
+                                                       }
+                                                       else {
+                                                               wprintf("<i>%s</i> %s<br>",
+                                                                       _("Starting date/time:"), buf);
+                                                               tt = icaltime_as_timet(end_t);
+                                                               webcit_fmt_date(buf, 256, tt, DATEFMT_BRIEF);
                                                                wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
                                                        }
                                                        
@@ -400,6 +442,10 @@ void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
                        p = icalcomponent_get_first_property(
                                Cal->cal,
                                ICAL_SUMMARY_PROPERTY);
+                       if (p == NULL) {
+                               p = icalproperty_new_summary(_("Untitled Event"));
+                               icalcomponent_add_property(Cal->cal, p);
+                       }
                        e = icalcomponent_get_first_property(
                                Cal->cal, 
                                ICAL_DTEND_PROPERTY);
@@ -770,6 +816,7 @@ void calendar_day_view_display_events(time_t thetime,
        struct icaltimetype end_t;
        struct icaltimetype today_start_t;
        struct icaltimetype today_end_t;
+       struct icaltimetype today_t;
        struct tm starting_tm;
        struct tm ending_tm;
        int top = 0;
@@ -780,8 +827,6 @@ void calendar_day_view_display_events(time_t thetime,
        int endmin = 0;
 
         char buf[256];
-        struct tm d_tm;
-        char d_str[32];
 
        if (GetCount(WCC->disp_cal_items) == 0) {
                /* nothing to display */
@@ -809,6 +854,14 @@ void calendar_day_view_display_events(time_t thetime,
        today_end_t = icaltime_from_timet_with_zone(mktime(&ending_tm), 0, icaltimezone_get_utc_timezone());
        today_end_t.is_utc = 1;
 
+       /*
+        * Create another one without caring about the timezone for all day events.
+        */
+       today_t = icaltime_null_date();
+       today_t.year = year;
+       today_t.month = month;
+       today_t.day = day;
+
        /* Now loop through our list of events to see which ones occur today.
         */
        Pos = GetNewHashPos(WCC->disp_cal_items, 0);
@@ -827,29 +880,46 @@ void calendar_day_view_display_events(time_t thetime,
                else {
                        memset(&t, 0, sizeof t);
                }
+
+               if (t.is_date) all_day_event = 1;
+
                q = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
                if (q != NULL) {
                        end_t = icalproperty_get_dtend(q);
-                       event_tte = icaltime_as_timet(end_t);
-                       localtime_r(&event_tte, &event_tm);
                }
                else {
-                       memset(&end_t, 0, sizeof end_t);
+                       /* no end given means end = start */
+                       memcpy(&end_t, &t, sizeof(struct icaltimetype));
                }
-               if (t.is_date) all_day_event = 1;
 
                if (all_day_event)
                {
-                       show_event = ((t.year == year) && (t.month == month) && (t.day == day) && (notime_events));
+                       show_event = ical_ctdl_is_overlap(t, end_t, today_t, icaltime_null_time());
+                       if (icaltime_compare(t, end_t)) {
+                               /*
+                                * the end date is non-inclusive so adjust it by one
+                                * day because our test is inclusive, note that a day is
+                                * not to much because we are talking about all day
+                                * events
+                                */
+                               icaltime_adjust(&end_t, -1, 0, 0, 0);
+                       }
                }
                else
                {
                        show_event = ical_ctdl_is_overlap(t, end_t, today_start_t, today_end_t);
                }
 
+               event_tte = icaltime_as_timet(end_t);
+               localtime_r(&event_tte, &event_tm);
+
                /* If we determined that this event occurs today, then display it.
                 */
                p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
+               if (p == NULL) {
+                       p = icalproperty_new_summary(_("Untitled Event"));
+                       icalcomponent_add_property(Cal->cal, p);
+               }
 
                if ((show_event) && (p != NULL)) {
 
@@ -874,13 +944,17 @@ void calendar_day_view_display_events(time_t thetime,
                                         wprintf("<i>%s</i> ", _("Location:"));
                                         escputs((char *)icalproperty_get_comment(q));
                                         wprintf("<br />");
-                                                               }
-                                memset(&d_tm, 0, sizeof d_tm);
-                                d_tm.tm_year = t.year - 1900;
-                                d_tm.tm_mon = t.month - 1;
-                                d_tm.tm_mday = t.day;
-                                wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
-                                wprintf("<i>%s</i> %s<br>",_("Date:"), d_str);
+                               }
+                               if (!icaltime_compare(t, end_t)) { /* one day only */
+                                       webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
+                                       wprintf("<i>%s</i> %s<br>", _("Date:"), buf);
+                               }
+                               else {
+                                       webcit_fmt_date(buf, 256, event_tt, DATEFMT_LOCALEDATE);
+                                       wprintf("<i>%s</i> %s<br>", _("Starting date:"), buf);
+                                       webcit_fmt_date(buf, 256, event_tte, DATEFMT_LOCALEDATE);
+                                       wprintf("<i>%s</i> %s<br>", _("Ending date:"), buf);
+                               }
                                q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
                                 if (q) {
                                         wprintf("<i>%s</i> ", _("Notes:"));
@@ -913,9 +987,9 @@ void calendar_day_view_display_events(time_t thetime,
                                         escputs((char *)icalproperty_get_comment(q));
                                         wprintf("<br />");
                                                                }
-                                webcit_fmt_date(buf, event_tt, 1);
+                                webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
                                 wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
-                                webcit_fmt_date(buf, event_tte, 1);
+                                webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
                                 wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
                                 q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
                                 if (q) {
@@ -992,10 +1066,16 @@ void calendar_day_view_display_events(time_t thetime,
                                         escputs((char *)icalproperty_get_comment(q));
                                         wprintf("<br />");
                                                                }
-                                webcit_fmt_date(buf, event_tt, 1);
-                                wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
-                                webcit_fmt_date(buf, event_tte, 1);
-                                wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
+                               if (!icaltime_compare(t, end_t)) { /* one day only */
+                                       webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
+                                       wprintf("<i>%s</i> %s<br>", _("Date/time:"), buf);
+                               }
+                               else {
+                                       webcit_fmt_date(buf, 256, event_tt, DATEFMT_BRIEF);
+                                       wprintf("<i>%s</i> %s<br>", _("Starting date/time:"), buf);
+                                       webcit_fmt_date(buf, 256, event_tte, DATEFMT_BRIEF);
+                                       wprintf("<i>%s</i> %s<br>", _("Ending date/time:"), buf);
+                               }
                                q = icalcomponent_get_first_property(Cal->cal,ICAL_DESCRIPTION_PROPERTY);
                                 if (q) {
                                         wprintf("<i>%s</i> ", _("Notes:"));
@@ -1027,6 +1107,8 @@ void calendar_day_view(int year, int month, int day) {
        int timeline = TIMELINE;
        int extratimeline = EXTRATIMELINE;
        int gap = 0;
+       int hourlabel;
+       int extrahourlabel;
 
        time_format = get_time_format_cached ();
        get_pref_long("daystart", &daystart, 8);
@@ -1072,6 +1154,10 @@ void calendar_day_view(int year, int month, int day) {
 
        /* Now the middle of the day... */
 
+       extrahourlabel = extratimeline - 2;
+       hourlabel = extrahourlabel * 150 / 100;
+       if (hourlabel > (timeline - 2)) hourlabel = timeline - 2;
+
        for (hour = 0; hour < daystart; ++hour) {       /* could do HEIGHT=xx */
                wprintf("<dt class=\"extrahour\"        "
                        "style=\"               "
@@ -1084,7 +1170,7 @@ void calendar_day_view(int year, int month, int day) {
                        "?calview=day?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
                        (hour * extratimeline ),
                        extratimeline,
-                       extratimeline - 2,
+                       extrahourlabel,
                        year, month, day, hour
                        );
 
@@ -1115,7 +1201,7 @@ void calendar_day_view(int year, int month, int day) {
                         "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
                         gap + ((hour - daystart) * timeline ),
                        timeline,
-                       timeline - 2,
+                       hourlabel,
                         year, month, day, hour
                        );
 
@@ -1146,7 +1232,7 @@ void calendar_day_view(int year, int month, int day) {
                         "?year=%d?month=%d?day=%d?hour=%d?minute=0\">",
                         gap + ((hour - dayend - 1) * extratimeline ),
                        extratimeline,
-                       extratimeline - 2,
+                       extrahourlabel,
                         year, month, day, hour
                 );
 
@@ -1273,7 +1359,7 @@ int calendar_summary_view(void) {
                        else {
                                all_day_event = 0;
                        }
-                       fmt_time(timestring, event_tt);
+                       fmt_time(timestring, SIZ, event_tt);
 
                        if (all_day_event) {
                                gmtime_r(&event_tt, &event_tm);
@@ -1288,6 +1374,10 @@ int calendar_summary_view(void) {
                        ) {
 
                                p = icalcomponent_get_first_property(Cal->cal, ICAL_SUMMARY_PROPERTY);
+                               if (p == NULL) {
+                                       p = icalproperty_new_summary(_("Untitled Task"));
+                                       icalcomponent_add_property(Cal->cal, p);
+                               }
                                if (p != NULL) {
 
 
@@ -1429,7 +1519,7 @@ void render_calendar_view(struct calview *c)
 /*
  * Helper function for do_tasks_view().  Returns the due date/time of a vtodo.
  */
-time_t get_task_due_date(icalcomponent *vtodo) {
+time_t get_task_due_date(icalcomponent *vtodo, int *is_date) {
        icalproperty *p;
 
        if (vtodo == NULL) {
@@ -1445,13 +1535,17 @@ time_t get_task_due_date(icalcomponent *vtodo) {
                return get_task_due_date(
                        icalcomponent_get_first_component(
                                vtodo, ICAL_VTODO_COMPONENT
-                               )
+                               ), is_date
                        );
        }
 
        p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
        if (p != NULL) {
-               return(icaltime_as_timet(icalproperty_get_due(p)));
+               struct icaltimetype t = icalproperty_get_due(p);
+
+               if (is_date)
+                       *is_date = t.is_date;
+               return(icaltime_as_timet(t));
        }
        else {
                return(0L);
@@ -1469,8 +1563,8 @@ int task_due_cmp(const void *vtask1, const void *vtask2) {
        time_t t1;
        time_t t2;
 
-       t1 =  get_task_due_date(Task1->cal);
-       t2 =  get_task_due_date(Task2->cal);
+       t1 =  get_task_due_date(Task1->cal, NULL);
+       t2 =  get_task_due_date(Task2->cal, NULL);
        if (t1 < t2) return(-1);
        if (t1 > t2) return(1);
        return(0);
@@ -1539,6 +1633,7 @@ void do_tasks_view(void) {
        Pos = GetNewHashPos(WCC->disp_cal_items, 0);
        while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
                icalproperty_status todoStatus;
+               int is_date;
 
                Cal = (disp_cal*)vCal;
                wprintf("<tr><td>");
@@ -1561,10 +1656,10 @@ void do_tasks_view(void) {
                wprintf("</a>\n");
                wprintf("</td>\n");
 
-               due = get_task_due_date(Cal->cal);
+               due = get_task_due_date(Cal->cal, &is_date);
                wprintf("<td><span");
                if (due > 0) {
-                       webcit_fmt_date(buf, due, 0);
+                       webcit_fmt_date(buf, SIZ, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL);
                        wprintf(">%s",buf);
                }
                else {