Files
mcde/programs/dtchat/ui_main.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

1343 lines
34 KiB
C

/*
* ui_main.c -- dtchat's main window and the T2.2 roster binding
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The roster is a flat XmList: one header per account, then one row per
* conversation with its presence mark and unread count. Selecting a row
* shows the conversation title and the last messages in the read-only
* transcript, then clears the unread count. The model is borrowed; the
* caller owns it.
*/
#define _POSIX_C_SOURCE 200809L
#include "ui_main.h"
#include "ui_accounts.h"
#include "ui_text.h"
#include "ui_peer.h"
#include "ui_file.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <Xm/CascadeB.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/PanedW.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/Separator.h>
#include <Xm/Text.h>
#include <Xm/Xm.h>
typedef struct {
char *account;
char *conv; /* NULL for an account header */
char *peer; /* set for a Nearby peer row */
} RosterRow;
struct DtChatUi {
Widget shell;
Widget roster;
Widget title;
Widget transcript;
Widget input;
Widget status;
DcModel *model;
RosterRow *rows;
size_t nrows;
char *cur_account;
char *cur_conv;
char *cur_peer;
DcPeers *peers;
DtChatFileSendFn file_fn;
void *file_arg;
size_t shown;
int selecting;
DtAccountsDialog *accounts;
DtChatSendFn send_fn;
void *send_arg;
char *history[64];
size_t nhistory;
size_t hist_pos;
char *draft;
};
typedef struct {
DtChatUi *ui;
const char *what;
} NotImpl;
static XmString
xs(const char *s)
{
return XmStringCreateLocalized((char *)s);
}
static void
set_status(DtChatUi *ui, const char *text)
{
XmString x = xs(text);
XtVaSetValues(ui->status, XmNlabelString, x, NULL);
XmStringFree(x);
}
void
dtchat_ui_set_status(DtChatUi *ui, const char *text)
{
if (ui == NULL || text == NULL)
return;
set_status(ui, text);
}
static void
quit_cb(Widget w, XtPointer client, XtPointer call)
{
(void)client;
(void)call;
XtAppSetExitFlag(XtWidgetToApplicationContext(w));
}
static void
not_implemented_cb(Widget w, XtPointer client, XtPointer call)
{
NotImpl *ni = client;
char line[160];
(void)w;
(void)call;
snprintf(line, sizeof(line), "%s is not implemented yet", ni->what);
set_status(ni->ui, line);
}
static XtPointer
not_impl(DtChatUi *ui, const char *what)
{
NotImpl *ni = malloc(sizeof(*ni));
if (ni == NULL)
return NULL;
ni->ui = ui;
ni->what = what;
return ni;
}
static void
clear_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
(void)w;
(void)call;
XmTextSetString(ui->transcript, "");
set_status(ui, dc_text(TX_TRANSCRIPT_CLEARED, "transcript cleared"));
}
static void
about_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
(void)w;
(void)call;
set_status(ui, "dtchat 0.2 - MCDE chat (T2.2 roster)");
}
static void
send_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
char *text;
(void)w;
(void)call;
text = XmTextGetString(ui->input);
if (text == NULL || *text == '\0') {
XtFree(text);
return;
}
if (dtchat_ui_send_text(ui, text) != 0)
set_status(ui, "no conversation selected");
else
XmTextSetString(ui->input, "");
XtFree(text);
}
void
dtchat_ui_clear_input(DtChatUi *ui)
{
if (ui != NULL)
XmTextSetString(ui->input, "");
}
void
dtchat_ui_set_input(DtChatUi *ui, const char *text)
{
if (ui == NULL)
return;
XmTextSetString(ui->input, (char *)(text != NULL ? text : ""));
XmTextSetInsertionPosition(ui->input,
XmTextGetLastPosition(ui->input));
}
int
dtchat_ui_input_equals(const DtChatUi *ui, const char *text)
{
char *value;
int same;
if (ui == NULL)
return 0;
value = XmTextGetString(ui->input);
if (value == NULL)
return text == NULL || *text == '\0';
same = strcmp(value, text != NULL ? text : "") == 0;
XtFree(value);
return same;
}
static void
history_add(DtChatUi *ui, const char *text)
{
if (ui == NULL || text == NULL || *text == '\0')
return;
if (ui->nhistory > 0 &&
strcmp(ui->history[ui->nhistory - 1], text) == 0)
return;
if (ui->nhistory == 64) {
free(ui->history[0]);
memmove(&ui->history[0], &ui->history[1],
63 * sizeof(ui->history[0]));
ui->nhistory--;
}
ui->history[ui->nhistory] = strdup(text);
if (ui->history[ui->nhistory] != NULL)
ui->nhistory++;
ui->hist_pos = ui->nhistory;
free(ui->draft);
ui->draft = NULL;
}
int
dtchat_ui_history(DtChatUi *ui, int direction)
{
if (ui == NULL || ui->nhistory == 0)
return 0;
if (ui->hist_pos > ui->nhistory)
ui->hist_pos = ui->nhistory;
if (direction < 0) {
if (ui->hist_pos == ui->nhistory) {
char *text = XmTextGetString(ui->input);
free(ui->draft);
ui->draft = text != NULL ? strdup(text) : NULL;
if (text != NULL)
XtFree(text);
}
if (ui->hist_pos == 0)
return 0;
ui->hist_pos--;
XmTextSetString(ui->input, ui->history[ui->hist_pos]);
} else {
if (ui->hist_pos >= ui->nhistory)
return 0;
ui->hist_pos++;
if (ui->hist_pos == ui->nhistory)
XmTextSetString(ui->input,
ui->draft != NULL ? ui->draft : "");
else
XmTextSetString(ui->input,
ui->history[ui->hist_pos]);
}
XmTextSetInsertionPosition(ui->input,
XmTextGetLastPosition(ui->input));
return 1;
}
int
dtchat_ui_complete(DtChatUi *ui)
{
DcConv *conv;
char *text;
char *word;
char *best = NULL;
size_t wordlen, start, i;
long pos;
int matches = 0;
if (ui == NULL || ui->cur_account == NULL || ui->cur_conv == NULL)
return 0;
conv = dc_model_find_conv(ui->model, ui->cur_account, ui->cur_conv);
if (conv == NULL)
return 0;
text = XmTextGetString(ui->input);
if (text == NULL)
return 0;
pos = XmTextGetInsertionPosition(ui->input);
if (pos < 0)
pos = (long)strlen(text);
start = (size_t)pos;
while (start > 0 && text[start - 1] != ' ' && text[start - 1] != '\n' &&
text[start - 1] != '\t')
start--;
wordlen = (size_t)pos - start;
word = malloc(wordlen + 1);
if (word == NULL) {
XtFree(text);
return 0;
}
memcpy(word, text + start, wordlen);
word[wordlen] = '\0';
if (wordlen > 0) {
for (i = 0; i < dc_conv_message_count(conv); i++) {
const DcMessage *msg = dc_conv_message_at(conv, i);
if (msg == NULL || msg->from == NULL ||
strcmp(msg->from, "me") == 0 ||
(msg->flags & DTCHAT_MSG_SYSTEM) != 0)
continue;
if (strncmp(msg->from, word, wordlen) != 0)
continue;
matches++;
if (best == NULL || strcmp(msg->from, best) < 0) {
free(best);
best = strdup(msg->from);
}
}
}
if (best != NULL) {
size_t len = strlen(best);
char *next = malloc(start + len + 2);
if (next != NULL) {
memcpy(next, text, start);
memcpy(next + start, best, len);
next[start + len] = ' ';
next[start + len + 1] = '\0';
XmTextSetString(ui->input, next);
XmTextSetInsertionPosition(ui->input,
(XmTextPosition)(start + len + 1));
free(next);
}
free(best);
}
free(word);
XtFree(text);
return matches > 0;
}
static void
input_key(Widget w, XtPointer client, XEvent *event, Boolean *cont)
{
DtChatUi *ui = client;
KeySym key;
if (event->type != KeyPress)
return;
key = XLookupKeysym(&event->xkey, 0);
if ((key == XK_Return || key == XK_KP_Enter) &&
(event->xkey.state & ShiftMask) == 0) {
send_cb(w, client, NULL);
*cont = False;
} else if (key == XK_Up) {
(void)dtchat_ui_history(ui, -1);
*cont = False;
} else if (key == XK_Down) {
(void)dtchat_ui_history(ui, 1);
*cont = False;
} else if (key == XK_Tab) {
(void)dtchat_ui_complete(ui);
*cont = False;
} else if (key == XK_Escape) {
dtchat_ui_clear_input(ui);
*cont = False;
}
}
/*
* XmCreateScrolledText uses XmAPPLICATION_DEFINED, so the caller owns the
* text size. Fit the text to the viewport on every resize (wordWrap then
* wraps at the real width) and drop the horizontal bar, which cannot be
* needed once the text is viewport-wide.
*/
static void
fit_scrolled_text(Widget sw, Widget text)
{
Dimension sw_w = 0, sw_h = 0;
Dimension width, height;
Dimension shadow = 0;
Dimension bar = 0;
Dimension vwidth = 0;
Widget vbar = NULL;
XtVaGetValues(sw, XmNwidth, &sw_w, XmNheight, &sw_h,
XmNshadowThickness, &shadow, XmNverticalScrollBar, &vbar, NULL);
if (vbar != NULL && XtIsManaged(vbar)) {
XtVaGetValues(vbar, XmNwidth, &vwidth, NULL);
bar = vwidth + shadow;
}
width = sw_w > bar + shadow ? sw_w - bar - shadow : 1;
height = sw_h > 2 * shadow ? sw_h - 2 * shadow : 1;
XtVaSetValues(text, XmNwidth, width, XmNheight, height, NULL);
}
static void
scrolled_resize(Widget w, XtPointer client, XEvent *event, Boolean *cont)
{
(void)event;
(void)cont;
fit_scrolled_text(w, client);
}
static Widget
make_scrolled_text(Widget parent, const char *name, int rows, int columns,
int wrap, Widget *sw_out)
{
char sw_name[64];
Widget sw;
Widget text;
Widget hbar = NULL;
Arg args[8];
int n = 0;
snprintf(sw_name, sizeof(sw_name), "%sSW", name);
/*
* XmNscrollHorizontal is a secondary XmText resource: it has to be
* in the creation arg list. XmText only word-wraps while it is off.
*/
XtSetArg(args[n], XmNscrollHorizontal, False); n++;
XtSetArg(args[n], XmNwordWrap, wrap ? True : False); n++;
XtSetArg(args[n], XmNrows, rows); n++;
XtSetArg(args[n], XmNcolumns, columns); n++;
XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
XtSetArg(args[n], XmNmarginWidth, 6); n++;
XtSetArg(args[n], XmNmarginHeight, 6); n++;
text = XmCreateScrolledText(parent, (char *)name, args, (Cardinal)n);
sw = XtParent(text);
XtAddEventHandler(sw, StructureNotifyMask, False, scrolled_resize,
text);
XtVaGetValues(sw, XmNhorizontalScrollBar, &hbar, NULL);
if (hbar != NULL)
XtUnmanageChild(hbar);
XtManageChild(text);
*sw_out = sw;
return text;
}
static Widget
make_menu(Widget bar, const char *label)
{
static int menu_no;
char menu_name[32], cascade_name[32];
Widget menu, cascade;
XmString x;
snprintf(menu_name, sizeof(menu_name), "menu%d", ++menu_no);
snprintf(cascade_name, sizeof(cascade_name), "cascade%d", menu_no);
menu = XmCreatePulldownMenu(bar, menu_name, NULL, 0);
cascade = XmCreateCascadeButton(bar, cascade_name, NULL, 0);
x = xs(label);
XtVaSetValues(cascade, XmNlabelString, x, XmNsubMenuId, menu, NULL);
XmStringFree(x);
XtManageChild(cascade);
return menu;
}
static Widget
make_item(Widget menu, const char *label, XtCallbackProc cb, XtPointer data)
{
static int item_no;
char name[32];
Widget item;
XmString x;
snprintf(name, sizeof(name), "item%d", ++item_no);
item = XmCreatePushButton(menu, name, NULL, 0);
x = xs(label);
XtVaSetValues(item, XmNlabelString, x, NULL);
XmStringFree(x);
XtManageChild(item);
if (cb != NULL)
XtAddCallback(item, XmNactivateCallback, cb, data);
return item;
}
static void
make_separator(Widget menu)
{
static int sep_no;
char name[32];
Widget sep;
snprintf(name, sizeof(name), "sep%d", ++sep_no);
sep = XmCreateSeparator(menu, name, NULL, 0);
XtManageChild(sep);
}
static DcJson *
schema_field(const char *key, const char *label, const char *type,
int required)
{
DcJson *entry = dc_json_new_object();
(void)dc_json_object_set(entry, "key", dc_json_new_string(key));
(void)dc_json_object_set(entry, "label", dc_json_new_string(label));
(void)dc_json_object_set(entry, "type", dc_json_new_string(type));
if (required)
(void)dc_json_object_set(entry, "required",
dc_json_new_bool(1));
return entry;
}
static DcJson *
demo_schema(void)
{
DcJson *schema = dc_json_new_array();
DcJson *entry;
DcJson *choices;
entry = schema_field("host", "Server", "text", 1);
(void)dc_json_object_set(entry, "default",
dc_json_new_string("irc.libera.chat"));
(void)dc_json_array_append(schema, entry);
entry = schema_field("port", "Port", "int", 0);
(void)dc_json_object_set(entry, "default", dc_json_new_int(6697));
(void)dc_json_array_append(schema, entry);
entry = schema_field("tls", "TLS", "bool", 0);
(void)dc_json_object_set(entry, "default", dc_json_new_bool(1));
(void)dc_json_array_append(schema, entry);
entry = schema_field("nick", "Nickname", "text", 1);
(void)dc_json_array_append(schema, entry);
entry = schema_field("password", "Password", "secret", 0);
(void)dc_json_array_append(schema, entry);
entry = schema_field("encoding", "Encoding", "choice", 0);
choices = dc_json_new_array();
{
DcJson *utf8 = dc_json_new_object();
DcJson *latin1 = dc_json_new_object();
(void)dc_json_object_set(utf8, "value",
dc_json_new_string("utf-8"));
(void)dc_json_object_set(utf8, "label",
dc_json_new_string("UTF-8"));
(void)dc_json_object_set(latin1, "value",
dc_json_new_string("latin1"));
(void)dc_json_object_set(latin1, "label",
dc_json_new_string("ISO-8859-1"));
(void)dc_json_array_append(choices, utf8);
(void)dc_json_array_append(choices, latin1);
}
(void)dc_json_object_set(entry, "choices", choices);
(void)dc_json_object_set(entry, "default",
dc_json_new_string("utf-8"));
(void)dc_json_array_append(schema, entry);
return schema;
}
static void
accounts_closed(void *arg)
{
DtChatUi *ui = arg;
ui->accounts = NULL;
dtchat_ui_refresh(ui);
}
static void
accounts_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
DcJson *schema;
(void)w;
(void)call;
if (ui->accounts != NULL) {
Widget shell = dtchat_accounts_dialog_shell(ui->accounts);
XMapRaised(XtDisplay(shell), XtWindow(shell));
return;
}
schema = demo_schema();
if (schema == NULL)
return;
ui->accounts = dtchat_accounts_dialog_create(ui->shell, ui->model,
schema, accounts_closed, ui);
dc_json_free(schema);
if (ui->accounts == NULL)
set_status(ui, "could not open the account editor");
}
static void
send_file_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
DcPeer *peer;
(void)w;
(void)call;
if (ui->cur_peer == NULL || ui->peers == NULL) {
set_status(ui, dc_text(TX_SELECT_PEER, "select a Peer in Nearby first"));
return;
}
peer = dc_peers_find(ui->peers, ui->cur_peer);
if (peer == NULL) {
set_status(ui, dc_text(TX_PEER_GONE, "peer is gone"));
return;
}
if (ui->file_fn == NULL) {
set_status(ui, dc_text(TX_LOCAL_NOT_CONNECTED, "local mode is not connected"));
return;
}
dtchat_file_send_dialog(ui->shell, dc_peer_id(peer),
dc_peer_name(peer), ui->file_fn, ui->file_arg);
}
static void
make_menu_bar(DtChatUi *ui, Widget form, Widget *bar_out)
{
Widget bar = XmCreateMenuBar(form, "menuBar", NULL, 0);
Widget file, edit, view, conversation, help;
XtVaSetValues(bar, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
XtManageChild(bar);
*bar_out = bar;
file = make_menu(bar, dc_text(TX_FILE, "File"));
(void)make_item(file, dc_text(TX_NEW_CONV, "New Conversation"), not_implemented_cb,
not_impl(ui, "New conversation"));
(void)make_item(file, dc_text(TX_ACCOUNTS_ELLIPSIS, "Accounts..."), accounts_cb, ui);
make_separator(file);
(void)make_item(file, dc_text(TX_QUIT, "Quit"), quit_cb, NULL);
edit = make_menu(bar, dc_text(TX_EDIT, "Edit"));
(void)make_item(edit, dc_text(TX_CUT, "Cut"), not_implemented_cb,
not_impl(ui, "Cut"));
(void)make_item(edit, dc_text(TX_COPY, "Copy"), not_implemented_cb,
not_impl(ui, "Copy"));
(void)make_item(edit, dc_text(TX_PASTE, "Paste"), not_implemented_cb,
not_impl(ui, "Paste"));
make_separator(edit);
(void)make_item(edit, dc_text(TX_CLEAR, "Clear Transcript"), clear_cb, ui);
view = make_menu(bar, dc_text(TX_VIEW, "View"));
(void)make_item(view, dc_text(TX_SHOW_OFFLINE, "Show Offline"), not_implemented_cb,
not_impl(ui, "Show offline"));
(void)make_item(view, dc_text(TX_TIMESTAMPS, "Timestamps"), not_implemented_cb,
not_impl(ui, "Timestamps"));
conversation = make_menu(bar, dc_text(TX_CONVERSATION, "Conversation"));
(void)make_item(conversation, dc_text(TX_JOIN, "Join Channel"), not_implemented_cb,
not_impl(ui, "Join a channel"));
(void)make_item(conversation, dc_text(TX_SEND_FILE, "Send File"), send_file_cb, ui);
(void)make_item(conversation, dc_text(TX_CALL, "Call"), not_implemented_cb,
not_impl(ui, "Call"));
help = make_menu(bar, dc_text(TX_HELP, "Help"));
(void)make_item(help, dc_text(TX_ABOUT, "About dtchat"), about_cb, ui);
}
static char
presence_mark(DcPresence presence)
{
switch (presence) {
case DC_PRESENCE_ONLINE: return '*';
case DC_PRESENCE_AWAY: return '~';
case DC_PRESENCE_DND: return '!';
case DC_PRESENCE_OFFLINE:
default: return 'o';
}
}
static void
format_time(long long ts, char *out, size_t len)
{
struct tm tm;
time_t t = (time_t)ts;
if (ts <= 0 || localtime_r(&t, &tm) == NULL) {
snprintf(out, len, "--:--");
return;
}
strftime(out, len, "%H:%M", &tm);
}
static void
append_message(DtChatUi *ui, const DcMessage *msg)
{
char when[8];
char *line;
size_t len;
long long pos;
format_time(msg->ts, when, sizeof(when));
len = strlen(msg->from) + strlen(msg->text) + 40;
line = malloc(len);
if (line == NULL)
return;
if (msg->flags & DTCHAT_MSG_SYSTEM)
snprintf(line, len, "-- %s\n", msg->text);
else if (msg->flags & DTCHAT_MSG_NOTICE)
snprintf(line, len, "[%s] -%s- %s\n", when, msg->from,
msg->text);
else if (msg->flags & DTCHAT_MSG_ACTION)
snprintf(line, len, "[%s] * %s %s\n", when, msg->from,
msg->text);
else
snprintf(line, len, "[%s] <%s> %s\n", when, msg->from,
msg->text);
pos = XmTextGetLastPosition(ui->transcript);
XmTextInsert(ui->transcript, pos, line);
XmTextShowPosition(ui->transcript, XmTextGetLastPosition(ui->transcript));
ui->shown++;
free(line);
}
static void refresh_roster(DtChatUi *ui);
static void
show_conversation(DtChatUi *ui, const char *account, const char *conv_id)
{
DcConv *conv = dc_model_find_conv(ui->model, account, conv_id);
size_t i;
char title[512];
if (conv == NULL)
return;
free(ui->cur_account);
free(ui->cur_conv);
ui->cur_account = strdup(account);
ui->cur_conv = strdup(conv_id);
if (ui->cur_account == NULL || ui->cur_conv == NULL)
return;
if (dc_conv_topic(conv) != NULL && *dc_conv_topic(conv) != '\0')
snprintf(title, sizeof(title), "%s - %s - %s",
dc_conv_id(conv), dc_conv_title(conv), dc_conv_topic(conv));
else
snprintf(title, sizeof(title), "%s - %s", dc_conv_id(conv),
dc_conv_title(conv));
{
XmString tx = xs(title);
XtVaSetValues(ui->title, XmNlabelString, tx, NULL);
XmStringFree(tx);
}
XmTextSetString(ui->transcript, "");
ui->shown = 0;
for (i = 0; i < dc_conv_message_count(conv); i++) {
const DcMessage *msg = dc_conv_message_at(conv, i);
if (msg != NULL)
append_message(ui, msg);
}
dc_conv_mark_read(conv);
set_status(ui, title);
refresh_roster(ui);
}
static void
show_peer(DtChatUi *ui, const char *id)
{
DcPeer *peer;
char line[1024];
char title[512];
char addr[256];
if (ui->peers == NULL)
return;
peer = dc_peers_find(ui->peers, id);
if (peer == NULL)
return;
free(ui->cur_account);
ui->cur_account = NULL;
free(ui->cur_conv);
ui->cur_conv = NULL;
free(ui->cur_peer);
ui->cur_peer = strdup(id);
if (ui->cur_peer == NULL)
return;
snprintf(title, sizeof(title), "Nearby - %s",
dc_peer_name(peer) != NULL ? dc_peer_name(peer) : id);
{
XmString tx = xs(title);
XtVaSetValues(ui->title, XmNlabelString, tx, NULL);
XmStringFree(tx);
}
XmTextSetString(ui->transcript, "");
snprintf(line, sizeof(line), "-- %s\n", title);
XmTextInsert(ui->transcript, XmTextGetLastPosition(ui->transcript),
line);
snprintf(line, sizeof(line), "Identity: %s\n", dc_peer_id(peer));
XmTextInsert(ui->transcript, XmTextGetLastPosition(ui->transcript),
line);
snprintf(line, sizeof(line), "Fingerprint: %s\n",
dc_peer_pin(peer) != NULL && dc_peer_pin(peer)[0] != '\0' ?
dc_peer_pin(peer) : "(not known yet)");
XmTextInsert(ui->transcript, XmTextGetLastPosition(ui->transcript),
line);
snprintf(addr, sizeof(addr), "%s:%d",
dc_peer_host(peer) != NULL && dc_peer_host(peer)[0] != '\0' ?
dc_peer_host(peer) : "(no address)", dc_peer_port(peer));
snprintf(line, sizeof(line), "Address: %s (%s%s)\n", addr,
dc_peer_online(peer) ? "online" : "offline",
dc_peer_manual(peer) ? ", added by hand" : "");
XmTextInsert(ui->transcript, XmTextGetLastPosition(ui->transcript),
line);
if (dc_peer_blocked(peer))
XmTextInsert(ui->transcript,
XmTextGetLastPosition(ui->transcript),
"This peer is blocked.\n");
set_status(ui, dc_text(TX_PEER_DETAILS_HINT, "peer details - double-click the row for the dialog"));
refresh_roster(ui);
}
static void
selection_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
XmListCallbackStruct *cbs = (XmListCallbackStruct *)call;
int pos = cbs->item_position;
(void)w;
if (ui->selecting || pos < 1 || (size_t)pos > ui->nrows)
return;
if (ui->rows[pos - 1].peer != NULL) {
show_peer(ui, ui->rows[pos - 1].peer);
return;
}
if (ui->rows[pos - 1].conv == NULL) {
set_status(ui, ui->rows[pos - 1].account);
return;
}
show_conversation(ui, ui->rows[pos - 1].account,
ui->rows[pos - 1].conv);
}
static void
roster_action_cb(Widget w, XtPointer client, XtPointer call)
{
DtChatUi *ui = client;
XmListCallbackStruct *cbs = (XmListCallbackStruct *)call;
int pos = cbs->item_position;
DcPeer *peer;
(void)w;
if (pos < 1 || (size_t)pos > ui->nrows)
return;
if (ui->rows[pos - 1].peer == NULL)
return;
peer = dc_peers_find(ui->peers, ui->rows[pos - 1].peer);
if (peer != NULL)
(void)dtchat_peer_dialog_create(ui->shell, peer, NULL, NULL);
}
static void
clear_rows(DtChatUi *ui)
{
size_t i;
for (i = 0; i < ui->nrows; i++) {
free(ui->rows[i].account);
free(ui->rows[i].conv);
free(ui->rows[i].peer);
}
free(ui->rows);
ui->rows = NULL;
ui->nrows = 0;
}
static int
add_row(DtChatUi *ui, const char *account, const char *conv,
const char *peer, const char *text)
{
RosterRow *next;
XmString item;
next = realloc(ui->rows, (ui->nrows + 1) * sizeof(*next));
if (next == NULL)
return -1;
ui->rows = next;
ui->rows[ui->nrows].account = strdup(account);
ui->rows[ui->nrows].conv = conv != NULL ? strdup(conv) : NULL;
ui->rows[ui->nrows].peer = peer != NULL ? strdup(peer) : NULL;
if (ui->rows[ui->nrows].account == NULL ||
(conv != NULL && ui->rows[ui->nrows].conv == NULL) ||
(peer != NULL && ui->rows[ui->nrows].peer == NULL)) {
free(ui->rows[ui->nrows].account);
free(ui->rows[ui->nrows].conv);
free(ui->rows[ui->nrows].peer);
return -1;
}
item = xs(text);
XmListAddItemUnselected(ui->roster, item, 0);
XmStringFree(item);
ui->nrows++;
return 0;
}
static void
refresh_roster(DtChatUi *ui)
{
size_t ai, ci;
char line[512];
clear_rows(ui);
XmListDeleteAllItems(ui->roster);
if (ui->model == NULL || dc_model_account_count(ui->model) == 0) {
(void)add_row(ui, dc_text(TX_NO_ACCOUNTS_YET, "no accounts yet"), NULL, NULL,
dc_text(TX_NO_ACCOUNTS_YET, "no accounts yet"));
return;
}
if (ui->peers != NULL && dc_peers_count(ui->peers) > 0) {
size_t pi;
(void)add_row(ui, dc_text(TX_NEARBY, "Nearby"), NULL, NULL,
dc_text(TX_NEARBY, "Nearby"));
for (pi = 0; pi < dc_peers_count(ui->peers); pi++) {
DcPeer *peer = dc_peers_at(ui->peers, pi);
const char *name = dc_peer_name(peer);
if (dc_peer_blocked(peer))
snprintf(line, sizeof(line), " %c %s (blocked)",
presence_mark(dc_peer_online(peer) ?
DC_PRESENCE_ONLINE : DC_PRESENCE_OFFLINE),
name != NULL ? name : dc_peer_id(peer));
else
snprintf(line, sizeof(line), " %c %s",
presence_mark(dc_peer_online(peer) ?
DC_PRESENCE_ONLINE : DC_PRESENCE_OFFLINE),
name != NULL ? name : dc_peer_id(peer));
(void)add_row(ui, "Nearby", NULL, dc_peer_id(peer),
line);
}
}
for (ai = 0; ai < dc_model_account_count(ui->model); ai++) {
DcAccount *account = dc_model_account_at(ui->model, ai);
const char *label = dc_account_label(account);
if (label == NULL || *label == '\0')
label = dc_account_id(account);
(void)add_row(ui, label, NULL, NULL, label);
for (ci = 0; ci < dc_account_conv_count(account); ci++) {
DcConv *conv = dc_account_conv_at(account, ci);
int unread = dc_conv_unread(conv);
if (unread > 0)
snprintf(line, sizeof(line), " %c %s (%d)",
presence_mark(dc_conv_presence(conv)),
dc_conv_id(conv), unread);
else
snprintf(line, sizeof(line), " %c %s",
presence_mark(dc_conv_presence(conv)),
dc_conv_id(conv));
(void)add_row(ui, dc_account_id(account),
dc_conv_id(conv), NULL, line);
}
}
ui->selecting = 1;
if (ui->cur_peer != NULL) {
for (ai = 0; ai < ui->nrows; ai++) {
if (ui->rows[ai].peer != NULL &&
strcmp(ui->rows[ai].peer, ui->cur_peer) == 0) {
XmListSelectPos(ui->roster, (int)ai + 1, False);
return;
}
}
}
if (ui->cur_account != NULL && ui->cur_conv != NULL) {
for (ai = 0; ai < ui->nrows; ai++) {
if (ui->rows[ai].conv != NULL &&
strcmp(ui->rows[ai].account, ui->cur_account) == 0 &&
strcmp(ui->rows[ai].conv, ui->cur_conv) == 0) {
XmListSelectPos(ui->roster, (int)ai + 1, False);
break;
}
}
} else {
for (ai = 0; ai < ui->nrows; ai++) {
if (ui->rows[ai].conv != NULL) {
XmListSelectPos(ui->roster, (int)ai + 1, False);
break;
}
}
}
ui->selecting = 0;
if (ui->cur_conv == NULL) {
for (ai = 0; ai < ui->nrows; ai++) {
if (ui->rows[ai].conv != NULL) {
show_conversation(ui, ui->rows[ai].account,
ui->rows[ai].conv);
break;
}
}
}
}
static void
show_first(DtChatUi *ui)
{
size_t ai, ci;
if (ui->model == NULL)
return;
for (ai = 0; ai < dc_model_account_count(ui->model); ai++) {
DcAccount *account = dc_model_account_at(ui->model, ai);
for (ci = 0; ci < dc_account_conv_count(account); ci++) {
DcConv *conv = dc_account_conv_at(account, ci);
show_conversation(ui, dc_account_id(account),
dc_conv_id(conv));
return;
}
}
}
DtChatUi *
dtchat_ui_create(Widget shell, DcModel *model)
{
DtChatUi *ui = calloc(1, sizeof(*ui));
Widget form, bar, paned, roster_parent, chat;
Widget transcript_parent, input_parent, send;
XmString x;
if (ui == NULL)
return NULL;
ui->shell = shell;
ui->model = model;
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell,
XmNwidth, 760, XmNheight, 540,
XmNmarginWidth, 2, XmNmarginHeight, 2, NULL);
make_menu_bar(ui, form, &bar);
ui->status = XmCreateLabel(form, "status", NULL, 0);
x = xs(dc_text(TX_OFFLINE_NO_ACCOUNTS, "offline - no accounts"));
XtVaSetValues(ui->status, XmNlabelString, x,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNbottomAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNheight, 22, XmNmarginWidth, 4, NULL);
XmStringFree(x);
XtManageChild(ui->status);
paned = XtVaCreateManagedWidget("paned", xmPanedWindowWidgetClass,
form, XmNorientation, XmHORIZONTAL,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar,
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, ui->status,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNsashWidth, 4, XmNsashHeight, 4, XmNspacing, 0, NULL);
ui->roster = XmCreateScrolledList(paned, "roster", NULL, 0);
roster_parent = XtParent(ui->roster);
XtVaSetValues(roster_parent, XmNwidth, 230, NULL);
XtVaSetValues(ui->roster, XmNvisibleItemCount, 20,
XmNselectionPolicy, XmBROWSE_SELECT, NULL);
XtManageChild(ui->roster);
XtAddCallback(ui->roster, XmNbrowseSelectionCallback, selection_cb, ui);
XtAddCallback(ui->roster, XmNdefaultActionCallback, roster_action_cb,
ui);
chat = XtVaCreateManagedWidget("chat", xmFormWidgetClass, paned,
NULL);
ui->title = XmCreateLabel(chat, "title", NULL, 0);
x = xs("no conversation");
XtVaSetValues(ui->title, XmNlabelString, x,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNheight, 24, XmNmarginWidth, 6, NULL);
XmStringFree(x);
XtManageChild(ui->title);
ui->transcript = make_scrolled_text(chat, "transcript", 16, 60, 1,
&transcript_parent);
XtVaSetValues(ui->transcript, XmNeditable, False,
XmNcursorPositionVisible, False, NULL);
ui->input = make_scrolled_text(chat, "input", 3, 40, 1,
&input_parent);
send = XmCreatePushButton(chat, "send", NULL, 0);
x = xs(dc_text(TX_SEND, "Send"));
XtVaSetValues(send, XmNlabelString, x,
XmNwidth, 84, XmNheight, 30, NULL);
XmStringFree(x);
XtVaSetValues(transcript_parent,
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, ui->title,
XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, input_parent, NULL);
XtVaSetValues(input_parent,
XmNleftAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_WIDGET,
XmNrightWidget, send, NULL);
XtVaSetValues(send,
XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, NULL);
XtManageChild(ui->transcript);
XtManageChild(ui->input);
XtManageChild(send);
XtAddCallback(send, XmNactivateCallback, send_cb, ui);
XtAddEventHandler(ui->input, KeyPressMask, False, input_key, ui);
XtManageChild(chat);
refresh_roster(ui);
if (ui->cur_conv == NULL)
show_first(ui);
if (ui->cur_conv == NULL)
dtchat_ui_append_system(ui, "no conversation selected");
return ui;
}
void
dtchat_ui_destroy(DtChatUi *ui)
{
if (ui == NULL)
return;
clear_rows(ui);
while (ui->nhistory > 0)
free(ui->history[--ui->nhistory]);
free(ui->draft);
free(ui->cur_account);
free(ui->cur_conv);
free(ui->cur_peer);
free(ui);
}
Widget
dtchat_ui_shell(const DtChatUi *ui)
{
return ui != NULL ? ui->shell : NULL;
}
int
dtchat_ui_is_viewable(DtChatUi *ui)
{
XWindowAttributes attr;
if (ui == NULL || !XtIsRealized(ui->shell))
return 0;
if (!XGetWindowAttributes(XtDisplay(ui->shell), XtWindow(ui->shell),
&attr))
return 0;
return attr.map_state == IsViewable;
}
void
dtchat_ui_refresh(DtChatUi *ui)
{
if (ui == NULL)
return;
if (ui->cur_account != NULL && ui->cur_conv != NULL)
show_conversation(ui, ui->cur_account, ui->cur_conv);
else
refresh_roster(ui);
}
void
dtchat_ui_message_added(DtChatUi *ui, const char *account, const char *conv_id)
{
DcConv *conv;
const DcMessage *msg;
if (ui == NULL || account == NULL || conv_id == NULL)
return;
conv = dc_model_find_conv(ui->model, account, conv_id);
if (conv == NULL)
return;
if (ui->cur_account != NULL && ui->cur_conv != NULL &&
strcmp(ui->cur_account, account) == 0 &&
strcmp(ui->cur_conv, conv_id) == 0) {
msg = dc_conv_message_at(conv,
dc_conv_message_count(conv) > 0 ?
dc_conv_message_count(conv) - 1 : 0);
if (msg != NULL)
append_message(ui, msg);
if (ui->shown > 2000)
show_conversation(ui, account, conv_id);
dc_conv_mark_read(conv);
refresh_roster(ui);
set_status(ui, "new message");
} else {
refresh_roster(ui);
}
}
int
dtchat_ui_send_text(DtChatUi *ui, const char *text)
{
DcConv *conv;
if (ui == NULL || text == NULL || *text == '\0' ||
ui->cur_account == NULL || ui->cur_conv == NULL)
return -1;
history_add(ui, text);
if (ui->send_fn != NULL) {
if (ui->send_fn(ui->send_arg, ui->cur_conv, text) != 0)
return -1;
set_status(ui, dc_text(TX_SENT, "sent"));
return 0;
}
conv = dc_model_find_conv(ui->model, ui->cur_account, ui->cur_conv);
if (conv == NULL)
return -1;
if (dc_conv_add_message(conv, "me", text, (long long)time(NULL),
DTCHAT_MSG_SELF) != 0)
return -1;
dtchat_ui_message_added(ui, ui->cur_account, ui->cur_conv);
set_status(ui, dc_text(TX_SENT_DEMO, "sent (demo)"));
return 0;
}
void
dtchat_ui_set_send_hook(DtChatUi *ui, DtChatSendFn fn, void *arg)
{
if (ui == NULL)
return;
ui->send_fn = fn;
ui->send_arg = arg;
}
size_t
dtchat_ui_shown_messages(const DtChatUi *ui)
{
return ui ? ui->shown : 0;
}
int
dtchat_ui_contains(const DtChatUi *ui, const char *needle)
{
char *text;
int found;
if (ui == NULL || needle == NULL)
return 0;
text = XmTextGetString(ui->transcript);
if (text == NULL)
return 0;
found = strstr(text, needle) != NULL;
XtFree(text);
return found;
}
void
dtchat_ui_set_peers(DtChatUi *ui, DcPeers *peers)
{
if (ui == NULL)
return;
ui->peers = peers;
refresh_roster(ui);
}
void
dtchat_ui_select_peer(DtChatUi *ui, const char *id)
{
size_t i;
if (ui == NULL || id == NULL)
return;
for (i = 0; i < ui->nrows; i++) {
if (ui->rows[i].peer != NULL &&
strcmp(ui->rows[i].peer, id) == 0) {
XmListSelectPos(ui->roster, (int)i + 1, True);
return;
}
}
}
void
dtchat_ui_set_file_send(DtChatUi *ui, DtChatFileSendFn fn, void *arg)
{
if (ui == NULL)
return;
ui->file_fn = fn;
ui->file_arg = arg;
}
int
dtchat_ui_is_peer(const DtChatUi *ui, const char *id)
{
return ui != NULL && ui->cur_peer != NULL && id != NULL &&
strcmp(ui->cur_peer, id) == 0;
}
int
dtchat_ui_smoke(DtChatUi *ui)
{
return ui != NULL && ui->nrows > 1 && ui->cur_conv != NULL &&
XmTextGetLastPosition(ui->transcript) > 0;
}
int
dtchat_ui_is_current(const DtChatUi *ui, const char *account, const char *conv)
{
return ui != NULL && ui->cur_account != NULL && ui->cur_conv != NULL &&
account != NULL && conv != NULL &&
strcmp(ui->cur_account, account) == 0 &&
strcmp(ui->cur_conv, conv) == 0;
}
void
dtchat_ui_select(DtChatUi *ui, const char *account, const char *conv)
{
size_t i;
if (ui == NULL || account == NULL || conv == NULL)
return;
for (i = 0; i < ui->nrows; i++) {
if (ui->rows[i].conv != NULL &&
strcmp(ui->rows[i].account, account) == 0 &&
strcmp(ui->rows[i].conv, conv) == 0) {
XmListSelectPos(ui->roster, (int)i + 1, True);
return;
}
}
}
void
dtchat_ui_append_system(DtChatUi *ui, const char *text)
{
char *line;
size_t len;
if (ui == NULL || text == NULL)
return;
len = strlen(text) + 7;
line = malloc(len);
if (line == NULL)
return;
snprintf(line, len, "-- %s\n", text);
XmTextInsert(ui->transcript, XmTextGetLastPosition(ui->transcript),
line);
XmTextShowPosition(ui->transcript,
XmTextGetLastPosition(ui->transcript));
free(line);
}