2022-06-30 13:23:14 +02:00
|
|
|
#include "paged_articles.h"
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
#include "core/os/dir_access.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
|
|
|
|
|
|
|
#include "../../html/html_builder.h"
|
|
|
|
#include "../../http/web_server_request.h"
|
2022-06-30 13:23:14 +02:00
|
|
|
|
|
|
|
#include "paged_article.h"
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
String PagedArticles::get_folder() {
|
|
|
|
return _folder;
|
|
|
|
}
|
|
|
|
void PagedArticles::set_folder(const String &val) {
|
|
|
|
_folder = val;
|
|
|
|
}
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
void PagedArticles::_handle_request(Ref<WebServerRequest> request) {
|
2022-06-30 13:23:14 +02:00
|
|
|
render_menu(request);
|
|
|
|
render_index(request);
|
|
|
|
|
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
void PagedArticles::_render_index(Ref<WebServerRequest> request) {
|
2022-06-30 13:23:14 +02:00
|
|
|
// summary page
|
2022-07-03 16:28:57 +02:00
|
|
|
request->body += _index_page;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
2022-07-03 16:28:57 +02:00
|
|
|
void PagedArticles::_render_preview(Ref<WebServerRequest> request) {
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PagedArticles::load() {
|
2022-07-03 16:28:57 +02:00
|
|
|
ERR_FAIL_COND_MSG(_folder == "", "Error: PagedArticles::load called, but a folder is not set!");
|
|
|
|
|
2022-07-03 17:29:52 +02:00
|
|
|
String folder = DirAccess::get_filesystem_abspath_for(_folder);
|
2022-07-03 16:28:57 +02:00
|
|
|
folder = folder.path_clean_end_slash();
|
|
|
|
|
|
|
|
DirAccess *dir = DirAccess::open(folder);
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
ERR_FAIL_COND_MSG(!dir, "Error opening PagedArticles::folder! folder: " + folder);
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
dir->list_dir_begin();
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
String file_name = dir->get_next();
|
|
|
|
while (file_name != "") {
|
|
|
|
if (dir->current_is_dir() && file_name != "." && file_name != "..") {
|
|
|
|
String ff = folder + "/" + file_name;
|
2022-06-30 13:23:14 +02:00
|
|
|
|
2022-07-03 17:35:07 +02:00
|
|
|
PagedArticle *p = memnew(PagedArticle);
|
2022-07-03 16:28:57 +02:00
|
|
|
|
|
|
|
p->set_articles_folder(ff);
|
|
|
|
p->set_uri_segment(file_name);
|
2022-06-30 13:23:14 +02:00
|
|
|
add_child(p);
|
|
|
|
}
|
2022-07-03 16:28:57 +02:00
|
|
|
|
|
|
|
file_name = dir->get_next();
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
dir->list_dir_end();
|
|
|
|
memdelete(dir);
|
|
|
|
|
2022-06-30 13:23:14 +02:00
|
|
|
generate_index_page();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedArticles::generate_index_page() {
|
|
|
|
HTMLBuilder b;
|
|
|
|
|
|
|
|
b.div("article_list");
|
|
|
|
|
|
|
|
for (int i = 0; i < get_child_count(); ++i) {
|
|
|
|
PagedArticle *a = Object::cast_to<PagedArticle>(get_child(i));
|
|
|
|
|
|
|
|
if (a) {
|
|
|
|
b.a(a->get_full_uri());
|
|
|
|
b.div("article_list_entry")->w(a->get_summary())->cdiv();
|
|
|
|
b.ca();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cdiv();
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
_index_page = b.result;
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
void PagedArticles::_notification(int what) {
|
2022-06-30 13:23:14 +02:00
|
|
|
switch (what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE:
|
|
|
|
load();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
PagedArticles::PagedArticles() {
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PagedArticles::~PagedArticles() {
|
2022-07-03 16:28:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PagedArticles::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_folder"), &PagedArticles::get_folder);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_folder", "val"), &PagedArticles::set_folder);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "folder"), "set_folder", "get_folder");
|
|
|
|
}
|