3cb23ebddbd3366b2b4c947cf73fd5f4f5dc16f2
[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(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         StrBufAppendPrintf(WC->trailing_javascript,
92                 "PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');",
93                 bstr("target_input")
94         );
95
96         end_ajax_response();
97 }
98
99
100
101 /*
102  * Address book popup results
103  */
104 void display_address_book_inner_div() {
105         char buf[256];
106         int num_targets = 0;
107         char target_id[64];
108         char target_label[64];
109         long len;
110         char *Name;
111         const char *VCName;
112         void *Namee;
113         HashList *List;
114         HashPos  *it;
115         int i;
116         char saved_roomname[128];
117
118         begin_ajax_response();
119
120         List = NewHash(1, NULL);
121         wprintf("<div align=center><form onSubmit=\"return false;\">"
122                 "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
123
124         if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
125                 serv_puts("LIST");
126                 serv_getln(buf, sizeof buf);
127                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
128                         Name = (char*) malloc(len + 1);
129                         len = extract_token(Name, buf, 0, '|', len + 1);
130                         if((len > 5) && (strncmp(Name, "SYS_", 4) == 0)) {
131                                 free(Name);
132                                 continue;
133                         }
134                         Put(List, Name, len, Name, NULL);
135
136                 }
137                 SortByHashKey(List, 1);
138                 it = GetNewHashPos(List, 0);
139                 while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
140                         wprintf("<option value=\"");
141                         escputs((char*)Namee);
142                         wprintf("\">");
143                         escputs((char*)Namee);
144                         wprintf("</option>\n");
145                 }
146                 DeleteHashPos(&it);
147                 DeleteHash(&List);
148         }
149
150         else {
151                 set_room_pref("defaddrbook",NewStrBufDup(sbstr("which_addr_book")), 0);
152                 safestrncpy(saved_roomname, WC->wc_roomname, sizeof saved_roomname);
153                 gotoroom(bstr("which_addr_book"));
154                 serv_puts("DVCA");
155                 serv_getln(buf, sizeof buf);
156                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
157                         Name = (char*) malloc(len + 1);
158                         len = extract_token(Name, buf, 0, '|', len + 1);
159                         Put(List, Name, len, Name, NULL);
160
161                 }
162                 SortByHashKey(List, 1);
163                 it = GetNewHashPos(List, 0);
164                 while (GetNextHashPos(List, it, &len, &VCName, (void**)&Namee)) {
165                         wprintf("<option value=\"");
166                         escputs((char*)Namee);
167                         wprintf("\">");
168                         escputs((char*)Namee);
169                         wprintf("</option>\n");
170                 }
171                 DeleteHashPos(&it);
172                 DeleteHash(&List);
173                 gotoroom(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 }