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