* merge from dav_rework:
[citadel.git] / webcit / roomlist.c
1 /*
2  * $Id$
3  * room listings and filters.
4  */
5
6 #include "webcit.h"
7 #include "webserver.h"
8
9
10 void DeleteFloor(void *vFloor)
11 {
12         Floor *pFloor;
13         pFloor = (Floor*) vFloor;
14         FreeStrBuf(&pFloor->Name);
15         free(pFloor);
16 }
17
18 int SortFloorsByNameOrder(const void *vfloor1, const void *vfloor2) 
19 {
20         Floor *f1 = (Floor*) GetSearchPayload(vfloor1);
21         Floor *f2 = (Floor*) GetSearchPayload(vfloor2);
22         
23         /* prefer My floor over alpabetical sort */
24         if (f1->ID == VIRTUAL_MY_FLOOR)
25                 return 1;
26         if (f2->ID == VIRTUAL_MY_FLOOR)
27                 return -1;
28
29         return strcmp(ChrPtr(f1->Name), ChrPtr(f2->Name));
30 }
31
32 HashList *GetFloorListHash(StrBuf *Target, WCTemplputParams *TP) 
33 {
34         int Done = 0;
35         const char *Err;
36         StrBuf *Buf;
37         HashList *floors;
38         HashList *floorsbyname;
39         HashPos *it;
40         Floor *pFloor;
41         void *vFloor;
42         const char *Pos;
43         int i;
44         wcsession *WCC = WC;
45         const char *HashKey;
46         long HKLen;
47
48
49         if (WCC->Floors != NULL)
50                 return WCC->Floors;
51         WCC->Floors = floors = NewHash(1, Flathash);
52         WCC->FloorsByName = floorsbyname = NewHash(1, NULL);
53         Buf = NewStrBuf();
54
55         pFloor = (Floor*) malloc(sizeof(Floor));
56         pFloor->ID = VIRTUAL_MY_FLOOR;
57         pFloor->Name = NewStrBufPlain(_("My Folders"), -1);
58         pFloor->NRooms = 0;
59         
60         Put(floors, IKEY(pFloor->ID), pFloor, DeleteFloor);
61         Put(floorsbyname, SKEY(pFloor->Name), pFloor, reference_free_handler);
62
63         serv_puts("LFLR"); /* get floors */
64         StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err); /* '100', we hope */
65         if (GetServerStatus(Buf, NULL) == 1) 
66         {
67                 while(!Done && StrBuf_ServGetln(Buf))
68                         if ( (StrLength(Buf)==3) && 
69                              !strcmp(ChrPtr(Buf), "000")) 
70                         {
71                                 Done = 1;
72                         }
73                         else
74                         {
75                         
76                                 Pos = NULL;
77
78                                 pFloor = (Floor*) malloc(sizeof(Floor));
79                                 pFloor->ID = StrBufExtractNext_int(Buf, &Pos, '|');
80                                 pFloor->Name = NewStrBufPlain(NULL, StrLength(Buf));
81                                 StrBufExtract_NextToken(pFloor->Name, Buf, &Pos, '|');
82                                 pFloor->NRooms = StrBufExtractNext_long(Buf, &Pos, '|');
83
84                                 Put(floors, IKEY(pFloor->ID), pFloor, DeleteFloor);
85                                 Put(floorsbyname, SKEY(pFloor->Name), pFloor, reference_free_handler);
86                         }
87         }
88         FreeStrBuf(&Buf);
89         
90         /* now lets pre-sort them alphabeticaly. */
91         i = 1;
92         SortByPayload(floors, SortFloorsByNameOrder);
93         it = GetNewHashPos(floors, 0);
94         while ( GetNextHashPos(floors, it, &HKLen, &HashKey, &vFloor)) 
95                 ((Floor*) vFloor)->AlphaN = i++;
96         DeleteHashPos(&it);
97         SortByHashKeyStr(floors);
98
99         return floors;
100 }
101
102 void tmplput_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
103 {
104         Floor *myFloor = (Floor *)CTX;
105
106         StrBufAppendPrintf(Target, "%d", myFloor->ID);
107 }
108
109 void tmplput_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
110 {
111         Floor *myFloor = (Floor *)CTX;
112
113         StrBufAppendTemplate(Target, TP, myFloor->Name, 0);
114 }
115
116 void tmplput_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
117 {
118         Floor *myFloor = (Floor *)CTX;
119
120         StrBufAppendPrintf(Target, "%d", myFloor->NRooms);
121 }
122 HashList *GetRoomListHashLKRA(StrBuf *Target, WCTemplputParams *TP) 
123 {
124         wcsession *WCC = WC;
125
126         if (WCC->Floors == NULL)
127                 GetFloorListHash(Target, TP);
128         serv_puts("LKRA");
129         if (WCC->Rooms == NULL) 
130                 WCC->Rooms =  GetRoomListHash(Target, TP);
131         return WCC->Rooms;
132 }
133
134 void FlushFolder(folder *room)
135 {
136         int i;
137
138         FreeStrBuf(&room->name);
139         if (room->RoomNameParts != NULL)
140         {
141                 for (i=0; i < room->nRoomNameParts; i++)
142                         FreeStrBuf(&room->RoomNameParts[i]);
143                 free(room->RoomNameParts);
144         }
145 }
146
147 void vDeleteFolder(void *vFolder)
148 {
149         folder *room;
150
151         room = (folder*) vFolder;
152         FlushFolder(room);
153
154         free(room);
155 }
156
157
158 HashList *GetRoomListHash(StrBuf *Target, WCTemplputParams *TP) 
159 {
160         int Done = 0;
161         HashList *rooms;
162         folder *room;
163         StrBuf *Buf;
164         const char *Pos;
165         const char *Err;
166         void *vFloor;
167         wcsession *WCC = WC;
168         CompareFunc SortIt;
169         WCTemplputParams SubTP;
170
171         Buf = NewStrBuf();
172         rooms = NewHash(1, NULL);
173         StrBufTCP_read_line(Buf, &WC->serv_sock, 0, &Err);
174         if (GetServerStatus(Buf, NULL) == 1) 
175         {
176                 while(!Done && StrBuf_ServGetln(Buf))
177                         if ( (StrLength(Buf)==3) && 
178                              !strcmp(ChrPtr(Buf), "000")) 
179                         {
180                                 Done = 1;
181                         }
182                         else
183                         {                               
184                                 Pos = NULL;
185                                 room = (folder*) malloc (sizeof(folder));
186                                 memset(room, 0, sizeof(folder));
187
188                                 /* Load the base data from the server reply */
189                                 room->name = NewStrBufPlain(NULL, StrLength(Buf));
190                                 StrBufExtract_NextToken(room->name, Buf, &Pos, '|');
191
192                                 room->QRFlags = StrBufExtractNext_long(Buf, &Pos, '|');
193                                 room->floorid = StrBufExtractNext_int(Buf, &Pos, '|');
194                                 room->listorder = StrBufExtractNext_long(Buf, &Pos, '|');
195                                 room->QRFlags2 = StrBufExtractNext_long(Buf, &Pos, '|');
196
197                                 room->RAFlags = StrBufExtractNext_long(Buf, &Pos, '|');
198
199 /*
200   ACWHUT?
201   room->ACL = NewStrBufPlain(NULL, StrLength(Buf));
202   StrBufExtract_NextToken(room->ACL, Buf, &Pos, '|');
203 */
204
205                                 room->view = StrBufExtractNext_long(Buf, &Pos, '|');
206                                 room->defview = StrBufExtractNext_long(Buf, &Pos, '|');
207                                 room->lastchange = StrBufExtractNext_long(Buf, &Pos, '|');
208
209                                 /* Evaluate the Server sent data for later use */
210                                 /* find out, whether we are in a sub-room */
211                                 room->nRoomNameParts = StrBufNum_tokens(room->name, '\\');
212                                 if (room->nRoomNameParts > 1)
213                                 {
214                                         int i;
215
216                                         Pos = NULL;
217                                         room->RoomNameParts = malloc(sizeof(StrBuf*) * (room->nRoomNameParts + 1));
218                                         memset(room->RoomNameParts, 0, sizeof(StrBuf*) * (room->nRoomNameParts + 1));
219                                         for (i=0; i < room->nRoomNameParts; i++)
220                                         {
221                                                 room->RoomNameParts[i] = NewStrBuf();
222                                                 StrBufExtract_NextToken(room->RoomNameParts[i],
223                                                                         room->name, &Pos, '\\');
224                                         }
225                                 }
226
227                                 /* Private mailboxes on the main floor get remapped to the personal folder */
228                                 if ((room->QRFlags & QR_MAILBOX) && 
229                                     (room->floorid == 0))
230                                 {
231                                         room->floorid = VIRTUAL_MY_FLOOR;
232                                         if ((room->nRoomNameParts == 1) && 
233                                             (StrLength(room->name) == 4) && 
234                                             (strcmp(ChrPtr(room->name), "Mail") == 0))
235                                         {
236                                                 room->is_inbox = 1;
237                                         }
238
239                                 }
240                                 /* get a pointer to the floor we're on: */
241                                 GetHash(WCC->Floors, IKEY(room->floorid), &vFloor);
242                                 room->Floor = (const Floor*) vFloor;
243
244
245
246                                 /* now we know everything, remember it... */
247                                 Put(rooms, SKEY(room->name), room, vDeleteFolder);
248                         }
249         }
250
251         SubTP.Filter.ContextType = CTX_ROOMS;
252         SortIt = RetrieveSort(&SubTP, NULL, 0, HKEY("fileunsorted"), 0);
253         if (SortIt != NULL)
254                 SortByPayload(rooms, SortIt);
255         else 
256                 SortByPayload(rooms, SortRoomsByListOrder);
257         FreeStrBuf(&Buf);
258         return rooms;
259 }
260
261 /** Unused function that orders rooms by the listorder flag */
262 int SortRoomsByListOrder(const void *room1, const void *room2) 
263 {
264         folder *r1 = (folder*) GetSearchPayload(room1);
265         folder *r2 = (folder*) GetSearchPayload(room2);
266   
267         if (r1->listorder == r2->listorder) return 0;
268         if (r1->listorder > r2->listorder) return 1;
269         return -1;
270 }
271
272 int CompareRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
273 {
274         folder *r1 = (folder*) GetSearchPayload(room1);
275         folder *r2 = (folder*) GetSearchPayload(room2);
276   
277         if ((r1->Floor == NULL)  ||
278             (r2->Floor == NULL))
279                 return 0;
280                 
281         /**
282          * are we on the same floor? else sort by floor.
283          */
284         if (r1->Floor != r2->Floor)
285         {
286                 /**
287                  * the private rooms are first in any case.
288                  */
289                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
290                         return -1;
291                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
292                         return 1;
293                 /**
294                  * else decide alpaheticaly by floorname
295                  */
296                 return (r1->Floor->AlphaN > r2->Floor->AlphaN)? 1 : -1;
297         }
298
299         /**
300          * if we have different levels of subdirectories, 
301          * we want the toplevel to be first, regardless of sort
302          * sequence.
303          */
304         if (((r1->nRoomNameParts > 1) || 
305             (r2->nRoomNameParts > 1)    )&&
306             (r1->nRoomNameParts != r2->nRoomNameParts))
307         {
308                 int i, ret;
309                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
310                         r2->nRoomNameParts : r1->nRoomNameParts;
311
312                 for (i=0; i < nparts; i++)
313                 {
314                         ret = strcmp (ChrPtr(r1->name), 
315                                       ChrPtr(r2->name));
316                         /**
317                          * Deltas in common parts? exit here.
318                          */
319                         if (ret != 0) 
320                                 return ret;
321                 }
322
323                 /**
324                  * who's a subdirectory of whom?
325                  */
326                 if (r1->nRoomNameParts > r2->nRoomNameParts)
327                         return 1;
328                 else
329                         return -1;
330
331         }
332
333         /**
334          * else just sort alphabeticaly.
335          */
336         return strcmp (ChrPtr(r1->name), 
337                        ChrPtr(r2->name));
338 }
339
340 int CompareRoomListByFloorRoomPrivFirstRev(const void *room1, const void *room2) 
341 {
342         folder *r1 = (folder*) GetSearchPayload(room1);
343         folder *r2 = (folder*) GetSearchPayload(room2);
344
345         if ((r1->Floor == NULL)  ||
346             (r2->Floor == NULL))
347                 return 0;
348
349         /**
350          * are we on the same floor? else sort by floor.
351          */
352         if (r2->Floor != r1->Floor)
353         {
354                 /**
355                  * the private rooms are first in any case.
356                  */
357                 if (r1->Floor->ID == VIRTUAL_MY_FLOOR)
358                         return -1;
359                 if (r2->Floor->ID == VIRTUAL_MY_FLOOR)
360                         return 1;
361                 /**
362                  * else decide alpaheticaly by floorname
363                  */
364
365                 return (r1->Floor->AlphaN < r2->Floor->AlphaN)? 1 : -1;
366         }
367
368         /**
369          * if we have different levels of subdirectories, 
370          * we want the toplevel to be first, regardless of sort
371          * sequence.
372          */
373         if (((r1->nRoomNameParts > 1) || 
374             (r2->nRoomNameParts > 1)    )&&
375             (r1->nRoomNameParts != r2->nRoomNameParts))
376         {
377                 int i, ret;
378                 int nparts = (r1->nRoomNameParts > r2->nRoomNameParts)?
379                         r2->nRoomNameParts : r1->nRoomNameParts;
380
381                 for (i=0; i < nparts; i++)
382                 {
383                         /**
384                          * special cases if one room is top-level...
385                          */
386                         if (r2->nRoomNameParts == 1)
387                                 ret = strcmp (ChrPtr(r2->name), 
388                                               ChrPtr(r1->RoomNameParts[i]));
389                         else if (r1->nRoomNameParts == 1)
390                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]),
391                                               ChrPtr(r1->name));
392                         else 
393                                 ret = strcmp (ChrPtr(r2->RoomNameParts[i]), 
394                                               ChrPtr(r1->RoomNameParts[i]));
395                         /**
396                          * Deltas in common parts? exit here.
397                          */
398                         if (ret != 0) 
399                                 return ret;
400                 }
401
402                 /**
403                  * who's a subdirectory of whom?
404                  */
405                 if (r1->nRoomNameParts > r2->nRoomNameParts)
406                         return 1;
407                 else
408                         return -1;
409         }
410
411         return strcmp (ChrPtr(r2->name), 
412                        ChrPtr(r1->name));
413 }
414
415 int GroupchangeRoomListByFloorRoomPrivFirst(const void *room1, const void *room2) 
416 {
417         folder *r1 = (folder*) room1;
418         folder *r2 = (folder*) room2;
419   
420
421         if ((r1->Floor == NULL)  ||
422             (r2->Floor == NULL))
423                 return 0;
424                 
425         if (r1->Floor == r2->Floor)
426                 return 0;
427         else 
428         {
429                 wcsession *WCC = WC;
430                 static int columns = 3;
431                 int boxes_per_column = 0;
432                 int nf;
433
434                 nf = GetCount(WCC->Floors);
435                 while (nf % columns != 0) ++nf;
436                 boxes_per_column = (nf / columns);
437                 if (boxes_per_column < 1)
438                         boxes_per_column = 1;
439                 if (r1->Floor->AlphaN % boxes_per_column == 0)
440                         return 2;
441                 else 
442                         return 1;
443         }
444 }
445
446
447
448
449
450
451 void tmplput_ROOM_NAME(StrBuf *Target, WCTemplputParams *TP) 
452 {
453         folder *Folder = (folder *)CTX;
454
455         StrBufAppendTemplate(Target, TP, Folder->name, 0);
456 }
457 void tmplput_ROOM_BASENAME(StrBuf *Target, WCTemplputParams *TP) 
458 {
459         folder *room = (folder *)CTX;
460
461         if (room->nRoomNameParts > 1)
462                 StrBufAppendTemplate(Target, TP, 
463                                       room->RoomNameParts[room->nRoomNameParts - 1], 0);
464         else 
465                 StrBufAppendTemplate(Target, TP, room->name, 0);
466 }
467 void tmplput_ROOM_LEVEL_N_TIMES(StrBuf *Target, WCTemplputParams *TP) 
468 {
469         folder *room = (folder *)CTX;
470         int i;
471         const char *AppendMe;
472         long AppendMeLen;
473
474
475         if (room->nRoomNameParts > 1)
476         {
477                 GetTemplateTokenString(Target, TP, 0, &AppendMe, &AppendMeLen);
478                 for (i = 0; i < room->nRoomNameParts; i++)
479                         StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, 0);
480         }
481 }
482
483 void tmplput_ROOM_ACL(StrBuf *Target, WCTemplputParams *TP) 
484 {
485         folder *Folder = (folder *)CTX;
486
487         StrBufAppendPrintf(Target, "%ld", Folder->RAFlags, 0);
488 }
489
490
491 void tmplput_ROOM_QRFLAGS(StrBuf *Target, WCTemplputParams *TP) 
492 {
493         folder *Folder = (folder *)CTX;
494         StrBufAppendPrintf(Target, "%d", Folder->QRFlags);
495 }
496
497 void tmplput_ROOM_RAFLAGS(StrBuf *Target, WCTemplputParams *TP) 
498 {
499         folder *Folder = (folder *)(TP->Context);
500         StrBufAppendPrintf(Target, "%d", Folder->RAFlags);
501 }
502
503
504 void tmplput_ROOM_FLOORID(StrBuf *Target, WCTemplputParams *TP) 
505 {
506         folder *Folder = (folder *)CTX;
507         StrBufAppendPrintf(Target, "%d", Folder->floorid);
508 }
509
510 void tmplput_ROOM_LISTORDER(StrBuf *Target, WCTemplputParams *TP) 
511 {
512         folder *Folder = (folder *)CTX;
513         StrBufAppendPrintf(Target, "%d", Folder->listorder);
514 }
515 void tmplput_ROOM_VIEW(StrBuf *Target, WCTemplputParams *TP) 
516 {
517         folder *Folder = (folder *)CTX;
518         StrBufAppendPrintf(Target, "%d", Folder->view);
519 }
520 void tmplput_ROOM_DEFVIEW(StrBuf *Target, WCTemplputParams *TP) 
521 {
522         folder *Folder = (folder *)CTX;
523         StrBufAppendPrintf(Target, "%d", Folder->defview);
524 }
525 void tmplput_ROOM_LASTCHANGE(StrBuf *Target, WCTemplputParams *TP) 
526 {
527         folder *Folder = (folder *)CTX;
528         StrBufAppendPrintf(Target, "%d", Folder->lastchange);
529 }
530 void tmplput_ROOM_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
531 {
532         folder *Folder = (folder *)CTX;
533         const Floor *pFloor = Folder->Floor;
534
535         if (pFloor == NULL)
536                 return;
537
538         StrBufAppendPrintf(Target, "%d", pFloor->ID);
539 }
540
541 void tmplput_ROOM_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
542 {
543         folder *Folder = (folder *)CTX;
544         const Floor *pFloor = Folder->Floor;
545
546         if (pFloor == NULL)
547                 return;
548
549         StrBufAppendTemplate(Target, TP, pFloor->Name, 0);
550 }
551
552 void tmplput_ROOM_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
553 {
554         folder *Folder = (folder *)CTX;
555         const Floor *pFloor = Folder->Floor;
556
557         if (pFloor == NULL)
558                 return;
559         StrBufAppendPrintf(Target, "%d", pFloor->NRooms);
560 }
561
562
563
564 int ConditionalRoomHas_UA_KNOWN(StrBuf *Target, WCTemplputParams *TP)
565 {
566         folder *Folder = (folder *)CTX;
567         return (Folder->RAFlags & UA_KNOWN) != 0;
568 }
569
570 int ConditionalRoomHas_UA_GOTOALLOWED(StrBuf *Target, WCTemplputParams *TP)
571 {
572         folder *Folder = (folder *)CTX;
573         return (Folder->RAFlags & UA_GOTOALLOWED) != 0;
574 }
575
576 int ConditionalRoomHas_UA_HASNEWMSGS(StrBuf *Target, WCTemplputParams *TP)
577 {
578         folder *Folder = (folder *)CTX;
579         return (Folder->RAFlags & UA_HASNEWMSGS) != 0;
580 }
581
582 int ConditionalRoomHas_UA_ZAPPED(StrBuf *Target, WCTemplputParams *TP)
583 {
584         folder *Folder = (folder *)CTX;
585         return (Folder->RAFlags & UA_ZAPPED) != 0;
586 }
587
588 int ConditionalRoomHas_UA_POSTALLOWED(StrBuf *Target, WCTemplputParams *TP)
589 {
590         folder *Folder = (folder *)CTX;
591         return (Folder->RAFlags & UA_POSTALLOWED) != 0;
592 }
593
594 int ConditionalRoomHas_UA_ADMINALLOWED(StrBuf *Target, WCTemplputParams *TP)
595 {
596         folder *Folder = (folder *)CTX;
597         return (Folder->RAFlags & UA_ADMINALLOWED) != 0;
598 }
599
600 int ConditionalRoomHas_UA_DELETEALLOWED(StrBuf *Target, WCTemplputParams *TP)
601 {
602         folder *Folder = (folder *)CTX;
603         return (Folder->RAFlags & UA_DELETEALLOWED) != 0;
604 }
605
606
607 int ConditionalRoomIsInbox(StrBuf *Target, WCTemplputParams *TP)
608 {
609         folder *Folder = (folder *)CTX;
610         return Folder->is_inbox;
611 }
612
613 void tmplput_ROOM_COLLECTIONTYPE(StrBuf *Target, WCTemplputParams *TP) 
614 {
615         folder *Folder = (folder *)CTX;
616         
617         switch(Folder->view) {
618         case VIEW_CALENDAR:
619                 StrBufAppendBufPlain(Target, HKEY("vevent"), 0);
620                 break;
621         case VIEW_TASKS:
622                 StrBufAppendBufPlain(Target, HKEY("vtodo"), 0);
623                 break;
624         case VIEW_ADDRESSBOOK:
625                 StrBufAppendBufPlain(Target, HKEY("vcard"), 0);
626                 break;
627         case VIEW_NOTES:
628                 StrBufAppendBufPlain(Target, HKEY("vnotes"), 0);
629                 break;
630         case VIEW_JOURNAL:
631                 StrBufAppendBufPlain(Target, HKEY("vjournal"), 0);
632                 break;
633         case VIEW_WIKI:
634                 StrBufAppendBufPlain(Target, HKEY("wiki"), 0);
635                 break;
636         }
637 }
638
639
640
641
642 int ConditionalRoomHasGroupdavContent(StrBuf *Target, WCTemplputParams *TP)
643 {
644         folder *Folder = (folder *)CTX;
645
646         lprintf(0, "-> %s: %ld\n", ChrPtr(Folder->name), Folder->view);
647
648         return ((Folder->view == VIEW_CALENDAR) || 
649                 (Folder->view == VIEW_TASKS) || 
650                 (Folder->view == VIEW_ADDRESSBOOK) ||
651                 (Folder->view == VIEW_NOTES) ||
652                 (Folder->view == VIEW_JOURNAL) );
653 }
654
655
656
657 int ConditionalFloorIsRESTSubFloor(StrBuf *Target, WCTemplputParams *TP)
658 {
659         wcsession  *WCC = WC;
660         Floor *MyFloor = (Floor *)CTX;
661         /** if we have dav_depth the client just wants the subfloors */
662         if ((WCC->Hdr->HR.dav_depth == 1) && 
663             (GetCount(WCC->Directory) == 0))
664                 return 1;
665         return WCC->CurrentFloor == MyFloor;
666 }
667
668
669 int ConditionalRoomIsRESTSubRoom(StrBuf *Target, WCTemplputParams *TP)
670 {
671         wcsession  *WCC = WC;
672         folder     *Folder = (folder *)CTX;
673         HashPos    *it;
674         StrBuf     * Dir;
675         void       *vDir;
676         long        len;
677         const char *Key;
678         int i, j, urlp;
679         int delta;
680
681
682         /* list only folders relative to the current floor... */
683         if (Folder->Floor != WCC->CurrentFloor)
684                 return 0;
685
686         urlp = GetCount(WCC->Directory);
687         delta = Folder->nRoomNameParts - urlp + 1;
688
689         lprintf(0, "\n->%s: %ld - %ld ", ChrPtr(Folder->name), urlp, 
690                 Folder->nRoomNameParts);
691         /* list only the floors which are in relation to the dav_depth header */
692         if (WCC->Hdr->HR.dav_depth != delta) {
693                 lprintf(0, "1\n");
694                 return 0;
695         }
696
697
698         it = GetNewHashPos(WCC->Directory, 0);
699         /* Fast forward the floorname we checked above... */
700         GetNextHashPos(WCC->Directory, it, &len, &Key, &vDir);
701
702         if (Folder->nRoomNameParts > 1) {               
703                 for (i = 0, j = 1; 
704                      (i > Folder->nRoomNameParts) && (j > urlp); 
705                      i++, j++)
706                 {
707                         if (!GetNextHashPos(WCC->Directory, 
708                                             it, &len, &Key, &vDir) ||
709                             (vDir == NULL))
710                         {
711                                 DeleteHashPos(&it);
712
713                                 lprintf(0, "3\n");
714                                 return 0;
715                         }
716                         Dir = (StrBuf*) vDir;
717                         if (strcmp(ChrPtr(Folder->RoomNameParts[i]), 
718                                    ChrPtr(Dir)) != 0)
719                         {
720                                 DeleteHashPos(&it);
721                                 lprintf(0, "4\n");
722                                 return 0;
723                         }
724                 }
725                 DeleteHashPos(&it);
726                 return 1;
727         }
728         else {
729                 if (!GetNextHashPos(WCC->Directory, 
730                                     it, &len, &Key, &vDir) ||
731                     (vDir == NULL))
732                 {
733                         DeleteHashPos(&it);
734                         
735                         lprintf(0, "5\n");
736                         return WCC->Hdr->HR.dav_depth == 1;
737                 }
738                 DeleteHashPos(&it);
739                 Dir = (StrBuf*) vDir;
740                 if (WCC->Hdr->HR.dav_depth == 0) {
741                         return (strcmp(ChrPtr(Folder->name), 
742                                        ChrPtr(Dir))
743                                 == 0);
744
745                 }
746                 return 0;
747         }
748 }
749
750
751 void jsonRoomFlr(void) 
752 {
753         /* Send as our own (application/json) content type */
754         hprintf("HTTP/1.1 200 OK\r\n");
755         hprintf("Content-type: application/json; charset=utf-8\r\n");
756         hprintf("Server: %s / %s\r\n", PACKAGE_STRING, ChrPtr(WC->serv_info->serv_software));
757         hprintf("Connection: close\r\n");
758         hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
759         begin_burst();
760         DoTemplate(HKEY("json_roomflr"),NULL,&NoCtx);
761         end_burst(); 
762 }
763
764
765 void 
766 SessionDetachModule_ROOMLIST
767 (wcsession *sess)
768 {
769         DeleteHash(&sess->Floors);
770         DeleteHash(&sess->Rooms);
771         DeleteHash(&sess->FloorsByName);
772 }
773
774 void 
775 InitModule_ROOMLIST
776 (void)
777 {
778         WebcitAddUrlHandler(HKEY("json_roomflr"), "", 0, jsonRoomFlr, 0);
779
780
781         RegisterNamespace("FLOOR:ID", 0, 0, tmplput_FLOOR_ID, NULL, CTX_FLOORS);
782         RegisterNamespace("FLOOR:NAME", 0, 1, tmplput_FLOOR_NAME, NULL, CTX_FLOORS);
783         RegisterNamespace("FLOOR:NROOMS", 0, 0, tmplput_FLOOR_NROOMS, NULL, CTX_FLOORS);
784         RegisterConditional(HKEY("COND:ROOM:REST:ISSUBFLOOR"), 0, ConditionalFloorIsRESTSubFloor, CTX_FLOORS);
785
786         RegisterIterator("LFLR", 0, NULL, GetFloorListHash, NULL, NULL, CTX_FLOORS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
787
788         RegisterIterator("LKRA", 0, NULL, GetRoomListHashLKRA, NULL, NULL, CTX_ROOMS, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
789
790         RegisterNamespace("ROOM:INFO:FLOORID", 0, 1, tmplput_ROOM_FLOORID, NULL, CTX_ROOMS);
791         RegisterNamespace("ROOM:INFO:NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);
792         RegisterNamespace("ROOM:INFO:PRINT_NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);/// TODO!
793         RegisterNamespace("ROOM:INFO:BASENAME", 0, 1, tmplput_ROOM_BASENAME, NULL, CTX_ROOMS);
794         RegisterNamespace("ROOM:INFO:LEVELNTIMES", 1, 2, tmplput_ROOM_LEVEL_N_TIMES, NULL, CTX_ROOMS);
795
796         RegisterNamespace("ROOM:INFO:ACL", 0, 1, tmplput_ROOM_ACL, NULL, CTX_ROOMS);
797         RegisterNamespace("ROOM:INFO:QRFLAGS", 0, 1, tmplput_ROOM_QRFLAGS, NULL, CTX_ROOMS);
798         RegisterNamespace("ROOM:INFO:RAFLAGS", 0, 1, tmplput_ROOM_RAFLAGS, NULL, CTX_ROOMS);
799         RegisterNamespace("ROOM:INFO:LISTORDER", 0, 1, tmplput_ROOM_LISTORDER, NULL, CTX_ROOMS);
800         RegisterNamespace("ROOM:INFO:VIEW", 0, 1, tmplput_ROOM_VIEW, NULL, CTX_ROOMS);
801         RegisterNamespace("ROOM:INFO:DEFVIEW", 0, 1, tmplput_ROOM_DEFVIEW, NULL, CTX_ROOMS);
802         RegisterNamespace("ROOM:INFO:LASTCHANGE", 0, 1, tmplput_ROOM_LASTCHANGE, NULL, CTX_ROOMS);
803         RegisterNamespace("ROOM:INFO:COLLECTIONTYPE", 0, 1, tmplput_ROOM_COLLECTIONTYPE, NULL, CTX_ROOMS);
804         RegisterNamespace("ROOM:INFO:FLOOR:ID", 0, 0, tmplput_ROOM_FLOOR_ID, NULL, CTX_ROOMS);
805         RegisterNamespace("ROOM:INFO:FLOOR:NAME", 0, 1, tmplput_ROOM_FLOOR_NAME, NULL, CTX_ROOMS);
806         RegisterNamespace("ROOM:INFO:FLOOR:NROOMS", 0, 0, tmplput_ROOM_FLOOR_NROOMS, NULL, CTX_ROOMS);
807
808         RegisterConditional(HKEY("COND:ROOM:REST:ISSUBROOM"), 0, ConditionalRoomIsRESTSubRoom, CTX_ROOMS);
809
810         RegisterConditional(HKEY("COND:ROOM:INFO:IS_INBOX"), 0, ConditionalRoomIsInbox, CTX_ROOMS);
811         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_KNOWN"), 0, ConditionalRoomHas_UA_KNOWN, CTX_ROOMS);
812         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_GOTOALLOWED"), 0, ConditionalRoomHas_UA_GOTOALLOWED, CTX_ROOMS);
813         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_HASNEWMSGS"), 0, ConditionalRoomHas_UA_HASNEWMSGS, CTX_ROOMS);
814         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_ZAPPED"), 0, ConditionalRoomHas_UA_ZAPPED, CTX_ROOMS);
815         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_POSTALLOWED"), 0, ConditionalRoomHas_UA_POSTALLOWED, CTX_ROOMS);
816         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_ADMINALLOWED"), 0, ConditionalRoomHas_UA_ADMINALLOWED, CTX_ROOMS);
817         RegisterConditional(HKEY("COND:ROOM:FLAGS:UA_DELETEALLOWED"), 0, ConditionalRoomHas_UA_DELETEALLOWED, CTX_ROOMS);
818         RegisterConditional(HKEY("COND:ROOM:GROUPDAV_CONTENT"), 0, ConditionalRoomHasGroupdavContent, CTX_ROOMS);
819
820
821
822         RegisterSortFunc(HKEY("byfloorroom"),
823                          NULL, 0,
824                          CompareRoomListByFloorRoomPrivFirst,
825                          CompareRoomListByFloorRoomPrivFirstRev,
826                          GroupchangeRoomListByFloorRoomPrivFirst,
827                          CTX_ROOMS);
828
829 }