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