timekeeper

My first (abandoned unfinished) web application for time tracking
git clone git://jacobedwards.org/timekeeper
Log | Files | Refs | README

archive.c (2989B)


      1 #include <assert.h>
      2 #define const
      3 
      4 #include <time.h>
      5 
      6 #include "common.h"
      7 #include "pages.h"
      8 #include "../times.h"
      9 
     10 enum kcgi_err
     11 showform(struct pagedata *pd, char *name, int page, int64_t period)
     12 {
     13 	enum kcgi_err status;
     14 
     15 	if ((status = khtml_attr(&pd->html, KELEM_FORM,
     16 	        KATTR_ACTION, pd->pages[page], KATTR__MAX)) != KCGI_OK ||
     17 	    (status = khtml_attrx(&pd->html, KELEM_INPUT,
     18 	        KATTR_TYPE, KATTRX_STRING, "hidden", 
     19 	        KATTR_NAME, KATTRX_STRING, pd->keys[KeyPeriod].name,
     20 		KATTR_VALUE, KATTRX_INT, period,
     21 		KATTR__MAX)) != KCGI_OK ||
     22 	    (status = khtml_attr(&pd->html, KELEM_INPUT,
     23 	        KATTR_TYPE, "submit",
     24 		KATTR_VALUE, name, KATTR__MAX)) != KCGI_OK ||
     25 	    (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
     26 		return status;
     27 	return KCGI_OK;
     28 }
     29 
     30 enum kcgi_err
     31 showperiod(struct pagedata *pd, struct timesheet **rts)
     32 {
     33 	enum kcgi_err status;
     34 	time_t from, to;
     35 	struct timesheet *ts, *lts;
     36 	time_t duration;
     37 	unsigned int period;
     38 	unsigned int count;
     39 
     40 	ts = *rts;	
     41 
     42 	if (!ts->period) {
     43 		while (ts && !ts->period)
     44 			ts = ts->next;
     45 		*rts = ts;
     46 		return KCGI_OK;
     47 	}
     48 
     49 	assert(ts->set & StartTimeFlag);
     50 	from = ts->times[StartTime];
     51 
     52 	duration = 0;
     53 	period = ts->period;
     54 	count = 0;
     55 	for (; ts && ts->period == period; ts = ts->next) {
     56 		duration += getduration(ts);
     57 		lts = ts;
     58 		++count;
     59 	}
     60 
     61 	assert(lts->set & EndTimeFlag);
     62 	to = lts->times[EndTime];
     63 
     64 	if ((status = khtml_attrx(&pd->html, KELEM_LI,
     65 	        KATTR_VALUE, KATTRX_INT, (int64_t)period,
     66 	        KATTR__MAX)) != KCGI_OK ||
     67 	    (status = htmldate(pd, from)) != KCGI_OK ||
     68 	    (status = khtml_puts(&pd->html, " to ")) != KCGI_OK ||
     69 	    (status = htmldate(pd, to)) != KCGI_OK ||
     70 	    (status = khtml_printf(&pd->html, "; %d entries, ", count)) != KCGI_OK ||
     71 	    (status = htmlduration(pd, duration, NULL, 0)) != KCGI_OK ||
     72 	    (status = khtml_puts(&pd->html, " hours")) != KCGI_OK ||
     73 	    (status = showform(pd, "Show", PageMain, period)) != KCGI_OK ||
     74 	    (status = showform(pd, "Export", PageExport, period)) != KCGI_OK ||
     75 	    (status = khtml_closeelem(&pd->html, 1)))
     76 		return status;
     77 
     78 	*rts = ts;
     79 	return KCGI_OK;
     80 }
     81 
     82 enum kcgi_err
     83 pagearchive(struct pagedata *pd)
     84 {
     85 	static char *css[] = { "css/main.css", NULL };
     86 	static struct pagetemplate template = {
     87 		"Archive",
     88 		.css = css
     89 	};
     90 
     91 	enum kcgi_err status;
     92 	struct timesheet *times, *firsttimes;
     93 
     94 	if (!pd->user)
     95 		return tk_prompt_login(pd);
     96 
     97 	if ((status = tk_startpage(pd, &template, KHTTP_200)) != KCGI_OK)
     98 		return status;
     99 
    100 	if (gettimes(pd, pd->user->hash, -1, &times))
    101 		return KCGI_SYSTEM;
    102 
    103 	if ((status = htmlwithin(pd, KELEM_H1, "Archive")) != KCGI_OK ||
    104 	    (status = htmlwithin(pd, KELEM_H2, "Periods")) != KCGI_OK ||
    105 	    (status = khtml_elem(&pd->html, KELEM_OL)) != KCGI_OK)
    106 		return status;
    107 
    108 	firsttimes = times;
    109 	while (times && showperiod(pd, &times) == KCGI_OK)
    110 		;
    111 	freetimesheet(firsttimes);
    112 
    113 	if ((status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
    114 		return status;
    115 	return endpage(pd, &template);
    116 }