pandemonium_engine/modules/web/nodes/static_pages/static_web_page.cpp

95 lines
2.7 KiB
C++

#include "static_web_page.h"
#include "../../http/web_server_request.h"
#include "core/os/file_access.h"
String StaticWebPage::get_data() {
return _data;
}
void StaticWebPage::set_data(const String &val) {
_data = val;
}
String StaticWebPage::get_preview_data() {
return _preview_data;
}
void StaticWebPage::set_preview_data(const String &val) {
_preview_data = val;
}
bool StaticWebPage::get_should_render_menu() {
return _should_render_menu;
}
void StaticWebPage::set_should_render_menu(const bool &val) {
_should_render_menu = val;
}
void StaticWebPage::_handle_request(Ref<WebServerRequest> request) {
if (_should_render_menu) {
render_main_menu(request);
}
render_index(request);
request->compile_and_send_body();
}
void StaticWebPage::_render_index(Ref<WebServerRequest> request) {
request->body += _data;
}
void StaticWebPage::_render_preview(Ref<WebServerRequest> request) {
request->body += _preview_data;
}
void StaticWebPage::load_file(const String &path) {
FileAccess *f = FileAccess::open(path, FileAccess::READ);
if (f) {
_data = f->get_as_utf8_string();
f->close();
memdelete(f);
} else {
_data = "";
}
}
void StaticWebPage::load_and_process_file(const String &path) {
FileAccess *f = FileAccess::open(path, FileAccess::READ);
if (f) {
_data = f->get_as_utf8_string();
f->close();
memdelete(f);
} else {
_data = "";
}
//todo prcess
//could support bbcode easily
//should probably support md -> I should probably write a parser
}
StaticWebPage::StaticWebPage() {
_should_render_menu = true;
}
StaticWebPage::~StaticWebPage() {
}
void StaticWebPage::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_data"), &StaticWebPage::get_data);
ClassDB::bind_method(D_METHOD("set_data", "val"), &StaticWebPage::set_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "data", PROPERTY_HINT_MULTILINE_TEXT), "set_data", "get_data");
ClassDB::bind_method(D_METHOD("get_preview_data"), &StaticWebPage::get_preview_data);
ClassDB::bind_method(D_METHOD("set_preview_data", "val"), &StaticWebPage::set_preview_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "preview_data", PROPERTY_HINT_MULTILINE_TEXT), "set_preview_data", "get_preview_data");
ClassDB::bind_method(D_METHOD("get_should_render_menu"), &StaticWebPage::get_should_render_menu);
ClassDB::bind_method(D_METHOD("set_should_render_menu", "val"), &StaticWebPage::set_should_render_menu);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "should_render_menu"), "set_should_render_menu", "get_should_render_menu");
ClassDB::bind_method(D_METHOD("load_file", "path"), &StaticWebPage::load_file);
ClassDB::bind_method(D_METHOD("load_and_process_file", "path"), &StaticWebPage::load_and_process_file);
}