InventoryFolderImpl.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Collections.Generic;
  28. using libsecondlife;
  29. //using System.Reflection;
  30. //using log4net;
  31. namespace OpenSim.Framework.Communications.Cache
  32. {
  33. public class InventoryFolderImpl : InventoryFolderBase
  34. {
  35. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  36. // Fields
  37. public Dictionary<LLUUID, InventoryItemBase> Items = new Dictionary<LLUUID, InventoryItemBase>();
  38. public Dictionary<LLUUID, InventoryFolderImpl> SubFolders = new Dictionary<LLUUID, InventoryFolderImpl>();
  39. // Accessors
  40. public int SubFoldersCount
  41. {
  42. get { return SubFolders.Count; }
  43. }
  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. This exists only in the cache.
  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(LLUUID folderID, string folderName, ushort type)
  65. {
  66. lock (SubFolders)
  67. {
  68. if (!SubFolders.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. SubFolders.Add(subFold.ID, subFold);
  77. return subFold;
  78. }
  79. }
  80. return null;
  81. }
  82. /// <summary>
  83. /// Delete all the folders and items in this folder.
  84. /// </summary>
  85. public void Purge()
  86. {
  87. foreach (InventoryFolderImpl folder in SubFolders.Values)
  88. {
  89. folder.Purge();
  90. }
  91. SubFolders.Clear();
  92. Items.Clear();
  93. }
  94. /// <summary>
  95. /// Returns the item if it exists in this folder or any of this folder's subfolders?
  96. /// </summary>
  97. /// <param name="itemID"></param>
  98. /// <returns>null if the item is not found</returns>
  99. public InventoryItemBase FindItem(LLUUID itemID)
  100. {
  101. lock (Items)
  102. {
  103. if (Items.ContainsKey(itemID))
  104. {
  105. return Items[itemID];
  106. }
  107. }
  108. lock (SubFolders)
  109. {
  110. foreach (InventoryFolderImpl folder in SubFolders.Values)
  111. {
  112. InventoryItemBase item = folder.FindItem(itemID);
  113. if (item != null)
  114. {
  115. return item;
  116. }
  117. }
  118. }
  119. return null;
  120. }
  121. /// <summary>
  122. /// Deletes an item if it exists in this folder or any children
  123. /// </summary>
  124. /// <param name="folderID"></param>
  125. /// <returns></returns>
  126. public bool DeleteItem(LLUUID itemID)
  127. {
  128. bool found = false;
  129. lock (Items)
  130. {
  131. if (Items.ContainsKey(itemID))
  132. {
  133. Items.Remove(itemID);
  134. return true;
  135. }
  136. }
  137. lock (SubFolders)
  138. {
  139. foreach (InventoryFolderImpl folder in SubFolders.Values)
  140. {
  141. found = folder.DeleteItem(itemID);
  142. if (found == true)
  143. {
  144. break;
  145. }
  146. }
  147. }
  148. return found;
  149. }
  150. /// <summary>
  151. /// Returns the folder requested if it is this folder or is a descendent of this folder. The search is depth
  152. /// first.
  153. /// </summary>
  154. /// <returns>The requested folder if it exists, null if it does not.</returns>
  155. public InventoryFolderImpl FindFolder(LLUUID folderID)
  156. {
  157. if (folderID == ID)
  158. {
  159. return this;
  160. }
  161. lock (SubFolders)
  162. {
  163. foreach (InventoryFolderImpl folder in SubFolders.Values)
  164. {
  165. InventoryFolderImpl returnFolder = folder.FindFolder(folderID);
  166. if (returnFolder != null)
  167. {
  168. return returnFolder;
  169. }
  170. }
  171. }
  172. return null;
  173. }
  174. /// <summary>
  175. /// Return the list of child items in this folder
  176. /// </summary>
  177. public List<InventoryItemBase> RequestListOfItems()
  178. {
  179. List<InventoryItemBase> itemList = new List<InventoryItemBase>();
  180. lock (Items)
  181. {
  182. foreach (InventoryItemBase item in Items.Values)
  183. {
  184. itemList.Add(item);
  185. }
  186. }
  187. //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
  188. return itemList;
  189. }
  190. /// <summary>
  191. /// Return the list of immediate child folders in this folder.
  192. /// </summary>
  193. public List<InventoryFolderBase> RequestListOfFolders()
  194. {
  195. List<InventoryFolderBase> folderList = new List<InventoryFolderBase>();
  196. lock (SubFolders)
  197. {
  198. foreach (InventoryFolderBase folder in SubFolders.Values)
  199. {
  200. folderList.Add(folder);
  201. }
  202. }
  203. return folderList;
  204. }
  205. }
  206. }