CachedUserInfo.cs 36 KB

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