Enter TaskbarInfo

After this change it's time to say thank you and good-bye to
`withTaskbarInfo()`. But it will take a while.
This commit is contained in:
Veloman Yunkan 2022-03-24 19:26:25 +04:00 committed by Matthieu Gautier
parent 9bc09a815c
commit 0ecbdbcf63
3 changed files with 40 additions and 3 deletions

View File

@ -627,9 +627,9 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
} }
if (archive == nullptr) { if (archive == nullptr) {
const std::string error_details = noSuchBookErrorMsg(bookName); return HTTP404HtmlResponse(*this, request)
auto response = Response::build_404(*this, "", error_details); + noSuchBookErrorMsg(bookName)
return withTaskbarInfo(bookName, nullptr, std::move(response)); + TaskbarInfo(bookName);
} }
try { try {

View File

@ -136,6 +136,20 @@ HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const std::string& msg)
return *this; return *this;
} }
HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const TaskbarInfo& taskbarInfo)
{
this->taskbarInfo.reset(new TaskbarInfo(taskbarInfo));
return *this;
}
std::unique_ptr<ContentResponse> HTTP404HtmlResponse::generateResponseObject() const
{
auto r = ContentResponseBlueprint::generateResponseObject();
return taskbarInfo
? withTaskbarInfo(taskbarInfo->bookName, taskbarInfo->archive, std::move(r))
: std::move(r);
}
std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength) std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength)
{ {
auto response = Response::build(server); auto response = Response::build(server);

View File

@ -148,6 +148,8 @@ public: // functions
, m_template(templateStr) , m_template(templateStr)
{} {}
virtual ~ContentResponseBlueprint() = default;
ContentResponseBlueprint& operator+(kainjow::mustache::data&& data) ContentResponseBlueprint& operator+(kainjow::mustache::data&& data)
{ {
this->m_data = std::move(data); this->m_data = std::move(data);
@ -155,6 +157,11 @@ public: // functions
} }
operator std::unique_ptr<ContentResponse>() const operator std::unique_ptr<ContentResponse>() const
{
return generateResponseObject();
}
virtual std::unique_ptr<ContentResponse> generateResponseObject() const
{ {
auto r = ContentResponse::build(m_server, m_template, m_data, m_mimeType); auto r = ContentResponse::build(m_server, m_template, m_data, m_mimeType);
r->set_code(m_httpStatusCode); r->set_code(m_httpStatusCode);
@ -179,6 +186,17 @@ class UrlNotFoundMsg {};
extern const UrlNotFoundMsg urlNotFoundMsg; extern const UrlNotFoundMsg urlNotFoundMsg;
struct TaskbarInfo
{
const std::string bookName;
const zim::Archive* const archive;
TaskbarInfo(const std::string& bookName, const zim::Archive* a = nullptr)
: bookName(bookName)
, archive(a)
{}
};
struct HTTP404HtmlResponse : ContentResponseBlueprint struct HTTP404HtmlResponse : ContentResponseBlueprint
{ {
HTTP404HtmlResponse(const InternalServer& server, HTTP404HtmlResponse(const InternalServer& server,
@ -186,6 +204,11 @@ struct HTTP404HtmlResponse : ContentResponseBlueprint
HTTP404HtmlResponse& operator+(UrlNotFoundMsg /*unused*/); HTTP404HtmlResponse& operator+(UrlNotFoundMsg /*unused*/);
HTTP404HtmlResponse& operator+(const std::string& errorDetails); HTTP404HtmlResponse& operator+(const std::string& errorDetails);
HTTP404HtmlResponse& operator+(const TaskbarInfo& taskbarInfo);
std::unique_ptr<ContentResponse> generateResponseObject() const override;
std::unique_ptr<TaskbarInfo> taskbarInfo;
}; };
class ItemResponse : public Response { class ItemResponse : public Response {