InventoryFolderImpl.cs 7.9 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 CreateNewSubFolder(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. /// Does this folder or any of its subfolders contain the given item?
  96. /// </summary>
  97. /// <param name="itemID"></param>
  98. /// <returns></returns>
  99. public InventoryItemBase HasItem(LLUUID itemID)
  100. {
  101. InventoryItemBase base2 = null;
  102. lock (Items)
  103. {
  104. if (Items.ContainsKey(itemID))
  105. {
  106. return Items[itemID];
  107. }
  108. }
  109. lock (SubFolders)
  110. {
  111. foreach (InventoryFolderImpl folder in SubFolders.Values)
  112. {
  113. base2 = folder.HasItem(itemID);
  114. if (base2 != null)
  115. {
  116. break;
  117. }
  118. }
  119. }
  120. return base2;
  121. }
  122. /// <summary>
  123. /// Delete an item from the folder.
  124. /// </summary>
  125. /// <param name="folderID"></param>
  126. /// <returns></returns>
  127. public bool DeleteItem(LLUUID itemID)
  128. {
  129. bool found = false;
  130. lock (Items)
  131. {
  132. if (Items.ContainsKey(itemID))
  133. {
  134. Items.Remove(itemID);
  135. return true;
  136. }
  137. }
  138. lock (SubFolders)
  139. {
  140. foreach (InventoryFolderImpl folder in SubFolders.Values)
  141. {
  142. found = folder.DeleteItem(itemID);
  143. if (found == true)
  144. {
  145. break;
  146. }
  147. }
  148. }
  149. return found;
  150. }
  151. /// <summary>
  152. /// Returns the folder requested if it exists as a descendent of this folder
  153. /// </summary>
  154. /// <returns>The requested folder if it exists, null if it does not.</returns>
  155. public InventoryFolderImpl GetDescendentFolder(LLUUID folderID)
  156. {
  157. InventoryFolderImpl returnFolder = null;
  158. lock (SubFolders)
  159. {
  160. if (SubFolders.ContainsKey(folderID))
  161. {
  162. returnFolder = SubFolders[folderID];
  163. }
  164. else
  165. {
  166. foreach (InventoryFolderImpl folder in SubFolders.Values)
  167. {
  168. returnFolder = folder.GetDescendentFolder(folderID);
  169. if (returnFolder != null)
  170. {
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. return returnFolder;
  177. }
  178. /// <summary>
  179. /// Return the list of items in this folder
  180. /// </summary>
  181. public List<InventoryItemBase> RequestListOfItems()
  182. {
  183. List<InventoryItemBase> itemList = new List<InventoryItemBase>();
  184. lock (Items)
  185. {
  186. foreach (InventoryItemBase item in Items.Values)
  187. {
  188. itemList.Add(item);
  189. }
  190. }
  191. //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
  192. return itemList;
  193. }
  194. /// <summary>
  195. /// Return the list of immediate child folders in this folder.
  196. /// </summary>
  197. public List<InventoryFolderBase> RequestListOfFolders()
  198. {
  199. List<InventoryFolderBase> folderList = new List<InventoryFolderBase>();
  200. lock (SubFolders)
  201. {
  202. foreach (InventoryFolderBase folder in SubFolders.Values)
  203. {
  204. folderList.Add(folder);
  205. }
  206. }
  207. return folderList;
  208. }
  209. }
  210. }