InventoryFolderImpl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. 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. // m_log.DebugFormat(
  358. // "[INVENTORY FOLDER IMPL]: Returning item {0} {1}, OwnerPermissions {2:X}",
  359. // item.Name, item.ID, item.CurrentPermissions);
  360. itemList.Add(item);
  361. }
  362. }
  363. //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
  364. return itemList;
  365. }
  366. /// <summary>
  367. /// Return a copy of the list of child folders in this folder. The folders themselves are the originals.
  368. /// </summary>
  369. public List<InventoryFolderBase> RequestListOfFolders()
  370. {
  371. List<InventoryFolderBase> folderList = new List<InventoryFolderBase>();
  372. lock (m_childFolders)
  373. {
  374. foreach (InventoryFolderBase folder in m_childFolders.Values)
  375. {
  376. folderList.Add(folder);
  377. }
  378. }
  379. return folderList;
  380. }
  381. public List<InventoryFolderImpl> RequestListOfFolderImpls()
  382. {
  383. List<InventoryFolderImpl> folderList = new List<InventoryFolderImpl>();
  384. lock (m_childFolders)
  385. {
  386. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  387. {
  388. folderList.Add(folder);
  389. }
  390. }
  391. return folderList;
  392. }
  393. /// <value>
  394. /// The total number of items in this folder and in the immediate child folders (though not from other
  395. /// descendants).
  396. /// </value>
  397. public int TotalCount
  398. {
  399. get
  400. {
  401. int total = Items.Count;
  402. foreach (InventoryFolderImpl folder in m_childFolders.Values)
  403. {
  404. total = total + folder.TotalCount;
  405. }
  406. return total;
  407. }
  408. }
  409. }
  410. }