SUBST: Dynamicaly generate contexts
[citadel.git] / webcit / roomtokens.c
index d41b36af1a5e52a717f36fdab5670dae4471bbf0..57d21443ef342c3071900b984cf666ee4fa23056 100644 (file)
@@ -1,26 +1,22 @@
 /*
  * Lots of different room-related operations.
  *
- * Copyright (c) 1996-2011 by the citadel.org team
+ * Copyright (c) 1996-2012 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as published
- * by the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * modify it under the terms of the GNU General Public License, version 3.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
 #include "webcit.h"
 #include "webserver.h"
 
+CtxType CTX_ROOMS = CTX_NONE;
+CtxType CTX_FLOORS = CTX_NONE;
 
 /*
  * Embed the room banner
@@ -67,7 +63,7 @@ void tmplput_roombanner(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
 {
-       Floor *myFloor = (Floor *)CTX;
+       Floor *myFloor = (Floor *)CTX(CTX_FLOORS);
 
        StrBufAppendPrintf(Target, "%d", myFloor->ID);
 }
@@ -75,14 +71,14 @@ void tmplput_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_FLOORID(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        StrBufAppendPrintf(Target, "%d", Folder->floorid);
 }
 
 
 void tmplput_ROOM_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        const Floor *pFloor = Folder->Floor;
 
        if (pFloor == NULL)
@@ -94,7 +90,7 @@ void tmplput_ROOM_FLOOR_ID(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        const Floor *pFloor = Folder->Floor;
 
        if (pFloor == NULL)
@@ -123,7 +119,7 @@ void tmplput_ThisRoomFloorName(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP) 
 {
-       Floor *myFloor = (Floor *)CTX;
+       Floor *myFloor = (Floor *)CTX(CTX_FLOORS);
 
        StrBufAppendTemplate(Target, TP, myFloor->Name, 0);
 }
@@ -131,7 +127,7 @@ void tmplput_FLOOR_NAME(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
 {
-       Floor *myFloor = (Floor *)CTX;
+       Floor *myFloor = (Floor *)CTX(CTX_FLOORS);
 
        StrBufAppendPrintf(Target, "%d", myFloor->NRooms);
 }
@@ -139,7 +135,7 @@ void tmplput_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        const Floor *pFloor = Folder->Floor;
 
        if (pFloor == NULL)
@@ -150,7 +146,7 @@ void tmplput_ROOM_FLOOR_NROOMS(StrBuf *Target, WCTemplputParams *TP)
 
 int ConditionalFloorHaveNRooms(StrBuf *Target, WCTemplputParams *TP)
 {
-       Floor *MyFloor = (Floor *)CTX;
+       Floor *MyFloor = (Floor *)CTX(CTX_FLOORS);
        int HaveN;
 
        HaveN = GetTemplateTokenNumber(Target, TP, 0, 0);
@@ -162,7 +158,7 @@ int ConditionalFloorHaveNRooms(StrBuf *Target, WCTemplputParams *TP)
 int ConditionalFloorIsRESTSubFloor(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession  *WCC = WC;
-       Floor *MyFloor = (Floor *)CTX;
+       Floor *MyFloor = (Floor *)CTX(CTX_FLOORS);
        /** if we have dav_depth the client just wants the subfloors */
        if ((WCC->Hdr->HR.dav_depth == 1) && 
            (GetCount(WCC->Directory) == 0))
@@ -174,7 +170,7 @@ int ConditionalFloorIsRESTSubFloor(StrBuf *Target, WCTemplputParams *TP)
 int ConditionalFloorIsSUBROOM(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession  *WCC = WC;
-       Floor *MyFloor = (Floor *)CTX;
+       Floor *MyFloor = (Floor *)CTX(CTX_FLOORS);
 
        return WCC->CurRoom.floorid == MyFloor->ID;
 }
@@ -182,7 +178,7 @@ int ConditionalFloorIsSUBROOM(StrBuf *Target, WCTemplputParams *TP)
 
 int ConditionalFloorIsVirtual(StrBuf *Target, WCTemplputParams *TP)
 {
-       Floor *MyFloor = (Floor *)CTX;
+       Floor *MyFloor = (Floor *)CTX(CTX_FLOORS);
 
        return MyFloor->ID == VIRTUAL_MY_FLOOR;
 }
@@ -193,14 +189,7 @@ int ConditionalFloorIsVirtual(StrBuf *Target, WCTemplputParams *TP)
  *******************************************************************************/
 /**** Name ******/
 
-
-void tmplput_RoomName(StrBuf *Target, WCTemplputParams *TP)
-{
-       StrBufAppendTemplate(Target, TP, WC->CurRoom.name, 0);
-}
-
-
-void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
+void tmplput_ThisRoom(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession *WCC = WC;
 
@@ -215,23 +204,15 @@ void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_NAME(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
-
-       if (Folder == NULL)
-       {
-               wcsession *WCC = WC;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
 
-               if (WCC == NULL)
-                       return;
-               Folder = &WCC->CurRoom;
-       }
        StrBufAppendTemplate(Target, TP, Folder->name, 0);
 }
 
 
 void tmplput_ROOM_BASENAME(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *room = (folder *)CTX;
+       folder *room = (folder *)CTX(CTX_ROOMS);
 
        if (room->nRoomNameParts > 1)
                StrBufAppendTemplate(Target, TP, 
@@ -243,7 +224,7 @@ void tmplput_ROOM_BASENAME(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_LEVEL_N_TIMES(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *room = (folder *)CTX;
+       folder *room = (folder *)CTX(CTX_ROOMS);
        int i;
         const char *AppendMe;
         long AppendMeLen;
@@ -260,12 +241,21 @@ void tmplput_ROOM_LEVEL_N_TIMES(StrBuf *Target, WCTemplputParams *TP)
 
 int ConditionalRoomIsInbox(StrBuf *Target, WCTemplputParams *TP)
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        return Folder->is_inbox;
 }
 
 
 /****** Properties ******/
+int ConditionalRoom_MayEdit(StrBuf *Target, WCTemplputParams *TP)
+{
+       wcsession *WCC = WC;
+
+       LoadRoomXA ();
+
+       return WCC->CurRoom.XALoaded == 1;
+}
+
 int ConditionalThisRoomHas_QRFlag(StrBuf *Target, WCTemplputParams *TP)
 {
        long QR_CheckFlag;
@@ -307,7 +297,7 @@ int ConditionalRoomHas_QRFlag(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_QRFLAGS(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        StrBufAppendPrintf(Target, "%d", Folder->QRFlags);
 }
 
@@ -363,7 +353,7 @@ int ConditionalRoomHas_UAFlag(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_ACL(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
 
        StrBufAppendPrintf(Target, "%ld", Folder->RAFlags, 0);
 }
@@ -471,7 +461,7 @@ int ConditionalThisRoomOrder(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_LISTORDER(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        StrBufAppendPrintf(Target, "%d", Folder->Order);
 }
 
@@ -528,7 +518,7 @@ void tmplput_ThisRoomInfoText(StrBuf *Target, WCTemplputParams *TP)
 
 void tmplput_ROOM_LASTCHANGE(StrBuf *Target, WCTemplputParams *TP) 
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        StrBufAppendPrintf(Target, "%d", Folder->lastchange);
 }
 
@@ -568,7 +558,7 @@ void tmplput_ThisRoomX_FileString(StrBuf *Target, WCTemplputParams *TP)
 
 int ConditionalIsThisThatRoom(StrBuf *Target, WCTemplputParams *TP)
 {
-       folder *Folder = (folder *)CTX;
+       folder *Folder = (folder *)CTX(CTX_ROOMS);
        wcsession *WCC = WC;
 
        if (WCC == NULL)
@@ -582,6 +572,10 @@ void
 InitModule_ROOMTOKENS
 (void)
 {
+       /* we duplicate this, just to be shure its already done. */
+       RegisterCTX(CTX_ROOMS);
+       RegisterCTX(CTX_FLOORS);
+
        RegisterNamespace("ROOMBANNER", 0, 1, tmplput_roombanner, NULL, CTX_NONE);
 
        RegisterNamespace("FLOOR:ID", 0, 0, tmplput_FLOOR_ID, NULL, CTX_FLOORS);
@@ -602,10 +596,9 @@ InitModule_ROOMTOKENS
 
        /**** Room... ******/
         /**** Name ******/
-       RegisterNamespace("ROOMNAME", 0, 1, tmplput_RoomName, NULL, CTX_NONE);
-       RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, NULL, CTX_NONE);
+       RegisterNamespace("THISROOM:NAME", 0, 1, tmplput_ThisRoom, NULL, CTX_NONE);
+
        RegisterNamespace("ROOM:INFO:NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_ROOMS);
-       RegisterNamespace("ROOM:INFO:PRINT_NAME", 0, 1, tmplput_ROOM_NAME, NULL, CTX_NONE);
        RegisterNamespace("ROOM:INFO:BASENAME", 0, 1, tmplput_ROOM_BASENAME, NULL, CTX_ROOMS);
        RegisterNamespace("ROOM:INFO:LEVELNTIMES", 1, 2, tmplput_ROOM_LEVEL_N_TIMES, NULL, CTX_ROOMS);
        RegisterConditional(HKEY("COND:ROOM:INFO:IS_INBOX"), 0, ConditionalRoomIsInbox, CTX_ROOMS);
@@ -613,6 +606,7 @@ InitModule_ROOMTOKENS
        /****** Properties ******/
        RegisterNamespace("ROOM:INFO:QRFLAGS", 0, 1, tmplput_ROOM_QRFLAGS, NULL, CTX_ROOMS);
        RegisterConditional(HKEY("COND:THISROOM:FLAG:QR"), 0, ConditionalThisRoomHas_QRFlag, CTX_NONE);
+       RegisterConditional(HKEY("COND:THISROOM:EDIT"), 0, ConditionalRoom_MayEdit, CTX_NONE);
        RegisterConditional(HKEY("COND:ROOM:FLAG:QR"), 0, ConditionalRoomHas_QRFlag, CTX_ROOMS);
 
        RegisterConditional(HKEY("COND:THISROOM:FLAG:QR2"), 0, ConditionalThisRoomHas_QRFlag2, CTX_NONE);