plugins_processor.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 "plugins_processor.h"
00024 #include <webview/page_reply.h>
00025 #include <webview/redirect_reply.h>
00026
00027 #include <plugin/manager.h>
00028
00029 #include <string>
00030 #include <cstring>
00031 #include <cstdlib>
00032
00033 using namespace fawkes;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 WebviewPluginsRequestProcessor::WebviewPluginsRequestProcessor(const char *baseurl,
00046 PluginManager *manager)
00047 {
00048 __baseurl = strdup(baseurl);
00049 __baseurl_len = strlen(__baseurl);
00050 __manager = manager;
00051 }
00052
00053
00054
00055 WebviewPluginsRequestProcessor::~WebviewPluginsRequestProcessor()
00056 {
00057 free(__baseurl);
00058 }
00059
00060
00061 WebReply *
00062 WebviewPluginsRequestProcessor::process_request(const char *url,
00063 const char *method,
00064 const char *version,
00065 const char *upload_data,
00066 size_t *upload_data_size,
00067 void **session_data)
00068 {
00069 if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
00070
00071 std::string subpath = std::string(url).substr(__baseurl_len);
00072
00073 if (subpath.find("/load/") == 0) {
00074 std::string plugin_name = subpath.substr(std::string("/load/").length());
00075 __manager->load(plugin_name.c_str());
00076 return new WebRedirectReply(__baseurl);
00077 } else if (subpath.find("/unload/") == 0) {
00078 std::string plugin_name = subpath.substr(std::string("/unload/").length());
00079 __manager->unload(plugin_name.c_str());
00080 return new WebRedirectReply(__baseurl);
00081 } else {
00082 WebPageReply *r = new WebPageReply("BlackBoard");
00083 *r += "<h2>Fawkes Plugins</h2>\n";
00084
00085 *r += "<table>\n";
00086 *r += "<tr><th>Name</th><th>Description</th><th>Loaded</th><th>Action</th></tr>\n";
00087
00088 std::list<std::pair<std::string, std::string> > available_plugins;
00089 std::list<std::pair<std::string, std::string> >::iterator i;
00090
00091 available_plugins = __manager->get_available_plugins();
00092
00093 for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
00094 bool is_loaded = __manager->is_loaded(i->first.c_str());
00095
00096 const char *loaded_color = is_loaded ? "green" : "red";
00097 const char *loaded = is_loaded ? "Yes" : "No";
00098 const char *action_link = is_loaded ? "unload" : "load";
00099
00100 r->append_body("<tr><td>%s</td><td>%s</td>"
00101 "<td><span style=\"color:%s\">%s<span></td>"
00102 "<td><a href=\"%s/%s/%s\">%s</a></td>\n",
00103 i->first.c_str(), i->second.c_str(), loaded_color, loaded,
00104 __baseurl, action_link, i->first.c_str(), action_link);
00105 }
00106
00107 *r += "</table>\n";
00108
00109 return r;
00110 }
00111 } else {
00112 return NULL;
00113 }
00114 }