/* * ui_peer.c -- the peer details dialog * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * Shows what the local peer model knows: name, identity, certificate * fingerprint, address, state and whether the peer came from mDNS or was * added by hand. The Blocked toggle is applied at once (dc_peer_set_blocked) * and reported to the caller; the dialog stays open. */ #define _POSIX_C_SOURCE 200809L #include "ui_peer.h" #include "ui_text.h" #include #include #include #include #include #include #include #include #include #include #include struct DtPeerDialog { Widget shell; Widget blocked; DcPeer *peer; DtPeerBlockFn fn; void *arg; int applied; }; static XmString xs(const char *s) { return XmStringCreateLocalized((char *)s); } static void set_label(Widget w, const char *text) { XmString x = xs(text); XtVaSetValues(w, XmNlabelString, x, NULL); XmStringFree(x); } static Widget row(Widget parent, Widget previous, const char *name, const char *value) { Widget form = XtVaCreateManagedWidget(name, xmFormWidgetClass, parent, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNtopAttachment, previous != NULL ? XmATTACH_WIDGET : XmATTACH_FORM, XmNtopWidget, previous, XmNheight, 26, NULL); Widget label = XmCreateLabel(form, "label", NULL, 0); Widget text = XmCreateLabel(form, "value", NULL, 0); XmString x = xs(name); XtVaSetValues(label, XmNlabelString, x, XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, XmNwidth, 130, XmNrecomputeSize, False, XmNalignment, XmALIGNMENT_END, XmNmarginRight, 8, NULL); XmStringFree(x); XtManageChild(label); set_label(text, value != NULL ? value : ""); XtVaSetValues(text, XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, label, XmNrightAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, XmNalignment, XmALIGNMENT_BEGINNING, NULL); XtManageChild(text); return form; } static void apply_cb(Widget w, XtPointer client, XtPointer call) { DtPeerDialog *dialog = client; int blocked; (void)w; (void)call; blocked = XmToggleButtonGetState(dialog->blocked) ? 1 : 0; dc_peer_set_blocked(dialog->peer, blocked); dialog->applied = 1; if (dialog->fn != NULL) dialog->fn(dc_peer_id(dialog->peer), blocked, dialog->arg); } static void close_cb(Widget w, XtPointer client, XtPointer call) { DtPeerDialog *dialog = client; (void)w; (void)call; dtchat_peer_dialog_destroy(dialog); } DtPeerDialog * dtchat_peer_dialog_create(Widget parent, DcPeer *peer, DtPeerBlockFn blocked, void *arg) { DtPeerDialog *dialog; Widget form, rows, buttons, apply, close; char line[512]; const char *name; const char *id; const char *pin; Widget previous = NULL; XmString x; if (parent == NULL || peer == NULL) return NULL; dialog = calloc(1, sizeof(*dialog)); if (dialog == NULL) return NULL; dialog->peer = peer; dialog->fn = blocked; dialog->arg = arg; form = XmCreateFormDialog(parent, "peerDialog", NULL, 0); if (form == NULL) { free(dialog); return NULL; } dialog->shell = XtParent(form); XtVaSetValues(dialog->shell, XmNtitle, (char *)dc_text(TX_PEER, "Peer"), XmNwidth, 480, XmNheight, 330, XmNdeleteResponse, XmDESTROY, NULL); rows = XtVaCreateManagedWidget("rows", xmFormWidgetClass, form, XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNmarginHeight, 6, NULL); name = dc_peer_name(peer); id = dc_peer_id(peer); pin = dc_peer_pin(peer); previous = row(rows, NULL, dc_text(TX_NAME, "Name"), name); previous = row(rows, previous, dc_text(TX_IDENTITY, "Identity"), id); previous = row(rows, previous, dc_text(TX_FINGERPRINT, "Fingerprint"), pin != NULL && *pin != '\0' ? pin : dc_text(TX_NOT_KNOWN, "(not known yet)")); snprintf(line, sizeof(line), "%s:%d", dc_peer_host(peer) != NULL && dc_peer_host(peer)[0] != '\0' ? dc_peer_host(peer) : "(no address)", dc_peer_port(peer)); previous = row(rows, previous, dc_text(TX_ADDRESS, "Address"), line); previous = row(rows, previous, dc_text(TX_STATE, "State"), dc_peer_online(peer) ? "online" : "offline"); previous = row(rows, previous, dc_text(TX_SOURCE, "Source"), dc_peer_manual(peer) ? dc_text(TX_ADDED_BY_HAND, "added by hand") : dc_text(TX_FOUND_NETWORK, "found on the network")); dialog->blocked = XmCreateToggleButton(rows, "blocked", NULL, 0); x = xs(dc_text(TX_BLOCK_PEER, "Block this peer")); XtVaSetValues(dialog->blocked, XmNlabelString, x, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, previous, XmNleftAttachment, XmATTACH_FORM, XmNtopOffset, 8, NULL); XmStringFree(x); XmToggleButtonSetState(dialog->blocked, dc_peer_blocked(peer), False); XtManageChild(dialog->blocked); XtVaSetValues(rows, XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, dialog->blocked, NULL); buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, form, XmNorientation, XmHORIZONTAL, XmNspacing, 6, XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNmarginHeight, 6, NULL); apply = XmCreatePushButton(buttons, "apply", NULL, 0); set_label(apply, dc_text(TX_APPLY, "Apply")); XtAddCallback(apply, XmNactivateCallback, apply_cb, dialog); XtManageChild(apply); close = XmCreatePushButton(buttons, "close", NULL, 0); set_label(close, dc_text(TX_CLOSE, "Close")); XtAddCallback(close, XmNactivateCallback, close_cb, dialog); XtManageChild(close); XtManageChild(form); XtPopup(dialog->shell, XtGrabNone); return dialog; } void dtchat_peer_dialog_destroy(DtPeerDialog *dialog) { if (dialog == NULL) return; if (dialog->shell != NULL) { Widget shell = dialog->shell; dialog->shell = NULL; XtDestroyWidget(shell); } free(dialog); } Widget dtchat_peer_dialog_shell(const DtPeerDialog *dialog) { return dialog != NULL ? dialog->shell : NULL; }