Revert "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         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                         wc_printf("<p>Message %d %ld</p>\n", i, BLOG->msgs[i]);
122
123                         /* maybe put some of this into its own function later */
124                         StrBuf *Buf;
125                         Buf = NewStrBuf();
126                         serv_printf("MSG0 %ld|1", BLOG->msgs[i]);       /* top level citadel headers only */
127                         StrBuf_ServGetln(Buf);
128                         if (GetServerStatus(Buf, NULL) == 1) {
129                                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
130                                         wc_printf("%s<br>\n", ChrPtr(Buf));
131                                 }
132                         }
133                         FreeStrBuf(&Buf);
134                 }
135         }
136
137         return(0);
138 }
139
140
141 int blogview_Cleanup(void **ViewSpecific)
142 {
143         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
144
145         if (BLOG->alloc_msgs != 0) {
146                 free(BLOG->msgs);
147         }
148         free(BLOG);
149
150         wDumpContent(1);
151         return 0;
152 }
153
154
155 void 
156 InitModule_BLOGVIEWRENDERERS
157 (void)
158 {
159         RegisterReadLoopHandlerset(
160                 VIEW_BLOG,
161                 blogview_GetParamsGetServerCall,
162                 NULL,
163                 NULL, 
164                 blogview_LoadMsgFromServer,
165                 blogview_render,
166                 blogview_Cleanup
167         );
168 }