InventoryServiceBase.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Data;
  32. namespace OpenSim.Framework.Communications
  33. {
  34. /// <summary>
  35. /// Abstract base class used by local and grid implementations of an inventory service.
  36. /// </summary>
  37. public abstract class InventoryServiceBase : IInventoryServices, IInterServiceInventoryServices
  38. {
  39. private static readonly ILog m_log
  40. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. protected List<IInventoryDataPlugin> m_plugins = new List<IInventoryDataPlugin>();
  42. #region Plugin methods
  43. /// <summary>
  44. /// Add a new inventory data plugin - plugins will be requested in the order they were added.
  45. /// </summary>
  46. /// <param name="plugin">The plugin that will provide data</param>
  47. public void AddPlugin(IInventoryDataPlugin plugin)
  48. {
  49. m_plugins.Add(plugin);
  50. }
  51. /// <summary>
  52. /// Adds a list of inventory data plugins, as described by `provider'
  53. /// and `connect', to `m_plugins'.
  54. /// </summary>
  55. /// <param name="provider">
  56. /// The filename of the inventory server plugin DLL.
  57. /// </param>
  58. /// <param name="connect">
  59. /// The connection string for the storage backend.
  60. /// </param>
  61. public void AddPlugin(string provider, string connect)
  62. {
  63. m_plugins.AddRange(DataPluginFactory.LoadDataPlugins<IInventoryDataPlugin>(provider, connect));
  64. }
  65. #endregion
  66. #region IInventoryServices methods
  67. public string Host
  68. {
  69. get { return "default"; }
  70. }
  71. public List<InventoryFolderBase> GetInventorySkeleton(UUID userId)
  72. {
  73. // m_log.DebugFormat("[AGENT INVENTORY]: Getting inventory skeleton for {0}", userId);
  74. InventoryFolderBase rootFolder = RequestRootFolder(userId);
  75. // Agent has no inventory structure yet.
  76. if (null == rootFolder)
  77. {
  78. return null;
  79. }
  80. List<InventoryFolderBase> userFolders = new List<InventoryFolderBase>();
  81. userFolders.Add(rootFolder);
  82. foreach (IInventoryDataPlugin plugin in m_plugins)
  83. {
  84. IList<InventoryFolderBase> folders = plugin.getFolderHierarchy(rootFolder.ID);
  85. userFolders.AddRange(folders);
  86. }
  87. // foreach (InventoryFolderBase folder in userFolders)
  88. // {
  89. // m_log.DebugFormat("[AGENT INVENTORY]: Got folder {0} {1}", folder.name, folder.folderID);
  90. // }
  91. return userFolders;
  92. }
  93. // See IInventoryServices
  94. public virtual bool HasInventoryForUser(UUID userID)
  95. {
  96. return false;
  97. }
  98. // See IInventoryServices
  99. public virtual InventoryFolderBase RequestRootFolder(UUID userID)
  100. {
  101. // FIXME: Probably doesn't do what was originally intended - only ever queries the first plugin
  102. foreach (IInventoryDataPlugin plugin in m_plugins)
  103. {
  104. return plugin.getUserRootFolder(userID);
  105. }
  106. return null;
  107. }
  108. // See IInventoryServices
  109. public bool CreateNewUserInventory(UUID user)
  110. {
  111. InventoryFolderBase existingRootFolder = RequestRootFolder(user);
  112. if (null != existingRootFolder)
  113. {
  114. m_log.WarnFormat(
  115. "[AGENT INVENTORY]: Did not create a new inventory for user {0} since they already have "
  116. + "a root inventory folder with id {1}",
  117. user, existingRootFolder.ID);
  118. }
  119. else
  120. {
  121. UsersInventory inven = new UsersInventory();
  122. inven.CreateNewInventorySet(user);
  123. AddNewInventorySet(inven);
  124. return true;
  125. }
  126. return false;
  127. }
  128. // See IInventoryServices
  129. public abstract void RequestInventoryForUser(UUID userID, InventoryReceiptCallback callback);
  130. public List<InventoryItemBase> GetActiveGestures(UUID userId)
  131. {
  132. foreach (IInventoryDataPlugin plugin in m_plugins)
  133. {
  134. return plugin.fetchActiveGestures(userId);
  135. }
  136. return new List<InventoryItemBase>();
  137. }
  138. #endregion
  139. #region Methods used by GridInventoryService
  140. public List<InventoryFolderBase> RequestSubFolders(UUID parentFolderID)
  141. {
  142. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  143. foreach (IInventoryDataPlugin plugin in m_plugins)
  144. {
  145. return plugin.getInventoryFolders(parentFolderID);
  146. }
  147. return inventoryList;
  148. }
  149. public List<InventoryItemBase> RequestFolderItems(UUID folderID)
  150. {
  151. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  152. foreach (IInventoryDataPlugin plugin in m_plugins)
  153. {
  154. itemsList = plugin.getInventoryInFolder(folderID);
  155. return itemsList;
  156. }
  157. return itemsList;
  158. }
  159. #endregion
  160. // See IInventoryServices
  161. public virtual bool AddFolder(InventoryFolderBase folder)
  162. {
  163. m_log.DebugFormat(
  164. "[AGENT INVENTORY]: Adding folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  165. foreach (IInventoryDataPlugin plugin in m_plugins)
  166. {
  167. plugin.addInventoryFolder(folder);
  168. }
  169. // FIXME: Should return false on failure
  170. return true;
  171. }
  172. // See IInventoryServices
  173. public virtual bool UpdateFolder(InventoryFolderBase folder)
  174. {
  175. m_log.DebugFormat(
  176. "[AGENT INVENTORY]: Updating folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  177. foreach (IInventoryDataPlugin plugin in m_plugins)
  178. {
  179. plugin.updateInventoryFolder(folder);
  180. }
  181. // FIXME: Should return false on failure
  182. return true;
  183. }
  184. // See IInventoryServices
  185. public virtual bool MoveFolder(InventoryFolderBase folder)
  186. {
  187. m_log.DebugFormat(
  188. "[AGENT INVENTORY]: Moving folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  189. foreach (IInventoryDataPlugin plugin in m_plugins)
  190. {
  191. plugin.moveInventoryFolder(folder);
  192. }
  193. // FIXME: Should return false on failure
  194. return true;
  195. }
  196. // See IInventoryServices
  197. public virtual bool AddItem(InventoryItemBase item)
  198. {
  199. m_log.DebugFormat(
  200. "[AGENT INVENTORY]: Adding item {0} {1} to folder {2}", item.Name, item.ID, item.Folder);
  201. foreach (IInventoryDataPlugin plugin in m_plugins)
  202. {
  203. plugin.addInventoryItem(item);
  204. }
  205. // FIXME: Should return false on failure
  206. return true;
  207. }
  208. // See IInventoryServices
  209. public virtual bool UpdateItem(InventoryItemBase item)
  210. {
  211. m_log.InfoFormat(
  212. "[AGENT INVENTORY]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  213. foreach (IInventoryDataPlugin plugin in m_plugins)
  214. {
  215. plugin.updateInventoryItem(item);
  216. }
  217. // FIXME: Should return false on failure
  218. return true;
  219. }
  220. // See IInventoryServices
  221. public virtual bool DeleteItem(InventoryItemBase item)
  222. {
  223. m_log.InfoFormat(
  224. "[AGENT INVENTORY]: Deleting item {0} {1} from folder {2}", item.Name, item.ID, item.Folder);
  225. foreach (IInventoryDataPlugin plugin in m_plugins)
  226. {
  227. plugin.deleteInventoryItem(item.ID);
  228. }
  229. // FIXME: Should return false on failure
  230. return true;
  231. }
  232. /// <summary>
  233. /// Purge a folder of all items items and subfolders.
  234. ///
  235. /// FIXME: Really nasty in a sense, because we have to query the database to get information we may
  236. /// already know... Needs heavy refactoring.
  237. /// </summary>
  238. /// <param name="folder"></param>
  239. public virtual bool PurgeFolder(InventoryFolderBase folder)
  240. {
  241. m_log.DebugFormat(
  242. "[AGENT INVENTORY]: Purging folder {0} {1} of its contents", folder.Name, folder.ID);
  243. List<InventoryFolderBase> subFolders = RequestSubFolders(folder.ID);
  244. foreach (InventoryFolderBase subFolder in subFolders)
  245. {
  246. // m_log.DebugFormat("[AGENT INVENTORY]: Deleting folder {0} {1}", subFolder.Name, subFolder.ID);
  247. foreach (IInventoryDataPlugin plugin in m_plugins)
  248. {
  249. plugin.deleteInventoryFolder(subFolder.ID);
  250. }
  251. }
  252. List<InventoryItemBase> items = RequestFolderItems(folder.ID);
  253. foreach (InventoryItemBase item in items)
  254. {
  255. DeleteItem(item);
  256. }
  257. // FIXME: Should return false on failure
  258. return true;
  259. }
  260. private void AddNewInventorySet(UsersInventory inventory)
  261. {
  262. foreach (InventoryFolderBase folder in inventory.Folders.Values)
  263. {
  264. AddFolder(folder);
  265. }
  266. }
  267. /// <summary>
  268. /// Used to create a new user inventory.
  269. /// </summary>
  270. private class UsersInventory
  271. {
  272. public Dictionary<UUID, InventoryFolderBase> Folders = new Dictionary<UUID, InventoryFolderBase>();
  273. public Dictionary<UUID, InventoryItemBase> Items = new Dictionary<UUID, InventoryItemBase>();
  274. public virtual void CreateNewInventorySet(UUID user)
  275. {
  276. InventoryFolderBase folder = new InventoryFolderBase();
  277. folder.ParentID = UUID.Zero;
  278. folder.Owner = user;
  279. folder.ID = UUID.Random();
  280. folder.Name = "My Inventory";
  281. folder.Type = (short)AssetType.Folder;
  282. folder.Version = 1;
  283. Folders.Add(folder.ID, folder);
  284. UUID rootFolder = folder.ID;
  285. folder = new InventoryFolderBase();
  286. folder.ParentID = rootFolder;
  287. folder.Owner = user;
  288. folder.ID = UUID.Random();
  289. folder.Name = "Animations";
  290. folder.Type = (short)AssetType.Animation;
  291. folder.Version = 1;
  292. Folders.Add(folder.ID, folder);
  293. folder = new InventoryFolderBase();
  294. folder.ParentID = rootFolder;
  295. folder.Owner = user;
  296. folder.ID = UUID.Random();
  297. folder.Name = "Body Parts";
  298. folder.Type = (short)AssetType.Bodypart;
  299. folder.Version = 1;
  300. Folders.Add(folder.ID, folder);
  301. folder = new InventoryFolderBase();
  302. folder.ParentID = rootFolder;
  303. folder.Owner = user;
  304. folder.ID = UUID.Random();
  305. folder.Name = "Calling Cards";
  306. folder.Type = (short)AssetType.CallingCard;
  307. folder.Version = 1;
  308. Folders.Add(folder.ID, folder);
  309. folder = new InventoryFolderBase();
  310. folder.ParentID = rootFolder;
  311. folder.Owner = user;
  312. folder.ID = UUID.Random();
  313. folder.Name = "Clothing";
  314. folder.Type = (short)AssetType.Clothing;
  315. folder.Version = 1;
  316. Folders.Add(folder.ID, folder);
  317. folder = new InventoryFolderBase();
  318. folder.ParentID = rootFolder;
  319. folder.Owner = user;
  320. folder.ID = UUID.Random();
  321. folder.Name = "Gestures";
  322. folder.Type = (short)AssetType.Gesture;
  323. folder.Version = 1;
  324. Folders.Add(folder.ID, folder);
  325. folder = new InventoryFolderBase();
  326. folder.ParentID = rootFolder;
  327. folder.Owner = user;
  328. folder.ID = UUID.Random();
  329. folder.Name = "Landmarks";
  330. folder.Type = (short)AssetType.Landmark;
  331. folder.Version = 1;
  332. Folders.Add(folder.ID, folder);
  333. folder = new InventoryFolderBase();
  334. folder.ParentID = rootFolder;
  335. folder.Owner = user;
  336. folder.ID = UUID.Random();
  337. folder.Name = "Lost And Found";
  338. folder.Type = (short)AssetType.LostAndFoundFolder;
  339. folder.Version = 1;
  340. Folders.Add(folder.ID, folder);
  341. folder = new InventoryFolderBase();
  342. folder.ParentID = rootFolder;
  343. folder.Owner = user;
  344. folder.ID = UUID.Random();
  345. folder.Name = "Notecards";
  346. folder.Type = (short)AssetType.Notecard;
  347. folder.Version = 1;
  348. Folders.Add(folder.ID, folder);
  349. folder = new InventoryFolderBase();
  350. folder.ParentID = rootFolder;
  351. folder.Owner = user;
  352. folder.ID = UUID.Random();
  353. folder.Name = "Objects";
  354. folder.Type = (short)AssetType.Object;
  355. folder.Version = 1;
  356. Folders.Add(folder.ID, folder);
  357. folder = new InventoryFolderBase();
  358. folder.ParentID = rootFolder;
  359. folder.Owner = user;
  360. folder.ID = UUID.Random();
  361. folder.Name = "Photo Album";
  362. folder.Type = (short)AssetType.SnapshotFolder;
  363. folder.Version = 1;
  364. Folders.Add(folder.ID, folder);
  365. folder = new InventoryFolderBase();
  366. folder.ParentID = rootFolder;
  367. folder.Owner = user;
  368. folder.ID = UUID.Random();
  369. folder.Name = "Scripts";
  370. folder.Type = (short)AssetType.LSLText;
  371. folder.Version = 1;
  372. Folders.Add(folder.ID, folder);
  373. folder = new InventoryFolderBase();
  374. folder.ParentID = rootFolder;
  375. folder.Owner = user;
  376. folder.ID = UUID.Random();
  377. folder.Name = "Sounds";
  378. folder.Type = (short)AssetType.Sound;
  379. folder.Version = 1;
  380. Folders.Add(folder.ID, folder);
  381. folder = new InventoryFolderBase();
  382. folder.ParentID = rootFolder;
  383. folder.Owner = user;
  384. folder.ID = UUID.Random();
  385. folder.Name = "Textures";
  386. folder.Type = (short)AssetType.Texture;
  387. folder.Version = 1;
  388. Folders.Add(folder.ID, folder);
  389. folder = new InventoryFolderBase();
  390. folder.ParentID = rootFolder;
  391. folder.Owner = user;
  392. folder.ID = UUID.Random();
  393. folder.Name = "Trash";
  394. folder.Type = (short)AssetType.TrashFolder;
  395. folder.Version = 1;
  396. Folders.Add(folder.ID, folder);
  397. }
  398. }
  399. }
  400. }