* updated listsorts to have '0' as sort order in all places that used SortByHashKey
[citadel.git] / webcit / addressbook_popup.c
index 6d479b3f0e9f03ed669ee8cdb089da85bc07672a..5e3118b0c88b197fc9dd572bcc2b77af28aaac5d 100644 (file)
@@ -1,20 +1,36 @@
 /*
- * $Id:  $
- *//**
- * \defgroup AjaxAutoCompletion ajax-powered autocompletion...
- * \ingroup ClientPower
+ * $Id$
+ *
+ * AJAX-powered auto-completion
  */
 
-/*@{*/
 #include "webcit.h"
 
 
-/**
- * \brief Address book popup window
+/*
+ * Call this right before wDumpContent() on any page which requires the address book popup
+ */
+void address_book_popup(void) {
+       /* Open a new div, hidden initially, for address book popups. */
+       wprintf("</div>\n");    /* End of 'content' div */
+       wprintf("<div id=\"address_book_popup\" style=\"display:none;\">");
+       wprintf("<div id=\"address_book_popup_container_div\">");
+       wprintf("<div id=\"address_book_popup_middle_div\"></div>");
+       wprintf("<div id=\"address_book_inner_div\"></div>");
+       wprintf("</div>");
+       /* The 'address_book_popup' div will be closed by wDumpContent() */
+}
+
+/*
+ * Address book popup window
  */
 void display_address_book_middle_div(void) {
        char buf[256];
-       char ebuf[256];
+       long len;
+       char *Name;
+       void *Namee;
+       HashList *List;
+       HashPos  *it;
 
        begin_ajax_response();
 
@@ -22,19 +38,39 @@ void display_address_book_middle_div(void) {
        wprintf("<td align=left><img src=\"static/viewcontacts_32x.gif\"></td>");
        wprintf("<td align=center>");
 
-       wprintf("<form><select class=\"address_book_popup_title\" size=1>");
+       wprintf("<form>"
+               "<select class=\"address_book_popup_title\" size=1 id=\"which_addr_book\" "
+               " onChange=\"PopulateAddressBookInnerDiv($('which_addr_book').value,'%s')\">",
+               bstr("target_input")
+       );
+
+       wprintf("<option value=\"__LOCAL_USERS__\">");
+       escputs(serv_info.serv_humannode);
+       wprintf("</option>\n");
+
+       
+       List = NewHash(1, NULL);
        serv_puts("LKRA");
        serv_getln(buf, sizeof buf);
-       if (buf[0] == '1') while(serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+       if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if (extract_int(buf, 6) == VIEW_ADDRESSBOOK) {
-                       extract_token(ebuf, buf, 0, '|', sizeof ebuf);
-                       wprintf("<option value=\"");
-                       urlescputs(ebuf);
-                       wprintf("\">");
-                       escputs(ebuf);
-                       wprintf("</option>\n");
+                       Name = (char*) malloc(len + 1);
+                       len = extract_token(Name, buf, 0, '|', len);
+                       Put(List, Name, len, Name, NULL);
                }
        }
+
+       SortByHashKey(List, 0);
+       it = GetNewHashPos();
+       while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
+               wprintf("<option value=\"");
+               urlescputs((char*)Namee);
+               wprintf("\">");
+               escputs((char*)Namee);
+               wprintf("</option>\n");
+       }
+       DeleteHashPos(&it);
+       DeleteHash(&List);
        wprintf("</select></form>");
 
        wprintf("</td>");
@@ -43,30 +79,106 @@ void display_address_book_middle_div(void) {
                "><img src=\"static/closewindow.gif\">");
        wprintf("</td></tr></table>");
 
+       wprintf("<script type=\"text/javascript\">"
+               "PopulateAddressBookInnerDiv($('which_addr_book').value,'%s');"
+               "</script>\n",
+               bstr("target_input")
+       );
+
        end_ajax_response();
 }
 
 
 
-/**
- * \brief Address book popup results
+/*
+ * Address book popup results
  */
-void display_address_book_inner_div(void) {
+void display_address_book_inner_div() {
+       char buf[256];
+       int num_targets = 0;
+       char target_id[64];
+       char target_label[64];
+       long len;
+       char *Name;
+       void *Namee;
+       HashList *List;
+       HashPos  *it;
        int i;
+       char saved_roomname[128];
 
        begin_ajax_response();
 
-       wprintf("<div align=center><form>"
-               "<select name=\"whichaddr\" size=\"15\">\n");
+       List = NewHash(1, NULL);
+       wprintf("<div align=center><form onSubmit=\"return false;\">"
+               "<select multiple name=\"whichaddr\" id=\"whichaddr\" size=\"15\">\n");
+
+       if (!strcasecmp(bstr("which_addr_book"), "__LOCAL_USERS__")) {
+               serv_puts("LIST");
+               serv_getln(buf, sizeof buf);
+               if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                       Name = (char*) malloc(len + 1);
+                       len = extract_token(Name, buf, 0, '|', len + 1);
+                       Put(List, Name, len, Name, NULL);
+
+               }
+               SortByHashKey(List, 0);
+               it = GetNewHashPos();
+               while (GetNextHashPos(List, it, &len, &Name, &Namee)) {
+                       wprintf("<option value=\"");
+                       escputs((char*)Namee);
+                       wprintf("\">");
+                       escputs((char*)Namee);
+                       wprintf("</option>\n");
+               }
+               DeleteHashPos(&it);
+               DeleteHash(&List);
+       }
+
+       else {
+               safestrncpy(saved_roomname, WC->wc_roomname, sizeof saved_roomname);
+               gotoroom(bstr("which_addr_book"));
+               serv_puts("DVCA");
+               serv_getln(buf, sizeof buf);
+               if (buf[0] == '1') while(len = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                       Name = (char*) malloc(len + 1);
+                       len = extract_token(Name, buf, 0, '|', len + 1);
+                       Put(List, Name, len, Name, NULL);
+
+               }
+               SortByHashKey(List, 0);
+               it = GetNewHashPos();
+               while (GetNextHashPos(List, it, &len, &Name, (void**)&Namee)) {
+                       wprintf("<option value=\"");
+                       escputs((char*)Namee);
+                       wprintf("\">");
+                       escputs((char*)Namee);
+                       wprintf("</option>\n");
+               }
+               DeleteHashPos(&it);
+               DeleteHash(&List);
+               gotoroom((char*)BSTR(saved_roomname)); /* TODO: get rid of typecast */
+       }
+
+       wprintf("</select>\n");
 
-       for (i=0; i<100; ++i) {
-               wprintf("<option>Contact %d &lt;contact%d@example.com&gt;</option>\n", i, i);
+       wprintf("%s: ", _("Add"));
+
+       num_targets = num_tokens(bstr("target_input"), '|');
+       for (i=0; i<num_targets; i+=2) {
+               extract_token(target_id, bstr("target_input"), i, '|', sizeof target_id);
+               extract_token(target_label, bstr("target_input"), i+1, '|', sizeof target_label);
+               wprintf("<INPUT TYPE=\"submit\" NAME=\"select_button\" VALUE=\"%s\" ", target_label);
+               wprintf("onClick=\"AddContactsToTarget($('%s'),$('whichaddr'));\">", target_id);
        }
 
-       wprintf("</select></form></div>\n");
+       /* This 'close window' button works.  Omitting it because we already have a close button
+        * in the upper right corner, and this one takes up space.
+        *
+       wprintf("<INPUT TYPE=\"submit\" NAME=\"close_button\" VALUE=\"%s\" ", _("Close window"));
+       wprintf("onclick=\"javascript:$('address_book_popup').style.display='none';\">");
+        */
+
+       wprintf("</form></div>\n");
 
        end_ajax_response();
 }
-
-
-/** @} */