PageRenderTime 88ms CodeModel.GetById 25ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/newview/llfriendcard.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 609 lines | 429 code | 99 blank | 81 comment | 69 complexity | 800d0196b8c760fff38c4b44f3524f6c MD5 | raw file
  1/** 
  2 * @file llfriendcard.cpp
  3 * @brief Implementation of classes to process Friends Cards
  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 "llfriendcard.h"
 30
 31#include "llagent.h"
 32#include "llavatarnamecache.h"
 33#include "llinventory.h"
 34#include "llinventoryfunctions.h"
 35#include "llinventoryobserver.h"
 36#include "lltrans.h"
 37
 38#include "llcallingcard.h" // for LLAvatarTracker
 39#include "llviewerinventory.h"
 40#include "llinventorymodel.h"
 41
 42// Constants;
 43
 44static const std::string INVENTORY_STRING_FRIENDS_SUBFOLDER = "Friends";
 45static const std::string INVENTORY_STRING_FRIENDS_ALL_SUBFOLDER = "All";
 46
 47// helper functions
 48
 49// NOTE: For now Friends & All folders are created as protected folders of the LLFolderType::FT_CALLINGCARD type.
 50// So, their names will be processed in the LLFolderViewItem::refreshFromListener() to be localized
 51// using "InvFolder LABEL_NAME" as LLTrans::findString argument.
 52
 53// We must use in this file their hard-coded names to ensure found them on different locales. EXT-5829.
 54// These hard-coded names will be stored in InventoryItems but shown localized in FolderViewItems
 55
 56// If hack in the LLFolderViewItem::refreshFromListener() to localize protected folder is removed
 57// or these folders are not protected these names should be localized in another place/way.
 58inline const std::string get_friend_folder_name()
 59{
 60	return INVENTORY_STRING_FRIENDS_SUBFOLDER;
 61}
 62
 63inline const std::string get_friend_all_subfolder_name()
 64{
 65	return INVENTORY_STRING_FRIENDS_ALL_SUBFOLDER;
 66}
 67
 68void move_from_to_arrays(LLInventoryModel::cat_array_t& from, LLInventoryModel::cat_array_t& to)
 69{
 70	while (from.count() > 0)
 71	{
 72		to.put(from.get(0));
 73		from.remove(0);
 74	}
 75}
 76
 77const LLUUID& get_folder_uuid(const LLUUID& parentFolderUUID, LLInventoryCollectFunctor& matchFunctor)
 78{
 79	LLInventoryModel::cat_array_t cats;
 80	LLInventoryModel::item_array_t items;
 81
 82	gInventory.collectDescendentsIf(parentFolderUUID, cats, items,
 83		LLInventoryModel::EXCLUDE_TRASH, matchFunctor);
 84
 85	S32 cats_count = cats.count();
 86
 87	if (cats_count > 1)
 88	{
 89		LL_WARNS("LLFriendCardsManager")
 90			<< "There is more than one Friend card folder."
 91			<< "The first folder will be used."
 92			<< LL_ENDL;
 93	}
 94
 95	return (cats_count >= 1) ? cats.get(0)->getUUID() : LLUUID::null;
 96}
 97
 98/**
 99 * Class LLFindAgentCallingCard
100 *
101 * An inventory collector functor for checking that agent's own calling card
102 * exists within the Calling Cards category and its sub-folders.
103 */
104class LLFindAgentCallingCard : public LLInventoryCollectFunctor
105{
106public:
107	LLFindAgentCallingCard() : mIsAgentCallingCardFound(false) {}
108	virtual ~LLFindAgentCallingCard() {}
109	virtual bool operator()(LLInventoryCategory* cat, LLInventoryItem* item);
110	bool isAgentCallingCardFound() { return mIsAgentCallingCardFound; }
111
112private:
113	bool mIsAgentCallingCardFound;
114};
115
116bool LLFindAgentCallingCard::operator()(LLInventoryCategory* cat, LLInventoryItem* item)
117{
118	if (mIsAgentCallingCardFound) return true;
119
120	if (item && item->getType() == LLAssetType::AT_CALLINGCARD && item->getCreatorUUID() == gAgentID)
121	{
122		mIsAgentCallingCardFound = true;
123	}
124
125	return mIsAgentCallingCardFound;
126}
127
128/**
129 * Class for fetching initial friend cards data
130 *
131 * Implemented to fix an issue when Inventory folders are in incomplete state.
132 * See EXT-2320, EXT-2061, EXT-1935, EXT-813.
133 * Uses a callback to sync Inventory Friends/All folder with agent's Friends List.
134 */
135class LLInitialFriendCardsFetch : public LLInventoryFetchDescendentsObserver
136{
137public:
138	typedef boost::function<void()> callback_t;
139
140	LLInitialFriendCardsFetch(const LLUUID& folder_id,
141							  callback_t cb) :
142		LLInventoryFetchDescendentsObserver(folder_id),
143		mCheckFolderCallback(cb)	
144	{}
145
146	/* virtual */ void done();
147
148private:
149	callback_t		mCheckFolderCallback;
150};
151
152void LLInitialFriendCardsFetch::done()
153{
154	// This observer is no longer needed.
155	gInventory.removeObserver(this);
156
157	mCheckFolderCallback();
158
159	delete this;
160}
161
162// LLFriendCardsManager Constructor / Destructor
163LLFriendCardsManager::LLFriendCardsManager()
164{
165	LLAvatarTracker::instance().addObserver(this);
166}
167
168LLFriendCardsManager::~LLFriendCardsManager()
169{
170	LLAvatarTracker::instance().removeObserver(this);
171}
172
173void LLFriendCardsManager::putAvatarData(const LLUUID& avatarID)
174{
175	llinfos << "Store avatar data, avatarID: " << avatarID << llendl;
176	std::pair< avatar_uuid_set_t::iterator, bool > pr;
177	pr = mBuddyIDSet.insert(avatarID);
178	if (pr.second == false)
179	{
180		llwarns << "Trying to add avatar UUID for the stored avatar: " 
181			<< avatarID
182			<< llendl;
183	}
184}
185
186const LLUUID LLFriendCardsManager::extractAvatarID(const LLUUID& avatarID)
187{
188	LLUUID rv;
189	avatar_uuid_set_t::iterator it = mBuddyIDSet.find(avatarID);
190	if (mBuddyIDSet.end() == it)
191	{
192		llwarns << "Call method for non-existent avatar name in the map: " << avatarID << llendl;
193	}
194	else
195	{
196		rv = (*it);
197		mBuddyIDSet.erase(it);
198	}
199	return rv;
200}
201
202bool LLFriendCardsManager::isItemInAnyFriendsList(const LLViewerInventoryItem* item)
203{
204	if (item->getType() != LLAssetType::AT_CALLINGCARD)
205		return false;
206
207	LLInventoryModel::item_array_t items;
208	findMatchedFriendCards(item->getCreatorUUID(), items);
209
210	return items.count() > 0;
211}
212
213
214bool LLFriendCardsManager::isObjDirectDescendentOfCategory(const LLInventoryObject* obj, 
215	const LLViewerInventoryCategory* cat) const
216{
217	// we need both params to proceed.
218	if ( !obj || !cat )
219		return false;
220
221	// Need to check that target category is in the Calling Card/Friends folder. 
222	// In other case function returns unpredictable result.
223	if ( !isCategoryInFriendFolder(cat) )
224		return false;
225
226	bool result = false;
227
228	LLInventoryModel::item_array_t* items;
229	LLInventoryModel::cat_array_t* cats;
230
231	gInventory.lockDirectDescendentArrays(cat->getUUID(), cats, items);
232	if ( items )
233	{
234		if ( obj->getType() == LLAssetType::AT_CALLINGCARD )
235		{
236			// For CALLINGCARD compare items by creator's id, if they are equal assume
237			// that it is same card and return true. Note: UUID's of compared items
238			// may be not equal. Also, we already know that obj should be type of LLInventoryItem,
239			// but in case inventory database is broken check what dynamic_cast returns.
240			const LLInventoryItem* item = dynamic_cast < const LLInventoryItem* > (obj);
241			if ( item )
242			{
243				LLUUID creator_id = item->getCreatorUUID();
244				LLViewerInventoryItem* cur_item = NULL;
245				for ( S32 i = items->count() - 1; i >= 0; --i )
246				{
247					cur_item = items->get(i);
248					if ( creator_id == cur_item->getCreatorUUID() )
249					{
250						result = true;
251						break;
252					}
253				}
254			}
255		}
256		else
257		{
258			// Else check that items have same type and name.
259			// Note: UUID's of compared items also may be not equal.
260			std::string obj_name = obj->getName();
261			LLViewerInventoryItem* cur_item = NULL;
262			for ( S32 i = items->count() - 1; i >= 0; --i )
263			{
264				cur_item = items->get(i);
265				if ( obj->getType() != cur_item->getType() )
266					continue;
267				if ( obj_name == cur_item->getName() )
268				{
269					result = true;
270					break;
271				}
272			}
273		}
274	}
275	if ( !result && cats )
276	{
277		// There is no direct descendent in items, so check categories.
278		// If target obj and descendent category have same type and name
279		// then return true. Note: UUID's of compared items also may be not equal.
280		std::string obj_name = obj->getName();
281		LLViewerInventoryCategory* cur_cat = NULL;
282		for ( S32 i = cats->count() - 1; i >= 0; --i )
283		{
284			cur_cat = cats->get(i);
285			if ( obj->getType() != cur_cat->getType() )
286				continue;
287			if ( obj_name == cur_cat->getName() )
288			{
289				result = true;
290				break;
291			}
292		}
293	}
294	gInventory.unlockDirectDescendentArrays(cat->getUUID());
295
296	return result;
297}
298
299
300bool LLFriendCardsManager::isCategoryInFriendFolder(const LLViewerInventoryCategory* cat) const
301{
302	if (NULL == cat)
303		return false;
304	return TRUE == gInventory.isObjectDescendentOf(cat->getUUID(), findFriendFolderUUIDImpl());
305}
306
307bool LLFriendCardsManager::isAnyFriendCategory(const LLUUID& catID) const
308{
309	const LLUUID& friendFolderID = findFriendFolderUUIDImpl();
310	if (catID == friendFolderID)
311		return true;
312
313	return TRUE == gInventory.isObjectDescendentOf(catID, friendFolderID);
314}
315
316void LLFriendCardsManager::syncFriendCardsFolders()
317{
318	const LLUUID callingCardsFolderID = gInventory.findCategoryUUIDForType(LLFolderType::FT_CALLINGCARD);
319
320	fetchAndCheckFolderDescendents(callingCardsFolderID,
321			boost::bind(&LLFriendCardsManager::ensureFriendsFolderExists, this));
322}
323
324
325/************************************************************************/
326/*		Private Methods                                                 */
327/************************************************************************/
328const LLUUID& LLFriendCardsManager::findFriendFolderUUIDImpl() const
329{
330	const LLUUID callingCardsFolderID = gInventory.findCategoryUUIDForType(LLFolderType::FT_CALLINGCARD);
331
332	std::string friendFolderName = get_friend_folder_name();
333
334	return findChildFolderUUID(callingCardsFolderID, friendFolderName);
335}
336
337const LLUUID& LLFriendCardsManager::findFriendAllSubfolderUUIDImpl() const
338{
339	LLUUID friendFolderUUID = findFriendFolderUUIDImpl();
340
341	std::string friendAllSubfolderName = get_friend_all_subfolder_name();
342
343	return findChildFolderUUID(friendFolderUUID, friendAllSubfolderName);
344}
345
346const LLUUID& LLFriendCardsManager::findChildFolderUUID(const LLUUID& parentFolderUUID, const std::string& nonLocalizedName) const
347{
348	LLNameCategoryCollector matchFolderFunctor(nonLocalizedName);
349
350	return get_folder_uuid(parentFolderUUID, matchFolderFunctor);
351}
352const LLUUID& LLFriendCardsManager::findFriendCardInventoryUUIDImpl(const LLUUID& avatarID)
353{
354	LLUUID friendAllSubfolderUUID = findFriendAllSubfolderUUIDImpl();
355	LLInventoryModel::cat_array_t cats;
356	LLInventoryModel::item_array_t items;
357	LLInventoryModel::item_array_t::const_iterator it;
358
359	// it is not necessary to check friendAllSubfolderUUID against NULL. It will be processed by collectDescendents
360	gInventory.collectDescendents(friendAllSubfolderUUID, cats, items, LLInventoryModel::EXCLUDE_TRASH);
361	for (it = items.begin(); it != items.end(); ++it)
362	{
363		if ((*it)->getCreatorUUID() == avatarID)
364			return (*it)->getUUID();
365	}
366
367	return LLUUID::null;
368}
369
370void LLFriendCardsManager::findMatchedFriendCards(const LLUUID& avatarID, LLInventoryModel::item_array_t& items) const
371{
372	LLInventoryModel::cat_array_t cats;
373	LLUUID friendFolderUUID = findFriendFolderUUIDImpl();
374
375
376	LLViewerInventoryCategory* friendFolder = gInventory.getCategory(friendFolderUUID);
377	if (NULL == friendFolder)
378		return;
379
380	LLParticularBuddyCollector matchFunctor(avatarID);
381	LLInventoryModel::cat_array_t subFolders;
382	subFolders.push_back(friendFolder);
383
384	while (subFolders.count() > 0)
385	{
386		LLViewerInventoryCategory* cat = subFolders.get(0);
387		subFolders.remove(0);
388
389		gInventory.collectDescendentsIf(cat->getUUID(), cats, items, 
390			LLInventoryModel::EXCLUDE_TRASH, matchFunctor);
391
392		move_from_to_arrays(cats, subFolders);
393	}
394}
395
396void LLFriendCardsManager::fetchAndCheckFolderDescendents(const LLUUID& folder_id,  callback_t cb)
397{
398	// This instance will be deleted in LLInitialFriendCardsFetch::done().
399	LLInitialFriendCardsFetch* fetch = new LLInitialFriendCardsFetch(folder_id, cb);
400	fetch->startFetch();
401	if(fetch->isFinished())
402	{
403		// everything is already here - call done.
404		fetch->done();
405	}
406	else
407	{
408		// it's all on it's way - add an observer, and the inventory
409		// will call done for us when everything is here.
410		gInventory.addObserver(fetch);
411	}
412}
413
414// Make sure LLInventoryModel::buildParentChildMap() has been called before it.
415// This method must be called before any actions with friends list.
416void LLFriendCardsManager::ensureFriendsFolderExists()
417{
418	const LLUUID calling_cards_folder_ID = gInventory.findCategoryUUIDForType(LLFolderType::FT_CALLINGCARD);
419
420	// If "Friends" folder exists in "Calling Cards" we should check if "All" sub-folder
421	// exists in "Friends", otherwise we create it.
422	LLUUID friends_folder_ID = findFriendFolderUUIDImpl();
423	if (friends_folder_ID.notNull())
424	{
425		fetchAndCheckFolderDescendents(friends_folder_ID,
426				boost::bind(&LLFriendCardsManager::ensureFriendsAllFolderExists, this));
427	}
428	else
429	{
430		if (!gInventory.isCategoryComplete(calling_cards_folder_ID))
431		{
432			LLViewerInventoryCategory* cat = gInventory.getCategory(calling_cards_folder_ID);
433			std::string cat_name = cat ? cat->getName() : "unknown";
434			llwarns << "Failed to find \"" << cat_name << "\" category descendents in Category Tree." << llendl;
435		}
436
437		friends_folder_ID = gInventory.createNewCategory(calling_cards_folder_ID,
438			LLFolderType::FT_CALLINGCARD, get_friend_folder_name());
439
440		gInventory.createNewCategory(friends_folder_ID,
441			LLFolderType::FT_CALLINGCARD, get_friend_all_subfolder_name());
442
443		// Now when we have all needed folders we can sync their contents with buddies list.
444		syncFriendsFolder();
445	}
446}
447
448// Make sure LLFriendCardsManager::ensureFriendsFolderExists() has been called before it.
449void LLFriendCardsManager::ensureFriendsAllFolderExists()
450{
451	LLUUID friends_all_folder_ID = findFriendAllSubfolderUUIDImpl();
452	if (friends_all_folder_ID.notNull())
453	{
454		fetchAndCheckFolderDescendents(friends_all_folder_ID,
455				boost::bind(&LLFriendCardsManager::syncFriendsFolder, this));
456	}
457	else
458	{
459		LLUUID friends_folder_ID = findFriendFolderUUIDImpl();
460
461		if (!gInventory.isCategoryComplete(friends_folder_ID))
462		{
463			LLViewerInventoryCategory* cat = gInventory.getCategory(friends_folder_ID);
464			std::string cat_name = cat ? cat->getName() : "unknown";
465			llwarns << "Failed to find \"" << cat_name << "\" category descendents in Category Tree." << llendl;
466		}
467
468		friends_all_folder_ID = gInventory.createNewCategory(friends_folder_ID,
469			LLFolderType::FT_CALLINGCARD, get_friend_all_subfolder_name());
470
471		// Now when we have all needed folders we can sync their contents with buddies list.
472		syncFriendsFolder();
473	}
474}
475
476void LLFriendCardsManager::syncFriendsFolder()
477{
478	LLAvatarTracker::buddy_map_t all_buddies;
479	LLAvatarTracker::instance().copyBuddyList(all_buddies);
480
481	// 1. Check if own calling card exists
482	const LLUUID calling_cards_folder_id = gInventory.findCategoryUUIDForType(LLFolderType::FT_CALLINGCARD);
483
484	LLInventoryModel::cat_array_t cats;
485	LLInventoryModel::item_array_t items;
486	LLFindAgentCallingCard collector;
487	gInventory.collectDescendentsIf(calling_cards_folder_id, cats, items, LLInventoryModel::EXCLUDE_TRASH, collector);
488
489	// Create own calling card if it was not found in Friends/All folder
490	if (!collector.isAgentCallingCardFound())
491	{
492		LLAvatarName av_name;
493		LLAvatarNameCache::get( gAgentID, &av_name );
494
495		create_inventory_item(gAgentID,
496							  gAgent.getSessionID(),
497							  calling_cards_folder_id,
498							  LLTransactionID::tnull,
499							  av_name.getCompleteName(),
500							  gAgentID.asString(),
501							  LLAssetType::AT_CALLINGCARD,
502							  LLInventoryType::IT_CALLINGCARD,
503							  NOT_WEARABLE,
504							  PERM_MOVE | PERM_TRANSFER,
505							  NULL);
506	}
507
508	// 2. Add missing Friend Cards for friends
509	LLAvatarTracker::buddy_map_t::const_iterator buddy_it = all_buddies.begin();
510	llinfos << "try to build friends, count: " << all_buddies.size() << llendl;
511	for(; buddy_it != all_buddies.end(); ++buddy_it)
512	{
513		const LLUUID& buddy_id = (*buddy_it).first;
514		addFriendCardToInventory(buddy_id);
515	}
516}
517
518class CreateFriendCardCallback : public LLInventoryCallback
519{
520public:
521	void fire(const LLUUID& inv_item_id)
522	{
523		LLViewerInventoryItem* item = gInventory.getItem(inv_item_id);
524
525		if (item)
526			LLFriendCardsManager::instance().extractAvatarID(item->getCreatorUUID());
527	}
528};
529
530void LLFriendCardsManager::addFriendCardToInventory(const LLUUID& avatarID)
531{
532
533	bool shouldBeAdded = true;
534	LLAvatarName av_name;
535	LLAvatarNameCache::get(avatarID, &av_name);
536	const std::string& name = av_name.mUsername;
537
538	lldebugs << "Processing buddy name: " << name 
539		<< ", id: " << avatarID
540		<< llendl; 
541
542	if (shouldBeAdded && findFriendCardInventoryUUIDImpl(avatarID).notNull())
543	{
544		shouldBeAdded = false;
545		lldebugs << "is found in Inventory: " << name << llendl; 
546	}
547
548	if (shouldBeAdded && isAvatarDataStored(avatarID))
549	{
550		shouldBeAdded = false;
551		lldebugs << "is found in sentRequests: " << name << llendl; 
552	}
553
554	if (shouldBeAdded)
555	{
556		putAvatarData(avatarID);
557		lldebugs << "Sent create_inventory_item for " << avatarID << ", " << name << llendl;
558
559		// TODO: mantipov: Is CreateFriendCardCallback really needed? Probably not
560		LLPointer<LLInventoryCallback> cb = new CreateFriendCardCallback();
561
562		create_inventory_callingcard(avatarID, findFriendAllSubfolderUUIDImpl(), cb);
563	}
564}
565
566void LLFriendCardsManager::removeFriendCardFromInventory(const LLUUID& avatarID)
567{
568	LLInventoryModel::item_array_t items;
569	findMatchedFriendCards(avatarID, items);
570
571	LLInventoryModel::item_array_t::const_iterator it;
572	for (it = items.begin(); it != items.end(); ++ it)
573	{
574		gInventory.removeItem((*it)->getUUID());
575	}
576}
577
578void LLFriendCardsManager::onFriendListUpdate(U32 changed_mask)
579{
580	LLAvatarTracker& at = LLAvatarTracker::instance();
581
582	switch(changed_mask) {
583	case LLFriendObserver::ADD:
584		{
585			const std::set<LLUUID>& changed_items = at.getChangedIDs();
586			std::set<LLUUID>::const_iterator id_it = changed_items.begin();
587			std::set<LLUUID>::const_iterator id_end = changed_items.end();
588			for (;id_it != id_end; ++id_it)
589			{
590				LLFriendCardsManager::instance().addFriendCardToInventory(*id_it);
591			}
592		}
593		break;
594	case LLFriendObserver::REMOVE:
595		{
596			const std::set<LLUUID>& changed_items = at.getChangedIDs();
597			std::set<LLUUID>::const_iterator id_it = changed_items.begin();
598			std::set<LLUUID>::const_iterator id_end = changed_items.end();
599			for (;id_it != id_end; ++id_it)
600			{
601				LLFriendCardsManager::instance().removeFriendCardFromInventory(*id_it);
602			}
603		}
604
605	default:;
606	}
607}
608
609// EOF