Added some comments to the code to jog my memory when I start to
[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 /* 
27  * Array type for a blog post.  The first message is the post; the rest are comments
28  */
29 struct blogpost {
30         long *msgs;             /* Array of msgnums for messages we are displaying */
31         int num_msgs;           /* Number of msgnums stored in 'msgs' */
32         int alloc_msgs;         /* Currently allocated size of array */
33 };
34
35
36 /*
37  * Destructor for 'struct blogpost' which does the rendering first.
38  * By rendering from here, we eliminate the need for a separate iterator, although
39  * we might run into trouble when we get around to displaying newest-to-oldest...
40  * FIXME do the needful with regard to gettext
41  */
42 void blogpost_render_and_destroy(struct blogpost *bp) {
43         if (bp->num_msgs > 0) wc_printf("Blog post %ld<br>\n", bp->msgs[0]);
44         if (bp->num_msgs > 1) wc_printf("&nbsp;<i>%d comments</i><br>\n", bp->num_msgs - 1);
45         wc_printf("<br>\n");
46         if (bp->alloc_msgs > 0) {
47                 free(bp->msgs);
48         }
49         free(bp);
50 }
51
52
53 /*
54  * Data which gets returned from a call to blogview_learn_thread_references()
55  */
56 struct bltr {
57         int id;
58         int refs;
59 };
60
61
62 /*
63  * Entry point for message read operations.
64  */
65 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
66                                    void **ViewSpecific, 
67                                    long oper, 
68                                    char *cmd, 
69                                    long len)
70 {
71         HashList *BLOG = NewHash(1, NULL);
72         *ViewSpecific = BLOG;
73
74         Stat->startmsg = (-1);                                  /* not used here */
75         Stat->sortit = 1;                                       /* not used here */
76         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
77         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
78         
79         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
80         rlid[2].cmd(cmd, len);
81         
82         return 200;
83 }
84
85
86 /*
87  * Given a 'struct blogpost' containing a msgnum, populate the id
88  * and refs fields by fetching them from the Citadel server
89  */
90 struct bltr blogview_learn_thread_references(long msgnum)
91 {
92         StrBuf *Buf;
93         StrBuf *r;
94         struct bltr bltr = { 0, 0 } ;
95         Buf = NewStrBuf();
96         r = NewStrBuf();
97         serv_printf("MSG0 %ld|1", msgnum);              /* top level citadel headers only */
98         StrBuf_ServGetln(Buf);
99         if (GetServerStatus(Buf, NULL) == 1) {
100                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
101                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
102                                 StrBufCutLeft(Buf, 5);
103                                 bltr.id = HashLittle(ChrPtr(Buf), StrLength(Buf));
104                         }
105                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
106                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
107                                 StrBufExtract_token(r, Buf, 0, '|');
108                                 bltr.refs = HashLittle(ChrPtr(r), StrLength(r));
109                         }
110                 }
111         }
112         FreeStrBuf(&Buf);
113         FreeStrBuf(&r);
114         return(bltr);
115 }
116
117
118 /*
119  * This function is called for every message in the list.
120  */
121 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
122                               void **ViewSpecific, 
123                               message_summary* Msg, 
124                               int is_new, 
125                               int i)
126 {
127         HashList *BLOG = (HashList *) *ViewSpecific;
128         struct bltr b;
129         struct blogpost *bp = NULL;
130
131         b = blogview_learn_thread_references(Msg->msgnum);
132
133         /* FIXME an optimization here -- one we ought to perform -- is to exit this
134          * function immediately if the viewer is only interested in a single post and
135          * that message ID is neither the id nor the refs.  Actually, that might *be*
136          * the way to display only a single message (with or without comments).
137          */
138
139         if (b.refs == 0) {
140                 bp = malloc(sizeof(struct blogpost));
141                 if (!bp) return(200);
142                 memset(bp, 0, sizeof (struct blogpost));
143                 Put(BLOG, (const char *)&b.id, sizeof(b.id), bp,
144                                         (DeleteHashDataFunc)blogpost_render_and_destroy);
145         }
146         else {
147                 GetHash(BLOG, (const char *)&b.refs , sizeof(b.refs), (void *)&bp);
148         }
149
150         /*
151          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
152          * blog post itself or a comment attached to it; either way, the code is the same from
153          * this point onward.
154          */
155         if (bp != NULL) {
156                 if (bp->alloc_msgs == 0) {
157                         bp->alloc_msgs = 1000;
158                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
159                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
160                 }
161                 if (bp->num_msgs >= bp->alloc_msgs) {
162                         bp->alloc_msgs *= 2;
163                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
164                         memset(&bp->msgs[bp->num_msgs], 0,
165                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
166                 }
167                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
168         }
169
170         return 200;
171 }
172
173
174 /*
175  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
176  */
177 int blogview_sortfunc(const void *s1, const void *s2) {
178         long *l1 = (long *)(s1);
179         long *l2 = (long *)(s2);
180
181         if (*l1 > *l2) return(-1);
182         if (*l1 < *l2) return(+1);
183         return(0);
184 }
185
186
187 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
188 {
189         /*HashList *BLOG = (HashList *) *ViewSpecific;*/
190
191         /*
192          * This will require several different modes:
193          * * Top level
194          * * Single story permalink
195          * * Comments
196          * * etc
197          */
198
199         return(0);
200 }
201
202
203 int blogview_Cleanup(void **ViewSpecific)
204 {
205         HashList *BLOG = (HashList *) *ViewSpecific;
206
207         DeleteHash(&BLOG);
208
209         wDumpContent(1);
210         return 0;
211 }
212
213
214 void 
215 InitModule_BLOGVIEWRENDERERS
216 (void)
217 {
218         RegisterReadLoopHandlerset(
219                 VIEW_BLOG,
220                 blogview_GetParamsGetServerCall,
221                 NULL,
222                 NULL, 
223                 blogview_LoadMsgFromServer,
224                 blogview_render,
225                 blogview_Cleanup
226         );
227 }