Fawkes API  Fawkes Development Version
plugins_processor.cpp
1 
2 /***************************************************************************
3  * plugins_processor.cpp - Web request processor for plugin info
4  *
5  * Created: Thu Feb 12 13:00:37 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 "plugins_processor.h"
24 #include <webview/page_reply.h>
25 #include <webview/redirect_reply.h>
26 
27 #include <plugin/manager.h>
28 
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 using namespace fawkes;
34 
35 /** @class WebviewPluginsRequestProcessor "plugins_processor.h"
36  * Plugins web request processor.
37  * Provides access to plugin lists and allows for loading/unloading plugins.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param baseurl Base URL where processor is mounted
43  * @param manager PluginManager instance
44  */
46  PluginManager *manager)
47 {
48  __baseurl = strdup(baseurl);
49  __baseurl_len = strlen(__baseurl);
50  __manager = manager;
51 }
52 
53 
54 /** Destructor. */
56 {
57  free(__baseurl);
58 }
59 
60 
61 WebReply *
63 {
64  if ( strncmp(__baseurl, request->url().c_str(), __baseurl_len) == 0 ) {
65  // It is in our URL prefix range
66  std::string subpath = request->url().substr(__baseurl_len);
67 
68  if (subpath.find("/load/") == 0) {
69  std::string plugin_name = subpath.substr(std::string("/load/").length());
70  try {
71  __manager->load(plugin_name.c_str());
72  return new WebRedirectReply(__baseurl);
73  } catch (Exception &e) {
74  WebPageReply *r = new WebPageReply("Loading plugin failed");
75  r->append_body("<h1>Loading plugin '%s' failed</h1>", plugin_name.c_str());
76  *r += "<p>The encountered error was:</p>";
77  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
78  *r += std::string(*i) + "<br/>\n";
79  }
80  r->append_body("<p><a href=\"%s\">Back to overview</a> - "
81  "<a href=\"%s\">Retry</a></p>", __baseurl, request->url().c_str());
82  return r;
83  }
84  } else if (subpath.find("/unload/") == 0) {
85  std::string plugin_name = subpath.substr(std::string("/unload/").length());
86  try {
87  __manager->unload(plugin_name.c_str());
88  return new WebRedirectReply(__baseurl);
89  } catch (Exception &e) {
90  WebPageReply *r = new WebPageReply("Unloading plugin failed");
91  r->append_body("<h1>Unloading plugin '%s' failed</h1>",
92  plugin_name.c_str());
93  *r += "<p>The encountered error was:</p>";
94  for (Exception::iterator i = e.begin(); i != e.end(); ++i) {
95  *r += std::string(*i) + "<br/>\n";
96  }
97  r->append_body("<p><a href=\"%s\">Back to overview</a> - "
98  "<a href=\"%s\">Retry</a></p>", __baseurl, request->url().c_str());
99  return r;
100  }
101  } else {
102  WebPageReply *r = new WebPageReply("Plugins");
103  *r += "<h2>Fawkes Plugins</h2>\n";
104 
105  *r +=
106  "<style type=\"text/css\">\n"
107  " tr:hover { background-color: #eeeeee; }\n"
108  " :link:hover, :visited:hover { background-color: #bb0000; color: white; }\n"
109  "</style>";
110 
111  *r += "<table>\n";
112  *r += "<tr><th>Name</th><th>Description</th><th>Loaded</th><th>Action</th></tr>\n";
113 
114  std::list<std::pair<std::string, std::string> > available_plugins;
115  std::list<std::pair<std::string, std::string> >::iterator i;
116 
117  available_plugins = __manager->get_available_plugins();
118 
119  for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
120  bool is_loaded = __manager->is_loaded(i->first.c_str());
121 
122  const char *loaded_color = is_loaded ? "green" : "red";
123  const char *loaded = is_loaded ? "Yes" : "No";
124  const char *action_link = is_loaded ? "unload" : "load";
125 
126  r->append_body("<tr><td>%s</td><td>%s</td>"
127  "<td><span style=\"color:%s\">%s<span></td>"
128  "<td><a href=\"%s/%s/%s\">%s</a></td>\n",
129  i->first.c_str(), i->second.c_str(), loaded_color, loaded,
130  __baseurl, action_link, i->first.c_str(), action_link);
131  }
132 
133  *r += "</table>\n";
134 
135  return r;
136  }
137  } else {
138  return NULL;
139  }
140 }
Fawkes library namespace.
WebviewPluginsRequestProcessor(const char *baseurl, fawkes::PluginManager *manager)
Constructor.
Message iterator for exceptions.
Definition: exception.h:72
Fawkes Plugin Manager.
Definition: manager.h:51
iterator end()
Get end iterator for messages.
Definition: exception.cpp:717
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual fawkes::WebReply * process_request(const fawkes::WebRequest *request)
Process a request.
iterator begin()
Get iterator for messages.
Definition: exception.cpp:700
Web request meta data carrier.
Definition: request.h:42
Basic page reply.
Definition: page_reply.h:36
Basic web reply.
Definition: reply.h:36
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:224
const std::string & url() const
Get URL.
Definition: request.h:69
virtual ~WebviewPluginsRequestProcessor()
Destructor.
Redirect reply for webview.