The dialog now edits real accounts instead of in-memory demo entries: the schema is Label, Protocol (irc/xmpp/telegram/whatsapp/demo), Adapter path, Settings (JSON) and Secrets (JSON). It loads accounts.json/secrets.json through the runtime (label, protocol, adapter, settings, masked secrets), and Save builds the complete accounts/secrets documents for every account, extracts label/protocol/adapter and parses the two JSON fields, then calls the runtime's writer. The runtime saves via dc_store_save_accounts/_secrets (files 0600), swaps its stored JSON, removes deleted accounts from the model, and restarts all adapters from the new set; the UI data pointers are refreshed. Add/Delete/Save semantics are now file-backed. Verified through the real UI under Xvfb: Add filled (Label 'Fake via UI', adapter /tmp/opencode/fake_adapter, settings {"mode":"echo"}), Save wrote both files (0600) and started the fake adapter online; restarting dtchat shows the account and all fields again (screenshots dtchat-accounts-editor.png, dtchat-accounts-loaded.png). In-tree build and harness follow.
1206 lines
31 KiB
C
1206 lines
31 KiB
C
/*
|
|
* ui_accounts.c -- the generic account editor
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* The left list holds the accounts of the model; the right form is generated
|
|
* from the adapter's settings schema. Text, secret, int, bool, choice, info
|
|
* and button fields are supported. An account is id, protocol, label,
|
|
* adapter path and a settings JSON object; its secrets are a second JSON
|
|
* object kept out of the status line. Save writes every account through the
|
|
* callback the accounts runtime supplies, so the files are the source of
|
|
* truth (accounts.json, secrets.json) and the adapters restart from it.
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include "ui_accounts.h"
|
|
#include "ui_text.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/List.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/ScrolledW.h>
|
|
#include <Xm/Text.h>
|
|
#include <Xm/TextF.h>
|
|
#include <Xm/ToggleB.h>
|
|
#include <Xm/Xm.h>
|
|
|
|
typedef enum {
|
|
F_TEXT,
|
|
F_SECRET,
|
|
F_INT,
|
|
F_BOOL,
|
|
F_CHOICE,
|
|
F_INFO,
|
|
F_BUTTON
|
|
} FieldType;
|
|
|
|
typedef struct {
|
|
char *key;
|
|
char *label;
|
|
FieldType type;
|
|
int required;
|
|
Widget widget;
|
|
Widget option;
|
|
char **values;
|
|
char **labels;
|
|
size_t nchoices;
|
|
char *choice;
|
|
char *secret;
|
|
int loading;
|
|
char *def;
|
|
long long defint;
|
|
int defbool;
|
|
} Field;
|
|
|
|
typedef struct {
|
|
char *id;
|
|
char *protocol;
|
|
char *label;
|
|
char *adapter;
|
|
DcJson *settings;
|
|
DcJson *secrets;
|
|
} AccountData;
|
|
|
|
struct DtAccountsDialog {
|
|
Widget shell;
|
|
Widget list;
|
|
Widget fields_form;
|
|
Widget status;
|
|
DcModel *model;
|
|
Field *fields;
|
|
size_t nfields;
|
|
AccountData *data;
|
|
size_t ndata;
|
|
char *selected;
|
|
const DcJson *accounts_json;
|
|
const DcJson *secrets_json;
|
|
DtAccountsSaveFn save;
|
|
void *save_arg;
|
|
DtAccountsClosedFn closed;
|
|
void *arg;
|
|
};
|
|
|
|
static XmString
|
|
xs(const char *s)
|
|
{
|
|
return XmStringCreateLocalized((char *)s);
|
|
}
|
|
|
|
static DcJson *
|
|
clone_json(const DcJson *json)
|
|
{
|
|
char *text;
|
|
size_t len = 0;
|
|
DcJson *copy;
|
|
|
|
if (json == NULL)
|
|
return dc_json_new_object();
|
|
text = dc_json_write(json, &len);
|
|
if (text == NULL)
|
|
return NULL;
|
|
copy = dc_json_parse(text, len, NULL, 0);
|
|
free(text);
|
|
return copy;
|
|
}
|
|
|
|
static char *
|
|
json_text(const DcJson *json)
|
|
{
|
|
size_t len = 0;
|
|
char *text;
|
|
|
|
if (json == NULL)
|
|
return strdup("");
|
|
text = dc_json_write(json, &len);
|
|
return text != NULL ? text : strdup("");
|
|
}
|
|
|
|
static void
|
|
set_status(DtAccountsDialog *dialog, const char *text)
|
|
{
|
|
XmString x = xs(text);
|
|
|
|
XtVaSetValues(dialog->status, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
|
|
static FieldType
|
|
field_type(const char *type)
|
|
{
|
|
if (type == NULL)
|
|
return F_TEXT;
|
|
if (strcmp(type, "secret") == 0)
|
|
return F_SECRET;
|
|
if (strcmp(type, "int") == 0)
|
|
return F_INT;
|
|
if (strcmp(type, "bool") == 0)
|
|
return F_BOOL;
|
|
if (strcmp(type, "choice") == 0)
|
|
return F_CHOICE;
|
|
if (strcmp(type, "info") == 0)
|
|
return F_INFO;
|
|
if (strcmp(type, "button") == 0)
|
|
return F_BUTTON;
|
|
return F_TEXT;
|
|
}
|
|
|
|
static void
|
|
choice_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
Field *field = ((void **)client)[0];
|
|
|
|
(void)w;
|
|
(void)call;
|
|
free(field->choice);
|
|
field->choice = strdup((const char *)((void **)client)[1]);
|
|
{
|
|
XmString x = xs((const char *)((void **)client)[2]);
|
|
|
|
XtVaSetValues(field->option, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
}
|
|
|
|
static void
|
|
field_button_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
DtAccountsDialog *dialog = ((void **)client)[0];
|
|
const char *what = ((void **)client)[1];
|
|
char line[160];
|
|
|
|
(void)w;
|
|
(void)call;
|
|
snprintf(line, sizeof(line), "%s is not implemented yet", what);
|
|
set_status(dialog, line);
|
|
}
|
|
|
|
static void
|
|
secret_verify_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
Field *field = client;
|
|
XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)call;
|
|
char *old;
|
|
char *next;
|
|
size_t oldlen, start, end, ins, newlen, i;
|
|
|
|
(void)w;
|
|
if (!cbs->doit || field->loading)
|
|
return;
|
|
old = field->secret != NULL ? field->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(field->secret);
|
|
field->secret = next;
|
|
if (ins > 0 && cbs->text != NULL && cbs->text->ptr != NULL) {
|
|
for (i = 0; i < ins; i++)
|
|
cbs->text->ptr[i] = '*';
|
|
}
|
|
}
|
|
|
|
static Widget
|
|
make_row(DtAccountsDialog *dialog, Widget parent, Widget previous, Field *field)
|
|
{
|
|
Widget row = XtVaCreateManagedWidget("row", xmFormWidgetClass, parent,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopAttachment, previous != NULL ? XmATTACH_WIDGET :
|
|
XmATTACH_FORM,
|
|
XmNtopWidget, previous,
|
|
XmNheight, 32, NULL);
|
|
Widget label = XmCreateLabel(row, "fieldLabel", NULL, 0);
|
|
XmString x = xs(field->label != NULL ? field->label : field->key);
|
|
|
|
XtVaSetValues(label, XmNlabelString, x,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNwidth, 150, XmNrecomputeSize, False,
|
|
XmNalignment, XmALIGNMENT_END, XmNmarginRight, 6, NULL);
|
|
XmStringFree(x);
|
|
XtManageChild(label);
|
|
|
|
switch (field->type) {
|
|
case F_TEXT:
|
|
case F_SECRET:
|
|
case F_INT:
|
|
field->widget = XmCreateTextField(row, "fieldValue", NULL, 0);
|
|
XtVaSetValues(field->widget,
|
|
XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, label,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNcolumns, 28, NULL);
|
|
if (field->type == F_SECRET)
|
|
XtAddCallback(field->widget, XmNmodifyVerifyCallback,
|
|
secret_verify_cb, field);
|
|
XtManageChild(field->widget);
|
|
break;
|
|
case F_BOOL:
|
|
field->widget = XmCreateToggleButton(row, "fieldValue", NULL, 0);
|
|
x = xs("");
|
|
XtVaSetValues(field->widget,
|
|
XmNlabelString, x,
|
|
XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, label,
|
|
XmNtopAttachment, XmATTACH_FORM, NULL);
|
|
XmStringFree(x);
|
|
XtManageChild(field->widget);
|
|
break;
|
|
case F_CHOICE:
|
|
field->option = XmCreateOptionMenu(row, "fieldValue", NULL, 0);
|
|
XtVaSetValues(field->option,
|
|
XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, label,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopAttachment, XmATTACH_FORM, NULL);
|
|
{
|
|
Widget menu = XmCreatePulldownMenu(field->option,
|
|
"choiceMenu", NULL, 0);
|
|
size_t i;
|
|
|
|
XtVaSetValues(field->option, XmNsubMenuId, menu, NULL);
|
|
for (i = 0; i < field->nchoices; i++) {
|
|
void **ctx = malloc(3 * sizeof(void *));
|
|
char name[32];
|
|
Widget item;
|
|
|
|
if (ctx == NULL)
|
|
continue;
|
|
ctx[0] = field;
|
|
ctx[1] = field->values[i];
|
|
ctx[2] = field->labels[i];
|
|
snprintf(name, sizeof(name), "choice%zu", i);
|
|
item = XmCreatePushButton(menu, name, NULL, 0);
|
|
x = xs(field->labels[i]);
|
|
XtVaSetValues(item, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
XtAddCallback(item, XmNactivateCallback,
|
|
choice_cb, ctx);
|
|
XtManageChild(item);
|
|
}
|
|
}
|
|
XtManageChild(field->option);
|
|
break;
|
|
case F_INFO:
|
|
field->widget = XmCreateLabel(row, "fieldInfo", NULL, 0);
|
|
x = xs(field->def != NULL ? field->def : "");
|
|
XtVaSetValues(field->widget, XmNlabelString, x,
|
|
XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, label,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
|
|
XmStringFree(x);
|
|
XtManageChild(field->widget);
|
|
break;
|
|
case F_BUTTON:
|
|
field->widget = XmCreatePushButton(row, "fieldButton", NULL, 0);
|
|
x = xs(field->label != NULL ? field->label : field->key);
|
|
XtVaSetValues(field->widget, XmNlabelString, x,
|
|
XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, label,
|
|
XmNtopAttachment, XmATTACH_FORM, NULL);
|
|
XmStringFree(x);
|
|
{
|
|
void **ctx = malloc(2 * sizeof(void *));
|
|
|
|
if (ctx != NULL) {
|
|
ctx[0] = dialog;
|
|
ctx[1] = field->key;
|
|
XtAddCallback(field->widget,
|
|
XmNactivateCallback, field_button_cb, ctx);
|
|
}
|
|
}
|
|
XtManageChild(field->widget);
|
|
break;
|
|
}
|
|
return row;
|
|
}
|
|
|
|
static int
|
|
build_fields(DtAccountsDialog *dialog, const DcJson *schema)
|
|
{
|
|
Widget previous = NULL;
|
|
size_t n = dc_json_array_size(schema);
|
|
size_t i;
|
|
|
|
dialog->fields = calloc(n != 0 ? n : 1, sizeof(*dialog->fields));
|
|
if (dialog->fields == NULL)
|
|
return -1;
|
|
for (i = 0; i < n; i++) {
|
|
DcJson *entry = dc_json_array_get(schema, i);
|
|
Field *field = &dialog->fields[dialog->nfields];
|
|
const char *key = dc_json_string(dc_json_get(entry, "key"));
|
|
const char *label = dc_json_string(dc_json_get(entry, "label"));
|
|
const char *type = dc_json_string(dc_json_get(entry, "type"));
|
|
DcJson *def = dc_json_get(entry, "default");
|
|
size_t c;
|
|
|
|
if (key == NULL || *key == '\0')
|
|
continue;
|
|
field->key = strdup(key);
|
|
field->label = strdup(label != NULL ? label : key);
|
|
field->type = field_type(type);
|
|
field->required = dc_json_bool(dc_json_get(entry,
|
|
"required"));
|
|
field->def = dc_json_string(def) != NULL ?
|
|
strdup(dc_json_string(def)) : NULL;
|
|
field->defint = dc_json_int(def);
|
|
field->defbool = dc_json_bool(def);
|
|
if (field->type == F_CHOICE) {
|
|
DcJson *choices = dc_json_get(entry, "choices");
|
|
|
|
field->nchoices = dc_json_array_size(choices);
|
|
if (field->nchoices > 0) {
|
|
field->values = calloc(field->nchoices,
|
|
sizeof(char *));
|
|
field->labels = calloc(field->nchoices,
|
|
sizeof(char *));
|
|
}
|
|
for (c = 0; c < field->nchoices; c++) {
|
|
DcJson *choice = dc_json_array_get(choices, c);
|
|
const char *v = dc_json_string(
|
|
dc_json_get(choice, "value"));
|
|
const char *l = dc_json_string(
|
|
dc_json_get(choice, "label"));
|
|
|
|
field->values[c] = strdup(v != NULL ? v : "");
|
|
field->labels[c] = strdup(l != NULL ? l :
|
|
field->values[c]);
|
|
}
|
|
}
|
|
previous = make_row(dialog, dialog->fields_form, previous,
|
|
field);
|
|
dialog->nfields++;
|
|
}
|
|
XtVaSetValues(dialog->fields_form, XmNheight,
|
|
(int)(dialog->nfields * 34 + 4), NULL);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
set_defaults(DtAccountsDialog *dialog)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < dialog->nfields; i++) {
|
|
Field *f = &dialog->fields[i];
|
|
char num[32];
|
|
|
|
switch (f->type) {
|
|
case F_TEXT:
|
|
XmTextFieldSetString(f->widget, f->def != NULL ?
|
|
f->def : "");
|
|
break;
|
|
case F_SECRET:
|
|
f->loading = 1;
|
|
XmTextFieldSetString(f->widget, "");
|
|
f->loading = 0;
|
|
free(f->secret);
|
|
f->secret = strdup("");
|
|
break;
|
|
case F_INT:
|
|
snprintf(num, sizeof(num), "%lld", f->defint);
|
|
XmTextFieldSetString(f->widget, num);
|
|
break;
|
|
case F_BOOL:
|
|
XmToggleButtonSetState(f->widget, f->defbool, False);
|
|
break;
|
|
case F_CHOICE:
|
|
if (f->nchoices > 0) {
|
|
const char *value = f->def != NULL ?
|
|
f->def : f->values[0];
|
|
size_t c;
|
|
|
|
for (c = 0; c < f->nchoices; c++) {
|
|
if (strcmp(f->values[c], value) == 0)
|
|
break;
|
|
}
|
|
if (c == f->nchoices)
|
|
c = 0;
|
|
free(f->choice);
|
|
f->choice = strdup(f->values[c]);
|
|
if (f->option != NULL) {
|
|
XmString x = xs(f->labels[c]);
|
|
|
|
XtVaSetValues(f->option,
|
|
XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
}
|
|
break;
|
|
case F_INFO:
|
|
case F_BUTTON:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void
|
|
load_account(DtAccountsDialog *dialog, AccountData *data)
|
|
{
|
|
size_t i;
|
|
DcJson *settings = data != NULL ? data->settings : NULL;
|
|
DcJson *secrets = data != NULL ? data->secrets : NULL;
|
|
|
|
for (i = 0; i < dialog->nfields; i++) {
|
|
Field *f = &dialog->fields[i];
|
|
DcJson *value;
|
|
|
|
if (strcmp(f->key, "label") == 0 && f->type == F_TEXT) {
|
|
XmTextFieldSetString(f->widget, data != NULL &&
|
|
data->label != NULL ? data->label : "");
|
|
continue;
|
|
}
|
|
if (strcmp(f->key, "adapter") == 0 && f->type == F_TEXT) {
|
|
XmTextFieldSetString(f->widget, data != NULL &&
|
|
data->adapter != NULL ? data->adapter : "");
|
|
continue;
|
|
}
|
|
if (strcmp(f->key, "protocol") == 0 && f->type == F_CHOICE) {
|
|
const char *want = data != NULL &&
|
|
data->protocol != NULL ? data->protocol : "irc";
|
|
size_t c;
|
|
|
|
for (c = 0; c < f->nchoices; c++)
|
|
if (strcmp(f->values[c], want) == 0)
|
|
break;
|
|
if (c == f->nchoices)
|
|
c = 0;
|
|
free(f->choice);
|
|
f->choice = strdup(f->values[c]);
|
|
if (f->option != NULL) {
|
|
XmString x = xs(f->labels[c]);
|
|
|
|
XtVaSetValues(f->option, XmNlabelString, x,
|
|
NULL);
|
|
XmStringFree(x);
|
|
}
|
|
continue;
|
|
}
|
|
if (strcmp(f->key, "settings_json") == 0) {
|
|
char *text = json_text(data != NULL ? data->settings :
|
|
NULL);
|
|
|
|
XmTextFieldSetString(f->widget, text != NULL ? text :
|
|
"{}");
|
|
free(text);
|
|
continue;
|
|
}
|
|
if (strcmp(f->key, "secrets_json") == 0) {
|
|
char *text = json_text(data != NULL ? data->secrets :
|
|
NULL);
|
|
size_t k, n = strlen(text != NULL ? text : "");
|
|
char *mask = malloc(n + 1);
|
|
|
|
if (mask != NULL) {
|
|
for (k = 0; k < n; k++)
|
|
mask[k] = '*';
|
|
mask[n] = '\0';
|
|
f->loading = 1;
|
|
XmTextFieldSetString(f->widget, mask);
|
|
f->loading = 0;
|
|
free(mask);
|
|
}
|
|
free(f->secret);
|
|
f->secret = text != NULL ? text : strdup("{}");
|
|
continue;
|
|
}
|
|
if (f->type == F_SECRET)
|
|
value = dc_json_get(secrets, f->key);
|
|
else
|
|
value = dc_json_get(settings, f->key);
|
|
switch (f->type) {
|
|
case F_TEXT:
|
|
XmTextFieldSetString(f->widget, (char *)
|
|
(dc_json_string(value) != NULL ?
|
|
dc_json_string(value) : (f->def != NULL ?
|
|
f->def : "")));
|
|
break;
|
|
case F_SECRET:
|
|
{
|
|
const char *secret = dc_json_string(value);
|
|
char *mask;
|
|
size_t k;
|
|
|
|
if (secret == NULL)
|
|
secret = "";
|
|
mask = malloc(strlen(secret) + 1);
|
|
if (mask != NULL) {
|
|
for (k = 0; secret[k] != '\0'; k++)
|
|
mask[k] = '*';
|
|
mask[k] = '\0';
|
|
f->loading = 1;
|
|
XmTextFieldSetString(f->widget, mask);
|
|
f->loading = 0;
|
|
free(mask);
|
|
}
|
|
free(f->secret);
|
|
f->secret = strdup(secret);
|
|
}
|
|
break;
|
|
case F_INT:
|
|
{
|
|
char num[32];
|
|
|
|
snprintf(num, sizeof(num), "%lld",
|
|
value != NULL ? dc_json_int(value) :
|
|
f->defint);
|
|
XmTextFieldSetString(f->widget, num);
|
|
}
|
|
break;
|
|
case F_BOOL:
|
|
XmToggleButtonSetState(f->widget,
|
|
value != NULL ? dc_json_bool(value) :
|
|
f->defbool, False);
|
|
break;
|
|
case F_CHOICE:
|
|
if (f->nchoices > 0) {
|
|
const char *want = dc_json_string(value);
|
|
size_t c;
|
|
|
|
if (want == NULL)
|
|
want = f->def != NULL ? f->def :
|
|
f->values[0];
|
|
for (c = 0; c < f->nchoices; c++)
|
|
if (strcmp(f->values[c], want) == 0)
|
|
break;
|
|
if (c == f->nchoices)
|
|
c = 0;
|
|
free(f->choice);
|
|
f->choice = strdup(f->values[c]);
|
|
if (f->option != NULL) {
|
|
XmString x = xs(f->labels[c]);
|
|
|
|
XtVaSetValues(f->option,
|
|
XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
}
|
|
}
|
|
break;
|
|
case F_INFO:
|
|
case F_BUTTON:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static AccountData *
|
|
find_data(DtAccountsDialog *dialog, const char *id)
|
|
{
|
|
size_t i;
|
|
|
|
if (id == NULL)
|
|
return NULL;
|
|
for (i = 0; i < dialog->ndata; i++)
|
|
if (strcmp(dialog->data[i].id, id) == 0)
|
|
return &dialog->data[i];
|
|
return NULL;
|
|
}
|
|
|
|
static AccountData *
|
|
ensure_data(DtAccountsDialog *dialog, const char *id)
|
|
{
|
|
AccountData *next;
|
|
AccountData *data = find_data(dialog, id);
|
|
char *copy;
|
|
|
|
if (data != NULL)
|
|
return data;
|
|
next = realloc(dialog->data, (dialog->ndata + 1) * sizeof(*next));
|
|
if (next == NULL)
|
|
return NULL;
|
|
dialog->data = next;
|
|
data = &dialog->data[dialog->ndata];
|
|
memset(data, 0, sizeof(*data));
|
|
copy = strdup(id);
|
|
if (copy == NULL)
|
|
return NULL;
|
|
data->id = copy;
|
|
data->protocol = strdup("irc");
|
|
data->label = strdup(id);
|
|
data->adapter = strdup("");
|
|
data->settings = dc_json_new_object();
|
|
data->secrets = dc_json_new_object();
|
|
if (data->settings == NULL || data->secrets == NULL) {
|
|
dc_json_free(data->settings);
|
|
dc_json_free(data->secrets);
|
|
free(data->id);
|
|
return NULL;
|
|
}
|
|
dialog->ndata++;
|
|
return data;
|
|
}
|
|
|
|
static int
|
|
collect(DtAccountsDialog *dialog, DcJson **settings_out,
|
|
DcJson **secrets_out)
|
|
{
|
|
DcJson *settings = dc_json_new_object();
|
|
DcJson *secrets = dc_json_new_object();
|
|
size_t i;
|
|
|
|
if (settings == NULL || secrets == NULL) {
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
return -1;
|
|
}
|
|
for (i = 0; i < dialog->nfields; i++) {
|
|
Field *f = &dialog->fields[i];
|
|
DcJson *target;
|
|
|
|
if (f->type == F_INFO || f->type == F_BUTTON)
|
|
continue;
|
|
target = f->type == F_SECRET ? secrets : settings;
|
|
if (f->type == F_TEXT) {
|
|
char *value = XmTextFieldGetString(f->widget);
|
|
|
|
if (value == NULL)
|
|
value = strdup("");
|
|
if (f->required && (*value == '\0')) {
|
|
char line[160];
|
|
|
|
snprintf(line, sizeof(line),
|
|
"%s is required", f->label);
|
|
set_status(dialog, line);
|
|
XtFree(value);
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
return -1;
|
|
}
|
|
(void)dc_json_object_set(target, f->key,
|
|
dc_json_new_string(value));
|
|
XtFree(value);
|
|
} else if (f->type == F_SECRET) {
|
|
const char *value = f->secret != NULL ? f->secret : "";
|
|
|
|
if (f->required && (*value == '\0')) {
|
|
char line[160];
|
|
|
|
snprintf(line, sizeof(line),
|
|
"%s is required", f->label);
|
|
set_status(dialog, line);
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
return -1;
|
|
}
|
|
(void)dc_json_object_set(target, f->key,
|
|
dc_json_new_string(value));
|
|
} else if (f->type == F_INT) {
|
|
char *value = XmTextFieldGetString(f->widget);
|
|
char *end = NULL;
|
|
long long number = 0;
|
|
|
|
if (value != NULL && *value != '\0') {
|
|
errno = 0;
|
|
number = strtoll(value, &end, 10);
|
|
if (errno != 0 || end == value || *end != '\0') {
|
|
set_status(dialog, "not a number");
|
|
XtFree(value);
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
return -1;
|
|
}
|
|
}
|
|
XtFree(value);
|
|
(void)dc_json_object_set(target, f->key,
|
|
dc_json_new_int(number));
|
|
} else if (f->type == F_BOOL) {
|
|
(void)dc_json_object_set(target, f->key,
|
|
dc_json_new_bool(XmToggleButtonGetState(
|
|
f->widget)));
|
|
} else if (f->type == F_CHOICE) {
|
|
(void)dc_json_object_set(target, f->key,
|
|
dc_json_new_string(f->choice != NULL ?
|
|
f->choice : ""));
|
|
}
|
|
}
|
|
*settings_out = settings;
|
|
*secrets_out = secrets;
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
refresh_list(DtAccountsDialog *dialog)
|
|
{
|
|
size_t i;
|
|
|
|
XmListDeleteAllItems(dialog->list);
|
|
for (i = 0; i < dc_model_account_count(dialog->model); i++) {
|
|
DcAccount *account = dc_model_account_at(dialog->model, i);
|
|
const char *label = dc_account_label(account);
|
|
XmString item;
|
|
|
|
if (label == NULL || *label == '\0')
|
|
label = dc_account_id(account);
|
|
item = xs(label);
|
|
XmListAddItemUnselected(dialog->list, item, 0);
|
|
XmStringFree(item);
|
|
}
|
|
if (dc_model_account_count(dialog->model) == 0) {
|
|
XmString item = xs(dc_text(TX_NO_ACCOUNTS_YET, "no accounts yet"));
|
|
|
|
XmListAddItemUnselected(dialog->list, item, 0);
|
|
XmStringFree(item);
|
|
}
|
|
if (dialog->selected != NULL) {
|
|
for (i = 0; i < dc_model_account_count(dialog->model); i++) {
|
|
DcAccount *account = dc_model_account_at(dialog->model,
|
|
i);
|
|
|
|
if (strcmp(dc_account_id(account),
|
|
dialog->selected) == 0) {
|
|
XmListSelectPos(dialog->list, (int)i + 1,
|
|
True);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (dc_model_account_count(dialog->model) > 0)
|
|
XmListSelectPos(dialog->list, 1, True);
|
|
}
|
|
|
|
static void
|
|
selection_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
DtAccountsDialog *dialog = client;
|
|
XmListCallbackStruct *cbs = (XmListCallbackStruct *)call;
|
|
int pos = cbs->item_position;
|
|
DcAccount *account;
|
|
AccountData *data;
|
|
|
|
(void)w;
|
|
if (pos < 1 || (size_t)pos > dc_model_account_count(dialog->model))
|
|
return;
|
|
account = dc_model_account_at(dialog->model, (size_t)pos - 1);
|
|
free(dialog->selected);
|
|
dialog->selected = strdup(dc_account_id(account));
|
|
data = find_data(dialog, dialog->selected);
|
|
if (data != NULL)
|
|
load_account(dialog, data);
|
|
else
|
|
set_defaults(dialog);
|
|
}
|
|
|
|
static void
|
|
add_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
DtAccountsDialog *dialog = client;
|
|
char id[32];
|
|
int i;
|
|
DcAccount *account;
|
|
|
|
(void)w;
|
|
(void)call;
|
|
for (i = 1; i < 1000; i++) {
|
|
snprintf(id, sizeof(id), "acct%d", i);
|
|
if (dc_model_find_account(dialog->model, id) == NULL)
|
|
break;
|
|
}
|
|
account = dc_model_add_account(dialog->model, id, "demo",
|
|
"New account");
|
|
if (account == NULL)
|
|
return;
|
|
(void)ensure_data(dialog, id);
|
|
free(dialog->selected);
|
|
dialog->selected = strdup(id);
|
|
refresh_list(dialog);
|
|
set_defaults(dialog);
|
|
set_status(dialog, "new account; fill the fields and Save");
|
|
}
|
|
|
|
static void
|
|
delete_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
DtAccountsDialog *dialog = client;
|
|
size_t i;
|
|
|
|
(void)w;
|
|
(void)call;
|
|
if (dialog->selected == NULL)
|
|
return;
|
|
(void)dc_model_remove_account(dialog->model, dialog->selected);
|
|
for (i = 0; i < dialog->ndata; i++) {
|
|
if (strcmp(dialog->data[i].id, dialog->selected) == 0) {
|
|
free(dialog->data[i].id);
|
|
free(dialog->data[i].protocol);
|
|
free(dialog->data[i].label);
|
|
free(dialog->data[i].adapter);
|
|
dc_json_free(dialog->data[i].settings);
|
|
dc_json_free(dialog->data[i].secrets);
|
|
memmove(&dialog->data[i], &dialog->data[i + 1],
|
|
(dialog->ndata - i - 1) * sizeof(*dialog->data));
|
|
dialog->ndata--;
|
|
break;
|
|
}
|
|
}
|
|
free(dialog->selected);
|
|
dialog->selected = NULL;
|
|
refresh_list(dialog);
|
|
set_status(dialog, "account deleted (demo)");
|
|
}
|
|
|
|
static void
|
|
save_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
DtAccountsDialog *dialog = client;
|
|
DcJson *settings = NULL;
|
|
DcJson *secrets = NULL;
|
|
DcJson *parsed_settings = NULL;
|
|
DcJson *parsed_secrets = NULL;
|
|
DcJson *root;
|
|
DcJson *array;
|
|
DcJson *secroot;
|
|
DcJson *secmap;
|
|
AccountData *data;
|
|
const char *label;
|
|
const char *protocol;
|
|
const char *adapter;
|
|
const char *settings_json;
|
|
const char *secrets_json;
|
|
size_t i;
|
|
|
|
(void)w;
|
|
(void)call;
|
|
if (dialog->selected == NULL) {
|
|
set_status(dialog, "select or add an account first");
|
|
return;
|
|
}
|
|
if (collect(dialog, &settings, &secrets) != 0)
|
|
return;
|
|
label = dc_json_string(dc_json_get(settings, "label"));
|
|
protocol = dc_json_string(dc_json_get(settings, "protocol"));
|
|
adapter = dc_json_string(dc_json_get(settings, "adapter"));
|
|
settings_json = dc_json_string(dc_json_get(settings,
|
|
"settings_json"));
|
|
secrets_json = dc_json_string(dc_json_get(secrets, "secrets_json"));
|
|
/* the secret field keeps the last typed value in Field.secret; the
|
|
* collected object only has the masked text, so take it from there */
|
|
for (i = 0; i < dialog->nfields; i++)
|
|
if (strcmp(dialog->fields[i].key, "secrets_json") == 0)
|
|
secrets_json = dialog->fields[i].secret;
|
|
if (settings_json != NULL && *settings_json != '\0')
|
|
parsed_settings = dc_json_parse(settings_json,
|
|
strlen(settings_json), NULL, 0);
|
|
if (parsed_settings == NULL)
|
|
parsed_settings = dc_json_new_object();
|
|
if (secrets_json != NULL && *secrets_json != '\0')
|
|
parsed_secrets = dc_json_parse(secrets_json,
|
|
strlen(secrets_json), NULL, 0);
|
|
if (parsed_secrets == NULL)
|
|
parsed_secrets = dc_json_new_object();
|
|
if (parsed_settings == NULL || parsed_secrets == NULL) {
|
|
set_status(dialog, "cannot parse the JSON");
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
dc_json_free(parsed_settings);
|
|
dc_json_free(parsed_secrets);
|
|
return;
|
|
}
|
|
data = ensure_data(dialog, dialog->selected);
|
|
if (data == NULL) {
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
dc_json_free(parsed_settings);
|
|
dc_json_free(parsed_secrets);
|
|
return;
|
|
}
|
|
free(data->protocol);
|
|
free(data->label);
|
|
free(data->adapter);
|
|
data->protocol = strdup(protocol != NULL ? protocol : "irc");
|
|
data->label = strdup(label != NULL && *label != '\0' ? label :
|
|
dialog->selected);
|
|
data->adapter = strdup(adapter != NULL ? adapter : "");
|
|
dc_json_free(data->settings);
|
|
dc_json_free(data->secrets);
|
|
data->settings = parsed_settings;
|
|
data->secrets = parsed_secrets;
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
|
|
root = dc_json_new_object();
|
|
array = dc_json_new_array();
|
|
secroot = dc_json_new_object();
|
|
secmap = dc_json_new_object();
|
|
if (root == NULL || array == NULL || secroot == NULL ||
|
|
secmap == NULL) {
|
|
dc_json_free(root);
|
|
dc_json_free(array);
|
|
dc_json_free(secroot);
|
|
dc_json_free(secmap);
|
|
set_status(dialog, "out of memory");
|
|
return;
|
|
}
|
|
for (i = 0; i < dialog->ndata; i++) {
|
|
AccountData *d = &dialog->data[i];
|
|
DcJson *entry = dc_json_new_object();
|
|
DcJson *s = clone_json(d->settings);
|
|
DcJson *sec = clone_json(d->secrets);
|
|
|
|
(void)dc_json_object_set(entry, "id",
|
|
dc_json_new_string(d->id));
|
|
(void)dc_json_object_set(entry, "protocol",
|
|
dc_json_new_string(d->protocol != NULL ? d->protocol :
|
|
"irc"));
|
|
(void)dc_json_object_set(entry, "label",
|
|
dc_json_new_string(d->label != NULL ? d->label : d->id));
|
|
(void)dc_json_object_set(entry, "adapter",
|
|
dc_json_new_string(d->adapter != NULL ? d->adapter :
|
|
""));
|
|
(void)dc_json_object_set(entry, "enabled",
|
|
dc_json_new_bool(1));
|
|
(void)dc_json_object_set(entry, "settings", s);
|
|
(void)dc_json_object_set(secmap, d->id, sec);
|
|
(void)dc_json_array_append(array, entry);
|
|
(void)dc_model_add_account(dialog->model, d->id,
|
|
d->protocol != NULL ? d->protocol : "irc",
|
|
d->label != NULL ? d->label : d->id);
|
|
}
|
|
(void)dc_json_object_set(root, "accounts", array);
|
|
(void)dc_json_object_set(secroot, "accounts", secmap);
|
|
if (dialog->save == NULL)
|
|
set_status(dialog, "no account store in this mode");
|
|
else if (dialog->save(dialog->save_arg, root, secroot) != 0)
|
|
set_status(dialog, "could not write accounts.json/secrets.json");
|
|
else
|
|
set_status(dialog, "saved; the adapters restart from the files");
|
|
dc_json_free(root);
|
|
dc_json_free(secroot);
|
|
refresh_list(dialog);
|
|
}
|
|
|
|
static void
|
|
close_cb(Widget w, XtPointer client, XtPointer call)
|
|
{
|
|
DtAccountsDialog *dialog = client;
|
|
|
|
(void)w;
|
|
(void)call;
|
|
if (dialog->closed != NULL)
|
|
dialog->closed(dialog->arg);
|
|
dtchat_accounts_dialog_destroy(dialog);
|
|
}
|
|
|
|
DtAccountsDialog *
|
|
dtchat_accounts_dialog_create(Widget parent, DcModel *model,
|
|
const DcJson *schema, const DcJson *accounts, const DcJson *secrets,
|
|
DtAccountsSaveFn save, void *save_arg, DtAccountsClosedFn closed,
|
|
void *arg)
|
|
{
|
|
DtAccountsDialog *dialog;
|
|
Widget form, list_parent, buttons, status;
|
|
Widget saveb, add, del, close;
|
|
static int dialog_no;
|
|
char name[32];
|
|
XmString x;
|
|
|
|
if (parent == NULL || model == NULL)
|
|
return NULL;
|
|
dialog = calloc(1, sizeof(*dialog));
|
|
if (dialog == NULL)
|
|
return NULL;
|
|
dialog->model = model;
|
|
dialog->accounts_json = accounts;
|
|
dialog->secrets_json = secrets;
|
|
dialog->save = save;
|
|
dialog->save_arg = save_arg;
|
|
dialog->closed = closed;
|
|
dialog->arg = arg;
|
|
snprintf(name, sizeof(name), "accountsDialog%d", ++dialog_no);
|
|
form = XmCreateFormDialog(parent, name, NULL, 0);
|
|
dialog->shell = XtParent(form);
|
|
XtVaSetValues(dialog->shell, XmNtitle,
|
|
(char *)dc_text(TX_ACCOUNTS, "Accounts"),
|
|
XmNwidth, 760, XmNheight, 500, NULL);
|
|
|
|
dialog->list = XmCreateScrolledList(form, "accounts", NULL, 0);
|
|
list_parent = XtParent(dialog->list);
|
|
XtVaSetValues(list_parent,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNwidth, 210, NULL);
|
|
XtVaSetValues(dialog->list, XmNvisibleItemCount, 16,
|
|
XmNselectionPolicy, XmBROWSE_SELECT, NULL);
|
|
XtManageChild(dialog->list);
|
|
XtAddCallback(dialog->list, XmNbrowseSelectionCallback, selection_cb,
|
|
dialog);
|
|
|
|
buttons = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass,
|
|
form, XmNorientation, XmHORIZONTAL, XmNspacing, 6,
|
|
XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, NULL);
|
|
saveb = XmCreatePushButton(buttons, "save", NULL, 0);
|
|
x = xs(dc_text(TX_SAVE, "Save"));
|
|
XtVaSetValues(saveb, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
XtAddCallback(saveb, XmNactivateCallback, save_cb, dialog);
|
|
XtManageChild(saveb);
|
|
add = XmCreatePushButton(buttons, "add", NULL, 0);
|
|
x = xs(dc_text(TX_ADD, "Add"));
|
|
XtVaSetValues(add, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
XtAddCallback(add, XmNactivateCallback, add_cb, dialog);
|
|
XtManageChild(add);
|
|
del = XmCreatePushButton(buttons, "delete", NULL, 0);
|
|
x = xs(dc_text(TX_DELETE, "Delete"));
|
|
XtVaSetValues(del, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
XtAddCallback(del, XmNactivateCallback, delete_cb, dialog);
|
|
XtManageChild(del);
|
|
close = XmCreatePushButton(buttons, "close", NULL, 0);
|
|
x = xs(dc_text(TX_CLOSE, "Close"));
|
|
XtVaSetValues(close, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
XtAddCallback(close, XmNactivateCallback, close_cb, dialog);
|
|
XtManageChild(close);
|
|
|
|
status = XmCreateLabel(form, "status", NULL, 0);
|
|
x = xs("select an account or add one");
|
|
XtVaSetValues(status, XmNlabelString, x,
|
|
XmNalignment, XmALIGNMENT_BEGINNING,
|
|
XmNbottomAttachment, XmATTACH_WIDGET,
|
|
XmNbottomWidget, buttons,
|
|
XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNheight, 22, XmNmarginWidth, 4, NULL);
|
|
XmStringFree(x);
|
|
dialog->status = status;
|
|
XtManageChild(status);
|
|
XtVaSetValues(list_parent,
|
|
XmNbottomAttachment, XmATTACH_WIDGET,
|
|
XmNbottomWidget, status, NULL);
|
|
|
|
dialog->fields_form = XtVaCreateManagedWidget("fields",
|
|
xmFormWidgetClass, form,
|
|
XmNtopAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_WIDGET,
|
|
XmNleftWidget, list_parent,
|
|
XmNrightAttachment, XmATTACH_FORM,
|
|
XmNbottomAttachment, XmATTACH_WIDGET,
|
|
XmNbottomWidget, status, NULL);
|
|
if (build_fields(dialog, schema) != 0) {
|
|
dtchat_accounts_dialog_destroy(dialog);
|
|
return NULL;
|
|
}
|
|
{
|
|
const DcJson *list = dc_json_get(accounts, "accounts");
|
|
const DcJson *secmap = dc_json_get(secrets, "accounts");
|
|
size_t k;
|
|
|
|
for (k = 0; k < dc_json_array_size(list); k++) {
|
|
const DcJson *entry = dc_json_array_get(list, k);
|
|
const char *id = dc_json_string(dc_json_get(entry,
|
|
"id"));
|
|
const char *protocol = dc_json_string(dc_json_get(
|
|
entry, "protocol"));
|
|
const char *label = dc_json_string(dc_json_get(entry,
|
|
"label"));
|
|
const char *adapter = dc_json_string(dc_json_get(entry,
|
|
"adapter"));
|
|
const DcJson *settings = dc_json_get(entry,
|
|
"settings");
|
|
AccountData *d;
|
|
|
|
if (id == NULL)
|
|
continue;
|
|
(void)dc_model_add_account(model, id,
|
|
protocol != NULL ? protocol : "irc",
|
|
label != NULL ? label : id);
|
|
d = ensure_data(dialog, id);
|
|
if (d == NULL)
|
|
continue;
|
|
free(d->protocol);
|
|
free(d->label);
|
|
free(d->adapter);
|
|
d->protocol = strdup(protocol != NULL ? protocol :
|
|
"irc");
|
|
d->label = strdup(label != NULL ? label : id);
|
|
d->adapter = strdup(adapter != NULL ? adapter : "");
|
|
dc_json_free(d->settings);
|
|
d->settings = clone_json(settings);
|
|
dc_json_free(d->secrets);
|
|
d->secrets = clone_json(dc_json_get(secmap, id));
|
|
}
|
|
}
|
|
refresh_list(dialog);
|
|
if (dc_model_account_count(model) > 0)
|
|
XmListSelectPos(dialog->list, 1, True);
|
|
else
|
|
set_defaults(dialog);
|
|
|
|
XtManageChild(form);
|
|
XtPopup(dialog->shell, XtGrabNone);
|
|
return dialog;
|
|
}
|
|
|
|
void
|
|
dtchat_accounts_dialog_destroy(DtAccountsDialog *dialog)
|
|
{
|
|
size_t i, j;
|
|
|
|
if (dialog == NULL)
|
|
return;
|
|
if (dialog->shell != NULL)
|
|
XtDestroyWidget(dialog->shell);
|
|
for (i = 0; i < dialog->nfields; i++) {
|
|
Field *f = &dialog->fields[i];
|
|
|
|
free(f->key);
|
|
free(f->label);
|
|
free(f->def);
|
|
free(f->choice);
|
|
free(f->secret);
|
|
for (j = 0; j < f->nchoices; j++) {
|
|
free(f->values[j]);
|
|
free(f->labels[j]);
|
|
}
|
|
free(f->values);
|
|
free(f->labels);
|
|
}
|
|
free(dialog->fields);
|
|
for (i = 0; i < dialog->ndata; i++) {
|
|
free(dialog->data[i].id);
|
|
free(dialog->data[i].protocol);
|
|
free(dialog->data[i].label);
|
|
free(dialog->data[i].adapter);
|
|
dc_json_free(dialog->data[i].settings);
|
|
dc_json_free(dialog->data[i].secrets);
|
|
}
|
|
free(dialog->data);
|
|
free(dialog->selected);
|
|
free(dialog);
|
|
}
|
|
|
|
Widget
|
|
dtchat_accounts_dialog_shell(const DtAccountsDialog *dialog)
|
|
{
|
|
return dialog ? dialog->shell : NULL;
|
|
}
|