Files
mcde/programs/dtchat/ui_file.c
T
lotherk f46a914c73 dtchat: add the message catalog and the German UI (T10.4)
i18n.c opens catopen("dtchat", NL_CAT_LOCALE) and dc_text(id, fallback) reads set 1; the program calls dc_i18n_init after setlocale. The visible labels of the main window, the account editor, the request/peer/file/pair dialogs and the local status texts use ui_text.h ids; dtchat.msg (English) has 75 entries and programs/localized/de_DE.UTF-8/msg/dtchat.msg the German copy (ASCII transliteration, core font). test_i18n builds both catalogs with gencat and checks File/Datei, Send/Senden and the fallback; the German run under Xvfb shows Datei/Bearbeiten/Ansicht/Konversation/Hilfe, In der Naehe and Senden (screenshot build/mcde-shots/dtchat-t104-de.png) and the smoke stays green. Dynamic formatted statuses are partly still English. The in-tree build is pending: a foreign release chain holds the lock (pid 141722).
2026-11-17 07:06:55 +00:00

252 lines
6.1 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Xm.h>
struct DtFileOfferDialog {
Widget shell;
DtFileOfferAnswerFn answer;
void *arg;
int answered;
};
typedef struct {
char *peer_id;
DtFileSendFn fn;
void *arg;
} SendCtx;
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);
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);
XtPopup(dialog->shell, XtGrabNone);
return dialog;
}
void
dtchat_file_offer_dialog_destroy(DtFileOfferDialog *dialog)
{
if (dialog == NULL)
return;
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);
}