Readloop remove special cases
[citadel.git] / webcit / summary.c
1 /*
2  * Displays the "Summary Page"
3  *
4  * Copyright (c) 1996-2011 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include "webcit.h"
22 #include "calendar.h"
23
24 extern int calendar_summary_view(void);
25
26 /*
27  * Display today's date in a friendly format
28  */
29 void output_date(void) {
30         struct tm tm;
31         time_t now;
32         char buf[128];
33
34         time(&now);
35         localtime_r(&now, &tm);
36
37         wc_strftime(buf, 32, "%A, %x", &tm);
38         wc_printf("%s", buf);
39 }
40
41 void tmplput_output_date(StrBuf *Target, WCTemplputParams *TP)
42 {
43         struct tm tm;
44         time_t now;
45         char buf[128];
46         size_t n;
47
48         time(&now);
49         localtime_r(&now, &tm);
50
51         n = wc_strftime(buf, 32, "%A, %x", &tm);
52         StrBufAppendBufPlain(Target, buf, n, 0);
53 }
54
55
56 /*
57  * New messages section
58  */
59 void new_messages_section(void) {
60         char buf[SIZ];
61         char room[SIZ];
62         int i;
63         int number_of_rooms_to_check;
64         char *rooms_to_check = "Mail|Lobby";
65
66
67         number_of_rooms_to_check = num_tokens(rooms_to_check, '|');
68         if (number_of_rooms_to_check == 0) return;
69
70         wc_printf("<table border=\"0\" width=\"100%%\">\n");
71         for (i=0; i<number_of_rooms_to_check; ++i) {
72                 extract_token(room, rooms_to_check, i, '|', sizeof room);
73
74                 serv_printf("GOTO %s", room);
75                 serv_getln(buf, sizeof buf);
76                 if (buf[0] == '2') {
77                         extract_token(room, &buf[4], 0, '|', sizeof room);
78                         wc_printf("<tr><td><a href=\"dotgoto?room=");
79                         urlescputs(room);
80                         wc_printf("\">");
81                         escputs(room);
82                         wc_printf("</a></td><td>%d/%d</td></tr>\n",
83                                 extract_int(&buf[4], 1),
84                                 extract_int(&buf[4], 2)
85                         );
86                 }
87         }
88         wc_printf("</table>\n");
89
90 }
91
92
93 /*
94  * Task list section
95  */
96 void tasks_section(void) {
97         int num_msgs = 0;
98         HashPos *at;
99         const char *HashKey;
100         long HKLen;
101         void *vMsg;
102         message_summary *Msg;
103         wcsession *WCC = WC;
104         StrBuf *Buf;
105         SharedMessageStatus Stat;
106
107         memset(&Stat, 0, sizeof(SharedMessageStatus));
108         Stat.maxload = 10000;
109         Stat.lowest_found = (-1);
110         Stat.highest_found = (-1);
111
112         Buf = NewStrBufPlain(HKEY("_TASKS_"));
113         gotoroom(Buf);
114         FreeStrBuf(&Buf);
115
116         if (WCC->CurRoom.view != VIEW_TASKS) {
117                 num_msgs = 0;
118         }
119         else {
120                 num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
121         }
122
123         if (num_msgs > 0) {
124                 at = GetNewHashPos(WCC->summ, 0);
125                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
126                         Msg = (message_summary*) vMsg;          
127                         tasks_LoadMsgFromServer(NULL, NULL, Msg, 0, 0);
128                 }
129                 DeleteHashPos(&at);
130         }
131
132         if (calendar_summary_view() < 1) {
133                 wc_printf("<i>");
134                 wc_printf(_("(None)"));
135                 wc_printf("</i><br>\n");
136         }
137 }
138
139
140 /*
141  * Calendar section
142  */
143 void calendar_section(void) {
144         char cmd[SIZ];
145         char filter[SIZ];
146         int num_msgs = 0;
147         HashPos *at;
148         const char *HashKey;
149         long HKLen;
150         void *vMsg;
151         message_summary *Msg;
152         wcsession *WCC = WC;
153         StrBuf *Buf;
154         void *v = NULL;
155         SharedMessageStatus Stat;
156
157         memset(&Stat, 0, sizeof(SharedMessageStatus));
158         Stat.maxload = 10000;
159         Stat.lowest_found = (-1);
160         Stat.highest_found = (-1);
161         
162         Buf = NewStrBufPlain(HKEY("_CALENDAR_"));
163         gotoroom(Buf);
164         FreeStrBuf(&Buf);
165         if ( (WC->CurRoom.view != VIEW_CALENDAR) && (WC->CurRoom.view != VIEW_CALBRIEF) ) {
166                 num_msgs = 0;
167         }
168         else {
169                 num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
170         }
171         calendar_GetParamsGetServerCall(&Stat, 
172                                         &v,
173                                         readnew, 
174                                         cmd, 
175                                         sizeof(cmd),
176                                         filter,
177                                         sizeof(filter));
178
179
180         if (num_msgs > 0) {
181                 at = GetNewHashPos(WCC->summ, 0);
182                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
183                         Msg = (message_summary*) vMsg;          
184                         calendar_LoadMsgFromServer(NULL, &v, Msg, 0, 0);
185                 }
186                 DeleteHashPos(&at);
187         }
188         if (calendar_summary_view() < 1) {
189                 wc_printf("<i>");
190                 wc_printf(_("(Nothing)"));
191                 wc_printf("</i><br>\n");
192         }
193         __calendar_Cleanup(&v);
194 }
195
196 void tmplput_new_messages_section(StrBuf *Target, WCTemplputParams *TP) {
197         new_messages_section();
198 }
199 void tmplput_tasks_section(StrBuf *Target, WCTemplputParams *TP) {
200         tasks_section();
201 }
202 void tmplput_calendar_section(StrBuf *Target, WCTemplputParams *TP) {
203         calendar_section();
204 }
205
206 void 
207 InitModule_SUMMARY
208 (void)
209 {
210         RegisterNamespace("TIME:NOW", 0, 0, tmplput_output_date, NULL, CTX_NONE);
211         RegisterNamespace("SUMMARY:NEWMESSAGES_SELECTION", 0, 0, tmplput_new_messages_section, NULL, CTX_NONE);
212         RegisterNamespace("SUMMARY:TASKSSECTION", 0, 0, tmplput_tasks_section, NULL, CTX_NONE);
213         RegisterNamespace("SUMMARY:CALENDAR_SECTION", 0, 0, tmplput_calendar_section, NULL, CTX_NONE);
214
215         WebcitAddUrlHandler(HKEY("new_messages_html"), "", 0, new_messages_section, AJAX);
216         WebcitAddUrlHandler(HKEY("tasks_inner_html"), "", 0, tasks_section, AJAX);
217         WebcitAddUrlHandler(HKEY("calendar_inner_html"), "", 0, calendar_section, AJAX);
218
219 }
220