Skeleton renderer for 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  * Data which gets passed around between the various functions in this module
27  *
28  */
29 struct blogview {
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  * Entry point for message read operations.
38  */
39 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
40                                    void **ViewSpecific, 
41                                    long oper, 
42                                    char *cmd, 
43                                    long len)
44 {
45         struct blogview *BLOG = malloc(sizeof(struct blogview));
46         memset(BLOG, 0, sizeof(struct blogview));
47         *ViewSpecific = BLOG;
48
49         Stat->startmsg = (-1);                                  /* not used here */
50         Stat->sortit = 1;                                       /* not used here */
51         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
52         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
53         
54         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
55         rlid[2].cmd(cmd, len);
56         
57         return 200;
58 }
59
60
61 /*
62  * This function is called for every message in the list.
63  */
64 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
65                               void **ViewSpecific, 
66                               message_summary* Msg, 
67                               int is_new, 
68                               int i)
69 {
70         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
71
72         if (BLOG->alloc_msgs == 0) {
73                 BLOG->alloc_msgs = 1000;
74                 BLOG->msgs = malloc(BLOG->alloc_msgs * sizeof(long));
75                 memset(BLOG->msgs, 0, (BLOG->alloc_msgs * sizeof(long)) );
76         }
77
78         /* Check our buffer size */
79         if (BLOG->num_msgs >= BLOG->alloc_msgs) {
80                 BLOG->alloc_msgs *= 2;
81                 BLOG->msgs = realloc(BLOG->msgs, (BLOG->alloc_msgs * sizeof(long)));
82                 memset(&BLOG->msgs[BLOG->num_msgs], 0, ((BLOG->alloc_msgs - BLOG->num_msgs) * sizeof(long)) );
83         }
84
85         BLOG->msgs[BLOG->num_msgs++] = Msg->msgnum;
86
87         return 200;
88 }
89
90
91 int blogview_sortfunc(const void *s1, const void *s2) {
92         long l1;
93         long l2;
94
95         l1 = *(long *)(s1);
96         l2 = *(long *)(s2);
97
98         if (l1 > l2) return(+1);
99         if (l1 < l2) return(-1);
100         return(0);
101 }
102
103
104 int blogview_RenderView_or_Tail(SharedMessageStatus *Stat, 
105                                void **ViewSpecific, 
106                                long oper)
107 {
108         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
109         int i;
110         const StrBuf *Mime;
111
112         wc_printf("<div class=\"fix_scrollbar_bug\">");
113
114         if (Stat->nummsgs > 0) {
115                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
116                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(long), blogview_sortfunc);
117         }
118
119         for (i=0; (i<BLOG->num_msgs); ++i) {
120                 if (BLOG->msgs[i] > 0L) {
121                         read_message(WC->WBuf, HKEY("view_message"), BLOG->msgs[i], NULL, &Mime);
122                 }
123         }
124
125         wc_printf("</div>\n");
126         return(0);
127 }
128
129
130 int blogview_Cleanup(void **ViewSpecific)
131 {
132         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
133
134         if (BLOG->alloc_msgs != 0) {
135                 free(BLOG->msgs);
136         }
137         free(BLOG);
138
139         wDumpContent(1);
140         return 0;
141 }
142
143
144 void 
145 InitModule_BLOGVIEWRENDERERS
146 (void)
147 {
148         RegisterReadLoopHandlerset(
149                 VIEW_BLOG,
150                 blogview_GetParamsGetServerCall,
151                 NULL,
152                 NULL, 
153                 blogview_LoadMsgFromServer,
154                 blogview_RenderView_or_Tail,
155                 blogview_Cleanup
156         );
157 }