OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
InventoryFolderImpl.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using log4net;
32 using OpenMetaverse;
33 
34 namespace OpenSim.Framework
35 {
37  {
38 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
39 
40  public static readonly string PATH_DELIMITER = "/";
41 
45  public Dictionary<UUID, InventoryItemBase> Items = new Dictionary<UUID, InventoryItemBase>();
46 
50  protected Dictionary<UUID, InventoryFolderImpl> m_childFolders = new Dictionary<UUID, InventoryFolderImpl>();
51 
52  // Constructors
54  {
55  Owner = folderbase.Owner;
56  ID = folderbase.ID;
57  Name = folderbase.Name;
58  ParentID = folderbase.ParentID;
59  Type = folderbase.Type;
60  Version = folderbase.Version;
61  }
62 
64  {
65  }
66 
74  public InventoryFolderImpl CreateChildFolder(UUID folderID, string folderName, ushort type)
75  {
76  lock (m_childFolders)
77  {
78  if (!m_childFolders.ContainsKey(folderID))
79  {
81  subFold.Name = folderName;
82  subFold.ID = folderID;
83  subFold.Type = (short)type;
84  subFold.ParentID = this.ID;
85  subFold.Owner = Owner;
86  m_childFolders.Add(subFold.ID, subFold);
87 
88  return subFold;
89  }
90  }
91 
92  return null;
93  }
94 
99  public void AddChildFolder(InventoryFolderImpl folder)
100  {
101  lock (m_childFolders)
102  {
103  folder.ParentID = ID;
104  m_childFolders[folder.ID] = folder;
105  }
106  }
107 
113  public bool ContainsChildFolder(UUID folderID)
114  {
115  return m_childFolders.ContainsKey(folderID);
116  }
117 
123  public InventoryFolderImpl GetChildFolder(UUID folderID)
124  {
125  InventoryFolderImpl folder = null;
126 
127  lock (m_childFolders)
128  {
129  m_childFolders.TryGetValue(folderID, out folder);
130  }
131 
132  return folder;
133  }
134 
143  {
144  InventoryFolderImpl removedFolder = null;
145 
146  lock (m_childFolders)
147  {
148  if (m_childFolders.ContainsKey(folderID))
149  {
150  removedFolder = m_childFolders[folderID];
151  m_childFolders.Remove(folderID);
152  }
153  }
154 
155  return removedFolder;
156  }
157 
161  public void Purge()
162  {
163  foreach (InventoryFolderImpl folder in m_childFolders.Values)
164  {
165  folder.Purge();
166  }
167 
168  m_childFolders.Clear();
169  Items.Clear();
170  }
171 
177  public InventoryItemBase FindItem(UUID itemID)
178  {
179  lock (Items)
180  {
181  if (Items.ContainsKey(itemID))
182  {
183  return Items[itemID];
184  }
185  }
186 
187  lock (m_childFolders)
188  {
189  foreach (InventoryFolderImpl folder in m_childFolders.Values)
190  {
191  InventoryItemBase item = folder.FindItem(itemID);
192 
193  if (item != null)
194  {
195  return item;
196  }
197  }
198  }
199 
200  return null;
201  }
202 
203  public InventoryItemBase FindAsset(UUID assetID)
204  {
205  lock (Items)
206  {
207  foreach (InventoryItemBase item in Items.Values)
208  {
209  if (item.AssetID == assetID)
210  return item;
211  }
212  }
213 
214  lock (m_childFolders)
215  {
216  foreach (InventoryFolderImpl folder in m_childFolders.Values)
217  {
218  InventoryItemBase item = folder.FindAsset(assetID);
219 
220  if (item != null)
221  {
222  return item;
223  }
224  }
225  }
226 
227  return null;
228  }
229 
235  public bool DeleteItem(UUID itemID)
236  {
237  bool found = false;
238 
239  lock (Items)
240  {
241  if (Items.ContainsKey(itemID))
242  {
243  Items.Remove(itemID);
244  return true;
245  }
246  }
247 
248  lock (m_childFolders)
249  {
250  foreach (InventoryFolderImpl folder in m_childFolders.Values)
251  {
252  found = folder.DeleteItem(itemID);
253 
254  if (found == true)
255  {
256  break;
257  }
258  }
259  }
260 
261  return found;
262  }
263 
269  public InventoryFolderImpl FindFolder(UUID folderID)
270  {
271  if (folderID == ID)
272  return this;
273 
274  lock (m_childFolders)
275  {
276  foreach (InventoryFolderImpl folder in m_childFolders.Values)
277  {
278  InventoryFolderImpl returnFolder = folder.FindFolder(folderID);
279 
280  if (returnFolder != null)
281  return returnFolder;
282  }
283  }
284 
285  return null;
286  }
287 
294  {
295  lock (m_childFolders)
296  {
297  foreach (InventoryFolderImpl f in m_childFolders.Values)
298  {
299  if (f.Type == type)
300  return f;
301  }
302  }
303 
304  return null;
305  }
306 
324  {
325  if (path == string.Empty)
326  return this;
327 
328  path = path.Trim();
329 
330  if (path == PATH_DELIMITER)
331  return this;
332 
333  string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
334 
335  lock (m_childFolders)
336  {
337  foreach (InventoryFolderImpl folder in m_childFolders.Values)
338  {
339  if (folder.Name == components[0])
340  if (components.Length > 1)
341  return folder.FindFolderByPath(components[1]);
342  else
343  return folder;
344  }
345  }
346 
347  // We didn't find a folder with the given name
348  return null;
349  }
350 
365  public InventoryItemBase FindItemByPath(string path)
366  {
367  string[] components = path.Split(new string[] { PATH_DELIMITER }, 2, StringSplitOptions.None);
368 
369  if (components.Length == 1)
370  {
371  lock (Items)
372  {
373  foreach (InventoryItemBase item in Items.Values)
374  {
375  if (item.Name == components[0])
376  return item;
377  }
378  }
379  }
380  else
381  {
382  lock (m_childFolders)
383  {
384  foreach (InventoryFolderImpl folder in m_childFolders.Values)
385  {
386  if (folder.Name == components[0])
387  return folder.FindItemByPath(components[1]);
388  }
389  }
390  }
391 
392  // We didn't find an item or intermediate folder with the given name
393  return null;
394  }
395 
399  public List<InventoryItemBase> RequestListOfItems()
400  {
401  List<InventoryItemBase> itemList = new List<InventoryItemBase>();
402 
403  lock (Items)
404  {
405  foreach (InventoryItemBase item in Items.Values)
406  {
407 // m_log.DebugFormat(
408 // "[INVENTORY FOLDER IMPL]: Returning item {0} {1}, OwnerPermissions {2:X}",
409 // item.Name, item.ID, item.CurrentPermissions);
410 
411  itemList.Add(item);
412  }
413  }
414 
415  //m_log.DebugFormat("[INVENTORY FOLDER IMPL]: Found {0} items", itemList.Count);
416 
417  return itemList;
418  }
419 
423  public List<InventoryFolderBase> RequestListOfFolders()
424  {
425  List<InventoryFolderBase> folderList = new List<InventoryFolderBase>();
426 
427  lock (m_childFolders)
428  {
429  foreach (InventoryFolderBase folder in m_childFolders.Values)
430  {
431  folderList.Add(folder);
432  }
433  }
434 
435  return folderList;
436  }
437 
438  public List<InventoryFolderImpl> RequestListOfFolderImpls()
439  {
440  List<InventoryFolderImpl> folderList = new List<InventoryFolderImpl>();
441 
442  lock (m_childFolders)
443  {
444  foreach (InventoryFolderImpl folder in m_childFolders.Values)
445  {
446  folderList.Add(folder);
447  }
448  }
449 
450  return folderList;
451  }
452 
457  public int TotalCount
458  {
459  get
460  {
461  int total = Items.Count;
462 
463  foreach (InventoryFolderImpl folder in m_childFolders.Values)
464  {
465  total = total + folder.TotalCount;
466  }
467 
468  return total;
469  }
470  }
471  }
472 }
List< InventoryFolderImpl > RequestListOfFolderImpls()
List< InventoryFolderBase > RequestListOfFolders()
Return a copy of the list of child folders in this folder. The folders themselves are the originals...
void Purge()
Delete all the folders and items in this folder.
bool ContainsChildFolder(UUID folderID)
Does this folder contain the given child folder?
InventoryFolderImpl FindFolder(UUID folderID)
Returns the folder requested if it is this folder or is a descendent of this folder. The search is depth first.
InventoryItemBase FindItemByPath(string path)
Find an item given a PATH_DELIMITOR delimited path starting from this folder.
InventoryFolderImpl(InventoryFolderBase folderbase)
InventoryFolderImpl CreateChildFolder(UUID folderID, string folderName, ushort type)
Create a new subfolder.
InventoryItemBase FindItem(UUID itemID)
Returns the item if it exists in this folder or in any of this folder's descendant folders ...
InventoryItemBase FindAsset(UUID assetID)
InventoryFolderImpl FindFolderForType(int type)
Look through all child subfolders for a folder marked as one for a particular asset type...
List< InventoryItemBase > RequestListOfItems()
Return a copy of the list of child items in this folder. The items themselves are the originals...
InventoryFolderImpl RemoveChildFolder(UUID folderID)
Removes the given child subfolder.
InventoryFolderImpl FindFolderByPath(string path)
Find a folder given a PATH_DELIMITER delimited path starting from this folder
Inventory Item - contains all the properties associated with an individual inventory piece...
virtual string Name
The name of the node (64 characters or less)
bool DeleteItem(UUID itemID)
Deletes an item if it exists in this folder or any children
InventoryFolderImpl GetChildFolder(UUID folderID)
Get a child folder
void AddChildFolder(InventoryFolderImpl folder)
Add a folder that already exists.