header_generator.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "header_generator.h"
00024
00025 #include <utils/system/hostinfo.h>
00026
00027 #include <cstdio>
00028 #include <cstdlib>
00029
00030
00031
00032
00033
00034
00035
00036
00037 const char * WebviewHeaderGenerator::PAGE_HEADER =
00038 "<html>\n"
00039 " <head>\n"
00040 " <title>%s (%s)</title>\n"
00041 " <link rel=\"stylesheet\" type=\"text/css\" href=\"/static/webview.css\" />\n"
00042 " </head>\n"
00043 " <body>\n"
00044 " <div id=\"header\">"
00045 "<a id=\"logo\" href=\"/\"/><img src=\"/static/webview.png\" alt=\"Fawkes WebView\"/></a>"
00046 "<hr /></div>\n";
00047
00048
00049 WebviewHeaderGenerator::WebviewHeaderGenerator()
00050 {
00051 }
00052
00053
00054
00055
00056
00057 void
00058 WebviewHeaderGenerator::add_nav_entry(std::string baseurl, std::string name)
00059 {
00060 __nav_entries[baseurl] = name;
00061 }
00062
00063
00064
00065
00066 void
00067 WebviewHeaderGenerator::remove_nav_entry(std::string baseurl)
00068 {
00069 __nav_entries.erase(baseurl);
00070 }
00071
00072 std::string
00073 WebviewHeaderGenerator::html_header(std::string &title,
00074 std::string &active_baseurl)
00075 {
00076 fawkes::HostInfo hi;
00077
00078 std::string rv = "";
00079 char *s;
00080 if ( asprintf(&s, PAGE_HEADER, title.c_str(), hi.short_name()) != -1 ) {
00081 rv = s;
00082 free(s);
00083 }
00084
00085 rv += " <div id=\"mainnav\" class=\"nav\"><ul>";
00086 std::map<std::string, std::string>::iterator nei;
00087 for (nei = __nav_entries.begin(); nei != __nav_entries.end(); ++nei) {
00088 rv += "<li";
00089 if ( nei->first == active_baseurl ) {
00090 rv += " class=\"active\"";
00091 }
00092 rv += "><a href=\"" + nei->first + "\">" + nei->second + "</a></li>";
00093 }
00094 rv += "</ul></div>";
00095
00096 return rv;
00097 }