More underpinnings for blog thread view
[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         StrBuf *id;
33         StrBuf *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 = NULL;
95         BLOG->msgs[BLOG->num_msgs].refs = NULL;
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                                 bp->id = NewStrBufDup(Buf);
131                                 StrBufCutLeft(bp->id, 5);
132                         }
133                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
134                                 bp->refs = NewStrBufDup(Buf);
135                                 StrBufCutLeft(bp->refs, 5);
136                         }
137                 }
138         }
139         FreeStrBuf(&Buf);
140 }
141
142
143
144
145 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
146 {
147         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
148         int i;
149
150         /* Pass #1 - sort */
151         if (Stat->nummsgs > 0) {
152                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
153                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(struct blogpost), blogview_sortfunc);
154         }
155
156         /* Pass #2 - learn thread references */
157         for (i=0; (i<BLOG->num_msgs); ++i) {
158                 if (BLOG->msgs[i].msgnum > 0L) {
159                         blogview_learn_thread_references(&BLOG->msgs[i]);
160                 }
161         }
162
163         /* FIXME here is where we should actually turn it into a thread tree */
164
165         /* Pass #3 - render */
166         for (i=0; (i<BLOG->num_msgs); ++i) {
167                 if (BLOG->msgs[i].msgnum > 0L) {
168                         if (BLOG->msgs[i].refs == NULL) {
169                                 wc_printf("<b>");
170                         }
171                         wc_printf("Message %d, #%ld, id '%s', refs '%s'",
172                                 i,
173                                 BLOG->msgs[i].msgnum,
174                                 ChrPtr(BLOG->msgs[i].id),
175                                 ChrPtr(BLOG->msgs[i].refs)
176                         );
177                         if (BLOG->msgs[i].refs == NULL) {
178                                 wc_printf("</b>");
179                         }
180                         wc_printf("<br>\n");
181                 }
182         }
183
184         return(0);
185 }
186
187
188 int blogview_Cleanup(void **ViewSpecific)
189 {
190         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
191         int i;
192
193         if (BLOG->alloc_msgs != 0) {
194                 for (i=0; i<BLOG->num_msgs; ++i) {
195                         FreeStrBuf(&BLOG->msgs[i].id);
196                         FreeStrBuf(&BLOG->msgs[i].refs);
197                 }
198                 free(BLOG->msgs);
199         }
200         free(BLOG);
201
202         wDumpContent(1);
203         return 0;
204 }
205
206
207 void 
208 InitModule_BLOGVIEWRENDERERS
209 (void)
210 {
211         RegisterReadLoopHandlerset(
212                 VIEW_BLOG,
213                 blogview_GetParamsGetServerCall,
214                 NULL,
215                 NULL, 
216                 blogview_LoadMsgFromServer,
217                 blogview_render,
218                 blogview_Cleanup
219         );
220 }