* copy daves great handler script and modify it to fit the simpler needs of webcit.
[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         void *Namee;
32         HashList *List;
33         HashPos  *it;
34
35         begin_ajax_response();
36
37         wprintf("<table border=0 width=100%%><tr valign=middle>");
38         wprintf("<td align=left><img src=\"static/viewcontacts_32x.gif\"></td>");
39         wprintf("<td align=center>");
40
41         wprintf("<form>"
42                 "<select class=\"address_book_popup_title\" size=1 id=\"which_addr_book\" "
43                 " onChange=\"PopulateAddressBookInnerDiv($('which_addr_book').value,'%s')\">",
44                 bstr("target_input")
45         );
46
47         wprintf("<option value=\"__LOCAL_USERS__\">");
48         escputs(serv_info.serv_humannode);
49         wprintf("</option>\n");
50
51         
52         List = NewHash(1, NULL);
53         serv_puts("LKRA");
54         serv_getln(buf, sizeof buf);
55         if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
56                 if (extract_int(buf, 6) == VIEW_ADDRESSBOOK) {
57                         Name = (char*) malloc(len + 1);
58                         len = extract_token(Name, buf, 0, '|', len);
59                         Put(List, Name, len, Name, NULL);
60                 }
61         }
62
63         SortByHashKey(List, 1);
64         it = GetNewHashPos();
65         while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
66                 wprintf("<option value=\"");
67                 urlescputs((char*)Namee);
68                 wprintf("\">");
69                 escputs((char*)Namee);
70                 wprintf("</option>\n");
71         }
72         DeleteHashPos(&it);
73         DeleteHash(&List);
74         wprintf("</select></form>");
75
76         wprintf("</td>");
77         wprintf("<td align=right "
78                 "onclick=\"javascript:$('address_book_popup').style.display='none';\" "
79                 "><img src=\"static/closewindow.gif\">");
80         wprintf("</td></tr></table>");
81
82         wprintf("<script type=\"text/javascript\">"
83                 "PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');"
84                 "</script>\n",
85                 bstr("target_input")
86         );
87
88         end_ajax_response();
89 }
90
91
92
93 /*
94  * Address book popup results
95  */
96 void display_address_book_inner_div() {
97         char buf[256];
98         int num_targets = 0;
99         char target_id[64];
100         char target_label[64];
101         long len;
102         char *Name;
103         void *Namee;
104         HashList *List;
105         HashPos  *it;
106         int i;
107         char saved_roomname[128];
108
109         begin_ajax_response();
110
111         List = NewHash(1, NULL);
112         wprintf("<div align=center><form onSubmit=\"return false;\">"
113                 "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
114
115         if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
116                 serv_puts("LIST");
117                 serv_getln(buf, sizeof buf);
118                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
119                         Name = (char*) malloc(len + 1);
120                         len = extract_token(Name, buf, 0, '|', len + 1);
121                         Put(List, Name, len, Name, NULL);
122
123                 }
124                 SortByHashKey(List, 1);
125                 it = GetNewHashPos();
126                 while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
127                         wprintf("<option value=\"");
128                         escputs((char*)Namee);
129                         wprintf("\">");
130                         escputs((char*)Namee);
131                         wprintf("</option>\n");
132                 }
133                 DeleteHashPos(&it);
134                 DeleteHash(&List);
135         }
136
137         else {
138                 safestrncpy(saved_roomname, WC->wc_roomname, sizeof saved_roomname);
139                 gotoroom(bstr("which_addr_book"));
140                 serv_puts("DVCA");
141                 serv_getln(buf, sizeof buf);
142                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
143                         Name = (char*) malloc(len + 1);
144                         len = extract_token(Name, buf, 0, '|', len + 1);
145                         Put(List, Name, len, Name, NULL);
146
147                 }
148                 SortByHashKey(List, 1);
149                 it = GetNewHashPos();
150                 while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
151                         wprintf("<option value=\"");
152                         escputs((char*)Namee);
153                         wprintf("\">");
154                         escputs((char*)Namee);
155                         wprintf("</option>\n");
156                 }
157                 DeleteHashPos(&it);
158                 DeleteHash(&List);
159                 gotoroom(saved_roomname);
160         }
161
162         wprintf("</select>\n");
163
164         wprintf("%s: ", _("Add"));
165
166         num_targets = num_tokens(bstr("target_input"), '|');
167         for (i=0; i<num_targets; i+=2) {
168                 extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
169                 extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
170                 wprintf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
171                 wprintf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
172         }
173
174         /* This 'close window' button works.  Omitting it because we already have a close button
175          * in the upper right corner, and this one takes up space.
176          *
177         wprintf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
178         wprintf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
179          */
180
181         wprintf("</form></div>\n");
182
183         end_ajax_response();
184 }
185
186
187
188
189 void 
190 InitModule_ADDRBOOK_POPUP
191 (void)
192 {
193         WebcitAddUrlHandler(HKEY("display_address_book_middle_div"), display_address_book_middle_div, 0);
194         WebcitAddUrlHandler(HKEY("display_address_book_inner_div"), display_address_book_inner_div, 0);
195 }