Fawkes API  Fawkes Development Version
page_reply.cpp
1 
2 /***************************************************************************
3  * page_reply.h - Web request reply for a normal page
4  *
5  * Created: Thu Oct 23 16:13:48 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <webview/page_reply.h>
24 #include <webview/page_header_generator.h>
25 #include <webview/page_footer_generator.h>
26 #include <utils/system/hostinfo.h>
27 
28 #include <cstdlib>
29 #include <cstring>
30 #include <cstdio>
31 
32 namespace fawkes {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 /** @class WebPageReply <webview/page_reply.h>
38  * Basic page reply.
39  * This reply adds header and footer as appropriate to form a HTML document
40  * with logo and navigation.
41  * @author Tim Niemueller
42  */
43 
44 /** Page header template. */
45 const char * WebPageReply::PAGE_HEADER =
46  "<html>\n"
47  " <head>\n"
48  " <title>%s</title>\n"
49  " <link rel=\"stylesheet\" type=\"text/css\" href=\"/static/css/webview.css\" />\n"
50  "%s"
51  " </head>\n"
52  " <body>\n";
53 
54 /** Page footer template. */
55 const char * WebPageReply::PAGE_FOOTER =
56  "\n </body>\n"
57  "</html>\n";
58 
59 /** Constructor.
60  * @param title title of the page
61  * @param body Optional initial body of the page
62  */
63 WebPageReply::WebPageReply(std::string title, std::string body)
64  : StaticWebReply(WebReply::HTTP_OK, body)
65 {
66  _title = title;
67  navbar_enabled_ = true;
68  footer_enabled_ = true;
69 
70  add_header("Content-type", "text/html");
71 }
72 
73 
74 /** Base constructor.
75  * Constructor that does not set a title or anything. Use for sub-classes.
76  * @param code HTTP code for this reply
77  */
79  : StaticWebReply(code)
80 {
81  navbar_enabled_ = true;
82  footer_enabled_ = true;
83 
84  add_header("Content-type", "text/html");
85 }
86 
87 
88 /** Set HTML header text.
89  * The given text is placed in the head section of the HTML page. You can use it
90  * for example to add custom stylesheets or JavaScript.
91  * @param h header to set
92  */
93 void
95 {
96  __html_header = h;
97 }
98 
99 
100 /** Pack web page reply.
101  * This method creates the final page by calling the header and footer generators
102  * if supplied (otherwise a standard header is chosen) and the body.
103  * @param active_baseurl the active navigation URL, can be used for instance
104  * to high-light the current section in the navigation.
105  * @param headergen header generator
106  * @param footergen footer generator
107  */
108 void
109 WebPageReply::pack(std::string active_baseurl,
110  WebPageHeaderGenerator *headergen,
111  WebPageFooterGenerator *footergen)
112 {
113  if (headergen && navbar_enabled_)
114  __merged_body += headergen->html_header(_title, active_baseurl, __html_header);
115  else {
116  fawkes::HostInfo hi;
117  char *s;
118  if ( asprintf(&s, PAGE_HEADER, _title.c_str(), __html_header.c_str(), hi.short_name()) != -1 ) {
119  __merged_body += s;
120  free(s);
121  }
122  }
123 
124  __merged_body += _body;
125 
126  if (footergen && footer_enabled_) __merged_body += footergen->html_footer();
127  else __merged_body += PAGE_FOOTER;
128 }
129 
130 std::string::size_type
132 {
133  return __merged_body.length();
134 }
135 
136 
137 const std::string &
139 {
140  return __merged_body;
141 }
142 
143 /**
144  * Enable or disable the Fawkes Navigationbar (default enabled)
145  * @param enabled enabled
146  */
148 {
149  navbar_enabled_ = enabled;
150 }
151 /**
152  * Is the Fawkes Navigation bar enabled?
153  * @return enabled
154  */
156 {
157  return navbar_enabled_;
158 }
159 /**
160  * Enable or disable the Fawkes Webview footer (default enabled)
161  * @param enabled enabled
162  */
164 {
165  footer_enabled_ = enabled;
166 }
167 /**
168  * Is the Fawkes Webview footer enabled?
169  * @return enabled
170  */
172 {
173  return footer_enabled_;
174 }
175 
176 
177 } // end namespace fawkes
virtual void set_html_header(std::string h)
Set HTML header text.
Definition: page_reply.cpp:94
const char * short_name()
Get short hostname (up to first dot).
Definition: hostinfo.cpp:114
bool get_navbar_enabled()
Is the Fawkes Navigation bar enabled?
Definition: page_reply.cpp:155
std::string _title
Title of the page.
Definition: page_reply.h:60
Fawkes library namespace.
void add_header(std::string header, std::string content)
Add a HTTP header.
Definition: reply.cpp:97
virtual const std::string & body()
Get body.
Definition: page_reply.cpp:138
virtual void pack()
Pack the data.
Definition: page_reply.h:43
bool get_footer_enabled()
Is the Fawkes Webview footer enabled?
Definition: page_reply.cpp:171
std::string _body
Body of the reply.
Definition: reply.h:147
Interface for HTML header generator.
Code code() const
Get response code.
Definition: reply.cpp:86
Host information.
Definition: hostinfo.h:31
virtual std::string html_header(std::string &title, std::string &active_baseurl, std::string &html_header)=0
Generate HTML header.
void set_navbar_enabled(bool enabled)
Enable or disable the Fawkes Navigationbar (default enabled)
Definition: page_reply.cpp:147
Basic web reply.
Definition: reply.h:36
Interface for HTML footer generator.
void set_footer_enabled(bool enabled)
Enable or disable the Fawkes Webview footer (default enabled)
Definition: page_reply.cpp:163
Code
HTTP response code.
Definition: reply.h:40
virtual std::string::size_type body_length()
Get length of body.
Definition: page_reply.cpp:131
virtual std::string html_footer()=0
Generate HTML footer.
WebPageReply(std::string title, std::string page="")
Constructor.
Definition: page_reply.cpp:63
Static web reply.
Definition: reply.h:133