Mailing list header changes (fuck you Google)
[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 != NULL) &&
221                (WCC->serv_info->serv_supports_guest))
222                       ) )
223          {
224                 DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
225         }
226 }
227
228
229 void 
230 ServerShutdownModule_ICONBAR
231 (void)
232 {
233         DeleteHash(&IBDfl);
234 }
235
236
237
238 void
239 ServerStartModule_ICONBAR
240 (void)
241 {
242         int i = 1;
243         IBDfl = NewHash(1, NULL);
244
245         while (IconbarDefaults[i].len != 0)
246         {
247                 Put(IBDfl, 
248                     IconbarDefaults[i].Key, 
249                     IconbarDefaults[i].len, 
250                     &IconbarDefaults[i], 
251                     reference_free_handler);
252                 i++;
253         }
254 }
255
256
257 int ConditionalWholistExpanded(StrBuf *Target, WCTemplputParams *TP)
258 {
259         int r = 0;
260         if (WC) r = WC->ib_wholist_expanded;
261         syslog(LOG_DEBUG, "ConditionalWholistExpanded() returns %d", r);
262         return(r);
263 }
264
265
266 int ConditionalRoomlistExpanded(StrBuf *Target, WCTemplputParams *TP)
267 {
268         if (WC) return(WC->ib_roomlist_expanded);
269         return(0);
270 }
271
272
273
274 /*
275  * Toggle the roomlist expanded state in session memory
276  */
277 void toggle_roomlist_expanded_state(void) {
278         wcsession *WCC = WC;
279
280         if (!WCC) {
281                 wc_printf("no session");
282                 return;
283         }
284
285         WCC->ib_roomlist_expanded = IBSTR("wstate");
286         wc_printf("%d", WCC->ib_roomlist_expanded);
287         syslog(LOG_DEBUG, "ib_roomlist_expanded set to %d", WCC->ib_roomlist_expanded);
288 }
289
290
291 /*
292  * Toggle the wholist expanded state in session memory
293  */
294 void toggle_wholist_expanded_state(void) {
295         wcsession *WCC = WC;
296
297         if (!WCC) {
298                 wc_printf("no session");
299                 return;
300         }
301
302         WCC->ib_wholist_expanded = IBSTR("wstate");
303         wc_printf("%d", WCC->ib_wholist_expanded);
304         syslog(LOG_DEBUG, "ib_wholist_expanded set to %d", WCC->ib_wholist_expanded);
305 }
306
307
308 void 
309 InitModule_ICONBAR
310 (void)
311 {
312         long l;
313
314         /*WebcitAddUrlHandler(HKEY("user_iconbar"), "", 0, doUserIconStylesheet, 0); */
315         WebcitAddUrlHandler(HKEY("commit_iconbar"), "", 0, commit_iconbar, 0);
316         WebcitAddUrlHandler(HKEY("toggle_wholist_expanded_state"), "", 0, toggle_wholist_expanded_state, AJAX);
317         WebcitAddUrlHandler(HKEY("toggle_roomlist_expanded_state"), "", 0, toggle_roomlist_expanded_state, AJAX);
318         RegisterConditional("COND:ICONBAR:ACTIVE", 3, ConditionalIsActiveStylesheet, CTX_NONE);
319         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, NULL, CTX_NONE);
320         RegisterConditional("COND:ICONBAR:WHOLISTEXPANDED", 0, ConditionalWholistExpanded, CTX_NONE);
321         RegisterConditional("COND:ICONBAR:ROOMLISTEXPANDED", 0, ConditionalRoomlistExpanded, CTX_NONE);
322
323         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
324         l = 1;
325         while (IconbarDefaults[l].len != 0)
326         {
327                 RegisterTokenParamDefine(IconbarDefaults[l].Key, 
328                                          IconbarDefaults[l].len, l);
329                 l ++;
330         }
331         nIBV = l;
332 }
333
334
335
336 void 
337 SessionDestroyModule_ICONBAR
338 (wcsession *sess)
339 {
340         if (sess->IBSettingsVec != NULL)
341                 free(sess->IBSettingsVec);
342 }
343