InventoryFolderImpl.cs 15 KB

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