indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / iconbar.c
1
2 /*
3  * Displays and customizes the iconbar.
4  *
5  * Copyright (c) 1996-2012 by the citadel.org team
6  *
7  * This program is open source software.  You can redistribute it and/or
8  * modify it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17
18 /* Values for ib_displayas ... don't change these or you will break the templates */
19 #define IB_PICTEXT      0       /* picture and text */
20 #define IB_PICONLY      1       /* just a picture */
21 #define IB_TEXTONLY     2       /* just text */
22
23 void DontDeleteThis(void *Data) {
24 }
25
26 #define IconbarIsEnabled(a, b) IconbarIsENABLED(a, sizeof(a) - 1, b)
27
28
29 HashList *IB_Seeting_Order = NULL;
30 typedef struct _dflt_IB_Setting {
31         int DefVal;             /* default value for non-set users */
32         long n;                 /* counter for internal purposes   */
33         const char *Key;        /* Stringvalue */
34         long len;               /* Length... */
35 } dflt_IB_Setting;
36
37 long nIBV = 0;
38 dflt_IB_Setting IconbarDefaults[] = {
39         { 0, 0, HKEY("unused") },
40         { 0, 1, HKEY("ib_displayas") },
41         { 0, 2, HKEY("ib_logo") },
42         { 1, 3, HKEY("ib_summary") },
43         { 1, 4, HKEY("ib_inbox") },
44         { 1, 5, HKEY("ib_calendar") },
45         { 1, 6, HKEY("ib_contacts") },
46         { 1, 7, HKEY("ib_notes") },
47         { 1, 8, HKEY("ib_tasks") },
48         { 1, 9, HKEY("ib_rooms") },
49         { 1, 10, HKEY("ib_users") },
50         { 1, 11, HKEY("ib_chat") },
51         { 1, 12, HKEY("ib_advanced") },
52         { 1, 13, HKEY("ib_logoff") },
53         { 1, 14, HKEY("ib_citadel") },
54         { 0, 15, HKEY("") }
55 };
56
57 HashList *IBDfl = NULL;
58
59
60 long IconbarIsENABLED(long val, const char *key, size_t keylen) {
61         void *vIBDfl = NULL;
62         wcsession *WCC = WC;
63
64         if ((WCC != NULL) && (WCC->IBSettingsVec != NULL) && (val < nIBV)) {
65                 return WCC->IBSettingsVec[val];
66         }
67         if (GetHash(IBDfl, key, keylen, &vIBDfl)) {
68                 dflt_IB_Setting *Set = (dflt_IB_Setting *) vIBDfl;
69                 return Set->DefVal;
70         }
71         else
72                 return 1;
73 }
74
75 #ifdef DBG_ICONBAR_HASH
76 static char nbuf[32];
77 inline const char *PrintInt(void *Prefstr) {
78         snprintf(nbuf, sizeof(nbuf), "%ld", (long) Prefstr);
79         return nbuf;
80 }
81 #endif
82
83 /* 
84         hprintf("Cache-Control: private\r\n");
85 */
86
87
88 int ConditionalIsActiveStylesheet(StrBuf * Target, WCTemplputParams * TP) {
89         long testFor;
90         long lookAt;
91         long ib_displayas;
92
93         lookAt = GetTemplateTokenNumber(Target, TP, 3, IB_PICTEXT);
94         testFor = GetTemplateTokenNumber(Target, TP, 2, IB_PICTEXT);
95
96
97
98         ib_displayas = IconbarIsENABLED(lookAt, TKEY(3));
99
100 /*
101         printf ("%ld == %ld ? %s : %s\n", 
102                 testFor, 
103                 ib_displayas, 
104                 IconbarDefaults[lookAt ].Key, 
105                 ChrPtr(TP->Tokens->FlatToken));
106 */
107
108         return (testFor == ib_displayas);
109 }
110
111 void LoadIconSettings(StrBuf * iconbar, long lvalue) {
112         void *vIBDfl;
113         dflt_IB_Setting *Set;
114         const char *pCh = NULL;
115
116         wcsession *WCC = WC;
117         StrBuf *buf;
118         StrBuf *key;
119         long val;
120
121         buf = NewStrBuf();
122         key = NewStrBuf();
123         if (WCC->IBSettingsVec == NULL) {
124                 WCC->IBSettingsVec = (long *) malloc(nIBV * sizeof(long));
125         }
126         /*
127          * The initialized values of these variables also happen to
128          * specify the default values for users who haven't customized
129          * their iconbars.  These should probably be set in a master
130          * configuration somewhere.
131          */
132
133         while (StrBufExtract_NextToken(buf, iconbar, &pCh, ',') >= 0) {
134                 StrBufExtract_token(key, buf, 0, '=');
135                 val = StrBufExtract_long(buf, 1, '=');
136
137                 if (!GetHash(IBDfl, SKEY(key), &vIBDfl))
138                         continue;
139                 Set = (dflt_IB_Setting *) vIBDfl;
140
141                 WCC->IBSettingsVec[Set->n] = val;
142
143 /*              printf("%ld %s %s -> %ld \n", Set->n, Set->Key, IconbarDefaults[Set->n].Key, val);*/
144         }
145 #ifdef DBG_ICONBAR_HASH
146         dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
147 #endif
148
149         FreeStrBuf(&key);
150         FreeStrBuf(&buf);
151 }
152
153
154 /*
155  * save changes to iconbar settings
156  */
157 void commit_iconbar(void) {
158         const StrBuf *MimeType;
159         StrBuf *iconbar;
160         StrBuf *buf;
161         int i;
162
163
164         if (!havebstr("ok_button")) {
165                 display_main_menu();
166                 return;
167         }
168
169         iconbar = NewStrBuf();
170         buf = NewStrBuf();
171         StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
172         for (i = 0; i < (sizeof(IconbarDefaults) / sizeof(dflt_IB_Setting)); ++i) {
173                 char *Val;
174                 if (!strcasecmp(Bstr(IconbarDefaults[i].Key, IconbarDefaults[i].len), "yes")) {
175                         Val = "1";
176                 }
177                 else if (!strcasecmp(Bstr(IconbarDefaults[i].Key, IconbarDefaults[i].len), "yeslist")) {
178                         Val = "2";
179                 }
180                 else {
181                         Val = "0";
182                 }
183                 StrBufPrintf(buf, ",%s=%s", IconbarDefaults[i].Key, Val);
184                 StrBufAppendBuf(iconbar, buf, 0);
185
186         }
187         FreeStrBuf(&buf);
188         set_preference("iconbar", iconbar, 1);
189
190
191         begin_burst();
192         MimeType = DoTemplate(HKEY("iconbar_save"), NULL, &NoCtx);
193         http_transmit_thing(ChrPtr(MimeType), 0);
194 #ifdef DBG_ICONBAR_HASH
195         dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
196 #endif
197 }
198
199
200 /*
201  * Display the icon bar as long as we have an active session,
202  * and either the user is logged in or the server allows guest mode.
203  */
204 void tmplput_iconbar(StrBuf * Target, WCTemplputParams * TP) {
205         wcsession *WCC = WC;
206
207         if ((WCC != NULL) && ((WCC->logged_in) || ((WCC->serv_info != NULL) && (WCC->serv_info->serv_supports_guest))
208             )) {
209                 DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
210         }
211 }
212
213
214 void ServerShutdownModule_ICONBAR(void) {
215         DeleteHash(&IBDfl);
216 }
217
218
219
220 void ServerStartModule_ICONBAR(void) {
221         int i = 1;
222         IBDfl = NewHash(1, NULL);
223
224         while (IconbarDefaults[i].len != 0) {
225                 Put(IBDfl, IconbarDefaults[i].Key, IconbarDefaults[i].len, &IconbarDefaults[i], reference_free_handler);
226                 i++;
227         }
228 }
229
230
231 int ConditionalWholistExpanded(StrBuf * Target, WCTemplputParams * TP) {
232         int r = 0;
233         if (WC)
234                 r = WC->ib_wholist_expanded;
235         syslog(LOG_DEBUG, "ConditionalWholistExpanded() returns %d", r);
236         return (r);
237 }
238
239
240 int ConditionalRoomlistExpanded(StrBuf * Target, WCTemplputParams * TP) {
241         if (WC)
242                 return (WC->ib_roomlist_expanded);
243         return (0);
244 }
245
246
247
248 /*
249  * Toggle the roomlist expanded state in session memory
250  */
251 void toggle_roomlist_expanded_state(void) {
252         wcsession *WCC = WC;
253
254         if (!WCC) {
255                 wc_printf("no session");
256                 return;
257         }
258
259         WCC->ib_roomlist_expanded = IBSTR("wstate");
260         wc_printf("%d", WCC->ib_roomlist_expanded);
261         syslog(LOG_DEBUG, "ib_roomlist_expanded set to %d", WCC->ib_roomlist_expanded);
262 }
263
264
265 /*
266  * Toggle the wholist expanded state in session memory
267  */
268 void toggle_wholist_expanded_state(void) {
269         wcsession *WCC = WC;
270
271         if (!WCC) {
272                 wc_printf("no session");
273                 return;
274         }
275
276         WCC->ib_wholist_expanded = IBSTR("wstate");
277         wc_printf("%d", WCC->ib_wholist_expanded);
278         syslog(LOG_DEBUG, "ib_wholist_expanded set to %d", WCC->ib_wholist_expanded);
279 }
280
281
282 void InitModule_ICONBAR(void) {
283         long l;
284
285         /*WebcitAddUrlHandler(HKEY("user_iconbar"), "", 0, doUserIconStylesheet, 0); */
286         WebcitAddUrlHandler(HKEY("commit_iconbar"), "", 0, commit_iconbar, 0);
287         WebcitAddUrlHandler(HKEY("toggle_wholist_expanded_state"), "", 0, toggle_wholist_expanded_state, AJAX);
288         WebcitAddUrlHandler(HKEY("toggle_roomlist_expanded_state"), "", 0, toggle_roomlist_expanded_state, AJAX);
289         RegisterConditional("COND:ICONBAR:ACTIVE", 3, ConditionalIsActiveStylesheet, CTX_NONE);
290         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, NULL, CTX_NONE);
291         RegisterConditional("COND:ICONBAR:WHOLISTEXPANDED", 0, ConditionalWholistExpanded, CTX_NONE);
292         RegisterConditional("COND:ICONBAR:ROOMLISTEXPANDED", 0, ConditionalRoomlistExpanded, CTX_NONE);
293
294         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
295         l = 1;
296         while (IconbarDefaults[l].len != 0) {
297                 RegisterTokenParamDefine(IconbarDefaults[l].Key, IconbarDefaults[l].len, l);
298                 l++;
299         }
300         nIBV = l;
301 }
302
303
304
305 void SessionDestroyModule_ICONBAR(wcsession * sess) {
306         if (sess->IBSettingsVec != NULL)
307                 free(sess->IBSettingsVec);
308 }