InventoryFolderImpl.cs 15 KB

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