PageRenderTime 38ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/llpanelmediasettingssecurity.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 356 lines | 209 code | 50 blank | 97 comment | 23 complexity | dfb9237f62c2f0147e9062c8c7c196f5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llpanelmediasettingssecurity.cpp
  3. * @brief LLPanelMediaSettingsSecurity class implementation
  4. *
  5. * $LicenseInfo:firstyear=2009&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. #include "llviewerprecompiledheaders.h"
  27. #include "llpanelmediasettingssecurity.h"
  28. #include "llfloaterreg.h"
  29. #include "llpanelcontents.h"
  30. #include "llcheckboxctrl.h"
  31. #include "llnotificationsutil.h"
  32. #include "llscrolllistctrl.h"
  33. #include "llscrolllistitem.h"
  34. #include "lluictrlfactory.h"
  35. #include "llwindow.h"
  36. #include "llviewerwindow.h"
  37. #include "llsdutil.h"
  38. #include "llselectmgr.h"
  39. #include "llmediaentry.h"
  40. #include "lltextbox.h"
  41. #include "llfloaterwhitelistentry.h"
  42. #include "llfloatermediasettings.h"
  43. ////////////////////////////////////////////////////////////////////////////////
  44. //
  45. LLPanelMediaSettingsSecurity::LLPanelMediaSettingsSecurity() :
  46. mParent( NULL )
  47. {
  48. mCommitCallbackRegistrar.add("Media.whitelistAdd", boost::bind(&LLPanelMediaSettingsSecurity::onBtnAdd, this));
  49. mCommitCallbackRegistrar.add("Media.whitelistDelete", boost::bind(&LLPanelMediaSettingsSecurity::onBtnDel, this));
  50. // build dialog from XML
  51. buildFromFile( "panel_media_settings_security.xml");
  52. }
  53. ////////////////////////////////////////////////////////////////////////////////
  54. //
  55. BOOL LLPanelMediaSettingsSecurity::postBuild()
  56. {
  57. mEnableWhiteList = getChild< LLCheckBoxCtrl >( LLMediaEntry::WHITELIST_ENABLE_KEY );
  58. mWhiteListList = getChild< LLScrollListCtrl >( LLMediaEntry::WHITELIST_KEY );
  59. mHomeUrlFailsWhiteListText = getChild<LLTextBox>( "home_url_fails_whitelist" );
  60. setDefaultBtn("whitelist_add");
  61. return true;
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. // virtual
  65. LLPanelMediaSettingsSecurity::~LLPanelMediaSettingsSecurity()
  66. {
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. //
  70. void LLPanelMediaSettingsSecurity::draw()
  71. {
  72. // housekeeping
  73. LLPanel::draw();
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // static
  77. void LLPanelMediaSettingsSecurity::initValues( void* userdata, const LLSD& media_settings , bool editable)
  78. {
  79. LLPanelMediaSettingsSecurity *self =(LLPanelMediaSettingsSecurity *)userdata;
  80. std::string base_key( "" );
  81. std::string tentative_key( "" );
  82. struct
  83. {
  84. std::string key_name;
  85. LLUICtrl* ctrl_ptr;
  86. std::string ctrl_type;
  87. } data_set [] =
  88. {
  89. { LLMediaEntry::WHITELIST_ENABLE_KEY, self->mEnableWhiteList, "LLCheckBoxCtrl" },
  90. { LLMediaEntry::WHITELIST_KEY, self->mWhiteListList, "LLScrollListCtrl" },
  91. { "", NULL , "" }
  92. };
  93. for( int i = 0; data_set[ i ].key_name.length() > 0; ++i )
  94. {
  95. base_key = std::string( data_set[ i ].key_name );
  96. tentative_key = base_key + std::string( LLPanelContents::TENTATIVE_SUFFIX );
  97. bool enabled_overridden = false;
  98. // TODO: CP - I bet there is a better way to do this using Boost
  99. if ( media_settings[ base_key ].isDefined() )
  100. {
  101. if ( data_set[ i ].ctrl_type == "LLCheckBoxCtrl" )
  102. {
  103. static_cast< LLCheckBoxCtrl* >( data_set[ i ].ctrl_ptr )->
  104. setValue( media_settings[ base_key ].asBoolean() );
  105. }
  106. else
  107. if ( data_set[ i ].ctrl_type == "LLScrollListCtrl" )
  108. {
  109. // get control
  110. LLScrollListCtrl* list = static_cast< LLScrollListCtrl* >( data_set[ i ].ctrl_ptr );
  111. list->deleteAllItems();
  112. // points to list of white list URLs
  113. LLSD url_list = media_settings[ base_key ];
  114. // better be the whitelist
  115. llassert(data_set[ i ].ctrl_ptr == self->mWhiteListList);
  116. // If tentative, don't add entries
  117. if (media_settings[ tentative_key ].asBoolean())
  118. {
  119. self->mWhiteListList->setEnabled(false);
  120. enabled_overridden = true;
  121. }
  122. else {
  123. // iterate over them and add to scroll list
  124. LLSD::array_iterator iter = url_list.beginArray();
  125. while( iter != url_list.endArray() )
  126. {
  127. std::string entry = *iter;
  128. self->addWhiteListEntry( entry );
  129. ++iter;
  130. }
  131. }
  132. };
  133. if ( ! enabled_overridden) data_set[ i ].ctrl_ptr->setEnabled(editable);
  134. data_set[ i ].ctrl_ptr->setTentative( media_settings[ tentative_key ].asBoolean() );
  135. };
  136. };
  137. // initial update - hides/shows status messages etc.
  138. self->updateWhitelistEnableStatus();
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////
  141. // static
  142. void LLPanelMediaSettingsSecurity::clearValues( void* userdata , bool editable)
  143. {
  144. LLPanelMediaSettingsSecurity *self =(LLPanelMediaSettingsSecurity *)userdata;
  145. self->mEnableWhiteList->clear();
  146. self->mWhiteListList->deleteAllItems();
  147. self->mEnableWhiteList->setEnabled(editable);
  148. self->mWhiteListList->setEnabled(editable);
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////
  151. //
  152. void LLPanelMediaSettingsSecurity::preApply()
  153. {
  154. // no-op
  155. }
  156. ////////////////////////////////////////////////////////////////////////////////
  157. //
  158. void LLPanelMediaSettingsSecurity::getValues( LLSD &fill_me_in, bool include_tentative )
  159. {
  160. if (include_tentative || !mEnableWhiteList->getTentative())
  161. fill_me_in[LLMediaEntry::WHITELIST_ENABLE_KEY] = (LLSD::Boolean)mEnableWhiteList->getValue();
  162. if (include_tentative || !mWhiteListList->getTentative())
  163. {
  164. // iterate over white list and extract items
  165. std::vector< LLScrollListItem* > whitelist_items = mWhiteListList->getAllData();
  166. std::vector< LLScrollListItem* >::iterator iter = whitelist_items.begin();
  167. // *NOTE: need actually set the key to be an emptyArray(), or the merge
  168. // we do with this LLSD will think there's nothing to change.
  169. fill_me_in[LLMediaEntry::WHITELIST_KEY] = LLSD::emptyArray();
  170. while( iter != whitelist_items.end() )
  171. {
  172. LLScrollListCell* cell = (*iter)->getColumn( ENTRY_COLUMN );
  173. std::string whitelist_url = cell->getValue().asString();
  174. fill_me_in[ LLMediaEntry::WHITELIST_KEY ].append( whitelist_url );
  175. ++iter;
  176. };
  177. }
  178. }
  179. ////////////////////////////////////////////////////////////////////////////////
  180. //
  181. void LLPanelMediaSettingsSecurity::postApply()
  182. {
  183. // no-op
  184. }
  185. ///////////////////////////////////////////////////////////////////////////////
  186. // Try to make a valid URL if a fragment (
  187. // white list list box widget and build a list to test against. Can also
  188. const std::string LLPanelMediaSettingsSecurity::makeValidUrl( const std::string& src_url )
  189. {
  190. // use LLURI to determine if we have a valid scheme
  191. LLURI candidate_url( src_url );
  192. if ( candidate_url.scheme().empty() )
  193. {
  194. // build a URL comprised of default scheme and the original fragment
  195. const std::string default_scheme( "http://" );
  196. return default_scheme + src_url;
  197. };
  198. // we *could* test the "default scheme" + "original fragment" URL again
  199. // using LLURI to see if it's valid but I think the outcome is the same
  200. // in either case - our only option is to return the original URL
  201. // we *think* the original url passed in was valid
  202. return src_url;
  203. }
  204. ///////////////////////////////////////////////////////////////////////////////
  205. // wrapper for testing a URL against the whitelist. We grab entries from
  206. // white list list box widget and build a list to test against.
  207. bool LLPanelMediaSettingsSecurity::urlPassesWhiteList( const std::string& test_url )
  208. {
  209. // If the whitlelist list is tentative, it means we have multiple settings.
  210. // In that case, we have no choice but to return true
  211. if ( mWhiteListList->getTentative() ) return true;
  212. // the checkUrlAgainstWhitelist(..) function works on a vector
  213. // of strings for the white list entries - in this panel, the white list
  214. // is stored in the widgets themselves so we need to build something compatible.
  215. std::vector< std::string > whitelist_strings;
  216. whitelist_strings.clear(); // may not be required - I forget what the spec says.
  217. // step through whitelist widget entries and grab them as strings
  218. std::vector< LLScrollListItem* > whitelist_items = mWhiteListList->getAllData();
  219. std::vector< LLScrollListItem* >::iterator iter = whitelist_items.begin();
  220. while( iter != whitelist_items.end() )
  221. {
  222. LLScrollListCell* cell = (*iter)->getColumn( ENTRY_COLUMN );
  223. std::string whitelist_url = cell->getValue().asString();
  224. whitelist_strings.push_back( whitelist_url );
  225. ++iter;
  226. };
  227. // possible the URL is just a fragment so we validize it
  228. const std::string valid_url = makeValidUrl( test_url );
  229. // indicate if the URL passes whitelist
  230. return LLMediaEntry::checkUrlAgainstWhitelist( valid_url, whitelist_strings );
  231. }
  232. ///////////////////////////////////////////////////////////////////////////////
  233. //
  234. void LLPanelMediaSettingsSecurity::updateWhitelistEnableStatus()
  235. {
  236. // get the value for home URL and make it a valid URL
  237. const std::string valid_url = makeValidUrl( mParent->getHomeUrl() );
  238. // now check to see if the home url passes the whitelist in its entirity
  239. if ( urlPassesWhiteList( valid_url ) )
  240. {
  241. mEnableWhiteList->setEnabled( true );
  242. mHomeUrlFailsWhiteListText->setVisible( false );
  243. }
  244. else
  245. {
  246. mEnableWhiteList->set( false );
  247. mEnableWhiteList->setEnabled( false );
  248. mHomeUrlFailsWhiteListText->setVisible( true );
  249. };
  250. }
  251. ///////////////////////////////////////////////////////////////////////////////
  252. // Add an entry to the whitelist scrollbox and indicate if the current
  253. // home URL passes this entry or not using an icon
  254. void LLPanelMediaSettingsSecurity::addWhiteListEntry( const std::string& entry )
  255. {
  256. // grab the home url
  257. std::string home_url( "" );
  258. if ( mParent )
  259. home_url = mParent->getHomeUrl();
  260. // try to make a valid URL based on what the user entered - missing scheme for example
  261. const std::string valid_url = makeValidUrl( home_url );
  262. // check the home url against this single whitelist entry
  263. std::vector< std::string > whitelist_entries;
  264. whitelist_entries.push_back( entry );
  265. bool home_url_passes_entry = LLMediaEntry::checkUrlAgainstWhitelist( valid_url, whitelist_entries );
  266. // build an icon cell based on whether or not the home url pases it or not
  267. LLSD row;
  268. if ( home_url_passes_entry || home_url.empty() )
  269. {
  270. row[ "columns" ][ ICON_COLUMN ][ "type" ] = "icon";
  271. row[ "columns" ][ ICON_COLUMN ][ "value" ] = "";
  272. row[ "columns" ][ ICON_COLUMN ][ "width" ] = 20;
  273. }
  274. else
  275. {
  276. row[ "columns" ][ ICON_COLUMN ][ "type" ] = "icon";
  277. row[ "columns" ][ ICON_COLUMN ][ "value" ] = "Parcel_Exp_Color";
  278. row[ "columns" ][ ICON_COLUMN ][ "width" ] = 20;
  279. };
  280. // always add in the entry itself
  281. row[ "columns" ][ ENTRY_COLUMN ][ "type" ] = "text";
  282. row[ "columns" ][ ENTRY_COLUMN ][ "value" ] = entry;
  283. // add to the white list scroll box
  284. mWhiteListList->addElement( row );
  285. };
  286. ///////////////////////////////////////////////////////////////////////////////
  287. // static
  288. void LLPanelMediaSettingsSecurity::onBtnAdd( void* userdata )
  289. {
  290. LLFloaterReg::showInstance("whitelist_entry");
  291. }
  292. ///////////////////////////////////////////////////////////////////////////////
  293. // static
  294. void LLPanelMediaSettingsSecurity::onBtnDel( void* userdata )
  295. {
  296. LLPanelMediaSettingsSecurity *self =(LLPanelMediaSettingsSecurity *)userdata;
  297. self->mWhiteListList->deleteSelectedItems();
  298. // contents of whitelist changed so recheck it against home url
  299. self->updateWhitelistEnableStatus();
  300. }
  301. ////////////////////////////////////////////////////////////////////////////////
  302. //
  303. void LLPanelMediaSettingsSecurity::setParent( LLFloaterMediaSettings* parent )
  304. {
  305. mParent = parent;
  306. };