PageRenderTime 707ms CodeModel.GetById 224ms app.highlight 210ms RepoModel.GetById 162ms app.codeStats 0ms

/indra/newview/llfloaterurlentry.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 284 lines | 174 code | 42 blank | 68 comment | 19 complexity | 678dbbbe20299c075a05ae5763518883 MD5 | raw file
  1/**
  2 * @file llfloaterurlentry.cpp
  3 * @brief LLFloaterURLEntry class implementation
  4 *
  5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llhttpclient.h"
 30
 31#include "llfloaterurlentry.h"
 32
 33#include "llpanellandmedia.h"
 34#include "llpanelface.h"
 35
 36#include "llcombobox.h"
 37#include "llmimetypes.h"
 38#include "llnotificationsutil.h"
 39#include "llurlhistory.h"
 40#include "lluictrlfactory.h"
 41#include "llwindow.h"
 42#include "llviewerwindow.h"
 43
 44static LLFloaterURLEntry* sInstance = NULL;
 45
 46// Move this to its own file.
 47// helper class that tries to download a URL from a web site and calls a method
 48// on the Panel Land Media and to discover the MIME type
 49class LLMediaTypeResponder : public LLHTTPClient::Responder
 50{
 51public:
 52	LLMediaTypeResponder( const LLHandle<LLFloater> parent ) :
 53	  mParent( parent )
 54	  {}
 55
 56	  LLHandle<LLFloater> mParent;
 57
 58
 59	  virtual void completedHeader(U32 status, const std::string& reason, const LLSD& content)
 60	  {
 61		  std::string media_type = content["content-type"].asString();
 62		  std::string::size_type idx1 = media_type.find_first_of(";");
 63		  std::string mime_type = media_type.substr(0, idx1);
 64		  completeAny(status, mime_type);
 65	  }
 66
 67	  void completeAny(U32 status, const std::string& mime_type)
 68	  {
 69		  // Set empty type to none/none.  Empty string is reserved for legacy parcels
 70		  // which have no mime type set.
 71		  std::string resolved_mime_type = ! mime_type.empty() ? mime_type : LLMIMETypes::getDefaultMimeType();
 72		  LLFloaterURLEntry* floater_url_entry = (LLFloaterURLEntry*)mParent.get();
 73		  if ( floater_url_entry )
 74			  floater_url_entry->headerFetchComplete( status, resolved_mime_type );
 75	  }
 76};
 77
 78//-----------------------------------------------------------------------------
 79// LLFloaterURLEntry()
 80//-----------------------------------------------------------------------------
 81LLFloaterURLEntry::LLFloaterURLEntry(LLHandle<LLPanel> parent)
 82	: LLFloater(LLSD()),
 83	  mPanelLandMediaHandle(parent)
 84{
 85	buildFromFile("floater_url_entry.xml");
 86}
 87
 88//-----------------------------------------------------------------------------
 89// ~LLFloaterURLEntry()
 90//-----------------------------------------------------------------------------
 91LLFloaterURLEntry::~LLFloaterURLEntry()
 92{
 93	sInstance = NULL;
 94}
 95
 96BOOL LLFloaterURLEntry::postBuild()
 97{
 98	mMediaURLEdit = getChild<LLComboBox>("media_entry");
 99
100	// Cancel button
101	childSetAction("cancel_btn", onBtnCancel, this);
102
103	// Cancel button
104	childSetAction("clear_btn", onBtnClear, this);
105	// clear media list button
106	LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
107	bool enable_clear_button = parcel_history.size() > 0 ? true : false;
108	getChildView("clear_btn")->setEnabled(enable_clear_button );
109
110	// OK button
111	childSetAction("ok_btn", onBtnOK, this);
112
113	setDefaultBtn("ok_btn");
114	buildURLHistory();
115
116	return TRUE;
117}
118void LLFloaterURLEntry::buildURLHistory()
119{
120	LLCtrlListInterface* url_list = childGetListInterface("media_entry");
121	if (url_list)
122	{
123		url_list->operateOnAll(LLCtrlListInterface::OP_DELETE);
124	}
125
126	// Get all of the entries in the "parcel" collection
127	LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
128
129	LLSD::array_iterator iter_history =
130		parcel_history.beginArray();
131	LLSD::array_iterator end_history =
132		parcel_history.endArray();
133	for(; iter_history != end_history; ++iter_history)
134	{
135		url_list->addSimpleElement((*iter_history).asString());
136	}
137}
138
139void LLFloaterURLEntry::headerFetchComplete(U32 status, const std::string& mime_type)
140{
141	LLPanelLandMedia* panel_media = dynamic_cast<LLPanelLandMedia*>(mPanelLandMediaHandle.get());
142	if (panel_media)
143	{
144		// status is ignored for now -- error = "none/none"
145		panel_media->setMediaType(mime_type);
146		panel_media->setMediaURL(mMediaURLEdit->getValue().asString());
147	}
148	else
149	{
150		LLPanelFace* panel_face = dynamic_cast<LLPanelFace*>(mPanelLandMediaHandle.get());
151		if(panel_face)
152		{
153			panel_face->setMediaType(mime_type);
154			panel_face->setMediaURL(mMediaURLEdit->getValue().asString());
155		}
156
157	}
158	// Decrement the cursor
159	getWindow()->decBusyCount();
160	getChildView("loading_label")->setVisible( false);
161	closeFloater();
162}
163
164// static
165LLHandle<LLFloater> LLFloaterURLEntry::show(LLHandle<LLPanel> parent, const std::string media_url)
166{
167	if (!sInstance)
168	{
169		sInstance = new LLFloaterURLEntry(parent);
170	}
171	sInstance->openFloater();
172	sInstance->addURLToCombobox(media_url);
173	return sInstance->getHandle();
174}
175
176
177
178bool LLFloaterURLEntry::addURLToCombobox(const std::string& media_url)
179{
180	if(! mMediaURLEdit->setSimple( media_url ) && ! media_url.empty())
181	{
182		mMediaURLEdit->add( media_url );
183		mMediaURLEdit->setSimple( media_url );
184		return true;
185	}
186
187	// URL was not added for whatever reason (either it was empty or already existed)
188	return false;
189}
190
191// static
192//-----------------------------------------------------------------------------
193// onBtnOK()
194//-----------------------------------------------------------------------------
195void LLFloaterURLEntry::onBtnOK( void* userdata )
196{
197	LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
198
199	std::string media_url	= self->mMediaURLEdit->getValue().asString();
200	self->mMediaURLEdit->remove(media_url);
201	LLURLHistory::removeURL("parcel", media_url);
202	if(self->addURLToCombobox(media_url))
203	{
204		// Add this url to the parcel collection
205		LLURLHistory::addURL("parcel", media_url);
206	}
207
208	// leading whitespace causes problems with the MIME-type detection so strip it
209	LLStringUtil::trim( media_url );
210
211	// First check the URL scheme
212	LLURI url(media_url);
213	std::string scheme = url.scheme();
214
215	// We assume that an empty scheme is an http url, as this is how we will treat it.
216	if(scheme == "")
217	{
218		scheme = "http";
219	}
220
221	// Discover the MIME type only for "http" scheme.
222	if(scheme == "http" || scheme == "https")
223	{
224		LLHTTPClient::getHeaderOnly( media_url,
225			new LLMediaTypeResponder(self->getHandle()));
226	}
227	else
228	{
229		self->headerFetchComplete(0, scheme);
230	}
231
232	// Grey the buttons until we get the header response
233	self->getChildView("ok_btn")->setEnabled(false);
234	self->getChildView("cancel_btn")->setEnabled(false);
235	self->getChildView("media_entry")->setEnabled(false);
236
237	// show progress bar here?
238	getWindow()->incBusyCount();
239	self->getChildView("loading_label")->setVisible( true);
240}
241
242// static
243//-----------------------------------------------------------------------------
244// onBtnCancel()
245//-----------------------------------------------------------------------------
246void LLFloaterURLEntry::onBtnCancel( void* userdata )
247{
248	LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
249	self->closeFloater();
250}
251
252// static
253//-----------------------------------------------------------------------------
254// onBtnClear()
255//-----------------------------------------------------------------------------
256void LLFloaterURLEntry::onBtnClear( void* userdata )
257{
258	LLNotificationsUtil::add( "ConfirmClearMediaUrlList", LLSD(), LLSD(), 
259									boost::bind(&LLFloaterURLEntry::callback_clear_url_list, (LLFloaterURLEntry*)userdata, _1, _2) );
260}
261
262bool LLFloaterURLEntry::callback_clear_url_list(const LLSD& notification, const LLSD& response)
263{
264	S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
265	if ( option == 0 ) // YES
266	{
267		// clear saved list
268		LLCtrlListInterface* url_list = childGetListInterface("media_entry");
269		if ( url_list )
270		{
271			url_list->operateOnAll( LLCtrlListInterface::OP_DELETE );
272		}
273
274		// clear current contents of combo box
275		mMediaURLEdit->clear();
276
277		// clear stored version of list
278		LLURLHistory::clear("parcel");
279
280		// cleared the list so disable Clear button
281		getChildView("clear_btn")->setEnabled(false );
282	}
283	return false;
284}