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!");
|
|
|
|
|
|
|
|
String folder = _folder;
|
|
|
|
|
|
|
|
if (folder.begins_with("res://")) {
|
|
|
|
if (ProjectSettings::get_singleton()) {
|
|
|
|
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
|
|
|
|
if (resource_path != "") {
|
|
|
|
folder = folder.replace_first("res:/", resource_path);
|
|
|
|
} else {
|
|
|
|
folder = folder.replace_first("res://", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (folder.begins_with("user://")) {
|
|
|
|
String data_dir = OS::get_singleton()->get_user_data_dir();
|
|
|
|
if (data_dir != "") {
|
|
|
|
folder = folder.replace_first("user:/", data_dir);
|
|
|
|
} else {
|
|
|
|
folder = folder.replace_first("user://", "");
|
|
|
|
}
|
2022-06-30 13:23:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-03 16:28:57 +02:00
|
|
|
folder = DirAccess::get_full_path(folder, DirAccess::ACCESS_FILESYSTEM);
|
|
|
|
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
|
|
|
|
|
|
|
PagedArticle *p = new 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");
|
|
|
|
}
|