ef660310bd70f5fc1e1bd526cf4ffc81a7022c3c
[citadel.git] / webcit / addressbook_popup.c
1 /*
2  * $Id$
3  *
4  * AJAX-powered auto-completion
5  *
6  * Copyright (c) 2005-2010 by the citadel.org team
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "webcit.h"
24
25
26 /*
27  * Call this right before wDumpContent() on any page which requires the address book popup
28  */
29 void address_book_popup(void) {
30         /* Open a new div, hidden initially, for address book popups. */
31         wc_printf("</div>\n");  /* End of 'content' div */
32         wc_printf("<div id=\"address_book_popup\" style=\"display:none;\">");
33         wc_printf("<div id=\"address_book_popup_container_div\">");
34         wc_printf("<div id=\"address_book_popup_middle_div\"></div>");
35         wc_printf("<div id=\"address_book_inner_div\"></div>");
36         wc_printf("</div>");
37         /* The 'address_book_popup' div will be closed by wDumpContent() */
38 }
39
40 /*
41  * Address book popup window
42  */
43 void display_address_book_middle_div(void) {
44         char buf[256];
45         long len;
46         char *Name;
47         const char *VCName;
48         void *Namee;
49         StrBuf *DefAddrBook;
50         HashList *List;
51         HashPos  *it;
52
53         begin_ajax_response();
54
55         DefAddrBook = get_room_pref("defaddrbook");
56
57         wc_printf("<table border=0 width=100%%><tr valign=middle>");
58         wc_printf("<td align=left><img src=\"static/viewcontacts_32x.gif\"></td>");
59         wc_printf("<td align=center>");
60
61         wc_printf("<form>"
62                 "<select class=\"address_book_popup_title\" size=1 id=\"which_addr_book\" "
63                 " onChange=\"PopulateAddressBookInnerDiv($('which_addr_book').value,'%s')\">",
64                 bstr("target_input")
65         );
66
67         wc_printf("<option value=\"__LOCAL_USERS__\" %s>", 
68                 (strcmp(ChrPtr(DefAddrBook), "__LOCAL_USERS__") == 0)?
69                 "selected=\"selected\" ":"");
70         escputs(ChrPtr(WC->serv_info->serv_humannode));
71         wc_printf("</option>\n");
72
73         
74         List = NewHash(1, NULL);
75         serv_puts("LKRA");
76         serv_getln(buf, sizeof buf);
77         if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
78                 if (extract_int(buf, 6) == VIEW_ADDRESSBOOK) {
79                         Name = (char*) malloc(len + 1);
80                         len = extract_token(Name, buf, 0, '|', len);
81                         Put(List, Name, len, Name, NULL);
82                 }
83         }
84
85         SortByHashKey(List, 1);
86         it = GetNewHashPos(List, 0);
87         while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
88                 wc_printf("<option value=\"");
89                 urlescputs((char*)Namee);
90                 if (strcmp(ChrPtr(DefAddrBook), Namee) == 0)
91                         wc_printf("\" selected=\"selected\" >");
92                 else
93                         wc_printf("\">");
94                 escputs((char*)Namee);
95                 wc_printf("</option>\n");
96         }
97         DeleteHashPos(&it);
98         DeleteHash(&List);
99         wc_printf("</select></form>");
100
101         wc_printf("</td>");
102         wc_printf("<td align=right "
103                 "onclick=\"javascript:$('address_book_popup').style.display='none';\" "
104                 "><img src=\"static/closewindow.gif\">");
105         wc_printf("</td></tr></table>");
106
107         wc_printf("<script type=\"text/javascript\">PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');</script>",
108                 bstr("target_input")
109         );
110
111         end_ajax_response();
112 }
113
114
115
116 /*
117  * Address book popup results
118  */
119 void display_address_book_inner_div() {
120         char buf[256];
121         int num_targets = 0;
122         char target_id[64];
123         char target_label[64];
124         long len;
125         char *Name;
126         const char *VCName;
127         void *Namee;
128         HashList *List;
129         HashPos  *it;
130         int i;
131         StrBuf *saved_roomname;
132
133         begin_ajax_response();
134
135         List = NewHash(1, NULL);
136         wc_printf("<div align=center><form onSubmit=\"return false;\">"
137                 "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
138
139         if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
140                 serv_puts("LIST");
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                         if((len > 5) && (strncmp(Name, "SYS_", 4) == 0)) {
146                                 free(Name);
147                                 continue;
148                         }
149                         Put(List, Name, len, Name, NULL);
150
151                 }
152                 SortByHashKey(List, 1);
153                 it = GetNewHashPos(List, 0);
154                 while (GetNextHashPos(List, it, &len, &VCName, &Namee)) {
155                         wc_printf("<option value=\"");
156                         escputs((char*)Namee);
157                         wc_printf("\">");
158                         escputs((char*)Namee);
159                         wc_printf("</option>\n");
160                 }
161                 DeleteHashPos(&it);
162                 DeleteHash(&List);
163         }
164
165         else {
166                 set_room_pref("defaddrbook",NewStrBufDup(sbstr("which_addr_book")), 0);
167                 saved_roomname = NewStrBufDup(WC->CurRoom.name);
168                 gotoroom(sbstr("which_addr_book"));
169                 serv_puts("DVCA");
170                 serv_getln(buf, sizeof buf);
171                 if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
172                         Name = (char*) malloc(len + 1);
173                         len = extract_token(Name, buf, 0, '|', len + 1);
174                         Put(List, Name, len, Name, NULL);
175
176                 }
177                 SortByHashKey(List, 1);
178                 it = GetNewHashPos(List, 0);
179                 while (GetNextHashPos(List, it, &len, &VCName, (void**)&Namee)) {
180                         wc_printf("<option value=\"");
181                         escputs((char*)Namee);
182                         wc_printf("\">");
183                         escputs((char*)Namee);
184                         wc_printf("</option>\n");
185                 }
186                 DeleteHashPos(&it);
187                 DeleteHash(&List);
188                 gotoroom(saved_roomname);
189                 FreeStrBuf(&saved_roomname);
190         }
191
192         wc_printf("</select>\n");
193
194         wc_printf("%s: ", _("Add"));
195
196         num_targets = num_tokens(bstr("target_input"), '|');
197         for (i=0; i<num_targets; i+=2) {
198                 extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
199                 extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
200                 wc_printf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
201                 wc_printf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
202         }
203
204         /* This 'close window' button works.  Omitting it because we already have a close button
205          * in the upper right corner, and this one takes up space.
206          *
207         wc_printf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
208         wc_printf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
209          */
210
211         wc_printf("</form></div>\n");
212
213         end_ajax_response();
214 }
215
216
217
218
219 void 
220 InitModule_ADDRBOOK_POPUP
221 (void)
222 {
223         WebcitAddUrlHandler(HKEY("display_address_book_middle_div"), "", 0, display_address_book_middle_div, 0);
224         WebcitAddUrlHandler(HKEY("display_address_book_inner_div"), "", 0, display_address_book_inner_div, 0);
225 }