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