5758ec3b0641d7e91c6e2f106b73dd7ae8828705
[citadel.git] / webcit / addressbook_popup.c
1 /*
2  * $Id$
3  *
4  * \defgroup AjaxAutoCompletion ajax-powered autocompletion...
5  * \ingroup ClientPower
6  */
7
8 /*@{*/
9 #include "webcit.h"
10
11
12 /**
13  * \brief Call this right before wDumpContent() on any page which requires the address book popup
14  */
15 void address_book_popup(void) {
16         /* Open a new div, hidden initially, for address book popups. */
17         wprintf("</div>\n");    /* End of 'content' div */
18         wprintf("<div id=\"address_book_popup\" style=\"display:none;\">");
19         wprintf("<div id=\"address_book_popup_container_div\">");
20         wprintf("<div id=\"address_book_popup_middle_div\"></div>");
21         wprintf("<div id=\"address_book_inner_div\"></div>");
22         wprintf("</div>");
23         /* The 'address_book_popup' div will be closed by wDumpContent() */
24 }
25
26 /**
27  * \brief Address book popup window
28  */
29 void display_address_book_middle_div(void) {
30         char buf[256];
31         long len;
32         char *Name, *Namee;
33         HashList *List;
34         HashPos  *it;
35
36         begin_ajax_response();
37
38         wprintf("<table border=0 width=100%%><tr valign=middle>");
39         wprintf("<td align=left><img src=\"static/viewcontacts_32x.gif\"></td>");
40         wprintf("<td align=center>");
41
42         wprintf("<form>"
43                 "<select class=\"address_book_popup_title\" size=1 id=\"which_addr_book\" "
44                 " onChange=\"PopulateAddressBookInnerDiv($('which_addr_book').value,'%s')\">",
45                 bstr("target_input")
46         );
47
48         wprintf("<option value=\"__LOCAL_USERS__\">");
49         escputs(serv_info.serv_humannode);
50         wprintf("</option>\n");
51
52         
53         List = NewHash();
54         serv_puts("LKRA");
55         serv_getln(buf, sizeof buf);
56         if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
57                 if (extract_int(buf, 6) == VIEW_ADDRESSBOOK) {
58                         Name = (char*) malloc(len + 1);
59                         len = extract_token(Name, buf, 0, '|', len);
60                         Put(List, Name, len, Name, NULL);
61                 }
62         }
63
64         SortByHashKey(List);
65         it = GetNewHashPos();
66         while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
67                 wprintf("<option value=\"");
68                 urlescputs(Namee);
69                 wprintf("\">");
70                 escputs(Namee);
71                 wprintf("</option>\n");
72         }
73         DeleteHashPos(&it);
74         DeleteHash(&List);
75         wprintf("</select></form>");
76
77         wprintf("</td>");
78         wprintf("<td align=right "
79                 "onclick=\"javascript:$('address_book_popup').style.display='none';\" "
80                 "><img src=\"static/closewindow.gif\">");
81         wprintf("</td></tr></table>");
82
83         wprintf("<script type=\"text/javascript\">"
84                 "PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');"
85                 "</script>\n",
86                 bstr("target_input")
87         );
88
89         end_ajax_response();
90 }
91
92
93
94 /**
95  * \brief Address book popup results
96  */
97 void display_address_book_inner_div() {
98         char buf[256];
99         int num_targets = 0;
100         char target_id[64];
101         char target_label[64];
102         long len;
103         char *Name, *Namee;
104         HashList *List;
105         HashPos  *it;
106         int i;
107         char saved_roomname[128];
108
109         begin_ajax_response();
110
111         List = NewHash();
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);
125                 it = GetNewHashPos();
126                 while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
127                         wprintf("<option value=\"");
128                         escputs(Namee);
129                         wprintf("\">");
130                         escputs(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);
149                 it = GetNewHashPos();
150                 while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
151                         wprintf("<option value=\"");
152                         escputs(Namee);
153                         wprintf("\">");
154                         escputs(Namee);
155                         wprintf("</option>\n");
156                 }
157                 DeleteHashPos(&it);
158                 DeleteHash(&List);
159                 gotoroom(bstr(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 /** @} */