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