* templatize iconbar editing
[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 long IconbarIsENABLED(const char *key, size_t keylen, long defval)
19 {
20         void *Data = NULL;
21         wcsession *WCC = WC;
22
23         if (WCC == NULL) 
24                 return defval;
25
26         if (GetHash(WCC->IconBarSettings, 
27                     key, 
28                     keylen,
29                     &Data))
30                 return (long) Data;
31         else 
32                 return defval;
33 }
34
35 #ifdef DBG_ICONBAR_HASH
36 static char nbuf[32];
37 inline const char *PrintInt(void *Prefstr)
38 {
39         snprintf(nbuf, sizeof(nbuf), "%ld", (long)Prefstr);
40         return nbuf;
41 }
42 #endif
43
44 /* Produces a stylesheet which hides any iconbar icons the user does not want */
45 void doUserIconStylesheet(void) {
46         HashPos *pos;
47         void *Data;
48         long value;
49         const char *key;
50         long HKLen;
51         
52         output_custom_content_header("text/css");
53         hprintf("Cache-Control: private\r\n");
54         
55         begin_burst();
56         wprintf("#global { left: 16%%; }\r\n");
57         pos = GetNewHashPos(WC->IconBarSettings, 0);
58         while(GetNextHashPos(WC->IconBarSettings, pos, &HKLen, &key, &Data)) {
59                 value = (long) Data;
60                 if (value == 0 
61                     && strncasecmp("ib_displayas",key,12) 
62                     && strncasecmp("ib_logoff", key, 9)) {
63                         /* Don't shoot me for this */
64                         wprintf("#%s { display: none !important; }\r\n",key);
65                 } else if (!strncasecmp("ib_users",key, 8) && value == 2) {
66                         wprintf("#online_users { display: block; !important } \r\n");
67                 }
68         }
69         DeleteHashPos(&pos);
70         end_burst();
71 }
72
73 int ConditionalIsActiveStylesheet(StrBuf *Target, WCTemplputParams *TP) {
74         long testFor;
75         int ib_displayas;
76
77         testFor = GetTemplateTokenNumber(Target, TP, 3, IB_PICTEXT);
78         ib_displayas = IconbarIsENABLED(TKEY(2),0);
79         return (testFor == ib_displayas);
80 }
81
82 void LoadIconSettings(StrBuf *iconbar, long lvalue)
83 {
84         wcsession *WCC = WC;
85         StrBuf *buf;
86         StrBuf *key;
87         long val;
88         int i, nTokens;
89
90         buf = NewStrBuf();;
91         key = NewStrBuf();
92         if (WCC->IconBarSettings == NULL)
93                 WCC->IconBarSettings = NewHash(1, NULL);
94         /**
95          * The initialized values of these variables also happen to
96          * specify the default values for users who haven't customized
97          * their iconbars.  These should probably be set in a master
98          * configuration somewhere.
99          */
100
101         nTokens = StrBufNum_tokens(iconbar, ',');
102         for (i=0; i<nTokens; ++i) {
103                 StrBufExtract_token(buf, iconbar, i, ',');
104                 StrBufExtract_token(key, buf, 0, '=');
105                 val = StrBufExtract_long(buf, 1, '=');
106                 Put(WCC->IconBarSettings, 
107                     ChrPtr(key), StrLength(key),
108                     (void*)val, DontDeleteThis);
109         }
110
111 #ifdef DBG_ICONBAR_HASH
112         dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
113 #endif
114
115         FreeStrBuf(&key);
116         FreeStrBuf(&buf);
117 }
118
119
120 /*
121  * save changes to iconbar settings
122  */
123 void commit_iconbar(void) {
124         const StrBuf *MimeType;
125         StrBuf *iconbar;
126         StrBuf *buf;
127         int i;
128
129         char *boxen[] = {
130                 "ib_logo",
131                 "ib_summary",
132                 "ib_inbox",
133                 "ib_calendar",
134                 "ib_contacts",
135                 "ib_notes",
136                 "ib_tasks",
137                 "ib_rooms",
138                 "ib_users",
139                 "ib_chat",
140                 "ib_advanced",
141                 "ib_logoff",
142                 "ib_citadel"
143         };
144
145         if (!havebstr("ok_button")) {
146                 display_main_menu();
147                 return;
148         }
149
150         iconbar = NewStrBuf();
151         buf = NewStrBuf();
152         StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
153         for (i=0; i<(sizeof(boxen)/sizeof(char *)); ++i) {
154                 char *Val;
155                 if (!strcasecmp(BSTR(boxen[i]), "yes")) {
156                         Val = "1";
157                 }
158                 else if (!strcasecmp(BSTR(boxen[i]), "yeslist")) {
159                         Val = "2";
160                 }
161                 else {
162                         Val = "0";
163                 }
164                 StrBufPrintf(buf, ",%s=%s", boxen[i], Val);
165                 StrBufAppendBuf(iconbar, buf, 0);
166
167         }
168         FreeStrBuf(&buf);
169         set_preference("iconbar", iconbar, 1);
170
171
172         begin_burst();
173         MimeType = DoTemplate(HKEY("iconbar_save"), NULL, &NoCtx);
174         http_transmit_thing(ChrPtr(MimeType), 0);
175 #ifdef DBG_ICONBAR_HASH
176         dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
177 #endif
178 }
179
180
181 void tmplput_iconbar(StrBuf *Target, WCTemplputParams *TP)
182 {
183         wcsession *WCC = WC;
184         
185         if ((WCC != NULL) && (WCC->logged_in)) {
186           DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
187         }
188 }
189
190 void 
191 InitModule_ICONBAR
192 (void)
193 {
194         WebcitAddUrlHandler(HKEY("user_iconbar"), doUserIconStylesheet, 0);
195         WebcitAddUrlHandler(HKEY("commit_iconbar"), commit_iconbar, 0);
196         RegisterConditional(HKEY("COND:ICONBAR:ACTIVE"), 3, ConditionalIsActiveStylesheet, CTX_NONE);
197         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, 0);
198
199         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
200 }
201
202
203
204 void 
205 SessionDestroyModule_ICONBAR
206 (wcsession *sess)
207 {
208         DeleteHash(&sess->IconBarSettings);
209 }