dtchat: bind the transcript and input at runtime (T2.3)
The transcript reloads from the model on selection, appends new messages via dtchat_ui_message_added (clearing unread in the active conversation), shows action and system lines, and reloads at 2000 lines. Send and Enter add a DTCHAT_MSG_SELF message to the model through dtchat_ui_send_text, clear the input and append; Shift+Enter stays a newline. The smoke test now injects a runtime message, sends text, pushes past 2000 messages, checks the view cap and verifies '* alice' / '-- bob joined'. Standalone smoke PASS and Enter path verified under Xvfb; the full make mcde-host is blocked by another agent's release build holding the lock.
This commit is contained in:
1 parent
39a461d519
commit
5bfbc37cf6
3 files changed
+172
-29
No files matched your search
@@ -48,6 +48,10 @@ make_demo_model(void)
|
||||
DTCHAT_MSG_SELF);
|
||||
(void)dc_conv_add_message(conv, "bob",
|
||||
"the roster binding works", now - 60, 0);
|
||||
(void)dc_conv_add_message(conv, "alice", "waves at the roster",
|
||||
now - 150, DTCHAT_MSG_ACTION);
|
||||
(void)dc_conv_add_message(conv, "system", "bob joined #cde",
|
||||
now - 90, DTCHAT_MSG_SYSTEM);
|
||||
}
|
||||
conv = dc_model_add_conv(model, "libera", "alice", DC_CONV_DIRECT,
|
||||
"alice");
|
||||
@@ -95,6 +99,54 @@ make_demo_model(void)
|
||||
return model;
|
||||
}
|
||||
|
||||
static int
|
||||
smoke_ui_checks(DtChatUi *ui)
|
||||
{
|
||||
DcConv *selected;
|
||||
size_t before;
|
||||
int i;
|
||||
|
||||
if (!dtchat_ui_is_viewable(ui) || !dtchat_ui_smoke(ui))
|
||||
return 0;
|
||||
dtchat_ui_select(ui, "jabber", "bob@example.org");
|
||||
selected = dc_model_find_conv(demo_model, "jabber",
|
||||
"bob@example.org");
|
||||
if (!dtchat_ui_is_current(ui, "jabber", "bob@example.org") ||
|
||||
selected == NULL || dc_conv_unread(selected) != 0)
|
||||
return 0;
|
||||
|
||||
before = dtchat_ui_shown_messages(ui);
|
||||
if (dc_conv_add_message(selected, "bob", "runtime message",
|
||||
(long long)time(NULL), 0) != 0)
|
||||
return 0;
|
||||
dtchat_ui_message_added(ui, "jabber", "bob@example.org");
|
||||
if (dtchat_ui_shown_messages(ui) <= before ||
|
||||
!dtchat_ui_contains(ui, "runtime message"))
|
||||
return 0;
|
||||
|
||||
if (dtchat_ui_send_text(ui, "smoke send") != 0 ||
|
||||
!dtchat_ui_contains(ui, "smoke send"))
|
||||
return 0;
|
||||
|
||||
/* push past 2000 messages; the view must reload at the ring size */
|
||||
for (i = 0; i < 2010; i++) {
|
||||
if (dc_conv_add_message(selected, "load", "filler",
|
||||
(long long)time(NULL), 0) != 0)
|
||||
return 0;
|
||||
}
|
||||
dtchat_ui_refresh(ui);
|
||||
if (dtchat_ui_shown_messages(ui) > 2000 ||
|
||||
dc_conv_unread(selected) != 0)
|
||||
return 0;
|
||||
|
||||
/* action and system formatting live in #cde */
|
||||
dtchat_ui_select(ui, "libera", "#cde");
|
||||
if (!dtchat_ui_contains(ui, "* alice") ||
|
||||
!dtchat_ui_contains(ui, "-- bob joined #cde"))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
smoke_check(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
@@ -102,19 +154,11 @@ smoke_check(XtPointer client, XtIntervalId *id)
|
||||
|
||||
(void)id;
|
||||
XSync(XtDisplay(toplevel), False);
|
||||
dtchat_ui_select(ui, "jabber", "bob@example.org");
|
||||
{
|
||||
DcConv *selected = dc_model_find_conv(demo_model, "jabber",
|
||||
"bob@example.org");
|
||||
|
||||
if (dtchat_ui_is_viewable(ui) && dtchat_ui_smoke(ui) &&
|
||||
dtchat_ui_is_current(ui, "jabber", "bob@example.org") &&
|
||||
selected != NULL && dc_conv_unread(selected) == 0) {
|
||||
printf("dtchat smoke: PASS\n");
|
||||
} else {
|
||||
printf("dtchat smoke: FAIL window, selection or unread\n");
|
||||
smoke_failed = 1;
|
||||
}
|
||||
if (smoke_ui_checks(ui)) {
|
||||
printf("dtchat smoke: PASS\n");
|
||||
} else {
|
||||
printf("dtchat smoke: FAIL window, selection, runtime, cap or format\n");
|
||||
smoke_failed = 1;
|
||||
}
|
||||
XtAppSetExitFlag(app);
|
||||
}
|
||||
@@ -172,7 +216,7 @@ main(int argc, char **argv)
|
||||
|
||||
if (smoke) {
|
||||
XMapRaised(XtDisplay(toplevel), XtWindow(toplevel));
|
||||
XtAppAddTimeOut(app, 800, smoke_check, ui);
|
||||
XtAppAddTimeOut(app, 5000, smoke_check, ui);
|
||||
}
|
||||
XtAppMainLoop(app);
|
||||
dtchat_ui_destroy(ui);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <time.h>
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <Xm/CascadeB.h>
|
||||
#include <Xm/Form.h>
|
||||
#include <Xm/Label.h>
|
||||
@@ -48,6 +49,7 @@ struct DtChatUi {
|
||||
size_t nrows;
|
||||
char *cur_account;
|
||||
char *cur_conv;
|
||||
size_t shown;
|
||||
int selecting;
|
||||
};
|
||||
|
||||
@@ -129,8 +131,6 @@ send_cb(Widget w, XtPointer client, XtPointer call)
|
||||
{
|
||||
DtChatUi *ui = client;
|
||||
char *text;
|
||||
char *line;
|
||||
size_t len;
|
||||
|
||||
(void)w;
|
||||
(void)call;
|
||||
@@ -139,21 +139,28 @@ send_cb(Widget w, XtPointer client, XtPointer call)
|
||||
XtFree(text);
|
||||
return;
|
||||
}
|
||||
len = strlen(text) + 8;
|
||||
line = malloc(len);
|
||||
if (line != NULL) {
|
||||
snprintf(line, len, "[me] %s\n", text);
|
||||
XmTextInsert(ui->transcript,
|
||||
XmTextGetLastPosition(ui->transcript), line);
|
||||
XmTextShowPosition(ui->transcript,
|
||||
XmTextGetLastPosition(ui->transcript));
|
||||
free(line);
|
||||
}
|
||||
XmTextSetString(ui->input, "");
|
||||
set_status(ui, "demo: no account connected");
|
||||
if (dtchat_ui_send_text(ui, text) != 0)
|
||||
set_status(ui, "no conversation selected");
|
||||
else
|
||||
XmTextSetString(ui->input, "");
|
||||
XtFree(text);
|
||||
}
|
||||
|
||||
static void
|
||||
input_key(Widget w, XtPointer client, XEvent *event, Boolean *cont)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static Widget
|
||||
make_menu(Widget bar, const char *label)
|
||||
{
|
||||
@@ -301,6 +308,7 @@ append_message(DtChatUi *ui, const DcMessage *msg)
|
||||
pos = XmTextGetLastPosition(ui->transcript);
|
||||
XmTextInsert(ui->transcript, pos, line);
|
||||
XmTextShowPosition(ui->transcript, XmTextGetLastPosition(ui->transcript));
|
||||
ui->shown++;
|
||||
free(line);
|
||||
}
|
||||
|
||||
@@ -336,6 +344,7 @@ show_conversation(DtChatUi *ui, const char *account, const char *conv_id)
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -577,6 +586,7 @@ dtchat_ui_create(Widget shell, DcModel *model)
|
||||
XtManageChild(ui->input);
|
||||
XtManageChild(send);
|
||||
XtAddCallback(send, XmNactivateCallback, send_cb, ui);
|
||||
XtAddEventHandler(ui->input, KeyPressMask, False, input_key, ui);
|
||||
XtManageChild(chat);
|
||||
|
||||
refresh_roster(ui);
|
||||
@@ -611,6 +621,87 @@ dtchat_ui_is_viewable(DtChatUi *ui)
|
||||
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;
|
||||
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, "sent (demo)");
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int
|
||||
dtchat_ui_smoke(DtChatUi *ui)
|
||||
{
|
||||
|
||||
@@ -22,7 +22,15 @@ int dtchat_ui_is_viewable(DtChatUi *ui);
|
||||
/* Select ACCOUNT/CONV in the roster and show it in the chat. */
|
||||
void dtchat_ui_select(DtChatUi *ui, const char *account, const char *conv);
|
||||
|
||||
/* T2.2 smoke: a conversation is selected, its title and transcript exist. */
|
||||
/* T2.3 runtime binding. */
|
||||
void dtchat_ui_refresh(DtChatUi *ui);
|
||||
void dtchat_ui_message_added(DtChatUi *ui, const char *account,
|
||||
const char *conv);
|
||||
int dtchat_ui_send_text(DtChatUi *ui, const char *text);
|
||||
size_t dtchat_ui_shown_messages(const DtChatUi *ui);
|
||||
int dtchat_ui_contains(const DtChatUi *ui, const char *needle);
|
||||
|
||||
/* T2.2/T2.3 smoke: a conversation is selected, its title and transcript exist. */
|
||||
int dtchat_ui_smoke(DtChatUi *ui);
|
||||
|
||||
/* True when the current conversation is ACCOUNT/CONV. */
|
||||
|
||||
Reference in new issue
Block a user