Revert "Removed the "fix_scrollbarbug" div and all references to it."
[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 /*
92  * People expect blogs to be sorted newest-to-oldest
93  */
94 int blogview_sortfunc(const void *s1, const void *s2) {
95         long l1;
96         long l2;
97
98         l1 = *(long *)(s1);
99         l2 = *(long *)(s2);
100
101         if (l1 > l2) return(-1);
102         if (l1 < l2) return(+1);
103         return(0);
104 }
105
106
107 int blogview_render(SharedMessageStatus *Stat, 
108                                void **ViewSpecific, 
109                                long oper)
110 {
111         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
112         int i;
113
114         wc_printf("<div class=\"fix_scrollbar_bug\">");
115
116         if (Stat->nummsgs > 0) {
117                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
118                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(long), blogview_sortfunc);
119         }
120
121         for (i=0; (i<BLOG->num_msgs); ++i) {
122                 if (BLOG->msgs[i] > 0L) {
123                         wc_printf("<p>Message %d %ld</p>\n", i, BLOG->msgs[i]);
124
125                         /* maybe put some of this into its own function later */
126                         StrBuf *Buf;
127                         Buf = NewStrBuf();
128                         serv_printf("MSG0 %ld|1", BLOG->msgs[i]);       /* top level citadel headers only */
129                         StrBuf_ServGetln(Buf);
130                         if (GetServerStatus(Buf, NULL) == 1) {
131                                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
132                                         wc_printf("%s<br>\n", ChrPtr(Buf));
133                                 }
134                         }
135                         FreeStrBuf(&Buf);
136                 }
137         }
138
139         wc_printf("</div>\n");
140         return(0);
141 }
142
143
144 int blogview_Cleanup(void **ViewSpecific)
145 {
146         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
147
148         if (BLOG->alloc_msgs != 0) {
149                 free(BLOG->msgs);
150         }
151         free(BLOG);
152
153         wDumpContent(1);
154         return 0;
155 }
156
157
158 void 
159 InitModule_BLOGVIEWRENDERERS
160 (void)
161 {
162         RegisterReadLoopHandlerset(
163                 VIEW_BLOG,
164                 blogview_GetParamsGetServerCall,
165                 NULL,
166                 NULL, 
167                 blogview_LoadMsgFromServer,
168                 blogview_render,
169                 blogview_Cleanup
170         );
171 }