Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * header_generator.cpp - Generator of page header 00004 * 00005 * Created: Sun Aug 30 14:40:26 2009 00006 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "header_generator.h" 00024 00025 #include <utils/system/hostinfo.h> 00026 00027 #include <cstdio> 00028 #include <cstdlib> 00029 00030 /** @class WebviewHeaderGenerator "header_generator.h" 00031 * Webview page header. 00032 * Custom page header that shows the logo and a navigation bar. 00033 * @author Tim Niemueller 00034 */ 00035 00036 /** Page header template. */ 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 /** Constructor. */ 00049 WebviewHeaderGenerator::WebviewHeaderGenerator() 00050 { 00051 } 00052 00053 /** Add navigation entry. 00054 * @param baseurl baseurl that should be linked for this entry 00055 * @param name string to print as link name 00056 */ 00057 void 00058 WebviewHeaderGenerator::add_nav_entry(std::string baseurl, std::string name) 00059 { 00060 __nav_entries[baseurl] = name; 00061 } 00062 00063 /** Remove navigation entry. 00064 * @param baseurl baseurl whose config entry to remove 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 }