* move room listing related stuff into its own file
[citadel.git] / webcit / roomlist.c
1 /*
2  * $Id: roomlist.c 7751 2009-08-28 21:13:28Z dothebart $
3  * room listings and filters.
4  */
5
6 #include "webcit.h"
7 #include "webserver.h"
8 #include "roomops.h"
9
10 void DeleteFloor(void *vFloor)
11 {
12         floor *Floor;
13         Floor = (floor*) vFloor;
14         FreeStrBuf(&Floor->Name);
15         free(Floor);
16 }
17
18 HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP) {
19         const char *Err;
20         StrBuf *Buf;
21         HashList *floors;
22         floor *Floor;
23         const char *Pos;
24         wcsession *WCC = WC;
25
26         if (WCC->Floors != NULL)
27                 return WCC->Floors;
28         WCC->Floors = floors = NewHash(1, NULL);
29         Buf = NewStrBuf();
30  
31         Floor = malloc(sizeof(floor));
32         Floor->ID = VIRTUAL_MY_FLOOR;
33         Floor->Name = NewStrBufPlain(_("My Folders"), -1);
34         Floor->NRooms = 0;
35         
36         Put(floors, IKEY(Floor->ID), Floor, DeleteFloor);
37         serv_puts("LFLR"); /* get floors */
38         StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err); /* '100', we hope */
39         if (GetServerStatus(Buf, NULL) == 1) 
40         {
41                 while(StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err), strcmp(ChrPtr(Buf), "000")) 
42                 {
43                         
44                         Pos = NULL;
45
46                         Floor = malloc(sizeof(floor));
47                         Floor->ID = StrBufExtractNext_long(Buf, &Pos, '|');
48                         Floor->Name = NewStrBufPlain(NULL, StrLength(Buf));
49                         StrBufExtract_NextToken(Floor->Name, Buf, &Pos, '|');
50                         Floor->NRooms = StrBufExtractNext_long(Buf, &Pos, '|');
51
52                         Put(floors, IKEY(Floor->ID), Floor, DeleteFloor);
53                 }
54         }
55         FreeStrBuf(&Buf);
56         return floors;
57 }
58
59 void tmplput_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
60 {
61         floor *Floor = (floor *)(TP->Context);
62
63         StrBufAppendPrintf(Target, "%d", Floor->ID);
64 }
65
66 void tmplput_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
67 {
68         floor *Floor = (floor *)(TP->Context);
69
70         StrBufAppendTemplate(Target, TP, Floor->Name, 0);
71 }
72
73 void tmplput_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
74 {
75         floor *Floor = (floor *)(TP->Context);
76
77         StrBufAppendPrintf(Target, "%d", Floor->NRooms);
78 }
79 HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP) 
80 {
81         wcsession *WCC = WC;
82
83         if (WCC->Floors == NULL)
84                 GetFloorListHash(Target, TP);
85         serv_puts("LKRA");
86         return GetRoomListHash(Target, TP);
87 }
88
89 void DeleteFolder(void *vFolder)
90 {
91         folder *room;
92         room = (folder*) vFolder;
93
94         FreeStrBuf(&room->name);
95         FreeStrBuf(&room->ACL);
96
97         //// FreeStrBuf(&room->room);
98
99         free(room);
100 }
101
102
103 HashList *GetRoomListHash(StrBuf *Target, WCTemplputParams *TP) 
104 {
105         HashList *rooms;
106         folder *room;
107         StrBuf *Buf;
108         const char *Pos;
109         const char *Err;
110         void *vFloor;
111         wcsession *WCC = WC;
112
113         Buf = NewStrBuf();
114         rooms = NewHash(1, NULL);
115         StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err);
116         if (GetServerStatus(Buf, NULL) == 1) 
117         {
118                 while(StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err), 
119                       strcmp(ChrPtr(Buf), "000")) 
120                 {
121
122                         Pos = NULL;
123                         room = (folder*) malloc (sizeof(folder));
124                         memset(room, 0, sizeof(folder));
125
126                         room->name = NewStrBufPlain(NULL, StrLength(Buf));
127                         StrBufExtract_NextToken(room->name, Buf, &Pos, '|');
128
129                         room->QRFlags = StrBufExtractNext_long(Buf, &Pos, '|');
130                         room->floorid = StrBufExtractNext_long(Buf, &Pos, '|');
131
132                         room->listorder = StrBufExtractNext_long(Buf, &Pos, '|');
133
134                         room->ACL = NewStrBufPlain(NULL, StrLength(Buf));
135                         StrBufExtract_NextToken(room->ACL, Buf, &Pos, '|');
136
137                         room->view = StrBufExtractNext_long(Buf, &Pos, '|');
138                         room->defview = StrBufExtractNext_long(Buf, &Pos, '|');
139                         room->lastchange = StrBufExtractNext_long(Buf, &Pos, '|');
140
141                         if ((room->QRFlags & QR_MAILBOX) && 
142                             (room->floorid == 0))
143                                 room->floorid = VIRTUAL_MY_FLOOR;
144                         GetHash(WCC->Floors, IKEY(room->floorid), &vFloor);
145                         room->Floor = (const floor*) vFloor;
146                         Put(rooms, SKEY(room->name), room, DeleteFolder);
147                 }
148         }
149         SortByHashKey(rooms, 1);
150         /*SortByPayload(rooms, SortRoomsByListOrder);  */
151         FreeStrBuf(&Buf);
152         return rooms;
153 }
154
155 /** Unused function that orders rooms by the listorder flag */
156 int SortRoomsByListOrder(const void *room1, const void *room2) 
157 {
158         folder *r1 = (folder*) room1;
159         folder *r2 = (folder*) room2;
160   
161         if (r1->listorder == r2->listorder) return 0;
162         if (r1->listorder > r2->listorder) return 1;
163         return -1;
164 }
165
166 int SortRoomsByFloorAndName(const void *room1, const void *room2) 
167 {
168         folder *r1 = (folder*) room1;
169         folder *r2 = (folder*) room2;
170   
171         if (r1->Floor != r2->Floor)
172                 return strcmp(ChrPtr(r1->Floor->Name), 
173                               ChrPtr(r2->Floor->Name));
174         return strcmp (ChrPtr(r1->name), 
175                        ChrPtr(r2->name));
176 }
177
178
179
180 void tmplput_ROOM_NAME(StrBuf *Target, WCTemplputParams *TP) 
181 {
182         folder *Folder = (folder *)(TP->Context);
183
184         StrBufAppendTemplate(Target, TP, Folder->name, 0);
185 }
186
187 void tmplput_ROOM_ACL(StrBuf *Target, WCTemplputParams *TP) 
188 {
189         folder *Folder = (folder *)(TP->Context);
190
191         StrBufAppendTemplate(Target, TP, Folder->ACL, 0);
192 }
193
194
195 void tmplput_ROOM_QRFLAGS(StrBuf *Target, WCTemplputParams *TP) 
196 {
197         folder *Folder = (folder *)(TP->Context);
198         StrBufAppendPrintf(Target, "%d", Folder->QRFlags);
199 }
200
201
202
203 void tmplput_ROOM_FLOORID(StrBuf *Target, WCTemplputParams *TP) 
204 {
205         folder *Folder = (folder *)(TP->Context);
206         StrBufAppendPrintf(Target, "%d", Folder->floorid);
207 }
208
209 void tmplput_ROOM_LISTORDER(StrBuf *Target, WCTemplputParams *TP) 
210 {
211         folder *Folder = (folder *)(TP->Context);
212         StrBufAppendPrintf(Target, "%d", Folder->listorder);
213 }
214 void tmplput_ROOM_VIEW(StrBuf *Target, WCTemplputParams *TP) 
215 {
216         folder *Folder = (folder *)(TP->Context);
217         StrBufAppendPrintf(Target, "%d", Folder->view);
218 }
219 void tmplput_ROOM_DEFVIEW(StrBuf *Target, WCTemplputParams *TP) 
220 {
221         folder *Folder = (folder *)(TP->Context);
222         StrBufAppendPrintf(Target, "%d", Folder->defview);
223 }
224 void tmplput_ROOM_LASTCHANGE(StrBuf *Target, WCTemplputParams *TP) 
225 {
226         folder *Folder = (folder *)(TP->Context);
227         StrBufAppendPrintf(Target, "%d", Folder->lastchange);
228 }
229 void tmplput_ROOM_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
230 {
231         folder *Folder = (folder *)(TP->Context);
232         const floor *Floor = Folder->Floor;
233
234         if (Floor == NULL)
235                 return;
236
237         StrBufAppendPrintf(Target, "%d", Floor->ID);
238 }
239
240 void tmplput_ROOM_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
241 {
242         folder *Folder = (folder *)(TP->Context);
243         const floor *Floor = Folder->Floor;
244
245         if (Floor == NULL)
246                 return;
247
248         StrBufAppendTemplate(Target, TP, Floor->Name, 0);
249 }
250
251 void tmplput_ROOM_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
252 {
253         folder *Folder = (folder *)(TP->Context);
254         const floor *Floor = Folder->Floor;
255
256         if (Floor == NULL)
257                 return;
258         StrBufAppendPrintf(Target, "%d", Floor->NRooms);
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 void jsonRoomFlr(void) 
275 {
276         /* Send as our own (application/json) content type */
277         hprintf("HTTP/1.1 200 OK\r\n");
278         hprintf("Content-type: application/json; charset=utf-8\r\n");
279         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
280         hprintf("Connection: close\r\n");
281         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
282         begin_burst();
283         DoTemplate(HKEY("json_roomflr"),NULL,&NoCtx);
284         end_burst(); 
285 }
286
287
288
289 void 
290 InitModule_ROOMLIST
291 (void)
292 {
293         WebcitAddUrlHandler(HKEY("json_roomflr"), jsonRoomFlr, 0);
294
295
296         RegisterNamespace("FLOOR:ID", 0, 0, tmplput_FLOOR_ID, CTX_FLOORS);
297         RegisterNamespace("FLOOR:NAME", 0, 1, tmplput_FLOOR_NAME, CTX_FLOORS);
298         RegisterNamespace("FLOOR:NROOMS", 0, 0, tmplput_FLOOR_NROOMS, CTX_FLOORS);
299
300
301
302         RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, DeleteHash, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
303
304         RegisterNamespace("ROOM:INFO:FLOORID", 0, 1, tmplput_ROOM_FLOORID, CTX_ROOMS);
305         RegisterNamespace("ROOM:INFO:NAME", 0, 1, tmplput_ROOM_NAME, CTX_ROOMS);
306         RegisterNamespace("ROOM:INFO:ACL", 0, 1, tmplput_ROOM_ACL, CTX_ROOMS);
307         RegisterNamespace("ROOM:INFO:QRFLAGS", 0, 1, tmplput_ROOM_QRFLAGS, CTX_ROOMS);
308         RegisterNamespace("ROOM:INFO:LISTORDER", 0, 1, tmplput_ROOM_LISTORDER, CTX_ROOMS);
309         RegisterNamespace("ROOM:INFO:VIEW", 0, 1, tmplput_ROOM_VIEW, CTX_ROOMS);
310         RegisterNamespace("ROOM:INFO:DEFVIEW", 0, 1, tmplput_ROOM_DEFVIEW, CTX_ROOMS);
311         RegisterNamespace("ROOM:INFO:LASTCHANGE", 0, 1, tmplput_ROOM_LASTCHANGE, CTX_ROOMS);
312         RegisterNamespace("ROOM:INFO:FLOOR:ID", 0, 0, tmplput_ROOM_FLOOR_ID, CTX_ROOMS);
313         RegisterNamespace("ROOM:INFO:FLOOR:NAME", 0, 1, tmplput_ROOM_FLOOR_NAME, CTX_ROOMS);
314         RegisterNamespace("ROOM:INFO:FLOOR:NROOMS", 0, 0, tmplput_ROOM_FLOOR_NROOMS, CTX_ROOMS);
315
316 /*
317         RegisterSortFunc(HKEY("byfloorroom"),
318                          NULL, 0,
319                          CompareRoomListByFloorRoomPrivFirst,
320                          CompareRoomListByFloorRoomPrivFirstRev,
321                          GroupchangeRoomListByFloorRoomPrivFirst,
322                          CTX_ROOMS);
323 */
324 }