summaryrefslogtreecommitdiff
path: root/indra/newview/llpanelmediasettingssecurity.cpp
blob: bec2494eac9b589d67fe20d5bad5c7060c817069 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
 * @file llpanelmediasettingssecurity.cpp
 * @brief LLPanelMediaSettingsSecurity class implementation
 *
 * $LicenseInfo:firstyear=2009&license=viewergpl$
 * 
 * Copyright (c) 2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"
#include "llfloaterreg.h"
#include "llpanelmediasettingssecurity.h"
#include "llpanelcontents.h"
#include "llcheckboxctrl.h"
#include "llscrolllistctrl.h"
#include "llscrolllistitem.h"
#include "lluictrlfactory.h"
#include "llwindow.h"
#include "llviewerwindow.h"
#include "llsdutil.h"
#include "llselectmgr.h"
#include "llmediaentry.h"
#include "llfloaterwhitelistentry.h"
#include "llfloatermediasettings.h"
////////////////////////////////////////////////////////////////////////////////
//
LLPanelMediaSettingsSecurity::LLPanelMediaSettingsSecurity() :
	mParent( NULL )
{
	mCommitCallbackRegistrar.add("Media.whitelistAdd",		boost::bind(&LLPanelMediaSettingsSecurity::onBtnAdd, this));
	mCommitCallbackRegistrar.add("Media.whitelistDelete",	boost::bind(&LLPanelMediaSettingsSecurity::onBtnDel, this));	
	// build dialog from XML
	LLUICtrlFactory::getInstance()->buildPanel(this, "panel_media_settings_security.xml");

}

////////////////////////////////////////////////////////////////////////////////
//
BOOL LLPanelMediaSettingsSecurity::postBuild()
{
	mEnableWhiteList = getChild< LLCheckBoxCtrl >( LLMediaEntry::WHITELIST_ENABLE_KEY );
	mWhiteListList = getChild< LLScrollListCtrl >( LLMediaEntry::WHITELIST_KEY );
	
	setDefaultBtn("whitelist_add");

	return true;
}

////////////////////////////////////////////////////////////////////////////////
// virtual
LLPanelMediaSettingsSecurity::~LLPanelMediaSettingsSecurity()
{
}

////////////////////////////////////////////////////////////////////////////////
// 
void LLPanelMediaSettingsSecurity::draw()
{
	// housekeeping
	LLPanel::draw();

	// if list is empty, disable DEL button and checkbox to enable use of list
	if ( mWhiteListList->isEmpty() )
	{
		childSetEnabled( "whitelist_del", false );
		childSetEnabled( LLMediaEntry::WHITELIST_KEY, false );
		childSetEnabled( LLMediaEntry::WHITELIST_ENABLE_KEY, false );
	}
	else
	{
		childSetEnabled( "whitelist_del", true );
		childSetEnabled( LLMediaEntry::WHITELIST_KEY, true );
		childSetEnabled( LLMediaEntry::WHITELIST_ENABLE_KEY, true );
	};

	// if nothing is selected, disable DEL button
	if ( mWhiteListList->getSelectedValue().asString().empty() )
	{
		childSetEnabled( "whitelist_del", false );
	}
	else
	{
		childSetEnabled( "whitelist_del", true );
	};
}

////////////////////////////////////////////////////////////////////////////////
// static 
void LLPanelMediaSettingsSecurity::initValues( void* userdata, const LLSD& media_settings , bool editable)
{
	LLPanelMediaSettingsSecurity *self =(LLPanelMediaSettingsSecurity *)userdata;

	if ( LLFloaterMediaSettings::getInstance()->mIdenticalHasMediaInfo )
	{
		if(LLFloaterMediaSettings::getInstance()->mMultipleMedia) 
		{
			self->clearValues(self, editable);
			// only show multiple 
			return;
		}
		
	}
	else
	{
		if(LLFloaterMediaSettings::getInstance()->mMultipleValidMedia) 
		{
			self->clearValues(self, editable);
			// only show multiple 
			return;
		}			
		
	}
	std::string base_key( "" );
	std::string tentative_key( "" );

	struct 
	{
		std::string key_name;
		LLUICtrl* ctrl_ptr;
		std::string ctrl_type;

	} data_set [] = 
	{
		{ LLMediaEntry::WHITELIST_ENABLE_KEY,	self->mEnableWhiteList,		"LLCheckBoxCtrl" },
		{ LLMediaEntry::WHITELIST_KEY,			self->mWhiteListList,		"LLScrollListCtrl" },
		{ "", NULL , "" }
	};

	for( int i = 0; data_set[ i ].key_name.length() > 0; ++i )
	{
		base_key = std::string( data_set[ i ].key_name );
        tentative_key = base_key + std::string( LLPanelContents::TENTATIVE_SUFFIX );

		// TODO: CP - I bet there is a better way to do this using Boost
		if ( media_settings[ base_key ].isDefined() )
		{
			if ( data_set[ i ].ctrl_type == "LLCheckBoxCtrl" )
			{
				static_cast< LLCheckBoxCtrl* >( data_set[ i ].ctrl_ptr )->
					setValue( media_settings[ base_key ].asBoolean() );
			}
			else
			if ( data_set[ i ].ctrl_type == "LLScrollListCtrl" )
			{
				// get control 
				LLScrollListCtrl* list = static_cast< LLScrollListCtrl* >( data_set[ i ].ctrl_ptr );
				list->deleteAllItems();

				// points to list of white list URLs
				LLSD url_list = media_settings[ base_key ];

				// iterate over them and add to scroll list
				LLSD::array_iterator iter = url_list.beginArray();
				while( iter != url_list.endArray() )
				{
					// TODO: is iter guaranteed to be valid here?
					std::string url = *iter;
					list->addSimpleElement( url );
					++iter;
				};
			};
			data_set[ i ].ctrl_ptr->setEnabled(editable);
			data_set[ i ].ctrl_ptr->setTentative( media_settings[ tentative_key ].asBoolean() );
		};
	};
}

////////////////////////////////////////////////////////////////////////////////
// static 
void LLPanelMediaSettingsSecurity::clearValues( void* userdata , bool editable)
{
	LLPanelMediaSettingsSecurity *self =(LLPanelMediaSettingsSecurity *)userdata;
	self->mEnableWhiteList->clear();
	self->mWhiteListList->deleteAllItems();
	self->mEnableWhiteList->setEnabled(editable);
	self->mWhiteListList->setEnabled(editable);
}

////////////////////////////////////////////////////////////////////////////////
// 
void LLPanelMediaSettingsSecurity::preApply()
{
	// no-op
}

////////////////////////////////////////////////////////////////////////////////
//
void LLPanelMediaSettingsSecurity::getValues( LLSD &fill_me_in )
{
    fill_me_in[LLMediaEntry::WHITELIST_ENABLE_KEY] = mEnableWhiteList->getValue();

    // iterate over white list and extract items
    std::vector< LLScrollListItem* > white_list_items = mWhiteListList->getAllData();
    std::vector< LLScrollListItem* >::iterator iter = white_list_items.begin();
	// *NOTE: need actually set the key to be an emptyArray(), or the merge
	// we do with this LLSD will think there's nothing to change.
    fill_me_in[LLMediaEntry::WHITELIST_KEY] = LLSD::emptyArray();
    while( iter != white_list_items.end() )
    {
        std::string white_list_url = (*iter)->getValue().asString();
        fill_me_in[ LLMediaEntry::WHITELIST_KEY ].append( white_list_url );
        ++iter;
    };
}

////////////////////////////////////////////////////////////////////////////////
// 
void LLPanelMediaSettingsSecurity::postApply()
{
	// no-op
}

///////////////////////////////////////////////////////////////////////////////
// Try to make a valid URL if a fragment (
// white list list box widget and build a list to test against. Can also
const std::string LLPanelMediaSettingsSecurity::makeValidUrl( const std::string& src_url )
{
	// use LLURI to determine if we have a valid scheme
	LLURI candidate_url( src_url );
	if ( candidate_url.scheme().empty() )
	{
		// build a URL comprised of default scheme and the original fragment 
		const std::string default_scheme( "http://" );
		return default_scheme + src_url;
	};

	// we *could* test the "default scheme" + "original fragment" URL again
	// using LLURI to see if it's valid but I think the outcome is the same
	// in either case - our only option is to return the original URL

	// we *think* the original url passed in was valid
	return src_url;
}

///////////////////////////////////////////////////////////////////////////////
// wrapper for testing a URL against the whitelist. We grab entries from
// white list list box widget and build a list to test against. Can also
// optionally pass the URL that you are trying to add to the widget since
// it won't be added until this call returns.
bool LLPanelMediaSettingsSecurity::passesWhiteList( const std::string& added_url,
													const std::string& test_url )
{
	// the checkUrlAgainstWhitelist(..) function works on a vector
	// of strings for the white list entries - in this panel, the white list
	// is stored in the widgets themselves so we need to build something compatible.
	std::vector< std::string > whitelist_strings;
	whitelist_strings.clear();	// may not be required - I forget what the spec says.

	// step through whitelist widget entries and grab them as strings
    std::vector< LLScrollListItem* > white_list_items = mWhiteListList->getAllData();
    std::vector< LLScrollListItem* >::iterator iter = white_list_items.begin(); 
	while( iter != white_list_items.end()  )
    {
        const std::string whitelist_url = (*iter)->getValue().asString();
		whitelist_strings.push_back( whitelist_url );

		++iter;
    };

	// add in the URL that might be added to the whitelist so we can test that too
	if ( added_url.length() )
		whitelist_strings.push_back( added_url );

	// possible the URL is just a fragment so we validize it
	const std::string valid_url = makeValidUrl( test_url );

	// indicate if the URL passes whitelist
	return LLMediaEntry::checkUrlAgainstWhitelist( valid_url, whitelist_strings );
}

///////////////////////////////////////////////////////////////////////////////
//
void LLPanelMediaSettingsSecurity::addWhiteListItem(const std::string& url)
{
	// grab home URL from the general panel (via the parent floater)
	std::string home_url( "" );
	if ( mParent )
		home_url = mParent->getHomeUrl();

	// if the home URL is blank (user hasn't entered it yet) then
	// don't bother to check if it passes the white list
	if ( home_url.empty() )
	{
		mWhiteListList->addSimpleElement( url );
		return;
	};

	// if the URL passes the white list, add it
	if ( passesWhiteList( url, home_url ) )
	{
		mWhiteListList->addSimpleElement( url );
	}
	else
	// display a message indicating you can't do that
	{
		LLNotifications::instance().add("WhiteListInvalidatesHomeUrl");
	};
}

///////////////////////////////////////////////////////////////////////////////
// static
void LLPanelMediaSettingsSecurity::onBtnAdd( void* userdata )
{
	LLFloaterReg::showInstance("whitelist_entry");
}

///////////////////////////////////////////////////////////////////////////////
// static
void LLPanelMediaSettingsSecurity::onBtnDel( void* userdata )
{
	LLPanelMediaSettingsSecurity *self =(LLPanelMediaSettingsSecurity *)userdata;

	self->mWhiteListList->deleteSelectedItems();
}

////////////////////////////////////////////////////////////////////////////////
//
void LLPanelMediaSettingsSecurity::setParent( LLFloaterMediaSettings* parent )
{
	mParent = parent;
};