InventoryService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 = GetRootFolder(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 GetRootFolder(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 = GetRootFolder(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 InventoryCollection GetFolderContent(UUID userID, UUID folderID)
  194. {
  195. m_log.Info("[INVENTORY SERVICE]: Processing request for folder " + folderID);
  196. // Uncomment me to simulate a slow responding inventory server
  197. //Thread.Sleep(16000);
  198. InventoryCollection invCollection = new InventoryCollection();
  199. List<InventoryItemBase> items = GetFolderItems(userID, folderID);
  200. List<InventoryFolderBase> folders = RequestSubFolders(folderID);
  201. invCollection.UserID = userID;
  202. invCollection.Folders = folders;
  203. invCollection.Items = items;
  204. m_log.DebugFormat("[INVENTORY SERVICE]: Found {0} items and {1} folders", items.Count, folders.Count);
  205. return invCollection;
  206. }
  207. public InventoryFolderBase GetFolderForType(UUID userID, AssetType type)
  208. {
  209. InventoryFolderBase root = m_Database.getUserRootFolder(userID);
  210. if (root != null)
  211. {
  212. List<InventoryFolderBase> folders = RequestSubFolders(root.ID);
  213. foreach (InventoryFolderBase folder in folders)
  214. {
  215. if (folder.Type == (short)type)
  216. return folder;
  217. }
  218. }
  219. // we didn't find any folder of that type. Return the root folder
  220. // hopefully the root folder is not null. If it is, too bad
  221. return root;
  222. }
  223. public Dictionary<AssetType, InventoryFolderBase> GetSystemFolders(UUID userID)
  224. {
  225. InventoryFolderBase root = GetRootFolder(userID);
  226. if (root != null)
  227. {
  228. InventoryCollection content = GetFolderContent(userID, root.ID);
  229. if (content != null)
  230. {
  231. Dictionary<AssetType, InventoryFolderBase> folders = new Dictionary<AssetType, InventoryFolderBase>();
  232. foreach (InventoryFolderBase folder in content.Folders)
  233. {
  234. if ((folder.Type != (short)AssetType.Folder) && (folder.Type != (short)AssetType.Unknown))
  235. folders[(AssetType)folder.Type] = folder;
  236. }
  237. return folders;
  238. }
  239. }
  240. m_log.WarnFormat("[INVENTORY SERVICE]: System folders for {0} not found", userID);
  241. return new Dictionary<AssetType, InventoryFolderBase>();
  242. }
  243. public List<InventoryItemBase> GetActiveGestures(UUID userId)
  244. {
  245. List<InventoryItemBase> activeGestures = new List<InventoryItemBase>();
  246. activeGestures.AddRange(m_Database.fetchActiveGestures(userId));
  247. return activeGestures;
  248. }
  249. #endregion
  250. #region Methods used by GridInventoryService
  251. public List<InventoryFolderBase> RequestSubFolders(UUID parentFolderID)
  252. {
  253. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  254. inventoryList.AddRange(m_Database.getInventoryFolders(parentFolderID));
  255. return inventoryList;
  256. }
  257. public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
  258. {
  259. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  260. itemsList.AddRange(m_Database.getInventoryInFolder(folderID));
  261. return itemsList;
  262. }
  263. #endregion
  264. // See IInventoryServices
  265. public virtual bool AddFolder(InventoryFolderBase folder)
  266. {
  267. m_log.DebugFormat(
  268. "[INVENTORY SERVICE]: Adding folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  269. m_Database.addInventoryFolder(folder);
  270. // FIXME: Should return false on failure
  271. return true;
  272. }
  273. // See IInventoryServices
  274. public virtual bool UpdateFolder(InventoryFolderBase folder)
  275. {
  276. m_log.DebugFormat(
  277. "[INVENTORY SERVICE]: Updating folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  278. m_Database.updateInventoryFolder(folder);
  279. // FIXME: Should return false on failure
  280. return true;
  281. }
  282. // See IInventoryServices
  283. public virtual bool MoveFolder(InventoryFolderBase folder)
  284. {
  285. m_log.DebugFormat(
  286. "[INVENTORY SERVICE]: Moving folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  287. m_Database.moveInventoryFolder(folder);
  288. // FIXME: Should return false on failure
  289. return true;
  290. }
  291. // See IInventoryServices
  292. public virtual bool AddItem(InventoryItemBase item)
  293. {
  294. m_log.DebugFormat(
  295. "[INVENTORY SERVICE]: Adding item {0} {1} to folder {2}", item.Name, item.ID, item.Folder);
  296. m_Database.addInventoryItem(item);
  297. // FIXME: Should return false on failure
  298. return true;
  299. }
  300. // See IInventoryServices
  301. public virtual bool UpdateItem(InventoryItemBase item)
  302. {
  303. m_log.InfoFormat(
  304. "[INVENTORY SERVICE]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  305. m_Database.updateInventoryItem(item);
  306. // FIXME: Should return false on failure
  307. return true;
  308. }
  309. // See IInventoryServices
  310. public virtual bool DeleteItem(InventoryItemBase item)
  311. {
  312. m_log.InfoFormat(
  313. "[INVENTORY SERVICE]: Deleting item {0} {1} from folder {2}", item.Name, item.ID, item.Folder);
  314. m_Database.deleteInventoryItem(item.ID);
  315. // FIXME: Should return false on failure
  316. return true;
  317. }
  318. public virtual InventoryItemBase QueryItem(InventoryItemBase item)
  319. {
  320. InventoryItemBase result = m_Database.queryInventoryItem(item.ID);
  321. if (result != null)
  322. return result;
  323. return null;
  324. }
  325. public virtual InventoryFolderBase QueryFolder(InventoryFolderBase item)
  326. {
  327. InventoryFolderBase result = m_Database.queryInventoryFolder(item.ID);
  328. if (result != null)
  329. return result;
  330. return null;
  331. }
  332. /// <summary>
  333. /// Purge a folder of all items items and subfolders.
  334. ///
  335. /// FIXME: Really nasty in a sense, because we have to query the database to get information we may
  336. /// already know... Needs heavy refactoring.
  337. /// </summary>
  338. /// <param name="folder"></param>
  339. public virtual bool PurgeFolder(InventoryFolderBase folder)
  340. {
  341. m_log.DebugFormat(
  342. "[INVENTORY SERVICE]: Purging folder {0} {1} of its contents", folder.Name, folder.ID);
  343. List<InventoryFolderBase> subFolders = RequestSubFolders(folder.ID);
  344. foreach (InventoryFolderBase subFolder in subFolders)
  345. {
  346. // m_log.DebugFormat("[INVENTORY SERVICE]: Deleting folder {0} {1}", subFolder.Name, subFolder.ID);
  347. m_Database.deleteInventoryFolder(subFolder.ID);
  348. }
  349. List<InventoryItemBase> items = GetFolderItems(folder.Owner, folder.ID);
  350. foreach (InventoryItemBase item in items)
  351. {
  352. DeleteItem(item);
  353. }
  354. // FIXME: Should return false on failure
  355. return true;
  356. }
  357. private void AddNewInventorySet(UsersInventory inventory)
  358. {
  359. foreach (InventoryFolderBase folder in inventory.Folders.Values)
  360. {
  361. AddFolder(folder);
  362. }
  363. }
  364. public InventoryItemBase GetInventoryItem(UUID itemID)
  365. {
  366. InventoryItemBase item = m_Database.getInventoryItem(itemID);
  367. if (item != null)
  368. return item;
  369. return null;
  370. }
  371. /// <summary>
  372. /// Used to create a new user inventory.
  373. /// </summary>
  374. private class UsersInventory
  375. {
  376. public Dictionary<UUID, InventoryFolderBase> Folders = new Dictionary<UUID, InventoryFolderBase>();
  377. public Dictionary<UUID, InventoryItemBase> Items = new Dictionary<UUID, InventoryItemBase>();
  378. public virtual void CreateNewInventorySet(UUID user)
  379. {
  380. InventoryFolderBase folder = new InventoryFolderBase();
  381. folder.ParentID = UUID.Zero;
  382. folder.Owner = user;
  383. folder.ID = UUID.Random();
  384. folder.Name = "My Inventory";
  385. folder.Type = (short)AssetType.Folder;
  386. folder.Version = 1;
  387. Folders.Add(folder.ID, folder);
  388. UUID rootFolder = folder.ID;
  389. folder = new InventoryFolderBase();
  390. folder.ParentID = rootFolder;
  391. folder.Owner = user;
  392. folder.ID = UUID.Random();
  393. folder.Name = "Animations";
  394. folder.Type = (short)AssetType.Animation;
  395. folder.Version = 1;
  396. Folders.Add(folder.ID, folder);
  397. folder = new InventoryFolderBase();
  398. folder.ParentID = rootFolder;
  399. folder.Owner = user;
  400. folder.ID = UUID.Random();
  401. folder.Name = "Body Parts";
  402. folder.Type = (short)AssetType.Bodypart;
  403. folder.Version = 1;
  404. Folders.Add(folder.ID, folder);
  405. folder = new InventoryFolderBase();
  406. folder.ParentID = rootFolder;
  407. folder.Owner = user;
  408. folder.ID = UUID.Random();
  409. folder.Name = "Calling Cards";
  410. folder.Type = (short)AssetType.CallingCard;
  411. folder.Version = 1;
  412. Folders.Add(folder.ID, folder);
  413. folder = new InventoryFolderBase();
  414. folder.ParentID = rootFolder;
  415. folder.Owner = user;
  416. folder.ID = UUID.Random();
  417. folder.Name = "Clothing";
  418. folder.Type = (short)AssetType.Clothing;
  419. folder.Version = 1;
  420. Folders.Add(folder.ID, folder);
  421. folder = new InventoryFolderBase();
  422. folder.ParentID = rootFolder;
  423. folder.Owner = user;
  424. folder.ID = UUID.Random();
  425. folder.Name = "Gestures";
  426. folder.Type = (short)AssetType.Gesture;
  427. folder.Version = 1;
  428. Folders.Add(folder.ID, folder);
  429. folder = new InventoryFolderBase();
  430. folder.ParentID = rootFolder;
  431. folder.Owner = user;
  432. folder.ID = UUID.Random();
  433. folder.Name = "Landmarks";
  434. folder.Type = (short)AssetType.Landmark;
  435. folder.Version = 1;
  436. Folders.Add(folder.ID, folder);
  437. folder = new InventoryFolderBase();
  438. folder.ParentID = rootFolder;
  439. folder.Owner = user;
  440. folder.ID = UUID.Random();
  441. folder.Name = "Lost And Found";
  442. folder.Type = (short)AssetType.LostAndFoundFolder;
  443. folder.Version = 1;
  444. Folders.Add(folder.ID, folder);
  445. folder = new InventoryFolderBase();
  446. folder.ParentID = rootFolder;
  447. folder.Owner = user;
  448. folder.ID = UUID.Random();
  449. folder.Name = "Notecards";
  450. folder.Type = (short)AssetType.Notecard;
  451. folder.Version = 1;
  452. Folders.Add(folder.ID, folder);
  453. folder = new InventoryFolderBase();
  454. folder.ParentID = rootFolder;
  455. folder.Owner = user;
  456. folder.ID = UUID.Random();
  457. folder.Name = "Objects";
  458. folder.Type = (short)AssetType.Object;
  459. folder.Version = 1;
  460. Folders.Add(folder.ID, folder);
  461. folder = new InventoryFolderBase();
  462. folder.ParentID = rootFolder;
  463. folder.Owner = user;
  464. folder.ID = UUID.Random();
  465. folder.Name = "Photo Album";
  466. folder.Type = (short)AssetType.SnapshotFolder;
  467. folder.Version = 1;
  468. Folders.Add(folder.ID, folder);
  469. folder = new InventoryFolderBase();
  470. folder.ParentID = rootFolder;
  471. folder.Owner = user;
  472. folder.ID = UUID.Random();
  473. folder.Name = "Scripts";
  474. folder.Type = (short)AssetType.LSLText;
  475. folder.Version = 1;
  476. Folders.Add(folder.ID, folder);
  477. folder = new InventoryFolderBase();
  478. folder.ParentID = rootFolder;
  479. folder.Owner = user;
  480. folder.ID = UUID.Random();
  481. folder.Name = "Sounds";
  482. folder.Type = (short)AssetType.Sound;
  483. folder.Version = 1;
  484. Folders.Add(folder.ID, folder);
  485. folder = new InventoryFolderBase();
  486. folder.ParentID = rootFolder;
  487. folder.Owner = user;
  488. folder.ID = UUID.Random();
  489. folder.Name = "Textures";
  490. folder.Type = (short)AssetType.Texture;
  491. folder.Version = 1;
  492. Folders.Add(folder.ID, folder);
  493. folder = new InventoryFolderBase();
  494. folder.ParentID = rootFolder;
  495. folder.Owner = user;
  496. folder.ID = UUID.Random();
  497. folder.Name = "Trash";
  498. folder.Type = (short)AssetType.TrashFolder;
  499. folder.Version = 1;
  500. Folders.Add(folder.ID, folder);
  501. }
  502. }
  503. }
  504. }