PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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