a9d121a027a642e7b82550ddc42d475ea57afd3f
[citadel.git] / webcit / iconbar.c
1 /*
2  * $Id$
3  *
4  * Displays and customizes the iconbar.
5  */
6
7 #include "webcit.h"
8
9 /* Values for ib_displayas ... don't change these or you will break the templates */
10 #define IB_PICTEXT      0       /* picture and text */
11 #define IB_PICONLY      1       /* just a picture */
12 #define IB_TEXTONLY     2       /* just text */
13
14 void DontDeleteThis(void *Data){}
15
16 #define IconbarIsEnabled(a, b) IconbarIsENABLED(a, sizeof(a) - 1, b)
17
18
19 HashList *IB_Seeting_Order = NULL;
20 typedef struct _dflt_IB_Setting {
21         int         DefVal;  /* default value for non-set users */
22         long        n;       /* counter for internal purposes   */
23         const char *Key;     /* Stringvalue */
24         long        len;     /* Length... */
25 }dflt_IB_Setting;
26
27 long nIBV = 0;
28 dflt_IB_Setting IconbarDefaults[] = {
29         {0,  0, HKEY("unused")},
30         {0,  1, HKEY("ib_displayas")},
31         {0,  2, HKEY("ib_logo")},
32         {1,  3, HKEY("ib_summary")},
33         {1,  4, HKEY("ib_inbox")},
34         {1,  5, HKEY("ib_calendar")},
35         {1,  6, HKEY("ib_contacts")},
36         {1,  7, HKEY("ib_notes")},
37         {1,  8, HKEY("ib_tasks")},
38         {1,  9, HKEY("ib_rooms")},
39         {1, 10, HKEY("ib_users")},
40         {1, 11, HKEY("ib_chat")},
41         {1, 12, HKEY("ib_advanced")},
42         {1, 13, HKEY("ib_logoff")},
43         {1, 14, HKEY("ib_citadel")},
44         {0, 15, HKEY("")}
45 };
46
47 HashList *IBDfl = NULL;
48
49
50 long IconbarIsENABLED(long val, const char *key, size_t keylen)
51 {
52         void *vIBDfl = NULL;
53         wcsession *WCC = WC;
54
55         if ((WCC != NULL) && 
56             (WCC->IBSettingsVec != NULL) && 
57             (val < nIBV))
58         {
59                 return WCC->IBSettingsVec[val];
60         }
61         if (GetHash(IBDfl, key, keylen, &vIBDfl)) {
62                 dflt_IB_Setting *Set = (dflt_IB_Setting*)vIBDfl;
63                 return Set->DefVal;
64         }
65         else 
66                 return 1;
67 }
68
69 #ifdef DBG_ICONBAR_HASH
70 static char nbuf[32];
71 inline const char *PrintInt(void *Prefstr)
72 {
73         snprintf(nbuf, sizeof(nbuf), "%ld", (long)Prefstr);
74         return nbuf;
75 }
76 #endif
77
78 /* 
79         hprintf("Cache-Control: private\r\n");
80 */
81
82
83 int ConditionalIsActiveStylesheet(StrBuf *Target, WCTemplputParams *TP) {
84         long testFor;
85         long lookAt;
86         long ib_displayas;
87
88         lookAt = GetTemplateTokenNumber(Target, TP, 3, IB_PICTEXT);
89         testFor = GetTemplateTokenNumber(Target, TP, 2, IB_PICTEXT);
90
91
92
93         ib_displayas = IconbarIsENABLED(lookAt, TKEY(3));
94 /*
95         printf ("%ld == %ld ? %s : %s\n", 
96                 testFor, 
97                 ib_displayas, 
98                 IconbarDefaults[lookAt ].Key, 
99                 ChrPtr(TP->Tokens->FlatToken));
100 */
101
102         return (testFor == ib_displayas);
103 }
104
105 void LoadIconSettings(StrBuf *iconbar, long lvalue)
106 {
107         void *vIBDfl;
108         dflt_IB_Setting *Set;
109         const char *pCh = NULL;
110
111         wcsession *WCC = WC;
112         StrBuf *buf;
113         StrBuf *key;
114         long val;
115
116         buf = NewStrBuf();
117         key = NewStrBuf();
118         if (WCC->IBSettingsVec == NULL)
119         {
120                 WCC->IBSettingsVec = (long*) malloc (nIBV * sizeof(long));
121         }
122         /**
123          * The initialized values of these variables also happen to
124          * specify the default values for users who haven't customized
125          * their iconbars.  These should probably be set in a master
126          * configuration somewhere.
127          */
128
129         while (StrBufExtract_NextToken(buf, iconbar, &pCh,  ',') >= 0)
130         {
131                 StrBufExtract_token(key, buf, 0, '=');
132                 val = StrBufExtract_long(buf, 1, '=');
133
134                 if (!GetHash(IBDfl, SKEY(key), &vIBDfl)) 
135                         continue;
136                 Set = (dflt_IB_Setting*)vIBDfl;
137
138                 WCC->IBSettingsVec[Set->n] = val;
139 /*              printf("%ld %s %s -> %ld \n", Set->n, Set->Key, IconbarDefaults[Set->n].Key, val);*/
140         }
141 #ifdef DBG_ICONBAR_HASH
142         dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
143 #endif
144
145         FreeStrBuf(&key);
146         FreeStrBuf(&buf);
147 }
148
149
150 /*
151  * save changes to iconbar settings
152  */
153 void commit_iconbar(void) {
154         const StrBuf *MimeType;
155         StrBuf *iconbar;
156         StrBuf *buf;
157         int i;
158
159
160         if (!havebstr("ok_button")) {
161                 display_main_menu();
162                 return;
163         }
164
165         iconbar = NewStrBuf();
166         buf = NewStrBuf();
167         StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
168         for (i=0; i<(sizeof(IconbarDefaults)/sizeof(dflt_IB_Setting )); ++i) {
169                 char *Val;
170                 if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
171                                      IconbarDefaults[i].len),
172                                 "yes")) 
173                 {
174                         Val = "1";
175                 }
176                 else if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
177                                           IconbarDefaults[i].len),
178                                      "yeslist")) 
179                 {
180                         Val = "2";
181                 }
182                 else {
183                         Val = "0";
184                 }
185                 StrBufPrintf(buf, ",%s=%s", IconbarDefaults[i].Key, Val);
186                 StrBufAppendBuf(iconbar, buf, 0);
187
188         }
189         FreeStrBuf(&buf);
190         set_preference("iconbar", iconbar, 1);
191
192
193         begin_burst();
194         MimeType = DoTemplate(HKEY("iconbar_save"), NULL, &NoCtx);
195         http_transmit_thing(ChrPtr(MimeType), 0);
196 #ifdef DBG_ICONBAR_HASH
197         dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
198 #endif
199 }
200
201
202 void tmplput_iconbar(StrBuf *Target, WCTemplputParams *TP)
203 {
204         wcsession *WCC = WC;
205         
206         if ((WCC != NULL) && (WCC->logged_in)) {
207           DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
208         }
209 }
210
211
212 void 
213 ServerShutdownModule_ICONBAR
214 (void)
215 {
216         DeleteHash(&IBDfl);
217 }
218
219
220
221 void
222 ServerStartModule_ICONBAR
223 (void)
224 {
225         int i = 1;
226         IBDfl = NewHash(1, NULL);
227
228         while (IconbarDefaults[i].len != 0)
229         {
230                 Put(IBDfl, 
231                     IconbarDefaults[i].Key, 
232                     IconbarDefaults[i].len, 
233                     &IconbarDefaults[i], 
234                     reference_free_handler);
235                 i++;
236         }
237 }
238
239 void 
240 InitModule_ICONBAR
241 (void)
242 {
243         long l;
244
245         /*WebcitAddUrlHandler(HKEY("user_iconbar"), "", 0, doUserIconStylesheet, 0); */
246         WebcitAddUrlHandler(HKEY("commit_iconbar"), "", 0, commit_iconbar, 0);
247         RegisterConditional(HKEY("COND:ICONBAR:ACTIVE"), 3, ConditionalIsActiveStylesheet, CTX_NONE);
248         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, NULL, CTX_NONE);
249
250         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
251         l = 1;
252         while (IconbarDefaults[l].len != 0)
253         {
254                 RegisterTokenParamDefine(IconbarDefaults[l].Key, 
255                                          IconbarDefaults[l].len, l);
256                 l ++;
257         }
258         nIBV = l;
259 }
260
261
262
263 void 
264 SessionDestroyModule_ICONBAR
265 (wcsession *sess)
266 {
267         if (sess->IBSettingsVec != NULL)
268                 free(sess->IBSettingsVec);
269 }