6372245a003b84145e906a89c607a4f52f5b9402
[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 /*
25  * Display today's date in a friendly format
26  */
27 void output_date(void) {
28         struct tm tm;
29         time_t now;
30         char buf[128];
31
32         time(&now);
33         localtime_r(&now, &tm);
34
35         wc_strftime(buf, 32, "%A, %x", &tm);
36         wc_printf("%s", buf);
37 }
38
39 void tmplput_output_date(StrBuf *Target, WCTemplputParams *TP)
40 {
41         struct tm tm;
42         time_t now;
43         char buf[128];
44         size_t n;
45
46         time(&now);
47         localtime_r(&now, &tm);
48
49         n = wc_strftime(buf, 32, "%A, %x", &tm);
50         StrBufAppendBufPlain(Target, buf, n, 0);
51 }
52
53
54 /*
55  * New messages section
56  */
57 void new_messages_section(void) {
58         char buf[SIZ];
59         char room[SIZ];
60         int i;
61         int number_of_rooms_to_check;
62         char *rooms_to_check = "Mail|Lobby";
63
64
65         number_of_rooms_to_check = num_tokens(rooms_to_check, '|');
66         if (number_of_rooms_to_check == 0) return;
67
68         wc_printf("<table border=\"0\" width=\"100%%\">\n");
69         for (i=0; i<number_of_rooms_to_check; ++i) {
70                 extract_token(room, rooms_to_check, i, '|', sizeof room);
71
72                 serv_printf("GOTO %s", room);
73                 serv_getln(buf, sizeof buf);
74                 if (buf[0] == '2') {
75                         extract_token(room, &buf[4], 0, '|', sizeof room);
76                         wc_printf("<tr><td><a href=\"dotgoto?room=");
77                         urlescputs(room);
78                         wc_printf("\">");
79                         escputs(room);
80                         wc_printf("</a></td><td>%d/%d</td></tr>\n",
81                                 extract_int(&buf[4], 1),
82                                 extract_int(&buf[4], 2)
83                         );
84                 }
85         }
86         wc_printf("</table>\n");
87
88 }
89
90
91 /*
92  * Task list section
93  */
94 void tasks_section(void) {
95         int num_msgs = 0;
96         HashPos *at;
97         const char *HashKey;
98         long HKLen;
99         void *vMsg;
100         message_summary *Msg;
101         wcsession *WCC = WC;
102         StrBuf *Buf;
103         SharedMessageStatus Stat;
104
105         memset(&Stat, 0, sizeof(SharedMessageStatus));
106         Stat.maxload = 10000;
107         Stat.lowest_found = (-1);
108         Stat.highest_found = (-1);
109
110         Buf = NewStrBufPlain(HKEY("_TASKS_"));
111         gotoroom(Buf);
112         FreeStrBuf(&Buf);
113
114         if (WCC->CurRoom.view != VIEW_TASKS) {
115                 num_msgs = 0;
116         }
117         else {
118                 num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
119         }
120
121         if (num_msgs > 0) {
122                 at = GetNewHashPos(WCC->summ, 0);
123                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
124                         Msg = (message_summary*) vMsg;          
125                         tasks_LoadMsgFromServer(NULL, NULL, Msg, 0, 0);
126                 }
127                 DeleteHashPos(&at);
128         }
129
130         if (calendar_summary_view() < 1) {
131                 wc_printf("<i>");
132                 wc_printf(_("(None)"));
133                 wc_printf("</i><br>\n");
134         }
135 }
136
137
138 /*
139  * Calendar section
140  */
141 void calendar_section(void) {
142         char cmd[SIZ];
143         int num_msgs = 0;
144         HashPos *at;
145         const char *HashKey;
146         long HKLen;
147         void *vMsg;
148         message_summary *Msg;
149         wcsession *WCC = WC;
150         StrBuf *Buf;
151         void *v = NULL;
152         SharedMessageStatus Stat;
153
154         memset(&Stat, 0, sizeof(SharedMessageStatus));
155         Stat.maxload = 10000;
156         Stat.lowest_found = (-1);
157         Stat.highest_found = (-1);
158         
159         Buf = NewStrBufPlain(HKEY("_CALENDAR_"));
160         gotoroom(Buf);
161         FreeStrBuf(&Buf);
162         if ( (WC->CurRoom.view != VIEW_CALENDAR) && (WC->CurRoom.view != VIEW_CALBRIEF) ) {
163                 num_msgs = 0;
164         }
165         else {
166                 num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
167         }
168         calendar_GetParamsGetServerCall(&Stat, 
169                                         &v,
170                                         readnew, 
171                                         cmd, 
172                                         sizeof(cmd));
173
174         if (num_msgs > 0) {
175                 at = GetNewHashPos(WCC->summ, 0);
176                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
177                         Msg = (message_summary*) vMsg;          
178                         calendar_LoadMsgFromServer(NULL, &v, Msg, 0, 0);
179                 }
180                 DeleteHashPos(&at);
181         }
182         if (calendar_summary_view() < 1) {
183                 wc_printf("<i>");
184                 wc_printf(_("(Nothing)"));
185                 wc_printf("</i><br>\n");
186         }
187         __calendar_Cleanup(&v);
188 }
189
190 void tmplput_new_messages_section(StrBuf *Target, WCTemplputParams *TP) {
191         new_messages_section();
192 }
193 void tmplput_tasks_section(StrBuf *Target, WCTemplputParams *TP) {
194         tasks_section();
195 }
196 void tmplput_calendar_section(StrBuf *Target, WCTemplputParams *TP) {
197         calendar_section();
198 }
199
200 void 
201 InitModule_SUMMARY
202 (void)
203 {
204         RegisterNamespace("TIME:NOW", 0, 0, tmplput_output_date, NULL, CTX_NONE);
205         RegisterNamespace("SUMMARY:NEWMESSAGES_SELECTION", 0, 0, tmplput_new_messages_section, NULL, CTX_NONE);
206         RegisterNamespace("SUMMARY:TASKSSECTION", 0, 0, tmplput_tasks_section, NULL, CTX_NONE);
207         RegisterNamespace("SUMMARY:CALENDAR_SECTION", 0, 0, tmplput_calendar_section, NULL, CTX_NONE);
208
209         WebcitAddUrlHandler(HKEY("new_messages_html"), "", 0, new_messages_section, AJAX);
210         WebcitAddUrlHandler(HKEY("tasks_inner_html"), "", 0, tasks_section, AJAX);
211         WebcitAddUrlHandler(HKEY("calendar_inner_html"), "", 0, calendar_section, AJAX);
212         WebcitAddUrlHandler(HKEY("mini_calendar"), "", 0, ajax_mini_calendar, AJAX);
213
214 }
215