* correct all GetNextHashPos() calls to have const chars
[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(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();
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\">"
92                 "PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');"
93                 "</script>\n",
94                 bstr("target_input")
95         );
96
97         end_ajax_response();
98 }
99
100
101
102 /*
103  * Address book popup results
104  */
105 void display_address_book_inner_div() {
106         char buf[256];
107         int num_targets = 0;
108         char target_id[64];
109         char target_label[64];
110         long len;
111         char *Name;
112         const char *VCName;
113         void *Namee;
114         HashList *List;
115         HashPos  *it;
116         int i;
117         char saved_roomname[128];
118
119         begin_ajax_response();
120
121         List = NewHash(1, NULL);
122         wprintf("<div align=center><form onSubmit=\"return false;\">"
123                 "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
124
125         if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
126                 serv_puts("LIST");
127                 serv_getln(buf, sizeof buf);
128                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
129                         Name = (char*) malloc(len + 1);
130                         len = extract_token(Name, buf, 0, '|', len + 1);
131                         if((len > 5) && (strncmp(Name, "SYS_", 4) == 0)) {
132                                 free(Name);
133                                 continue;
134                         }
135                         Put(List, Name, len, Name, NULL);
136
137                 }
138                 SortByHashKey(List, 1);
139                 it = GetNewHashPos();
140                 while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
141                         wprintf("<option value=\"");
142                         escputs((char*)Namee);
143                         wprintf("\">");
144                         escputs((char*)Namee);
145                         wprintf("</option>\n");
146                 }
147                 DeleteHashPos(&it);
148                 DeleteHash(&List);
149         }
150
151         else {
152                 set_room_pref("defaddrbook",NewStrBufDup(sbstr("which_addr_book")), 0);
153                 safestrncpy(saved_roomname, WC->wc_roomname, sizeof saved_roomname);
154                 gotoroom(bstr("which_addr_book"));
155                 serv_puts("DVCA");
156                 serv_getln(buf, sizeof buf);
157                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
158                         Name = (char*) malloc(len + 1);
159                         len = extract_token(Name, buf, 0, '|', len + 1);
160                         Put(List, Name, len, Name, NULL);
161
162                 }
163                 SortByHashKey(List, 1);
164                 it = GetNewHashPos();
165                 while (GetNextHashPos(List, it, &len, &VCName, (void**)&Namee)) {
166                         wprintf("<option value=\"");
167                         escputs((char*)Namee);
168                         wprintf("\">");
169                         escputs((char*)Namee);
170                         wprintf("</option>\n");
171                 }
172                 DeleteHashPos(&it);
173                 DeleteHash(&List);
174                 gotoroom(saved_roomname);
175         }
176
177         wprintf("</select>\n");
178
179         wprintf("%s: ", _("Add"));
180
181         num_targets = num_tokens(bstr("target_input"), '|');
182         for (i=0; i<num_targets; i+=2) {
183                 extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
184                 extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
185                 wprintf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
186                 wprintf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
187         }
188
189         /* This 'close window' button works.  Omitting it because we already have a close button
190          * in the upper right corner, and this one takes up space.
191          *
192         wprintf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
193         wprintf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
194          */
195
196         wprintf("</form></div>\n");
197
198         end_ajax_response();
199 }
200
201
202
203
204 void 
205 InitModule_ADDRBOOK_POPUP
206 (void)
207 {
208         WebcitAddUrlHandler(HKEY("display_address_book_middle_div"), display_address_book_middle_div, 0);
209         WebcitAddUrlHandler(HKEY("display_address_book_inner_div"), display_address_book_inner_div, 0);
210 }