[ADD/WIP] On-the-fly forms

Still WIP-tier, also I need to make a way to index a multi-option list
like a regular array with the forms thing, the current one-at-a-time
idea is not great...
This commit is contained in:
LDA 2024-07-14 22:24:59 +02:00
commit 0cb19a15d8
4 changed files with 146 additions and 6 deletions

View file

@ -13,6 +13,7 @@ struct XMPPCommand {
/* TODO: On-the-fly generation of options */
Array *options;
XMPPOptionWriter otf;
};
XMPPCommand *
@ -35,8 +36,20 @@ XMPPBasicCmd(char *node, char *name, XMPPCmdCallback callback_funct)
/* No options -> no form required */
cmd->options = NULL;
cmd->otf = NULL;
return cmd;
}
void
XMPPCmdOptionsCreator(XMPPCommand *cmd, XMPPOptionWriter writer)
{
if (!cmd)
{
return;
}
cmd->otf = writer;
}
void
XMPPSetFormTitle(XMPPCommand *cmd, char *title)
{
@ -95,12 +108,28 @@ XMPPAddOption(XMPPCommand *cmd, XMPPOption *opt)
}
XMLElement *
XMPPFormifyCommand(XMPPCommand *cmd)
XMPPFormifyCommand(XMPPCommandManager *m, XMPPCommand *cmd, char *from)
{
XMLElement *x, *field;
size_t i;
if (!cmd || !cmd->options)
if (!cmd || !m)
{
return NULL;
}
if (cmd->otf)
{
for (i = 0; i < ArraySize(cmd->options); i++)
{
XMPPOption *opt = ArrayGet(cmd->options, i);
XMPPFreeOption(opt);
}
ArrayFree(cmd->options);
cmd->options = NULL;
cmd->otf(m, cmd, from);
}
if (!cmd->options)
{
return NULL;
}
@ -152,7 +181,7 @@ XMPPGetCommandDesc(XMPPCommand *cmd)
bool
XMPPCommandRequiresForm(XMPPCommand *cmd)
{
return cmd ? !!cmd->options : false;
return cmd ? (cmd->otf || !!cmd->options) : false;
}
void
XMPPExecuteCommand(XMPPCommandManager *m, XMPPCommand *cmd, char *from, XMLElement *to, XMLElement *form)

View file

@ -271,7 +271,7 @@ XMPPManageCommand(XMPPCommandManager *m, XMLElement *stanza, ParseeData *data)
XMLAddChild(command_xml, actions);
XMLAddChild(iq, command_xml);
x = XMPPFormifyCommand(cmd);
x = XMPPFormifyCommand(m, cmd, from);
XMLAddChild(command_xml, x);
pthread_mutex_lock(&jabber->write_lock);