SUBST: Dynamicaly generate contexts
[citadel.git] / webcit / addressbook_popup.c
1 /*
2  * AJAX-powered auto-completion
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 modify
7  * 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
18 /*
19  * Call this right before wDumpContent() on any page which requires the address book popup
20  */
21 void address_book_popup(void) {
22         /* Open a new div, hidden initially, for address book popups. */
23         wc_printf("</div>\n");  /* End of 'content' div */
24         wc_printf("<div id=\"address_book_popup\" style=\"display:none;\">");
25         wc_printf("<div id=\"address_book_popup_container_div\">");
26         wc_printf("<div id=\"address_book_popup_middle_div\"></div>");
27         wc_printf("<div id=\"address_book_inner_div\"></div>");
28         wc_printf("</div>");
29         /* The 'address_book_popup' div will be closed by wDumpContent() */
30 }
31
32 /*
33  * Address book popup window
34  */
35 void display_address_book_middle_div(void) {
36         char buf[256];
37         long len;
38         char *Name;
39         const char *VCName;
40         void *Namee;
41         StrBuf *DefAddrBook;
42         HashList *List;
43         HashPos  *it;
44
45         begin_ajax_response();
46
47         DefAddrBook = get_room_pref("defaddrbook");
48
49         wc_printf("<table border=0 width=100%%><tr valign=middle>");
50         wc_printf("<td align=left><img src=\"static/webcit_icons/essen/32x32/contact.png\"></td>");
51         wc_printf("<td align=center>");
52
53         wc_printf("<form>"
54                 "<select class=\"address_book_popup_title\" size=1 id=\"which_addr_book\" "
55                 " onChange=\"PopulateAddressBookInnerDiv($('which_addr_book').value,'%s')\">",
56                 bstr("target_input")
57         );
58
59         wc_printf("<option value=\"__LOCAL_USERS__\" %s>", 
60                 (strcmp(ChrPtr(DefAddrBook), "__LOCAL_USERS__") == 0)?
61                 "selected=\"selected\" ":"");
62         escputs(ChrPtr(WC->serv_info->serv_humannode));
63         wc_printf("</option>\n");
64
65         
66         List = NewHash(1, NULL);
67         serv_puts("LKRA");
68         serv_getln(buf, sizeof buf);
69         if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
70                 if (extract_int(buf, 6) == VIEW_ADDRESSBOOK) {
71                         Name = (char*) malloc(len + 1);
72                         len = extract_token(Name, buf, 0, '|', len);
73                         Put(List, Name, len, Name, NULL);
74                 }
75         }
76
77         SortByHashKey(List, 1);
78         it = GetNewHashPos(List, 0);
79         while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
80                 wc_printf("<option value=\"");
81                 urlescputs((char*)Namee);
82                 if (strcmp(ChrPtr(DefAddrBook), Namee) == 0)
83                         wc_printf("\" selected=\"selected\" >");
84                 else
85                         wc_printf("\">");
86                 escputs((char*)Namee);
87                 wc_printf("</option>\n");
88         }
89         DeleteHashPos(&it);
90         DeleteHash(&List);
91         wc_printf("</select></form>");
92
93         wc_printf("</td>");
94         wc_printf("<td align=right "
95                 "onclick=\"javascript:$('address_book_popup').style.display='none';\" "
96                 "><img src=\"static/webcit_icons/closewindow.gif\">");
97         wc_printf("</td></tr></table>");
98
99         wc_printf("<script type=\"text/javascript\">PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');</script>",
100                 bstr("target_input")
101         );
102
103         end_ajax_response();
104 }
105
106
107
108 /*
109  * Address book popup results
110  */
111 void display_address_book_inner_div() {
112         char buf[256];
113         int num_targets = 0;
114         char target_id[64];
115         char target_label[64];
116         long len;
117         char *Name;
118         const char *VCName;
119         void *Namee;
120         HashList *List;
121         HashPos  *it;
122         int i;
123         StrBuf *saved_roomname;
124
125         begin_ajax_response();
126
127         List = NewHash(1, NULL);
128         wc_printf("<div align=center><form onSubmit=\"return false;\">"
129                 "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
130
131         if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
132                 serv_puts("LIST");
133                 serv_getln(buf, sizeof buf);
134                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
135                         Name = (char*) malloc(len + 1);
136                         len = extract_token(Name, buf, 0, '|', len + 1);
137                         if((len > 5) && (strncmp(Name, "SYS_", 4) == 0)) {
138                                 free(Name);
139                                 continue;
140                         }
141                         Put(List, Name, len, Name, NULL);
142
143                 }
144                 SortByHashKey(List, 1);
145                 it = GetNewHashPos(List, 0);
146                 while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
147                         wc_printf("<option value=\"");
148                         escputs((char*)Namee);
149                         wc_printf("\">");
150                         escputs((char*)Namee);
151                         wc_printf("</option>\n");
152                 }
153                 DeleteHashPos(&it);
154                 DeleteHash(&List);
155         }
156
157         else {
158                 set_room_pref("defaddrbook",NewStrBufDup(sbstr("which_addr_book")), 0);
159                 saved_roomname = NewStrBufDup(WC->CurRoom.name);
160                 gotoroom(sbstr("which_addr_book"));
161                 serv_puts("DVCA");
162                 serv_getln(buf, sizeof buf);
163                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
164                         Name = (char*) malloc(len + 1);
165                         len = extract_token(Name, buf, 0, '|', len + 1);
166                         Put(List, Name, len, Name, NULL);
167
168                 }
169                 SortByHashKey(List, 1);
170                 it = GetNewHashPos(List, 0);
171                 while (GetNextHashPos(List, it, &len, &VCName, (void**)&Namee)) {
172                         wc_printf("<option value=\"");
173                         escputs((char*)Namee);
174                         wc_printf("\">");
175                         escputs((char*)Namee);
176                         wc_printf("</option>\n");
177                 }
178                 DeleteHashPos(&it);
179                 DeleteHash(&List);
180                 gotoroom(saved_roomname);
181                 FreeStrBuf(&saved_roomname);
182         }
183
184         wc_printf("</select>\n");
185
186         wc_printf("%s: ", _("Add"));
187
188         num_targets = num_tokens(bstr("target_input"), '|');
189         for (i=0; i<num_targets; i+=2) {
190                 extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
191                 extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
192                 wc_printf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
193                 wc_printf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
194         }
195
196         /* This 'close window' button works.  Omitting it because we already have a close button
197          * in the upper right corner, and this one takes up space.
198          *
199         wc_printf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
200         wc_printf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
201          */
202
203         wc_printf("</form></div>\n");
204
205         end_ajax_response();
206 }
207
208
209
210
211 void 
212 InitModule_ADDRBOOK_POPUP
213 (void)
214 {
215         WebcitAddUrlHandler(HKEY("display_address_book_middle_div"), "", 0, display_address_book_middle_div, 0);
216         WebcitAddUrlHandler(HKEY("display_address_book_inner_div"), "", 0, display_address_book_inner_div, 0);
217 }