]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* add first draft of group-change detection
[citadel.git] / webcit / subst.c
index 1de23b6ae5e67ffb899d09b0a8324e581727be96..e5adac22356bfa3108d74bbeda44ce8c55007ffd 100644 (file)
@@ -59,6 +59,25 @@ typedef struct _HashHandler {
 void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere);
 int EvaluateConditional(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, void *Context, int Neg, int state, int ContextType);
 
+typedef struct _SortStruct {
+       StrBuf *Name;
+       StrBuf *PrefPrepend;
+       CompareFunc Forward;
+       CompareFunc Reverse;
+       CompareFunc GroupChange;
+
+       long ContextType;
+}SortStruct;
+
+void DestroySortStruct(void *vSort)
+{
+       SortStruct *Sort = (SortStruct*) vSort;
+       FreeStrBuf(&Sort->Name);
+       FreeStrBuf(&Sort->PrefPrepend);
+       free (Sort);
+}
+
+
 void RegisterNS(const char *NSName, 
                long len, 
                int nMinArgs, 
@@ -485,29 +504,25 @@ void GetTemplateTokenString(WCTemplateToken *Tokens,
                *len = Tokens->Params[N]->len;
                break;
        case TYPE_BSTR:
-               Buf = (StrBuf*) SBstr(Tokens->Params[N]->Start, 
-                                     Tokens->Params[N]->len);
+               Buf = (StrBuf*) SBstr(TKEY(N));
                *Value = ChrPtr(Buf);
                *len = StrLength(Buf);
                break;
        case TYPE_PREFSTR:
-               get_PREFERENCE(
-                       Tokens->Params[N]->Start, 
-                       Tokens->Params[N]->len, 
-                       &Buf);
+               get_PREFERENCE(TKEY(N), &Buf);
                *Value = ChrPtr(Buf);
                *len = StrLength(Buf);
                break;
        case TYPE_LONG:
        case TYPE_PREFINT:
-               break; ///todo: string to text?
+               break; /* todo: string to text? */
        case TYPE_GETTEXT:
                *Value = _(Tokens->Params[N]->Start);
                *len = strlen(*Value);
                break;
        default:
                break;
-//todo log error
+/*/todo log error */
        }
 }
 
@@ -523,13 +538,13 @@ void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int
        void *vVar;
 
        /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
-       /// TODO: debricated!
+       /* TODO: depricated! */
        if (Tokens->pName[0] == '=') {
                DoTemplate(Tokens->pName+1, Tokens->NameEnd - 1, NULL, NULL, 0);
        }
 
-//////TODO: if param[1] == "U" -> urlescape
-/// X -> escputs
+/*/////TODO: if param[1] == "U" -> urlescape
+/// X -> escputs */
        /** Page-local variables */
        if ((WCC->vars!= NULL) && GetHash(WCC->vars, Tokens->pName, Tokens->NameEnd, &vVar)) {
                ptr = (wcsubst*) vVar;
@@ -956,8 +971,8 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                        break;
                }
                if (!GetHash(Conditionals, 
-                            NewToken->Params[0]->Start,
-                            NewToken->Params[0]->len,
+                            NewToken->Params[0]->Start, 
+                            NewToken->Params[0]->len, 
                             &vVar) || 
                    (vVar == NULL)) {
                        if ((NewToken->Params[0]->len == 1) &&
@@ -1094,7 +1109,6 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
 }
 
 
-///void PrintTemplate(const char *Key, void *vSubst, int odd)
 const char* PrintTemplate(void *vSubst)
 {
        WCTemplate *Tmpl = vSubst;
@@ -1193,8 +1207,9 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, vo
        long AppendMeLen;
        HashHandler *Handler;
        void *vVar;
-// much output, since pName is not terminated...
-//     lprintf(1,"Doing token: %s\n",Token->pName);
+/* much output, since pName is not terminated...
+       lprintf(1,"Doing token: %s\n",Token->pName);
+*/
 
        switch (Tokens->Flags) {
        case SV_GETTEXT:
@@ -1240,7 +1255,7 @@ int EvaluateToken(StrBuf *Target, WCTemplateToken *Tokens, WCTemplate *pTmpl, vo
                break;
        case SV_SUBTEMPL:
                if (Tokens->nParameters == 1)
-                       DoTemplate(Tokens->Params[0]->Start, Tokens->Params[0]->len, NULL, NULL, ContextType);
+                       DoTemplate(TKEY(0), NULL, NULL, ContextType);
                break;
        case SV_PREEVALUATED:
                Handler = (HashHandler*) Tokens->PreEval;
@@ -1435,8 +1450,10 @@ void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Contex
                StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>", 
                                   templatename, len, 
                                   (long)strlen(templatename));
-///            dbg_PrintHash(Static, PrintTemplate, NULL);
-//             PrintHash(Static, VarPrintTransition, PrintTemplate);
+#if 0
+               dbg_PrintHash(Static, PrintTemplate, NULL);
+               PrintHash(Static, VarPrintTransition, PrintTemplate);
+#endif
                return;
        }
        if (vTmpl == NULL) 
@@ -1452,6 +1469,7 @@ typedef struct _HashIterator {
        int AdditionalParams;
        int ContextType;
        int XPectContextType;
+       int Flags;
        RetrieveHashlistFunc GetHash;
        HashDestructorFunc Destructor;
        SubTemplFunc DoSubTemplate;
@@ -1464,7 +1482,8 @@ void RegisterITERATOR(const char *Name, long len,
                      SubTemplFunc DoSubTempl,
                      HashDestructorFunc Destructor,
                      int ContextType, 
-                     int XPectContextType)
+                     int XPectContextType, 
+                     int Flags)
 {
        HashIterator *It = (HashIterator*)malloc(sizeof(HashIterator));
        It->StaticList = StaticList;
@@ -1474,6 +1493,7 @@ void RegisterITERATOR(const char *Name, long len,
        It->Destructor = Destructor;
        It->ContextType = ContextType;
        It->XPectContextType = XPectContextType;
+       It->Flags = Flags;
        Put(Iterators, Name, len, It, NULL);
 }
 
@@ -1483,18 +1503,19 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        HashIterator *It;
        HashList *List;
        HashPos  *it;
+       SortStruct *SortBy;
+       void *vSortBy;
+       int DetectGroupChange = 0;
        int nMembersUsed;
        int nMembersCounted = 0;
        long len; 
        const char *Key;
        void *vContext;
+       void *vLastContext;
        StrBuf *SubBuf;
        int oddeven = 0;
        
-       if (!GetHash(Iterators, 
-                    Tokens->Params[0]->Start,
-                    Tokens->Params[0]->len,
-                    &vIt)) {
+       if (!GetHash(Iterators, TKEY(0), &vIt)) {
                lprintf(1, "unknown Iterator [%s] (in '%s' line %ld); "
                        " [%s]\n", 
                        Tokens->Params[0]->Start,
@@ -1563,10 +1584,35 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
        else
                List = It->StaticList;
 
+       DetectGroupChange = (It->Flags & IT_FLAG_DETECT_GROUPCHANGE) != 0;
+       if (DetectGroupChange) {
+               const StrBuf *BSort;
+               DetectGroupChange = 0;
+               if (havebstr("SortBy")) {
+                       BSort = sbstr("SortBy");
+                       if (GetHash(SortHash, SKEY(BSort), &vSortBy) &&
+                           (vSortBy != NULL)) {
+                               SortBy = (SortStruct*)vSortBy;
+                               /** Ok, its us, lets see in which direction we should sort... */
+                               if (havebstr("SortOrder")) {
+                                       int SortOrder;
+                                       SortOrder = LBSTR("SortOrder");
+                                       if (SortOrder != 0)
+                                               DetectGroupChange = 1;
+                               }
+                       }
+               }
+       }
        nMembersUsed = GetCount(List);
        SubBuf = NewStrBuf();
        it = GetNewHashPos(List, 0);
        while (GetNextHashPos(List, it, &len, &Key, &vContext)) {
+               if (DetectGroupChange && nMembersCounted > 0) {
+                       if (SortBy->GroupChange(vContext, vLastContext))
+                               svputlong("ITERATE:ISGROUPCHANGE", 1);                  
+                       else
+                               svputlong("ITERATE:ISGROUPCHANGE", 0);
+               }
                svprintf(HKEY("ITERATE:ODDEVEN"), WCS_STRING, "%s", 
                         (oddeven) ? "odd" : "even");
                svprintf(HKEY("ITERATE:KEY"), WCS_STRING, "%s", Key);
@@ -1575,14 +1621,12 @@ void tmpl_iterate_subtmpl(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
 
                if (It->DoSubTemplate != NULL)
                        It->DoSubTemplate(SubBuf, vContext, Tokens);
-               DoTemplate(Tokens->Params[1]->Start,
-                          Tokens->Params[1]->len,
-                          SubBuf, vContext,
-                          It->ContextType);
+               DoTemplate(TKEY(1), SubBuf, vContext, It->ContextType);
                        
                StrBufAppendBuf(Target, SubBuf, 0);
                FlushStrBuf(SubBuf);
                oddeven = ~ oddeven;
+               vLastContext = vContext;
        }
        FreeStrBuf(&SubBuf);
        DeleteHashPos(&it);
@@ -1640,10 +1684,7 @@ int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
        void *vsubst;
        wcsubst *subst;
        
-       if (!GetHash(WC->vars, 
-                    Tokens->Params[2]->Start,
-                    Tokens->Params[2]->len,
-                    &vsubst))
+       if (!GetHash(WC->vars, TKEY(2), &vsubst))
                return 0;
        subst = (wcsubst*) vsubst;
        if ((subst->ContextRequired != CTX_NONE) &&
@@ -1656,7 +1697,7 @@ int ConditionalVar(WCTemplateToken *Tokens, void *Context, int ContextType)
        case WCS_FUNCTION:
                return (subst->wcs_function!=NULL);
        case WCS_SERVCMD:
-               lprintf(1, "  -> Server [%s]\n", subst->wcs_value);////todo
+               lprintf(1, "  -> Server [%s]\n", subst->wcs_value);/* TODO */
                return 1;
        case WCS_STRING:
        case WCS_STRBUF:
@@ -1716,11 +1757,7 @@ void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Con
        if (nArgs == 2) {
                if (Tokens->Params[1]->Type == TYPE_STR) {
                        Headline = NewStrBuf();
-                       DoTemplate(Tokens->Params[1]->Start, 
-                                  Tokens->Params[1]->len,
-                                  Headline, 
-                                  Context, 
-                                  ContextType);
+                       DoTemplate(TKEY(1), Headline, Context, ContextType);
                }
                else {
                        const char *Ch;
@@ -1734,11 +1771,7 @@ void tmpl_do_boxed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Con
        }
        
        DoTemplate(HKEY("beginbox"), Target, Headline, CTX_STRBUF);
-       DoTemplate(Tokens->Params[0]->Start, 
-                  Tokens->Params[0]->len,
-                  Target, 
-                  Context, 
-                  ContextType);
+       DoTemplate(TKEY(0), Target, Context, ContextType);
        DoTemplate(HKEY("endbox"), Target, Context, ContextType);
        FreeStrBuf(&Headline);
 }
@@ -1758,11 +1791,7 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
        for (i = 0; i < ntabs; i++) {
                TabNames[i] = NewStrBuf();
                if (Tokens->Params[i * 2]->len > 0) {
-                       DoTemplate(Tokens->Params[i * 2]->Start, 
-                                  Tokens->Params[i * 2]->len,
-                                  TabNames[i],
-                                  Context,
-                                  ContextType);
+                       DoTemplate(TKEY(i * 2), TabNames[i], Context, ContextType);
                }
                else { 
                        /** A Tab without subject? we can't count that, add it as silent */
@@ -1774,11 +1803,7 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
        for (i = 0; i < ntabs; i++) {
                StrBeginTab(Target, i, nTabs);
 
-               DoTemplate(Tokens->Params[i * 2 + 1]->Start, 
-                          Tokens->Params[i * 2 + 1]->len,
-                          Target,
-                          Context, 
-                          ContextType);
+               DoTemplate(TKEY(i * 2 + 1), Target, Context, ContextType);
                StrEndTab(Target, i, nTabs);
        }
 }
@@ -1788,27 +1813,12 @@ void tmpl_do_tabbed(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Co
  *                      Sorting-API
  */
 
-typedef struct _SortStruct {
-       StrBuf *Name;
-       StrBuf *PrefPrepend;
-       CompareFunc Forward;
-       CompareFunc Reverse;
-
-       long ContextType;
-}SortStruct;
-
-void DestroySortStruct(void *vSort)
-{
-       SortStruct *Sort = (SortStruct*) vSort;
-       FreeStrBuf(&Sort->Name);
-       FreeStrBuf(&Sort->PrefPrepend);
-       free (Sort);
-}
 
 void RegisterSortFunc(const char *name, long len, 
                      const char *prepend, long preplen,
                      CompareFunc Forward, 
                      CompareFunc Reverse, 
+                     CompareFunc GroupChange, 
                      long ContextType)
 {
        SortStruct *NewSort = (SortStruct*) malloc(sizeof(SortStruct));
@@ -1819,6 +1829,7 @@ void RegisterSortFunc(const char *name, long len,
                NewSort->PrefPrepend = NULL;
        NewSort->Forward = Forward;
        NewSort->Reverse = Reverse;
+       NewSort->GroupChange = GroupChange;
        NewSort->ContextType = ContextType;
        Put(SortHash, name, len, NewSort, DestroySortStruct);
 }
@@ -1830,7 +1841,7 @@ CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
        const StrBuf *BSort;
        SortStruct *SortBy;
        void *vSortBy;
-       long SortOrder;
+       long SortOrder = -1;
        
        if (havebstr("SortBy")) {
                BSort = sbstr("SortBy");
@@ -1840,7 +1851,15 @@ CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
                        BSort = get_room_pref("sort");
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /*TODO: nail prefprepend to sort, and lookup this! */
+               }
+               if (BSort != NULL)
+                       putbstr("SortBy", NewStrBufDup(BSort));
+               else {
+                       StrBuf *Buf;
+
+                       BSort = Buf = NewStrBufPlain(Default, ldefault);
+                       putbstr("SortBy", Buf);
                }
        }
 
@@ -1860,12 +1879,21 @@ CompareFunc RetrieveSort(long ContextType, const char *OtherPrefix,
                SortOrder = LBSTR("SortOrder");
        }
        else { /** Try to fallback to our remembered values... */
+               StrBuf *Buf;
                if (SortBy->PrefPrepend == NULL) {
-                       SortOrder = StrTol(get_room_pref("SortOrder"));
+                       Buf = get_room_pref("SortOrder");
+                       SortOrder = StrTol(Buf);
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /* TODO: nail prefprepend to sort, and lookup this! */
                }
+
+               if (Buf == NULL)
+                       SortOrder = DefaultDirection;
+
+               Buf = NewStrBufPlain(NULL, 64);
+               StrBufPrintf(Buf, "%ld", SortOrder);
+               putbstr("SortOrder", Buf);
        }
        switch (SortOrder) {
        default:
@@ -1907,7 +1935,7 @@ int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param
        
        *SortOrder = 0;
        *Next = NULL;
-       if (!GetHash(SortHash, Tokens->Params[0]->Start, Tokens->Params[0]->len, &vSort) || 
+       if (!GetHash(SortHash, TKEY(0), &vSort) || 
            (vSort == NULL))
                return eNO_SUCH_SORT;
        *Param = (SortStruct*) vSort;
@@ -1922,7 +1950,7 @@ int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param
                        BSort = get_room_pref("sort");
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /* TODO: nail prefprepend to sort, and lookup this! */
                }
        }
 
@@ -1941,7 +1969,7 @@ int GetSortMetric(WCTemplateToken *Tokens, SortStruct **Next, SortStruct **Param
                        *SortOrder = StrTol(get_room_pref("SortOrder"));
                }
                else {
-                       ////todo: nail prefprepend to sort, and lookup this!
+                       /* TODO: nail prefprepend to sort, and lookup this! */
                }
        }
        if (*SortOrder > 2)
@@ -2134,7 +2162,22 @@ void tmplput_long_vector(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, voi
        }
 }
 
+void dbg_print_longvector(long *LongVector)
+{
+       StrBuf *Buf = NewStrBufPlain(HKEY("Longvector: ["));
+       int nItems = LongVector[0];
+       int i;
 
+       for (i = 0; i < nItems; i++) {
+               if (i + 1 < nItems)
+                       StrBufAppendPrintf(Buf, "%d: %ld | ", i, LongVector[i]);
+               else
+                       StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
+
+       }
+       lprintf(1, ChrPtr(Buf));
+       FreeStrBuf(&Buf);
+}
 
 int ConditionalLongVector(WCTemplateToken *Tokens, void *Context, int ContextType)
 {