dtchat: make the account editor persistent (T2.4)
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.
This commit is contained in:
1 parent
790669d989
commit
f0144bc092
6 files changed
+493
-113
No files matched your search
+160
-45
@@ -75,6 +75,9 @@ struct DtAccounts {
|
||||
int file_seq;
|
||||
int test_sent;
|
||||
Incoming *incoming;
|
||||
DcJson *accounts_json;
|
||||
DcJson *secrets_json;
|
||||
char *adapter_dir;
|
||||
};
|
||||
|
||||
static void poll_cb(XtPointer client, XtIntervalId *id);
|
||||
@@ -848,38 +851,47 @@ account_path(const char *adapter_dir, const char *adapter,
|
||||
adapter_dir != NULL ? adapter_dir : ".", protocol);
|
||||
}
|
||||
|
||||
DtAccounts *
|
||||
dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
const char *config_dir, const char *adapter_dir)
|
||||
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 void
|
||||
stop_adapters(DtAccounts *accounts)
|
||||
{
|
||||
DtAccounts *accounts;
|
||||
DcJson *json;
|
||||
DcJson *secrets;
|
||||
DcJson *list;
|
||||
size_t i;
|
||||
|
||||
if (shell == NULL || ui == NULL || model == NULL)
|
||||
return NULL;
|
||||
accounts = calloc(1, sizeof(*accounts));
|
||||
if (accounts == NULL)
|
||||
return NULL;
|
||||
accounts->shell = shell;
|
||||
accounts->ui = ui;
|
||||
accounts->model = model;
|
||||
accounts->store = dc_store_open(config_dir);
|
||||
if (accounts->store == NULL) {
|
||||
free(accounts);
|
||||
return NULL;
|
||||
for (i = 0; i < accounts->count; i++) {
|
||||
if (accounts->run[i].adapter != NULL) {
|
||||
dc_adapter_stop(accounts->run[i].adapter);
|
||||
dc_adapter_free(accounts->run[i].adapter);
|
||||
accounts->run[i].adapter = NULL;
|
||||
}
|
||||
}
|
||||
accounts->loop = dc_loop_new();
|
||||
if (accounts->loop == NULL) {
|
||||
dc_store_close(accounts->store);
|
||||
free(accounts);
|
||||
return NULL;
|
||||
}
|
||||
json = dc_store_load_accounts(accounts->store);
|
||||
secrets = dc_store_load_secrets(accounts->store);
|
||||
list = dc_json_get(json, "accounts");
|
||||
accounts->count = 0;
|
||||
}
|
||||
|
||||
/* start one adapter per enabled account in the stored JSON */
|
||||
static void
|
||||
accounts_spawn(DtAccounts *accounts)
|
||||
{
|
||||
const DcJson *list = dc_json_get(accounts->accounts_json, "accounts");
|
||||
const DcJson *secrets = accounts->secrets_json;
|
||||
size_t i;
|
||||
|
||||
accounts->count = 0;
|
||||
for (i = 0; list != NULL && i < dc_json_array_size(list) &&
|
||||
accounts->count < MAX_ACCOUNTS; i++) {
|
||||
const DcJson *entry = dc_json_array_get(list, i);
|
||||
@@ -908,13 +920,12 @@ dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
protocol != NULL ? protocol : "unknown");
|
||||
snprintf(run->label, sizeof(run->label), "%s",
|
||||
label != NULL ? label : id);
|
||||
account_path(adapter_dir, adapter, run->protocol, run->path,
|
||||
sizeof(run->path));
|
||||
account_path(accounts->adapter_dir, adapter, run->protocol,
|
||||
run->path, sizeof(run->path));
|
||||
settings = dc_json_get(entry, "settings");
|
||||
secret = (DcJson *)secrets_for(secrets, id);
|
||||
if (secret == NULL) {
|
||||
if (secret == NULL)
|
||||
secret = secret_empty = dc_json_new_object();
|
||||
}
|
||||
if (settings == NULL) {
|
||||
empty = dc_json_new_object();
|
||||
settings = empty;
|
||||
@@ -932,15 +943,124 @@ dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
run->path);
|
||||
continue;
|
||||
}
|
||||
(void)dc_model_add_account(model, run->id, run->protocol,
|
||||
run->label);
|
||||
(void)dc_model_add_account(accounts->model, run->id,
|
||||
run->protocol, run->label);
|
||||
accounts->count++;
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts %s: started %s\n", run->id,
|
||||
run->path);
|
||||
}
|
||||
dc_json_free(json);
|
||||
dc_json_free(secrets);
|
||||
}
|
||||
|
||||
static int
|
||||
account_in_json(const DcJson *json, const char *id)
|
||||
{
|
||||
const DcJson *list = dc_json_get(json, "accounts");
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < dc_json_array_size(list); i++) {
|
||||
const char *want = dc_json_string(dc_json_get(
|
||||
dc_json_array_get(list, i), "id"));
|
||||
|
||||
if (want != NULL && strcmp(want, id) == 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* the editor's writer: files, then the adapters restart from the new set */
|
||||
static int
|
||||
accounts_save_cb(void *arg, const DcJson *accounts_json,
|
||||
const DcJson *secrets_json)
|
||||
{
|
||||
DtAccounts *accounts = arg;
|
||||
DcJson *new_accounts;
|
||||
DcJson *new_secrets;
|
||||
size_t i;
|
||||
|
||||
if (accounts == NULL || accounts->store == NULL)
|
||||
return -1;
|
||||
if (dc_store_save_accounts(accounts->store, accounts_json) != 0)
|
||||
return -1;
|
||||
if (dc_store_save_secrets(accounts->store, secrets_json) != 0)
|
||||
return -1;
|
||||
new_accounts = clone_json(accounts_json);
|
||||
new_secrets = clone_json(secrets_json);
|
||||
if (new_accounts == NULL || new_secrets == NULL) {
|
||||
dc_json_free(new_accounts);
|
||||
dc_json_free(new_secrets);
|
||||
return -1;
|
||||
}
|
||||
while (accounts->incoming != NULL)
|
||||
incoming_free(accounts, accounts->incoming, 1);
|
||||
stop_adapters(accounts);
|
||||
dc_json_free(accounts->accounts_json);
|
||||
dc_json_free(accounts->secrets_json);
|
||||
accounts->accounts_json = new_accounts;
|
||||
accounts->secrets_json = new_secrets;
|
||||
/* accounts that were deleted in the editor leave the model */
|
||||
for (i = dc_model_account_count(accounts->model); i-- > 0;) {
|
||||
DcAccount *acct = dc_model_account_at(accounts->model, i);
|
||||
|
||||
if (acct != NULL &&
|
||||
!account_in_json(accounts->accounts_json,
|
||||
dc_account_id(acct)))
|
||||
(void)dc_model_remove_account(accounts->model,
|
||||
dc_account_id(acct));
|
||||
}
|
||||
accounts_spawn(accounts);
|
||||
dtchat_ui_set_accounts_data(accounts->ui, accounts->accounts_json,
|
||||
accounts->secrets_json, accounts_save_cb, accounts);
|
||||
dtchat_ui_refresh(accounts->ui);
|
||||
if (debug_on())
|
||||
fprintf(stderr, "accounts: saved and restarted (%zu)\n",
|
||||
accounts->count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DtAccounts *
|
||||
dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
const char *config_dir, const char *adapter_dir)
|
||||
{
|
||||
DtAccounts *accounts;
|
||||
|
||||
if (shell == NULL || ui == NULL || model == NULL)
|
||||
return NULL;
|
||||
accounts = calloc(1, sizeof(*accounts));
|
||||
if (accounts == NULL)
|
||||
return NULL;
|
||||
accounts->shell = shell;
|
||||
accounts->ui = ui;
|
||||
accounts->model = model;
|
||||
accounts->adapter_dir = strdup(adapter_dir != NULL ? adapter_dir : ".");
|
||||
accounts->store = dc_store_open(config_dir);
|
||||
if (accounts->store == NULL || accounts->adapter_dir == NULL) {
|
||||
dc_store_close(accounts->store);
|
||||
free(accounts->adapter_dir);
|
||||
free(accounts);
|
||||
return NULL;
|
||||
}
|
||||
accounts->loop = dc_loop_new();
|
||||
if (accounts->loop == NULL) {
|
||||
dc_store_close(accounts->store);
|
||||
free(accounts->adapter_dir);
|
||||
free(accounts);
|
||||
return NULL;
|
||||
}
|
||||
accounts->accounts_json = dc_store_load_accounts(accounts->store);
|
||||
accounts->secrets_json = dc_store_load_secrets(accounts->store);
|
||||
if (accounts->accounts_json == NULL || accounts->secrets_json == NULL) {
|
||||
dc_json_free(accounts->accounts_json);
|
||||
dc_json_free(accounts->secrets_json);
|
||||
dc_loop_free(accounts->loop);
|
||||
dc_store_close(accounts->store);
|
||||
free(accounts->adapter_dir);
|
||||
free(accounts);
|
||||
return NULL;
|
||||
}
|
||||
accounts_spawn(accounts);
|
||||
dtchat_ui_set_accounts_data(ui, accounts->accounts_json,
|
||||
accounts->secrets_json, accounts_save_cb, accounts);
|
||||
dtchat_ui_refresh(ui);
|
||||
dtchat_ui_set_send_hook(ui, accounts_send_hook, accounts);
|
||||
dtchat_ui_set_file_send(ui, accounts_file_send, accounts);
|
||||
@@ -953,8 +1073,6 @@ dtchat_accounts_start(Widget shell, DtChatUi *ui, DcModel *model,
|
||||
void
|
||||
dtchat_accounts_stop(DtAccounts *accounts)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (accounts == NULL)
|
||||
return;
|
||||
if (accounts->timer != 0) {
|
||||
@@ -967,16 +1085,13 @@ dtchat_accounts_stop(DtAccounts *accounts)
|
||||
dtchat_ui_set_join_hook(accounts->ui, NULL, NULL);
|
||||
while (accounts->incoming != NULL)
|
||||
incoming_free(accounts, accounts->incoming, 1);
|
||||
for (i = 0; i < accounts->count; i++) {
|
||||
if (accounts->run[i].adapter != NULL) {
|
||||
dc_adapter_stop(accounts->run[i].adapter);
|
||||
dc_adapter_free(accounts->run[i].adapter);
|
||||
accounts->run[i].adapter = NULL;
|
||||
}
|
||||
}
|
||||
stop_adapters(accounts);
|
||||
if (accounts->loop != NULL)
|
||||
dc_loop_free(accounts->loop);
|
||||
if (accounts->store != NULL)
|
||||
dc_store_close(accounts->store);
|
||||
dc_json_free(accounts->accounts_json);
|
||||
dc_json_free(accounts->secrets_json);
|
||||
free(accounts->adapter_dir);
|
||||
free(accounts);
|
||||
}
|
||||
@@ -510,6 +510,13 @@ ui_send_test_cb(XtPointer client, XtIntervalId *id)
|
||||
(void)dtchat_ui_join(ui, join);
|
||||
}
|
||||
|
||||
static void
|
||||
ui_accounts_dialog_cb(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
(void)id;
|
||||
dtchat_ui_show_accounts((DtChatUi *)client);
|
||||
}
|
||||
|
||||
static void
|
||||
ui_join_dialog_cb(XtPointer client, XtIntervalId *id)
|
||||
{
|
||||
@@ -983,6 +990,8 @@ main(int argc, char **argv)
|
||||
XtAppAddTimeOut(app, 2500, ui_send_test_cb, ui);
|
||||
if (getenv("DTCHAT_TEST_JOIN_DIALOG") != NULL)
|
||||
XtAppAddTimeOut(app, 1500, ui_join_dialog_cb, ui);
|
||||
if (getenv("DTCHAT_TEST_ACCOUNTS_DIALOG") != NULL)
|
||||
XtAppAddTimeOut(app, 1500, ui_accounts_dialog_cb, ui);
|
||||
if (demo_request != NULL) {
|
||||
XMapRaised(XtDisplay(toplevel), XtWindow(toplevel));
|
||||
XtAppAddTimeOut(app, 700, demo_request_cb, (XtPointer)ui);
|
||||
|
||||
+261
-32
@@ -5,9 +5,11 @@
|
||||
*
|
||||
* 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).
|
||||
* 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
|
||||
|
||||
@@ -60,6 +62,9 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
char *id;
|
||||
char *protocol;
|
||||
char *label;
|
||||
char *adapter;
|
||||
DcJson *settings;
|
||||
DcJson *secrets;
|
||||
} AccountData;
|
||||
@@ -75,6 +80,10 @@ struct DtAccountsDialog {
|
||||
AccountData *data;
|
||||
size_t ndata;
|
||||
char *selected;
|
||||
const DcJson *accounts_json;
|
||||
const DcJson *secrets_json;
|
||||
DtAccountsSaveFn save;
|
||||
void *save_arg;
|
||||
DtAccountsClosedFn closed;
|
||||
void *arg;
|
||||
};
|
||||
@@ -85,6 +94,35 @@ 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)
|
||||
{
|
||||
@@ -433,6 +471,65 @@ load_account(DtAccountsDialog *dialog, AccountData *data)
|
||||
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
|
||||
@@ -544,6 +641,9 @@ ensure_data(DtAccountsDialog *dialog, const char *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) {
|
||||
@@ -747,6 +847,9 @@ delete_cb(Widget w, XtPointer client, XtPointer call)
|
||||
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],
|
||||
@@ -767,8 +870,18 @@ 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;
|
||||
char label[128] = "";
|
||||
const char *label;
|
||||
const char *protocol;
|
||||
const char *adapter;
|
||||
const char *settings_json;
|
||||
const char *secrets_json;
|
||||
size_t i;
|
||||
|
||||
(void)w;
|
||||
@@ -779,39 +892,106 @@ save_cb(Widget w, XtPointer client, XtPointer call)
|
||||
}
|
||||
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 = settings;
|
||||
data->secrets = secrets;
|
||||
for (i = 0; i < dialog->nfields; i++) {
|
||||
Field *f = &dialog->fields[i];
|
||||
data->settings = parsed_settings;
|
||||
data->secrets = parsed_secrets;
|
||||
dc_json_free(settings);
|
||||
dc_json_free(secrets);
|
||||
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (*label != '\0')
|
||||
(void)dc_model_add_account(dialog->model, dialog->selected,
|
||||
"demo", label);
|
||||
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);
|
||||
set_status(dialog, "saved (demo); secrets kept separately");
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -828,11 +1008,13 @@ close_cb(Widget w, XtPointer client, XtPointer call)
|
||||
|
||||
DtAccountsDialog *
|
||||
dtchat_accounts_dialog_create(Widget parent, DcModel *model,
|
||||
const DcJson *schema, DtAccountsClosedFn closed, void *arg)
|
||||
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 save, add, del, close;
|
||||
Widget saveb, add, del, close;
|
||||
static int dialog_no;
|
||||
char name[32];
|
||||
XmString x;
|
||||
@@ -843,6 +1025,10 @@ dtchat_accounts_dialog_create(Widget parent, DcModel *model,
|
||||
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);
|
||||
@@ -870,12 +1056,12 @@ dtchat_accounts_dialog_create(Widget parent, DcModel *model,
|
||||
XmNbottomAttachment, XmATTACH_FORM,
|
||||
XmNleftAttachment, XmATTACH_FORM,
|
||||
XmNrightAttachment, XmATTACH_FORM, NULL);
|
||||
save = XmCreatePushButton(buttons, "save", NULL, 0);
|
||||
saveb = XmCreatePushButton(buttons, "save", NULL, 0);
|
||||
x = xs(dc_text(TX_SAVE, "Save"));
|
||||
XtVaSetValues(save, XmNlabelString, x, NULL);
|
||||
XtVaSetValues(saveb, XmNlabelString, x, NULL);
|
||||
XmStringFree(x);
|
||||
XtAddCallback(save, XmNactivateCallback, save_cb, dialog);
|
||||
XtManageChild(save);
|
||||
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);
|
||||
@@ -923,6 +1109,46 @@ dtchat_accounts_dialog_create(Widget parent, DcModel *model,
|
||||
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);
|
||||
@@ -961,6 +1187,9 @@ dtchat_accounts_dialog_destroy(DtAccountsDialog *dialog)
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
|
||||
typedef struct DtAccountsDialog DtAccountsDialog;
|
||||
typedef void (*DtAccountsClosedFn)(void *arg);
|
||||
/* write the accounts and secrets (the runtime owns the files) */
|
||||
typedef int (*DtAccountsSaveFn)(void *arg, const DcJson *accounts,
|
||||
const DcJson *secrets);
|
||||
|
||||
/*
|
||||
* Build the account list and the settings form from SCHEMA (the adapter's
|
||||
@@ -20,7 +23,9 @@ typedef void (*DtAccountsClosedFn)(void *arg);
|
||||
* widgets are destroyed. MODEL is borrowed.
|
||||
*/
|
||||
DtAccountsDialog *dtchat_accounts_dialog_create(Widget parent, DcModel *model,
|
||||
const DcJson *schema, DtAccountsClosedFn closed, void *arg);
|
||||
const DcJson *schema, const DcJson *accounts, const DcJson *secrets,
|
||||
DtAccountsSaveFn save, void *save_arg, DtAccountsClosedFn closed,
|
||||
void *arg);
|
||||
void dtchat_accounts_dialog_destroy(DtAccountsDialog *dialog);
|
||||
Widget dtchat_accounts_dialog_shell(const DtAccountsDialog *dialog);
|
||||
|
||||
|
||||
+52
-35
@@ -70,6 +70,10 @@ struct DtChatUi {
|
||||
void *typing_arg;
|
||||
DtChatJoinFn join_fn;
|
||||
void *join_arg;
|
||||
const DcJson *accounts_json;
|
||||
const DcJson *secrets_json;
|
||||
DtAccountsSaveFn accounts_save;
|
||||
void *accounts_save_arg;
|
||||
XtIntervalId typing_timer;
|
||||
int typing_on;
|
||||
int suppress_typing;
|
||||
@@ -558,52 +562,45 @@ schema_field(const char *key, const char *label, const char *type,
|
||||
}
|
||||
|
||||
static DcJson *
|
||||
demo_schema(void)
|
||||
accounts_schema(void)
|
||||
{
|
||||
DcJson *schema = dc_json_new_array();
|
||||
DcJson *entry;
|
||||
DcJson *choices;
|
||||
DcJson *choice;
|
||||
|
||||
entry = schema_field("host", "Server", "text", 1);
|
||||
(void)dc_json_object_set(entry, "default",
|
||||
dc_json_new_string("irc.libera.chat"));
|
||||
entry = schema_field("label", "Label", "text", 1);
|
||||
(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);
|
||||
entry = schema_field("protocol", "Protocol", "choice", 1);
|
||||
choices = dc_json_new_array();
|
||||
{
|
||||
DcJson *utf8 = dc_json_new_object();
|
||||
DcJson *latin1 = dc_json_new_object();
|
||||
static const char *const protocols[] = {
|
||||
"irc", "xmpp", "telegram", "whatsapp", "demo"
|
||||
};
|
||||
size_t i;
|
||||
|
||||
(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);
|
||||
for (i = 0; i < sizeof(protocols) / sizeof(protocols[0]); i++) {
|
||||
choice = dc_json_new_object();
|
||||
(void)dc_json_object_set(choice, "value",
|
||||
dc_json_new_string(protocols[i]));
|
||||
(void)dc_json_object_set(choice, "label",
|
||||
dc_json_new_string(protocols[i]));
|
||||
(void)dc_json_array_append(choices, choice);
|
||||
}
|
||||
}
|
||||
(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);
|
||||
|
||||
entry = schema_field("adapter", "Adapter path", "text", 0);
|
||||
(void)dc_json_array_append(schema, entry);
|
||||
|
||||
entry = schema_field("settings_json", "Settings (JSON)", "text", 0);
|
||||
(void)dc_json_array_append(schema, entry);
|
||||
|
||||
entry = schema_field("secrets_json", "Secrets (JSON)", "secret", 0);
|
||||
(void)dc_json_array_append(schema, entry);
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
@@ -630,11 +627,12 @@ accounts_cb(Widget w, XtPointer client, XtPointer call)
|
||||
XMapRaised(XtDisplay(shell), XtWindow(shell));
|
||||
return;
|
||||
}
|
||||
schema = demo_schema();
|
||||
schema = accounts_schema();
|
||||
if (schema == NULL)
|
||||
return;
|
||||
ui->accounts = dtchat_accounts_dialog_create(ui->shell, ui->model,
|
||||
schema, accounts_closed, ui);
|
||||
schema, ui->accounts_json, ui->secrets_json, ui->accounts_save,
|
||||
ui->accounts_save_arg, accounts_closed, ui);
|
||||
dc_json_free(schema);
|
||||
if (ui->accounts == NULL)
|
||||
set_status(ui, "could not open the account editor");
|
||||
@@ -795,6 +793,25 @@ dtchat_ui_set_join_hook(DtChatUi *ui, DtChatJoinFn fn, void *arg)
|
||||
ui->join_arg = arg;
|
||||
}
|
||||
|
||||
void
|
||||
dtchat_ui_show_accounts(DtChatUi *ui)
|
||||
{
|
||||
if (ui != NULL)
|
||||
accounts_cb(NULL, ui, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
dtchat_ui_set_accounts_data(DtChatUi *ui, const DcJson *accounts,
|
||||
const DcJson *secrets, DtAccountsSaveFn save, void *arg)
|
||||
{
|
||||
if (ui == NULL)
|
||||
return;
|
||||
ui->accounts_json = accounts;
|
||||
ui->secrets_json = secrets;
|
||||
ui->accounts_save = save;
|
||||
ui->accounts_save_arg = arg;
|
||||
}
|
||||
|
||||
static void
|
||||
make_menu_bar(DtChatUi *ui, Widget form, Widget *bar_out)
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <X11/Intrinsic.h>
|
||||
|
||||
#include "dc_internal.h"
|
||||
#include "ui_accounts.h"
|
||||
|
||||
typedef struct DtChatUi DtChatUi;
|
||||
|
||||
@@ -67,6 +68,10 @@ int dtchat_ui_smoke(DtChatUi *ui);
|
||||
void dtchat_ui_set_send_hook(DtChatUi *ui, DtChatSendFn fn, void *arg);
|
||||
void dtchat_ui_set_typing_hook(DtChatUi *ui, DtChatTypingFn fn, void *arg);
|
||||
void dtchat_ui_set_join_hook(DtChatUi *ui, DtChatJoinFn fn, void *arg);
|
||||
/* the account files and the writer the editor uses (the accounts runtime) */
|
||||
void dtchat_ui_set_accounts_data(DtChatUi *ui, const DcJson *accounts,
|
||||
const DcJson *secrets, DtAccountsSaveFn save, void *arg);
|
||||
void dtchat_ui_show_accounts(DtChatUi *ui);
|
||||
/* ask for a channel/room and join it (the dialog), or join CONV directly */
|
||||
void dtchat_ui_join_dialog(DtChatUi *ui);
|
||||
int dtchat_ui_join(DtChatUi *ui, const char *conv);
|
||||
|
||||
Reference in new issue
Block a user