* Cleaned up the HTML and CSS for the new bbs view.
[citadel.git] / webcit / bbsview_renderer.c
1 /* 
2  * $Id: $
3  *
4  * BBS View renderer module for WebCit
5  *
6  * Copyright (c) 1996-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "webcit.h"
24 #include "webserver.h"
25 #include "groupdav.h"
26
27 /*
28  * Data which gets passed around between the various functions in this module
29  */
30 struct bbsview {
31         long *msgs;             /* Array of msgnums for messages we are displaying */
32         int num_msgs;           /* Number of msgnums stored in 'msgs' */
33         int alloc_msgs;         /* Currently allocated size of array */
34 };
35
36
37 /*
38  * Entry point for message read operations.
39  */
40 int bbsview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
41                                    void **ViewSpecific, 
42                                    long oper, 
43                                    char *cmd, 
44                                    long len)
45 {
46         struct bbsview *BBS = malloc(sizeof(struct bbsview));
47         memset(BBS, 0, sizeof(struct bbsview));
48         *ViewSpecific = BBS;
49
50         Stat->defaultsortorder = 1;
51         Stat->startmsg = -1;
52         Stat->sortit = 1;
53         
54         rlid[oper].cmd(cmd, len);               /* this performs the server call to fetch the msg list */
55         
56         if (havebstr("maxmsgs")) {
57                 Stat->maxmsgs = ibstr("maxmsgs");
58         }
59         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
60         
61         if (havebstr("startmsg")) {
62                 Stat->startmsg = lbstr("startmsg");
63         }
64         if (lbstr("SortOrder") == 2) {
65                 Stat->reverse = 1;
66                 Stat->num_displayed = -DEFAULT_MAXMSGS;
67         }
68         else {
69                 Stat->reverse = 0;
70                 Stat->num_displayed = DEFAULT_MAXMSGS;
71         }
72
73         return 200;
74 }
75
76
77 /*
78  * begin_ajax_response() was moved from bbsview_LoadMsgFromServer() to here ...
79  */
80 int bbsview_PrintViewHeader(SharedMessageStatus *Stat, void **ViewSpecific)
81 {
82         if (WC->is_ajax) {
83                 begin_ajax_response();          /* for non-ajax, headers are output in messages.c */
84         }
85
86         return 200;
87 }
88
89
90 /*
91  * This function is called for every message in the list.
92  */
93 int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat, 
94                               void **ViewSpecific, 
95                               message_summary* Msg, 
96                               int is_new, 
97                               int i)
98 {
99         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
100
101         if (BBS->alloc_msgs == 0) {
102                 BBS->alloc_msgs = Stat->maxmsgs;
103                 BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
104         }
105
106         /* Theoretically this never happens because the initial allocation == maxmsgs */
107         if (BBS->num_msgs >= BBS->alloc_msgs) {
108                 BBS->alloc_msgs *= 2;
109                 BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
110         }
111
112         BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
113
114         return 200;
115 }
116
117 int bbsview_sortfunc_reverse(const void *s1, const void *s2) {
118         long l1;
119         long l2;
120
121         l1 = *(long *)(s1);
122         l2 = *(long *)(s2);
123
124         if (l1 > l2) return(-1);
125         if (l1 < l2) return(+1);
126         return(0);
127 }
128
129
130 int bbsview_sortfunc_forward(const void *s1, const void *s2) {
131         long l1;
132         long l2;
133
134         l1 = *(long *)(s1);
135         l2 = *(long *)(s2);
136
137         if (l1 > l2) return(+1);
138         if (l1 < l2) return(-1);
139         return(0);
140 }
141
142
143 int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat, 
144                                void **ViewSpecific, 
145                                long oper)
146 {
147         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
148         int i;
149         const StrBuf *Mime;
150         char olderdiv[64];
151         char newerdiv[64];
152         int doing_older_messages = 0;
153         int doing_newer_messages = 0;
154
155         snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
156         snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
157
158         /* If this is the initial page load (and not an update), supply the required JavaScript code */
159         if (!WC->is_ajax) {
160            StrBufAppendPrintf(WC->trailing_javascript,
161                 "       function moremsgs(target_div, gt_or_lt, gt_or_lt_value, maxmsgs, sortorder) {   \n"
162                 "               $(target_div).innerHTML = '<div class=\"moreprompt\">%s ... <img src=\"static/throbber.gif\"></div>';   \n"
163                 "               p = gt_or_lt + '=' + gt_or_lt_value + '&maxmsgs=' + maxmsgs             \n"
164                 "                       + '&is_summary=0&SortOrder=' + sortorder + '&is_ajax=1'         \n"
165                 "                       + '&gt_or_lt=' + gt_or_lt                                       \n"
166                 "                       + '&r=' + CtdlRandomString();                                   \n"
167                 "               new Ajax.Updater(target_div, 'read' + gt_or_lt,                                 \n"
168                 "                       { method: 'get', parameters: p, evalScripts: true } );          \n"
169                 "       }                                                                               \n"
170                 "",
171                 _("Loading")
172            );
173         }
174
175
176         /* Determine whether we are in the middle of a 'click for older messages' or 'click for
177          * newer messages' operation.  If neither, then we are in the initial page load.
178          */
179         if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
180                 doing_older_messages = 1;
181                 doing_newer_messages = 0;
182         }
183         else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
184                 doing_older_messages = 0;
185                 doing_newer_messages = 1;
186         }
187         else {
188                 doing_older_messages = 0;
189                 doing_newer_messages = 0;
190         }
191
192
193         /* Cut the message list down to the requested size */
194         if (Stat->nummsgs > 0) {
195                 lprintf(9, "sorting %d messages\n", BBS->num_msgs);
196                 qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), (Stat->reverse ? bbsview_sortfunc_reverse : bbsview_sortfunc_forward));
197
198                 /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
199
200                 if (BBS->num_msgs > Stat->maxmsgs) {
201
202                         if (doing_older_messages) {
203                                 /* LT ... cut it down to the LAST 20 messages received */
204                                 memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
205                                         (Stat->maxmsgs * sizeof(long))
206                                 );
207                                 BBS->num_msgs = Stat->maxmsgs;
208                         }
209                         else {
210                                 /* GT ... cut it down to the FIRST 20 messages received */
211                                 BBS->num_msgs = Stat->maxmsgs;
212                         }
213                 }
214         }
215
216
217         /* Supply the link to prepend the previous 20 messages */
218
219         if (doing_newer_messages == 0) {
220                 wc_printf("<div id=\"%s\">", olderdiv);
221                 /* if (Stat->nummsgs > 0) { */
222                 if (Stat->nummsgs > 0) {
223                         wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld, %d );\">",
224                                 olderdiv,
225                                 BBS->msgs[0],
226                                 Stat->maxmsgs,
227                                 (Stat->reverse ? 2 : 1)
228                         );
229                 
230                         wc_printf("<div class=\"moreprompt\">"
231                                 "&uarr; &uarr; &uarr; %s &uarr; &uarr; &uarr;"
232                                 "</div>", _("older messages")
233                         );
234                         wc_printf("</a>");
235                 }
236                 wc_printf("</div>");
237         }
238
239
240
241         /* Handle the empty message set gracefully... */
242         if (Stat->nummsgs == 0) {
243                 if (!WC->is_ajax) {
244                         wc_printf("<div class=\"nomsgs\"><br><em>");
245                         wc_printf(_("No messages here."));
246                         wc_printf("</em><br></div>\n");
247                 }
248         }
249
250         /* Non-empty message set... */
251         else {
252                 /* Render the messages */
253         
254                 for (i=0; i<BBS->num_msgs; ++i) {
255                         read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
256                 }
257
258         }
259
260
261         /* Supply the link to append the next 20 messages */
262
263         if (doing_older_messages == 0) {
264                 wc_printf("<div id=\"%s\">", newerdiv);
265                 /* if (Stat->nummsgs > 0) { */
266                 if (Stat->nummsgs >= Stat->maxmsgs) {
267                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
268                                 newerdiv,
269                                 BBS->msgs[BBS->num_msgs-1],
270                                 Stat->maxmsgs,
271                                 (Stat->reverse ? 2 : 1)
272                         );
273                 
274                         wc_printf("<div class=\"moreprompt\">"
275                                 "&darr; &darr; &darr; %s &darr; &darr; &darr;"
276                                 "</div>", _("newer messages")
277                         );
278                         wc_printf("</a>");
279                 }
280                 else {
281                         long gt = 0;    /* if new messages appear later, where will they begin? */
282                         if (Stat->nummsgs > 0) {
283                                 gt = BBS->msgs[BBS->num_msgs-1];
284                         }
285                         else {
286                                 gt = atol(bstr("gt"));
287                         }
288                         wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
289                                 newerdiv,
290                                 gt,
291                                 Stat->maxmsgs,
292                                 (Stat->reverse ? 2 : 1)
293                         );
294                         wc_printf("<div class=\"moreprompt\">");
295                         wc_printf("%s", _("no more messages"));
296                         wc_printf("</div>");
297                         wc_printf("</a>");
298                 }
299                 wc_printf("</div>");
300         }
301
302         /* Leave a little padding at the bottom, but only for the initial page load -- don't keep
303          * adding it every time we extend the visible message set.
304          */
305         if (!WC->is_ajax) {
306                 wc_printf("<br><br><br><br>");
307         }
308
309         return(0);
310 }
311
312
313 int bbsview_Cleanup(void **ViewSpecific)
314 {
315         struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
316         free(BBS);
317
318         if (WC->is_ajax) {
319                 end_ajax_response();
320                 WC->is_ajax = 0;
321         }
322         else {
323                 wDumpContent(1);
324         }
325         return 0;
326 }
327
328 void 
329 InitModule_BBSVIEWRENDERERS
330 (void)
331 {
332         RegisterReadLoopHandlerset(
333                 VIEW_BBS,
334                 bbsview_GetParamsGetServerCall,
335                 bbsview_PrintViewHeader,
336                 bbsview_LoadMsgFromServer,
337                 bbsview_RenderView_or_Tail,
338                 bbsview_Cleanup);
339 }