Ok, *this* is the way we want it structured internally.
[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         StrBuf *r;
125         Buf = NewStrBuf();
126         r = NewStrBuf();
127         serv_printf("MSG0 %ld|1", bp->msgnum);          /* top level citadel headers only */
128         StrBuf_ServGetln(Buf);
129         if (GetServerStatus(Buf, NULL) == 1) {
130                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
131                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
132                                 StrBufCutLeft(Buf, 5);
133                                 wc_printf("id %s, ", ChrPtr(Buf));
134                                 bp->id = HashLittle(ChrPtr(Buf), StrLength(Buf));
135                         }
136                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
137                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
138                                 wc_printf("refs %s, ", ChrPtr(Buf));
139                                 StrBufExtract_token(r, Buf, 0, '|');
140                                 wc_printf("topref %s, ", ChrPtr(r));
141                                 bp->refs = HashLittle(ChrPtr(r), StrLength(r));
142                         }
143                 }
144                 wc_printf("<br>\n");
145         }
146         FreeStrBuf(&Buf);
147         FreeStrBuf(&r);
148 }
149
150
151
152
153 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
154 {
155         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
156         int i, j;
157
158         /* Pass #1 - sort */
159         if (Stat->nummsgs > 0) {
160                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
161                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(struct blogpost), blogview_sortfunc);
162         }
163
164         /* Pass #2 - learn thread references */
165         lprintf(9, "learning thread references\n");
166         for (i=0; (i<BLOG->num_msgs); ++i) {
167                 if (BLOG->msgs[i].msgnum > 0L) {
168                         blogview_learn_thread_references(&BLOG->msgs[i]);
169                 }
170         }
171
172         /* Pass #3 - turn it into a thread tree */
173         /* FIXME implement this */
174
175         /* Pass #4 - render
176          * This will require several different modes:
177          * * Top level
178          * * Single story permalink
179          * * Comments
180          * * etc
181          */
182
183         wc_printf("<hr>\n");
184
185         for (i=0; (i<BLOG->num_msgs); ++i) {
186                 if (BLOG->msgs[i].msgnum > 0L) {
187                         wc_printf("Message %d, #%ld, id %d, refs %d<br>\n",
188                                 i,
189                                 BLOG->msgs[i].msgnum,
190                                 BLOG->msgs[i].id,
191                                 BLOG->msgs[i].refs
192                         );
193                 }
194         }
195
196         wc_printf("<hr>\n");
197
198         for (i=0; (i<BLOG->num_msgs); ++i) {
199                 if (BLOG->msgs[i].msgnum > 0L) {
200                         if (BLOG->msgs[i].refs == 0) {
201                                 wc_printf("<b>Message %d, #%ld, id %d, refs %d</b><br>\n",
202                                         i,
203                                         BLOG->msgs[i].msgnum,
204                                         BLOG->msgs[i].id,
205                                         BLOG->msgs[i].refs
206                                 );
207                                 for (j=0; (j<BLOG->num_msgs); ++j) {
208                                         if (BLOG->msgs[j].refs == BLOG->msgs[i].id) {
209                                                 wc_printf("* comment %d<br>\n", j);
210                                         }
211                                 }
212                         }
213                 }
214         }
215
216         return(0);
217 }
218
219
220 int blogview_Cleanup(void **ViewSpecific)
221 {
222         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
223
224         if (BLOG->alloc_msgs != 0) {
225                 free(BLOG->msgs);
226         }
227         free(BLOG);
228
229         wDumpContent(1);
230         return 0;
231 }
232
233
234 void 
235 InitModule_BLOGVIEWRENDERERS
236 (void)
237 {
238         RegisterReadLoopHandlerset(
239                 VIEW_BLOG,
240                 blogview_GetParamsGetServerCall,
241                 NULL,
242                 NULL, 
243                 blogview_LoadMsgFromServer,
244                 blogview_render,
245                 blogview_Cleanup
246         );
247 }