+/*
+ * Render the address book using info we gathered during the scan
+ *
+ * addrbook the addressbook to render
+ * num_ab the number of the addressbook
+ */
+void do_addrbook_view(vcardview_struct* VS) {
+ long i = 0;
+ int displayed = 0;
+ int bg = 0;
+ static int NAMESPERPAGE = 60;
+ int num_pages = 0;
+ int tabfirst = 0;
+ int tablast = 0;
+ int page = 0;
+ char **tablabels;
+ int num_ab = GetCount(VS->addrbook);
+ HashList *headlines;
+ HashPos *it;
+ wcsession *WCC = WC;
+
+ WCTemplputParams *TP = NULL;
+ WCTemplputParams SubTP;
+
+ memset(&SubTP, 0, sizeof(WCTemplputParams));
+
+ if (num_ab == 0) {
+ do_template("vcard_list_empty");
+ return;
+ }
+
+ if (num_ab > 1) {
+ ///SortByHashKey(VS->addrbook, 0);
+ }
+
+ num_pages = (GetCount(VS->addrbook) / NAMESPERPAGE) + 1;
+
+ tablabels = malloc(num_pages * sizeof (char *));
+ if (tablabels == NULL) {
+ return;
+ }
+
+ headlines = NewHash(0, lFlathash);
+ for (i=0; i<num_pages; ++i) {
+ void *v1 = NULL;
+ void *v2 = NULL;
+ long hklen1, hklen2;
+ const char *c1, *c2;
+ StrBuf *headline;
+ addrbookent *a1, *a2;
+
+ tabfirst = i * NAMESPERPAGE;
+ tablast = tabfirst + NAMESPERPAGE - 1;
+ if (tablast > (num_ab - 1)) tablast = (num_ab - 1);
+
+ headline = NewStrBufPlain(NULL, StrLength(v1) + StrLength(v2) + 10);
+ if (GetHashAt(VS->addrbook, tabfirst, &hklen1, &c1, &v1)) {
+ a1 = (addrbookent*) v1;
+ StrBufAppendBuf(headline, a1->name, 0);
+
+ if (GetHashAt(VS->addrbook, tablast, &hklen2, &c2, &v2)) {
+
+ a2 = (addrbookent*) v2;
+ StrBufAppendBufPlain(headline, HKEY(" - "), 0);
+ StrBufAppendBuf(headline, a2->name, 0);
+ }
+ }
+ Put(headlines, LKEY(i), headline, HFreeStrBuf);
+ }
+
+ tabbed_dialog(num_pages, tablabels);
+ page = (-1);
+
+ it = GetNewHashPos(VS->addrbook, 0);
+ for (i=0; i<num_ab; ++i) {
+ void *v;
+ long hklen;
+ const char *key;
+ addrbookent *abEnt;
+ GetNextHashPos(VS->addrbook, it, &hklen, &key, &v);
+ if (v == NULL)
+ continue;
+ abEnt = (addrbookent *) v;
+ if ((i / NAMESPERPAGE) != page) { /* New tab */
+ page = (i / NAMESPERPAGE);
+ if (page > 0) {
+ do_template("vcard_list_section_end");
+ end_tab(page-1, num_pages);
+ }
+ begin_tab(page, num_pages);
+ do_template("vcard_list_section_start");
+ displayed = 0;
+ }
+
+ if ((displayed % 4) == 0) {
+ if (displayed > 0) {
+ do_template("vcard_list_row_end");
+ }
+ do_template("vcard_list_row_start");
+ bg = 1 - bg;
+ }
+
+
+ StackContext(TP, &SubTP, abEnt, CTX_VCARD, 0, NULL);
+
+ DoTemplate(HKEY("vcard_list_entry"), WCC->WBuf, &SubTP);
+ UnStackContext(&SubTP);
+
+ ++displayed;
+ }
+ DeleteHashPos(&it);
+
+ /* Placeholders for empty columns at end */
+ if ((num_ab % 4) != 0) {
+ for (i=0; i<(4-(num_ab % 4)); ++i) {
+ do_template("vcard_list_cell_end");
+ }
+ }
+
+ do_template("vcard_list_section_end");
+ end_tab((num_pages-1), num_pages);
+
+ begin_tab(num_pages, num_pages);
+ /* FIXME there ought to be something here */
+ end_tab(num_pages, num_pages);
+
+ DeleteHash(&headlines);
+ wDumpContent(1);
+}
+
+
+