1
0

InventoryFolderImpl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_DELIMITER delimited path starting from this folder
  269. /// </summary>
  270. ///
  271. /// This method does not handle paths that contain multiple delimitors
  272. ///
  273. /// FIXME: We do not yet handle situations where folders have the same name. We could handle this by some
  274. /// XPath like expression
  275. ///
  276. /// FIXME: Delimitors which occur in names themselves are not currently escapable.
  277. ///
  278. /// <param name="path">
  279. /// The path to the required folder.
  280. /// It this is empty or consists only of the PATH_DELIMTER then this folder itself is returned.
  281. /// </param>
  282. /// <returns>null if the folder is not found</returns>
  283. public InventoryFolderImpl FindFolderByPath(string path)
  284. {
  285. if (path == string.Empty)
  286. return this;
  287. path = path.Trim();
  288. if (path == PATH_DELIMITER)
  289. return this;
  290. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  291. lock (m_childFolders)
  292. {
  293. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  294. {
  295. if (folder.Name == components[0])
  296. if (components.Length > 1)
  297. return folder.FindFolderByPath(components[1]);
  298. else
  299. return folder;
  300. }
  301. }
  302. // We didn't find a folder with the given name
  303. return null;
  304. }
  305. /// <summary>
  306. /// Find an item given a PATH_DELIMITOR delimited path starting from this folder.
  307. ///
  308. /// This method does not handle paths that contain multiple delimitors
  309. ///
  310. /// FIXME: We do not yet handle situations where folders or items have the same name. We could handle this by some
  311. /// XPath like expression
  312. ///
  313. /// FIXME: Delimitors which occur in names themselves are not currently escapable.
  314. /// </summary>
  315. /// <param name="path">
  316. /// The path to the required item.
  317. /// </param>
  318. /// <returns>null if the item is not found</returns>
  319. public InventoryItemBase FindItemByPath(string path)
  320. {
  321. string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
  322. if (components.Length == 1)
  323. {
  324. lock (Items)
  325. {
  326. foreach (InventoryItemBase item in Items.Values)
  327. {
  328. if (item.Name == components[0])
  329. return item;
  330. }
  331. }
  332. }
  333. else
  334. {
  335. lock (m_childFolders)
  336. {
  337. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  338. {
  339. if (folder.Name == components[0])
  340. return folder.FindItemByPath(components[1]);
  341. }
  342. }
  343. }
  344. // We didn't find an item or intermediate folder with the given name
  345. return null;
  346. }
  347. /// <summary>
  348. /// Return a copy of the list of child items in this folder. The items themselves are the originals.
  349. /// </summary>
  350. public List<InventoryItemBase> RequestListOfItems()
  351. {
  352. List<InventoryItemBase> itemList = new List<InventoryItemBase>();
  353. lock (Items)
  354. {
  355. foreach (InventoryItemBase item in Items.Values)
  356. {
  357. itemList.Add(item);
  358. }
  359. }
  360. //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
  361. return itemList;
  362. }
  363. /// <summary>
  364. /// Return a copy of the list of child folders in this folder. The folders themselves are the originals.
  365. /// </summary>
  366. public List<InventoryFolderBase> RequestListOfFolders()
  367. {
  368. List<InventoryFolderBase> folderList = new List<InventoryFolderBase>();
  369. lock (m_childFolders)
  370. {
  371. foreach (InventoryFolderBase folder in m_childFolders.Values)
  372. {
  373. folderList.Add(folder);
  374. }
  375. }
  376. return folderList;
  377. }
  378. public List<InventoryFolderImpl> RequestListOfFolderImpls()
  379. {
  380. List<InventoryFolderImpl> folderList = new List<InventoryFolderImpl>();
  381. lock (m_childFolders)
  382. {
  383. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  384. {
  385. folderList.Add(folder);
  386. }
  387. }
  388. return folderList;
  389. }
  390. /// <value>
  391. /// The total number of items in this folder and in the immediate child folders (though not from other
  392. /// descendants).
  393. /// </value>
  394. public int TotalCount
  395. {
  396. get
  397. {
  398. int total = Items.Count;
  399. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  400. {
  401. total = total + folder.TotalCount;
  402. }
  403. return total;
  404. }
  405. }
  406. }
  407. }