Allow for prefill of form elements, improved I18N.

This commit is contained in:
Martin Preuss
2023-08-09 00:55:07 +02:00
parent 0cc498d830
commit fc2c18b489
3 changed files with 63 additions and 27 deletions

View File

@@ -18,15 +18,23 @@
#include "aqhome/http/httpservice_conf.h" #include "aqhome/http/httpservice_conf.h"
#include <gwenhywfar/gwenhywfar.h> #include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h> #include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint.h> #include <gwenhywfar/i18n.h>
#include <gwenhywfar/mdigest.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/timestamp.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _handleUrl(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq); static int _handleUrl(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static int _handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq); static int _handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static int _handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq); static int _handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
@@ -35,10 +43,14 @@ static void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_B
static void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf); static void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf);
static int _handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq); static int _handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
static int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf); static int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *dbValues, GWEN_BUFFER *pageBuf);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
AQH_HTTP_URLHANDLER *AQH_RoomsHttpUrlHandler_new(AQH_SERVICE *sv) AQH_HTTP_URLHANDLER *AQH_RoomsHttpUrlHandler_new(AQH_SERVICE *sv)
{ {
@@ -211,9 +223,13 @@ void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *
if (sto) { if (sto) {
const AQH_ROOM_LIST *rl; const AQH_ROOM_LIST *rl;
GWEN_Buffer_AppendString(pageBuf, "<h2>Rooms</h2>"); GWEN_Buffer_AppendArgs(pageBuf,
GWEN_Buffer_AppendString(pageBuf, "<table>"); "<h2>%s</h2>"
GWEN_Buffer_AppendString(pageBuf, "<tr><th>Id</th><th>Name</th><th>Description</th></tr>"); "<table>"
" <tr><th>Id</th><th>%s</th><th>%s</th></tr>",
I18N("Rooms"),
I18N("Name"),
I18N("Description"));
rl=AQH_Storage_GetRoomList(sto); rl=AQH_Storage_GetRoomList(sto);
if (rl) { if (rl) {
const AQH_ROOM *r; const AQH_ROOM *r;
@@ -232,15 +248,14 @@ void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *
r=AQH_Room_List_Next(r); r=AQH_Room_List_Next(r);
} }
} }
GWEN_Buffer_AppendString(pageBuf, "</table>"); GWEN_Buffer_AppendArgs(pageBuf, "</table><a href=\"/rooms/add\">%s</a><br>", I18N("Add Room"));
GWEN_Buffer_AppendString(pageBuf, "<a href=\"/rooms/add\">Add Room</a><br>");
} }
else { else {
GWEN_Buffer_AppendString(pageBuf, "<p>Internal error.</p>"); GWEN_Buffer_AppendString(pageBuf, "<p>Internal error.</p>");
} }
} }
else { else {
GWEN_Buffer_AppendString(pageBuf, "<p>No permissions to see list of rooms.</p>"); GWEN_Buffer_AppendArgs(pageBuf, "<p>%s</p>", I18N("No permissions to see list of rooms."));
} }
} }
@@ -253,7 +268,7 @@ void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *p
DBG_ERROR(NULL, "ADD"); DBG_ERROR(NULL, "ADD");
perms=AQH_HttpRequest_GetModulePerms(rq); perms=AQH_HttpRequest_GetModulePerms(rq);
if (perms & AQHOME_HTTP_PERMS_ADD_ROOM) { if (perms & AQHOME_HTTP_PERMS_ADD_ROOM) {
_writeAddPage(uh, rq, pageBuf); _writeAddPage(uh, rq, NULL, pageBuf);
} }
else { else {
DBG_INFO(NULL, "No permissions to add a room."); DBG_INFO(NULL, "No permissions to add a room.");
@@ -263,18 +278,36 @@ void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *p
int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf) int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *dbValues, GWEN_BUFFER *pageBuf)
{ {
int rv; const char *name=NULL;
int pos; const char *descr=NULL;
pos=GWEN_Buffer_GetPos(pageBuf); if (dbValues) {
rv=AQH_HttpService_AddFile(AQH_HttpUrlHandler_GetHttpService(uh), NULL, "roomadd.html", pageBuf); name=GWEN_DB_GetCharValue(dbValues, "name", 0, NULL);
if (rv<0) { descr=GWEN_DB_GetCharValue(dbValues, "description", 0, NULL);
DBG_ERROR(AQH_LOGDOMAIN, "Error reading file \"roomadd.html\"");
GWEN_Buffer_Crop(pageBuf, 0, pos);
GWEN_Buffer_AppendString(pageBuf, "<p>Internal error.</p>");
} }
GWEN_Buffer_AppendArgs(pageBuf,
"<h2>%s</h2><br>\n"
"<form action=\"/rooms/add\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">"
" <table>"
" <tr>"
" <td><label for=\"name\">%s: </label></td>"
" <td><input type=\"text\" name=\"name\" value=\"%s\" required></td>"
" </tr>"
" <tr>"
" <td><label for=\"description\">%s: </label></td>"
" <td><input type=\"text\" name=\"description\" value=\"%s\" ></td>"
" </tr>"
" </table>"
" <input type=\"submit\" value=\"%s\">"
"</form>",
I18N("Create a Room"),
I18N("Room Name"),
name?name:"",
I18N("Description"),
descr?descr:"",
I18N("Add Room"));
return 0; return 0;
} }
@@ -298,11 +331,11 @@ int _handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
db=AQH_HttpRequest_GetDbPostBody(rq); db=AQH_HttpRequest_GetDbPostBody(rq);
roomName=GWEN_DB_GetCharValue(db, "roomname", 0, NULL); roomName=GWEN_DB_GetCharValue(db, "name", 0, NULL);
roomDescr=GWEN_DB_GetCharValue(db, "descr", 0, NULL); roomDescr=GWEN_DB_GetCharValue(db, "description", 0, NULL);
if (!(roomName && *roomName)) { if (!(roomName && *roomName)) {
msgOut=AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", "Missing room name", 1, _writeAddPage); msgOut=AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Missing room name"), 1, db, _writeAddPage);
AQH_HttpRequest_SetResponseMsg(rq, msgOut); AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0; return 0;
} }
@@ -316,7 +349,7 @@ int _handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
if (roomDescr && *roomDescr) if (roomDescr && *roomDescr)
AQH_Room_SetDescription(r, roomDescr); AQH_Room_SetDescription(r, roomDescr);
if (AQH_Storage_GetRoomByName(sto, roomName)!=NULL) { if (AQH_Storage_GetRoomByName(sto, roomName)!=NULL) {
msgOut=AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", "Room already exists", 1, _writeAddPage); msgOut=AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Room already exists"), 1, db, _writeAddPage);
AQH_HttpRequest_SetResponseMsg(rq, msgOut); AQH_HttpRequest_SetResponseMsg(rq, msgOut);
return 0; return 0;
} }

View File

@@ -143,6 +143,7 @@ GWEN_MSG *AQH_HttpUrlHandler_CreatePageMessage(AQH_HTTP_URLHANDLER *uh,
AQH_HTTP_REQUEST *rq, AQH_HTTP_REQUEST *rq,
const char *statusTextColor, const char *statusMsg, const char *statusTextColor, const char *statusMsg,
int withHeadersAndFooters, int withHeadersAndFooters,
GWEN_DB_NODE *dbValues,
AQH_HTTP_URLHANDLER_WRITEPAGE_CB cb) AQH_HTTP_URLHANDLER_WRITEPAGE_CB cb)
{ {
GWEN_BUFFER *pageBuf; GWEN_BUFFER *pageBuf;
@@ -164,7 +165,7 @@ GWEN_MSG *AQH_HttpUrlHandler_CreatePageMessage(AQH_HTTP_URLHANDLER *uh,
if (statusMsg) if (statusMsg)
GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"%s\">%s</font></p>", statusTextColor?statusTextColor:"black", statusMsg); GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"%s\">%s</font></p>", statusTextColor?statusTextColor:"black", statusMsg);
if (cb) { if (cb) {
rv=cb(uh, rq, pageBuf); rv=cb(uh, rq, dbValues, pageBuf);
if (rv<0) { if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error adding headers"); DBG_ERROR(AQH_LOGDOMAIN, "Error adding headers");
GWEN_Buffer_free(pageBuf); GWEN_Buffer_free(pageBuf);

View File

@@ -51,11 +51,13 @@ AQHOME_API int AQH_HttpUrlHandler_HandleMessage(AQH_HTTP_URLHANDLER *uh, AQH_HTT
typedef int (*AQH_HTTP_URLHANDLER_WRITEPAGE_CB)(AQH_HTTP_URLHANDLER *uh, typedef int (*AQH_HTTP_URLHANDLER_WRITEPAGE_CB)(AQH_HTTP_URLHANDLER *uh,
AQH_HTTP_REQUEST *rq, AQH_HTTP_REQUEST *rq,
GWEN_DB_NODE *dbValues,
GWEN_BUFFER *pageBuf); GWEN_BUFFER *pageBuf);
AQHOME_API GWEN_MSG *AQH_HttpUrlHandler_CreatePageMessage(AQH_HTTP_URLHANDLER *uh, AQHOME_API GWEN_MSG *AQH_HttpUrlHandler_CreatePageMessage(AQH_HTTP_URLHANDLER *uh,
AQH_HTTP_REQUEST *rq, AQH_HTTP_REQUEST *rq,
const char *statusTextColor, const char *statusMsg, const char *statusTextColor, const char *statusMsg,
int withHeadersAndFooters, int withHeadersAndFooters,
GWEN_DB_NODE *dbValues,
AQH_HTTP_URLHANDLER_WRITEPAGE_CB cb); AQH_HTTP_URLHANDLER_WRITEPAGE_CB cb);