llfloaterurlentry.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llfloaterurlentry.h"
00035 
00036 #include "llpanellandmedia.h"
00037 
00038 // project includes
00039 #include "llcombobox.h"
00040 #include "llurlhistory.h"
00041 #include "lluictrlfactory.h"
00042 #include "llwindow.h"
00043 #include "llviewerwindow.h"
00044 
00045 static LLFloaterURLEntry* sInstance = NULL;
00046 
00047 // Move this to its own file.
00048 // helper class that tries to download a URL from a web site and calls a method
00049 // on the Panel Land Media and to discover the MIME type
00050 class LLMediaTypeResponder : public LLHTTPClient::Responder
00051 {
00052 public:
00053         LLMediaTypeResponder( const LLHandle<LLFloater> parent ) :
00054           mParent( parent )
00055           {}
00056 
00057           LLHandle<LLFloater> mParent;
00058 
00059 
00060           virtual void completedHeader(U32 status, const std::string& reason, const LLSD& content)
00061           {
00062                   std::string media_type = content["content-type"].asString();
00063                   std::string::size_type idx1 = media_type.find_first_of(";");
00064                   std::string mime_type = media_type.substr(0, idx1);
00065                   completeAny(status, mime_type);
00066           }
00067 
00068           virtual void error( U32 status, const std::string& reason )
00069           {
00070                   completeAny(status, "none/none");
00071           }
00072 
00073           void completeAny(U32 status, const std::string& mime_type)
00074           {
00075                   // Set empty type to none/none.  Empty string is reserved for legacy parcels
00076                   // which have no mime type set.
00077                   std::string resolved_mime_type = ! mime_type.empty() ? mime_type : "none/none";
00078                   LLFloaterURLEntry* floater_url_entry = (LLFloaterURLEntry*)mParent.get();
00079                   if ( floater_url_entry )
00080                           floater_url_entry->headerFetchComplete( status, resolved_mime_type );
00081           }
00082 };
00083 
00084 //-----------------------------------------------------------------------------
00085 // LLFloaterURLEntry()
00086 //-----------------------------------------------------------------------------
00087 LLFloaterURLEntry::LLFloaterURLEntry(LLHandle<LLPanel> parent)
00088         :
00089         LLFloater(),
00090         mPanelLandMediaHandle(parent)
00091 {
00092         LLUICtrlFactory::getInstance()->buildFloater(this, "floater_url_entry.xml");
00093 
00094         mMediaURLEdit = getChild<LLComboBox>("media_entry");
00095 
00096         // Cancel button
00097         childSetAction("cancel_btn", onBtnCancel, this);
00098 
00099         // Cancel button
00100         childSetAction("clear_btn", onBtnClear, this);
00101 
00102         // clear media list button
00103         LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
00104         bool enable_clear_button = parcel_history.size() > 0 ? true : false;
00105         childSetEnabled( "clear_btn", enable_clear_button );
00106 
00107         // OK button
00108         childSetAction("ok_btn", onBtnOK, this);
00109 
00110         setDefaultBtn("ok_btn");
00111         buildURLHistory();
00112 
00113         sInstance = this;
00114 }
00115 
00116 //-----------------------------------------------------------------------------
00117 // ~LLFloaterURLEntry()
00118 //-----------------------------------------------------------------------------
00119 LLFloaterURLEntry::~LLFloaterURLEntry()
00120 {
00121         sInstance = NULL;
00122 }
00123 
00124 void LLFloaterURLEntry::buildURLHistory()
00125 {
00126         LLCtrlListInterface* url_list = childGetListInterface("media_entry");
00127         if (url_list)
00128         {
00129                 url_list->operateOnAll(LLCtrlListInterface::OP_DELETE);
00130         }
00131 
00132         // Get all of the entries in the "parcel" collection
00133         LLSD parcel_history = LLURLHistory::getURLHistory("parcel");
00134 
00135         LLSD::array_iterator iter_history =
00136                 parcel_history.beginArray();
00137         LLSD::array_iterator end_history =
00138                 parcel_history.endArray();
00139         for(; iter_history != end_history; ++iter_history)
00140         {
00141                 url_list->addSimpleElement((*iter_history).asString());
00142         }
00143 }
00144 
00145 void LLFloaterURLEntry::headerFetchComplete(U32 status, const std::string& mime_type)
00146 {
00147         LLPanelLandMedia* panel_media = (LLPanelLandMedia*)mPanelLandMediaHandle.get();
00148         if (panel_media)
00149         {
00150                 // status is ignored for now -- error = "none/none"
00151                 panel_media->setMediaType(mime_type);
00152                 panel_media->setMediaURL(mMediaURLEdit->getValue().asString());
00153         }
00154         // Decrement the cursor
00155         getWindow()->decBusyCount();
00156         childSetVisible("loading_label", false);
00157         close();
00158 }
00159 
00160 // static
00161 LLHandle<LLFloater> LLFloaterURLEntry::show(LLHandle<LLPanel> parent)
00162 {
00163         if (sInstance)
00164         {
00165                 sInstance->open();
00166         }
00167         else
00168         {
00169                 sInstance = new LLFloaterURLEntry(parent);
00170         }
00171         sInstance->updateFromLandMediaPanel();
00172         return sInstance->getHandle();
00173 }
00174 
00175 void LLFloaterURLEntry::updateFromLandMediaPanel()
00176 {
00177         LLPanelLandMedia* panel_media = (LLPanelLandMedia*)mPanelLandMediaHandle.get();
00178         if (panel_media)
00179         {
00180                 std::string media_url = panel_media->getMediaURL();
00181                 addURLToCombobox(media_url);
00182         }
00183 }
00184 
00185 bool LLFloaterURLEntry::addURLToCombobox(const std::string& media_url)
00186 {
00187         if(! mMediaURLEdit->setSimple( media_url ) && ! media_url.empty())
00188         {
00189                 mMediaURLEdit->add( media_url );
00190                 mMediaURLEdit->setSimple( media_url );
00191                 return true;
00192         }
00193 
00194         // URL was not added for whatever reason (either it was empty or already existed)
00195         return false;
00196 }
00197 
00198 // static
00199 //-----------------------------------------------------------------------------
00200 // onBtnOK()
00201 //-----------------------------------------------------------------------------
00202 void LLFloaterURLEntry::onBtnOK( void* userdata )
00203 {
00204         LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
00205 
00206         std::string media_url   = self->mMediaURLEdit->getValue().asString();
00207         self->mMediaURLEdit->remove(media_url);
00208         LLURLHistory::removeURL("parcel", media_url);
00209         if(self->addURLToCombobox(media_url))
00210         {
00211                 // Add this url to the parcel collection
00212                 LLURLHistory::addURL("parcel", media_url);
00213         }
00214 
00215         // leading whitespace causes problems with the MIME-type detection so strip it
00216         LLString::trim( media_url );
00217 
00218         // First check the URL scheme
00219         LLURI url(media_url);
00220         std::string scheme = url.scheme();
00221 
00222         // We assume that an empty scheme is an http url, as this is how we will treat it.
00223         if(scheme == "")
00224         {
00225                 scheme = "http";
00226         }
00227 
00228         // Discover the MIME type only for "http" scheme.
00229         if(scheme == "http")
00230         {
00231                 LLHTTPClient::getHeaderOnly( media_url,
00232                         new LLMediaTypeResponder(self->getHandle()));
00233         }
00234         else
00235         {
00236                 self->headerFetchComplete(0, scheme);
00237         }
00238 
00239         // Grey the buttons until we get the header response
00240         self->childSetEnabled("ok_btn", false);
00241         self->childSetEnabled("cancel_btn", false);
00242         self->childSetEnabled("media_entry", false);
00243 
00244         // show progress bar here?
00245         getWindow()->incBusyCount();
00246         self->childSetVisible("loading_label", true);
00247 }
00248 
00249 // static
00250 //-----------------------------------------------------------------------------
00251 // onBtnCancel()
00252 //-----------------------------------------------------------------------------
00253 void LLFloaterURLEntry::onBtnCancel( void* userdata )
00254 {
00255         LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
00256         self->close();
00257 }
00258 
00259 // static
00260 //-----------------------------------------------------------------------------
00261 // onBtnClear()
00262 //-----------------------------------------------------------------------------
00263 void LLFloaterURLEntry::onBtnClear( void* userdata )
00264 {
00265         gViewerWindow->alertXml( "ConfirmClearMediaUrlList", callback_clear_url_list, userdata );
00266 }
00267 
00268 void LLFloaterURLEntry::callback_clear_url_list(S32 option, void* userdata)
00269 {
00270         if ( option == 0 ) // YES
00271         {
00272                 LLFloaterURLEntry *self =(LLFloaterURLEntry *)userdata;
00273 
00274                 if ( self )
00275                 {
00276                         // clear saved list
00277                         LLCtrlListInterface* url_list = self->childGetListInterface("media_entry");
00278                         if ( url_list )
00279                         {
00280                                 url_list->operateOnAll( LLCtrlListInterface::OP_DELETE );
00281                         }
00282 
00283                         // clear current contents of combo box
00284                         self->mMediaURLEdit->clear();
00285 
00286                         // clear stored version of list
00287                         LLURLHistory::clear("parcel");
00288 
00289                         // cleared the list so disable Clear button
00290                         self->childSetEnabled( "clear_btn", false );
00291                 }
00292         }
00293 }

Generated on Fri May 16 08:33:33 2008 for SecondLife by  doxygen 1.5.5