Move some of the new code into a separate function
[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 };
35
36 struct blogview {
37         struct blogpost *msgs;  /* Array of msgnums for messages we are displaying */
38         int num_msgs;           /* Number of msgnums stored in 'msgs' */
39         int alloc_msgs;         /* Currently allocated size of array */
40 };
41
42
43 /*
44  * Entry point for message read operations.
45  */
46 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
47                                    void **ViewSpecific, 
48                                    long oper, 
49                                    char *cmd, 
50                                    long len)
51 {
52         struct blogview *BLOG = malloc(sizeof(struct blogview));
53         memset(BLOG, 0, sizeof(struct blogview));
54         *ViewSpecific = BLOG;
55
56         Stat->startmsg = (-1);                                  /* not used here */
57         Stat->sortit = 1;                                       /* not used here */
58         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
59         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
60         
61         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
62         rlid[2].cmd(cmd, len);
63         
64         return 200;
65 }
66
67
68 /*
69  * This function is called for every message in the list.
70  */
71 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
72                               void **ViewSpecific, 
73                               message_summary* Msg, 
74                               int is_new, 
75                               int i)
76 {
77         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
78
79         if (BLOG->alloc_msgs == 0) {
80                 BLOG->alloc_msgs = 1000;
81                 BLOG->msgs = malloc(BLOG->alloc_msgs * sizeof(struct blogpost));
82                 memset(BLOG->msgs, 0, (BLOG->alloc_msgs * sizeof(struct blogpost)) );
83         }
84
85         /* Check our buffer size */
86         if (BLOG->num_msgs >= BLOG->alloc_msgs) {
87                 BLOG->alloc_msgs *= 2;
88                 BLOG->msgs = realloc(BLOG->msgs, (BLOG->alloc_msgs * sizeof(long)));
89                 memset(&BLOG->msgs[BLOG->num_msgs], 0, ((BLOG->alloc_msgs - BLOG->num_msgs) * sizeof(long)) );
90         }
91
92         BLOG->msgs[BLOG->num_msgs++].msgnum = Msg->msgnum;
93         BLOG->msgs[BLOG->num_msgs].id = NULL;
94         BLOG->msgs[BLOG->num_msgs].refs = NULL;
95
96         return 200;
97 }
98
99
100 /*
101  * People expect blogs to be sorted newest-to-oldest
102  */
103 int blogview_sortfunc(const void *s1, const void *s2) {
104         struct blogpost *l1 = (struct blogpost *)(s1);
105         struct blogpost *l2 = (struct blogpost *)(s2);
106
107         if (l1->msgnum > l2->msgnum) return(-1);
108         if (l1->msgnum < l2->msgnum) return(+1);
109         return(0);
110 }
111
112
113
114 /*
115  * Given a 'struct blogpost' containing a msgnum, populate the id
116  * and refs fields by fetching them from the Citadel server
117  */
118 void blogview_learn_thread_references(struct blogpost *bp)
119 {
120         StrBuf *Buf;
121         Buf = NewStrBuf();
122         serv_printf("MSG0 %ld|1", bp->msgnum);          /* top level citadel headers only */
123         StrBuf_ServGetln(Buf);
124         if (GetServerStatus(Buf, NULL) == 1) {
125                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
126                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
127                                 bp->id = NewStrBufDup(Buf);
128                                 StrBufCutLeft(bp->id, 5);
129                         }
130                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
131                                 bp->refs = NewStrBufDup(Buf);
132                                 StrBufCutLeft(bp->refs, 5);
133                         }
134                 }
135         }
136         FreeStrBuf(&Buf);
137 }
138
139
140
141
142 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
143 {
144         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
145         int i;
146
147         if (Stat->nummsgs > 0) {
148                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
149                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(struct blogpost), blogview_sortfunc);
150         }
151
152         for (i=0; (i<BLOG->num_msgs); ++i) {
153                 if (BLOG->msgs[i].msgnum > 0L) {
154                         blogview_learn_thread_references(&BLOG->msgs[i]);
155                         wc_printf("Message %d, #%ld, id '%s', refs '%s'<br>\n",
156                                 i,
157                                 BLOG->msgs[i].msgnum,
158                                 ChrPtr(BLOG->msgs[i].id),
159                                 ChrPtr(BLOG->msgs[i].refs)
160                         );
161                 }
162         }
163
164         return(0);
165 }
166
167
168 int blogview_Cleanup(void **ViewSpecific)
169 {
170         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
171         int i;
172
173         if (BLOG->alloc_msgs != 0) {
174                 for (i=0; i<BLOG->num_msgs; ++i) {
175                         FreeStrBuf(&BLOG->msgs[i].id);
176                         FreeStrBuf(&BLOG->msgs[i].refs);
177                 }
178                 free(BLOG->msgs);
179         }
180         free(BLOG);
181
182         wDumpContent(1);
183         return 0;
184 }
185
186
187 void 
188 InitModule_BLOGVIEWRENDERERS
189 (void)
190 {
191         RegisterReadLoopHandlerset(
192                 VIEW_BLOG,
193                 blogview_GetParamsGetServerCall,
194                 NULL,
195                 NULL, 
196                 blogview_LoadMsgFromServer,
197                 blogview_render,
198                 blogview_Cleanup
199         );
200 }