summaryrefslogtreecommitdiff
path: root/indra/newview/llfloaterurlentry.cpp
blob: 40f6d0728631d444ec251e949f3cd7a2c1221b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 * @file llfloaterurlentry.cpp
 * @brief LLFloaterURLEntry class implementation
 *
 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llhttpclient.h"

#include "llfloaterurlentry.h"

#include "llpanellandmedia.h"
#include "llpanelface.h"

#include "llcombobox.h"
#include "llmimetypes.h"
#include "llnotificationsutil.h"
#include "llurlhistory.h"
#include "lluictrlfactory.h"
#include "llwindow.h"
#include "llviewerwindow.h"

static LLFloaterURLEntry* sInstance = NULL;

// Move this to its own file.
// helper class that tries to download a URL from a web site and calls a method
// on the Panel Land Media and to discover the MIME type
class LLMediaTypeResponder : public LLHTTPClient::Responder
{
public:
	LLMediaTypeResponder( const LLHandle<LLFloater> parent ) :
	  mParent( parent )
	  {}

	  LLHandle<LLFloater> mParent;


	  virtual void completedHeader(U32 status, const std::string& reason, const LLSD& content)
	  {
		  std::string media_type = content["content-type"].asString();
		  std::string::size_type idx1 = media_type.find_first_of(";");
		  std::string mime_type = media_type.substr(0, idx1);
		  completeAny(status, mime_type);
	  }

	  void completeAny(U32 status, const std::string& mime_type)
	  {
		  // Set empty type to none/none.  Empty string is reserved for legacy parcels
		  // which have no mime type set.
		  std::string resolved_mime_type = ! mime_type.empty() ? mime_type : LLMIMETypes::getDefaultMimeType();
		  LLFloaterURLEntry* floater_url_entry = (LLFloaterURLEntry*)mParent.get();
		  if ( floater_url_entry )
			  floater_url_entry->headerFetchComplete( status, resolved_mime_type );
	  }
};

//-----------------------------------------------------------------------------
// LLFloaterURLEntry()
//-----------------------------------------------------------------------------
LLFloaterURLEntry::LLFloaterURLEntry(LLHandle<LLPanel> parent)
	: LLFloater(LLSD()),
	  mPanelLandMediaHandle(parent)
{
	LLUICtrlFactory::getInstance()->buildFloater(this, "floater_url_entry.xml", NULL);
}

//-----------------------------------------------------------------------------
// ~LLFloaterURLEntry()
//-----------------------------------------------------------------------------
LLFloaterURLEntry::~LLFloaterURLEntry()
{
	sInstance = NULL;
}

BOOL LLFloaterURLEntry::postBuild()
{
	mMediaURLEdit = getChild<LLComboBox>("media_entry");

	// Cancel button
	childSetAction("cancel_btn", onBtnCancel, this);

	// Cancel button
	childSetAction("clear_btn", onBtnClear, this);
	// clear media list button
	LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
	bool enable_clear_button = parcel_history.size() > 0 ? true : false;
	getChildView("clear_btn")->setEnabled(enable_clear_button );

	// OK button
	childSetAction("ok_btn", onBtnOK, this);

	setDefaultBtn("ok_btn");
	buildURLHistory();

	return TRUE;
}
void LLFloaterURLEntry::buildURLHistory()
{
	LLCtrlListInterface* url_list = childGetListInterface("media_entry");
	if (url_list)
	{
		url_list->operateOnAll(LLCtrlListInterface::OP_DELETE);
	}

	// Get all of the entries in the "parcel" collection
	LLSD parcel_history = LLURLHistory::getURLHistory("parcel");

	LLSD::array_iterator iter_history =
		parcel_history.beginArray();
	LLSD::array_iterator end_history =
		parcel_history.endArray();
	for(; iter_history != end_history; ++iter_history)
	{
		url_list->addSimpleElement((*iter_history).asString());
	}
}

void LLFloaterURLEntry::headerFetchComplete(U32 status, const std::string& mime_type)
{
	LLPanelLandMedia* panel_media = dynamic_cast<LLPanelLandMedia*>(mPanelLandMediaHandle.get());
	if (panel_media)
	{
		// status is ignored for now -- error = "none/none"
		panel_media->setMediaType(mime_type);
		panel_media->setMediaURL(mMediaURLEdit->getValue().asString());
	}
	else
	{
		LLPanelFace* panel_face = dynamic_cast<LLPanelFace*>(mPanelLandMediaHandle.get());
		if(panel_face)
		{
			panel_face->setMediaType(mime_type);
			panel_face->setMediaURL(mMediaURLEdit->getValue().asString());
		}

	}
	// Decrement the cursor
	getWindow()->decBusyCount();
	getChildView("loading_label")->setVisible( false);
	closeFloater();
}

// static
LLHandle<LLFloater> LLFloaterURLEntry::show(LLHandle<LLPanel> parent, const std::string media_url)
{
	if (!sInstance)
	{
		sInstance = new LLFloaterURLEntry(parent);
	}
	sInstance->openFloater();
	sInstance->addURLToCombobox(media_url);
	return sInstance->getHandle();
}



bool LLFloaterURLEntry::addURLToCombobox(const std::string& media_url)
{
	if(! mMediaURLEdit->setSimple( media_url ) && ! media_url.empty())
	{
		mMediaURLEdit->add( media_url );
		mMediaURLEdit->setSimple( media_url );
		return true;
	}

	// URL was not added for whatever reason (either it was empty or already existed)
	return false;
}

// static
//-----------------------------------------------------------------------------
// onBtnOK()
//-----------------------------------------------------------------------------
void LLFloaterURLEntry::onBtnOK( void* userdata )
{
	LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;

	std::string media_url	= self->mMediaURLEdit->getValue().asString();
	self->mMediaURLEdit->remove(media_url);
	LLURLHistory::removeURL("parcel", media_url);
	if(self->addURLToCombobox(media_url))
	{
		// Add this url to the parcel collection
		LLURLHistory::addURL("parcel", media_url);
	}

	// leading whitespace causes problems with the MIME-type detection so strip it
	LLStringUtil::trim( media_url );

	// First check the URL scheme
	LLURI url(media_url);
	std::string scheme = url.scheme();

	// We assume that an empty scheme is an http url, as this is how we will treat it.
	if(scheme == "")
	{
		scheme = "http";
	}

	// Discover the MIME type only for "http" scheme.
	if(scheme == "http" || scheme == "https")
	{
		LLHTTPClient::getHeaderOnly( media_url,
			new LLMediaTypeResponder(self->getHandle()));
	}
	else
	{
		self->headerFetchComplete(0, scheme);
	}

	// Grey the buttons until we get the header response
	self->getChildView("ok_btn")->setEnabled(false);
	self->getChildView("cancel_btn")->setEnabled(false);
	self->getChildView("media_entry")->setEnabled(false);

	// show progress bar here?
	getWindow()->incBusyCount();
	self->getChildView("loading_label")->setVisible( true);
}

// static
//-----------------------------------------------------------------------------
// onBtnCancel()
//-----------------------------------------------------------------------------
void LLFloaterURLEntry::onBtnCancel( void* userdata )
{
	LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
	self->closeFloater();
}

// static
//-----------------------------------------------------------------------------
// onBtnClear()
//-----------------------------------------------------------------------------
void LLFloaterURLEntry::onBtnClear( void* userdata )
{
	LLNotificationsUtil::add( "ConfirmClearMediaUrlList", LLSD(), LLSD(), 
									boost::bind(&LLFloaterURLEntry::callback_clear_url_list, (LLFloaterURLEntry*)userdata, _1, _2) );
}

bool LLFloaterURLEntry::callback_clear_url_list(const LLSD& notification, const LLSD& response)
{
	S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
	if ( option == 0 ) // YES
	{
		// clear saved list
		LLCtrlListInterface* url_list = childGetListInterface("media_entry");
		if ( url_list )
		{
			url_list->operateOnAll( LLCtrlListInterface::OP_DELETE );
		}

		// clear current contents of combo box
		mMediaURLEdit->clear();

		// clear stored version of list
		LLURLHistory::clear("parcel");

		// cleared the list so disable Clear button
		getChildView("clear_btn")->setEnabled(false );
	}
	return false;
}