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).
977 lines
24 KiB
C
977 lines
24 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. Secrets are collected into a separate
|
|
* JSON object and never shown in the status line. T2.4 keeps the values in
|
|
* memory; the store/persistence wiring comes with the demo account (T2.6).
|
|
*/
|
|
#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;
|
|
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;
|
|
DtAccountsClosedFn closed;
|
|
void *arg;
|
|
};
|
|
|
|
static XmString
|
|
xs(const char *s)
|
|
{
|
|
return XmStringCreateLocalized((char *)s);
|
|
}
|
|
|
|
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 (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->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);
|
|
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;
|
|
AccountData *data;
|
|
char label[128] = "";
|
|
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;
|
|
data = ensure_data(dialog, dialog->selected);
|
|
if (data == NULL) {
|
|
dc_json_free(settings);
|
|
dc_json_free(secrets);
|
|
return;
|
|
}
|
|
dc_json_free(data->settings);
|
|
dc_json_free(data->secrets);
|
|
data->settings = settings;
|
|
data->secrets = secrets;
|
|
for (i = 0; i < dialog->nfields; i++) {
|
|
Field *f = &dialog->fields[i];
|
|
|
|
if ((strcmp(f->key, "label") == 0 ||
|
|
strcmp(f->key, "nick") == 0 ||
|
|
strcmp(f->key, "host") == 0) &&
|
|
(f->type == F_TEXT || f->type == F_INT)) {
|
|
char *value = XmTextFieldGetString(f->widget);
|
|
|
|
if (value != NULL && *value != '\0' &&
|
|
strlen(value) < sizeof(label)) {
|
|
snprintf(label, sizeof(label), "%s", value);
|
|
XtFree(value);
|
|
break;
|
|
}
|
|
XtFree(value);
|
|
}
|
|
}
|
|
if (*label != '\0')
|
|
(void)dc_model_add_account(dialog->model, dialog->selected,
|
|
"demo", label);
|
|
refresh_list(dialog);
|
|
set_status(dialog, "saved (demo); secrets kept separately");
|
|
}
|
|
|
|
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, DtAccountsClosedFn closed, void *arg)
|
|
{
|
|
DtAccountsDialog *dialog;
|
|
Widget form, list_parent, buttons, status;
|
|
Widget save, 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->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);
|
|
save = XmCreatePushButton(buttons, "save", NULL, 0);
|
|
x = xs(dc_text(TX_SAVE, "Save"));
|
|
XtVaSetValues(save, XmNlabelString, x, NULL);
|
|
XmStringFree(x);
|
|
XtAddCallback(save, XmNactivateCallback, save_cb, dialog);
|
|
XtManageChild(save);
|
|
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;
|
|
}
|
|
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);
|
|
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;
|
|
}
|