InventoryFolderImpl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 OpenMetaverse;
  32. namespace OpenSim.Framework
  33. {
  34. public class InventoryFolderImpl : InventoryFolderBase
  35. {
  36. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  37. public static readonly string PATH_DELIMITER = "/";
  38. /// <summary>
  39. /// Items that are contained in this folder
  40. /// </summary>
  41. public Dictionary<UUID, InventoryItemBase> Items = new Dictionary<UUID, InventoryItemBase>();
  42. /// <summary>
  43. /// Child folders that are contained in this folder
  44. /// </summary>
  45. protected Dictionary<UUID, InventoryFolderImpl> m_childFolders = new Dictionary<UUID, InventoryFolderImpl>();
  46. // Constructors
  47. public InventoryFolderImpl(InventoryFolderBase folderbase)
  48. {
  49. Owner = folderbase.Owner;
  50. ID = folderbase.ID;
  51. Name = folderbase.Name;
  52. ParentID = folderbase.ParentID;
  53. Type = folderbase.Type;
  54. Version = folderbase.Version;
  55. }
  56. public InventoryFolderImpl()
  57. {
  58. }
  59. /// <summary>
  60. /// Create a new subfolder.
  61. /// </summary>
  62. /// <param name="folderID"></param>
  63. /// <param name="folderName"></param>
  64. /// <param name="type"></param>
  65. /// <returns>The newly created subfolder. Returns null if the folder already exists</returns>
  66. public InventoryFolderImpl CreateChildFolder(UUID folderID, string folderName, ushort type)
  67. {
  68. lock (m_childFolders)
  69. {
  70. if (!m_childFolders.ContainsKey(folderID))
  71. {
  72. InventoryFolderImpl subFold = new InventoryFolderImpl()
  73. {
  74. Name = folderName,
  75. ID = folderID,
  76. Type = (short)type,
  77. ParentID = this.ID,
  78. Owner = Owner
  79. };
  80. m_childFolders.Add(subFold.ID, subFold);
  81. return subFold;
  82. }
  83. }
  84. return null;
  85. }
  86. /// <summary>
  87. /// Add a folder that already exists.
  88. /// </summary>
  89. /// <param name="folder"></param>
  90. public void AddChildFolder(InventoryFolderImpl folder)
  91. {
  92. lock (m_childFolders)
  93. {
  94. folder.ParentID = ID;
  95. m_childFolders[folder.ID] = folder;
  96. }
  97. }
  98. /// <summary>
  99. /// Does this folder contain the given child folder?
  100. /// </summary>
  101. /// <param name="folderID"></param>
  102. /// <returns></returns>
  103. public bool ContainsChildFolder(UUID folderID)
  104. {
  105. return m_childFolders.ContainsKey(folderID);
  106. }
  107. /// <summary>
  108. /// Get a child folder
  109. /// </summary>
  110. /// <param name="folderID"></param>
  111. /// <returns>The folder if it exists, null if it doesn't</returns>
  112. public InventoryFolderImpl GetChildFolder(UUID folderID)
  113. {
  114. lock (m_childFolders)
  115. {
  116. m_childFolders.TryGetValue(folderID, out InventoryFolderImpl folder);
  117. return folder;
  118. }
  119. }
  120. /// <summary>
  121. /// Removes the given child subfolder.
  122. /// </summary>
  123. /// <param name="folderID"></param>
  124. /// <returns>
  125. /// The folder removed, or null if the folder was not present.
  126. /// </returns>
  127. public InventoryFolderImpl RemoveChildFolder(UUID folderID)
  128. {
  129. lock (m_childFolders)
  130. {
  131. if (m_childFolders.TryGetValue(folderID, out InventoryFolderImpl removedFolder))
  132. {
  133. m_childFolders.Remove(folderID);
  134. return removedFolder;
  135. }
  136. }
  137. return null;
  138. }
  139. /// <summary>
  140. /// Delete all the folders and items in this folder.
  141. /// </summary>
  142. public void Purge()
  143. {
  144. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  145. {
  146. folder.Purge();
  147. }
  148. m_childFolders.Clear();
  149. Items.Clear();
  150. }
  151. /// <summary>
  152. /// Returns the item if it exists in this folder or in any of this folder's descendant folders
  153. /// </summary>
  154. /// <param name="itemID"></param>
  155. /// <returns>null if the item is not found</returns>
  156. public InventoryItemBase FindItem(UUID itemID)
  157. {
  158. lock (Items)
  159. {
  160. if (Items.TryGetValue(itemID, out InventoryItemBase it))
  161. return it;
  162. }
  163. lock (m_childFolders)
  164. {
  165. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  166. {
  167. InventoryItemBase item = folder.FindItem(itemID);
  168. if (item != null)
  169. return item;
  170. }
  171. }
  172. return null;
  173. }
  174. public InventoryItemBase FindAsset(UUID assetID)
  175. {
  176. lock (Items)
  177. {
  178. foreach (InventoryItemBase item in Items.Values)
  179. {
  180. if (item.AssetID.Equals(assetID))
  181. return item;
  182. }
  183. }
  184. lock (m_childFolders)
  185. {
  186. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  187. {
  188. InventoryItemBase item = folder.FindAsset(assetID);
  189. if (item != null)
  190. return item;
  191. }
  192. }
  193. return null;
  194. }
  195. /// <summary>
  196. /// Deletes an item if it exists in this folder or any children
  197. /// </summary>
  198. /// <param name="folderID"></param>
  199. /// <returns></returns>
  200. public bool DeleteItem(UUID itemID)
  201. {
  202. lock (Items)
  203. {
  204. if (Items.Remove(itemID))
  205. return true;
  206. }
  207. lock (m_childFolders)
  208. {
  209. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  210. {
  211. if(folder.DeleteItem(itemID))
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. /// <summary>
  218. /// Returns the folder requested if it is this folder or is a descendent of this folder. The search is depth
  219. /// first.
  220. /// </summary>
  221. /// <returns>The requested folder if it exists, null if it does not.</returns>
  222. public InventoryFolderImpl FindFolder(UUID folderID)
  223. {
  224. if (folderID.Equals(ID))
  225. return this;
  226. lock (m_childFolders)
  227. {
  228. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  229. {
  230. InventoryFolderImpl returnFolder = folder.FindFolder(folderID);
  231. if (returnFolder != null)
  232. return returnFolder;
  233. }
  234. }
  235. return null;
  236. }
  237. /// <summary>
  238. /// Look through all child subfolders for a folder marked as one for a particular asset type, and return it.
  239. /// </summary>
  240. /// <param name="type"></param>
  241. /// <returns>Returns null if no such folder is found</returns>
  242. public InventoryFolderImpl FindFolderForType(int type)
  243. {
  244. lock (m_childFolders)
  245. {
  246. foreach (InventoryFolderImpl f in m_childFolders.Values)
  247. {
  248. if (f.Type == type)
  249. return f;
  250. }
  251. }
  252. return null;
  253. }
  254. /// <summary>
  255. /// Find a folder given a PATH_DELIMITER delimited path starting from this folder
  256. /// </summary>
  257. ///
  258. /// This method does not handle paths that contain multiple delimitors
  259. ///
  260. /// FIXME: We do not yet handle situations where folders have the same name. We could handle this by some
  261. /// XPath like expression
  262. ///
  263. /// FIXME: Delimitors which occur in names themselves are not currently escapable.
  264. ///
  265. /// <param name="path">
  266. /// The path to the required folder.
  267. /// It this is empty or consists only of the PATH_DELIMTER then this folder itself is returned.
  268. /// </param>
  269. /// <returns>null if the folder is not found</returns>
  270. public InventoryFolderImpl FindFolderByPath(string path)
  271. {
  272. path = path.Trim();
  273. if (path.Length == 0)
  274. return this;
  275. if (path == PATH_DELIMITER)
  276. return this;
  277. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  278. lock (m_childFolders)
  279. {
  280. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  281. {
  282. if (folder.Name == components[0])
  283. if (components.Length > 1)
  284. return folder.FindFolderByPath(components[1]);
  285. else
  286. return folder;
  287. }
  288. }
  289. // We didn't find a folder with the given name
  290. return null;
  291. }
  292. /// <summary>
  293. /// Find an item given a PATH_DELIMITOR delimited path starting from this folder.
  294. ///
  295. /// This method does not handle paths that contain multiple delimitors
  296. ///
  297. /// FIXME: We do not yet handle situations where folders or items have the same name. We could handle this by some
  298. /// XPath like expression
  299. ///
  300. /// FIXME: Delimitors which occur in names themselves are not currently escapable.
  301. /// </summary>
  302. /// <param name="path">
  303. /// The path to the required item.
  304. /// </param>
  305. /// <returns>null if the item is not found</returns>
  306. public InventoryItemBase FindItemByPath(string path)
  307. {
  308. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  309. if (components.Length == 1)
  310. {
  311. lock (Items)
  312. {
  313. foreach (InventoryItemBase item in Items.Values)
  314. {
  315. if (item.Name == components[0])
  316. return item;
  317. }
  318. }
  319. }
  320. else
  321. {
  322. lock (m_childFolders)
  323. {
  324. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  325. {
  326. if (folder.Name == components[0])
  327. return folder.FindItemByPath(components[1]);
  328. }
  329. }
  330. }
  331. // We didn't find an item or intermediate folder with the given name
  332. return null;
  333. }
  334. /// <summary>
  335. /// Return a copy of the list of child items in this folder. The items themselves are the originals.
  336. /// </summary>
  337. public List<InventoryItemBase> RequestListOfItems()
  338. {
  339. List<InventoryItemBase> itemList = new List<InventoryItemBase>();
  340. lock (Items)
  341. {
  342. foreach (InventoryItemBase item in Items.Values)
  343. {
  344. //m_log.DebugFormat(
  345. // "[INVENTORY FOLDER IMPL]: Returning item {0} {1}, OwnerPermissions {2:X}",
  346. // item.Name, item.ID, item.CurrentPermissions);
  347. itemList.Add(item);
  348. }
  349. }
  350. //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
  351. return itemList;
  352. }
  353. /// <summary>
  354. /// Return a copy of the list of child folders in this folder. The folders themselves are the originals.
  355. /// </summary>
  356. public List<InventoryFolderBase> RequestListOfFolders()
  357. {
  358. List<InventoryFolderBase> folderList = new List<InventoryFolderBase>(m_childFolders.Count);
  359. lock (m_childFolders)
  360. {
  361. foreach (InventoryFolderBase folder in m_childFolders.Values)
  362. {
  363. folderList.Add(folder);
  364. }
  365. }
  366. return folderList;
  367. }
  368. public List<InventoryFolderImpl> RequestListOfFolderImpls()
  369. {
  370. List<InventoryFolderImpl> folderList = new List<InventoryFolderImpl>(m_childFolders.Count);
  371. lock (m_childFolders)
  372. {
  373. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  374. {
  375. folderList.Add(folder);
  376. }
  377. }
  378. return folderList;
  379. }
  380. /// <value>
  381. /// The total number of items in this folder and in the immediate child folders (though not from other
  382. /// descendants).
  383. /// </value>
  384. public int TotalCount
  385. {
  386. get
  387. {
  388. int total = Items.Count;
  389. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  390. {
  391. total += folder.TotalCount;
  392. }
  393. return total;
  394. }
  395. }
  396. }
  397. }