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