mirror of
https://github.com/rkd77/elinks.git
synced 2024-11-04 08:17:17 -05:00
[mujs] localstorage.c
This commit is contained in:
parent
72893a60a3
commit
c4843f488a
@ -1,6 +1,6 @@
|
||||
top_builddir=../../../..
|
||||
include $(top_builddir)/Makefile.config
|
||||
|
||||
OBJS = attr.o attributes.o collection.o console.o forms.o history.o implementation.o keyboard.o mapa.obj
|
||||
OBJS = attr.o attributes.o collection.o console.o forms.o history.o implementation.o keyboard.o localstorage.o mapa.obj
|
||||
|
||||
include $(top_srcdir)/Makefile.lib
|
||||
|
155
src/ecmascript/libdom/mujs/localstorage.c
Normal file
155
src/ecmascript/libdom/mujs/localstorage.c
Normal file
@ -0,0 +1,155 @@
|
||||
/* The quickjs localstorage object implementation. */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "elinks.h"
|
||||
|
||||
#include "bfu/leds.h"
|
||||
#include "document/view.h"
|
||||
#include "ecmascript/ecmascript.h"
|
||||
#include "ecmascript/localstorage-db.h"
|
||||
#include "ecmascript/mujs.h"
|
||||
#include "ecmascript/mujs/localstorage.h"
|
||||
#include "session/session.h"
|
||||
#include "viewer/text/vs.h"
|
||||
|
||||
/* IMPLEMENTS READ FROM STORAGE USING SQLITE DATABASE */
|
||||
static char *
|
||||
readFromStorage(const char *key)
|
||||
{
|
||||
|
||||
char *val;
|
||||
|
||||
if (local_storage_ready==0)
|
||||
{
|
||||
db_prepare_structure(local_storage_filename);
|
||||
local_storage_ready=1;
|
||||
}
|
||||
|
||||
val = db_query_by_key(local_storage_filename, key);
|
||||
|
||||
//DBG("Read: %s %s %s",local_storage_filename, key, val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
static void
|
||||
removeFromStorage(const char *key)
|
||||
{
|
||||
if (local_storage_ready==0)
|
||||
{
|
||||
db_prepare_structure(local_storage_filename);
|
||||
local_storage_ready=1;
|
||||
}
|
||||
db_delete_from(local_storage_filename, key);
|
||||
}
|
||||
|
||||
/* IMPLEMENTS SAVE TO STORAGE USING SQLITE DATABASE */
|
||||
static void
|
||||
saveToStorage(const char *key, const char *val)
|
||||
{
|
||||
if (local_storage_ready==0) {
|
||||
db_prepare_structure(local_storage_filename);
|
||||
local_storage_ready=1;
|
||||
}
|
||||
|
||||
int rows_affected=0;
|
||||
|
||||
rows_affected=db_update_set(local_storage_filename, key, val);
|
||||
|
||||
if (rows_affected==0) {
|
||||
rows_affected=db_insert_into(local_storage_filename, key, val);
|
||||
}
|
||||
|
||||
// DBG(log, "UPD ROWS: %d KEY: %s VAL: %s",rows_affected,key,val);
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
mjs_localstorage_getitem(js_State *J)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
const char *key = js_tostring(J, 1);
|
||||
|
||||
if (!key) {
|
||||
js_pushnull(J);
|
||||
return;
|
||||
}
|
||||
|
||||
char *val = readFromStorage(key);
|
||||
|
||||
if (!val) {
|
||||
js_pushnull(J);
|
||||
return;
|
||||
}
|
||||
js_pushstring(J, val);
|
||||
mem_free(val);
|
||||
}
|
||||
|
||||
static void
|
||||
mjs_localstorage_removeitem(js_State *J)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
const char *key = js_tostring(J, 1);
|
||||
|
||||
if (key) {
|
||||
removeFromStorage(key);
|
||||
}
|
||||
js_pushundefined(J);
|
||||
}
|
||||
|
||||
static void
|
||||
mjs_localstorage_setitem(js_State *J)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J);
|
||||
const char *key_str = js_tostring(J, 1);
|
||||
const char *val_str = js_tostring(J, 2);
|
||||
|
||||
if (!key_str || !val_str)
|
||||
{
|
||||
js_pushundefined(J);
|
||||
return;
|
||||
}
|
||||
saveToStorage(key_str, val_str);
|
||||
#ifdef CONFIG_LEDS
|
||||
set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
|
||||
#endif
|
||||
js_pushundefined(J);
|
||||
}
|
||||
|
||||
static void
|
||||
mjs_localstorage_toString(js_State *J)
|
||||
{
|
||||
#ifdef ECMASCRIPT_DEBUG
|
||||
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
||||
#endif
|
||||
js_pushstring(J, "[localstorage object]");
|
||||
}
|
||||
|
||||
int
|
||||
mjs_localstorage_init(js_State *J)
|
||||
{
|
||||
js_newobject(J);
|
||||
{
|
||||
addmethod(J, "localStorage.getItem", mjs_localstorage_getitem, 1);
|
||||
addmethod(J, "localStorage.removeItem", mjs_localstorage_removeitem, 1);
|
||||
addmethod(J, "localStorage.setItem", mjs_localstorage_setitem, 2);
|
||||
addmethod(J, "localStorage.toString", mjs_localstorage_toString, 0);
|
||||
}
|
||||
js_defglobal(J, "localStorage", JS_DONTENUM);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1 +1 @@
|
||||
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'forms.c', 'history.c', 'implementation.c', 'keyboard.c', 'mapa.cpp')
|
||||
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'forms.c', 'history.c', 'implementation.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp')
|
||||
|
@ -49,6 +49,8 @@
|
||||
#include "document/refresh.h"
|
||||
#include "terminal/screen.h"
|
||||
|
||||
#ifndef CONFIG_LIBDOM
|
||||
|
||||
/* IMPLEMENTS READ FROM STORAGE USING SQLITE DATABASE */
|
||||
static char *
|
||||
readFromStorage(const char *key)
|
||||
@ -183,3 +185,4 @@ mjs_localstorage_init(js_State *J)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
@ -3,6 +3,14 @@
|
||||
|
||||
#include <mujs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int mjs_localstorage_init(js_State *J);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user