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