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