PageRenderTime 803ms CodeModel.GetById 201ms app.highlight 370ms RepoModel.GetById 224ms app.codeStats 0ms

/indra/newview/llinventoryobserver.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 822 lines | 638 code | 96 blank | 88 comment | 128 complexity | db2212dedc240161e24b5021c0dfbfcd MD5 | raw file
  1/** 
  2 * @file llinventoryobserver.cpp
  3 * @brief Implementation of the inventory observers used to track agent inventory.
  4 *
  5 * $LicenseInfo:firstyear=2002&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
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llinventoryobserver.h"
 30
 31#include "llassetstorage.h"
 32#include "llcrc.h"
 33#include "lldir.h"
 34#include "llsys.h"
 35#include "llxfermanager.h"
 36#include "message.h"
 37
 38#include "llagent.h"
 39#include "llagentwearables.h"
 40#include "llfloater.h"
 41#include "llfocusmgr.h"
 42#include "llinventorybridge.h"
 43#include "llinventoryfunctions.h"
 44#include "llinventorymodel.h"
 45#include "llviewermessage.h"
 46#include "llviewerwindow.h"
 47#include "llviewerregion.h"
 48#include "llappviewer.h"
 49#include "lldbstrings.h"
 50#include "llviewerstats.h"
 51#include "llnotificationsutil.h"
 52#include "llcallbacklist.h"
 53#include "llpreview.h"
 54#include "llviewercontrol.h"
 55#include "llvoavatarself.h"
 56#include "llsdutil.h"
 57#include <deque>
 58
 59const F32 LLInventoryFetchItemsObserver::FETCH_TIMER_EXPIRY = 60.0f;
 60
 61
 62LLInventoryObserver::LLInventoryObserver()
 63{
 64}
 65
 66// virtual
 67LLInventoryObserver::~LLInventoryObserver()
 68{
 69}
 70
 71LLInventoryFetchObserver::LLInventoryFetchObserver(const LLUUID& id)
 72{
 73	mIDs.clear();
 74	if (id != LLUUID::null)
 75	{
 76		setFetchID(id);
 77	}
 78}
 79
 80LLInventoryFetchObserver::LLInventoryFetchObserver(const uuid_vec_t& ids)
 81{
 82	setFetchIDs(ids);
 83}
 84
 85BOOL LLInventoryFetchObserver::isFinished() const
 86{
 87	return mIncomplete.empty();
 88}
 89
 90void LLInventoryFetchObserver::setFetchIDs(const uuid_vec_t& ids)
 91{
 92	mIDs = ids;
 93}
 94void LLInventoryFetchObserver::setFetchID(const LLUUID& id)
 95{
 96	mIDs.clear();
 97	mIDs.push_back(id);
 98}
 99
100
101void LLInventoryCompletionObserver::changed(U32 mask)
102{
103	// scan through the incomplete items and move or erase them as
104	// appropriate.
105	if (!mIncomplete.empty())
106	{
107		for (uuid_vec_t::iterator it = mIncomplete.begin(); it < mIncomplete.end(); )
108		{
109			const LLViewerInventoryItem* item = gInventory.getItem(*it);
110			if (!item)
111			{
112				it = mIncomplete.erase(it);
113				continue;
114			}
115			if (item->isFinished())
116			{
117				mComplete.push_back(*it);
118				it = mIncomplete.erase(it);
119				continue;
120			}
121			++it;
122		}
123		if (mIncomplete.empty())
124		{
125			done();
126		}
127	}
128}
129
130void LLInventoryCompletionObserver::watchItem(const LLUUID& id)
131{
132	if (id.notNull())
133	{
134		mIncomplete.push_back(id);
135	}
136}
137
138LLInventoryFetchItemsObserver::LLInventoryFetchItemsObserver(const LLUUID& item_id) :
139	LLInventoryFetchObserver(item_id)
140{
141	mIDs.clear();
142	mIDs.push_back(item_id);
143}
144
145LLInventoryFetchItemsObserver::LLInventoryFetchItemsObserver(const uuid_vec_t& item_ids) :
146	LLInventoryFetchObserver(item_ids)
147{
148}
149
150void LLInventoryFetchItemsObserver::changed(U32 mask)
151{
152	lldebugs << this << " remaining incomplete " << mIncomplete.size()
153			 << " complete " << mComplete.size()
154			 << " wait period " << mFetchingPeriod.getRemainingTimeF32()
155			 << llendl;
156
157	// scan through the incomplete items and move or erase them as
158	// appropriate.
159	if (!mIncomplete.empty())
160	{
161
162		// Have we exceeded max wait time?
163		bool timeout_expired = mFetchingPeriod.hasExpired();
164
165		for (uuid_vec_t::iterator it = mIncomplete.begin(); it < mIncomplete.end(); )
166		{
167			const LLUUID& item_id = (*it);
168			LLViewerInventoryItem* item = gInventory.getItem(item_id);
169			if (item && item->isFinished())
170			{
171				mComplete.push_back(item_id);
172				it = mIncomplete.erase(it);
173			}
174			else
175			{
176				if (timeout_expired)
177				{
178					// Just concede that this item hasn't arrived in reasonable time and continue on.
179					llwarns << "Fetcher timed out when fetching inventory item UUID: " << item_id << LL_ENDL;
180					it = mIncomplete.erase(it);
181				}
182				else
183				{
184					// Keep trying.
185					++it;
186				}
187			}
188		}
189
190	}
191
192	if (mIncomplete.empty())
193	{
194		lldebugs << this << " done at remaining incomplete "
195				 << mIncomplete.size() << " complete " << mComplete.size() << llendl;
196		done();
197	}
198	//llinfos << "LLInventoryFetchItemsObserver::changed() mComplete size " << mComplete.size() << llendl;
199	//llinfos << "LLInventoryFetchItemsObserver::changed() mIncomplete size " << mIncomplete.size() << llendl;
200}
201
202void fetch_items_from_llsd(const LLSD& items_llsd)
203{
204	if (!items_llsd.size() || gDisconnected) return;
205
206	LLSD body;
207	body[0]["cap_name"] = "FetchInventory2";
208	body[1]["cap_name"] = "FetchLib2";
209	for (S32 i=0; i<items_llsd.size();i++)
210	{
211		if (items_llsd[i]["owner_id"].asString() == gAgent.getID().asString())
212		{
213			body[0]["items"].append(items_llsd[i]);
214			continue;
215		}
216		else if (items_llsd[i]["owner_id"].asString() == ALEXANDRIA_LINDEN_ID.asString())
217		{
218			body[1]["items"].append(items_llsd[i]);
219			continue;
220		}
221	}
222		
223	for (S32 i=0; i<body.size(); i++)
224	{
225		if (!gAgent.getRegion())
226		{
227			llwarns << "Agent's region is null" << llendl;
228			break;
229		}
230
231		if (0 == body[i]["items"].size()) {
232			lldebugs << "Skipping body with no items to fetch" << llendl;
233			continue;
234		}
235
236		std::string url = gAgent.getRegion()->getCapability(body[i]["cap_name"].asString());
237		if (!url.empty())
238		{
239			body[i]["agent_id"]	= gAgent.getID();
240			LLHTTPClient::post(url, body[i], new LLInventoryModel::fetchInventoryResponder(body[i]));
241			continue;
242		}
243
244		LLMessageSystem* msg = gMessageSystem;
245		BOOL start_new_message = TRUE;
246		for (S32 j=0; j<body[i]["items"].size(); j++)
247		{
248			LLSD item_entry = body[i]["items"][j];
249			if (start_new_message)
250			{
251				start_new_message = FALSE;
252				msg->newMessageFast(_PREHASH_FetchInventory);
253				msg->nextBlockFast(_PREHASH_AgentData);
254				msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
255				msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
256			}
257			msg->nextBlockFast(_PREHASH_InventoryData);
258			msg->addUUIDFast(_PREHASH_OwnerID, item_entry["owner_id"].asUUID());
259			msg->addUUIDFast(_PREHASH_ItemID, item_entry["item_id"].asUUID());
260			if (msg->isSendFull(NULL))
261			{
262				start_new_message = TRUE;
263				gAgent.sendReliableMessage();
264			}
265		}
266		if (!start_new_message)
267		{
268			gAgent.sendReliableMessage();
269		}
270	}
271}
272
273void LLInventoryFetchItemsObserver::startFetch()
274{
275	LLUUID owner_id;
276	LLSD items_llsd;
277	for (uuid_vec_t::const_iterator it = mIDs.begin(); it < mIDs.end(); ++it)
278	{
279		LLViewerInventoryItem* item = gInventory.getItem(*it);
280		if (item)
281		{
282			if (item->isFinished())
283			{
284				// It's complete, so put it on the complete container.
285				mComplete.push_back(*it);
286				continue;
287			}
288			else
289			{
290				owner_id = item->getPermissions().getOwner();
291			}
292		}
293		else
294		{
295			// assume it's agent inventory.
296			owner_id = gAgent.getID();
297		}
298
299		// Ignore categories since they're not items.  We
300		// could also just add this to mComplete but not sure what the
301		// side-effects would be, so ignoring to be safe.
302		LLViewerInventoryCategory* cat = gInventory.getCategory(*it);
303		if (cat)
304		{
305			continue;
306		}
307
308		// It's incomplete, so put it on the incomplete container, and
309		// pack this on the message.
310		mIncomplete.push_back(*it);
311
312		// Prepare the data to fetch
313		LLSD item_entry;
314		item_entry["owner_id"] = owner_id;
315		item_entry["item_id"] = (*it);
316		items_llsd.append(item_entry);
317	}
318
319	mFetchingPeriod.reset();
320	mFetchingPeriod.setTimerExpirySec(FETCH_TIMER_EXPIRY);
321
322	fetch_items_from_llsd(items_llsd);
323}
324
325LLInventoryFetchDescendentsObserver::LLInventoryFetchDescendentsObserver(const LLUUID& cat_id) :
326	LLInventoryFetchObserver(cat_id)
327{
328}
329
330LLInventoryFetchDescendentsObserver::LLInventoryFetchDescendentsObserver(const uuid_vec_t& cat_ids) :
331	LLInventoryFetchObserver(cat_ids)
332{
333}
334
335// virtual
336void LLInventoryFetchDescendentsObserver::changed(U32 mask)
337{
338	for (uuid_vec_t::iterator it = mIncomplete.begin(); it < mIncomplete.end();)
339	{
340		const LLViewerInventoryCategory* cat = gInventory.getCategory(*it);
341		if (!cat)
342		{
343			it = mIncomplete.erase(it);
344			continue;
345		}
346		if (isCategoryComplete(cat))
347		{
348			mComplete.push_back(*it);
349			it = mIncomplete.erase(it);
350			continue;
351		}
352		++it;
353	}
354	if (mIncomplete.empty())
355	{
356		done();
357	}
358}
359
360void LLInventoryFetchDescendentsObserver::startFetch()
361{
362	for (uuid_vec_t::const_iterator it = mIDs.begin(); it != mIDs.end(); ++it)
363	{
364		LLViewerInventoryCategory* cat = gInventory.getCategory(*it);
365		if (!cat) continue;
366		if (!isCategoryComplete(cat))
367		{
368			cat->fetch();		//blindly fetch it without seeing if anything else is fetching it.
369			mIncomplete.push_back(*it);	//Add to list of things being downloaded for this observer.
370		}
371		else
372		{
373			mComplete.push_back(*it);
374		}
375	}
376}
377
378BOOL LLInventoryFetchDescendentsObserver::isCategoryComplete(const LLViewerInventoryCategory* cat) const
379{
380	const S32 version = cat->getVersion();
381	const S32 expected_num_descendents = cat->getDescendentCount();
382	if ((version == LLViewerInventoryCategory::VERSION_UNKNOWN) ||
383		(expected_num_descendents == LLViewerInventoryCategory::DESCENDENT_COUNT_UNKNOWN))
384	{
385		return FALSE;
386	}
387	// it might be complete - check known descendents against
388	// currently available.
389	LLInventoryModel::cat_array_t* cats;
390	LLInventoryModel::item_array_t* items;
391	gInventory.getDirectDescendentsOf(cat->getUUID(), cats, items);
392	if (!cats || !items)
393	{
394		llwarns << "Category '" << cat->getName() << "' descendents corrupted, fetch failed." << llendl;
395		// NULL means the call failed -- cats/items map doesn't exist (note: this does NOT mean
396		// that the cat just doesn't have any items or subfolders).
397		// Unrecoverable, so just return done so that this observer can be cleared
398		// from memory.
399		return TRUE;
400	}
401	const S32 current_num_known_descendents = cats->count() + items->count();
402	
403	// Got the number of descendents that we were expecting, so we're done.
404	if (current_num_known_descendents == expected_num_descendents)
405	{
406		return TRUE;
407	}
408
409	// Error condition, but recoverable.  This happens if something was added to the
410	// category before it was initialized, so accountForUpdate didn't update descendent
411	// count and thus the category thinks it has fewer descendents than it actually has.
412	if (current_num_known_descendents >= expected_num_descendents)
413	{
414		llwarns << "Category '" << cat->getName() << "' expected descendentcount:" << expected_num_descendents << " descendents but got descendentcount:" << current_num_known_descendents << llendl;
415		const_cast<LLViewerInventoryCategory *>(cat)->setDescendentCount(current_num_known_descendents);
416		return TRUE;
417	}
418	return FALSE;
419}
420
421LLInventoryFetchComboObserver::LLInventoryFetchComboObserver(const uuid_vec_t& folder_ids,
422															 const uuid_vec_t& item_ids)
423{
424	mFetchDescendents = new LLInventoryFetchDescendentsObserver(folder_ids);
425
426	uuid_vec_t pruned_item_ids;
427	for (uuid_vec_t::const_iterator item_iter = item_ids.begin();
428		 item_iter != item_ids.end();
429		 ++item_iter)
430	{
431		const LLUUID& item_id = (*item_iter);
432		const LLViewerInventoryItem* item = gInventory.getItem(item_id);
433		if (item && std::find(folder_ids.begin(), folder_ids.end(), item->getParentUUID()) == folder_ids.end())
434		{
435			continue;
436		}
437		pruned_item_ids.push_back(item_id);
438	}
439
440	mFetchItems = new LLInventoryFetchItemsObserver(pruned_item_ids);
441	mFetchDescendents = new LLInventoryFetchDescendentsObserver(folder_ids);
442}
443
444LLInventoryFetchComboObserver::~LLInventoryFetchComboObserver()
445{
446	mFetchItems->done();
447	mFetchDescendents->done();
448	delete mFetchItems;
449	delete mFetchDescendents;
450}
451
452void LLInventoryFetchComboObserver::changed(U32 mask)
453{
454	mFetchItems->changed(mask);
455	mFetchDescendents->changed(mask);
456	if (mFetchItems->isFinished() && mFetchDescendents->isFinished())
457	{
458		done();
459	}
460}
461
462void LLInventoryFetchComboObserver::startFetch()
463{
464	mFetchItems->startFetch();
465	mFetchDescendents->startFetch();
466}
467
468void LLInventoryExistenceObserver::watchItem(const LLUUID& id)
469{
470	if (id.notNull())
471	{
472		mMIA.push_back(id);
473	}
474}
475
476void LLInventoryExistenceObserver::changed(U32 mask)
477{
478	// scan through the incomplete items and move or erase them as
479	// appropriate.
480	if (!mMIA.empty())
481	{
482		for (uuid_vec_t::iterator it = mMIA.begin(); it < mMIA.end(); )
483		{
484			LLViewerInventoryItem* item = gInventory.getItem(*it);
485			if (!item)
486			{
487				++it;
488				continue;
489			}
490			mExist.push_back(*it);
491			it = mMIA.erase(it);
492		}
493		if (mMIA.empty())
494		{
495			done();
496		}
497	}
498}
499
500void LLInventoryAddItemByAssetObserver::changed(U32 mask)
501{
502	if(!(mask & LLInventoryObserver::ADD))
503	{
504		return;
505	}
506
507	// nothing is watched
508	if (mWatchedAssets.size() == 0)
509	{
510		return;
511	}
512
513	LLMessageSystem* msg = gMessageSystem;
514	if (!(msg->getMessageName() && (0 == strcmp(msg->getMessageName(), "UpdateCreateInventoryItem"))))
515	{
516		// this is not our message
517		return; // to prevent a crash. EXT-7921;
518	}
519
520	LLPointer<LLViewerInventoryItem> item = new LLViewerInventoryItem;
521	S32 num_blocks = msg->getNumberOfBlocksFast(_PREHASH_InventoryData);
522	for(S32 i = 0; i < num_blocks; ++i)
523	{
524		item->unpackMessage(msg, _PREHASH_InventoryData, i);
525		const LLUUID& asset_uuid = item->getAssetUUID();
526		if (item->getUUID().notNull() && asset_uuid.notNull())
527		{
528			if (isAssetWatched(asset_uuid))
529			{
530				LL_DEBUGS("Inventory_Move") << "Found asset UUID: " << asset_uuid << LL_ENDL;
531				mAddedItems.push_back(item->getUUID());
532			}
533		}
534	}
535
536	if (mAddedItems.size() == mWatchedAssets.size())
537	{
538		done();
539		LL_DEBUGS("Inventory_Move") << "All watched items are added & processed." << LL_ENDL;
540		mAddedItems.clear();
541
542		// Unable to clean watched items here due to somebody can require to check them in current frame.
543		// set dirty state to clean them while next watch cycle.
544		mIsDirty = true;
545	}
546}
547
548void LLInventoryAddItemByAssetObserver::watchAsset(const LLUUID& asset_id)
549{
550	if(asset_id.notNull())
551	{
552		if (mIsDirty)
553		{
554			LL_DEBUGS("Inventory_Move") << "Watched items are dirty. Clean them." << LL_ENDL;
555			mWatchedAssets.clear();
556			mIsDirty = false;
557		}
558
559		mWatchedAssets.push_back(asset_id);
560		onAssetAdded(asset_id);
561	}
562}
563
564bool LLInventoryAddItemByAssetObserver::isAssetWatched( const LLUUID& asset_id )
565{
566	return std::find(mWatchedAssets.begin(), mWatchedAssets.end(), asset_id) != mWatchedAssets.end();
567}
568
569void LLInventoryAddedObserver::changed(U32 mask)
570{
571	if (!(mask & LLInventoryObserver::ADD))
572	{
573		return;
574	}
575
576	// *HACK: If this was in response to a packet off
577	// the network, figure out which item was updated.
578	LLMessageSystem* msg = gMessageSystem;
579
580	std::string msg_name = msg->getMessageName();
581	if (msg_name.empty())
582	{
583		return;
584	}
585	
586	// We only want newly created inventory items. JC
587	if ( msg_name != "UpdateCreateInventoryItem")
588	{
589		return;
590	}
591
592	LLPointer<LLViewerInventoryItem> titem = new LLViewerInventoryItem;
593	S32 num_blocks = msg->getNumberOfBlocksFast(_PREHASH_InventoryData);
594	for (S32 i = 0; i < num_blocks; ++i)
595	{
596		titem->unpackMessage(msg, _PREHASH_InventoryData, i);
597		if (!(titem->getUUID().isNull()))
598		{
599			//we don't do anything with null keys
600			mAdded.push_back(titem->getUUID());
601		}
602	}
603	if (!mAdded.empty())
604	{
605		done();
606	}
607}
608
609void LLInventoryCategoryAddedObserver::changed(U32 mask)
610{
611	if (!(mask & LLInventoryObserver::ADD))
612	{
613		return;
614	}
615	
616	const LLInventoryModel::changed_items_t& changed_ids = gInventory.getChangedIDs();
617	
618	for (LLInventoryModel::changed_items_t::const_iterator cit = changed_ids.begin(); cit != changed_ids.end(); ++cit)
619	{
620		LLViewerInventoryCategory* cat = gInventory.getCategory(*cit);
621		
622		if (cat)
623		{
624			mAddedCategories.push_back(cat);
625		}
626	}
627	
628	if (!mAddedCategories.empty())
629	{
630		done();
631		
632		mAddedCategories.clear();
633	}
634}
635
636
637LLInventoryTransactionObserver::LLInventoryTransactionObserver(const LLTransactionID& transaction_id) :
638	mTransactionID(transaction_id)
639{
640}
641
642void LLInventoryTransactionObserver::changed(U32 mask)
643{
644	if (mask & LLInventoryObserver::ADD)
645	{
646		// This could be it - see if we are processing a bulk update
647		LLMessageSystem* msg = gMessageSystem;
648		if (msg->getMessageName()
649		   && (0 == strcmp(msg->getMessageName(), "BulkUpdateInventory")))
650		{
651			// we have a match for the message - now check the
652			// transaction id.
653			LLUUID id;
654			msg->getUUIDFast(_PREHASH_AgentData, _PREHASH_TransactionID, id);
655			if (id == mTransactionID)
656			{
657				// woo hoo, we found it
658				uuid_vec_t folders;
659				uuid_vec_t items;
660				S32 count;
661				count = msg->getNumberOfBlocksFast(_PREHASH_FolderData);
662				S32 i;
663				for (i = 0; i < count; ++i)
664				{
665					msg->getUUIDFast(_PREHASH_FolderData, _PREHASH_FolderID, id, i);
666					if (id.notNull())
667					{
668						folders.push_back(id);
669					}
670				}
671				count = msg->getNumberOfBlocksFast(_PREHASH_ItemData);
672				for (i = 0; i < count; ++i)
673				{
674					msg->getUUIDFast(_PREHASH_ItemData, _PREHASH_ItemID, id, i);
675					if (id.notNull())
676					{
677						items.push_back(id);
678					}
679				}
680
681				// call the derived class the implements this method.
682				done(folders, items);
683			}
684		}
685	}
686}
687
688void LLInventoryCategoriesObserver::changed(U32 mask)
689{
690	if (!mCategoryMap.size())
691		return;
692
693	for (category_map_t::iterator iter = mCategoryMap.begin();
694		 iter != mCategoryMap.end();
695		 ++iter)
696	{
697		const LLUUID& cat_id = (*iter).first;
698
699		LLViewerInventoryCategory* category = gInventory.getCategory(cat_id);
700		if (!category)
701			continue;
702
703		const S32 version = category->getVersion();
704		const S32 expected_num_descendents = category->getDescendentCount();
705		if ((version == LLViewerInventoryCategory::VERSION_UNKNOWN) ||
706			(expected_num_descendents == LLViewerInventoryCategory::DESCENDENT_COUNT_UNKNOWN))
707		{
708			continue;
709		}
710
711		// Check number of known descendents to find out whether it has changed.
712		LLInventoryModel::cat_array_t* cats;
713		LLInventoryModel::item_array_t* items;
714		gInventory.getDirectDescendentsOf(cat_id, cats, items);
715		if (!cats || !items)
716		{
717			llwarns << "Category '" << category->getName() << "' descendents corrupted, fetch failed." << llendl;
718			// NULL means the call failed -- cats/items map doesn't exist (note: this does NOT mean
719			// that the cat just doesn't have any items or subfolders).
720			// Unrecoverable, so just skip this category.
721
722			llassert(cats != NULL && items != NULL);
723
724			continue;
725		}
726		
727		const S32 current_num_known_descendents = cats->count() + items->count();
728
729		LLCategoryData& cat_data = (*iter).second;
730
731		bool cat_changed = false;
732
733		// If category version or descendents count has changed
734		// update category data in mCategoryMap
735		if (version != cat_data.mVersion || current_num_known_descendents != cat_data.mDescendentsCount)
736		{
737			cat_data.mVersion = version;
738			cat_data.mDescendentsCount = current_num_known_descendents;
739			cat_changed = true;
740		}
741
742		// If any item names have changed, update the name hash 
743		// Only need to check if (a) name hash has not previously been
744		// computed, or (b) a name has changed.
745		if (!cat_data.mIsNameHashInitialized || (mask & LLInventoryObserver::LABEL))
746		{
747			LLMD5 item_name_hash = gInventory.hashDirectDescendentNames(cat_id);
748			if (cat_data.mItemNameHash != item_name_hash)
749			{
750				cat_data.mIsNameHashInitialized = true;
751				cat_data.mItemNameHash = item_name_hash;
752				cat_changed = true;
753			}
754		}
755
756		// If anything has changed above, fire the callback.
757		if (cat_changed)
758			cat_data.mCallback();
759	}
760}
761
762bool LLInventoryCategoriesObserver::addCategory(const LLUUID& cat_id, callback_t cb)
763{
764	S32 version = LLViewerInventoryCategory::VERSION_UNKNOWN;
765	S32 current_num_known_descendents = LLViewerInventoryCategory::DESCENDENT_COUNT_UNKNOWN;
766	bool can_be_added = true;
767
768	LLViewerInventoryCategory* category = gInventory.getCategory(cat_id);
769	// If category could not be retrieved it might mean that
770	// inventory is unusable at the moment so the category is
771	// stored with VERSION_UNKNOWN and DESCENDENT_COUNT_UNKNOWN,
772	// it may be updated later.
773	if (category)
774	{
775		// Inventory category version is used to find out if some changes
776		// to a category have been made.
777		version = category->getVersion();
778
779		LLInventoryModel::cat_array_t* cats;
780		LLInventoryModel::item_array_t* items;
781		gInventory.getDirectDescendentsOf(cat_id, cats, items);
782		if (!cats || !items)
783		{
784			llwarns << "Category '" << category->getName() << "' descendents corrupted, fetch failed." << llendl;
785			// NULL means the call failed -- cats/items map doesn't exist (note: this does NOT mean
786			// that the cat just doesn't have any items or subfolders).
787			// Unrecoverable, so just return "false" meaning that the category can't be observed.
788			can_be_added = false;
789
790			llassert(cats != NULL && items != NULL);
791		}
792		else
793		{
794			current_num_known_descendents = cats->count() + items->count();
795		}
796	}
797
798	if (can_be_added)
799	{
800		mCategoryMap.insert(category_map_value_t(
801								cat_id,LLCategoryData(cat_id, cb, version, current_num_known_descendents)));
802	}
803
804	return can_be_added;
805}
806
807void LLInventoryCategoriesObserver::removeCategory(const LLUUID& cat_id)
808{
809	mCategoryMap.erase(cat_id);
810}
811
812LLInventoryCategoriesObserver::LLCategoryData::LLCategoryData(
813	const LLUUID& cat_id, callback_t cb, S32 version, S32 num_descendents)
814	
815	: mCatID(cat_id)
816	, mCallback(cb)
817	, mVersion(version)
818	, mDescendentsCount(num_descendents)
819	, mIsNameHashInitialized(false)
820{
821	mItemNameHash.finalize();
822}