llwearablelist.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llwearablelist.h"
00035 
00036 #include "message.h"
00037 #include "llassetstorage.h"
00038 #include "llagent.h"
00039 #include "llvoavatar.h"
00040 #include "llviewerinventory.h"
00041 //#include "llfloaterchat.h"
00042 #include "llviewerstats.h"
00043 #include "llnotify.h"
00044 
00045 // Globals
00046 LLWearableList gWearableList;
00047 
00048 
00049 struct LLWearableArrivedData
00050 {
00051         LLWearableArrivedData( 
00052                 LLAssetType::EType asset_type,
00053                 const LLString& wearable_name,
00054                 void(*asset_arrived_callback)(LLWearable*, void* userdata),
00055                 void* userdata ) 
00056                 :
00057                 mAssetType( asset_type ),
00058                 mCallback( asset_arrived_callback ), 
00059                 mUserdata( userdata ),
00060                 mName( wearable_name )
00061                 {}
00062 
00063         LLAssetType::EType mAssetType;
00064         void    (*mCallback)(LLWearable*, void* userdata);
00065         void*   mUserdata;
00066         LLString mName;
00067 };
00068 
00069 
00070 
00072 // LLWearableList
00073 
00074 LLWearableList::~LLWearableList()
00075 {
00076         for_each(mList.begin(), mList.end(), DeletePairedPointer());
00077         mList.clear();
00078 }
00079 
00080 void LLWearableList::getAsset( const LLAssetID& assetID, const LLString& wearable_name, LLAssetType::EType asset_type, void(*asset_arrived_callback)(LLWearable*, void* userdata), void* userdata )
00081 {
00082         llassert( (asset_type == LLAssetType::AT_CLOTHING) || (asset_type == LLAssetType::AT_BODYPART) );
00083         LLWearable* instance = get_if_there(mList, assetID, (LLWearable*)NULL );
00084         if( instance )
00085         {
00086                 asset_arrived_callback( instance, userdata );
00087         }
00088         else
00089         {
00090                 gAssetStorage->getAssetData(
00091                         assetID,
00092                         asset_type,
00093                         LLWearableList::processGetAssetReply,
00094                         (void*)new LLWearableArrivedData( asset_type, wearable_name, asset_arrived_callback, userdata ),
00095                         TRUE);
00096         }
00097 }
00098 
00099 // static
00100 void LLWearableList::processGetAssetReply( const char* filename, const LLAssetID& uuid, void* userdata, S32 status, LLExtStat ext_status )
00101 {
00102         BOOL success = FALSE;
00103         LLWearableArrivedData* data = (LLWearableArrivedData*) userdata;
00104 
00105         if( !filename )
00106         {
00107                 llinfos << "Bad Wearable Asset: missing file." << llendl;
00108         }
00109         else
00110         if( status >= 0 )
00111         {
00112                 // read the file
00113                 LLFILE* fp = LLFile::fopen(filename, "rb");             /*Flawfinder: ignore*/
00114                 if( !fp )
00115                 {
00116                         llinfos << "Bad Wearable Asset: unable to open file: '" << filename << "'" << llendl;
00117                 }
00118                 else
00119                 {
00120                         LLWearable *wearable = new LLWearable(uuid);
00121                         if( wearable->importFile( fp ) )
00122                         {
00123 //                              llinfos << "processGetAssetReply()" << llendl;
00124 //                              wearable->dump();
00125 
00126                                 gWearableList.mList[ uuid ] = wearable;
00127                                 if( data->mCallback )
00128                                 {
00129                                         data->mCallback( wearable, data->mUserdata );
00130                                 }
00131                                 success = TRUE;
00132                         }
00133                         else
00134                         {
00135                                 LLString::format_map_t args;
00136                                 // *TODO:translate
00137                                 args["[TYPE]"] = LLAssetType::lookupHumanReadable(data->mAssetType);
00138                                 if (data->mName.empty())
00139                                 {
00140                                         LLNotifyBox::showXml("FailedToLoadWearableUnnamed", args);
00141                                 }
00142                                 else
00143                                 {
00144                                         args["[DESC]"] = data->mName;
00145                                         LLNotifyBox::showXml("FailedToLoadWearable", args);
00146                                 }
00147                                 delete wearable;
00148                         }
00149 
00150                         fclose( fp );
00151                         if(filename)
00152                         {
00153                                 LLFile::remove(filename);
00154                         }
00155                 }
00156         }
00157         else
00158         {
00159                 if(filename)
00160                 {
00161                         LLFile::remove(filename);
00162                 }
00163                 LLViewerStats::getInstance()->incStat( LLViewerStats::ST_DOWNLOAD_FAILED );
00164 
00165                 llwarns << "Wearable download failed: " << LLAssetStorage::getErrorString( status ) << " " << uuid << llendl;
00166                 switch( status )
00167                 {
00168                   case LL_ERR_ASSET_REQUEST_NOT_IN_DATABASE:
00169                   {
00170                           LLString::format_map_t args;
00171                           // *TODO:translate
00172                           args["[TYPE]"] = LLAssetType::lookupHumanReadable(data->mAssetType);
00173                           if (data->mName.empty())
00174                           {
00175                                   LLNotifyBox::showXml("FailedToFindWearableUnnamed", args);
00176                           }
00177                           else
00178                           {
00179                                   args["[DESC]"] = data->mName;
00180                                   LLNotifyBox::showXml("FailedToFindWearable", args);
00181                           }
00182 
00183                           // Asset does not exist in the database.
00184                           // Can't load asset, so return NULL
00185                           if( data->mCallback )
00186                           {
00187                                   data->mCallback( NULL, data->mUserdata );
00188                           }
00189                           break;
00190                   }
00191                   default:
00192                   {
00193                           // Try again
00194                           gAssetStorage->getAssetData(uuid,
00195                                                                                   data->mAssetType,
00196                                                                                   LLWearableList::processGetAssetReply,
00197                                                                                   userdata);  // re-use instead of deleting.
00198                           return;
00199                   }
00200                 }
00201         }
00202 
00203         delete data;
00204 }
00205 
00206 
00207 // Creates a new wearable just like the old_wearable but with data copied over from item
00208 LLWearable* LLWearableList::createWearableMatchedToInventoryItem( LLWearable* old_wearable, LLViewerInventoryItem* item )
00209 {
00210         lldebugs << "LLWearableList::createWearableMatchedToInventoryItem()" << llendl;
00211 
00212         LLTransactionID tid;
00213         LLAssetID new_asset_id;
00214         new_asset_id = tid.makeAssetID(gAgent.getSecureSessionID());
00215 
00216         LLWearable* wearable = new LLWearable( tid );
00217         wearable->copyDataFrom( old_wearable );
00218 
00219         wearable->setName( item->getName() );
00220         wearable->setDescription( item->getDescription() );
00221         wearable->setPermissions( item->getPermissions() );
00222         wearable->setSaleInfo( item->getSaleInfo() );
00223 
00224         mList[ new_asset_id ] = wearable;
00225 
00226         // Send to the dataserver
00227         wearable->saveNewAsset();
00228 
00229         return wearable;
00230 }
00231 
00232 LLWearable* LLWearableList::createCopyFromAvatar( LLWearable* old_wearable, const std::string& new_name )
00233 {
00234         lldebugs << "LLWearableList::createCopyFromAvatar()" << llendl;
00235 
00236         LLTransactionID tid;
00237         LLAssetID new_asset_id;
00238         tid.generate();
00239         new_asset_id = tid.makeAssetID(gAgent.getSecureSessionID());
00240         
00241         LLWearable* wearable = new LLWearable( tid );
00242         wearable->copyDataFrom( old_wearable );
00243         LLPermissions perm(old_wearable->getPermissions());
00244         perm.setOwnerAndGroup(LLUUID::null, gAgent.getID(), LLUUID::null, true);
00245         wearable->setPermissions(perm);
00246         wearable->readFromAvatar();  // update from the avatar
00247    
00248         if (!new_name.empty()) wearable->setName(new_name);
00249 
00250         mList[ new_asset_id ] = wearable;
00251 
00252         // Send to the dataserver
00253         wearable->saveNewAsset();
00254 
00255         return wearable;
00256 }
00257 
00258 
00259 LLWearable* LLWearableList::createCopy( LLWearable* old_wearable )
00260 {
00261         lldebugs << "LLWearableList::createCopy()" << llendl;
00262 
00263         LLTransactionID tid;
00264         LLAssetID new_asset_id;
00265         tid.generate();
00266         new_asset_id = tid.makeAssetID(gAgent.getSecureSessionID());
00267         
00268         LLWearable* wearable = new LLWearable( tid );
00269         wearable->copyDataFrom( old_wearable );
00270         LLPermissions perm(old_wearable->getPermissions());
00271         perm.setOwnerAndGroup(LLUUID::null, gAgent.getID(), LLUUID::null, true);
00272         wearable->setPermissions(perm);
00273         mList[ new_asset_id ] = wearable;
00274 
00275         // Send to the dataserver
00276         wearable->saveNewAsset();
00277 
00278         return wearable;
00279 }
00280 
00281 LLWearable* LLWearableList::createNewWearable( EWearableType type )
00282 {
00283         lldebugs << "LLWearableList::createNewWearable()" << llendl;
00284 
00285         LLTransactionID tid;
00286         LLAssetID new_asset_id;
00287         tid.generate();
00288         new_asset_id = tid.makeAssetID(gAgent.getSecureSessionID());
00289         
00290         LLWearable* wearable = new LLWearable( tid );
00291         wearable->setType( type );
00292         
00293         LLString name = "New ";
00294         name.append( wearable->getTypeLabel() );
00295         wearable->setName( name );
00296 
00297         LLPermissions perm;
00298         perm.init(gAgent.getID(), gAgent.getID(), LLUUID::null, LLUUID::null);
00299         perm.initMasks(PERM_ALL, PERM_ALL, PERM_NONE, PERM_NONE, PERM_MOVE | PERM_TRANSFER);
00300         wearable->setPermissions(perm);
00301 
00302         // Description and sale info have default values.
00303         
00304         wearable->setParamsToDefaults();
00305         wearable->setTexturesToDefaults();
00306 
00307         mList[ new_asset_id ] = wearable;
00308 
00309         // Send to the dataserver
00310         wearable->saveNewAsset();
00311 
00312         return wearable;
00313 }

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