* abstract setting per room preferences
[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         StrBuf *DefAddrBook;
33         HashList *List;
34         HashPos  *it;
35
36         begin_ajax_response();
37
38         DefAddrBook = get_room_pref("defaddrbook");
39
40         wprintf("<table border=0 width=100%%><tr valign=middle>");
41         wprintf("<td align=left><img src=\"static/viewcontacts_32x.gif\"></td>");
42         wprintf("<td align=center>");
43
44         wprintf("<form>"
45                 "<select class=\"address_book_popup_title\" size=1 id=\"which_addr_book\" "
46                 " onChange=\"PopulateAddressBookInnerDiv($('which_addr_book').value,'%s')\">",
47                 bstr("target_input")
48         );
49
50         wprintf("<option value=\"__LOCAL_USERS__\" %s>", 
51                 (strcmp(ChrPtr(DefAddrBook), "__LOCAL_USERS__") == 0)?
52                 "active=\"yes\" ":"");
53         escputs(serv_info.serv_humannode);
54         wprintf("</option>\n");
55
56         
57         List = NewHash(1, NULL);
58         serv_puts("LKRA");
59         serv_getln(buf, sizeof buf);
60         if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
61                 if (extract_int(buf, 6) == VIEW_ADDRESSBOOK) {
62                         Name = (char*) malloc(len + 1);
63                         len = extract_token(Name, buf, 0, '|', len);
64                         Put(List, Name, len, Name, NULL);
65                 }
66         }
67
68         SortByHashKey(List, 1);
69         it = GetNewHashPos();
70         while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
71                 wprintf("<option value=\"");
72                 urlescputs((char*)Namee);
73                 if (strcmp(ChrPtr(DefAddrBook), Namee) == 0)
74                         wprintf("\" active=\"yes\" >");
75                 else
76                         wprintf("\">");
77                 escputs((char*)Namee);
78                 wprintf("</option>\n");
79         }
80         DeleteHashPos(&it);
81         DeleteHash(&List);
82         wprintf("</select></form>");
83
84         wprintf("</td>");
85         wprintf("<td align=right "
86                 "onclick=\"javascript:$('address_book_popup').style.display='none';\" "
87                 "><img src=\"static/closewindow.gif\">");
88         wprintf("</td></tr></table>");
89
90         wprintf("<script type=\"text/javascript\">"
91                 "PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');"
92                 "</script>\n",
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         void *Namee;
112         HashList *List;
113         HashPos  *it;
114         int i;
115         char saved_roomname[128];
116
117         begin_ajax_response();
118
119         List = NewHash(1, NULL);
120         wprintf("<div align=center><form onSubmit=\"return false;\">"
121                 "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
122
123         if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
124                 serv_puts("LIST");
125                 serv_getln(buf, sizeof buf);
126                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
127                         Name = (char*) malloc(len + 1);
128                         len = extract_token(Name, buf, 0, '|', len + 1);
129                         if((len > 5) && (strncmp(Name, "SYS_", 4) == 0)) {
130                                 free(Name);
131                                 continue;
132                         }
133                         Put(List, Name, len, Name, NULL);
134
135                 }
136                 SortByHashKey(List, 1);
137                 it = GetNewHashPos();
138                 while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
139                         wprintf("<option value=\"");
140                         escputs((char*)Namee);
141                         wprintf("\">");
142                         escputs((char*)Namee);
143                         wprintf("</option>\n");
144                 }
145                 DeleteHashPos(&it);
146                 DeleteHash(&List);
147         }
148
149         else {
150                 set_room_pref("defaddrbook",NewStrBufDup(sbstr("which_addr_book")), 0);
151                 safestrncpy(saved_roomname, WC->wc_roomname, sizeof saved_roomname);
152                 gotoroom(bstr("which_addr_book"));
153                 serv_puts("DVCA");
154                 serv_getln(buf, sizeof buf);
155                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
156                         Name = (char*) malloc(len + 1);
157                         len = extract_token(Name, buf, 0, '|', len + 1);
158                         Put(List, Name, len, Name, NULL);
159
160                 }
161                 SortByHashKey(List, 1);
162                 it = GetNewHashPos();
163                 while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
164                         wprintf("<option value=\"");
165                         escputs((char*)Namee);
166                         wprintf("\">");
167                         escputs((char*)Namee);
168                         wprintf("</option>\n");
169                 }
170                 DeleteHashPos(&it);
171                 DeleteHash(&List);
172                 gotoroom(saved_roomname);
173         }
174
175         wprintf("</select>\n");
176
177         wprintf("%s: ", _("Add"));
178
179         num_targets = num_tokens(bstr("target_input"), '|');
180         for (i=0; i<num_targets; i+=2) {
181                 extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
182                 extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
183                 wprintf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
184                 wprintf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
185         }
186
187         /* This 'close window' button works.  Omitting it because we already have a close button
188          * in the upper right corner, and this one takes up space.
189          *
190         wprintf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
191         wprintf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
192          */
193
194         wprintf("</form></div>\n");
195
196         end_ajax_response();
197 }
198
199
200
201
202 void 
203 InitModule_ADDRBOOK_POPUP
204 (void)
205 {
206         WebcitAddUrlHandler(HKEY("display_address_book_middle_div"), display_address_book_middle_div, 0);
207         WebcitAddUrlHandler(HKEY("display_address_book_inner_div"), display_address_book_inner_div, 0);
208 }