/* * ui_file.c -- the local file dialogs * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The offer dialog names the peer, the file and its size and asks once; the * answer goes back as one call (Accept or Reject, the window manager's close * counts as Reject). The send dialog is a Motif file selection box; the * chosen path is handed to the local manager, which does the transfer. */ #define _POSIX_C_SOURCE 200809L #include "ui_file.h" #include "ui_text.h" #include #include #include #include #include #include #include #include #include #include struct DtFileOfferDialog { Widget shell; Widget form; DtFileOfferAnswerFn answer; void *arg; int answered; XtIntervalId timer; }; typedef struct { char *peer_id; DtFileSendFn fn; void *arg; } SendCtx; static void offer_size_cb(XtPointer client, XtIntervalId *id) { DtFileOfferDialog *dialog = client; (void)id; dialog->timer = 0; if (dialog->shell == NULL || dialog->form == NULL) return; XtVaSetValues(dialog->form, XmNwidth, 460, XmNheight, 190, NULL); XtVaSetValues(dialog->shell, XmNwidth, 460, XmNheight, 190, NULL); } 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 human_size(long long size, char *out, size_t outlen) { if (size >= 1024LL * 1024 * 1024) snprintf(out, outlen, "%.1f GB", (double)size / (1024.0 * 1024 * 1024)); else if (size >= 1024LL * 1024) snprintf(out, outlen, "%.1f MB", (double)size / (1024.0 * 1024)); else if (size >= 1024) snprintf(out, outlen, "%.1f KB", (double)size / 1024.0); else snprintf(out, outlen, "%lld bytes", size); } static void answer(DtFileOfferDialog *dialog, int accepted) { if (dialog->answered) return; dialog->answered = 1; if (dialog->answer != NULL) dialog->answer(accepted, dialog->arg); dtchat_file_offer_dialog_destroy(dialog); } static void accept_cb(Widget w, XtPointer client, XtPointer call) { (void)w; (void)call; answer(client, 1); } static void reject_cb(Widget w, XtPointer client, XtPointer call) { (void)w; (void)call; answer(client, 0); } DtFileOfferDialog * dtchat_file_offer_dialog(Widget parent, const char *peer, const char *name, long long size, DtFileOfferAnswerFn answer, void *arg) { DtFileOfferDialog *dialog; Widget form, who, text, buttons, accept, reject; char line[1024]; char human[64]; if (parent == NULL) return NULL; dialog = calloc(1, sizeof(*dialog)); if (dialog == NULL) return NULL; dialog->answer = answer; dialog->arg = arg; form = XmCreateFormDialog(parent, "fileOffer", NULL, 0); if (form == NULL) { free(dialog); return NULL; } dialog->shell = XtParent(form); dialog->form = form; XtVaSetValues(dialog->shell, XmNtitle, (char *)dc_text(TX_INCOMING_FILE, "Incoming file"), XmNwidth, 460, XmNheight, 190, XmNdeleteResponse, XmDESTROY, NULL); human_size(size, human, sizeof(human)); snprintf(line, sizeof(line), "%s offers", peer != NULL ? peer : "A peer"); who = XmCreateLabel(form, "who", NULL, 0); set_label(who, line); XtVaSetValues(who, XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNalignment, XmALIGNMENT_BEGINNING, XmNmarginTop, 12, XmNmarginLeft, 10, NULL); XtManageChild(who); snprintf(line, sizeof(line), "%s (%s)", name != NULL ? name : "a file", human); text = XmCreateLabel(form, "file", NULL, 0); set_label(text, line); XtVaSetValues(text, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, who, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNalignment, XmALIGNMENT_BEGINNING, XmNmarginLeft, 10, XmNtopOffset, 6, NULL); XtManageChild(text); buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, form, XmNorientation, XmHORIZONTAL, XmNspacing, 8, XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, XmNmarginHeight, 8, NULL); reject = XmCreatePushButton(buttons, "reject", NULL, 0); set_label(reject, dc_text(TX_REJECT, "Reject")); XtAddCallback(reject, XmNactivateCallback, reject_cb, dialog); XtManageChild(reject); accept = XmCreatePushButton(buttons, "accept", NULL, 0); set_label(accept, dc_text(TX_ACCEPT, "Accept")); XtAddCallback(accept, XmNactivateCallback, accept_cb, dialog); XtManageChild(accept); XtManageChild(form); XtVaSetValues(form, XmNwidth, 460, XmNheight, 190, NULL); XtPopup(dialog->shell, XtGrabNone); XtVaSetValues(dialog->shell, XmNwidth, 460, XmNheight, 190, NULL); dialog->timer = XtAppAddTimeOut( XtWidgetToApplicationContext(form), 50, offer_size_cb, dialog); return dialog; } void dtchat_file_offer_dialog_destroy(DtFileOfferDialog *dialog) { if (dialog == NULL) return; if (dialog->timer != 0) { (void)XtRemoveTimeOut(dialog->timer); dialog->timer = 0; } if (dialog->shell != NULL) { Widget shell = dialog->shell; dialog->shell = NULL; XtDestroyWidget(shell); } free(dialog); } Widget dtchat_file_offer_dialog_shell(const DtFileOfferDialog *dialog) { return dialog != NULL ? dialog->shell : NULL; } static void send_ok_cb(Widget w, XtPointer client, XtPointer call) { SendCtx *ctx = client; XmFileSelectionBoxCallbackStruct *cbs = call; char *path = NULL; (void)w; if (cbs->value != NULL) (void)XmStringGetLtoR(cbs->value, XmFONTLIST_DEFAULT_TAG, &path); if (path != NULL && ctx->fn != NULL) ctx->fn(ctx->peer_id, path, ctx->arg); XtFree(path); XtDestroyWidget(XtParent(w)); free(ctx->peer_id); free(ctx); } static void send_cancel_cb(Widget w, XtPointer client, XtPointer call) { SendCtx *ctx = client; (void)client; (void)call; XtDestroyWidget(XtParent(w)); free(ctx->peer_id); free(ctx); } void dtchat_file_send_dialog(Widget parent, const char *peer_id, const char *peer_name, DtFileSendFn fn, void *arg) { SendCtx *ctx; Widget box; char title[256]; if (parent == NULL || peer_id == NULL) return; ctx = calloc(1, sizeof(*ctx)); if (ctx == NULL) return; ctx->peer_id = strdup(peer_id); ctx->fn = fn; ctx->arg = arg; if (ctx->peer_id == NULL) { free(ctx); return; } box = XmCreateFileSelectionDialog(parent, "sendFile", NULL, 0); if (box == NULL) { free(ctx->peer_id); free(ctx); return; } snprintf(title, sizeof(title), dc_text(TX_SEND_FILE_TO, "Send a file to %s"), peer_name != NULL ? peer_name : peer_id); XtVaSetValues(XtParent(box), XmNtitle, title, XmNwidth, 560, XmNheight, 420, NULL); XtAddCallback(box, XmNokCallback, send_ok_cb, ctx); XtAddCallback(box, XmNcancelCallback, send_cancel_cb, ctx); XtManageChild(box); XtPopup(XtParent(box), XtGrabNone); }