Back to index

webcit  8.12-dfsg
iconbar.c
Go to the documentation of this file.
00001 /*
00002  * Displays and customizes the iconbar.
00003  *
00004  * Copyright (c) 1996-2012 by the citadel.org team
00005  *
00006  * This program is open source software.  You can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License, version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "webcit.h"
00016 
00017 /* Values for ib_displayas ... don't change these or you will break the templates */
00018 #define IB_PICTEXT   0      /* picture and text */
00019 #define IB_PICONLY   1      /* just a picture */
00020 #define IB_TEXTONLY  2      /* just text */
00021 
00022 void DontDeleteThis(void *Data){}
00023 
00024 #define IconbarIsEnabled(a, b) IconbarIsENABLED(a, sizeof(a) - 1, b)
00025 
00026 
00027 HashList *IB_Seeting_Order = NULL;
00028 typedef struct _dflt_IB_Setting {
00029        int         DefVal;  /* default value for non-set users */
00030        long        n;       /* counter for internal purposes   */
00031        const char *Key;     /* Stringvalue */
00032        long        len;     /* Length... */
00033 }dflt_IB_Setting;
00034 
00035 long nIBV = 0;
00036 dflt_IB_Setting IconbarDefaults[] = {
00037        {0,  0, HKEY("unused")},
00038        {0,  1, HKEY("ib_displayas")},
00039        {0,  2, HKEY("ib_logo")},
00040        {1,  3, HKEY("ib_summary")},
00041        {1,  4, HKEY("ib_inbox")},
00042        {1,  5, HKEY("ib_calendar")},
00043        {1,  6, HKEY("ib_contacts")},
00044        {1,  7, HKEY("ib_notes")},
00045        {1,  8, HKEY("ib_tasks")},
00046        {1,  9, HKEY("ib_rooms")},
00047        {1, 10, HKEY("ib_users")},
00048        {1, 11, HKEY("ib_chat")},
00049        {1, 12, HKEY("ib_advanced")},
00050        {1, 13, HKEY("ib_logoff")},
00051        {1, 14, HKEY("ib_citadel")},
00052        {0, 15, HKEY("")}
00053 };
00054 
00055 HashList *IBDfl = NULL;
00056 
00057 
00058 long IconbarIsENABLED(long val, const char *key, size_t keylen)
00059 {
00060        void *vIBDfl = NULL;
00061        wcsession *WCC = WC;
00062 
00063        if ((WCC != NULL) && 
00064            (WCC->IBSettingsVec != NULL) && 
00065            (val < nIBV))
00066        {
00067               return WCC->IBSettingsVec[val];
00068        }
00069        if (GetHash(IBDfl, key, keylen, &vIBDfl)) {
00070               dflt_IB_Setting *Set = (dflt_IB_Setting*)vIBDfl;
00071               return Set->DefVal;
00072        }
00073        else 
00074               return 1;
00075 }
00076 
00077 #ifdef DBG_ICONBAR_HASH
00078 static char nbuf[32];
00079 inline const char *PrintInt(void *Prefstr)
00080 {
00081        snprintf(nbuf, sizeof(nbuf), "%ld", (long)Prefstr);
00082        return nbuf;
00083 }
00084 #endif
00085 
00086 /* 
00087        hprintf("Cache-Control: private\r\n");
00088 */
00089 
00090 
00091 int ConditionalIsActiveStylesheet(StrBuf *Target, WCTemplputParams *TP) {
00092        long testFor;
00093        long lookAt;
00094        long ib_displayas;
00095 
00096        lookAt = GetTemplateTokenNumber(Target, TP, 3, IB_PICTEXT);
00097        testFor = GetTemplateTokenNumber(Target, TP, 2, IB_PICTEXT);
00098 
00099 
00100 
00101        ib_displayas = IconbarIsENABLED(lookAt, TKEY(3));
00102 /*
00103        printf ("%ld == %ld ? %s : %s\n", 
00104               testFor, 
00105               ib_displayas, 
00106               IconbarDefaults[lookAt ].Key, 
00107               ChrPtr(TP->Tokens->FlatToken));
00108 */
00109 
00110        return (testFor == ib_displayas);
00111 }
00112 
00113 void LoadIconSettings(StrBuf *iconbar, long lvalue)
00114 {
00115        void *vIBDfl;
00116        dflt_IB_Setting *Set;
00117        const char *pCh = NULL;
00118 
00119        wcsession *WCC = WC;
00120        StrBuf *buf;
00121        StrBuf *key;
00122        long val;
00123 
00124        buf = NewStrBuf();
00125        key = NewStrBuf();
00126        if (WCC->IBSettingsVec == NULL)
00127        {
00128               WCC->IBSettingsVec = (long*) malloc (nIBV * sizeof(long));
00129        }
00130        /*
00131         * The initialized values of these variables also happen to
00132         * specify the default values for users who haven't customized
00133         * their iconbars.  These should probably be set in a master
00134         * configuration somewhere.
00135         */
00136 
00137        while (StrBufExtract_NextToken(buf, iconbar, &pCh,  ',') >= 0)
00138        {
00139               StrBufExtract_token(key, buf, 0, '=');
00140               val = StrBufExtract_long(buf, 1, '=');
00141 
00142               if (!GetHash(IBDfl, SKEY(key), &vIBDfl)) 
00143                      continue;
00144               Set = (dflt_IB_Setting*)vIBDfl;
00145 
00146               WCC->IBSettingsVec[Set->n] = val;
00147 /*            printf("%ld %s %s -> %ld \n", Set->n, Set->Key, IconbarDefaults[Set->n].Key, val);*/
00148        }
00149 #ifdef DBG_ICONBAR_HASH
00150        dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
00151 #endif
00152 
00153        FreeStrBuf(&key);
00154        FreeStrBuf(&buf);
00155 }
00156 
00157 
00158 /*
00159  * save changes to iconbar settings
00160  */
00161 void commit_iconbar(void) {
00162        const StrBuf *MimeType;
00163        StrBuf *iconbar;
00164        StrBuf *buf;
00165        int i;
00166 
00167 
00168        if (!havebstr("ok_button")) {
00169               display_main_menu();
00170               return;
00171        }
00172 
00173        iconbar = NewStrBuf();
00174        buf = NewStrBuf();
00175        StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
00176        for (i=0; i<(sizeof(IconbarDefaults)/sizeof(dflt_IB_Setting )); ++i) {
00177               char *Val;
00178               if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
00179                                  IconbarDefaults[i].len),
00180                             "yes")) 
00181               {
00182                      Val = "1";
00183               }
00184               else if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
00185                                      IconbarDefaults[i].len),
00186                                  "yeslist")) 
00187               {
00188                      Val = "2";
00189               }
00190               else {
00191                      Val = "0";
00192               }
00193               StrBufPrintf(buf, ",%s=%s", IconbarDefaults[i].Key, Val);
00194               StrBufAppendBuf(iconbar, buf, 0);
00195 
00196        }
00197        FreeStrBuf(&buf);
00198        set_preference("iconbar", iconbar, 1);
00199 
00200 
00201        begin_burst();
00202        MimeType = DoTemplate(HKEY("iconbar_save"), NULL, &NoCtx);
00203        http_transmit_thing(ChrPtr(MimeType), 0);
00204 #ifdef DBG_ICONBAR_HASH
00205        dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
00206 #endif
00207 }
00208 
00209 
00210 /*
00211  * Display the icon bar as long as we have an active session,
00212  * and either the user is logged in or the server allows guest mode.
00213  */
00214 void tmplput_iconbar(StrBuf *Target, WCTemplputParams *TP)
00215 {
00216        wcsession *WCC = WC;
00217        
00218         if (  (WCC != NULL)
00219               && (   (WCC->logged_in)
00220                      || (WCC->serv_info->serv_supports_guest)
00221               )
00222        ) {
00223               DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
00224        }
00225 }
00226 
00227 
00228 void 
00229 ServerShutdownModule_ICONBAR
00230 (void)
00231 {
00232        DeleteHash(&IBDfl);
00233 }
00234 
00235 
00236 
00237 void
00238 ServerStartModule_ICONBAR
00239 (void)
00240 {
00241        int i = 1;
00242        IBDfl = NewHash(1, NULL);
00243 
00244        while (IconbarDefaults[i].len != 0)
00245        {
00246               Put(IBDfl, 
00247                   IconbarDefaults[i].Key, 
00248                   IconbarDefaults[i].len, 
00249                   &IconbarDefaults[i], 
00250                   reference_free_handler);
00251               i++;
00252        }
00253 }
00254 
00255 
00256 int ConditionalWholistExpanded(StrBuf *Target, WCTemplputParams *TP)
00257 {
00258        int r = 0;
00259        if (WC) r = WC->ib_wholist_expanded;
00260        syslog(LOG_DEBUG, "ConditionalWholistExpanded() returns %d", r);
00261        return(r);
00262 }
00263 
00264 
00265 int ConditionalRoomlistExpanded(StrBuf *Target, WCTemplputParams *TP)
00266 {
00267        if (WC) return(WC->ib_roomlist_expanded);
00268        return(0);
00269 }
00270 
00271 
00272 
00273 /*
00274  * Toggle the roomlist expanded state in session memory
00275  */
00276 void toggle_roomlist_expanded_state(void) {
00277        wcsession *WCC = WC;
00278 
00279        if (!WCC) {
00280               wc_printf("no session");
00281               return;
00282        }
00283 
00284        WCC->ib_roomlist_expanded = IBSTR("wstate");
00285        wc_printf("%d", WCC->ib_roomlist_expanded);
00286        syslog(LOG_DEBUG, "ib_roomlist_expanded set to %d", WCC->ib_roomlist_expanded);
00287 }
00288 
00289 
00290 /*
00291  * Toggle the wholist expanded state in session memory
00292  */
00293 void toggle_wholist_expanded_state(void) {
00294        wcsession *WCC = WC;
00295 
00296        if (!WCC) {
00297               wc_printf("no session");
00298               return;
00299        }
00300 
00301        WCC->ib_wholist_expanded = IBSTR("wstate");
00302        wc_printf("%d", WCC->ib_wholist_expanded);
00303        syslog(LOG_DEBUG, "ib_wholist_expanded set to %d", WCC->ib_wholist_expanded);
00304 }
00305 
00306 
00307 void 
00308 InitModule_ICONBAR
00309 (void)
00310 {
00311        long l;
00312 
00313        /*WebcitAddUrlHandler(HKEY("user_iconbar"), "", 0, doUserIconStylesheet, 0); */
00314        WebcitAddUrlHandler(HKEY("commit_iconbar"), "", 0, commit_iconbar, 0);
00315        WebcitAddUrlHandler(HKEY("toggle_wholist_expanded_state"), "", 0, toggle_wholist_expanded_state, AJAX);
00316        WebcitAddUrlHandler(HKEY("toggle_roomlist_expanded_state"), "", 0, toggle_roomlist_expanded_state, AJAX);
00317        RegisterConditional(HKEY("COND:ICONBAR:ACTIVE"), 3, ConditionalIsActiveStylesheet, CTX_NONE);
00318        RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, NULL, CTX_NONE);
00319        RegisterConditional(HKEY("COND:ICONBAR:WHOLISTEXPANDED"), 0, ConditionalWholistExpanded, CTX_NONE);
00320        RegisterConditional(HKEY("COND:ICONBAR:ROOMLISTEXPANDED"), 0, ConditionalRoomlistExpanded, CTX_NONE);
00321 
00322        RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
00323        l = 1;
00324        while (IconbarDefaults[l].len != 0)
00325        {
00326               RegisterTokenParamDefine(IconbarDefaults[l].Key, 
00327                                     IconbarDefaults[l].len, l);
00328               l ++;
00329        }
00330        nIBV = l;
00331 }
00332 
00333 
00334 
00335 void 
00336 SessionDestroyModule_ICONBAR
00337 (wcsession *sess)
00338 {
00339        if (sess->IBSettingsVec != NULL)
00340               free(sess->IBSettingsVec);
00341 }
00342