* remove Dave's SYS_ users from the addressbookpopup, since you don't want to send...
[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                         if((len > 5) && (strncmp(Name, "SYS_", 4) == 0)) {
122                                 free(Name);
123                                 continue;
124                         }
125                         Put(List, Name, len, Name, NULL);
126
127                 }
128                 SortByHashKey(List, 1);
129                 it = GetNewHashPos();
130                 while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
131                         wprintf("<option value=\"");
132                         escputs((char*)Namee);
133                         wprintf("\">");
134                         escputs((char*)Namee);
135                         wprintf("</option>\n");
136                 }
137                 DeleteHashPos(&it);
138                 DeleteHash(&List);
139         }
140
141         else {
142                 safestrncpy(saved_roomname, WC->wc_roomname, sizeof saved_roomname);
143                 gotoroom(bstr("which_addr_book"));
144                 serv_puts("DVCA");
145                 serv_getln(buf, sizeof buf);
146                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
147                         Name = (char*) malloc(len + 1);
148                         len = extract_token(Name, buf, 0, '|', len + 1);
149                         Put(List, Name, len, Name, NULL);
150
151                 }
152                 SortByHashKey(List, 1);
153                 it = GetNewHashPos();
154                 while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
155                         wprintf("<option value=\"");
156                         escputs((char*)Namee);
157                         wprintf("\">");
158                         escputs((char*)Namee);
159                         wprintf("</option>\n");
160                 }
161                 DeleteHashPos(&it);
162                 DeleteHash(&List);
163                 gotoroom(saved_roomname);
164         }
165
166         wprintf("</select>\n");
167
168         wprintf("%s: ", _("Add"));
169
170         num_targets = num_tokens(bstr("target_input"), '|');
171         for (i=0; i<num_targets; i+=2) {
172                 extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
173                 extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
174                 wprintf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
175                 wprintf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
176         }
177
178         /* This 'close window' button works.  Omitting it because we already have a close button
179          * in the upper right corner, and this one takes up space.
180          *
181         wprintf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
182         wprintf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
183          */
184
185         wprintf("</form></div>\n");
186
187         end_ajax_response();
188 }
189
190
191
192
193 void 
194 InitModule_ADDRBOOK_POPUP
195 (void)
196 {
197         WebcitAddUrlHandler(HKEY("display_address_book_middle_div"), display_address_book_middle_div, 0);
198         WebcitAddUrlHandler(HKEY("display_address_book_inner_div"), display_address_book_inner_div, 0);
199 }