InventoryService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 OpenSimulator 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 Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Data;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. namespace OpenSim.Services.InventoryService
  37. {
  38. /// <summary>
  39. /// The Inventory service reference implementation
  40. /// </summary>
  41. public class InventoryService : InventoryServiceBase, IInventoryService
  42. {
  43. private static readonly ILog m_log
  44. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. public InventoryService(IConfigSource config) : base(config)
  46. {
  47. m_log.Debug("[INVENTORY SERVICE]: Initialized.");
  48. }
  49. #region IInventoryServices methods
  50. public string Host
  51. {
  52. get { return "default"; }
  53. }
  54. public List<InventoryFolderBase> GetInventorySkeleton(UUID userId)
  55. {
  56. m_log.DebugFormat("[INVENTORY SERVICE]: Getting inventory skeleton for {0}", userId);
  57. InventoryFolderBase rootFolder = GetRootFolder(userId);
  58. // Agent has no inventory structure yet.
  59. if (null == rootFolder)
  60. {
  61. m_log.DebugFormat("[INVENTORY SERVICE]: No root folder");
  62. return null;
  63. }
  64. List<InventoryFolderBase> userFolders = new List<InventoryFolderBase>();
  65. userFolders.Add(rootFolder);
  66. IList<InventoryFolderBase> folders = m_Database.getFolderHierarchy(rootFolder.ID);
  67. userFolders.AddRange(folders);
  68. // m_log.DebugFormat("[INVENTORY SERVICE]: Got folder {0} {1}", folder.name, folder.folderID);
  69. return userFolders;
  70. }
  71. public virtual bool HasInventoryForUser(UUID userID)
  72. {
  73. return false;
  74. }
  75. // See IInventoryServices
  76. public virtual InventoryFolderBase GetRootFolder(UUID userID)
  77. {
  78. //m_log.DebugFormat("[INVENTORY SERVICE]: Getting root folder for {0}", userID);
  79. // Retrieve the first root folder we get from the DB.
  80. InventoryFolderBase rootFolder = m_Database.getUserRootFolder(userID);
  81. if (rootFolder != null)
  82. return rootFolder;
  83. // Return nothing if the plugin was unable to supply a root folder
  84. return null;
  85. }
  86. // See IInventoryServices
  87. public bool CreateUserInventory(UUID user)
  88. {
  89. InventoryFolderBase existingRootFolder;
  90. try
  91. {
  92. existingRootFolder = GetRootFolder(user);
  93. }
  94. catch /*(Exception e)*/
  95. {
  96. // Munch the exception, it has already been reported
  97. //
  98. return false;
  99. }
  100. if (null != existingRootFolder)
  101. {
  102. m_log.WarnFormat(
  103. "[INVENTORY SERVICE]: Did not create a new inventory for user {0} since they already have "
  104. + "a root inventory folder with id {1}",
  105. user, existingRootFolder.ID);
  106. return false;
  107. }
  108. else
  109. {
  110. return true;
  111. }
  112. }
  113. // See IInventoryServices
  114. /// <summary>
  115. /// Return a user's entire inventory synchronously
  116. /// </summary>
  117. /// <param name="rawUserID"></param>
  118. /// <returns>The user's inventory. If an inventory cannot be found then an empty collection is returned.</returns>
  119. public InventoryCollection GetUserInventory(UUID userID)
  120. {
  121. m_log.InfoFormat("[INVENTORY SERVICE]: Processing request for inventory of {0}", userID);
  122. // Uncomment me to simulate a slow responding inventory server
  123. //Thread.Sleep(16000);
  124. InventoryCollection invCollection = new InventoryCollection();
  125. List<InventoryFolderBase> allFolders = GetInventorySkeleton(userID);
  126. if (null == allFolders)
  127. {
  128. m_log.WarnFormat("[INVENTORY SERVICE]: No inventory found for user {0}", userID);
  129. return invCollection;
  130. }
  131. List<InventoryItemBase> allItems = new List<InventoryItemBase>();
  132. foreach (InventoryFolderBase folder in allFolders)
  133. {
  134. List<InventoryItemBase> items = GetFolderItems(userID, folder.ID);
  135. if (items != null)
  136. {
  137. allItems.InsertRange(0, items);
  138. }
  139. }
  140. invCollection.UserID = userID;
  141. invCollection.Folders = allFolders;
  142. invCollection.Items = allItems;
  143. // foreach (InventoryFolderBase folder in invCollection.Folders)
  144. // {
  145. // m_log.DebugFormat("[GRID INVENTORY SERVICE]: Sending back folder {0} {1}", folder.Name, folder.ID);
  146. // }
  147. //
  148. // foreach (InventoryItemBase item in invCollection.Items)
  149. // {
  150. // m_log.DebugFormat("[GRID INVENTORY SERVICE]: Sending back item {0} {1}, folder {2}", item.Name, item.ID, item.Folder);
  151. // }
  152. m_log.InfoFormat(
  153. "[INVENTORY SERVICE]: Sending back inventory response to user {0} containing {1} folders and {2} items",
  154. invCollection.UserID, invCollection.Folders.Count, invCollection.Items.Count);
  155. return invCollection;
  156. }
  157. /// <summary>
  158. /// Asynchronous inventory fetch.
  159. /// </summary>
  160. /// <param name="userID"></param>
  161. /// <param name="callback"></param>
  162. public void GetUserInventory(UUID userID, InventoryReceiptCallback callback)
  163. {
  164. m_log.InfoFormat("[INVENTORY SERVICE]: Requesting inventory for user {0}", userID);
  165. List<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  166. List<InventoryItemBase> items = new List<InventoryItemBase>();
  167. List<InventoryFolderBase> skeletonFolders = GetInventorySkeleton(userID);
  168. if (skeletonFolders != null)
  169. {
  170. InventoryFolderImpl rootFolder = null;
  171. // Need to retrieve the root folder on the first pass
  172. foreach (InventoryFolderBase folder in skeletonFolders)
  173. {
  174. if (folder.ParentID == UUID.Zero)
  175. {
  176. rootFolder = new InventoryFolderImpl(folder);
  177. folders.Add(rootFolder);
  178. items.AddRange(GetFolderItems(userID, rootFolder.ID));
  179. break; // Only 1 root folder per user
  180. }
  181. }
  182. if (rootFolder != null)
  183. {
  184. foreach (InventoryFolderBase folder in skeletonFolders)
  185. {
  186. if (folder.ID != rootFolder.ID)
  187. {
  188. folders.Add(new InventoryFolderImpl(folder));
  189. items.AddRange(GetFolderItems(userID, folder.ID));
  190. }
  191. }
  192. }
  193. m_log.InfoFormat(
  194. "[INVENTORY SERVICE]: Received inventory response for user {0} containing {1} folders and {2} items",
  195. userID, folders.Count, items.Count);
  196. }
  197. else
  198. {
  199. m_log.WarnFormat("[INVENTORY SERVICE]: User {0} inventory not available", userID);
  200. }
  201. Util.FireAndForget(delegate { callback(folders, items); });
  202. }
  203. public InventoryCollection GetFolderContent(UUID userID, UUID folderID)
  204. {
  205. // Uncomment me to simulate a slow responding inventory server
  206. //Thread.Sleep(16000);
  207. InventoryCollection invCollection = new InventoryCollection();
  208. List<InventoryItemBase> items = GetFolderItems(userID, folderID);
  209. List<InventoryFolderBase> folders = RequestSubFolders(folderID);
  210. invCollection.UserID = userID;
  211. invCollection.Folders = folders;
  212. invCollection.Items = items;
  213. m_log.DebugFormat("[INVENTORY SERVICE]: Found {0} items and {1} folders in folder {2}", items.Count, folders.Count, folderID);
  214. return invCollection;
  215. }
  216. public InventoryFolderBase GetFolderForType(UUID userID, AssetType type)
  217. {
  218. // m_log.DebugFormat("[INVENTORY SERVICE]: Looking for folder type {0} for user {1}", type, userID);
  219. InventoryFolderBase root = m_Database.getUserRootFolder(userID);
  220. if (root != null)
  221. {
  222. List<InventoryFolderBase> folders = RequestSubFolders(root.ID);
  223. foreach (InventoryFolderBase folder in folders)
  224. {
  225. if (folder.Type == (short)type)
  226. {
  227. // m_log.DebugFormat(
  228. // "[INVENTORY SERVICE]: Found folder {0} type {1}", folder.Name, (AssetType)folder.Type);
  229. return folder;
  230. }
  231. }
  232. }
  233. // we didn't find any folder of that type. Return the root folder
  234. // hopefully the root folder is not null. If it is, too bad
  235. return root;
  236. }
  237. public Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(UUID userID)
  238. {
  239. InventoryFolderBase root = GetRootFolder(userID);
  240. if (root != null)
  241. {
  242. InventoryCollection content = GetFolderContent(userID, root.ID);
  243. if (content != null)
  244. {
  245. Dictionary<AssetType, InventoryFolderBase> folders = new Dictionary<AssetType, InventoryFolderBase>();
  246. foreach (InventoryFolderBase folder in content.Folders)
  247. {
  248. if ((folder.Type != (short)AssetType.Folder) && (folder.Type != (short)AssetType.Unknown))
  249. folders[(AssetType)folder.Type] = folder;
  250. }
  251. m_log.DebugFormat("[INVENTORY SERVICE]: Got {0} system folders for {1}", folders.Count, userID);
  252. return folders;
  253. }
  254. }
  255. m_log.WarnFormat("[INVENTORY SERVICE]: System folders for {0} not found", userID);
  256. return new Dictionary<AssetType, InventoryFolderBase>();
  257. }
  258. public List<InventoryItemBase> GetActiveGestures(UUID userId)
  259. {
  260. List<InventoryItemBase> activeGestures = new List<InventoryItemBase>();
  261. activeGestures.AddRange(m_Database.fetchActiveGestures(userId));
  262. return activeGestures;
  263. }
  264. #endregion
  265. #region Methods used by GridInventoryService
  266. public List<InventoryFolderBase> RequestSubFolders(UUID parentFolderID)
  267. {
  268. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  269. inventoryList.AddRange(m_Database.getInventoryFolders(parentFolderID));
  270. return inventoryList;
  271. }
  272. public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
  273. {
  274. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  275. itemsList.AddRange(m_Database.getInventoryInFolder(folderID));
  276. return itemsList;
  277. }
  278. #endregion
  279. // See IInventoryServices
  280. public virtual bool AddFolder(InventoryFolderBase folder)
  281. {
  282. m_log.DebugFormat(
  283. "[INVENTORY SERVICE]: Adding folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  284. m_Database.addInventoryFolder(folder);
  285. // FIXME: Should return false on failure
  286. return true;
  287. }
  288. // See IInventoryServices
  289. public virtual bool UpdateFolder(InventoryFolderBase folder)
  290. {
  291. m_log.DebugFormat(
  292. "[INVENTORY SERVICE]: Updating folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  293. m_Database.updateInventoryFolder(folder);
  294. // FIXME: Should return false on failure
  295. return true;
  296. }
  297. // See IInventoryServices
  298. public virtual bool MoveFolder(InventoryFolderBase folder)
  299. {
  300. m_log.DebugFormat(
  301. "[INVENTORY SERVICE]: Moving folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  302. m_Database.moveInventoryFolder(folder);
  303. // FIXME: Should return false on failure
  304. return true;
  305. }
  306. // See IInventoryServices
  307. public virtual bool AddItem(InventoryItemBase item)
  308. {
  309. m_log.DebugFormat(
  310. "[INVENTORY SERVICE]: Adding item {0} {1} to folder {2}", item.Name, item.ID, item.Folder);
  311. m_Database.addInventoryItem(item);
  312. // FIXME: Should return false on failure
  313. return true;
  314. }
  315. // See IInventoryServices
  316. public virtual bool UpdateItem(InventoryItemBase item)
  317. {
  318. m_log.InfoFormat(
  319. "[INVENTORY SERVICE]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  320. m_Database.updateInventoryItem(item);
  321. // FIXME: Should return false on failure
  322. return true;
  323. }
  324. public virtual bool MoveItems(UUID ownerID, List<InventoryItemBase> items)
  325. {
  326. m_log.InfoFormat(
  327. "[INVENTORY SERVICE]: Moving {0} items from user {1}", items.Count, ownerID);
  328. InventoryItemBase itm = null;
  329. foreach (InventoryItemBase item in items)
  330. {
  331. itm = GetInventoryItem(item.ID);
  332. itm.Folder = item.Folder;
  333. if ((item.Name != null) && !item.Name.Equals(string.Empty))
  334. itm.Name = item.Name;
  335. m_Database.updateInventoryItem(itm);
  336. }
  337. return true;
  338. }
  339. // See IInventoryServices
  340. public virtual bool DeleteItems(UUID owner, List<UUID> itemIDs)
  341. {
  342. m_log.InfoFormat(
  343. "[INVENTORY SERVICE]: Deleting {0} items from user {1}", itemIDs.Count, owner);
  344. // uhh.....
  345. foreach (UUID uuid in itemIDs)
  346. m_Database.deleteInventoryItem(uuid);
  347. // FIXME: Should return false on failure
  348. return true;
  349. }
  350. public virtual InventoryItemBase GetItem(InventoryItemBase item)
  351. {
  352. InventoryItemBase result = m_Database.getInventoryItem(item.ID);
  353. if (result != null)
  354. return result;
  355. m_log.DebugFormat("[INVENTORY SERVICE]: GetItem failed to find item {0}", item.ID);
  356. return null;
  357. }
  358. public virtual InventoryFolderBase GetFolder(InventoryFolderBase folder)
  359. {
  360. InventoryFolderBase result = m_Database.getInventoryFolder(folder.ID);
  361. if (result != null)
  362. return result;
  363. m_log.DebugFormat("[INVENTORY SERVICE]: GetFolder failed to find folder {0}", folder.ID);
  364. return null;
  365. }
  366. public virtual bool DeleteFolders(UUID ownerID, List<UUID> folderIDs)
  367. {
  368. m_log.InfoFormat("[INVENTORY SERVICE]: Deleting {0} folders from user {1}", folderIDs.Count, ownerID);
  369. foreach (UUID id in folderIDs)
  370. {
  371. InventoryFolderBase folder = new InventoryFolderBase(id, ownerID);
  372. PurgeFolder(folder);
  373. m_Database.deleteInventoryFolder(id);
  374. }
  375. return true;
  376. }
  377. /// <summary>
  378. /// Purge a folder of all items items and subfolders.
  379. ///
  380. /// FIXME: Really nasty in a sense, because we have to query the database to get information we may
  381. /// already know... Needs heavy refactoring.
  382. /// </summary>
  383. /// <param name="folder"></param>
  384. public virtual bool PurgeFolder(InventoryFolderBase folder)
  385. {
  386. m_log.DebugFormat(
  387. "[INVENTORY SERVICE]: Purging folder {0} {1} of its contents", folder.Name, folder.ID);
  388. List<InventoryFolderBase> subFolders = RequestSubFolders(folder.ID);
  389. foreach (InventoryFolderBase subFolder in subFolders)
  390. {
  391. // m_log.DebugFormat("[INVENTORY SERVICE]: Deleting folder {0} {1}", subFolder.Name, subFolder.ID);
  392. m_Database.deleteInventoryFolder(subFolder.ID);
  393. }
  394. List<InventoryItemBase> items = GetFolderItems(folder.Owner, folder.ID);
  395. List<UUID> uuids = new List<UUID>();
  396. foreach (InventoryItemBase item in items)
  397. {
  398. uuids.Add(item.ID);
  399. }
  400. DeleteItems(folder.Owner, uuids);
  401. // FIXME: Should return false on failure
  402. return true;
  403. }
  404. public InventoryItemBase GetInventoryItem(UUID itemID)
  405. {
  406. InventoryItemBase item = m_Database.getInventoryItem(itemID);
  407. if (item != null)
  408. return item;
  409. return null;
  410. }
  411. public int GetAssetPermissions(UUID userID, UUID assetID)
  412. {
  413. InventoryFolderBase parent = GetRootFolder(userID);
  414. return FindAssetPerms(parent, assetID);
  415. }
  416. private int FindAssetPerms(InventoryFolderBase folder, UUID assetID)
  417. {
  418. InventoryCollection contents = GetFolderContent(folder.Owner, folder.ID);
  419. int perms = 0;
  420. foreach (InventoryItemBase item in contents.Items)
  421. {
  422. if (item.AssetID == assetID)
  423. perms = (int)item.CurrentPermissions | perms;
  424. }
  425. foreach (InventoryFolderBase subfolder in contents.Folders)
  426. perms = perms | FindAssetPerms(subfolder, assetID);
  427. return perms;
  428. }
  429. }
  430. }