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