Rendering test
[citadel.git] / webcit / blogview_renderer.c
1 /* 
2  * Blog view renderer module for WebCit
3  *
4  * Copyright (c) 1996-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (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 "webserver.h"
23 #include "groupdav.h"
24
25 /*
26  * Data which gets passed around between the various functions in this module
27  *
28  */
29
30 struct blogpost {
31         long msgnum;
32         int id;
33         int refs;
34         int comment_count;
35 };
36
37 struct blogview {
38         struct blogpost *msgs;  /* Array of msgnums for messages we are displaying */
39         int num_msgs;           /* Number of msgnums stored in 'msgs' */
40         int alloc_msgs;         /* Currently allocated size of array */
41 };
42
43
44 /*
45  * Entry point for message read operations.
46  */
47 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
48                                    void **ViewSpecific, 
49                                    long oper, 
50                                    char *cmd, 
51                                    long len)
52 {
53         struct blogview *BLOG = malloc(sizeof(struct blogview));
54         memset(BLOG, 0, sizeof(struct blogview));
55         *ViewSpecific = BLOG;
56
57         Stat->startmsg = (-1);                                  /* not used here */
58         Stat->sortit = 1;                                       /* not used here */
59         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
60         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
61         
62         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
63         rlid[2].cmd(cmd, len);
64         
65         return 200;
66 }
67
68
69 /*
70  * This function is called for every message in the list.
71  */
72 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
73                               void **ViewSpecific, 
74                               message_summary* Msg, 
75                               int is_new, 
76                               int i)
77 {
78         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
79
80         if (BLOG->alloc_msgs == 0) {
81                 BLOG->alloc_msgs = 1000;
82                 BLOG->msgs = malloc(BLOG->alloc_msgs * sizeof(struct blogpost));
83                 memset(BLOG->msgs, 0, (BLOG->alloc_msgs * sizeof(struct blogpost)) );
84         }
85
86         /* Check our buffer size */
87         if (BLOG->num_msgs >= BLOG->alloc_msgs) {
88                 BLOG->alloc_msgs *= 2;
89                 BLOG->msgs = realloc(BLOG->msgs, (BLOG->alloc_msgs * sizeof(long)));
90                 memset(&BLOG->msgs[BLOG->num_msgs], 0, ((BLOG->alloc_msgs - BLOG->num_msgs) * sizeof(long)) );
91         }
92
93         BLOG->msgs[BLOG->num_msgs++].msgnum = Msg->msgnum;
94         BLOG->msgs[BLOG->num_msgs].id = 0;
95         BLOG->msgs[BLOG->num_msgs].refs = 0;
96         BLOG->msgs[BLOG->num_msgs].comment_count = 0;
97
98         return 200;
99 }
100
101
102
103 /*
104  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
105  */
106 int blogview_sortfunc(const void *s1, const void *s2) {
107         struct blogpost *l1 = (struct blogpost *)(s1);
108         struct blogpost *l2 = (struct blogpost *)(s2);
109
110         if (l1->msgnum > l2->msgnum) return(-1);
111         if (l1->msgnum < l2->msgnum) return(+1);
112         return(0);
113 }
114
115
116
117 /*
118  * Given a 'struct blogpost' containing a msgnum, populate the id
119  * and refs fields by fetching them from the Citadel server
120  */
121 void blogview_learn_thread_references(struct blogpost *bp)
122 {
123         StrBuf *Buf;
124         Buf = NewStrBuf();
125         serv_printf("MSG0 %ld|1", bp->msgnum);          /* top level citadel headers only */
126         StrBuf_ServGetln(Buf);
127         if (GetServerStatus(Buf, NULL) == 1) {
128                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
129                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
130                                 StrBufCutLeft(Buf, 5);
131                                 bp->id = HashLittle(ChrPtr(Buf), StrLength(Buf));
132                         }
133                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
134                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
135                                 StrBufCutAt(Buf, 0, "|");       /* trim all but the first thread ref */
136                                 bp->refs = HashLittle(ChrPtr(Buf), StrLength(Buf));
137                         }
138                 }
139         }
140         FreeStrBuf(&Buf);
141 }
142
143
144
145
146 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
147 {
148         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
149         int i, j;
150
151         /* Pass #1 - sort */
152         if (Stat->nummsgs > 0) {
153                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
154                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(struct blogpost), blogview_sortfunc);
155         }
156
157         /* Pass #2 - learn thread references */
158         lprintf(9, "learning thread references\n");
159         for (i=0; (i<BLOG->num_msgs); ++i) {
160                 if (BLOG->msgs[i].msgnum > 0L) {
161                         blogview_learn_thread_references(&BLOG->msgs[i]);
162                 }
163         }
164
165         /* Pass #3 - turn it into a thread tree */
166         /* FIXME implement this */
167
168         /* Pass #4 - render
169          * This will require several different modes:
170          * * Top level
171          * * Single story permalink
172          * * Comments
173          * * etc
174          */
175
176
177         for (i=0; (i<BLOG->num_msgs); ++i) {
178                 if (BLOG->msgs[i].msgnum > 0L) {
179                         wc_printf("Message %d, #%ld, id %d, refs %d<br>\n",
180                                 i,
181                                 BLOG->msgs[i].msgnum,
182                                 BLOG->msgs[i].id,
183                                 BLOG->msgs[i].refs
184                         );
185                 }
186         }
187
188         wc_printf("<hr>\n");
189
190         for (i=0; (i<BLOG->num_msgs); ++i) {
191                 if (BLOG->msgs[i].msgnum > 0L) {
192                         if (BLOG->msgs[i].refs == 0) {
193                                 wc_printf("<b>Message %d, #%ld, id %d, refs %d</b><br>\n",
194                                         i,
195                                         BLOG->msgs[i].msgnum,
196                                         BLOG->msgs[i].id,
197                                         BLOG->msgs[i].refs
198                                 );
199                                 for (j=0; (j<BLOG->num_msgs); ++j) {
200                                         if (BLOG->msgs[j].refs == BLOG->msgs[i].id) {
201                                                 wc_printf("* comment %d<br>\n", j);
202                                         }
203                                 }
204                         }
205                 }
206         }
207
208         return(0);
209 }
210
211
212 int blogview_Cleanup(void **ViewSpecific)
213 {
214         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
215
216         if (BLOG->alloc_msgs != 0) {
217                 free(BLOG->msgs);
218         }
219         free(BLOG);
220
221         wDumpContent(1);
222         return 0;
223 }
224
225
226 void 
227 InitModule_BLOGVIEWRENDERERS
228 (void)
229 {
230         RegisterReadLoopHandlerset(
231                 VIEW_BLOG,
232                 blogview_GetParamsGetServerCall,
233                 NULL,
234                 NULL, 
235                 blogview_LoadMsgFromServer,
236                 blogview_render,
237                 blogview_Cleanup
238         );
239 }