]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* add first draft of group-change detection
[citadel.git] / webcit / subst.c
index 716a46e718077bbb15a6419d7d10262d593611f8..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, 
@@ -139,7 +158,7 @@ void VarPrintEntry(const char *Key, void *vSubst, int odd)
 /**
  * \brief Clear out the list of substitution variables local to this session
  */
-void clear_substs(struct wcsession *wc) {
+void clear_substs(wcsession *wc) {
 
        if (wc->vars != NULL) {
                DeleteHash(&wc->vars);
@@ -221,7 +240,7 @@ void deletevar(void *data)
 wcsubst *NewSubstVar(const char *keyname, int keylen, int type)
 {
        wcsubst* ptr;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        ptr = (wcsubst *) malloc(sizeof(wcsubst));
        memset(ptr, 0, sizeof(wcsubst));
@@ -259,7 +278,7 @@ void SVPRINTF(char *keyname, int keytype, const char *format,...)
        void *vPtr;
        wcsubst *ptr = NULL;
        size_t keylen;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        
        keylen = strlen(keyname);
        /**
@@ -295,7 +314,7 @@ void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
        va_list arg_ptr;
        void *vPtr;
        wcsubst *ptr = NULL;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
                
        /**
         * First look if we're doing a replacement of
@@ -329,7 +348,7 @@ void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
 {
        void *vPtr;
        wcsubst *ptr = NULL;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        
        /**
@@ -360,7 +379,7 @@ void SVPutLong(char *keyname, size_t keylen, long Data)
 {
        void *vPtr;
        wcsubst *ptr = NULL;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        
        /**
@@ -389,7 +408,7 @@ void SVCallback(char *keyname, size_t keylen, WCHandlerFunc fcn_ptr)
 {
        wcsubst *ptr;
        void *vPtr;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        /**
         * First look if we're doing a replacement of
@@ -419,7 +438,7 @@ void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref)
 {
        wcsubst *ptr;
        void *vPtr;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
 
        /**
         * First look if we're doing a replacement of
@@ -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 */
        }
 }
 
@@ -518,18 +533,18 @@ void GetTemplateTokenString(WCTemplateToken *Tokens,
  * \param keyname get a key to print
  */
 void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int ContextType) {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        wcsubst *ptr;
        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;
@@ -568,7 +583,7 @@ void print_value_of(StrBuf *Target, WCTemplateToken *Tokens, void *Context, int
 
 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup)
 {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        wcsubst *ptr;
        void *vVar;
 
@@ -606,7 +621,7 @@ int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLoo
 
 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup)
 {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        wcsubst *ptr;
        void *vVar;
 
@@ -647,7 +662,7 @@ void StrBufAppendTemplate(StrBuf *Target,
                          void *Context, int ContextType,
                          const StrBuf *Source, int FormatTypeIndex)
 {
-        struct wcsession *WCC;
+        wcsession *WCC;
        StrBuf *Buf;
        char EscapeAs = ' ';
 
@@ -936,28 +951,28 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
        case SV_CUST_STR_CONDITIONAL:
        case SV_CONDITIONAL:
        case SV_NEG_CONDITIONAL:
-               if (NewToken->Params[1]->lvalue == 0) {
+               if (NewToken->nParameters <2) {
                        lprintf(1, "Conditional (in '%s' line %ld); "
-                               "Conditional ID mustn't be 0! [%s]\n", 
+                               "require at least 2 parameters, you gave %ld params [%s]\n", 
                                ChrPtr(pTmpl->FileName),
                                NewToken->Line,
+                               NewToken->nParameters, 
                                ChrPtr(NewToken->FlatToken));
                        NewToken->Flags = 0;
                        break;
                }
-               if (NewToken->nParameters <2) {
+               if (NewToken->Params[1]->lvalue == 0) {
                        lprintf(1, "Conditional (in '%s' line %ld); "
-                               "require at least 2 parameters, you gave %ld params [%s]\n", 
+                               "Conditional ID mustn't be 0! [%s]\n", 
                                ChrPtr(pTmpl->FileName),
                                NewToken->Line,
-                               NewToken->nParameters, 
                                ChrPtr(NewToken->FlatToken));
                        NewToken->Flags = 0;
                        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) &&
@@ -1070,7 +1085,7 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
                pts = pch;
 
                /** Found one? parse it. */
-               for (; pch < pE - 1; pch ++) {
+               for (; pch <= pE - 1; pch ++) {
                        if (*pch == '"')
                                InDoubleQuotes = ! InDoubleQuotes;
                        else if (*pch == '\'')
@@ -1081,7 +1096,7 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
                                break;
                        }
                }
-               if (pch + 1 >= pE)
+               if (pch + 1 > pE)
                        continue;
                pte = pch;
                PutNewToken(NewTemplate, 
@@ -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)
 {