InventoryService.cs 20 KB

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