diff --git a/programs/dtchat/Makefile.am b/programs/dtchat/Makefile.am index a31ca2d2..0c3efdc4 100644 --- a/programs/dtchat/Makefile.am +++ b/programs/dtchat/Makefile.am @@ -9,7 +9,8 @@ dtchat_CPPFLAGS = -I$(top_srcdir)/include \ # The model lives in libDtChat; DtInitialize/DTCLIENTLIBS come with T2.7. dtchat_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la $(XTOOLLIB) -dtchat_SOURCES = main.c ui_main.c ui_accounts.c ui_main.h ui_accounts.h +dtchat_SOURCES = main.c ui_main.c ui_accounts.c ui_request.c \ + ui_main.h ui_accounts.h ui_request.h if SOLARIS dtchat_LDADD += -lintl diff --git a/programs/dtchat/main.c b/programs/dtchat/main.c index dd754298..7e5f875c 100644 --- a/programs/dtchat/main.c +++ b/programs/dtchat/main.c @@ -18,11 +18,215 @@ #include #include "ui_main.h" +#include "ui_request.h" static XtAppContext app; static Widget toplevel; static DcModel *demo_model; static int smoke_failed; +static const char *demo_kind; + +struct Answer { + int called; + char *value; +}; + +static void +answer_cb(const char *value, void *arg) +{ + struct Answer *answer = arg; + + answer->called++; + free(answer->value); + answer->value = value != NULL ? strdup(value) : NULL; +} + +static char * +b64_encode(const unsigned char *in, size_t len) +{ + static const char table[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + char *out = malloc((len + 2) / 3 * 4 + 1); + size_t i, o = 0; + + if (out == NULL) + return NULL; + for (i = 0; i + 2 < len; i += 3) { + out[o++] = table[in[i] >> 2]; + out[o++] = table[((in[i] & 3) << 4) | (in[i + 1] >> 4)]; + out[o++] = table[((in[i + 1] & 15) << 2) | (in[i + 2] >> 6)]; + out[o++] = table[in[i + 2] & 63]; + } + if (i < len) { + out[o++] = table[in[i] >> 2]; + if (i + 1 < len) { + out[o++] = table[((in[i] & 3) << 4) | + (in[i + 1] >> 4)]; + out[o++] = table[(in[i + 1] & 15) << 2]; + } else { + out[o++] = table[(in[i] & 3) << 4]; + out[o++] = '='; + } + out[o++] = '='; + } + out[o] = '\0'; + return out; +} + +static DcJson * +request_json(const char *kind, const char *title, const char *prompt, + const char *hint) +{ + DcJson *request = dc_json_new_object(); + + (void)dc_json_object_set(request, "kind", dc_json_new_string(kind)); + (void)dc_json_object_set(request, "title", dc_json_new_string(title)); + if (prompt != NULL) + (void)dc_json_object_set(request, "prompt", + dc_json_new_string(prompt)); + if (hint != NULL) + (void)dc_json_object_set(request, "hint", + dc_json_new_string(hint)); + return request; +} + +static DcJson * +qr_request(int *size_out) +{ + enum { QW = 21, QH = 21 }; + unsigned char modules[QW * QH]; + char *b64; + DcJson *request; + DcJson *qr; + int x, y, i; + + memset(modules, 0, sizeof(modules)); + for (y = 0; y < QH; y++) { + for (x = 0; x < QW; x++) { + int dark = 0; + + /* three finder patterns */ + for (i = 0; i < 3; i++) { + int ox = i == 1 ? 14 : 0; + int oy = i == 2 ? 14 : 0; + + if (x >= ox && x < ox + 7 && y >= oy && + y < oy + 7) { + int dx = x - ox, dy = y - oy; + dark = dx == 0 || dx == 6 || dy == 0 || + dy == 6 || + (dx >= 2 && dx <= 4 && + dy >= 2 && dy <= 4); + } + } + /* timing lines */ + if (x == 6 || y == 6) + dark = ((x + y) % 2) == 0; + modules[(size_t)y * QW + x] = (unsigned char)dark; + } + } + b64 = b64_encode(modules, sizeof(modules)); + if (b64 == NULL) + return NULL; + request = request_json("qr", "WhatsApp", + NULL, "WhatsApp > Linked devices > Link a device"); + qr = dc_json_new_object(); + (void)dc_json_object_set(qr, "width", dc_json_new_int(QW)); + (void)dc_json_object_set(qr, "height", dc_json_new_int(QH)); + (void)dc_json_object_set(qr, "data", dc_json_new_string(b64)); + (void)dc_json_object_set(request, "qr", qr); + *size_out = QW; + free(b64); + return request; +} + +static int +request_checks(void) +{ + struct Answer answer; + DtRequestDialog *dialog; + DcJson *request; + int qr_size; + + memset(&answer, 0, sizeof(answer)); + request = request_json("code", "Telegram", "Code from the app", NULL); + dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, + &answer); + dc_json_free(request); + if (dialog == NULL) + return 0; + dtchat_request_dialog_answer(dialog, "123456"); + if (answer.called != 1 || answer.value == NULL || + strcmp(answer.value, "123456") != 0) + return 0; + free(answer.value); + + memset(&answer, 0, sizeof(answer)); + request = request_json("password", "Account", "Password", "for the demo"); + dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, + &answer); + dc_json_free(request); + if (dialog == NULL) + return 0; + dtchat_request_dialog_answer(dialog, NULL); + if (answer.called != 1 || answer.value != NULL) + return 0; + + memset(&answer, 0, sizeof(answer)); + request = request_json("confirm", "Pairing", "Accept the peer?", NULL); + dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, + &answer); + dc_json_free(request); + if (dialog == NULL) + return 0; + dtchat_request_dialog_answer(dialog, "1"); + if (answer.called != 1 || answer.value == NULL || + strcmp(answer.value, "1") != 0) + return 0; + free(answer.value); + + memset(&answer, 0, sizeof(answer)); + request = request_json("notice", "Session", "Linked.", NULL); + dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, + &answer); + dc_json_free(request); + if (dialog == NULL) + return 0; + dtchat_request_dialog_answer(dialog, "1"); + if (answer.called != 1 || answer.value == NULL) + return 0; + free(answer.value); + + memset(&answer, 0, sizeof(answer)); + request = qr_request(&qr_size); + if (request == NULL) + return 0; + dialog = dtchat_request_dialog_create(toplevel, request, 0, answer_cb, + &answer); + dc_json_free(request); + if (dialog == NULL || dtchat_request_dialog_shell(dialog) == NULL) + return 0; + dtchat_request_dialog_answer(dialog, NULL); + if (answer.called != 1 || answer.value != NULL) + return 0; + + memset(&answer, 0, sizeof(answer)); + request = request_json("code", "Timeout", "Never answered", NULL); + dialog = dtchat_request_dialog_create(toplevel, request, 120, + answer_cb, &answer); + dc_json_free(request); + if (dialog == NULL) + return 0; + { + long long start = (long long)time(NULL); + + while (answer.called == 0 && (long long)time(NULL) - start < 3) + XtAppProcessEvent(app, XtIMAll); + } + if (answer.called != 1 || answer.value != NULL) + return 0; + return 1; +} static DcModel * make_demo_model(void) @@ -154,15 +358,51 @@ smoke_check(XtPointer client, XtIntervalId *id) (void)id; XSync(XtDisplay(toplevel), False); - if (smoke_ui_checks(ui)) { + if (smoke_ui_checks(ui) && request_checks()) { printf("dtchat smoke: PASS\n"); } else { - printf("dtchat smoke: FAIL window, selection, runtime, cap or format\n"); + printf("dtchat smoke: FAIL window, selection, runtime, cap, format or dialogs\n"); smoke_failed = 1; } XtAppSetExitFlag(app); } +static void +demo_request_cb(XtPointer client, XtIntervalId *id) +{ + DtChatUi *ui = client; + const char *kind = demo_kind != NULL ? demo_kind : "qr"; + int qr_size; + + (void)id; + if (strcmp(kind, "error") == 0) { + dtchat_error_dialog(dtchat_ui_shell(ui), "Telegram", + "The adapter could not sign in. Check the code and try again."); + return; + } + if (strcmp(kind, "qr") == 0) { + DcJson *request = qr_request(&qr_size); + + if (request != NULL) { + (void)dtchat_request_dialog_create(dtchat_ui_shell(ui), + request, 0, answer_cb, NULL); + dc_json_free(request); + } + return; + } + { + DcJson *request = request_json(kind, "dtchat", + strcmp(kind, "password") == 0 ? "Password" : + "Code from the app", NULL); + + if (request != NULL) { + (void)dtchat_request_dialog_create(dtchat_ui_shell(ui), + request, 0, answer_cb, NULL); + dc_json_free(request); + } + } +} + int main(int argc, char **argv) { @@ -176,6 +416,7 @@ main(int argc, char **argv) NULL }; DtChatUi *ui; + const char *demo_request = NULL; int smoke = 0; int i; @@ -190,6 +431,14 @@ main(int argc, char **argv) (size_t)(argc - i) * sizeof(argv[0])); argc--; i--; + } else if (strcmp(argv[i], "--demo-request") == 0 && + i + 1 < argc) { + demo_request = argv[i + 1]; + demo_kind = demo_request; + memmove(&argv[i], &argv[i + 2], + (size_t)(argc - i - 1) * sizeof(argv[0])); + argc -= 2; + i--; } } @@ -218,6 +467,10 @@ main(int argc, char **argv) XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); XtAppAddTimeOut(app, 5000, smoke_check, ui); } + if (demo_request != NULL) { + XMapRaised(XtDisplay(toplevel), XtWindow(toplevel)); + XtAppAddTimeOut(app, 700, demo_request_cb, (XtPointer)ui); + } XtAppMainLoop(app); dtchat_ui_destroy(ui); dc_model_free(demo_model); diff --git a/programs/dtchat/ui_main.c b/programs/dtchat/ui_main.c index 26259e94..53311a5b 100644 --- a/programs/dtchat/ui_main.c +++ b/programs/dtchat/ui_main.c @@ -707,6 +707,12 @@ dtchat_ui_destroy(DtChatUi *ui) free(ui); } +Widget +dtchat_ui_shell(const DtChatUi *ui) +{ + return ui != NULL ? ui->shell : NULL; +} + int dtchat_ui_is_viewable(DtChatUi *ui) { diff --git a/programs/dtchat/ui_main.h b/programs/dtchat/ui_main.h index 3f4ba7d2..c4573454 100644 --- a/programs/dtchat/ui_main.h +++ b/programs/dtchat/ui_main.h @@ -16,6 +16,9 @@ typedef struct DtChatUi DtChatUi; DtChatUi *dtchat_ui_create(Widget shell, DcModel *model); void dtchat_ui_destroy(DtChatUi *ui); +/* The toplevel shell, for dialog parents. */ +Widget dtchat_ui_shell(const DtChatUi *ui); + /* True when the shell is realized and its map state is IsViewable. */ int dtchat_ui_is_viewable(DtChatUi *ui); diff --git a/programs/dtchat/ui_request.c b/programs/dtchat/ui_request.c new file mode 100644 index 00000000..8c65d618 --- /dev/null +++ b/programs/dtchat/ui_request.c @@ -0,0 +1,648 @@ +/* + * ui_request.c -- the code/password/notice/confirm/QR dialogs + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * Every dialog maps one adapter "request" object of the v1 protocol. The + * answer is delivered exactly once, whether it comes from a button, from + * cancel, from the timeout or from the window manager's close box. The QR + * bitmap arrives base64-encoded (one byte per module, row-major) and is drawn + * into a depth-1 pixmap; no QR library and no terminal art. + */ +#define _POSIX_C_SOURCE 200809L + +#include "ui_request.h" + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +typedef enum { + R_CODE, + R_PASSWORD, + R_NOTICE, + R_CONFIRM, + R_QR +} RequestKind; + +struct DtRequestDialog { + Display *dpy; + Widget shell; + Widget form; + Widget field; + Widget pixmap_widget; + Pixmap pixmap; + XtIntervalId timer; + DtRequestAnswerFn answer; + void *arg; + char *secret; + int answered; + int secret_mode; +}; + +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 void +secret_verify_cb(Widget w, XtPointer client, XtPointer call) +{ + DtRequestDialog *dialog = client; + XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)call; + char *old; + char *next; + size_t oldlen, start, end, ins, newlen, i; + + (void)w; + if (!cbs->doit) + return; + old = dialog->secret != NULL ? dialog->secret : ""; + oldlen = strlen(old); + start = (size_t)cbs->startPos; + end = (size_t)cbs->endPos; + if (start > oldlen) + start = oldlen; + if (end > oldlen) + end = oldlen; + if (end < start) + end = start; + ins = cbs->text != NULL ? (size_t)cbs->text->length : 0; + newlen = start + ins + (oldlen - end); + next = malloc(newlen + 1); + if (next == NULL) + return; + memcpy(next, old, start); + if (ins > 0 && cbs->text != NULL && cbs->text->ptr != NULL) + memcpy(next + start, cbs->text->ptr, ins); + memcpy(next + start + ins, old + end, oldlen - end); + next[newlen] = '\0'; + free(dialog->secret); + dialog->secret = next; + if (ins > 0 && cbs->text != NULL && cbs->text->ptr != NULL) + for (i = 0; i < ins; i++) + cbs->text->ptr[i] = '*'; +} + +static int +base64_value(int c) +{ + if (c >= 'A' && c <= 'Z') + return c - 'A'; + if (c >= 'a' && c <= 'z') + return c - 'a' + 26; + if (c >= '0' && c <= '9') + return c - '0' + 52; + if (c == '+') + return 62; + if (c == '/') + return 63; + return -1; +} + +/* Decode base64, ignoring whitespace. Returns the number of bytes or -1. */ +static ssize_t +base64_decode(const char *text, unsigned char *out, size_t outlen) +{ + unsigned int acc = 0; + int bits = 0; + size_t written = 0; + + if (text == NULL) + return -1; + for (; *text != '\0'; text++) { + int v; + + if (isspace((unsigned char)*text)) + continue; + if (*text == '=') + break; + v = base64_value((unsigned char)*text); + if (v < 0) + return -1; + acc = (acc << 6) | (unsigned int)v; + bits += 6; + if (bits >= 8) { + bits -= 8; + if (written >= outlen) + return -1; + out[written++] = (unsigned char)((acc >> bits) & 0xff); + } + } + return (ssize_t)written; +} + +static Pixmap +make_qr_pixmap(DtRequestDialog *dialog, const DcJson *qr, int *size_out) +{ + Display *dpy = XtDisplay(dialog->shell); + const DcJson *qr_obj = qr; + int width = (int)dc_json_int(dc_json_get(qr_obj, "width")); + int height = (int)dc_json_int(dc_json_get(qr_obj, "height")); + const char *data = dc_json_string(dc_json_get(qr_obj, "data")); + unsigned char *modules = NULL; + unsigned char *bits = NULL; + ssize_t n; + int scale, pw, ph, stride, x, y; + Pixmap pixmap = None; + GC gc = NULL; + XImage *image = NULL; + + if (width <= 0 || height <= 0 || width > 200 || height > 200 || + data == NULL) + return None; + modules = calloc((size_t)width * (size_t)height, 1); + if (modules == NULL) + return None; + n = base64_decode(data, modules, (size_t)width * (size_t)height); + if (n < (ssize_t)((size_t)width * (size_t)height)) { + free(modules); + return None; + } + scale = 240 / (width > height ? width : height); + if (scale < 1) + scale = 1; + if (scale > 12) + scale = 12; + pw = width * scale; + ph = height * scale; + stride = (pw + 7) / 8; + bits = calloc((size_t)stride * (size_t)ph, 1); + if (bits == NULL) + goto out; + pixmap = XCreatePixmap(dpy, DefaultRootWindow(dpy), (unsigned int)pw, + (unsigned int)ph, 1); + if (pixmap == None) + goto out; + image = XCreateImage(dpy, DefaultVisual(dpy, DefaultScreen(dpy)), 1, + XYBitmap, 0, (char *)bits, (unsigned int)pw, (unsigned int)ph, + 8, 0); + if (image == NULL) + goto out; + for (y = 0; y < ph; y++) { + for (x = 0; x < pw; x++) { + int mx = x / scale; + int my = y / scale; + + XPutPixel(image, x, y, + modules[(size_t)my * (size_t)width + + (size_t)mx] ? 1 : 0); + } + } + gc = XCreateGC(dpy, pixmap, 0, NULL); + if (gc == NULL) + goto out; + XSetForeground(dpy, gc, 1); + XSetBackground(dpy, gc, 0); + XPutImage(dpy, pixmap, gc, image, 0, 0, 0, 0, (unsigned int)pw, + (unsigned int)ph); + *size_out = pw; +out: + if (gc != NULL) + XFreeGC(dpy, gc); + if (image != NULL) { + XDestroyImage(image); /* frees bits */ + bits = NULL; + } + free(bits); + free(modules); + return pixmap; +} + +static void +finish(DtRequestDialog *dialog, const char *value) +{ + DtRequestAnswerFn fn = dialog->answer; + void *arg = dialog->arg; + char *copy = value != NULL ? strdup(value) : NULL; + + if (dialog->answered) + return; + dialog->answered = 1; + if (dialog->timer != 0) { + (void)XtRemoveTimeOut(dialog->timer); + dialog->timer = 0; + } + if (dialog->shell != NULL) { + Widget shell = dialog->shell; + + dialog->shell = NULL; + XtDestroyWidget(shell); + } + dtchat_request_dialog_destroy(dialog); + if (fn != NULL) + fn(copy, arg); + free(copy); +} + +static void +timeout_cb(XtPointer client, XtIntervalId *id) +{ + DtRequestDialog *dialog = client; + + (void)id; + dialog->timer = 0; + finish(dialog, NULL); +} + +static void +ok_cb(Widget w, XtPointer client, XtPointer call) +{ + DtRequestDialog *dialog = client; + char *value = NULL; + const char *answer = NULL; + + (void)w; + (void)call; + if (dialog->field != NULL) { + if (dialog->secret_mode) { + answer = dialog->secret != NULL ? dialog->secret : ""; + } else { + value = XmTextFieldGetString(dialog->field); + answer = value != NULL ? value : ""; + } + } else { + answer = "1"; + } + finish(dialog, answer); + if (value != NULL) + XtFree(value); +} + +static void +cancel_cb(Widget w, XtPointer client, XtPointer call) +{ + (void)w; + (void)call; + finish(client, NULL); +} + +static void +confirm_no_cb(Widget w, XtPointer client, XtPointer call) +{ + (void)w; + (void)call; + finish(client, "0"); +} + +static void +destroyed_cb(Widget w, XtPointer client, XtPointer call) +{ + DtRequestDialog *dialog = client; + + (void)w; + (void)call; + if (dialog != NULL) { + dialog->shell = NULL; + if (!dialog->answered) { + DtRequestAnswerFn fn = dialog->answer; + void *arg = dialog->arg; + + dialog->answered = 1; + if (dialog->timer != 0) { + (void)XtRemoveTimeOut(dialog->timer); + dialog->timer = 0; + } + if (fn != NULL) + fn(NULL, arg); + } + } +} + +static Widget +button(Widget parent, const char *label, const char *name, + XtCallbackProc cb, DtRequestDialog *dialog) +{ + Widget b = XmCreatePushButton(parent, (char *)name, NULL, 0); + + set_label(b, label); + if (cb != NULL) + XtAddCallback(b, XmNactivateCallback, cb, dialog); + XtManageChild(b); + return b; +} + +static RequestKind +request_kind(const char *kind) +{ + if (kind == NULL) + return R_NOTICE; + if (strcmp(kind, "code") == 0) + return R_CODE; + if (strcmp(kind, "password") == 0) + return R_PASSWORD; + if (strcmp(kind, "confirm") == 0) + return R_CONFIRM; + if (strcmp(kind, "qr") == 0) + return R_QR; + return R_NOTICE; +} + +static DtRequestDialog * +dialog_new(Widget shell, DtRequestAnswerFn answer, void *arg) +{ + DtRequestDialog *dialog = calloc(1, sizeof(*dialog)); + + if (dialog == NULL) + return NULL; + dialog->dpy = XtDisplay(shell); + dialog->shell = shell; + dialog->answer = answer; + dialog->arg = arg; + XtAddCallback(shell, XmNdestroyCallback, destroyed_cb, dialog); + return dialog; +} + +DtRequestDialog * +dtchat_request_dialog_create(Widget parent, const DcJson *request, + int timeout_ms, DtRequestAnswerFn answer, void *arg) +{ + DtRequestDialog *dialog; + RequestKind kind; + const char *title; + const char *prompt; + const char *hint; + Widget form, text_label = NULL, buttons; + Widget hint_w = NULL, okb = NULL, cancelb = NULL; + int timeout; + + if (parent == NULL || request == NULL) + return NULL; + kind = request_kind(dc_json_string(dc_json_get(request, "kind"))); + title = dc_json_string(dc_json_get(request, "title")); + prompt = dc_json_string(dc_json_get(request, "prompt")); + hint = dc_json_string(dc_json_get(request, "hint")); + + form = XmCreateFormDialog(parent, "requestDialog", NULL, 0); + if (form == NULL) + return NULL; + dialog = dialog_new(XtParent(form), answer, arg); + if (dialog == NULL) { + XtDestroyWidget(form); + return NULL; + } + dialog->form = form; + XtVaSetValues(dialog->shell, XmNtitle, + (char *)(title != NULL ? title : "dtchat"), XmNwidth, 420, + XmNdeleteResponse, XmDESTROY, NULL); + + if (kind == R_QR) { + const DcJson *qr = dc_json_get(request, "qr"); + int side = 0; + Pixmap pixmap = None; + + if (qr != NULL) + pixmap = make_qr_pixmap(dialog, qr, &side); + if (pixmap != None) { + XmString x = xs(""); + + dialog->pixmap = pixmap; + dialog->pixmap_widget = XmCreateLabel(form, + "qrImage", NULL, 0); + XtVaSetValues(dialog->pixmap_widget, + XmNlabelType, XmPIXMAP, + XmNlabelPixmap, pixmap, + XmNlabelString, x, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNtopOffset, 8, NULL); + XmStringFree(x); + XtManageChild(dialog->pixmap_widget); + } else { + text_label = XmCreateLabel(form, "qrFallback", NULL, 0); + set_label(text_label, + "The adapter sent no usable QR bitmap."); + XtVaSetValues(text_label, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, NULL); + XtManageChild(text_label); + } + if (hint != NULL && *hint != '\0') { + hint_w = XmCreateLabel(form, "qrHint", NULL, 0); + set_label(hint_w, hint); + XtVaSetValues(hint_w, + XmNtopAttachment, XmATTACH_WIDGET, + XmNtopWidget, dialog->pixmap_widget != NULL ? + dialog->pixmap_widget : text_label, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNtopOffset, 8, NULL); + XtManageChild(hint_w); + } + } else { + text_label = XmCreateLabel(form, "prompt", NULL, 0); + set_label(text_label, prompt != NULL ? prompt : + (kind == R_NOTICE ? "" : "Answer the request:")); + XtVaSetValues(text_label, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNalignment, XmALIGNMENT_BEGINNING, + XmNmarginTop, 8, XmNmarginLeft, 8, + XmNmarginRight, 8, NULL); + XtManageChild(text_label); + + if (kind == R_CODE || kind == R_PASSWORD) { + dialog->field = XmCreateTextField(form, "answer", NULL, 0); + XtVaSetValues(dialog->field, + XmNtopAttachment, XmATTACH_WIDGET, + XmNtopWidget, text_label, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNtopOffset, 8, XmNleftOffset, 8, + XmNrightOffset, 8, XmNcolumns, 28, NULL); + if (kind == R_PASSWORD) { + dialog->secret_mode = 1; + dialog->secret = strdup(""); + XtAddCallback(dialog->field, + XmNmodifyVerifyCallback, secret_verify_cb, + dialog); + } + XtManageChild(dialog->field); + } + if (hint != NULL && *hint != '\0') { + hint_w = XmCreateLabel(form, "hint", NULL, 0); + set_label(hint_w, hint); + XtVaSetValues(hint_w, + XmNtopAttachment, XmATTACH_WIDGET, + XmNtopWidget, dialog->field != NULL ? + dialog->field : text_label, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNalignment, XmALIGNMENT_BEGINNING, + XmNmarginLeft, 8, XmNmarginRight, 8, + XmNtopOffset, 8, NULL); + XtManageChild(hint_w); + } + } + + buttons = XtVaCreateManagedWidget("buttons", xmFormWidgetClass, form, + XmNbottomAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNheight, 38, XmNmarginHeight, 4, NULL); + switch (kind) { + case R_NOTICE: + okb = button(buttons, "OK", "ok", ok_cb, dialog); + break; + case R_QR: + cancelb = button(buttons, "Cancel", "cancel", cancel_cb, dialog); + break; + case R_CONFIRM: + cancelb = button(buttons, "No", "no", confirm_no_cb, dialog); + okb = button(buttons, "Yes", "yes", ok_cb, dialog); + break; + case R_CODE: + case R_PASSWORD: + cancelb = button(buttons, "Cancel", "cancel", cancel_cb, dialog); + okb = button(buttons, "OK", "ok", ok_cb, dialog); + break; + } + if (cancelb != NULL) + XtVaSetValues(cancelb, + XmNrightAttachment, XmATTACH_FORM, + XmNbottomAttachment, XmATTACH_FORM, + XmNrightOffset, 6, XmNbottomOffset, 4, + XmNwidth, 88, NULL); + if (okb != NULL) { + XtVaSetValues(okb, + XmNrightAttachment, cancelb != NULL ? XmATTACH_WIDGET : + XmATTACH_FORM, + XmNrightWidget, cancelb, + XmNbottomAttachment, XmATTACH_FORM, + XmNrightOffset, 6, XmNbottomOffset, 4, + XmNwidth, 88, NULL); + } + if (kind == R_QR && hint_w != NULL) + XtVaSetValues(hint_w, + XmNbottomAttachment, XmATTACH_WIDGET, + XmNbottomWidget, buttons, NULL); + + timeout = timeout_ms; + if (timeout > 0) + dialog->timer = XtAppAddTimeOut( + XtWidgetToApplicationContext(parent), (unsigned long)timeout, + timeout_cb, dialog); + + XtManageChild(form); + XtVaSetValues(dialog->shell, XmNwidth, 340, + XmNheight, kind == R_QR ? 330 : 200, NULL); + XtManageChild(form); + XtPopup(dialog->shell, XtGrabNone); + if (dialog->field != NULL) + XmProcessTraversal(dialog->field, XmTRAVERSE_CURRENT); + return dialog; +} + +void +dtchat_request_dialog_answer(DtRequestDialog *dialog, const char *value) +{ + if (dialog == NULL) + return; + finish(dialog, value); +} + +void +dtchat_request_dialog_destroy(DtRequestDialog *dialog) +{ + if (dialog == NULL) + return; + if (dialog->timer != 0) + (void)XtRemoveTimeOut(dialog->timer); + if (dialog->shell != NULL) { + Widget shell = dialog->shell; + + dialog->shell = NULL; + XtDestroyWidget(shell); + } + if (dialog->pixmap != None) + XFreePixmap(dialog->dpy, dialog->pixmap); + free(dialog->secret); + free(dialog); +} + +Widget +dtchat_request_dialog_shell(const DtRequestDialog *dialog) +{ + return dialog != NULL ? dialog->shell : NULL; +} + +static void +error_close_cb(Widget w, XtPointer client, XtPointer call) +{ + DtRequestDialog *dialog = client; + + (void)w; + (void)call; + if (dialog->shell != NULL) { + Widget shell = dialog->shell; + + dialog->shell = NULL; + XtDestroyWidget(shell); + } + dtchat_request_dialog_destroy(dialog); +} + +void +dtchat_error_dialog(Widget parent, const char *title, const char *text) +{ + DtRequestDialog *dialog; + Widget form, label, buttons; + + if (parent == NULL) + return; + form = XmCreateFormDialog(parent, "errorDialog", NULL, 0); + if (form == NULL) + return; + dialog = dialog_new(XtParent(form), NULL, NULL); + if (dialog == NULL) { + XtDestroyWidget(form); + return; + } + dialog->form = form; + XtVaSetValues(dialog->shell, XmNtitle, + (char *)(title != NULL ? title : "dtchat"), XmNwidth, 380, + XmNdeleteResponse, XmDESTROY, NULL); + label = XmCreateLabel(form, "errorText", NULL, 0); + set_label(label, text != NULL ? text : "Unknown error"); + XtVaSetValues(label, + XmNtopAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNalignment, XmALIGNMENT_BEGINNING, + XmNmarginTop, 10, XmNmarginLeft, 10, XmNmarginRight, 10, NULL); + XtManageChild(label); + buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, + form, XmNorientation, XmHORIZONTAL, + XmNbottomAttachment, XmATTACH_FORM, + XmNleftAttachment, XmATTACH_FORM, + XmNrightAttachment, XmATTACH_FORM, + XmNmarginHeight, 6, NULL); + (void)button(buttons, "Dismiss", "dismiss", error_close_cb, dialog); + XtManageChild(form); + XtPopup(dialog->shell, XtGrabNone); +} diff --git a/programs/dtchat/ui_request.h b/programs/dtchat/ui_request.h new file mode 100644 index 00000000..6cf3349e --- /dev/null +++ b/programs/dtchat/ui_request.h @@ -0,0 +1,38 @@ +/* + * ui_request.h -- the request, error and QR dialogs of dtchat + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + */ +#ifndef _Dtchat_ui_request_h +#define _Dtchat_ui_request_h + +#include + +#include "dc_internal.h" + +typedef struct DtRequestDialog DtRequestDialog; + +/* + * Called exactly once: with the answer text, or with NULL for cancel, + * timeout and window-manager close. The dialog destroys itself before the + * callback returns, so the callback may open the next dialog. + */ +typedef void (*DtRequestAnswerFn)(const char *value, void *arg); + +/* + * Build the dialog for one adapter "request" object of the v1 protocol + * (kind code/password/notice/confirm/qr). TIMEOUT_MS <= 0 means no + * timeout. Returns NULL for an unknown kind or an allocation failure. + */ +DtRequestDialog *dtchat_request_dialog_create(Widget parent, + const DcJson *request, int timeout_ms, DtRequestAnswerFn answer, void *arg); +void dtchat_request_dialog_destroy(DtRequestDialog *dialog); +Widget dtchat_request_dialog_shell(const DtRequestDialog *dialog); + +/* Same path as the buttons: answers VALUE (NULL = cancel) once. */ +void dtchat_request_dialog_answer(DtRequestDialog *dialog, const char *value); + +/* An error/notice window with one Dismiss button; it frees itself. */ +void dtchat_error_dialog(Widget parent, const char *title, const char *text); + +#endif /* _Dtchat_ui_request_h */