Began making changes to do better handling of character sets.
[citadel.git] / webcit / calendar_view.c
index 9c273e386670b74ec80c3af5c63262def4e88029..dde6f216908bf6dd484c08c91e64e8b9edbbf0b0 100644 (file)
@@ -1,42 +1,38 @@
 /*
  * $Id$
- *
- *
  */
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
-#include <time.h>
+/**
+ * \defgroup CalHtmlHandles Handles the HTML display of calendar items.
+ * \ingroup Calendaring
+ */
+/*@{*/
 #include "webcit.h"
 #include "webserver.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");
+/**\brief stub for non-libical builds */
+void do_calendar_view(void) {
+       wprintf("<center><i>");
+       wprintf(_("The calendar view is not available."));
+       wprintf("</i></center><br />\n");
+}
+
+/**\brief stub for non-libical builds */
+void do_tasks_view(void) {     
+       wprintf("<center><I>");
+       wprintf(_("The tasks view is not available."));
+       wprintf("</i></center><br />\n");
 }
 
 #else  /* WEBCIT_WITH_CALENDAR_SERVICE */
 
 /****************************************************************************/
 
-
+/**
+ * \brief Display one day of a whole month view of a calendar
+ * \param thetime the month we want to see 
+ */
 void calendar_month_view_display_events(time_t thetime) {
        int i;
        time_t event_tt;
@@ -48,17 +44,17 @@ 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);
@@ -68,39 +64,30 @@ void calendar_month_view_display_events(time_t thetime) {
                        else all_day_event = 0;
 
                        if (all_day_event) {
-                               memcpy(&event_tm, gmtime(&event_tt), sizeof(struct tm));
+                               gmtime_r(&event_tt, &event_tm);
                        }
                        else {
-                               memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
+                               localtime_r(&event_tt, &event_tm);
                        }
 
-lprintf(9, "Event: %04d/%02d/%02d, Now: %04d/%02d/%02d\n",
-       event_tm.tm_year,
-       event_tm.tm_mon,
-       event_tm.tm_mday,
-       today_tm.tm_year,
-       today_tm.tm_mon,
-       today_tm.tm_mday);
-
-
                        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)) {
 
                                p = icalcomponent_get_first_property(
-                                                       WC->disp_cal[i],
+                                                       WC->disp_cal[i].cal,
                                                        ICAL_SUMMARY_PROPERTY);
                                if (p != NULL) {
 
                                        if (all_day_event) {
-                                               wprintf("<TABLE border=0 cellpadding=2><TR>"
-                                                       "<TD BGCOLOR=\"#CCCCDD\">"
+                                               wprintf("<table border=0 cellpadding=2><TR>"
+                                                       "<td bgcolor=\"#CCCCDD\">"
                                                );
                                        }
 
-                                       wprintf("<FONT SIZE=-1>"
-                                               "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
-                                               WC->cal_msgnum[i],
+                                       wprintf("<font size=-1>"
+                                               "<a href=\"display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
+                                               WC->disp_cal[i].cal_msgnum,
                                                bstr("calview"),
                                                bstr("year"),
                                                bstr("month"),
@@ -108,10 +95,10 @@ lprintf(9, "Event: %04d/%02d/%02d, Now: %04d/%02d/%02d\n",
                                        );
                                        escputs((char *)
                                                icalproperty_get_comment(p));
-                                       wprintf("</A></FONT><BR>\n");
+                                       wprintf("</a></font><br />\n");
 
                                        if (all_day_event) {
-                                               wprintf("</TD></TR></TABLE>");
+                                               wprintf("</td></tr></table>");
                                        }
 
                                }
@@ -124,7 +111,117 @@ lprintf(9, "Event: %04d/%02d/%02d, Now: %04d/%02d/%02d\n",
 }
 
 
+/**
+ * \brief Display one day of a whole month view of a calendar
+ * \param thetime the month we want to see 
+ */
+void calendar_month_view_brief_events(time_t thetime, const char *daycolor) {
+       int i;
+       time_t event_tt;
+       time_t event_tts;
+       time_t event_tte;
+       struct tm event_tms;
+       struct tm event_tme;
+       struct tm today_tm;
+       icalproperty *p;
+       icalproperty *e;
+       struct icaltimetype t;
+       int month, day, year;
+       int all_day_event = 0;
+       char calhourformat[16];
+       char *timeformat;
+
+       get_preference("calhourformat", calhourformat, sizeof calhourformat);
+       if (!strcasecmp(calhourformat, "24"))   timeformat="%k:%M";
+       else timeformat="%I:%M %p";
+
+       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].cal,
+                                               ICAL_DTSTART_PROPERTY);
+               if (p != NULL) {
+                       t = icalproperty_get_dtstart(p);
+                       event_tt = icaltime_as_timet(t);
+                       event_tts=event_tt;
+                       if (t.is_date) all_day_event = 1;
+                       else all_day_event = 0;
+
+                       if (all_day_event) {
+                               gmtime_r(&event_tts, &event_tms);
+                       }
+                       else {
+                               localtime_r(&event_tts, &event_tms);
+                       }
+                       /** \todo epoch &! daymask */
+                       if ((event_tms.tm_year == today_tm.tm_year)
+                          && (event_tms.tm_mon == today_tm.tm_mon)
+                          && (event_tms.tm_mday == today_tm.tm_mday)) {
+                               
+                               
+                               char sbuf[255];
+                               char ebuf[255];
+
+                               p = icalcomponent_get_first_property(
+                                                       WC->disp_cal[i].cal,
+                                                       ICAL_SUMMARY_PROPERTY);
+                               e = icalcomponent_get_first_property(
+                                                       WC->disp_cal[i].cal, 
+                                                       ICAL_DTEND_PROPERTY);
+                               if ((p != NULL) && (e != NULL)) {
+                                       time_t difftime;
+                                       int hours, minutes;
+                                       t = icalproperty_get_dtend(e);
+                                       event_tte = icaltime_as_timet(t);
+                                       localtime_r(&event_tte, &event_tme);
+                                       difftime=(event_tte-event_tts)/60;
+                                       hours=(int)(difftime / 60);
+                                       minutes=difftime % 60;
+                                       wprintf("<tr><td bgcolor='%s'>%i:%2i</td><td bgcolor='%s'>"
+                                                       "<font size=-1>"
+                                                       "<a href=\"display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
+                                                       daycolor,
+                                                       hours, minutes,
+                                                       daycolor,
+                                                       WC->disp_cal[i].cal_msgnum,
+                                                       bstr("calview"),
+                                                       bstr("year"),
+                                                       bstr("month"),
+                                                       bstr("day")
+                                                       );
+
+                                       escputs((char *)
+                                                       icalproperty_get_comment(p));
+                                       /** \todo: allso ammitime format */
+                                       wc_strftime(&sbuf[0], sizeof(sbuf), timeformat, &event_tms);
+                                       wc_strftime(&ebuf[0], sizeof(sbuf), timeformat, &event_tme);
+
+                                       wprintf("</a></font></td>"
+                                                       "<td bgcolor='%s'>%s</td><td bgcolor='%s'>%s</td></tr>",
+                                                       daycolor,
+                                                       sbuf,
+                                                       daycolor,
+                                                       ebuf);
+                                       
+                               }
+                               
+                       }
+                       
+                       
+               }
+       }
+}
+
 
+/**
+ * \brief view one month. pretty view
+ * \param year the year
+ * \param month the month
+ * \param day the actual day we want to see
+ */
 void calendar_month_view(int year, int month, int day) {
        struct tm starting_tm;
        struct tm tm;
@@ -132,8 +229,11 @@ void calendar_month_view(int year, int month, int day) {
        int i;
        time_t previous_month;
        time_t next_month;
+       time_t colheader_time;
+       struct tm colheader_tm;
+       char colheader_label[32];
 
-       /* Determine what day to start.
+       /** Determine what day to start.
         * First, back up to the 1st of the month...
         */
        memset(&starting_tm, 0, sizeof(struct tm));
@@ -145,115 +245,252 @@ 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 */
+       /** Determine previous and next months ... for links */
        previous_month = thetime - (time_t)864000L;     /* back 10 days */
        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));
+       /** Now back up until we're on a Sunday */
+       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 "
+       /** Outer table (to get the background color) */
+       wprintf("<div class=\"fix_scrollbar_bug\">"
+               "<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>");
+       wprintf("<td align=center>");
 
-       memcpy(&tm, localtime(&previous_month), sizeof(struct tm));
-       wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
+       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("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>\n");
 
+       wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
        wprintf("&nbsp;&nbsp;"
-               "<FONT SIZE=+1 COLOR=\"#FFFFFF\">"
+               "<font size=+1 color=\"#FFFFFF\">"
                "%s %d"
-               "</FONT>"
-               "&nbsp;&nbsp;", months[month-1], year);
+               "</font>"
+               "&nbsp;&nbsp;", colheader_label, year);
 
-       memcpy(&tm, localtime(&next_month), sizeof(struct tm));
-       wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
+       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("<img align=middle src=\"static/nextdate_32x.gif\" border=0></A>\n");
 
-       wprintf("</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 "
-               "bgcolor=#204B78><TR>");
+       /** Inner table (the real one) */
+       wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
+               "bgcolor=#204B78><tr>");
+       colheader_time = thetime;
        for (i=0; i<7; ++i) {
-               wprintf("<TD ALIGN=CENTER WIDTH=14%%>"
-                       "<FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
+               colheader_time = thetime + (i * 86400) ;
+               localtime_r(&colheader_time, &colheader_tm);
+               wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
+               wprintf("<td align=center width=14%%>"
+                       "<font color=\"#FFFFFF\">%s</font></th>", colheader_label);
+
        }
-       wprintf("</TR>\n");
+       wprintf("</tr>\n");
 
-       /* Now do 35 days */
+       /** 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 */
+               /** Before displaying Sunday, start a new row */
                if ((i % 7) == 0) {
-                       wprintf("<TR>");
+                       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"))
                );
                if ((i==0) || (tm.tm_mday == 1)) {
-                       wprintf("%s ", months[tm.tm_mon]);
+                       wc_strftime(colheader_label, sizeof colheader_label, "%B", &tm);
+                       wprintf("%s ", colheader_label);
                }
-               wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
-                       "%d</A></B><BR>",
+               wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
+                       "%d</a></b><br />",
                        tm.tm_year + 1900,
                        tm.tm_mon + 1,
                        tm.tm_mday,
                        tm.tm_mday);
 
-               /* put the data here, stupid */
+               /** put the data here, stupid */
                calendar_month_view_display_events(thetime);
 
-               wprintf("</TD>");
+               wprintf("</td>");
 
-               /* After displaying Saturday, end the row */
+               /** After displaying Saturday, end the row */
                if ((i % 7) == 6) {
-                       wprintf("</TR>\n");
+                       wprintf("</tr>\n");
                }
 
-               thetime += (time_t)86400;               /* ahead 24 hours */
+               thetime += (time_t)86400;               /** ahead 24 hours */
        }
 
-       wprintf("</TABLE>"                      /* end of inner table */
-               "</TD></TR></TABLE>"            /* end of outer table */
-               "</CENTER>\n");
+       wprintf("</table>"                      /** end of inner table */
+               "</td></tr></table>"            /** end of outer table */
+               "</div>\n");
 }
 
+/**
+ * \brief view one month. brief view
+ * \param year the year
+ * \param month the month
+ * \param day the actual day we want to see
+ */
+void calendar_brief_month_view(int year, int month, int day) {
+       struct tm starting_tm;
+       struct tm tm;
+       time_t thetime;
+       int i;
+       time_t previous_month;
+       time_t next_month;
+       char month_label[32];
+
+       /** Determine what day to start.
+        * First, back up to the 1st of the month...
+        */
+       memset(&starting_tm, 0, sizeof(struct tm));
+       starting_tm.tm_year = year - 1900;
+       starting_tm.tm_mon = month - 1;
+       starting_tm.tm_mday = day;
+       thetime = mktime(&starting_tm);
+
+       memcpy(&tm, &starting_tm, sizeof(struct tm));
+       while (tm.tm_mday != 1) {
+               thetime = thetime - (time_t)86400;      /* go back 24 hours */
+               localtime_r(&thetime, &tm);
+       }
+
+       /** Determine previous and next months ... for links */
+       previous_month = thetime - (time_t)864000L;     /* back 10 days */
+       next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
+
+       /** Now back up until we're on a Sunday */
+       localtime_r(&thetime, &tm);
+       while (tm.tm_wday != 0) {
+               thetime = thetime - (time_t)86400;      /* go back 24 hours */
+               localtime_r(&thetime, &tm);
+       }
+
+       /** Outer table (to get the background color) */
+       wprintf("<div class=\"fix_scrollbar_bug\">"
+               "<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>\n");
+
+       wprintf("<td align=center>");
+
+       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/prevdate_32x.gif\" border=0></A>\n");
+
+       wc_strftime(month_label, sizeof month_label, "%B", &tm);
+       wprintf("&nbsp;&nbsp;"
+               "<font size=+1 color=\"#FFFFFF\">"
+               "%s %d"
+               "</font>"
+               "&nbsp;&nbsp;", month_label, year);
+
+       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/nextdate_32x.gif\" border=0></A>\n");
+
+       wprintf("</td></tr></table>\n");
+
+       /** Inner table (the real one) */
+       wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
+               "bgcolor=#EEEECC><TR>");
+       wprintf("</tr>\n");
+       wprintf("<tr><td colspan=\"100%\">\n");
+
+       /** Now do 35 days */
+       for (i = 0; i < 35; ++i) {
+               char weeknumber[255];
+               char weekday_name[32];
+               char *daycolor;
+               localtime_r(&thetime, &tm);
 
+
+               /** Before displaying Sunday, start a new CELL */
+               if ((i % 7) == 0) {
+                       wc_strftime(&weeknumber[0], sizeof(weeknumber), "%U", &tm);
+                       wprintf("<table border='0' bgcolor=\"#EEEECC\" width='100%'> <tr><th colspan='4'>%s %s</th></tr>"
+                                       "   <tr><td>%s</td><td width='70%'>%s</td><td>%s</td><td>%s</td></tr>\n",
+                                       _("Week"), 
+                                       weeknumber,
+                                       _("Hours"),
+                                       _("Subject"),
+                                       _("Start"),
+                                       _("End")
+                                       );
+               }
+               
+               daycolor=((tm.tm_mon != month-1) ? "DDDDDD" :
+                                 ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
+                                  "FFFFFF"));
+               
+               /** Day Header */
+               wc_strftime(weekday_name, sizeof weekday_name, "%A", &tm);
+               wprintf("<tr><td bgcolor='%s' colspan='1' align='left'> %s,%i."
+                               "</td><td bgcolor='%s' colspan='3'><hr></td></tr>\n",
+                               daycolor,
+                               weekday_name,tm.tm_mday,
+                               daycolor);
+
+               /** put the data of one day  here, stupid */
+               calendar_month_view_brief_events(thetime, daycolor);
+
+
+               /** After displaying Saturday, end the row */
+               if ((i % 7) == 6) {
+                       wprintf("</td></tr></table>\n");
+               }
+
+               thetime += (time_t)86400;               /** ahead 24 hours */
+       }
+
+       wprintf("</table>"                      /** end of inner table */
+               "</td></tr></table>"            /** end of outer table */
+               "</div>\n");
+}
+
+/** 
+ * \brief view one week
+ * this should view just one week, but it's not here yet.
+ * \todo ny implemented
+ * \param year the year
+ * \param month the month
+ * \param day the day which we want to see the week around
+ */
 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");
 }
 
 
-/*
+/**
+ * \brief display one day
  * Display events for a particular hour of a particular day.
  * (Specify hour < 0 to show "all day" events)
+ * \param year the year
+ * \param month the month
+ * \param day the day
+ * \param hour the hour we want to start displaying?????
  */
 void calendar_day_view_display_events(int year, int month,
                                        int day, int hour) {
@@ -265,17 +502,22 @@ 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");
+               // \todo 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);
-                       if (t.is_date) all_day_event = 1;
+                       if (t.is_date) {
+                               all_day_event = 1;
+                       }
+                       else {
+                               all_day_event = 0;
+                       }
 
                        if (all_day_event) {
                                event_tm = gmtime(&event_tt);
@@ -292,27 +534,27 @@ void calendar_day_view_display_events(int year, int month,
 
 
                                p = icalcomponent_get_first_property(
-                                                       WC->disp_cal[i],
+                                                       WC->disp_cal[i].cal,
                                                        ICAL_SUMMARY_PROPERTY);
                                if (p != NULL) {
 
                                        if (all_day_event) {
-                                               wprintf("<TABLE border=1 cellpadding=2><TR>"
-                                                       "<TD BGCOLOR=\"#CCCCCC\">"
+                                               wprintf("<table border=1 cellpadding=2><TR>"
+                                                       "<td bgcolor=\"#CCCCCC\">"
                                                );
                                        }
 
-                                       wprintf("<FONT SIZE=-1>"
-                                               "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
-                                               WC->cal_msgnum[i],
+                                       wprintf("<font size=-1>"
+                                               "<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
+                                               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>");
+                                               wprintf("</td></tr></table>");
                                        }
                                }
 
@@ -324,13 +566,30 @@ void calendar_day_view_display_events(int year, int month,
 }
 
 
-
+/**
+ * \brief view one day
+ * \param year the year
+ * \param month the month 
+ * \param day the day we want to display
+ */
 void calendar_day_view(int year, int month, int day) {
        int hour;
        struct icaltimetype today, yesterday, tomorrow;
-
-
-       /* Figure out the dates for "yesterday" and "tomorrow" links */
+       char calhourformat[16];
+       int daystart = 8;
+       int dayend = 17;
+       char daystart_str[16], dayend_str[16];
+       struct tm d_tm;
+       char d_str[128];
+
+       get_preference("calhourformat", calhourformat, sizeof calhourformat);
+       get_preference("daystart", daystart_str, sizeof daystart_str);
+       if (strlen(daystart_str) > 0) daystart = atoi(daystart_str);
+       get_preference("dayend", dayend_str, sizeof dayend_str);
+       if (strlen(dayend_str) > 0) dayend = atoi(dayend_str);
+       
+
+       /** Figure out the dates for "yesterday" and "tomorrow" links */
 
        memset(&today, 0, sizeof(struct icaltimetype));
        today.year = year;
@@ -347,123 +606,129 @@ void calendar_day_view(int year, int month, int day) {
        tomorrow = icaltime_normalize(tomorrow);
 
 
-       /* Outer table (to get the background color) */
-       wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
-               "bgcolor=#204B78><TR><TD>\n");
+       /** Outer table (to get the background color) */
+       wprintf("<div class=\"fix_scrollbar_bug\">"
+               "<table width=100%% border=0 cellpadding=0 cellspacing=0 "
+               "bgcolor=#204B78><tr><td>\n");
 
-       /* Inner table (the real one) */
-       wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
-               "bgcolor=#204B78><TR>\n");
+       /** Inner table (the real one) */
+       wprintf("<table width=100%% border=0 cellpadding=1 cellspacing=1 "
+               "bgcolor=#204B78><tr>\n");
 
-       /* Innermost table (contains hours etc.) */
-       wprintf("<TD WIDTH=80%%>"
-               "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
+       /** Innermost table (contains hours etc.) */
+       wprintf("<td width=80%%>"
+               "<table width=100%% border=0 cellpadding=1 cellspacing=1 "
                "bgcolor=#204B78>\n");
 
-       /* Display events before 8:00 (hour=-1 is all-day events) */
-       wprintf("<TR>"
-               "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
-               "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
-       for (hour = (-1); hour <= 7; ++hour) {
+       /** Display events before 8:00 (hour=-1 is all-day events) */
+       wprintf("<tr>"
+               "<td bgcolor=\"#CCCCDD\" valign=middle width=10%%></td>"
+               "<td bgcolor=\"#FFFFFF\" valign=top>");
+       for (hour = (-1); hour <= (daystart-1); ++hour) {
                calendar_day_view_display_events(year, month, day, hour);
        }
-       wprintf("</TD></TR>\n");
+       wprintf("</td></tr>\n");
 
-       /* Now the middle of the day... */      
-       for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
-               wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
-                       "VALIGN=MIDDLE WIDTH=10%%>");
-               wprintf("<A HREF=\"/display_edit_event?msgnum=0"
+       /** Now the middle of the day... */     
+       for (hour = daystart; hour <= dayend; ++hour) { /* could do HEIGHT=xx */
+               wprintf("<tr height=30><td bgcolor=\"#CCCCDD\" align=middle "
+                       "valign=middle width=10%%>");
+               wprintf("<a href=\"display_edit_event?msgnum=0"
                        "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
                        year, month, day, hour
                );
-               wprintf("%d:00%s</A> ",
-                       (hour <= 12 ? hour : hour-12),
-                       (hour < 12 ? "am" : "pm")
-               );
-               wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
+
+               if (!strcasecmp(calhourformat, "24")) {
+                       wprintf("%2d:00</a> ", hour);
+               }
+               else {
+                       wprintf("%d:00%s</a> ",
+                               (hour <= 12 ? hour : hour-12),
+                               (hour < 12 ? "am" : "pm")
+                       );
+               }
+
+               wprintf("</td><td bgcolor=\"#FFFFFF\" valign=top>");
 
                /* put the data here, stupid */
                calendar_day_view_display_events(year, month, day, hour);
 
-               wprintf("</TD></TR>\n");
+               wprintf("</td></tr>\n");
        }
 
-       /* Display events after 5:00... */
-       wprintf("<TR>"
-               "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
-               "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
-       for (hour = 18; hour <= 23; ++hour) {
+       /** Display events after 5:00... */
+       wprintf("<tr>"
+               "<td bgcolor=\"#CCCCDD\" valign=middle width=10%%></td>"
+               "<td bgcolor=\"#FFFFFF\" valign=top>");
+       for (hour = (dayend+1); hour <= 23; ++hour) {
                calendar_day_view_display_events(year, month, day, hour);
        }
-       wprintf("</TD></TR>\n");
+       wprintf("</td></tr>\n");
 
 
-       wprintf("</TABLE>"                      /* end of innermost table */
-               "</TD>"
+       wprintf("</table>"                      /* end of innermost table */
+               "</td>"
        );
 
-       wprintf("<TD WIDTH=20%% VALIGN=top>");  /* begin stuff-on-the-right */
+       wprintf("<td width=20%% valign=top>");  /* begin stuff-on-the-right */
 
 
-       /* Begin todays-date-with-left-and-right-arrows */
-       wprintf("<TABLE BORDER=0 WIDTH=100%% "
-               "CELLSPACING=0 CELLPADDING=0 BGCOLOR=\"#FFFFFF\">\n");
-       wprintf("<TR>");
+       /** Begin todays-date-with-left-and-right-arrows */
+       wprintf("<table border=0 width=100%% "
+               "cellspacing=0 cellpadding=0 bgcolor=\"#FFFFFF\">\n");
+       wprintf("<tr>");
 
-       /* Left arrow */        
-       wprintf("<TD ALIGN=CENTER>");
-       wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
+       /** Left arrow */       
+       wprintf("<td align=center>");
+       wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
                yesterday.year, yesterday.month, yesterday.day);
-       wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>");
-       wprintf("</TD>");
-
-       /* 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>",
-               months[month-1], day, year);
-       wprintf("</TD>");
-
-       /* Right arrow */
-       wprintf("<TD ALIGN=CENTER>");
-       wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
-               tomorrow.year, tomorrow.month, tomorrow.day);
-       wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
-               " BORDER=0></A>\n");
-       wprintf("</TD>");
-
-       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("<img align=middle src=\"static/prevdate_32x.gif\" border=0></A>");
+       wprintf("</td>");
+
+       /** Today's date */
+       memset(&d_tm, 0, sizeof d_tm);
+       d_tm.tm_year = year - 1900;
+       d_tm.tm_mon = month - 1;
+       d_tm.tm_mday = day;
+       wc_strftime(d_str, sizeof d_str,
+               "<td align=center>"
+               "<font size=+2>%B</font><br />"
+               "<font size=+3>%d</font><br />"
+               "<font size=+2>%Y</font><br />"
+               "</td>",
+               &d_tm
        );
+       wprintf("%s", d_str);
+
+       /** Right arrow */
+       wprintf("<td align=center>");
+       wprintf("<a href=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
+               tomorrow.year, tomorrow.month, tomorrow.day);
+       wprintf("<img align=middle src=\"static/nextdate_32x.gif\""
+               " border=0></A>\n");
+       wprintf("</td>");
+
+       wprintf("</tr></table>\n");
+       /** End todays-date-with-left-and-right-arrows */
 
-       wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
-               "Back to month view</A>\n", year, month);
+       /** \todo In the future we might want to put a month-o-matic here */
 
-       wprintf("</FONT></CENTER>\n");
+       wprintf("</font></center>\n");
 
-       wprintf("</TD>");                       /* end stuff-on-the-right */
+       wprintf("</td>");                       /** end stuff-on-the-right */
 
 
 
-       wprintf("</TR></TABLE>"                 /* end of inner table */
-               "</TD></TR></TABLE>"            /* end of outer table */
+       wprintf("</tr></table>"                 /** end of inner table */
+               "</td></tr></table></div>"      /** end of outer table */
        );
 
 
 
 }
 
-/*
- * Display today's events.
+/**
+ * \brief Display today's events.
  */
 void calendar_summary_view(void) {
        int i;
@@ -481,22 +746,27 @@ 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;
+                       if (t.is_date) {
+                               all_day_event = 1;
+                       }
+                       else {
+                               all_day_event = 0;
+                       }
                        fmt_time(timestring, event_tt);
 
                        if (all_day_event) {
-                               memcpy(&event_tm, gmtime(&event_tt), sizeof(struct tm));
+                               gmtime_r(&event_tt, &event_tm);
                        }
                        else {
-                               memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
+                               localtime_r(&event_tt, &event_tm);
                        }
 
                        if ( (event_tm.tm_year == today_tm.tm_year)
@@ -506,12 +776,12 @@ void calendar_summary_view(void) {
 
 
                                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);
                                }
                        }
                }
@@ -520,41 +790,45 @@ void calendar_summary_view(void) {
 }
 
 
-
+/**
+ * \brief clean up ical memory
+ * \todo this could get troubel with future ical versions
+ */
 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;
 }
 
 
 
-
+/**
+ * \brief do the whole calendar page
+ * view any part of the calender. decide which way, etc.
+ */
 void do_calendar_view(void) {
        time_t now;
        struct tm tm;
        int year, month, day;
        char calview[SIZ];
 
-       /* In case no date was specified, go with today */
+       /** 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;
 
-       /* Now see if a date was specified */
+       /** Now see if a date was specified */
        if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
        if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
        if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
 
-       /* How would you like that cooked? */
+       /** How would you like that cooked? */
        if (strlen(bstr("calview")) > 0) {
                strcpy(calview, bstr("calview"));
        }
@@ -562,7 +836,7 @@ void do_calendar_view(void) {
                strcpy(calview, "month");
        }
 
-       /* Display the selected view */
+       /** Display the selected view */
        if (!strcasecmp(calview, "day")) {
                calendar_day_view(year, month, day);
        }
@@ -570,12 +844,141 @@ void do_calendar_view(void) {
                calendar_week_view(year, month, day);
        }
        else {
-               calendar_month_view(year, month, day);
+               if (WC->wc_view == VIEW_CALBRIEF) {
+                       calendar_brief_month_view(year, month, day);
+               }
+               else {
+                       calendar_month_view(year, month, day);
+               }
        }
 
-       /* Free the calendar stuff */
+       /** Free the calendar stuff */
+       free_calendar_buffer();
+
+}
+
+
+/**
+ * \brief get task due date
+ * Helper function for do_tasks_view().  
+ * \param vtodo a task to get the due date
+ * \return 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);
+       }
+}
+
+
+/**
+ * \brief Compare the due dates of two tasks (this is for sorting)
+ * \param task1 first task to compare
+ * \param task2 second task to compare
+ */
+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);
+}
+
+
+
+
+/**
+ * \brief do the whole task view stuff
+ */
+void do_tasks_view(void) {
+       int i;
+       time_t due;
+       int bg = 0;
+       char buf[SIZ];
+       icalproperty *p;
+
+       wprintf("<div class=\"fix_scrollbar_bug\">"
+               "<table border=0 cellspacing=0 width=100%% bgcolor=\"#FFFFFF\">\n<tr>\n"
+               "<th>");
+       wprintf(_("Name of task"));
+       wprintf("</th><th>");
+       wprintf(_("Date due"));
+       wprintf("</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("\">");
+               wprintf("<img align=middle "
+                       "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;");
+               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, 0);
+               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 */
+
+/** @} */