/* ***** BEGIN LICENSE BLOCK ***** * Copyright (C) 2012, Peter Hatina * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ***** END LICENSE BLOCK ***** */ #include #include #include #include #include "generator.h" std::set Generator::s_default_attributes; std::set Generator::s_default_methods; std::map Generator::s_default_attribute_values; Generator::Generator(const std::list &attributes, const std::list &methods): m_attributes(attributes), m_methods(methods) { init(); } Generator::~Generator() { } void Generator::init() { if (!s_default_attributes.empty()) return; s_default_attributes.insert("hostip"); s_default_attributes.insert("port"); s_default_attributes.insert("adminconsole"); s_default_attributes.insert("hotkey"); s_default_attributes.insert("smartcard"); s_default_methods.insert("setlanguagestringssection"); s_default_methods.insert("setlanguagestringslang"); s_default_methods.insert("setusbfilterfilter"); s_default_attribute_values["adminconsole"] = " checked"; s_default_attribute_values["hotkey"] = " value=\"toggle-fullscreen=shift+f11," \ "release-cursor=shift+f12,smartcard-insert=shift+f8,smartcard-remove=shift+f9\""; s_default_attribute_values["usblistenport"] = " value=\"32023\""; } void Generator::generate() { generateHeader(); generateConnectVars(); generateContent(); generateFooter(); } void Generator::generateHeader() { std::cout << "\n" << "\n" << "Spice-XPI test page (generated)\n" << "\n" << "\n\n" << "\n\n" << "
\n" << "

SPICE xpi test page (generated)

\n" << "This page was autogenerated using IDL description and should not be modified by hand.
\n" << "Disabled (greyed out) values are passed\n" << "to SPICE xpi as empty variables.\n
\n
\n\n" << "
\n\n" << "\n\n"; } void Generator::generateContent() { std::cout << "
\n\n" << "\n"; std::list::iterator ita; for (ita = m_attributes.begin(); ita != m_attributes.end(); ++ita) { std::cout << "\n\n" << "\n" << "\n\n"; } std::list::iterator itm; for (itm = m_methods.begin(); itm != m_methods.end(); ++itm) { std::list::iterator itp; std::list params = itm->getParams(); for (itp = params.begin(); itp != params.end(); ++itp) { std::cout << "\n\n\n" << "\n\n"; } } std::cout << "
getIdentifier() << "Toggled" << "\" onclick=\"toggle('" << ita->getIdentifier() << "Toggled" <<"', '" << ita->getIdentifier() << "')\" " << (attributeEnabled(*ita) ? "checked" : "") << "/>" << splitIdentifier(ita->getIdentifier()) << "" << attributeToHtmlElement(*ita) << "
getIdentifier() << itp->getIdentifier() << "Toggled\" onclick=\"toggle('" << itm->getIdentifier() << itp->getIdentifier() << "Toggled', '" << itm->getIdentifier() << itp->getIdentifier() << "')\"" << (methodEnabled(*itm, *itp) ? "checked" : "") << "/>" << splitIdentifier(itm->getIdentifier()) << " - " << splitIdentifier(itp->getIdentifier()) << "getIdentifier() << itp->getIdentifier() << "\" type=\"" << (itp->getType() == Token::T_BOOLEAN ? "checkbox" : "text") << "\" size=\"30\" " << (methodEnabled(*itm, *itp) ? "" : "disabled ") << "/>
\n\n
\n"; int i = 1; for (itm = m_methods.begin(); itm != m_methods.end(); ++itm, ++i) { std::cout << "getIdentifier()) << "\" style=\"min-width: 180px\" onclick=\"" << itm->getIdentifier() << "()\"/>\n"; if (i % 3 == 0 && i != static_cast(m_methods.size())) std::cout << "
\n"; } std::cout << "\n
\n\n"; } std::string Generator::lowerString(const std::string &str) { std::string s(str); std::transform(s.begin(), s.end(), s.begin(), tolower); return s; } std::string Generator::splitIdentifier(const std::string &str) { std::string result(str); result[0] = toupper(result[0]); for (size_t i = 1; i < result.size() - 1; ++i) { if (isupper(result[i]) && islower(result[i + 1])) result.insert(i++, " "); else if (isupper(result[i]) && islower(result[i - 1])) result.insert(i++, " "); } return result; } std::string Generator::attributeToHtmlElement(const Attribute &attr) { std::stringstream ss; std::string id = lowerString(attr.getIdentifier()); if (id == "truststore") { ss << ""; } else { ss << ""; } return ss.str(); } std::string Generator::attributeDefaultValue(const Attribute &attr) { std::string id(lowerString(attr.getIdentifier())); std::map::iterator found = s_default_attribute_values.find(id); return found != s_default_attribute_values.end() ? found->second : ""; } bool Generator::attributeEnabled(const Attribute &attr) { std::string id(lowerString(attr.getIdentifier())); std::set::iterator found = s_default_attributes.find(id); return found != s_default_attributes.end(); } bool Generator::methodEnabled(const Method &method, const Method::MethodParam ¶m) { std::string id(lowerString(method.getIdentifier() + param.getIdentifier())); std::set::iterator found = s_default_methods.find(id); return found != s_default_methods.end(); }