Fawkes API  Fawkes Development Version
header_generator.cpp
1 
2 /***************************************************************************
3  * header_generator.cpp - Generator of page header
4  *
5  * Created: Sun Aug 30 14:40:26 2009
6  * Copyright 2006-2009 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 "header_generator.h"
24 
25 #include <utils/system/hostinfo.h>
26 #include <webview/nav_manager.h>
27 
28 #include <cstdio>
29 #include <cstdlib>
30 
31 using namespace fawkes;
32 
33 /** @class WebviewHeaderGenerator "header_generator.h"
34  * Webview page header.
35  * Custom page header that shows the logo and a navigation bar.
36  * @author Tim Niemueller
37  */
38 
39 /** Page header template. */
40 const char * WebviewHeaderGenerator::PAGE_HEADER =
41  "<html>\n"
42  " <head>\n"
43  " <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n"
44  " <meta http-equiv=\"Content-Language\" content=\"en-us\" />\n"
45  " <title>%s (%s)</title>\n"
46  " <link rel=\"icon\" type=\"image/png\" href=\"/static/images/favicon.png\" />\n"
47  " <link rel=\"stylesheet\" type=\"text/css\" href=\"/static/css/webview.css\" />\n"
48  "%s"
49  " </head>\n"
50  " <body>\n";
51 
52 /** Constructor.
53  * @param nav_manager navigation manager to use to generate the navigation
54  */
56 {
57  __nav_manager = nav_manager;
58 }
59 
60 std::string
62  std::string &active_baseurl,
63  std::string &html_header)
64 {
66 
67  std::string rv = "";
68  char *s;
69  if ( asprintf(&s, PAGE_HEADER, title.c_str(), hi.short_name(),
70  html_header.c_str()) != -1 )
71  {
72  rv = s;
73  free(s);
74  }
75 
76  rv +=
77  " <div id=\"mainnav\" class=\"nav\"><a id=\"logo\" href=\"/\"/>"
78  "<img class=\"navlogo\" src=\"/static/chrome/navlogo.png\" /></a><ul>";
79  WebNavManager::NavMap::const_iterator nei;
80  const WebNavManager::NavMap &nav_entries(__nav_manager->get_nav_entries());
81  for (nei = nav_entries.begin(); nei != nav_entries.end(); ++nei) {
82  rv += "<li";
83  if ( nei->first == active_baseurl ) {
84  rv += " class=\"active\"";
85  }
86  rv += "><a href=\"" + nei->first + "\">" + nei->second + "</a></li>";
87  }
88  rv += "</ul></div>";
89 
90  return rv;
91 }
const char * short_name()
Get short hostname (up to first dot).
Definition: hostinfo.cpp:114
Fawkes library namespace.
Host information.
Definition: hostinfo.h:31
std::map< std::string, std::string > NavMap
Navigation map type, mapping URLs to labels.
Definition: nav_manager.h:40
Manage visible navigation entries.
Definition: nav_manager.h:36
WebviewHeaderGenerator(fawkes::WebNavManager *nav_manager)
Constructor.
std::string html_header(std::string &title, std::string &active_baseurl, std::string &html_header)
Generate HTML header.