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