* Added utility functions for displaying vcalendar timestamps in web forms
authorArt Cancro <ajc@citadel.org>
Sat, 21 Sep 2002 04:38:05 +0000 (04:38 +0000)
committerArt Cancro <ajc@citadel.org>
Sat, 21 Sep 2002 04:38:05 +0000 (04:38 +0000)
  and translating form data back to timestamps.
* Completed the "edit task" and "add new task" screens.

webcit/ChangeLog
webcit/Makefile.in
webcit/calendar.c
webcit/calendar_tools.c [new file with mode: 0644]
webcit/messages.c
webcit/webcit.h

index a31ffbb88ae568f7e716bda017476ef1d8aae369..a79e4bdabaa4f4d4159024638bc66a79e4cef325 100644 (file)
@@ -1,4 +1,9 @@
 $Log$
+Revision 400.20  2002/09/21 04:38:05  ajc
+* Added utility functions for displaying vcalendar timestamps in web forms
+  and translating form data back to timestamps.
+* Completed the "edit task" and "add new task" screens.
+
 Revision 400.19  2002/09/20 20:28:08  ajc
 * Smooth transition between "list tasks" & "edit task" screens
 
@@ -1004,4 +1009,3 @@ Sun Dec  6 19:50:55 EST 1998 Art Cancro <ajc@uncnsrd.mt-kisco.ny.us>
 
 1998-12-03 Nathan Bryant <bryant@cs.usm.maine.edu>
        * webserver.c: warning fix
-
index e2f726a434a54bfcec0babbec565376f2dc833d8..c843bbff1aee927aa919b6c55786a79232dbb568 100644 (file)
@@ -28,13 +28,13 @@ webserver: webserver.o context_loop.o tools.o \
        roomops.o messages.o userlist.o paging.o sysmsgs.o useredit.o \
        vcard.o vcard_edit.o preferences.o html2html.o listsub.o \
        mime_parser.o graphics.o netconf.o siteconfig.o subst.o \
-       calendar.o $(LIBOBJS)
+       calendar.o calendar_tools.o $(LIBOBJS)
        $(CC) webserver.o context_loop.o tools.o cookie_conversion.o \
        webcit.o auth.o tcp_sockets.o mainmenu.o serv_func.o who.o listsub.o \
        roomops.o messages.o userlist.o paging.o sysmsgs.o useredit.o \
        locate_host.o siteconfig.o subst.o vcard.o vcard_edit.o floors.o \
        mime_parser.o graphics.o netconf.o preferences.o html2html.o \
-       summary.o calendar.o \
+       summary.o calendar.o calendar_tools.o \
        $(LIBOBJS) $(LIBS) -o webserver
 
 .c.o:
index 9bd6c062bf54a02cc6be1500747e552dacd3e5cf..f5ff9cbb76d7f328304b31dcbaeba3256efc6a6a 100644 (file)
 #include <stdarg.h>
 #include <pthread.h>
 #include <signal.h>
+#include <time.h>
 #include "webcit.h"
 #include "webserver.h"
 
-#ifdef HAVE_ICAL_H
-#include <ical.h>
-#endif
-
-
 #ifndef HAVE_ICAL_H
 
 /*
@@ -247,17 +243,32 @@ void display_individual_task(icalcomponent *vtodo, long msgnum) {
 
        p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
        wprintf("<LI><A HREF=\"/display_edit_task?msgnum=%ld\">", msgnum);
-       if (p != NULL) escputs((char *)icalproperty_get_comment(p));
+       if (p != NULL) {
+               escputs((char *)icalproperty_get_comment(p));
+       }
        wprintf("</A>\n");
-       icalproperty_free(p);
 }
 
 
 /*
  * Display a task by itself (for editing)
  */
-void display_edit_individual_task(icalcomponent *vtodo, long msgnum) {
+void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
+       icalcomponent *vtodo;
        icalproperty *p;
+       struct icaltimetype t;
+       time_t now;
+       int created_new_vtodo = 0;
+
+       now = time(NULL);
+
+       if (supplied_vtodo != NULL) {
+               vtodo = supplied_vtodo;
+       }
+       else {
+               vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
+               created_new_vtodo = 1;
+       }
 
        output_headers(3);
        wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
@@ -274,20 +285,40 @@ void display_edit_individual_task(icalcomponent *vtodo, long msgnum) {
                "<INPUT TYPE=\"text\" NAME=\"summary\" "
                "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
        p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
-       if (p != NULL) escputs((char *)icalproperty_get_comment(p));
-       icalproperty_free(p);
+       if (p != NULL) {
+               escputs((char *)icalproperty_get_comment(p));
+       }
        wprintf("\"><BR>\n");
 
-       wprintf("Start date: FIXME <BR>\n");
+       wprintf("Start date: ");
+       p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
+       if (p != NULL) {
+               t = icalproperty_get_dtstart(p);
+       }
+       else {
+               t = icaltime_from_timet(now, 0);
+       }
+       display_icaltimetype_as_webform(&t, "dtstart");
+       wprintf("<BR>\n");
 
-       wprintf("Due date: FIXME <BR>\n");
+       wprintf("Due date: ");
+       p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
+       if (p != NULL) {
+               t = icalproperty_get_due(p);
+       }
+       else {
+               t = icaltime_from_timet(now, 0);
+       }
+       display_icaltimetype_as_webform(&t, "due");
+       wprintf("<BR>\n");
 
-       wprintf("<CENTER><TEXTAREA NAME=\"msgtext\" wrap=soft "
+       wprintf("<CENTER><TEXTAREA NAME=\"description\" wrap=soft "
                "ROWS=10 COLS=80 WIDTH=80>\n"
        );
        p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
-       if (p != NULL) escputs((char *)icalproperty_get_comment(p));
-       icalproperty_free(p);
+       if (p != NULL) {
+               escputs((char *)icalproperty_get_comment(p));
+       }
        wprintf("</TEXTAREA><BR>\n");
 
        wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
@@ -301,20 +332,68 @@ void display_edit_individual_task(icalcomponent *vtodo, long msgnum) {
        wprintf("</FORM>\n");
 
        wDumpContent(1);
+
+       if (created_new_vtodo) {
+               icalcomponent_free(vtodo);
+       }
 }
 
 /*
  * Save an edited task
  */
-void save_individual_task(icalcomponent *vtodo, long msgnum) {
+void save_individual_task(icalcomponent *supplied_vtodo, long msgnum) {
        char buf[SIZ];
        int delete_existing = 0;
+       icalproperty *prop;
+       icalcomponent *vtodo;
+       int created_new_vtodo = 0;
+
+       if (supplied_vtodo != NULL) {
+               vtodo = supplied_vtodo;
+       }
+       else {
+               vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
+               created_new_vtodo = 1;
+       }
 
        if (!strcasecmp(bstr("sc"), "Save")) {
 
                /* Replace values in the component with ones from the form */
-               /* FIXME ... do this */
 
+               while (prop = icalcomponent_get_first_property(vtodo,
+                     ICAL_SUMMARY_PROPERTY), prop != NULL) {
+                       icalcomponent_remove_property(vtodo, prop);
+               }
+               icalcomponent_add_property(vtodo,
+                       icalproperty_new_summary(bstr("summary")));
+               
+               while (prop = icalcomponent_get_first_property(vtodo,
+                     ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
+                       icalcomponent_remove_property(vtodo, prop);
+               }
+               icalcomponent_add_property(vtodo,
+                       icalproperty_new_description(bstr("description")));
+       
+               while (prop = icalcomponent_get_first_property(vtodo,
+                     ICAL_DTSTART_PROPERTY), prop != NULL) {
+                       icalcomponent_remove_property(vtodo, prop);
+               }
+               icalcomponent_add_property(vtodo,
+                       icalproperty_new_dtstart(
+                               icaltime_from_webform("dtstart")
+                       )
+               );
+       
+               while (prop = icalcomponent_get_first_property(vtodo,
+                     ICAL_DUE_PROPERTY), prop != NULL) {
+                       icalcomponent_remove_property(vtodo, prop);
+               }
+               icalcomponent_add_property(vtodo,
+                       icalproperty_new_due(
+                               icaltime_from_webform("due")
+                       )
+               );
+       
                /* Serialize it and save it to the message base */
                serv_puts("ENT0 1|||4");
                serv_gets(buf);
@@ -323,7 +402,7 @@ void save_individual_task(icalcomponent *vtodo, long msgnum) {
                        serv_puts("");
                        serv_puts(icalcomponent_as_ical_string(vtodo));
                        serv_puts("000");
-                       /* delete_existing = 1; */
+                       delete_existing = 1;
                }
        }
 
@@ -334,11 +413,15 @@ void save_individual_task(icalcomponent *vtodo, long msgnum) {
                delete_existing = 1;
        }
 
-       if (delete_existing) {
+       if ( (delete_existing) && (msgnum > 0L) ) {
                serv_printf("DELE %ld", atol(bstr("msgnum")));
                serv_gets(buf);
        }
 
+       if (created_new_vtodo) {
+               icalcomponent_free(vtodo);
+       }
+
        /* Go back to the task list */
        readloop("readfwd");
 }
@@ -390,9 +473,15 @@ void display_using_handler(long msgnum,
                relevant_source = load_mimepart(msgnum, relevant_partnum);
                if (relevant_source != NULL) {
 
-                       /* Display the task */
                        cal = icalcomponent_new_from_string(relevant_source);
                        if (cal != NULL) {
+
+                               /* Simple components of desired type */
+                               if (icalcomponent_isa(cal) == which_kind) {
+                                       callback(cal, msgnum);
+                               }
+
+                               /* Subcomponents of desired type */
                                for (c = icalcomponent_get_first_component(cal,
                                    which_kind);
                                    (c != 0);
@@ -424,18 +513,30 @@ void display_edit_task(void) {
        long msgnum = 0L;
 
        msgnum = atol(bstr("msgnum"));
-       display_using_handler(msgnum, "text/calendar",
+       if (msgnum > 0L) {
+               /* existing task */
+               display_using_handler(msgnum, "text/calendar",
                                ICAL_VTODO_COMPONENT,
                                display_edit_individual_task);
+       }
+       else {
+               /* new task */
+               display_edit_individual_task(NULL, 0L);
+       }
 }
 
 void save_task(void) {
        long msgnum = 0L;
 
        msgnum = atol(bstr("msgnum"));
-       display_using_handler(msgnum, "text/calendar",
+       if (msgnum > 0L) {
+               display_using_handler(msgnum, "text/calendar",
                                ICAL_VTODO_COMPONENT,
                                save_individual_task);
+       }
+       else {
+               save_individual_task(NULL, 0L);
+       }
 }
 
 #endif /* HAVE_ICAL_H */
diff --git a/webcit/calendar_tools.c b/webcit/calendar_tools.c
new file mode 100644 (file)
index 0000000..77f478e
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * $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>
+#include "webcit.h"
+#include "webserver.h"
+
+#ifdef HAVE_ICAL_H
+
+char *months[] = {
+       "January", "February", "March", "April", "May", "June", "July",
+       "August", "September", "October", "November", "December"
+};
+
+void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
+       int i;
+       time_t now;
+       struct tm *tm;
+       int this_year;
+       const int span = 10;
+
+       now = time(NULL);
+       tm = localtime(&now);
+       this_year = tm->tm_year + 1900;
+
+       if (t == NULL) return;
+
+       wprintf("Month: ");
+       wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
+       for (i=1; i<=12; ++i) {
+               wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
+                       ((t->month == i) ? "SELECTED" : ""),
+                       i,
+                       months[i-1]
+               );
+       }
+       wprintf("</SELECT>\n");
+
+       wprintf("Day: ");
+       wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
+       for (i=1; i<=31; ++i) {
+               wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
+                       ((t->day == i) ? "SELECTED" : ""),
+                       i, i
+               );
+       }
+       wprintf("</SELECT>\n");
+
+       wprintf("Year: ");
+       wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
+       if ((this_year - t->year) > span) {
+               wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
+                       t->year, t->year);
+       }
+       for (i=(this_year-span); i<=(this_year+span); ++i) {
+               wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
+                       ((t->year == i) ? "SELECTED" : ""),
+                       i, i
+               );
+       }
+       if ((t->year - this_year) > span) {
+               wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
+                       t->year, t->year);
+       }
+       wprintf("</SELECT>\n");
+
+       wprintf("Hour: ");
+       wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
+       for (i=0; i<=23; ++i) {
+               wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
+                       ((t->hour == i) ? "SELECTED" : ""),
+                       i, i
+               );
+       }
+       wprintf("</SELECT>\n");
+
+       wprintf("Minute: ");
+       wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
+       for (i=0; i<=59; ++i) {
+               wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
+                       ((t->minute == i) ? "SELECTED" : ""),
+                       i, i
+               );
+       }
+       wprintf("</SELECT>\n");
+
+}
+
+
+struct icaltimetype icaltime_from_webform(char *prefix) {
+       struct icaltimetype t;
+       time_t now;
+       char vname[SIZ];
+
+       now = time(NULL);
+       t = icaltime_from_timet(now, 0);
+
+       sprintf(vname, "%s_month", prefix);     t.month = atoi(bstr(vname));
+       sprintf(vname, "%s_day", prefix);       t.day = atoi(bstr(vname));
+       sprintf(vname, "%s_year", prefix);      t.year = atoi(bstr(vname));
+       sprintf(vname, "%s_hour", prefix);      t.hour = atoi(bstr(vname));
+       sprintf(vname, "%s_minute", prefix);    t.minute = atoi(bstr(vname));
+
+       t = icaltime_normalize(t);
+       return(t);
+}
+
+
+
+
+#endif
index c98a42aadfe1fc85e0bcd9af0fe44b45620811dc..f6a88c416f6a1c0b058815d0bce16221a448eb3e 100644 (file)
@@ -837,7 +837,10 @@ void readloop(char *oper)
        }
 
        if (is_tasks) {
-               wprintf("</UL>\n");
+               wprintf("</UL>\n"
+                       "<A HREF=\"/display_edit_task?msgnum=0\">"
+                       "Add new task</A>\n"
+               );
        }
 
        /* Bump these because although we're thinking in zero base, the user
index 06db86fb1cbd1b533c8c58d6602f6016bd22d404..4f24c5cadb19abe2ad2e4e601af8921d6c6011bf 100644 (file)
@@ -4,6 +4,10 @@
 #include <zlib.h>
 #endif
 
+#ifdef HAVE_ICAL_H
+#include <ical.h>
+#endif
+
 #define SIZ                    4096            /* generic buffer size */
 
 #define TRACE fprintf(stderr, "Checkpoint: %s, %d\n", __FILE__, __LINE__)
@@ -348,4 +352,6 @@ void display_task(long msgnum);
 #ifdef HAVE_ICAL_H
 void display_edit_task(void);
 void save_task(void);
+void display_icaltimetype_as_webform(struct icaltimetype *, char *);
+struct icaltimetype icaltime_from_webform(char *prefix);
 #endif