PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms 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
Possible License(s): LGPL-2.1
  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. #include "llviewerprecompiledheaders.h"
  27. #include "llhttpclient.h"
  28. #include "llfloaterurlentry.h"
  29. #include "llpanellandmedia.h"
  30. #include "llpanelface.h"
  31. #include "llcombobox.h"
  32. #include "llmimetypes.h"
  33. #include "llnotificationsutil.h"
  34. #include "llurlhistory.h"
  35. #include "lluictrlfactory.h"
  36. #include "llwindow.h"
  37. #include "llviewerwindow.h"
  38. static LLFloaterURLEntry* sInstance = NULL;
  39. // Move this to its own file.
  40. // helper class that tries to download a URL from a web site and calls a method
  41. // on the Panel Land Media and to discover the MIME type
  42. class LLMediaTypeResponder : public LLHTTPClient::Responder
  43. {
  44. public:
  45. LLMediaTypeResponder( const LLHandle<LLFloater> parent ) :
  46. mParent( parent )
  47. {}
  48. LLHandle<LLFloater> mParent;
  49. virtual void completedHeader(U32 status, const std::string& reason, const LLSD& content)
  50. {
  51. std::string media_type = content["content-type"].asString();
  52. std::string::size_type idx1 = media_type.find_first_of(";");
  53. std::string mime_type = media_type.substr(0, idx1);
  54. completeAny(status, mime_type);
  55. }
  56. void completeAny(U32 status, const std::string& mime_type)
  57. {
  58. // Set empty type to none/none. Empty string is reserved for legacy parcels
  59. // which have no mime type set.
  60. std::string resolved_mime_type = ! mime_type.empty() ? mime_type : LLMIMETypes::getDefaultMimeType();
  61. LLFloaterURLEntry* floater_url_entry = (LLFloaterURLEntry*)mParent.get();
  62. if ( floater_url_entry )
  63. floater_url_entry->headerFetchComplete( status, resolved_mime_type );
  64. }
  65. };
  66. //-----------------------------------------------------------------------------
  67. // LLFloaterURLEntry()
  68. //-----------------------------------------------------------------------------
  69. LLFloaterURLEntry::LLFloaterURLEntry(LLHandle<LLPanel> parent)
  70. : LLFloater(LLSD()),
  71. mPanelLandMediaHandle(parent)
  72. {
  73. buildFromFile("floater_url_entry.xml");
  74. }
  75. //-----------------------------------------------------------------------------
  76. // ~LLFloaterURLEntry()
  77. //-----------------------------------------------------------------------------
  78. LLFloaterURLEntry::~LLFloaterURLEntry()
  79. {
  80. sInstance = NULL;
  81. }
  82. BOOL LLFloaterURLEntry::postBuild()
  83. {
  84. mMediaURLEdit = getChild<LLComboBox>("media_entry");
  85. // Cancel button
  86. childSetAction("cancel_btn", onBtnCancel, this);
  87. // Cancel button
  88. childSetAction("clear_btn", onBtnClear, this);
  89. // clear media list button
  90. LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
  91. bool enable_clear_button = parcel_history.size() > 0 ? true : false;
  92. getChildView("clear_btn")->setEnabled(enable_clear_button );
  93. // OK button
  94. childSetAction("ok_btn", onBtnOK, this);
  95. setDefaultBtn("ok_btn");
  96. buildURLHistory();
  97. return TRUE;
  98. }
  99. void LLFloaterURLEntry::buildURLHistory()
  100. {
  101. LLCtrlListInterface* url_list = childGetListInterface("media_entry");
  102. if (url_list)
  103. {
  104. url_list->operateOnAll(LLCtrlListInterface::OP_DELETE);
  105. }
  106. // Get all of the entries in the "parcel" collection
  107. LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
  108. LLSD::array_iterator iter_history =
  109. parcel_history.beginArray();
  110. LLSD::array_iterator end_history =
  111. parcel_history.endArray();
  112. for(; iter_history != end_history; ++iter_history)
  113. {
  114. url_list->addSimpleElement((*iter_history).asString());
  115. }
  116. }
  117. void LLFloaterURLEntry::headerFetchComplete(U32 status, const std::string& mime_type)
  118. {
  119. LLPanelLandMedia* panel_media = dynamic_cast<LLPanelLandMedia*>(mPanelLandMediaHandle.get());
  120. if (panel_media)
  121. {
  122. // status is ignored for now -- error = "none/none"
  123. panel_media->setMediaType(mime_type);
  124. panel_media->setMediaURL(mMediaURLEdit->getValue().asString());
  125. }
  126. else
  127. {
  128. LLPanelFace* panel_face = dynamic_cast<LLPanelFace*>(mPanelLandMediaHandle.get());
  129. if(panel_face)
  130. {
  131. panel_face->setMediaType(mime_type);
  132. panel_face->setMediaURL(mMediaURLEdit->getValue().asString());
  133. }
  134. }
  135. // Decrement the cursor
  136. getWindow()->decBusyCount();
  137. getChildView("loading_label")->setVisible( false);
  138. closeFloater();
  139. }
  140. // static
  141. LLHandle<LLFloater> LLFloaterURLEntry::show(LLHandle<LLPanel> parent, const std::string media_url)
  142. {
  143. if (!sInstance)
  144. {
  145. sInstance = new LLFloaterURLEntry(parent);
  146. }
  147. sInstance->openFloater();
  148. sInstance->addURLToCombobox(media_url);
  149. return sInstance->getHandle();
  150. }
  151. bool LLFloaterURLEntry::addURLToCombobox(const std::string& media_url)
  152. {
  153. if(! mMediaURLEdit->setSimple( media_url ) && ! media_url.empty())
  154. {
  155. mMediaURLEdit->add( media_url );
  156. mMediaURLEdit->setSimple( media_url );
  157. return true;
  158. }
  159. // URL was not added for whatever reason (either it was empty or already existed)
  160. return false;
  161. }
  162. // static
  163. //-----------------------------------------------------------------------------
  164. // onBtnOK()
  165. //-----------------------------------------------------------------------------
  166. void LLFloaterURLEntry::onBtnOK( void* userdata )
  167. {
  168. LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
  169. std::string media_url = self->mMediaURLEdit->getValue().asString();
  170. self->mMediaURLEdit->remove(media_url);
  171. LLURLHistory::removeURL("parcel", media_url);
  172. if(self->addURLToCombobox(media_url))
  173. {
  174. // Add this url to the parcel collection
  175. LLURLHistory::addURL("parcel", media_url);
  176. }
  177. // leading whitespace causes problems with the MIME-type detection so strip it
  178. LLStringUtil::trim( media_url );
  179. // First check the URL scheme
  180. LLURI url(media_url);
  181. std::string scheme = url.scheme();
  182. // We assume that an empty scheme is an http url, as this is how we will treat it.
  183. if(scheme == "")
  184. {
  185. scheme = "http";
  186. }
  187. // Discover the MIME type only for "http" scheme.
  188. if(scheme == "http" || scheme == "https")
  189. {
  190. LLHTTPClient::getHeaderOnly( media_url,
  191. new LLMediaTypeResponder(self->getHandle()));
  192. }
  193. else
  194. {
  195. self->headerFetchComplete(0, scheme);
  196. }
  197. // Grey the buttons until we get the header response
  198. self->getChildView("ok_btn")->setEnabled(false);
  199. self->getChildView("cancel_btn")->setEnabled(false);
  200. self->getChildView("media_entry")->setEnabled(false);
  201. // show progress bar here?
  202. getWindow()->incBusyCount();
  203. self->getChildView("loading_label")->setVisible( true);
  204. }
  205. // static
  206. //-----------------------------------------------------------------------------
  207. // onBtnCancel()
  208. //-----------------------------------------------------------------------------
  209. void LLFloaterURLEntry::onBtnCancel( void* userdata )
  210. {
  211. LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
  212. self->closeFloater();
  213. }
  214. // static
  215. //-----------------------------------------------------------------------------
  216. // onBtnClear()
  217. //-----------------------------------------------------------------------------
  218. void LLFloaterURLEntry::onBtnClear( void* userdata )
  219. {
  220. LLNotificationsUtil::add( "ConfirmClearMediaUrlList", LLSD(), LLSD(),
  221. boost::bind(&LLFloaterURLEntry::callback_clear_url_list, (LLFloaterURLEntry*)userdata, _1, _2) );
  222. }
  223. bool LLFloaterURLEntry::callback_clear_url_list(const LLSD& notification, const LLSD& response)
  224. {
  225. S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
  226. if ( option == 0 ) // YES
  227. {
  228. // clear saved list
  229. LLCtrlListInterface* url_list = childGetListInterface("media_entry");
  230. if ( url_list )
  231. {
  232. url_list->operateOnAll( LLCtrlListInterface::OP_DELETE );
  233. }
  234. // clear current contents of combo box
  235. mMediaURLEdit->clear();
  236. // clear stored version of list
  237. LLURLHistory::clear("parcel");
  238. // cleared the list so disable Clear button
  239. getChildView("clear_btn")->setEnabled(false );
  240. }
  241. return false;
  242. }