CachedUserInfo.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. namespace OpenSim.Framework.Communications.Cache
  33. {
  34. internal delegate void AddItemDelegate(InventoryItemBase itemInfo);
  35. internal delegate void UpdateItemDelegate(InventoryItemBase itemInfo);
  36. internal delegate void DeleteItemDelegate(UUID itemID);
  37. internal delegate void CreateFolderDelegate(string folderName, UUID folderID, ushort folderType, UUID parentID);
  38. internal delegate void MoveFolderDelegate(UUID folderID, UUID parentID);
  39. internal delegate void PurgeFolderDelegate(UUID folderID);
  40. internal delegate void UpdateFolderDelegate(string name, UUID folderID, ushort type, UUID parentID);
  41. internal delegate void SendInventoryDescendentsDelegate(
  42. IClientAPI client, UUID folderID, bool fetchFolders, bool fetchItems);
  43. public delegate void OnItemReceivedDelegate(UUID itemID);
  44. /// <summary>
  45. /// Stores user profile and inventory data received from backend services for a particular user.
  46. /// </summary>
  47. public class CachedUserInfo
  48. {
  49. public event OnItemReceivedDelegate OnItemReceived;
  50. private static readonly ILog m_log
  51. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. /// <summary>
  53. /// The comms manager holds references to services (user, grid, inventory, etc.)
  54. /// </summary>
  55. private readonly CommunicationsManager m_commsManager;
  56. public UserProfileData UserProfile { get { return m_userProfile; } }
  57. private readonly UserProfileData m_userProfile;
  58. /// <summary>
  59. /// Have we received the user's inventory from the inventory service?
  60. /// </summary>
  61. public bool HasReceivedInventory { get { return m_hasReceivedInventory; } }
  62. private bool m_hasReceivedInventory;
  63. /// <summary>
  64. /// Inventory requests waiting for receipt of this user's inventory from the inventory service.
  65. /// </summary>
  66. private readonly IList<IInventoryRequest> m_pendingRequests = new List<IInventoryRequest>();
  67. /// <summary>
  68. /// The root folder of this user's inventory. Returns null if the root folder has not yet been received.
  69. /// </summary>
  70. public InventoryFolderImpl RootFolder { get { return m_rootFolder; } }
  71. private InventoryFolderImpl m_rootFolder;
  72. public UUID SessionID
  73. {
  74. get { return m_session_id; }
  75. set { m_session_id = value; }
  76. }
  77. private UUID m_session_id = UUID.Zero;
  78. /// <summary>
  79. /// Constructor
  80. /// </summary>
  81. /// <param name="commsManager"></param>
  82. /// <param name="userProfile"></param>
  83. public CachedUserInfo(CommunicationsManager commsManager, UserProfileData userProfile)
  84. {
  85. m_commsManager = commsManager;
  86. m_userProfile = userProfile;
  87. }
  88. /// <summary>
  89. /// This allows a request to be added to be processed once we receive a user's inventory
  90. /// from the inventory service. If we already have the inventory, the request
  91. /// is executed immediately instead.
  92. /// </summary>
  93. /// <param name="parent"></param>
  94. protected void AddRequest(IInventoryRequest request)
  95. {
  96. lock (m_pendingRequests)
  97. {
  98. if (HasReceivedInventory)
  99. {
  100. request.Execute();
  101. }
  102. else
  103. {
  104. m_pendingRequests.Add(request);
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Helper function for InventoryReceive() - Store a folder temporarily until we've received entire folder list
  110. /// </summary>
  111. /// <param name="folder"></param>
  112. private void AddFolderToDictionary(InventoryFolderImpl folder, IDictionary<UUID, IList<InventoryFolderImpl>> dictionary)
  113. {
  114. UUID parentFolderId = folder.ParentID;
  115. if (dictionary.ContainsKey(parentFolderId))
  116. dictionary[parentFolderId].Add(folder);
  117. else
  118. {
  119. IList<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  120. folders.Add(folder);
  121. dictionary[parentFolderId] = folders;
  122. }
  123. }
  124. /// <summary>
  125. /// Recursively, in depth-first order, add all the folders we've received (stored
  126. /// in a dictionary indexed by parent ID) into the tree that describes user folder
  127. /// heirarchy
  128. /// Any folder that is resolved into the tree is also added to resolvedFolderDictionary,
  129. /// indexed by folder ID.
  130. /// </summary>
  131. /// <param name="parentId">
  132. /// A <see cref="UUID"/>
  133. /// </param>
  134. private void ResolveReceivedFolders(InventoryFolderImpl parentFolder,
  135. IDictionary<UUID, IList<InventoryFolderImpl>> receivedFolderDictionary,
  136. IDictionary<UUID, InventoryFolderImpl> resolvedFolderDictionary)
  137. {
  138. if (receivedFolderDictionary.ContainsKey(parentFolder.ID))
  139. {
  140. List<InventoryFolderImpl> resolvedFolders = new List<InventoryFolderImpl>(); // Folders we've resolved with this invocation
  141. foreach (InventoryFolderImpl folder in receivedFolderDictionary[parentFolder.ID])
  142. {
  143. if (parentFolder.ContainsChildFolder(folder.ID))
  144. {
  145. m_log.WarnFormat(
  146. "[INVENTORY CACHE]: Received folder {0} {1} from inventory service which has already been received",
  147. folder.Name, folder.ID);
  148. }
  149. else
  150. {
  151. if (resolvedFolderDictionary.ContainsKey(folder.ID))
  152. {
  153. m_log.WarnFormat(
  154. "[INVENTORY CACHE]: Received folder {0} {1} from inventory service has already been received but with different parent",
  155. folder.Name, folder.ID);
  156. }
  157. else
  158. {
  159. resolvedFolders.Add(folder);
  160. resolvedFolderDictionary[folder.ID] = folder;
  161. parentFolder.AddChildFolder(folder);
  162. }
  163. }
  164. } // foreach (folder in pendingCategorizationFolders[parentFolder.ID])
  165. receivedFolderDictionary.Remove(parentFolder.ID);
  166. foreach (InventoryFolderImpl folder in resolvedFolders)
  167. ResolveReceivedFolders(folder, receivedFolderDictionary, resolvedFolderDictionary);
  168. } // if (receivedFolderDictionary.ContainsKey(parentFolder.ID))
  169. }
  170. /// <summary>
  171. /// Drop all cached inventory.
  172. /// </summary>
  173. public void DropInventory()
  174. {
  175. m_log.Debug("[INVENTORY CACHE]: DropInventory called");
  176. // Make sure there aren't pending requests around when we do this
  177. // FIXME: There is still a race condition where an inventory operation can be requested (since these aren't being locked).
  178. // Will have to extend locking to exclude this very soon.
  179. lock (m_pendingRequests)
  180. {
  181. m_hasReceivedInventory = false;
  182. m_rootFolder = null;
  183. }
  184. }
  185. /// <summary>
  186. /// Fetch inventory for this user.
  187. /// </summary>
  188. /// This has to be executed as a separate step once user information is retreived.
  189. /// This will occur synchronously if the inventory service is in the same process as this class, and
  190. /// asynchronously otherwise.
  191. public void FetchInventory()
  192. {
  193. if (m_commsManager.SecureInventoryService != null)
  194. {
  195. m_commsManager.SecureInventoryService.RequestInventoryForUser(
  196. UserProfile.ID, SessionID, InventoryReceive);
  197. }
  198. else
  199. {
  200. m_commsManager.InventoryService.RequestInventoryForUser(UserProfile.ID, InventoryReceive);
  201. }
  202. }
  203. /// <summary>
  204. /// Callback invoked when the inventory is received from an async request to the inventory service
  205. /// </summary>
  206. /// <param name="userID"></param>
  207. /// <param name="inventoryCollection"></param>
  208. public void InventoryReceive(ICollection<InventoryFolderImpl> folders, ICollection<InventoryItemBase> items)
  209. {
  210. // FIXME: Exceptions thrown upwards never appear on the console. Could fix further up if these
  211. // are simply being swallowed
  212. try
  213. {
  214. // collection of all received folders, indexed by their parent ID
  215. IDictionary<UUID, IList<InventoryFolderImpl>> receivedFolders =
  216. new Dictionary<UUID, IList<InventoryFolderImpl>>();
  217. // collection of all folders that have been placed into the folder heirarchy starting at m_rootFolder
  218. // This dictonary exists so we don't have to do an InventoryFolderImpl.FindFolder(), which is O(n) on the
  219. // number of folders in our inventory.
  220. // Maybe we should make this structure a member so we can skip InventoryFolderImpl.FindFolder() calls later too?
  221. IDictionary<UUID, InventoryFolderImpl> resolvedFolders =
  222. new Dictionary<UUID, InventoryFolderImpl>();
  223. // Take all received folders, find the root folder, and put ther rest into
  224. // the pendingCategorizationFolders collection
  225. foreach (InventoryFolderImpl folder in folders)
  226. AddFolderToDictionary(folder, receivedFolders);
  227. if (!receivedFolders.ContainsKey(UUID.Zero))
  228. throw new Exception("Database did not return a root inventory folder");
  229. else
  230. {
  231. IList<InventoryFolderImpl> rootFolderList = receivedFolders[UUID.Zero];
  232. m_rootFolder = rootFolderList[0];
  233. resolvedFolders[m_rootFolder.ID] = m_rootFolder;
  234. if (rootFolderList.Count > 1)
  235. {
  236. for (int i = 1; i < rootFolderList.Count; i++)
  237. {
  238. m_log.WarnFormat(
  239. "[INVENTORY CACHE]: Discarding extra root folder {0}. Using previously received root folder {1}",
  240. rootFolderList[i].ID, RootFolder.ID);
  241. }
  242. }
  243. receivedFolders.Remove(UUID.Zero);
  244. }
  245. // Now take the pendingCategorizationFolders collection, and turn that into a tree,
  246. // with the root being RootFolder
  247. if (RootFolder != null)
  248. ResolveReceivedFolders(RootFolder, receivedFolders, resolvedFolders);
  249. // Generate a warning for folders that are not part of the heirarchy
  250. foreach (KeyValuePair<UUID, IList<InventoryFolderImpl>> folderList in receivedFolders)
  251. {
  252. foreach (InventoryFolderImpl folder in folderList.Value)
  253. m_log.WarnFormat("[INVENTORY CACHE]: Malformed Database: Unresolved Pending Folder {0}", folder.Name);
  254. }
  255. // Take all ther received items and put them into the folder tree heirarchy
  256. foreach (InventoryItemBase item in items) {
  257. InventoryFolderImpl folder = resolvedFolders.ContainsKey(item.Folder) ? resolvedFolders[item.Folder] : null;
  258. ItemReceive(item, folder );
  259. }
  260. }
  261. catch (Exception e)
  262. {
  263. m_log.ErrorFormat("[INVENTORY CACHE]: Error processing inventory received from inventory service, {0}", e);
  264. }
  265. // Deal with pending requests
  266. lock (m_pendingRequests)
  267. {
  268. // We're going to change inventory status within the lock to avoid a race condition
  269. // where requests are processed after the AddRequest() method has been called.
  270. m_hasReceivedInventory = true;
  271. foreach (IInventoryRequest request in m_pendingRequests)
  272. {
  273. request.Execute();
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// Callback invoked when an item is received from an async request to the inventory service.
  279. ///
  280. /// We're assuming here that items are always received after all the folders
  281. /// received.
  282. /// If folder is null, we will search for it starting from RootFolder (an O(n) operation),
  283. /// otherwise we'll just put it into folder
  284. /// </summary>
  285. /// <param name="folderInfo"></param>
  286. private void ItemReceive(InventoryItemBase itemInfo, InventoryFolderImpl folder)
  287. {
  288. // m_log.DebugFormat(
  289. // "[INVENTORY CACHE]: Received item {0} {1} for user {2}",
  290. // itemInfo.Name, itemInfo.ID, userID);
  291. if (folder == null && RootFolder != null)
  292. folder = RootFolder.FindFolder(itemInfo.Folder);
  293. if (null == folder)
  294. {
  295. m_log.WarnFormat(
  296. "Received item {0} {1} but its folder {2} does not exist",
  297. itemInfo.Name, itemInfo.ID, itemInfo.Folder);
  298. return;
  299. }
  300. lock (folder.Items)
  301. {
  302. folder.Items[itemInfo.ID] = itemInfo;
  303. }
  304. if (OnItemReceived != null)
  305. OnItemReceived(itemInfo.ID);
  306. }
  307. /// <summary>
  308. /// Create a folder in this agent's inventory.
  309. /// </summary>
  310. ///
  311. /// If the inventory service has not yet delievered the inventory
  312. /// for this user then the request will be queued.
  313. ///
  314. /// <param name="parentID"></param>
  315. /// <returns></returns>
  316. public bool CreateFolder(string folderName, UUID folderID, ushort folderType, UUID parentID)
  317. {
  318. // m_log.DebugFormat(
  319. // "[AGENT INVENTORY]: Creating inventory folder {0} {1} for {2} {3}", folderID, folderName, remoteClient.Name, remoteClient.AgentId);
  320. if (m_hasReceivedInventory)
  321. {
  322. InventoryFolderImpl parentFolder = RootFolder.FindFolder(parentID);
  323. if (null == parentFolder)
  324. {
  325. m_log.WarnFormat(
  326. "[AGENT INVENTORY]: Tried to create folder {0} {1} but the parent {2} does not exist",
  327. folderName, folderID, parentID);
  328. return false;
  329. }
  330. InventoryFolderImpl createdFolder = parentFolder.CreateChildFolder(folderID, folderName, folderType);
  331. if (createdFolder != null)
  332. {
  333. InventoryFolderBase createdBaseFolder = new InventoryFolderBase();
  334. createdBaseFolder.Owner = createdFolder.Owner;
  335. createdBaseFolder.ID = createdFolder.ID;
  336. createdBaseFolder.Name = createdFolder.Name;
  337. createdBaseFolder.ParentID = createdFolder.ParentID;
  338. createdBaseFolder.Type = createdFolder.Type;
  339. createdBaseFolder.Version = createdFolder.Version;
  340. if (m_commsManager.SecureInventoryService != null)
  341. {
  342. m_commsManager.SecureInventoryService.AddFolder(createdBaseFolder, m_session_id);
  343. }
  344. else
  345. {
  346. m_commsManager.InventoryService.AddFolder(createdBaseFolder);
  347. }
  348. return true;
  349. }
  350. else
  351. {
  352. m_log.WarnFormat(
  353. "[AGENT INVENTORY]: Tried to create folder {0} {1} but the folder already exists",
  354. folderName, folderID);
  355. return false;
  356. }
  357. }
  358. else
  359. {
  360. AddRequest(
  361. new InventoryRequest(
  362. Delegate.CreateDelegate(typeof(CreateFolderDelegate), this, "CreateFolder"),
  363. new object[] { folderName, folderID, folderType, parentID }));
  364. return true;
  365. }
  366. }
  367. /// <summary>
  368. /// Handle a client request to update the inventory folder
  369. /// </summary>
  370. ///
  371. /// If the inventory service has not yet delievered the inventory
  372. /// for this user then the request will be queued.
  373. ///
  374. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  375. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  376. /// and needs to be changed.
  377. ///
  378. /// <param name="folderID"></param>
  379. /// <param name="type"></param>
  380. /// <param name="name"></param>
  381. /// <param name="parentID"></param>
  382. public bool UpdateFolder(string name, UUID folderID, ushort type, UUID parentID)
  383. {
  384. // m_log.DebugFormat(
  385. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  386. if (m_hasReceivedInventory)
  387. {
  388. InventoryFolderImpl folder = RootFolder.FindFolder(folderID);
  389. // Delegate movement if updated parent id isn't the same as the existing parentId
  390. if (folder.ParentID != parentID)
  391. MoveFolder(folderID, parentID);
  392. InventoryFolderBase baseFolder = new InventoryFolderBase();
  393. baseFolder.Owner = m_userProfile.ID;
  394. baseFolder.ID = folderID;
  395. baseFolder.Name = name;
  396. baseFolder.ParentID = parentID;
  397. baseFolder.Type = (short)type;
  398. baseFolder.Version = RootFolder.Version;
  399. if (m_commsManager.SecureInventoryService != null)
  400. {
  401. m_commsManager.SecureInventoryService.UpdateFolder(baseFolder, m_session_id);
  402. }
  403. else
  404. {
  405. m_commsManager.InventoryService.UpdateFolder(baseFolder);
  406. }
  407. folder.Name = name;
  408. folder.Type = (short)type;
  409. }
  410. else
  411. {
  412. AddRequest(
  413. new InventoryRequest(
  414. Delegate.CreateDelegate(typeof(UpdateFolderDelegate), this, "UpdateFolder"),
  415. new object[] { name, folderID, type, parentID }));
  416. }
  417. return true;
  418. }
  419. /// <summary>
  420. /// Handle an inventory folder move request from the client.
  421. ///
  422. /// If the inventory service has not yet delievered the inventory
  423. /// for this user then the request will be queued.
  424. /// </summary>
  425. ///
  426. /// <param name="folderID"></param>
  427. /// <param name="parentID"></param>
  428. /// <returns>
  429. /// true if the delete was successful, or if it was queued pending folder receipt
  430. /// false if the folder to be deleted did not exist.
  431. /// </returns>
  432. public bool MoveFolder(UUID folderID, UUID parentID)
  433. {
  434. // m_log.DebugFormat(
  435. // "[AGENT INVENTORY]: Moving inventory folder {0} into folder {1} for {2} {3}",
  436. // parentID, remoteClient.Name, remoteClient.Name, remoteClient.AgentId);
  437. if (m_hasReceivedInventory)
  438. {
  439. InventoryFolderBase baseFolder = new InventoryFolderBase();
  440. baseFolder.Owner = m_userProfile.ID;
  441. baseFolder.ID = folderID;
  442. baseFolder.ParentID = parentID;
  443. if (m_commsManager.SecureInventoryService != null)
  444. {
  445. m_commsManager.SecureInventoryService.MoveFolder(baseFolder, m_session_id);
  446. }
  447. else
  448. {
  449. m_commsManager.InventoryService.MoveFolder(baseFolder);
  450. }
  451. InventoryFolderImpl folder = RootFolder.FindFolder(folderID);
  452. InventoryFolderImpl parentFolder = RootFolder.FindFolder(parentID);
  453. if (parentFolder != null && folder != null)
  454. {
  455. InventoryFolderImpl oldParentFolder = RootFolder.FindFolder(folder.ParentID);
  456. if (oldParentFolder != null)
  457. {
  458. oldParentFolder.RemoveChildFolder(folderID);
  459. parentFolder.AddChildFolder(folder);
  460. }
  461. else
  462. {
  463. return false;
  464. }
  465. }
  466. else
  467. {
  468. return false;
  469. }
  470. return true;
  471. }
  472. else
  473. {
  474. AddRequest(
  475. new InventoryRequest(
  476. Delegate.CreateDelegate(typeof(MoveFolderDelegate), this, "MoveFolder"),
  477. new object[] { folderID, parentID }));
  478. return true;
  479. }
  480. }
  481. /// <summary>
  482. /// This method will delete all the items and folders in the given folder.
  483. /// </summary>
  484. /// If the inventory service has not yet delievered the inventory
  485. /// for this user then the request will be queued.
  486. ///
  487. /// <param name="folderID"></param>
  488. public bool PurgeFolder(UUID folderID)
  489. {
  490. // m_log.InfoFormat("[AGENT INVENTORY]: Purging folder {0} for {1} uuid {2}",
  491. // folderID, remoteClient.Name, remoteClient.AgentId);
  492. if (m_hasReceivedInventory)
  493. {
  494. InventoryFolderImpl purgedFolder = RootFolder.FindFolder(folderID);
  495. if (purgedFolder != null)
  496. {
  497. // XXX Nasty - have to create a new object to hold details we already have
  498. InventoryFolderBase purgedBaseFolder = new InventoryFolderBase();
  499. purgedBaseFolder.Owner = purgedFolder.Owner;
  500. purgedBaseFolder.ID = purgedFolder.ID;
  501. purgedBaseFolder.Name = purgedFolder.Name;
  502. purgedBaseFolder.ParentID = purgedFolder.ParentID;
  503. purgedBaseFolder.Type = purgedFolder.Type;
  504. purgedBaseFolder.Version = purgedFolder.Version;
  505. if (m_commsManager.SecureInventoryService != null)
  506. {
  507. m_commsManager.SecureInventoryService.PurgeFolder(purgedBaseFolder, m_session_id);
  508. }
  509. else
  510. {
  511. m_commsManager.InventoryService.PurgeFolder(purgedBaseFolder);
  512. }
  513. purgedFolder.Purge();
  514. return true;
  515. }
  516. }
  517. else
  518. {
  519. AddRequest(
  520. new InventoryRequest(
  521. Delegate.CreateDelegate(typeof(PurgeFolderDelegate), this, "PurgeFolder"),
  522. new object[] { folderID }));
  523. return true;
  524. }
  525. return false;
  526. }
  527. /// <summary>
  528. /// Add an item to the user's inventory.
  529. /// </summary>
  530. /// If the item has no folder set (i.e. it is UUID.Zero), then it is placed in the most appropriate folder
  531. /// for that type.
  532. /// <param name="itemInfo"></param>
  533. public void AddItem(InventoryItemBase item)
  534. {
  535. if (m_hasReceivedInventory)
  536. {
  537. if (item.Folder == UUID.Zero)
  538. {
  539. InventoryFolderImpl f = FindFolderForType(item.AssetType);
  540. if (f != null)
  541. item.Folder = f.ID;
  542. else
  543. item.Folder = RootFolder.ID;
  544. }
  545. ItemReceive(item, null);
  546. if (m_commsManager.SecureInventoryService != null)
  547. {
  548. m_commsManager.SecureInventoryService.AddItem(item, m_session_id);
  549. }
  550. else
  551. {
  552. m_commsManager.InventoryService.AddItem(item);
  553. }
  554. }
  555. else
  556. {
  557. AddRequest(
  558. new InventoryRequest(
  559. Delegate.CreateDelegate(typeof(AddItemDelegate), this, "AddItem"),
  560. new object[] { item }));
  561. }
  562. }
  563. /// <summary>
  564. /// Update an item in the user's inventory
  565. /// </summary>
  566. /// <param name="userID"></param>
  567. /// <param name="itemInfo"></param>
  568. public void UpdateItem(InventoryItemBase item)
  569. {
  570. if (m_hasReceivedInventory)
  571. {
  572. if (m_commsManager.SecureInventoryService != null)
  573. {
  574. m_commsManager.SecureInventoryService.UpdateItem(item, m_session_id);
  575. }
  576. else
  577. {
  578. m_commsManager.InventoryService.UpdateItem(item);
  579. }
  580. }
  581. else
  582. {
  583. AddRequest(
  584. new InventoryRequest(
  585. Delegate.CreateDelegate(typeof(UpdateItemDelegate), this, "UpdateItem"),
  586. new object[] { item }));
  587. }
  588. }
  589. /// <summary>
  590. /// Delete an item from the user's inventory
  591. ///
  592. /// If the inventory service has not yet delievered the inventory
  593. /// for this user then the request will be queued.
  594. /// </summary>
  595. /// <param name="itemID"></param>
  596. /// <returns>
  597. /// true on a successful delete or a if the request is queued.
  598. /// Returns false on an immediate failure
  599. /// </returns>
  600. public bool DeleteItem(UUID itemID)
  601. {
  602. if (m_hasReceivedInventory)
  603. {
  604. // XXX For historical reasons (grid comms), we need to retrieve the whole item in order to delete, even though
  605. // really only the item id is required.
  606. InventoryItemBase item = RootFolder.FindItem(itemID);
  607. if (null == item)
  608. {
  609. m_log.WarnFormat("[AGENT INVENTORY]: Tried to delete item {0} which does not exist", itemID);
  610. return false;
  611. }
  612. if (RootFolder.DeleteItem(item.ID))
  613. {
  614. if (m_commsManager.SecureInventoryService != null)
  615. {
  616. return m_commsManager.SecureInventoryService.DeleteItem(item, m_session_id);
  617. }
  618. else
  619. {
  620. return m_commsManager.InventoryService.DeleteItem(item);
  621. }
  622. }
  623. }
  624. else
  625. {
  626. AddRequest(
  627. new InventoryRequest(
  628. Delegate.CreateDelegate(typeof(DeleteItemDelegate), this, "DeleteItem"),
  629. new object[] { itemID }));
  630. return true;
  631. }
  632. return false;
  633. }
  634. /// <summary>
  635. /// Send details of the inventory items and/or folders in a given folder to the client.
  636. /// </summary>
  637. /// <param name="client"></param>
  638. /// <param name="folderID"></param>
  639. /// <param name="fetchFolders"></param>
  640. /// <param name="fetchItems"></param>
  641. /// <returns>true if the request was queued or successfully processed, false otherwise</returns>
  642. public bool SendInventoryDecendents(IClientAPI client, UUID folderID, bool fetchFolders, bool fetchItems)
  643. {
  644. if (m_hasReceivedInventory)
  645. {
  646. InventoryFolderImpl folder;
  647. if ((folder = RootFolder.FindFolder(folderID)) != null)
  648. {
  649. // m_log.DebugFormat(
  650. // "[AGENT INVENTORY]: Found folder {0} for client {1}",
  651. // folderID, remoteClient.AgentId);
  652. client.SendInventoryFolderDetails(
  653. client.AgentId, folderID, folder.RequestListOfItems(),
  654. folder.RequestListOfFolders(), fetchFolders, fetchItems);
  655. return true;
  656. }
  657. else
  658. {
  659. m_log.WarnFormat(
  660. "[AGENT INVENTORY]: Could not find folder {0} requested by user {1} {2}",
  661. folderID, client.Name, client.AgentId);
  662. return false;
  663. }
  664. }
  665. else
  666. {
  667. AddRequest(
  668. new InventoryRequest(
  669. Delegate.CreateDelegate(typeof(SendInventoryDescendentsDelegate), this, "SendInventoryDecendents", false, false),
  670. new object[] { client, folderID, fetchFolders, fetchItems }));
  671. return true;
  672. }
  673. }
  674. /// <summary>
  675. /// Find an appropriate folder for the given asset type
  676. /// </summary>
  677. /// <param name="type"></param>
  678. /// <returns>null if no appropriate folder exists</returns>
  679. public InventoryFolderImpl FindFolderForType(int type)
  680. {
  681. if (RootFolder == null)
  682. return null;
  683. return RootFolder.FindFolderForType(type);
  684. }
  685. }
  686. /// <summary>
  687. /// Should be implemented by callers which require a callback when the user's inventory is received
  688. /// </summary>
  689. public interface IInventoryRequest
  690. {
  691. /// <summary>
  692. /// This is the method executed once we have received the user's inventory by which the request can be fulfilled.
  693. /// </summary>
  694. void Execute();
  695. }
  696. /// <summary>
  697. /// Generic inventory request
  698. /// </summary>
  699. class InventoryRequest : IInventoryRequest
  700. {
  701. private Delegate m_delegate;
  702. private Object[] m_args;
  703. internal InventoryRequest(Delegate delegat, Object[] args)
  704. {
  705. m_delegate = delegat;
  706. m_args = args;
  707. }
  708. public void Execute()
  709. {
  710. if (m_delegate != null)
  711. m_delegate.DynamicInvoke(m_args);
  712. }
  713. }
  714. }