PageRenderTime 40ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llfloatersearch.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 204 lines | 125 code | 27 blank | 52 comment | 10 complexity | cc6308660f2bd0adba4d84683282c3ba MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llfloatersearch.cpp
  3. * @author Martin Reddy
  4. * @brief Search floater - uses an embedded web browser control
  5. *
  6. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library 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 GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #include "llviewerprecompiledheaders.h"
  28. #include "llcommandhandler.h"
  29. #include "llfloaterreg.h"
  30. #include "llfloatersearch.h"
  31. #include "llmediactrl.h"
  32. #include "llnotificationsutil.h"
  33. #include "lllogininstance.h"
  34. #include "lluri.h"
  35. #include "llagent.h"
  36. #include "llui.h"
  37. #include "llviewercontrol.h"
  38. #include "llweb.h"
  39. // support secondlife:///app/search/{CATEGORY}/{QUERY} SLapps
  40. class LLSearchHandler : public LLCommandHandler
  41. {
  42. public:
  43. // requires trusted browser to trigger
  44. LLSearchHandler() : LLCommandHandler("search", UNTRUSTED_THROTTLE) { }
  45. bool handle(const LLSD& tokens, const LLSD& query_map, LLMediaCtrl* web)
  46. {
  47. if (!LLUI::sSettingGroups["config"]->getBOOL("EnableSearch"))
  48. {
  49. LLNotificationsUtil::add("NoSearch", LLSD(), LLSD(), std::string("SwitchToStandardSkinAndQuit"));
  50. return true;
  51. }
  52. const size_t parts = tokens.size();
  53. // get the (optional) category for the search
  54. std::string category;
  55. if (parts > 0)
  56. {
  57. category = tokens[0].asString();
  58. }
  59. // get the (optional) search string
  60. std::string search_text;
  61. if (parts > 1)
  62. {
  63. search_text = tokens[1].asString();
  64. }
  65. // create the LLSD arguments for the search floater
  66. LLFloaterSearch::Params p;
  67. p.search.category = category;
  68. p.search.query = LLURI::unescape(search_text);
  69. // open the search floater and perform the requested search
  70. LLFloaterReg::showInstance("search", p);
  71. return true;
  72. }
  73. };
  74. LLSearchHandler gSearchHandler;
  75. LLFloaterSearch::SearchQuery::SearchQuery()
  76. : category("category", ""),
  77. query("query")
  78. {}
  79. LLFloaterSearch::LLFloaterSearch(const Params& key) :
  80. LLFloaterWebContent(key),
  81. mSearchGodLevel(0)
  82. {
  83. // declare a map that transforms a category name into
  84. // the URL suffix that is used to search that category
  85. mCategoryPaths = LLSD::emptyMap();
  86. mCategoryPaths["all"] = "search";
  87. mCategoryPaths["people"] = "search/people";
  88. mCategoryPaths["places"] = "search/places";
  89. mCategoryPaths["events"] = "search/events";
  90. mCategoryPaths["groups"] = "search/groups";
  91. mCategoryPaths["wiki"] = "search/wiki";
  92. mCategoryPaths["destinations"] = "destinations";
  93. mCategoryPaths["classifieds"] = "classifieds";
  94. }
  95. BOOL LLFloaterSearch::postBuild()
  96. {
  97. LLFloaterWebContent::postBuild();
  98. mWebBrowser->addObserver(this);
  99. return TRUE;
  100. }
  101. void LLFloaterSearch::onOpen(const LLSD& key)
  102. {
  103. Params p(key);
  104. p.trusted_content = true;
  105. p.allow_address_entry = false;
  106. LLFloaterWebContent::onOpen(p);
  107. search(p.search);
  108. }
  109. void LLFloaterSearch::onClose(bool app_quitting)
  110. {
  111. LLFloaterWebContent::onClose(app_quitting);
  112. // tear down the web view so we don't show the previous search
  113. // result when the floater is opened next time
  114. destroy();
  115. }
  116. void LLFloaterSearch::godLevelChanged(U8 godlevel)
  117. {
  118. // search results can change based upon god level - if the user
  119. // changes god level, then give them a warning (we don't refresh
  120. // the search as this might undo any page navigation or
  121. // AJAX-driven changes since the last search).
  122. //FIXME: set status bar text
  123. //getChildView("refresh_search")->setVisible( (godlevel != mSearchGodLevel));
  124. }
  125. void LLFloaterSearch::search(const SearchQuery &p)
  126. {
  127. if (! mWebBrowser || !p.validateBlock())
  128. {
  129. return;
  130. }
  131. // reset the god level warning as we're sending the latest state
  132. getChildView("refresh_search")->setVisible(FALSE);
  133. mSearchGodLevel = gAgent.getGodLevel();
  134. // work out the subdir to use based on the requested category
  135. LLSD subs;
  136. if (mCategoryPaths.has(p.category))
  137. {
  138. subs["CATEGORY"] = mCategoryPaths[p.category].asString();
  139. }
  140. else
  141. {
  142. subs["CATEGORY"] = mCategoryPaths["all"].asString();
  143. }
  144. // add the search query string
  145. subs["QUERY"] = LLURI::escape(p.query);
  146. // add the permissions token that login.cgi gave us
  147. // We use "search_token", and fallback to "auth_token" if not present.
  148. LLSD search_token = LLLoginInstance::getInstance()->getResponse("search_token");
  149. if (search_token.asString().empty())
  150. {
  151. search_token = LLLoginInstance::getInstance()->getResponse("auth_token");
  152. }
  153. subs["AUTH_TOKEN"] = search_token.asString();
  154. // add the user's preferred maturity (can be changed via prefs)
  155. std::string maturity;
  156. if (gAgent.prefersAdult())
  157. {
  158. maturity = "42"; // PG,Mature,Adult
  159. }
  160. else if (gAgent.prefersMature())
  161. {
  162. maturity = "21"; // PG,Mature
  163. }
  164. else
  165. {
  166. maturity = "13"; // PG
  167. }
  168. subs["MATURITY"] = maturity;
  169. // add the user's god status
  170. subs["GODLIKE"] = gAgent.isGodlike() ? "1" : "0";
  171. // get the search URL and expand all of the substitutions
  172. // (also adds things like [LANGUAGE], [VERSION], [OS], etc.)
  173. std::string url = gSavedSettings.getString("SearchURL");
  174. url = LLWeb::expandURLSubstitutions(url, subs);
  175. // and load the URL in the web view
  176. mWebBrowser->navigateTo(url, "text/html");
  177. }