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