]> code.citadel.org Git - citadel.git/blobdiff - webcit/calendar_view.c
* Replaced all the "centered 99% tables" with 100% width tables wrapped
[citadel.git] / webcit / calendar_view.c
index 9124a7c9cb7c6cf33d6419bac052e46b94bf8867..0428d382bfca3cf8c73d08f3a2073f964b0ca169 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- *
+ * Handles the HTML display of calendar items.
  */
 
 #include <ctype.h>
 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
 
 void do_calendar_view(void) {  /* stub for non-libical builds */
-       wprintf("<CENTER><I>Calendar view not available</I></CENTER><BR>\n");
+       wprintf("<CENTER><I>Calendar view not available</I></CENTER><br />\n");
+}
+
+void do_tasks_view(void) {     /* stub for non-libical builds */
+       wprintf("<CENTER><I>Tasks view not available</I></CENTER><br />\n");
 }
 
 #else  /* WEBCIT_WITH_CALENDAR_SERVICE */
@@ -48,31 +52,38 @@ void calendar_month_view_display_events(time_t thetime) {
        int all_day_event = 0;
 
        if (WC->num_cal == 0) {
-               wprintf("<BR><BR><BR>\n");
+               wprintf("<br /><br /><br />\n");
                return;
        }
 
-       memcpy(&today_tm, localtime(&thetime), sizeof(struct tm));
+       localtime_r(&thetime, &today_tm);
        month = today_tm.tm_mon + 1;
        day = today_tm.tm_mday;
        year = today_tm.tm_year + 1900;
 
        for (i=0; i<(WC->num_cal); ++i) {
-               p = icalcomponent_get_first_property(WC->disp_cal[i],
+               p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
                                                ICAL_DTSTART_PROPERTY);
                if (p != NULL) {
                        t = icalproperty_get_dtstart(p);
                        event_tt = icaltime_as_timet(t);
-                       memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
+
+                       if (t.is_date) all_day_event = 1;
+                       else all_day_event = 0;
+
+                       if (all_day_event) {
+                               gmtime_r(&event_tt, &event_tm);
+                       }
+                       else {
+                               localtime_r(&event_tt, &event_tm);
+                       }
+
                        if ((event_tm.tm_year == today_tm.tm_year)
                           && (event_tm.tm_mon == today_tm.tm_mon)
                           && (event_tm.tm_mday == today_tm.tm_mday)) {
 
-                               if (t.is_date) all_day_event = 1;
-                               else all_day_event = 0;
-
                                p = icalcomponent_get_first_property(
-                                                       WC->disp_cal[i],
+                                                       WC->disp_cal[i].cal,
                                                        ICAL_SUMMARY_PROPERTY);
                                if (p != NULL) {
 
@@ -84,7 +95,7 @@ void calendar_month_view_display_events(time_t thetime) {
 
                                        wprintf("<FONT SIZE=-1>"
                                                "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
-                                               WC->cal_msgnum[i],
+                                               WC->disp_cal[i].cal_msgnum,
                                                bstr("calview"),
                                                bstr("year"),
                                                bstr("month"),
@@ -92,7 +103,7 @@ void calendar_month_view_display_events(time_t thetime) {
                                        );
                                        escputs((char *)
                                                icalproperty_get_comment(p));
-                                       wprintf("</A></FONT><BR>\n");
+                                       wprintf("</A></FONT><br />\n");
 
                                        if (all_day_event) {
                                                wprintf("</TD></TR></TABLE>");
@@ -129,7 +140,7 @@ void calendar_month_view(int year, int month, int day) {
        memcpy(&tm, &starting_tm, sizeof(struct tm));
        while (tm.tm_mday != 1) {
                thetime = thetime - (time_t)86400;      /* go back 24 hours */
-               memcpy(&tm, localtime(&thetime), sizeof(struct tm));
+               localtime_r(&thetime, &tm);
        }
 
        /* Determine previous and next months ... for links */
@@ -137,46 +148,38 @@ void calendar_month_view(int year, int month, int day) {
        next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
 
        /* Now back up until we're on a Sunday */
-       memcpy(&tm, localtime(&thetime), sizeof(struct tm));
+       localtime_r(&thetime, &tm);
        while (tm.tm_wday != 0) {
                thetime = thetime - (time_t)86400;      /* go back 24 hours */
-               memcpy(&tm, localtime(&thetime), sizeof(struct tm));
+               localtime_r(&thetime, &tm);
        }
 
        /* Outer table (to get the background color) */
-       wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
+       wprintf("<div style=\"margin-right:1px\">"
+               "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
                "bgcolor=#204B78><TR><TD>\n");
 
-       wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
-               "<TR><TD align=left><font color=#FFFFFF>"
-               "&nbsp;<A HREF=\"/display_edit_event?msgnum=0"
-               "&year=%d&month=%d&day=%d\">"
-               "Add new calendar event</A>"
-               "</font></TD>\n",
-               year, month, day
-       );
+       wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0><tr>\n");
 
-       wprintf("<TD ALIGN=CENTER><FONT SIZE=+1>");
+       wprintf("<TD ALIGN=CENTER>");
 
-       memcpy(&tm, localtime(&previous_month), sizeof(struct tm));
+       localtime_r(&previous_month, &tm);
        wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
                (int)(tm.tm_year)+1900, tm.tm_mon + 1);
        wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
 
        wprintf("&nbsp;&nbsp;"
-               "<FONT COLOR=\"#FFFFFF\">"
+               "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
                "%s %d"
                "</FONT>"
                "&nbsp;&nbsp;", months[month-1], year);
 
-       memcpy(&tm, localtime(&next_month), sizeof(struct tm));
+       localtime_r(&next_month, &tm);
        wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
                (int)(tm.tm_year)+1900, tm.tm_mon + 1);
        wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
 
-       wprintf("</FONT></TD><TD align=right><font color=#FFFFFF size=-2>"
-               "Click on any date for day view&nbsp;"
-               "</FONT></TD></TR></TABLE>\n");
+       wprintf("</TD></TR></TABLE>\n");
 
        /* Inner table (the real one) */
        wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
@@ -189,14 +192,14 @@ void calendar_month_view(int year, int month, int day) {
 
        /* Now do 35 days */
        for (i = 0; i < 35; ++i) {
-               memcpy(&tm, localtime(&thetime), sizeof(struct tm));
+               localtime_r(&thetime, &tm);
 
                /* Before displaying Sunday, start a new row */
                if ((i % 7) == 0) {
                        wprintf("<TR>");
                }
 
-               wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
+               wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 align=left VALIGN=TOP><B>",
                        ((tm.tm_mon != month-1) ? "DDDDDD" :
                        ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
                        "FFFFFF"))
@@ -205,7 +208,7 @@ void calendar_month_view(int year, int month, int day) {
                        wprintf("%s ", months[tm.tm_mon]);
                }
                wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
-                       "%d</A></B><BR>",
+                       "%d</A></B><br />",
                        tm.tm_year + 1900,
                        tm.tm_mon + 1,
                        tm.tm_mday,
@@ -226,12 +229,12 @@ void calendar_month_view(int year, int month, int day) {
 
        wprintf("</TABLE>"                      /* end of inner table */
                "</TD></TR></TABLE>"            /* end of outer table */
-               "</CENTER>\n");
+               "</div>\n");
 }
 
 
 void calendar_week_view(int year, int month, int day) {
-       wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
+       wprintf("<CENTER><I>week view FIXME</I></CENTER><br />\n");
 }
 
 
@@ -249,27 +252,34 @@ void calendar_day_view_display_events(int year, int month,
        int all_day_event = 0;
 
        if (WC->num_cal == 0) {
-               wprintf("<BR><BR><BR>\n");
+               // FIXME wprintf("<br /><br /><br />\n");
                return;
        }
 
        for (i=0; i<(WC->num_cal); ++i) {
-               p = icalcomponent_get_first_property(WC->disp_cal[i],
+               p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
                                                ICAL_DTSTART_PROPERTY);
                if (p != NULL) {
                        t = icalproperty_get_dtstart(p);
                        event_tt = icaltime_as_timet(t);
-                       event_tm = localtime(&event_tt);
+                       if (t.is_date) all_day_event = 1;
+
+                       if (all_day_event) {
+                               event_tm = gmtime(&event_tt);
+                       }
+                       else {
+                               event_tm = localtime(&event_tt);
+                       }
+
                        if ((event_tm->tm_year == (year-1900))
                           && (event_tm->tm_mon == (month-1))
                           && (event_tm->tm_mday == day)
                           && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
                           ) {
 
-                               if (t.is_date) all_day_event = 1;
 
                                p = icalcomponent_get_first_property(
-                                                       WC->disp_cal[i],
+                                                       WC->disp_cal[i].cal,
                                                        ICAL_SUMMARY_PROPERTY);
                                if (p != NULL) {
 
@@ -281,12 +291,12 @@ void calendar_day_view_display_events(int year, int month,
 
                                        wprintf("<FONT SIZE=-1>"
                                                "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
-                                               WC->cal_msgnum[i],
+                                               WC->disp_cal[i].cal_msgnum,
                                                year, month, day
                                        );
                                        escputs((char *)
                                                icalproperty_get_comment(p));
-                                       wprintf("</A></FONT><BR>\n");
+                                       wprintf("</A></FONT><br />\n");
 
                                        if (all_day_event) {
                                                wprintf("</TD></TR></TABLE>");
@@ -325,7 +335,8 @@ void calendar_day_view(int year, int month, int day) {
 
 
        /* Outer table (to get the background color) */
-       wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
+       wprintf("<div style=\"margin-right:1px\">"
+               "<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
                "bgcolor=#204B78><TR><TD>\n");
 
        /* Inner table (the real one) */
@@ -397,9 +408,9 @@ void calendar_day_view(int year, int month, int day) {
 
        /* Today's date */
        wprintf("<TD ALIGN=CENTER>");
-       wprintf("<FONT SIZE=+2>%s</FONT><BR>"
-               "<FONT SIZE=+3>%d</FONT><BR>"
-               "<FONT SIZE=+2>%d</FONT><BR>",
+       wprintf("<FONT SIZE=+2>%s</FONT><br />"
+               "<FONT SIZE=+3>%d</FONT><br />"
+               "<FONT SIZE=+2>%d</FONT><br />",
                months[month-1], day, year);
        wprintf("</TD>");
 
@@ -414,16 +425,7 @@ void calendar_day_view(int year, int month, int day) {
        wprintf("</TR></TABLE>\n");
        /* End todays-date-with-left-and-right-arrows */
 
-       wprintf("<BR><BR><CENTER><font color=#FFFFFF>"
-               "&nbsp;<A HREF=\"/display_edit_event?msgnum=0"
-               "&year=%d&month=%d&day=%d\">"
-               "Add new calendar event</A>"
-               "<BR><BR>\n",
-               year, month, day
-       );
-
-       wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
-               "Back to month view</A>\n", year, month);
+       /* In the future we might want to put a month-o-matic here */
 
        wprintf("</FONT></CENTER>\n");
 
@@ -432,7 +434,7 @@ void calendar_day_view(int year, int month, int day) {
 
 
        wprintf("</TR></TABLE>"                 /* end of inner table */
-               "</TD></TR></TABLE>"            /* end of outer table */
+               "</TD></TR></TABLE></div>"      /* end of outer table */
        );
 
 
@@ -458,30 +460,37 @@ void calendar_summary_view(void) {
        }
 
        now = time(NULL);
-       memcpy(&today_tm, localtime(&now), sizeof(struct tm));
+       localtime_r(&now, &today_tm);
 
        for (i=0; i<(WC->num_cal); ++i) {
-               p = icalcomponent_get_first_property(WC->disp_cal[i],
+               p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
                                                ICAL_DTSTART_PROPERTY);
                if (p != NULL) {
                        t = icalproperty_get_dtstart(p);
                        event_tt = icaltime_as_timet(t);
+                       if (t.is_date) all_day_event = 1;
                        fmt_time(timestring, event_tt);
-                       memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
+
+                       if (all_day_event) {
+                               gmtime_r(&event_tt, &event_tm);
+                       }
+                       else {
+                               localtime_r(&event_tt, &event_tm);
+                       }
+
                        if ( (event_tm.tm_year == today_tm.tm_year)
                           && (event_tm.tm_mon == today_tm.tm_mon)
                           && (event_tm.tm_mday == today_tm.tm_mday)
                           ) {
 
-                               if (t.is_date) all_day_event = 1;
 
                                p = icalcomponent_get_first_property(
-                                                       WC->disp_cal[i],
+                                                       WC->disp_cal[i].cal,
                                                        ICAL_SUMMARY_PROPERTY);
                                if (p != NULL) {
                                        escputs((char *)
                                                icalproperty_get_comment(p));
-                                       wprintf(" (%s)<BR>\n", timestring);
+                                       wprintf(" (%s)<br />\n", timestring);
                                }
                        }
                }
@@ -494,13 +503,11 @@ void calendar_summary_view(void) {
 void free_calendar_buffer(void) {
        int i;
        if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
-               icalcomponent_free(WC->disp_cal[i]);
+               icalcomponent_free(WC->disp_cal[i].cal);
        }
        WC->num_cal = 0;
        free(WC->disp_cal);
        WC->disp_cal = NULL;
-       free(WC->cal_msgnum);
-       WC->cal_msgnum = NULL;
 }
 
 
@@ -514,7 +521,7 @@ void do_calendar_view(void) {
 
        /* In case no date was specified, go with today */
        now = time(NULL);
-       memcpy(&tm, localtime(&now), sizeof(struct tm));
+       localtime_r(&now, &tm);
        year = tm.tm_year + 1900;
        month = tm.tm_mon + 1;
        day = tm.tm_mday;
@@ -548,4 +555,113 @@ void do_calendar_view(void) {
 
 }
 
+
+/*
+ * Helper function for do_tasks_view().  Returns the date/time due.
+ */
+time_t get_task_due_date(icalcomponent *vtodo) {
+       icalproperty *p;
+
+       if (vtodo == NULL) {
+               return(0L);
+       }
+
+       /* If we're looking at a fully encapsulated VCALENDAR
+        * rather than a VTODO component, recurse into the data
+        * structure until we get a VTODO.
+        */
+       if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
+               return get_task_due_date(
+                       icalcomponent_get_first_component(
+                               vtodo, ICAL_VTODO_COMPONENT
+                       )
+               );
+       }
+
+       p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
+       if (p != NULL) {
+               return(icaltime_as_timet(icalproperty_get_due(p)));
+       }
+       else {
+               return(0L);
+       }
+}
+
+
+/*
+ * Compare the due dates of two tasks (this is for sorting)
+ */
+int task_due_cmp(const void *task1, const void *task2) {
+       time_t t1;
+       time_t t2;
+
+       t1 =  get_task_due_date(((struct disp_cal *)task1)->cal);
+       t2 =  get_task_due_date(((struct disp_cal *)task2)->cal);
+
+       if (t1 < t2) return(-1);
+       if (t1 > t2) return(1);
+       return(0);
+}
+
+
+
+
+
+void do_tasks_view(void) {
+       int i;
+       time_t due;
+       int bg = 0;
+       char buf[SIZ];
+       icalproperty *p;
+
+       wprintf("<div style=\"margin-right:1px\">"
+               "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
+               "<TH>Name of task</TH>\n"
+               "<TH>Date due</TH></TR>\n"
+       );
+
+       /* Sort them if necessary */
+       if (WC->num_cal > 1) {
+               qsort(WC->disp_cal,
+                       WC->num_cal,
+                       sizeof(struct disp_cal),
+                       task_due_cmp
+               );
+       }
+
+       if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
+
+               bg = 1 - bg;
+               wprintf("<TR BGCOLOR=\"#%s\"><TD>",
+                       (bg ? "DDDDDD" : "FFFFFF")
+               );
+
+               p = icalcomponent_get_first_property(WC->disp_cal[i].cal,
+                                                       ICAL_SUMMARY_PROPERTY);
+               wprintf("<A HREF=\"/display_edit_task?msgnum=%ld&taskrm=",
+                       WC->disp_cal[i].cal_msgnum );
+               urlescputs(WC->wc_roomname);
+               wprintf("\">");
+               if (p != NULL) {
+                       escputs((char *)icalproperty_get_comment(p));
+               }
+               wprintf("</A>\n");
+               wprintf("</TD>\n");
+
+               due = get_task_due_date(WC->disp_cal[i].cal);
+               fmt_date(buf, due);
+               wprintf("<TD><FONT");
+               if (due < time(NULL)) {
+                       wprintf(" COLOR=\"#FF0000\"");
+               }
+               wprintf(">%s</FONT></TD></TR>\n", buf);
+       }
+
+       wprintf("</table></div>\n");
+
+       /* Free the list */
+       free_calendar_buffer();
+
+}
+
 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */