PageRenderTime 39ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llviewercontrollistener.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 226 lines | 165 code | 23 blank | 38 comment | 12 complexity | a63445243470e17af6d72fc55261e479 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llviewercontrollistener.cpp
  3. * @author Brad Kittenbrink
  4. * @date 2009-07-09
  5. * @brief Implementation for llviewercontrollistener.
  6. *
  7. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "llviewerprecompiledheaders.h"
  29. #include "llviewercontrollistener.h"
  30. #include "llviewercontrol.h"
  31. #include "llcontrol.h"
  32. #include "llerror.h"
  33. #include "llsdutil.h"
  34. #include "stringize.h"
  35. #include <sstream>
  36. namespace {
  37. LLViewerControlListener sSavedSettingsListener;
  38. } // unnamed namespace
  39. LLViewerControlListener::LLViewerControlListener()
  40. : LLEventAPI("LLViewerControl",
  41. "LLViewerControl listener: set, toggle or set default for various controls")
  42. {
  43. std::ostringstream groupnames;
  44. groupnames << "[\"group\"] is one of ";
  45. const char* delim = "";
  46. for (LLControlGroup::key_iter cgki(LLControlGroup::beginKeys()),
  47. cgkend(LLControlGroup::endKeys());
  48. cgki != cgkend; ++cgki)
  49. {
  50. groupnames << delim << '"' << *cgki << '"';
  51. delim = ", ";
  52. }
  53. groupnames << '\n';
  54. std::string grouphelp(groupnames.str());
  55. std::string replyhelp("If [\"reply\"] requested, send new [\"value\"] on specified LLEventPump\n");
  56. add("set",
  57. std::string("Set [\"group\"] control [\"key\"] to optional value [\"value\"]\n"
  58. "If [\"value\"] omitted, set to control's defined default value\n") +
  59. grouphelp + replyhelp,
  60. &LLViewerControlListener::set,
  61. LLSDMap("group", LLSD())("key", LLSD()));
  62. add("toggle",
  63. std::string("Toggle [\"group\"] control [\"key\"], if boolean\n") + grouphelp + replyhelp,
  64. &LLViewerControlListener::toggle,
  65. LLSDMap("group", LLSD())("key", LLSD()));
  66. add("get",
  67. std::string("Query [\"group\"] control [\"key\"], replying on LLEventPump [\"reply\"]\n") +
  68. grouphelp,
  69. &LLViewerControlListener::get,
  70. LLSDMap("group", LLSD())("key", LLSD())("reply", LLSD()));
  71. add("groups",
  72. "Send on LLEventPump [\"reply\"] an array [\"groups\"] of valid group names",
  73. &LLViewerControlListener::groups,
  74. LLSDMap("reply", LLSD()));
  75. add("vars",
  76. std::string("For [\"group\"], send on LLEventPump [\"reply\"] an array [\"vars\"],\n"
  77. "each of whose entries looks like:\n"
  78. " [\"name\"], [\"type\"], [\"value\"], [\"comment\"]\n") + grouphelp,
  79. &LLViewerControlListener::vars,
  80. LLSDMap("group", LLSD())("reply", LLSD()));
  81. }
  82. struct Info
  83. {
  84. Info(const LLSD& request):
  85. response(LLSD(), request),
  86. groupname(request["group"]),
  87. group(LLControlGroup::getInstance(groupname)),
  88. key(request["key"]),
  89. control(NULL)
  90. {
  91. if (! group)
  92. {
  93. response.error(STRINGIZE("Unrecognized group '" << groupname << "'"));
  94. return;
  95. }
  96. control = group->getControl(key);
  97. if (! control)
  98. {
  99. response.error(STRINGIZE("In group '" << groupname
  100. << "', unrecognized control key '" << key << "'"));
  101. }
  102. }
  103. ~Info()
  104. {
  105. // If in fact the request passed to our constructor names a valid
  106. // group and key, grab the final value of the indicated control and
  107. // stuff it in our response. Since this outer destructor runs before
  108. // the contained Response destructor, this data will go into the
  109. // response we send.
  110. if (control)
  111. {
  112. response["name"] = control->getName();
  113. response["type"] = group->typeEnumToString(control->type());
  114. response["value"] = control->get();
  115. response["comment"] = control->getComment();
  116. }
  117. }
  118. LLEventAPI::Response response;
  119. std::string groupname;
  120. LLControlGroup* group;
  121. std::string key;
  122. LLControlVariable* control;
  123. };
  124. //static
  125. void LLViewerControlListener::set(LLSD const & request)
  126. {
  127. Info info(request);
  128. if (! info.control)
  129. return;
  130. if (request.has("value"))
  131. {
  132. info.control->setValue(request["value"]);
  133. }
  134. else
  135. {
  136. info.control->resetToDefault();
  137. }
  138. }
  139. //static
  140. void LLViewerControlListener::toggle(LLSD const & request)
  141. {
  142. Info info(request);
  143. if (! info.control)
  144. return;
  145. if (info.control->isType(TYPE_BOOLEAN))
  146. {
  147. info.control->set(! info.control->get().asBoolean());
  148. }
  149. else
  150. {
  151. info.response.error(STRINGIZE("toggle of non-boolean '" << info.groupname
  152. << "' control '" << info.key
  153. << "', type is "
  154. << info.group->typeEnumToString(info.control->type())));
  155. }
  156. }
  157. void LLViewerControlListener::get(LLSD const & request)
  158. {
  159. // The Info constructor and destructor actually do all the work here.
  160. Info info(request);
  161. }
  162. void LLViewerControlListener::groups(LLSD const & request)
  163. {
  164. // No Info, we're not looking up either a group or a control name.
  165. Response response(LLSD(), request);
  166. for (LLControlGroup::key_iter cgki(LLControlGroup::beginKeys()),
  167. cgkend(LLControlGroup::endKeys());
  168. cgki != cgkend; ++cgki)
  169. {
  170. response["groups"].append(*cgki);
  171. }
  172. }
  173. struct CollectVars: public LLControlGroup::ApplyFunctor
  174. {
  175. CollectVars(LLControlGroup* g):
  176. mGroup(g)
  177. {}
  178. virtual void apply(const std::string& name, LLControlVariable* control)
  179. {
  180. vars.append(LLSDMap
  181. ("name", name)
  182. ("type", mGroup->typeEnumToString(control->type()))
  183. ("value", control->get())
  184. ("comment", control->getComment()));
  185. }
  186. LLControlGroup* mGroup;
  187. LLSD vars;
  188. };
  189. void LLViewerControlListener::vars(LLSD const & request)
  190. {
  191. // This method doesn't use Info, because we're not looking up a specific
  192. // control name.
  193. Response response(LLSD(), request);
  194. std::string groupname(request["group"]);
  195. LLControlGroup* group(LLControlGroup::getInstance(groupname));
  196. if (! group)
  197. {
  198. return response.error(STRINGIZE("Unrecognized group '" << groupname << "'"));
  199. }
  200. CollectVars collector(group);
  201. group->applyToAll(&collector);
  202. response["vars"] = collector.vars;
  203. }