PageRenderTime 39ms CodeModel.GetById 13ms app.highlight 21ms 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
  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
 28#include "llviewerprecompiledheaders.h"
 29
 30#include "llcommandhandler.h"
 31#include "llfloaterreg.h"
 32#include "llfloatersearch.h"
 33#include "llmediactrl.h"
 34#include "llnotificationsutil.h"
 35#include "lllogininstance.h"
 36#include "lluri.h"
 37#include "llagent.h"
 38#include "llui.h"
 39#include "llviewercontrol.h"
 40#include "llweb.h"
 41
 42// support secondlife:///app/search/{CATEGORY}/{QUERY} SLapps
 43class LLSearchHandler : public LLCommandHandler
 44{
 45public:
 46	// requires trusted browser to trigger
 47	LLSearchHandler() : LLCommandHandler("search", UNTRUSTED_THROTTLE) { }
 48	bool handle(const LLSD& tokens, const LLSD& query_map, LLMediaCtrl* web)
 49	{
 50		if (!LLUI::sSettingGroups["config"]->getBOOL("EnableSearch"))
 51		{
 52			LLNotificationsUtil::add("NoSearch", LLSD(), LLSD(), std::string("SwitchToStandardSkinAndQuit"));
 53			return true;
 54		}
 55
 56		const size_t parts = tokens.size();
 57
 58		// get the (optional) category for the search
 59		std::string category;
 60		if (parts > 0)
 61		{
 62			category = tokens[0].asString();
 63		}
 64
 65		// get the (optional) search string
 66		std::string search_text;
 67		if (parts > 1)
 68		{
 69			search_text = tokens[1].asString();
 70		}
 71
 72		// create the LLSD arguments for the search floater
 73		LLFloaterSearch::Params p;
 74		p.search.category = category;
 75		p.search.query = LLURI::unescape(search_text);
 76
 77		// open the search floater and perform the requested search
 78		LLFloaterReg::showInstance("search", p);
 79		return true;
 80	}
 81};
 82LLSearchHandler gSearchHandler;
 83
 84LLFloaterSearch::SearchQuery::SearchQuery()
 85:	category("category", ""),
 86	query("query")
 87{}
 88
 89LLFloaterSearch::LLFloaterSearch(const Params& key) :
 90	LLFloaterWebContent(key),
 91	mSearchGodLevel(0)
 92{
 93	// declare a map that transforms a category name into
 94	// the URL suffix that is used to search that category
 95	mCategoryPaths = LLSD::emptyMap();
 96	mCategoryPaths["all"]          = "search";
 97	mCategoryPaths["people"]       = "search/people";
 98	mCategoryPaths["places"]       = "search/places";
 99	mCategoryPaths["events"]       = "search/events";
100	mCategoryPaths["groups"]       = "search/groups";
101	mCategoryPaths["wiki"]         = "search/wiki";
102	mCategoryPaths["destinations"] = "destinations";
103	mCategoryPaths["classifieds"]  = "classifieds";
104}
105
106BOOL LLFloaterSearch::postBuild()
107{
108	LLFloaterWebContent::postBuild();
109	mWebBrowser->addObserver(this);
110
111	return TRUE;
112}
113
114void LLFloaterSearch::onOpen(const LLSD& key)
115{
116	Params p(key);
117	p.trusted_content = true;
118	p.allow_address_entry = false;
119
120	LLFloaterWebContent::onOpen(p);
121	search(p.search);
122}
123
124void LLFloaterSearch::onClose(bool app_quitting)
125{
126	LLFloaterWebContent::onClose(app_quitting);
127	// tear down the web view so we don't show the previous search
128	// result when the floater is opened next time
129	destroy();
130}
131
132void LLFloaterSearch::godLevelChanged(U8 godlevel)
133{
134	// search results can change based upon god level - if the user
135	// changes god level, then give them a warning (we don't refresh
136	// the search as this might undo any page navigation or
137	// AJAX-driven changes since the last search).
138	
139	//FIXME: set status bar text
140
141	//getChildView("refresh_search")->setVisible( (godlevel != mSearchGodLevel));
142}
143
144void LLFloaterSearch::search(const SearchQuery &p)
145{
146	if (! mWebBrowser || !p.validateBlock())
147	{
148		return;
149	}
150
151	// reset the god level warning as we're sending the latest state
152	getChildView("refresh_search")->setVisible(FALSE);
153	mSearchGodLevel = gAgent.getGodLevel();
154
155	// work out the subdir to use based on the requested category
156	LLSD subs;
157	if (mCategoryPaths.has(p.category))
158	{
159		subs["CATEGORY"] = mCategoryPaths[p.category].asString();
160	}
161	else
162	{
163		subs["CATEGORY"] = mCategoryPaths["all"].asString();
164	}
165
166	// add the search query string
167	subs["QUERY"] = LLURI::escape(p.query);
168
169	// add the permissions token that login.cgi gave us
170	// We use "search_token", and fallback to "auth_token" if not present.
171	LLSD search_token = LLLoginInstance::getInstance()->getResponse("search_token");
172	if (search_token.asString().empty())
173	{
174		search_token = LLLoginInstance::getInstance()->getResponse("auth_token");
175	}
176	subs["AUTH_TOKEN"] = search_token.asString();
177
178	// add the user's preferred maturity (can be changed via prefs)
179	std::string maturity;
180	if (gAgent.prefersAdult())
181	{
182		maturity = "42";  // PG,Mature,Adult
183	}
184	else if (gAgent.prefersMature())
185	{
186		maturity = "21";  // PG,Mature
187	}
188	else
189	{
190		maturity = "13";  // PG
191	}
192	subs["MATURITY"] = maturity;
193
194	// add the user's god status
195	subs["GODLIKE"] = gAgent.isGodlike() ? "1" : "0";
196
197	// get the search URL and expand all of the substitutions
198	// (also adds things like [LANGUAGE], [VERSION], [OS], etc.)
199	std::string url = gSavedSettings.getString("SearchURL");
200	url = LLWeb::expandURLSubstitutions(url, subs);
201
202	// and load the URL in the web view
203	mWebBrowser->navigateTo(url, "text/html");
204}